How to Rename Files and Directories.

How To Rename Files And Directories.

In this tutorial, we will show you how to use the mv and rename commands to rename files and directories.

 

Renaming Files with the mv Command

The mv command (short of move) is used to rename or move files from one location to another. The syntax for the mv command is as follows:

mv [OPTIONS] source destination

The source can be one or more files, or directories and destination can be a single file or directory.

  • If you specify multiple files as source, the destination must be a directory. In this case, the source files are moved to the target directory.

  • If you specify a single file as source, and the destination target is an existing directory, then the file is moved to the specified directory.

  • To rename a file, you need to specify a single file as a source and a single file as a destination target.

For example, to rename the file file1.txt as file2.txt you would run:

mv file1.txt file2.txt

 

Renaming multiple files with the mv Command

The mv command can rename only one file at a time, but it can be used in conjunction with other commands such as find or inside bash for or while loops to rename multiple files.

The following example shows how to use the Bash for loop to rename all .html files in the current directory by changing the .html extension to .php.

for f in *.html; do
    mv -- "$f" "${f%.html}.php"
done

Let’s analyze the code line by line:

  • The first line creates a for loop and iterates through a list of all files edging with .html.

  • The second line applies to each item of the list and moves the file to a new one replacing .html with .php. The part ${file%.html} is using the shell parameter expansion to remove the .html part from the filename.

  • done indicates the end of the loop segment.

Here is an example using mv in combination with find to achieve the same as above:

find . -depth -name "*.html" -exec sh -c 'f="{}"; mv -- "$f" "${f%.html}.php"' \;

The find command is passing all files ending with .html in the current directory to mv one by one using the -exec option. The string {} is the name of the file currently being processed.

As you can see from the examples above, renaming multiple files using the mv command is not an easy task as it requires a good knowledge of Bash scripting.

 

Renaming Files with the rename Command

The rename command is used to rename multiple files. This command is more advanced than mv as it requires some basic knowledge of regular expressions.

There are two versions of the rename command with different syntax. In this tutorial, we will be using the Perl version of the rename command. If you don’t have this version installed on your system, you can easily install it using the package manager of your distribution.

  • Install rename on Ubuntu and Debian

    sudo apt install rename
  • Install rename on CentOS and Fedora

    sudo yum install prename
  • Install rename on Arch Linux

    yay perl-rename ## or yaourt -S perl-rename

The syntax for the rename command is as follows:

rename [OPTIONS] perlexpr files

 

The rename command will rename the files according to the specified perlexpr regular expression.

The following example will change all files with the extension .html to .php:

rename 's/.html/.php/' \*.html

You can use the -n option to print names of files to be renamed, without renaming them.

rename -n 's/.html/.php/' \*.html

The output will look something like this:

rename(file-90.html, file-90.php)
rename(file-91.html, file-91.php)
rename(file-92.html, file-92.php)
rename(file-93.html, file-93.php)
rename(file-94.html, file-94.php)

By default, the rename command doesn’t overwrite existing files. Pass the -f option to allow existing files to be over-written:

rename -f 's/.html/.php/' \*.html

Below are a few more common examples of how to use the rename command:

  • Replace spaces in filenames with underscores

    rename 'y/ /\_/' \*
  • Convert filenames to lowercase

    rename 'y/A-Z/a-z/' \*
  • Convert filenames to uppercase

    rename 'y/a-z/A-Z/' \*

 

Conclusion

We’ve shown you how to use the mv and rename commands to rename files.