I have multiple files in multiple directories and i have to rename these files from lowercase to uppercase; the file extension may vary and needs to be in lowercase (should be renamed too for files with extensions in uppercase).
NB: I have rename
version from util-linux
on CentOS Linux7.
i tried this :
find /mydir -depth | xargs -n 1 rename -v 's/(.*)/([^/]*)/$1/U$2/' {} ;
find /mydir -depth | xargs -n 1 rename -v 's/(.*)/([^/]*)/$2/L$2/' {} ;
but it’s not working it changes nothing and i have no output.
Itried another solution :
for SRC in `find my_root_dir -depth`
do
DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
if [ "${SRC}" != "${DST}" ]
then
[ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
fi
done
this one partially works but transforms the files extensions to uppercase too.
Any suggestions on how to keep/transform the extensions to lowercase ?
Thank you!
3
Answers
rename
-independent solution (usingfind
together withmv
)You can rename all files in a directory with a following command:
First part (
for i in $( ls | grep [A-Z] );
) looks for all uppercase characters and executes until all files are "scanned".Second part (“) turns all uppercase characters into lowercase ones.
Perl-based
rename
dependent solutionThis command changes uppercase characters to the lowercase ones.
-f
option allows overwriting of existing files, but it is not necessary.Possible solution with Perl
rename
:The commands in the question have several problems.
You seem to confuse the syntax of
find
‘s-exec
action andxargs
.The
xargs
version has problems in case a file name contains a space.If you replace
;
with+
, multiple file names are passed to one invocation ofrename
.The substitution command is only supported by the Perl version of the
rename
command. You might have to install this version. See Get the Perl rename utility instead of the built-in renameThe substitution did not work in my test. I successfully used
The first group
(.*/)?
optionally matches a sequence of characters with a trailing/
. This is used to copy the directory unchanged.The second group
([^.]*)
matches a sequence of characters except.
.This is the file name part before the first dot (if any) which will be converted to uppercase. In case the file name has more than one extension, all will remain unchanged, e.g.
Path/To/Foo.Bar.Baz
->Path/To/FOO.Bar.Baz
suggesting a trick with
awk
that will generate all requiredmv
commands:Inspect the result, and run all
mv
commands together:awk
scriptscript.awk
explanationTesting: