Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Rename Files and Directories in Linux - DesignLinux

Dec 15 2020
designlinux 0 Comments
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.

Related

Tags: mv, rename, terminal

How to Fix “W: Some index files failed to download.” Error In Ubuntu

Prev Post

How to Install MariaDB on CentOS 8

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 2
  • 694
  • 615,066

DesignLinux.com © All rights reserved

Go to mobile version