Menu

Finding multiple files and rename them in Linux

Scenario :

we have files under different folders under parent directory /data

file names are like 1.abc.txt , 2.abc.txt , 3.abc.txt
we need to rename file files by removing .abc and making files as 1.txt only.
You can use find to find all matching files recursively:

#find . -iname "*abc*" -exec rename _abc.txt .txt '{}' \;

Deatils: 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.

END.

Loading

Categories:   Linux, Middleware

Tags:  , , ,

Comments