Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find multiple files and rename them in Linux

I am having files like a_dbg.txt, b_dbg.txt ... in a Suse 10 system. I want to write a bash shell script which should rename these files by removing "_dbg" from them.

Google suggested me to use rename command. So I executed the command rename _dbg.txt .txt *dbg* on the CURRENT_FOLDER

My actual CURRENT_FOLDER contains the below files.

CURRENT_FOLDER/a_dbg.txt CURRENT_FOLDER/b_dbg.txt CURRENT_FOLDER/XX/c_dbg.txt CURRENT_FOLDER/YY/d_dbg.txt 

After executing the rename command,

CURRENT_FOLDER/a.txt CURRENT_FOLDER/b.txt CURRENT_FOLDER/XX/c_dbg.txt CURRENT_FOLDER/YY/d_dbg.txt 

Its not doing recursively, how to make this command to rename files in all subdirectories. Like XX and YY I will be having so many subdirectories which name is unpredictable. And also my CURRENT_FOLDER will be having some other files also.

like image 393
rashok Avatar asked May 14 '13 11:05

rashok


People also ask

How do I rename multiple files at once in Linux?

You can also use the find command, along with -exec option or xargs command to rename multiple files at once. This command will append . bak to every file that begins with the pattern “file”. This command uses find and the -exec option to append “_backup” to all files that end in the .

How do you rename a group of files in Linux?

To rename a group of files with a single command, use the rename command. It requires the use of regular expressions and can tell you what changes will be made before making them. For decades, Linux users have been renaming files with the mv command. It's easy, and the command does just what you expect.

How do you rename multiple files in Unix?

Rename Multiple Files with the mv Command On its own, the mv command renames a single file. However, combining it with other commands allows you to rename multiple files at the same time. Using this syntax, the find command defines an element of the current file name as the search parameter.


2 Answers

You can use find to find all matching files recursively:

$ find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' \; 

EDIT: what the '{}' and \; are?

The -exec argument makes find execute rename for every matching file found. '{}' will be replaced with the path name of the file. The last token, \; is there only to mark the end of the exec expression.

All that is described nicely in the man page for find:

 -exec utility [argument ...] ;          True if the program named utility returns a zero value as its          exit status.  Optional arguments may be passed to the utility.          The expression must be terminated by a semicolon (``;'').  If you          invoke find from a shell you may need to quote the semicolon if          the shell would otherwise treat it as a control operator.  If the          string ``{}'' appears anywhere in the utility name or the argu-          ments it is replaced by the pathname of the current file.          Utility will be executed from the directory from which find was          executed.  Utility and arguments are not subject to the further          expansion of shell patterns and constructs. 
like image 151
kamituel Avatar answered Sep 28 '22 08:09

kamituel


For renaming recursively I use the following commands:

find -iname \*.* | rename -v "s/ /-/g" 
like image 36
Gantan Avatar answered Sep 28 '22 09:09

Gantan