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?
-
1Are you comfortable with a Terminal/shell solution? Or do you want C/Objective-C code to accomplish that?– user557219Oct 16, 2011 at 20:22
-
Terminal solution will be ok.– Voloda2Oct 16, 2011 at 20:27
-
3I think this should be reopened. terminal code is code as well– DanieldOct 13, 2016 at 12:52
-
1This should be a valid question on stackoverflow. This is also code.– adevAug 27, 2017 at 6:06
4 Answers
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.)
-
10Be careful. If you have files named
foo.txt
andFOO.TXT
, this could clobber one of them. Oct 16, 2011 at 21:10 -
1With bash, you can just do:
mv "$f" "${f,,}"
, ordeclare -l g=$f; mv "$f" "$g"
Oct 16, 2011 at 23:27 -
2i tried all these commands, i get "mv: ‘PPP.txt’ and ‘ppp.txt’ are the same file" and it doesnt lower case my file... any ideas? Is it because its in the same directory?– lorlessAug 8, 2013 at 16:45
-
2user2066039: This must must be a recent thing on the Mac, because most answers older than a year don't account for it. I accomplished the task by using an intermediate extension like 'jpg1'. So, JPG -> jpg1 -> jpg. Hope that helps.– JoyceNov 1, 2013 at 17:42
-
18OS X users should rename each file to a temporary name first due to the case-insensitive filesystem, e.g.:
for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done
Mar 12, 2015 at 23:00
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
-
Yes - but your example suggests that you can lowercase filenames using 'mv X x'. So it was only that I commented on.– cpaludanJun 24, 2019 at 13:14
-
mojave 10.14.5 : # touch X x # ls -la total 0 -rw-r--r-- 1 root root 0 Jun 24 15:19 X # mv X x mv: 'X' and 'x' are the same file # ls -la -rw-r--r-- 1 root root 0 Jun 24 15:19 X– cpaludanJun 24, 2019 at 13:20
-
-
1And my bad - was in a ssh on my raspberry on a shared drive (hfsplus formatted) - there it doesn't work. But locally on my mac it does.– cpaludanJun 24, 2019 at 13:47
-
for i in * ; do j=$(tr '[:upper:]' '[:lower:]' <<< "$i") ; mv "$i" "$j" ; worked for me at catalina. Thanks. May 23, 2020 at 1:14
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