[ad_1]
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
#
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.
[ad_2]
Source link