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.
You can use this below.
For renaming recursively I use the following commands:
You can use
find
to find all matching files recursively:EDIT: what the
'{}'
and\;
are?The
-exec
argument makes find executerename
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:
If you just want to rename and don't mind using an external tool, then you can use rnm. The command would be:
-dp -1
will make it recursive to all subdirectories.-fo
implies file only mode.-ssf '_dbg'
searches for files with _dbg in the filename.-rs '/_dbg//'
replaces _dbg with empty string.You can run the above command with the path of the CURRENT_FOLDER too:
with bash:
classic solution: