When it comes to copying a directory that contains other directories within directories (subdirectories) and files, implementing the cp command with the -R
(recursive) flag can effectively accomplish operation via a simple command syntax like the one stated below.
$ cp -R path/to/source/directory path/to/destination/directory
However, in some scenarios, you might want to mimic a certain directory structure that already exists maybe for your personal projects or for a files storage system just because that directory structure makes perfect sense and might require a lot of time to completely recreate it from scratch.
This article will walk us through valid approaches to copying an empty directory structure from already populated directory files in Linux.
Problem Statement
Since the primary objective of this article is to successfully copy a directory structure from an already existing and populated directory (with subdirectories), we need our own populated directory for reference purposes.
Consider the following directory structure previewed by the Linux tree command:
$ tree -a dir1
As per the tree command output, the root directory (dir1) has three subdirectories and a total of 10 files. Our aim is to copy this directory structure skeleton to a new destination without the 10 files that already exist.
1. Copy Linux Directory Structure Using tree and xargs Commands
We already know that the tree command mimics a tree-like format while listing a directory’s content. On the other hand, the xargs command takes standard input from a system user or previous command execution output to build and execute command lines.
The actions associated with combining these two commands are as follows:
- Get all the directory paths from an existing directory structure.
- Redirect and use the retrieved directory paths as input.
- Use the mkdir -p command to recreate the retrieved directory paths on a new directory path.
Getting Linux Directory Paths
We have already acknowledged the tree command’s effectiveness in generating a tree-like format of an existing directory structure. However, we can add some flags and command options to ensure that the tree command outputs the directory paths without detailing the directory files.
$ tree -dfi --noreport dir1
Our final command implementation will look like the following:
$ tree -dfi --noreport dir1 | xargs -I{} mkdir -p "$HOME/Downloads/{}"
Let us now use the tree command again to confirm if the directory structure was copied without files:
$ tree -a $HOME/Downloads/dir1
We have three directories and zero files hence our objective is achieved.
2. Copy Linux Directory Structure Using find and xargs Commands
Just like with the tree command, we can use the find command to get the full directory paths on our targeted directory structure.
$ find dir1 -type d
The -type d
option informs find command to only retrieve directories.
The above command can then be piped to a xargs command with the destination (e.g $HOME/Documents) for creating the directory structure without files.
$ xargs -I{} mkdir -p "$HOME/Documents/{}"
Our final command will look like the following:
$ find dir1 -type d | xargs -I{} mkdir -p "$HOME/Documents/{}"
Confirm the creation of the directory structure:
$ tree -a $HOME/Documents/dir1
Alternatively, combining the find command with the -exec
argument yields the same results:
$ find dir1 -type d -exec mkdir -p "$HOME/Desktop/{}" \;
Know of other cool ways of copying a directory structure without files? Feel free to leave a comment or feedback.