I have several files with a.dat
, a.txt
, a.mp3
, b.dat
, b.txt
, b.mp3
, b.zip
, b.rar
, c.mp3
and so on. I want to rename all files with basename "a" to basename "x".
Such that files become x.dat
, x.txt
, x.mp3
, b.dat,
b.txt,
b.mp3,
b.zip,
b.rar,
c.mp3` and so on.
In Linux this can be done via terminal but requires lot of typing. I want a script to do the task for me.
I ll suggest a way, I think this could work. This is a little weird way I think, don't laugh. 10th std math.
First you grep all the names in the folder using the combination of ls and grep command
ls | grep ^a this will list you with all the files with a as the first letter. You can use a regular expression with this if you need only files with a as the name.
Read the file names one by one with a while loop
Store the file name into a variable (say $name1).And using sed and awk, extract the second part of the filename(ie. remove the dots into spaces and print the second coloumn) store this in another variable (say $extn).
you can rename the files using the first name stored in the variable ($name1) for specifying which file and use the second variable to specify the extension ($extn) for the new name...
This is a loooong route :) I am sure this will work. Try it. Consider this as the algorithm and script.I am sorry that I dint provide a script.Little bit lazy.
There is a small program called
mmv
which does the job:mmv
comes with typically any Linux distribution.You don't need a script when you have the
rename
(orprename
on some systems) command.It allows groups of files to be renamed using arbitrarily complex Perl regular expressions: