How to rename files/directories to uppercase/lowercase character names How to rename files/directories to upper case/lower case character names, convert the files names to lower case, convert the directories and all files in the subdirectories to upper case, convert the files names to upper case, convert the directories and all files in the subdirectories to upper case
I have created in my current working directory four files (A, B, C, D) and four directories (AA, BB, CC, DD) as follows:
$ ls -l total 16 -rw------- 1 root wheel 0 Jun 17 10:29 A drwx------ 2 root wheel 512 Jun 17 10:29 AA -rw------- 1 root wheel 0 Jun 17 10:29 B drwx------ 2 root wheel 512 Jun 17 10:29 BB -rw------- 1 root wheel 0 Jun 17 10:29 C drwx------ 2 root wheel 512 Jun 17 10:29 CC -rw------- 1 root wheel 0 Jun 17 10:29 D drwx------ 2 root wheel 512 Jun 17 10:29 DD
Now let's play a little with these upper-case names files and directories.
1) Rename all directories to lower case names
$ for i in `find . -type d`; do new_name=`echo $i | tr '[A-Z]' '[a-z]'`; mv $i $new_name ; done $ ls -l total 16 -rw------- 1 root wheel 0 Jun 17 10:29 A -rw------- 1 root wheel 0 Jun 17 10:29 B -rw------- 1 root wheel 0 Jun 17 10:29 C -rw------- 1 root wheel 0 Jun 17 10:29 D drwx------ 2 root wheel 512 Jun 17 10:29 aa drwx------ 2 root wheel 512 Jun 17 10:29 bb drwx------ 2 root wheel 512 Jun 17 10:29 cc drwx------ 2 root wheel 512 Jun 17 10:29 dd
Above we see directories renamed in lower cased names.
2) Rename all files to lower case names
$ for i in `find . -type f`; do new_name=`echo $i | tr '[A-Z]' '[a-z]'`; mv $i $new_name ; done $ ls -l total 16 -rw------- 1 root wheel 0 Jun 17 10:29 a drwx------ 2 root wheel 512 Jun 17 10:29 aa -rw------- 1 root wheel 0 Jun 17 10:29 b drwx------ 2 root wheel 512 Jun 17 10:29 bb -rw------- 1 root wheel 0 Jun 17 10:29 c drwx------ 2 root wheel 512 Jun 17 10:29 cc -rw------- 1 root wheel 0 Jun 17 10:29 d drwx------ 2 root wheel 512 Jun 17 10:29 dd
All files have been renamed to lower cased names.
3) Rename all files and directories to upper case names
$ for i in `find .`; do new_name=`echo $i | tr '[a-z]' '[A-Z]'`; mv $i $new_name ; done ls -l total 16 -rw------- 1 root wheel 0 Jun 17 10:29 A drwx------ 2 root wheel 512 Jun 17 10:29 AA -rw------- 1 root wheel 0 Jun 17 10:29 B drwx------ 2 root wheel 512 Jun 17 10:29 BB -rw------- 1 root wheel 0 Jun 17 10:29 C drwx------ 2 root wheel 512 Jun 17 10:29 CC -rw------- 1 root wheel 0 Jun 17 10:29 D drwx------ 2 root wheel 512 Jun 17 10:29 DD
All files and directories have been renamed to upper cased names.
Below there's a little script that converts all upper cased files&(sub)directories to lower cased names in a path:
#!/bin/sh DIR=/where/to/find/and/replace for i in `find $DIR` do new_name=`echo $i | tr '[A-Z]' '[a-z]'` mv $i $new_name done
To make this little script more interesting you should play a little with the find parameters (-name, -inum -mtime -ctime, etc.). For example, if you need to rename only .EXE types of files to lower cased names, you should use find . -name *.EXE.
Hello, Guest ! You can Login or Register to www.ivorde.ro!
Post comment:
4 comment(s) to How to rename files/directories to uppercase/lowercase character names:
1. Re: How to rename files/directories to uppercase/lowercase character names
Instructor by Rich
at August 03rd, 2011 - 21:42
I have looked at several web sites trying to find a simple way of renaming folder/files from uppercase to lower. GREAT job on the examples above!!!!
2. Re: How to rename files/directories to uppercase/lowercase character names
one more thing to replacing names by Andrei
at September 26th, 2008 - 15:53
Be careful to test it with files that contain metacharacters or spaces in their names. Probably will not work with those files.
3. Re: How to rename files/directories to uppercase/lowercase character names
Possible, but dangerous by Andrei
at September 26th, 2008 - 15:51
cd to the directory you want and use find piped to xargs: Something like the following command should rename replacing a-z with A-Z only in files (not directories) in your curent directory and subdirectories:
for i in `find . -type f`; do dir=`dirname $i`; file=`basename $i`; newfile=`echo $file | tr '[a-z]' '[A-Z]'`; mv "${dir}/${file}" "${dir}/${newfile}" ; done
Doing something like this with subdirectories from a simple script I find it a little hard (because the for loop uses subdirectories names in initial find results and changes it on the way, resulting in Xth loop not finding initial directories because their base directories were renamed in X-t loop). Hope you understand this. Contact me from my contact form if you have further questions. P.S.: MAKE SURE TO TEST IT first !! I offer no waranty to this command.
4. Re: How to rename files/directories to uppercase/lowercase character names
Unix Learner by Pollyanna
at August 17th, 2008 - 19:34
Is it possible to modify the script to recursively rename all sub-directories and it's files to upper case names?
Designed and developed by Andrei Manescu. Optimized for Mozilla Firefox.
Copyright 2007 Andrei Manescu
All trademarks and copyrights on this page are owned by their respective owners. Comments are owned by those who posted them.