Rename multiple directories in the command line
I have some folders called:
Session 1
Session 2
Session 3
Session 4
Session 5
Session 6
Session 7
Session 8
I would like to rename them all to:
Folder 1
Folder 2
Folder 3
Folder 4
Folder 5
Folder 6
Folder 7
Folder 8
How would I go about doing this in the terminal in the most effective way?
I know about mmv
mv
and rename
but not sure what I should use and how to do it.
prename
Simplest way would be to use rename
or prename
, which is a Perl script ( if you're a ksh
or mksh
user, that shell has rename
built-in function, which is different, so for the sake of consistency, I'll use prename
when referring to that Perl script; alternatively you could call /usr/bin/rename
- full path to the executable).
$ ls
Session 1/ Session 2/ Session 3/ Session 4/ Session 5/ Session 6/ Session 7/ Session 8/
$ prename 's/Session/Folder/' Session*/
$ ls
Folder 1/ Folder 2/ Folder 3/ Folder 4/ Folder 5/ Folder 6/ Folder 7/ Folder 8/
If you need recursive search or ensure that you find the right type of item ( maybe you also have files with word "Session" in the filename) you can combine that with find
utility:
$ ls
Folder 1/ Folder 2/ Folder 3/ Folder 4/ Folder 5/ Folder 6/ Folder 7/ Folder 8/
$ find -maxdepth 1 -type d -name "Session *" -exec prename 's/Session/Folder/' {} \;
$ ls
Folder 1/ Folder 2/ Folder 3/ Folder 4/ Folder 5/ Folder 6/ Folder 7/ Folder 8/
Slightly lengthy, maybe slightly redundant, but works.
mv
The small problem with mv
is that we need to alter the name of the destination each time, so by itself it can't do what we want. To do that, we'd have to combine it with some other tools, such as find
or bash
's tools.
$ ls
Session 1/ Session 2/ Session 3/ Session 4/ Session 5/ Session 6/ Session 7/ Session 8/
$ for dir in Session*/ ; do mv "${dir}" "Folder ${dir##*\ }" ;done
$ ls
Folder 1/ Folder 2/ Folder 3/ Folder 4/ Folder 5/ Folder 6/ Folder 7/ Folder 8/
What you see here is that we're looping over all items that contain the word Session
in them and are directory. We use parameter expansion `${dir##*\ }" to extract everything after space in directory's name ( which is the respective number), and form new string "Folder /".
In both prename
commands and mv
we're using globbing, which means these approaches will rename every directory that contains the word "Session" in them, even "Session blah". Not ideal, of course, but for the specific case where you know your folder naming is consistent, that'll work. Alternatively, we could augment the command with for dir in Session\ [1-9] ; do . . .done
.
In other words,this approach can work, but is very simplistic and isn't the best.
Python
Of course, other tools can be used as well. For instance, Python:
$ ls
Session 1/ Session 3/ Session 5/ Session 7/
Session 2/ Session 4/ Session 6/ Session 8/
$ python -c 'import os,shutil;map(lambda x:shutil.move(x,x.replace("Session","Folder")),os.listdir("."))'
$ ls
Folder 1/ Folder 2/ Folder 3/ Folder 4/ Folder 5/ Folder 6/ Folder 7/ Folder 8/
Go to the folder that contens all the folder you want to rename and do:
find . * | rename 's\Session\Folder\'