• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Rename Files and Directories in Linux

Written by Admin, Updated On December 14, 2020
mv, rename, terminal
Rename Files and Directories in Linux

It’s a basic requirement to rename the files and directories on a Linux system. There are two ways to rename files using command-line terminal or via GUI file manager. In this tutorial, we will cover how to rename files and directories using mv and rename commands.

Rename Files using mv Command#

The mv command is a short name of move. We can use it to rename or move files from one location to another. Below is the syntax for the mv command:

mv [OPTIONS] source_path destination_path

In source_path, there can be single or multiple files or directories. In destination_path can have only one files or directory.

  • When multiple files passed in source_path then the destination_path must be a directory. Here, all files given as source will be moved to a destination directory.
  • If you will give a single file in source_path and the destination_path is an existing directory then it will be moved to the target directory.
  • For rename, there should a single file as source_path and one file as a destination_path.

Let’s see an example for better understanding. Here, we will rename the file tns.txt to tecnstuff.txt, run:

mv tns.txt tecnstuff.txt

Rename multiple files with mv Command#

Using the mv command you can rename only one file at a time. You can use mv command with other commands such as find or inside bash for or while loops to rename multiple files.

In below example we used the Bash for loop to rename all .xls files to .xlsx in the current working directory.

for i in *.xls; do
    mv -- "$i" "${I%.xls}.xlsx"
done

Let’s see how the above code will work:

  • In first code line it will initiate the for loop and iterates through a list of all files ending with .xls.
  • Second line will change the extension .xlsx from .xls of each item in list.
  • Third line starts with done shows the end of the loop segment.

Following is the example of mv command with combination with find to do same process as above example:

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

Here, find command is gathering and passing all files which ends with .xls to the mv command using the -exec option. The string {} indicates currently processing file name.

Rename Files using rename Command#

To rename the multiple files you can use the rename command. It’s very easy and advanced then the mv command. There are two methods to use the rename command. Here, we will use the perl version of the rename command. You can easily install this version using package manager, if your system don’t have.

Following are the different commands for different distributions:

Install rename on Ubuntu and Debian#

sudo apt install rename

Installing rename on CentOS and Fedora#

sudo yum install prename

Install rename on Arch Linux#

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

Following is the basic syntax of rename command:

rename [OPTIONS] expression files

It will rename the files as per given regular expression. Visit perl regular expressions to learn more.

In this example, we will also change the files extension .xls to .xlsx:

rename 's/.xls/.xlsx/' *.xls

Use the -n option to print the file names which to be renamed.

rename -n 's/.xls/.xlsx/' *.xls

It will show the output like below:

rename(2018.xls, 2018.xlsx)
rename(2019.xls, 2019.xlsx)
rename(2020.xls, 2020.xlsx)

To overwrite the existing files you should use the -f option with rename command. The rename command doesn’t overwrite existing files by default.

rename -f 's/.xls/.xlsx/' *.xls

Following is the list of most used examples of how to use the rename command:

Convert filenames to lowercase#

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

Convert filenames to uppercase#

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

Replace spaces in filenames with underscores#

rename 'y/ /_/' *

Conclusion#

I hope you successfully learned how to use the mv and rename command to rename the files and directories. You can also use the mmv command to rename file in Linux system.

If you have any questions or feedback, feel free to leave a comment.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Linux Time Command
Next Article   Linux Head Command

Related Posts

  • How to Use sed to Find and Replace String in Files

    How to Use sed to Find and Replace String in Files

    December 18, 2020
  • Linux Head Command

    Linux Head Command

    December 16, 2020
  • Linux Time Command

    Linux Time Command

    December 14, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook