I have for example TREE.wav, ONE.WAV. I want to rename it to tree.wav, one.wav. How do I rename all files to lowercase?
If you're comfortable with the terminal:
- Open Terminal.app, type
cd
and then drag and drop the Folder containing the files to be renamed into the window. - To confirm you're in the correct directory, type
ls
and hit enter. Paste this code and hit enter:
for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
- To confirm that all your files are lowercased, type
ls
and hit enter again.
(Thanks to @bavarious on twitter for a few fixes, and thanks to John Whitley below for making this safer on case-insensitive filesystems.)
foo.txt
and FOO.TXT
, this could clobber one of them. –
Mccall echo "Test 1" >> test1; echo "Test 2" >> test2; mv test1 test2; cat test2
–
Nonresistant mv "$f" "${f,,}"
, or declare -l g=$f; mv "$f" "$g"
–
Fontenot echo $f | sed -e 's, ,_,g'
"; done found on this page: chrislongcreativeservices.com/… –
Telegraphy for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
–
Fungi *
to **
will make this recurse but I can't test it right now. –
Nonresistant test
and TEST
in the same directory around the time this question was answered (7 years ago). –
Nonresistant for f in *
to for f in
find . | grep '/';
. This works in concept, but may rename subdirectories first which won't work on case-sensitive filesystems. Running the command N times will work for N-deep structures though. –
Nonresistant for f in */**; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
–
Monstrous The question as-asked is general, and also important, so I wish to provide a more general answer:
Simplest case (safe most of the time, and on Mac OS X, but read on):
for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; done
You need to also handle spaces in filenames (any OS):
IFS=$'\n' ; for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; done
You need to safely handle filenames that differ only by case in a case-sensitive filesystem and not overwrite the target (e.g. Linux):
for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; [ -e "$j" ] && continue ; mv "$i" "$j" ; done
Note about Mac OS X:
Mac's filesystem is case-insensitive, case-preserving.
There is, however, no need to create temporary files, as suggested in the accepted answer and comments, because two filenames that differ only by case cannot exist in the first place, ref.
To show this:
$ mkdir test
$ cd test
$ touch X x
$ ls -l
total 0
-rw-r--r-- 1 alexharvey wheel 0 26 Sep 20:20 X
$ mv X x
$ ls -l
total 0
-rw-r--r-- 1 alexharvey wheel 0 26 Sep 20:20 x
A fish shell version:
for old in *
set new (echo $old | tr '[A-Z]' '[a-z]')
mv $old $new
end
For those wanting to lowercase all files in the current directory and sub-directories:
# lower case all files in current dir & subdirs
for d in ./**/ ; do (cd "$d" && for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done); done); done
#list all directories
for f in ./**/ ; do echo $f; done
# lower case all files in a directory
for x in ./*/ ; do (cd "$x" && for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done); done
-bash: cd: ./*/: No such file or directory
–
Housewifery A simple solution is by using tr
command within a for loop.
for file in ./* ; do
mv "$file" "$(tr '[:upper:]' '[:lower:]' <<<"$file")"
done
You can learn more about tr
command here description of tr command
i think this is the best solution.
for i in *; do mv "$i" "${i,,}"; done
easier, shorter, neater
© 2022 - 2024 — McMap. All rights reserved.