Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Rename Multiple Files on Linux

#1
Debian-based Operating Systems - Renaming Multiple Files on Linux

Debian and similar distributions such as Ubuntu and Linux Mint have a built in command line tool for batch renaming files. ‘rename’, offered on some Debian-based OSes, is one of the most powerful renaming tools on Linux. Rename works very similar to the way ‘sed’ works – it is essentially the sed utility for filenames. It can also use Perl expressions, so if you have some knowledge of Perl, this utility will work wonders for you.

Important: Some Debian-based operating systems that have ‘rename’ installed might actually have a version of ‘rename’ from the ‘util-linux’ package. This util-linux variant is a much simpler version of ‘rename’ which doesn’t have nearly as many features as the version that comes on Debian and Ubuntu.
Basic File Renaming

You can find and replace text in filenames by executing the following command. Make sure to change the values of ‘search’ and ‘replace’ to the strings of text you wish to affect. Keep in mind that wildcards are available inside of the search field. Note: You can use the -n option to perform a ‘dry run’ first, which does not affect your file names. The -v option makes all operations verbose.

Example:

Code:
rename -v 's/search/replace/' *


Changing filetypes

You can also rename the filetype of certain files by using this method – just replace ‘search’ and ‘replace’ with the current and new filetype. Note: Don’t forget to add a backslash before your filetype, or else the command won’t work.

Example:

Code:
rename -v 's/\.search/\.replace/' *


More Advanced Commands

If you would like to rename files and have a number incrementing at the end of the filename, this can be done as well with some Perl:

Code:
rename -v 's/search/our $i; sprintf("replace%03d.txt", 1+$i++)/e' *


The reason for the ‘%03d’ in this command is to describe how many zeroes we want in front of our numbers. This is used to keep files in order when sorting files alphabetically. ‘sprintf’ is actually a function from Perl, used to format the name. This shows one of the strengths of this version of rename. Here is an example.

Files:

file1

file2

Command:

Code:
rename -v 's/file*/our $i; sprintf("name%03d.txt", 1+$i++)/e' *


Output:

name001

name002

And in case you need a specific format for your filenames, there are plenty of Perl scripts available online for renaming files in all sorts of formats.


The
Code:
rename
syntax looks like this:
rename (option) 's/oldname/newname' file1.ext file24.ext
The letter “s” stands for “substitute” and it’s the main part of the regular expression. Single quotes around it are obligatory. Available options are:
  • -v
  • (verbose; prints the list of renamed files along with their new names)
    -n
  • (“no action”; a test mode or simulation which only shows the files that will be changed without touching them)
    -f
  • (a forced overwrite of the original files)
The
Code:
rename
command also accepts wildcards to rename multiple files of the same type, and it works on file extensions as well. For example, this would change all files with the extension .jpeg to .jpg:
rename 's\.jpeg/\.jpg/' *
The wildcard symbol (*) means that all files in the folder will be affected.
The regular expression also has its own options (modifiers): “g” (global; affects all occurrences of the expression) and “i” (performs case-insensitive substitution). They are written at the end of the expression, just before the closing single quote, and can be combined:
rename -n 's/DSC/photo/gi' *.jpg
This would apply to all .jpg files that contain “DSC”, “dSC”, “dsc”… and change that part of the filename to “photo”. However, because of the “-n” option, the command wouldn’t actually rename the files but just print them in the console window.
Substitution is not the only thing that this regular expression can do. There’s also translation – marked by the letter “y” – which can transform the filenames on a more complex level. It is most often used to change the filename case:
rename 'y/a-z/A-Z/' *.jpg
This would change the names of all .jpg files from lowercase to uppercase. To do it vice-versa, just switch the “oldname” and “newname” parts of the regular expression.
Using the
Code:
rename
command boils down to mixing a few basic patterns to achieve the desired result. Thanks to the “-n” option the users will never have to put their files at risk (or their nerves at stake), since it offers a safe and useful preview of what the renamed files will look like.




Another quick one i found that works a little differently.


Code:
ls | cat -n | while read n f; do mv "$f" "file-$n.jpg"; done

ls lists the files in the current directory and cat -n adds line numbers. The while loop reads the resulting numbered list of files line by line, stores the line number in the variable n and the filename in the variable f and performs the rename.
[Image: 8OHl5AB.png]
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016