
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: July 7, 2024
The Linux shell is an important tool that we can use to manage directories. For example, we can use it to move one directory into another directory with an identical name. As a result, this task can help us organize files and create backups.
In this tutorial, we’ll take a look at the commands we can utilize to complete this task.
In this section, we use the mv, cp, rsync, and rm commands to accomplish the task. Additionally, we’ll use absolute directory paths, keeping in mind that directories occur in a hierarchical structure starting from the root directory (/):
To demonstrate, we’ll move the contents in /home/francis/work to a new subdirectory in /home/francis/work/work.
Typically, we use the mv command to move and rename files and directories in Linux. Further, this command implements a simple syntax:
mv [source] [destination]
Above, [source] is for the source directory. Meanwhile, [destination] is for the destination directory.
However, moving the contents of /home/francis/work to the new directory /home/francis/work/work isn’t straightforward:
$ mv /home/francis/work /home/francis/work/work
mv: cannot move '/home/francis/work' to a subdirectory of itself, '/home/francis/work/work'
Above, we get an error message since the command attempts to move the /home/francis/work directory inside a subdirectory within itself, /home/francis/work/work. For this reason, we need an advanced approach:
$ mkdir -p /home/francis/work/work && shopt -s extglob && mv /home/francis/work/!(work) /home/francis/work/work
Here’s the breakdown:
Above, we first create another work directory inside /home/francis/work. After that, we move the contents of /home/francis/work to the location /home/francis/work/work.
Another option we can utilize is combining mv with find. To clarify, find enables us to search for the contents in /home/francis/work:
$ mkdir -p /home/francis/work/work && find /home/francis/work -maxdepth 1 ! -name work ! -name '.' -exec mv {} /home/francis/work/work/ \;
Let’s analyze this command:
So, the find command allows administrators to select which file and directories to move in the new work directory.
Alternatively, we can use the cp command in combination with the rm command to move a directory into another one with the same name:
$ mkdir -p /home/francis/work/work && shopt -s extglob && cp -r /home/francis/work/!(work) /home/francis/work/work/ && rm -r /home/francis/work/!(work)
Let’s discuss the command:
This command effectively moves the contents of the work directory into a subdirectory with the same name.
Another approach that we can use is the rsync command:
$ mkdir -p /home/francis/work/work && shopt -s extglob && rsync -av --exclude=work /home/francis/work/ /home/francis/work/work/ && rm -r /home/francis/work/!(work)
sending incremental file list
file1.txt
file2.txt
file3.txt
file4.txt
sent 289 bytes received 92 bytes 762.00 bytes/sec
total size is 0 speedup is 0.00
Here’s the breakdown:
Above, we move file_1.txt, file_2.txt, file_3.txt, and file_4.txt, the contents of /home/francis/work to /home/francis/work/work. After that, we delete these original files.
In this article, we explored how to use the Linux shell to move a directory into another directory with an identical name.
First, we discussed using the mv command. Then, we used a combination of the cp and rm commands. After that, we combined the rsync and rm commands.
Now we can manage directory structures using the Linux shell.