Renaming directories is one of the most primary operations you frequently want to perform on a Linux system. You can rename directories from the GUI document manager with more than one click or the use of the command-line terminal.
This article explains a way to rename directories by the use of the command-line.
In Linux and Unix-like running systems, you may use the mv (short of move) command to rename or circulate documents and directories from one place to any other.
The syntax of the mv command for shifting directories is as follows:
mv [OPTIONS] source destination
For instance, to rename the listing dir1as dir2 you will run:
mv dir1 dir2
When renaming directories, you need to specify precisely arguments to the mv command. The first argument is the present name of the directory, and the second argument is the new name.
It is essential to notice that if dir2 already exists, dir1 is moved to the dir2 directory.
To rename a listing that isn’t in the present running directory, you want to specify both absolutely the or relative path:
mv /home/user/dir1 /home/user/dir2
Renaming Multiple Directories
It is easy to rename a single directory challenge, however renaming more than one directories straight away may be a challenge, specifically for new Linux users.
Renaming more than one directories straight away is not often needed.
Renaming Multiple Directories with mv
The mv command can rename the handiest one report at a time. However, it may be used along with different instructions such as find or interior loops to rename more than one document straight away.
Here is an example displaying a way to use the Bash for loop to append the present date to the names of all directories in the present operating directory:
for d in *; do if [ -d "$d" ]; then mv -- "$d" "$d">_$(date +%Y%m%d)" fi done
Let’s examine the code line through line:
- The first line creates a loop and iterates by a listing of all documents.
- The 2nd line examines if the document is a directory.
- The 3rd line appends the present date to every directory.
Here is an answer to the same challenge the usage of mv in aggregate with find:
find . -mindepth 1 -prune -type d -exec sh -c 'd=""; mv -- "$d" "$_$(date +%Y%m%d)"' ;
The find command is passing all directories to mv one after the other the use of the -exec alternative. The string is the name of the listing presently being processed.
As you may see from the examples, renaming more than one directories with mv isn’t an easy challenge because it requires a very good understanding of Bash scripting.
Renaming more than one directories with rename
The rename command is used to rename more than one document and directories. This command is superior to mv because it calls for a fundamental understanding of regular expressions.
There are variations of the rename command with unique syntax. We use the Perl version of the rename command. The documents are renamed in step with the given perl ordinary expression .
The following instance suggests a way to update areas in the names of all directories in the present operating listing with underscores:
find . -mindepth 1 -prune -type d | rename 'y/ /_/'
To be on the safe side, pass the -n alternative to rename to print names of the directories to be renamed without renaming them.
Here is any other instance displaying a way to convert listing names to lowercase:
find . -mindepth 1 -prune -type d | rename 'y/A-Z/a-z/'
We’ve proven you a way to use the mv instructions to rename directories.
If you’ve got any questions or feedback, just leave a comment.