Copy Directory Structure Without Files on Linux


There are various commands in Linux to copy directories from one destination to another. In Linux, you can copy a directory recursively using the -R option with the cp command that copies the source directory, including all its files, to the destination. Conversely, sometimes we need to copy only the empty directory structure to a different target directory without copying its files from the source directory.

It takes a long time if you copy the directory structure one by one without the files. In this case, you can use some commands in Linux to copy the empty directory structure directly without files. This tutorial will show examples of all the available commands to copy the directory structure without files on Linux.

Copy Directory Structure Without Files on Linux

In Linux, you can copy any existing directory structure and subdirectories with the help of several commands. Here we will copy the 'Documents' directory structure to the 'Downloads' directories excluding its files. Let's consider the structure of the Documents Directory using the following tree command −

~$: tree -a Documents 
Documents
|__ dir1
|    |__sample1.txt
|__ dir2
|    |__ dir3
|          |__ example.txt
|__ sample2
|__ sample3

3 directories, 4 files

The above output shows that the Documents directory has 3 subdirectories and 4 files. Now, we will copy only these 3 directories to another location without any files. In this case, we can use three commands –

The rsync Command

The rsync is a powerful command that copies the files from the source to the destination. You can use this utility to copy the directory structure.

~$: rsync -av -f"+ */" -f"- *" " <source> <destination>"

In the above command, we use;

  • -a − for archive coping.

  • -v − To view a detailed log of the rsync command.

  • -f − Each 'f' defines a filter, and the filtering rule obeys the filter.

  • + − Include all directories.

  • - − Exclude all files.

For example, let's copy all the directories of the Documents to the Downloads directory −

~$: rsync -av -f"+ */" -f"- *" " /home/prateek/Documents Downloads"
sending incremental file list 
Documents/
Documents/dir1/
Documents/dir2/ 
sent 142 bytes received 28 bytes 340.00 bytes/sec
total size is 0 speedup is 0.00

If you are using an old version of rsync, then you can use the following command −

~$: rsync -av --include='*/' --exclude='*' /home/prateek/Documents Downloads

Hence, your source directory structure will be copied to the destination directory without the files.

The find Command

You can copy the full directory path to another destination with the find command with the xargs command / -exec option. First, display the directory structure you want to copy −

~$: find Documents -type d
Documents 
Documents/dir2
Documents/dir2/dir3
Documents/dir1
  • To copy directory structure without files, you have to pipe the above command with the xargs command as follows −

~$: find Documents -type d | xargs -I{} mkdir -p "$HOME/Downloads/{}"
  • Optionally, you can copy the directory structure without the files using the -exec argument with the find command and get the same results.

~$: find Documents -type d -exec mkdir -p "$HOME/Downloads/{}" \;

Here we used the -type d option to retrieve directories only.

The tree and xargs Commands

In Linux, you can use the tree command to list the contents of a directory in a tree-like format. Adding flags to this command allows you to display directory paths as output without expanding their files.

~$: tree -dfi --noreport Documents
Documents
Documents/dir1
Documents/dir2
Documents/dir2/dir3

In the above command −

  • -d − To print the directories only.

  • -f − Print the full path.

  • -i − Present output in the tree format.

  • -noreport − To suppress the summary report at the end of the output

Now, let's pipe the above command to the xargs command. It either takes the standard input from a previously executed command or system user to execute and build command lines −

~$: tree -dfi --noreport dir1 | xargs -I{} mkdir -p "$HOME/Downloads/{}"

Here we use the mkdir -p command to rebuild the retrieved directory paths to a new directory path.

Verifying the Copied Directory Structure

You can copy the directory structure without files using any of the above methods and verify it with the help of the following tree command −

~$: tree -dfi --noreport dir1 | xargs -I{} mkdir -p "$HOME/Downloads/{}"
~$: tree -a $HOME/Downloads/Documents
/home/prateek/Downloads/Documents
|__dir1
|__dir2
    |__dir3

3 directories, 0 files

As you can see, the system has copied three directories without any files (zero files) to the new location.

Conclusion

In this article, we have explained three commands to copy directory structures without files on Linux. We have also used some examples to explain the rsync, find briefly, and xargs commands. Copying the directory is very easy, and you can use any of the above approaches to copy the directory structure without files on Linux.

Updated on: 18-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements