How do I easily rename multiple files using command line?

Jiew Meng

One of the ways I quickly rename files in Windows is

F2 > Rename > Tab (to next file) > Rename ...

But in Ubuntu/Nautilus, I can't tab to next file. But being on Linux, I think there must be a command line alternative.

However, sometimes, I may want more control over how to rename specific files. In that case, perhaps its better to be able to tab to the next file

Matt

I use rename all the time. It is pretty simple, but hopefully you know basic regex:

rename "s/SEARCH/REPLACE/g"  *

This will replace the string SEARCH with REPLACE in every file (that is, *). The /g means global, so if you had a SEARCH_SEARCH.jpg, it would be renamed REPLACE_REPLACE.jpg. If you didn't have /g, it would have only done substitution once, and thus now named REPLACE_SEARCH.jpg. If you want case-insensitive, add /i (that would be, /gi or /ig at the end).

With regular expressions, you can do lots more.

Note that this rename is the prename (aka Perl rename) command, which supports complete Perl regular expressions. There is another rename which uses patterns, and is not as powerful. prename used to be installed by default on Ubuntu (along with Perl), but now you may have to do:

sudo apt install rename

Here are a few examples:

Prefix

Add:

rename 's/^/MyPrefix_/' * 
  • document.pdf renamed to MyPrefix_document.pdf

Remove:

Also you can remove unwanted strings. Let's say you had 20 MP3 files named like CD RIP 01 Song.mp3 and you wanted to remove the "CD RIP" part, and you wanted to remove that from all of them with one command.

rename 's/^CD RIP //' *
  • CD RIP 01 Song.mp3 to 01 Song.mp3

Notice the extra space in '^CD RIP ', without the space all files would have a space as the first character of the file. Also note, this will work without the ^ character, but would match CD RIP  in any part of the filename. The ^ guarantees it only removes the characters if they are the beginning of the file.

Suffix

Add:

rename 's/$/_MySuffix/' *
  • document.pdf renamed to document.pdf_MySuffix

Change:

rename 's/\.pdf$/.doc/' *

will change Something.pdf into Something.doc. (The reason for the backslash is, . is a wildcard character in regexp so .pdf matches qPDF whereas \.pdf only matches the exact string .pdf. Also very important to note, if you are not familiar with BASH, you must put backslashes in SINGLE quotes! You may not omit quotes or use double quotes, or bash will try to translate them. To bash \. and "\." equals .. (But double-quotes and backslashes are used, for example "\n" for a newline, but since "\." isn't a valid back escape sequence, it translates into .)

Actually, you can even enclose the parts of the string in quotes instead of the whole: 's/Search/Replace/g' is the same as s/'Search'/'Replace'/g and s/Search/Replace/g to BASH. You just have to be careful about special characters (and spaces).


I suggest using the -n option when you are not positive you have the correct regular expressions. It shows what would be renamed, then exits without doing it. For example:

rename -n s/'One'/'Two'/g *

This will list all changes it would have made, had you not put the -n flag there. If it looks good, press Up to go back, then erase the -n and press Enter (or replace it with -v to output all changes it makes).

Note: Ubuntu versions above 17.04 don't ship with rename by default, however it's still available in the repositories. Use sudo apt install rename to install it

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I easily rename multiple files using command line?

From Dev

How to script the rename of multiple files using the Mac command line

From Dev

how to rename files using command line

From Dev

How do I zip up multiple files on command line?

From Dev

How do I compile and link multiple files from the command line?

From Dev

How do I FTP multiple files from the command line?

From Dev

How do I upload files using the command line on Windows?

From Dev

How would I batch rename a lot of files using command-line?

From Dev

How do I batch rename a specific file inside multiple zip archives via the command line?

From Dev

Rename multiple files in sequence through command line

From Dev

How do I insert the same line into multiple files using VIM

From Dev

How do I rename a directory via the command line?

From Dev

How do I rename a bunch of files in the Command Prompt?

From Dev

How to rename files in bulk from command line?

From Dev

How do I run a command on multiple files

From Dev

Rename multiple files using windows command prompt

From Dev

How do I rename multiple files that start with "Image"?

From Dev

How do I rename multiple files by removing everything but numbers?

From Dev

How to rename multiple files with a command or script

From Dev

How do I easily download Ubuntu ISOs via bittorrent from the command line?

From Dev

How to rename files pulled from a list of files using a single command or line

From Dev

How do I rename files with spaces using the Linux shell?

From Dev

How do I rename files using the directory name?

From Dev

How do I convert .doc files to .txt using LibreOffice from the command line?

From Dev

How do I move all files from one folder to a subfolder except .html file using the command line?

From Dev

How do I copy files from the source with different dates than the destination using the Command Line

From Dev

how can I print multiple odt files from the command line?

From Dev

Rename files with one line command

From Dev

Rename Files Mac Command Line

Related Related

  1. 1

    How do I easily rename multiple files using command line?

  2. 2

    How to script the rename of multiple files using the Mac command line

  3. 3

    how to rename files using command line

  4. 4

    How do I zip up multiple files on command line?

  5. 5

    How do I compile and link multiple files from the command line?

  6. 6

    How do I FTP multiple files from the command line?

  7. 7

    How do I upload files using the command line on Windows?

  8. 8

    How would I batch rename a lot of files using command-line?

  9. 9

    How do I batch rename a specific file inside multiple zip archives via the command line?

  10. 10

    Rename multiple files in sequence through command line

  11. 11

    How do I insert the same line into multiple files using VIM

  12. 12

    How do I rename a directory via the command line?

  13. 13

    How do I rename a bunch of files in the Command Prompt?

  14. 14

    How to rename files in bulk from command line?

  15. 15

    How do I run a command on multiple files

  16. 16

    Rename multiple files using windows command prompt

  17. 17

    How do I rename multiple files that start with "Image"?

  18. 18

    How do I rename multiple files by removing everything but numbers?

  19. 19

    How to rename multiple files with a command or script

  20. 20

    How do I easily download Ubuntu ISOs via bittorrent from the command line?

  21. 21

    How to rename files pulled from a list of files using a single command or line

  22. 22

    How do I rename files with spaces using the Linux shell?

  23. 23

    How do I rename files using the directory name?

  24. 24

    How do I convert .doc files to .txt using LibreOffice from the command line?

  25. 25

    How do I move all files from one folder to a subfolder except .html file using the command line?

  26. 26

    How do I copy files from the source with different dates than the destination using the Command Line

  27. 27

    how can I print multiple odt files from the command line?

  28. 28

    Rename files with one line command

  29. 29

    Rename Files Mac Command Line

HotTag

Archive