Renaming directories is a basic Linux procedure that lets users efficiently organize their file systems. Regardless of your experience with Linux, knowing how to rename folders is crucial for efficient file management.
We’ll go over the easy procedures needed to rename a directory in Linux in this guide, giving you the skills and assurance you need to handle your file system with ease. One of the simplest things you will do on any operating system is rename a directory. There are numerous ways to rename directories using scripts and commands in the Linux terminal. To make things clear and reset the view in your current work scenario clear command plays an important role in avoiding obstacles.
Required Conditions
- A device with a Linux distribution installed
- An account that has sudo access
- Possessing a terminal window or command line accessible
- Having access to a text editor like Nano or Vim
Renaming Directories Utilizing the mv Command
The mv command in Linux is mostly used for shifting files and directories from one location to another. It makes use of the subsequent command syntax:
mv [options] [source] [destination]
The mv command renames the source directory if the destination directory is empty. The syntax here is altered to:
mv [options] [Name of the current directory] [Name of newly created directory]
As an illustration, suppose our Home directory contains Directory1, Directory2, and Directory 3:
mv Directory1 Example_Directory
If the command is successful, there will be no output, thus we must use the ls command to confirm the name change:
ls -l
Changing the Name of Directories Using the new name Command
Linux provides a specialized command for renaming files and directories, called rename. It is simpler to rename several directories at once when you use this command.
Applying the rename Command to a Single Directory Renaming
The syntax for the rename command is as follows:
rename [options] 's/[expression]/[replacement]/' [file name]
By substituting the replacement for the first instance of the expression, the command renames the file. For instance, if Directory1 were to be renamed Example_Directory:
rename 's/Directory1/Example_Directory/' *
The rename command syntax in this example can be seen to be divided into multiple sections:
- rename: Uses the command rename.
- The letter s, which stands for “substitute,” denotes that the replacement is being used in place of the expression.
- /Directory1: Indicates the portion of the previous directory name that you wish to change, or the expression.
- /Example_Directory/: Specifies the new or replacement directory name.
- *: Looks for names in the Home directory that match the supplied expression.
Using the ls command to verify the contents of the Home directory reveals that the directory has a new name:
Changing the Name of Several Directories Using the New name Command
Without the need for bash scripts, you can rename several directories at once with the rename command. To rename Directory1, Directory2, and Directory3 to Folder1, Folder2, and Folder3 is one example.
rename -v 's/Directory/Folder/' *
In the above example:
-v: Uses the verbose output, which enumerates each stage of the procedure.
‘s/Directory/Folder/’: Folder is used in place of Directory in the search result names.
*: Looks up names in the Home directory that fit the given expression.
Moreover, file names can be translated with the rename command by substituting the y argument for the s option. Character for character, it converts one string of characters into another in this instance.
For example:
rename 'y/abc/def/'
The a, b, and c characters are converted to d, e, and f, respectively, by the aforementioned instruction.
We converted blank spaces in directory names to underscores (_) in the example below.
rename -v 'y/ /_/' *
Changing the Name of Directories Utilizing the find Command
If you are unsure of the location of the directory you wish to rename, you can search for it and rename it after you find it by combining the mv and find commands:
mv [options] [Name of the current directory] [Name of newly created directory]
In the previous example, after the directory has been located by the search command, the -execdir option runs the mv command. As an example, the following command locates Directory1 and renames it to Example_Directory:
find.-depth -type d -name Directory1 -execdir mv {} Example_Directory \;
Utilizing Bash Scripts to Rename Directories
Another method for simultaneously renaming many directories is to use bash scripts. Bash scripts, in contrast to the rename command, let you store a template for later usage.
First, write the script using a text editor like Nano:
sudo nano rename_directories.sh
A bash script that looks for folders and adds the current date to their name is shown in the example below:
#!/bin/bash #finds folders and renames them using the provided pattern. for d in * do if [ -d "$d" ] then mv -- "$d" "{d}_$(date +%Y%m%d)" fi done
In the above example:
- The script is told to search through all of the files and folders in the current location in the first line.
- Lines two and three do a directory check.
- The current date is appended to the name of any directory encountered in lines 4 and 5.
To close and save the script, press Ctrl+X, write Y, and hit Enter. Let’s use the script mentioned above as an example to rename the Directories 1, 2, and 3 that are situated in the Example directory.
Go into the Example directory first.
cd Example
Next, use the sh command to run the script:
sh rename_directory.sh
Verifying the name change is possible with the ls command:
ls -l
Conclusion
Learning how to rename folders in Linux gives users the ability to keep a file system that is arranged according to their requirements. The simple instructions in this book will help you rename folders with confidence, optimize your file management workflow, and improve your Linux experience in general.
Thus, keep these easy-to-remember tips in mind the next time you need to rename a directory so you may easily manage your file system.
Frequently Asked Questions (FAQ)
1. Is there a Command specifically for Renaming Directories?
Linux doesn’t have a particular command for renaming directories. But the mv command is used for moving and renaming files and directories.
2. Can I Rename Directories that are currently in use?
It can create errors so better look into that the directories are not actively in use. To avoid such errors while renaming take care of that.