Unix / Linux: Rename Multiple Files

Objective: Rename multiple files in a directory on Unix or Linux.

One of the easiest ways to rename multiple files on Unix or Linux is by using the rename command. This is typically a Perl script. Let’s say we have the following 5 files in a directory:

To rename the *.htm files to *.html, we can use the following syntax.

The first argument is a Perl regular expression. The files are renamed according to the rule specified in the regular expression. The files to be renamed are specified in the second argument. Note that the syntax below would also work if you want to rename *.htm files to *.html. It says match the end of string and append an ‘l‘.

Another command for renaming files is rename.ul. It takes in three arguments. To rename *.htm files to *.html, we can use the following syntax.

The first occurrence of .htm (1st argument) will be replaced with .html (2nd argument) for all *.htm files (3rd argument).

If both rename and rename.ul commands are not available, you can fallback to POSIX or bash shell for renaming files as long as it supports parameter substitution. To rename *.htm files to *.html using POSIX shell parameter substitution, we can use the following for loop to do the job.

Another utility for renaming files is mmv. To install mmv on a Debain based Linux distribution, use:

To rename *.htm files to *.html using mmv, use the following syntax.

The first parameter is the search pattern. mmv looks for any filenames ending with .htm. The asterisk is a wildcard and just as in most shells, it matches one or more arbitrary characters (You can also use a question mark to match just a single character). Note that you should escape asterisks and question marks to prevent your shell from expanding them. The second parameter is the target filename. Every wildcard that matched in the search pattern can be inserted here by writing a hash sign followed a number. The number is that of the wildcard, counting from the left: the first wildcard becomes #1, second #2 and so on. We have just one wildcard (the asterisk), so we write a #1.

The mmv tool is a lot more powerful. It can also perform uppercase and lowercase conversion. To rename *.htm files to *.html and convert file names to uppercase or lowercase, use the following syntax.

The #u1 converts the matched wildcard to uppercase and #l1 converts the wildcard to lowercase.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }