Linux - Rename files

Posted on mercoledì 10 novembre 2010 by Ivano Binetti

How to rename recursively files in all the filesystem

The simplest method that I have found to rename recursively a group of file is to use the "rename" linux bash command with a simple perl regular expression.
To rename all the file with extension containing the word "foo" I can use the following command:


$ rename s/foo/dog/ *foo*

To rename all the files with ".txt" extension you only need to specify *.txt instead of *foo*, as shown in the following example:

$ rename s/foo/dog/ *.txt 

The real problem occurs when you want to change recursively the file name into multiple directory and sub directory.
I usually  use this simple bash script to accomplish this task:


$ for dir in $(find /root/truepoint -type d); do cd $dir; rename s/foo/doog/ *.txt; done

Notice as you cannot use simply the command:

$ for var in $(find /root/truepoint -type d); do rename s/foo/doog/ $var; done

because:
  1. "rename" command rename all file matching a rule (2th argument) into the current directory
  2. "rename" 2th argument is the file typology and not a directory 
so you need to a command to change recursively the current directory.

Enjoy your Linux!

0 Responses to "Linux - Rename files":