i have bunch of files that needs to be renamed.
file1.txt needs to be renamed to file1_file1.txt
file2.avi needs to be renamed to file2_file2.avi
as you can see i need the _ folowed by the original file name.
there are lot of these files.
i have bunch of files that needs to be renamed.
file1.txt needs to be renamed to file1_file1.txt
file2.avi needs to be renamed to file2_file2.avi
as you can see i need the _ folowed by the original file name.
there are lot of these files.
So far all the answers given either:
These two scripts solve all of those problems.
Bash 2.X/3.X
Bash 4.X
Be sure to remove the
echo
from whichever script you choose once you are satisfied with it's output and run it againEdit
Fixed problem in previous version that did not properly handle path names.
For your specific case, you want to use
mmv
as follows:You need to be aware that the wildcard matching is not greedy. That means that the file
a.b.txt
will be turned intoa_a.b.txt
, nota.b_a.b.txt
.The
mmv
program was installed as part of my CygWin but I had toon my Ubuntu box to get it down. If it's not in you standard distribution, whatever package manager you're using will hopefully have it available.
If, for some reason, you're not permitted to install it, you'll have to use one of the other
bash
for
-loop-type solutions shown in the other answers. I prefer the terseness ofmmv
myself but you may not have the option.This one won't rename sub directories, only regular files.
Idea for recursion
I like the PERL cookbook's rename script for this. It may not be /bin/sh but you can do regular expression-like renames.
The /bin/sh method would be to use sed/cut/awk to alter each filename inside a for loop. If the directory is large you'd need to rely on xargs.