1. Overview

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.

2. Moving the Directory

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 (/):

  • /home/francis/work – represents the source directory’s absolute path

To demonstrate, we’ll move the contents in /home/francis/work to a new subdirectory in /home/francis/work/work.

2.1. Using mv

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:

  • mkdir -p /home/francis/work/work – creates a new work directory inside the source directory
  • shopt -s extglob – allows us to use !(work) by enabling extended pattern matching
  • mv /home/francis/work/!(work) /home/francis/work/work – moves all files and directories inside /home/francis/work except for the work directory into /home/francis/work/work

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:

  • find /home/francis/work -maxdepth 1 – the find command searches within /home/francis/work excluding its subdirectories
  • ! -name work ! -name ‘.’ – excludes the new work directory and the current directory from the search
  • -exec mv {} /home/francis/work/work/ \; – executes the mv command for each item found

So, the find command allows administrators to select which file and directories to move in the new work directory.

2.2. Using cp

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:

  • cp -r /home/francis/work/!(work) /home/francis/work/work/ – copies all the contents of /home/francis/work except the work directory into /home/francis/work/work
  • rm -r /home/francis/work – deletes the original content in /home/francis/work apart from the new work subdirectory

This command effectively moves the contents of the work directory into a subdirectory with the same name.

2.3. Using rsync

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:

  • rsync – signifies the command we use to copy and synchronize files and directories
  • -a – represents the archive option, which preserves the directory attributes like permissions, ownerships, symbolic links
  • -v – stands for verbose, which displays a detailed output of the copying process
  • –exclude=work – excludes the copying of the new work directory
  • rm -r /home/francis/work – deletes the source directory

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.

3. Conclusion

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.