LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to change file names to uppercase in a directory (http://www.linuxquestions.org/questions/linux-general-1/how-to-change-file-names-to-uppercase-in-a-directory-409508/)

konramesh 01-30-2006 12:23 AM

how to change file names to uppercase in a directory
 
All my files are having names in lower case. I wanted to change these names to upper case. I am using red hat linux version 8. How can i change all the file names to upper case in a directory?

pljvaldez 01-30-2006 01:47 AM

Put this script named "ucf" in /usr/local/bin and, as root, type "chmod a+x ucf".

Code:

#!/bin/sh
# uppercase any filenames with lowercase chars
for file in $*
 do
 if [ -f $file ]
 then
  ucfile=`echo $file | tr [:lower:] [:upper:]`
  if [ $file != $ucfile ]
  then
    mv -i $file $ucfile
  fi   
 fi
done

Then you can change all lowercase filenames in the current directory to uppercase by typing "ucf *"

phidor 06-16-2009 01:04 AM

UPPERCASE to lowercase
 
Can I please ask how this useful script could be modified to change not only the files of a specific directory but also the files found in its subdirectories? If you could do this I would be very grateful.

Thank you. Phidor

ghostdog74 06-16-2009 01:32 AM

if you have Python and don't mind using an already made script, you can try my script called File Renamer(see my sig)
eg usage
Code:

# ls -ltR
-rw-r--r-- 1 root root    0 Jun 16 14:29 file1.Doc
-rw-r--r-- 1 root root    0 Jun 16 14:29 testFile.TXT
drwxr-xr-x 2 root root 4096 Jun 16 14:28 test

./test:
-rw-r--r-- 1 root root 0 Jun 16 14:28 FILE.PDF
-rw-r--r-- 1 root root 0 Jun 16 14:28 File.TIff
-rw-r--r-- 1 root root 0 Jun 16 14:28 file.txt

# filerenamer.py -d 2 -p ".*" -e "a-z" -l "*"
==>>>>  [ /home/test/testFile.TXT ]==>[ /home/test/images/testfile.txt ]
==>>>>  [ /home/test/images/file1.Doc ]==>[ /home/test/images/file1.doc ]
==>>>>  [ /home/test/images/test/FILE.PDF ]==>[ /home/test/images/test/file.pdf ]
==>>>>  [ /home/test/images/test/File.TIff ]==>[ /home/test/images/test/file.tiff ]

-d option means depth level (set to 2 levels deep, so change as appropriate). remove -l option to commit changes


if you don't have Python, then just use normal GNU find
Code:

find /home/test/ -type f -name "*[A-Z]*" -printf "%h:%f\n" | awk 'BEGIN{q="\047";FS=":"}
{
 newpath = $1"/"tolower($2)
 cmd = "mv "q $1"/"$2 q" "q newpath q
 system(cmd)
}
'


phidor 06-16-2009 06:13 AM

Great one! Tried the latter (ie without Python) and it ran like clockwork - first pop. Thanks so much. It was an academic exercise more than a practical necessity - I wanted to see if it could be done. It can! Thank you for your help. I'll now sit down and nut out the code!

Thanks again for such a prompt response!

Phidor.


All times are GMT -5. The time now is 06:50 AM.