7

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.

17

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/
4
  • 1
    Great, I used the first solution and it does exactly what I wanted. Is there any difference between using prename and rename?
    – Rtsne42
    Feb 11 '17 at 15:01
  • Nevermind, I see the update.
    – Rtsne42
    Feb 11 '17 at 15:05
  • @Danchm01 I was in the process of editing my answer, when you posted the comment, and the edit addresses exactly that. Basically, I'd prefer prename because if you're using Korn shell or its derivative instead of bash , there is a shell built in command named rename , so the syntax for prename wont work there and you'll have to explicitly call /usr/bin/rename to make it work. Feb 11 '17 at 15:05
  • 1
    If you cannot use python2, then this works python3 -c 'import os,shutil;list(map(lambda x:shutil.move(x,x.replace("Session","Folder")),os.listdir(".")))' Aug 12 '18 at 18:06
1

Go to the folder that contens all the folder you want to rename and do:

find . * | rename 's\Session\Folder\'
1
  • rename's syntax is with slash, not back slash. This piping will not work. one needs find . * -exec rename 's/Session/Folder/' {} \; Nov 3 '17 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.