Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Rename Directories in Linux - DesignLinux

designlinux 0 Comments

Renaming directories is one of the most basic operations you often need to perform on a Linux system. You can rename directories from the GUI file manager with a couple of clicks or using the command-line terminal.

This article explains how to rename directories using the command-line.

Renaming Directories #

In Linux and Unix-like operating systems, you can use the mv (short of move) command to rename or move files and directories from one location to another.

The syntax of the mv command for moving directories is as follows:

mv [OPTIONS] source destination

For example, to rename the directory dir1 as dir2 you would run:

mv dir1 dir2

When renaming directories, you must specify exactly two arguments to the mv command. The first argument is the current name of the directory, and the second one is the new name.

It is important to note that if dir2 already exists, dir1 is moved to the dir2 directory.

To rename a directory that is not in the current working directory, you need to specify either the absolute or relative path:

mv /home/user/dir1 /home/user/dir2

Renaming Multiple Directories #

Renaming a single directory is a simple task, but renaming multiple directories at once can be a challenge, especially for new Linux users.

Renaming multiple directories at once is rarely needed.

Renaming Multiple Directories with mv #

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

Here is an example showing how to use the Bash for loop to append the current date to the names of all directories in the current working directory:

for d in *; do 
  if [ -d "$d" ]; then
    mv -- "$d" "${d}_$(date +%Y%m%d)"
  fi
done

Let’s analyze the code line by line:

  • The first line creates a loop and iterates through a list of all files.
  • The second line checks if the file is a directory.
  • The third line appends the current date to each directory.

Here is a solution ti the same task using mv in combination with find:

find . -mindepth 1 -prune -type d -exec sh -c 'd="{}"; mv -- "$d" "${d}_$(date +%Y%m%d)"' \;

The find command is passing all directories to mv one by one using the -exec option. The string {} is the name of the directory currently being processed.

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

Renaming multiple directories with rename #

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

There are two versions of the rename command with different syntax. We’ll use the Perl version of the rename command. The files are renamed according to the given perl regular expression .

The following example shows how to replace spaces in the names of all directories in the current working directory with underscores:

find . -mindepth 1 -prune -type d | rename 'y/ /_/'

To be on the safe side, pass the -n option to rename to print names of the directories to be renamed without renaming them.

Here is another example showing how to convert directory names to lowercase:

find . -mindepth 1 -prune -type d | rename 'y/A-Z/a-z/'

Conclusion #

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

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

rename mv terminal

Related

Tags: mv, rename, terminal

How to Install Gradle on Ubuntu 20.04

Prev Post

How to Install Steam on Ubuntu 20.04

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
  • 5
  • 501
  • 612,680

DesignLinux.com © All rights reserved

Go to mobile version