
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: March 18, 2024
In Linux, copying files is easy and straightforward. However, copying specific file types while retaining the directory structure requires a more delicate approach. This can be useful if we want to backup files in a directory structure that’s already familiar to us, for example.
In this tutorial, we’ll explore various commands that we can use to achieve this task.
The find command helps us search for files as well as directories. Typically, this search depends on different criteria like file type, file name, and more.
In this section, we’ll first explore combining find with the cp command. Then, we’ll discuss it in combination with the tar command.
This combination enables us to copy specific file types and, in the process, retain the directory structure.
First, let’s use ls to view the contents of our directory of interest:
$ ls -R source
source:
file_1.txt file_2.txt script_1.sh script_2.sh source_2
source/source_2:
file_3.txt file_4.txt
The source directory contains a few files and a subdirectory named source_2. This subdirectory holds two text files.
In this next step, let’s navigate to the source directory and copy all the .txt file types to an empty directory destination. In our example, the destination directory exists at the same directory level as the source directory:
$ find ./ -type f -name "*.txt" -exec cp --parents {} ../destination \;
Let’s break down the command:
Above, we’ve managed to copy all the text files to the destination directory. At the same time, we retained the directory structure.
Now, let’s view the contents of the destination directory:
$ ls -R ../destination
../destination:
file_1.txt file_2.txt source_2
../destination/source_2:
file_3.txt file_4.txt
From the output above, we see that this directory contains all the text files from the source directory. Most importantly, the directory structure persists.
The tar command is known for creating and extracting archives. However, we can also use it to copy files while retaining the directory structure:
$ find ./ -type f -name "*.txt" -exec tar -cf - {} + | tar -xf - -C ../destination
Let’s understand the above command:
In this example, we avoid creating the tar archive on the filesystem.
rysnc is a resourceful command line tool for copying files both locally and remotely. Furthermore, we can use it to preserve the directory structure of the files during the copying process. To clarify, we’ll work locally with the same files as the ones in the previous section.
From the terminal, let’s navigate to the source directory and execute our command:
$ rsync -av --include='*/' --include='*.txt' --exclude='*' ./ ../destination
sending incremental file list
./
file_1.txt
file_2.txt
source_2/
source_2/file_3.txt
source_2/file_4.txt
...
This output displays the progress of the copying process. The source_2 directory is also copied since it contains files that we’re interested in.
Let’s break down the command to understand it:
At this point, all the .txt files have been copied to the destination directory. Also, the directory structure matches the one in the source directory.
In this article, we learned different ways to copy specific file types while keeping the directory structure. First, we discussed using the find command in combination with the cp and tar commands. Then, we implemented a Bash script to automate our task. Afterward, we took a look at the rsync command.
Any of these approaches is suitable depending on our preferences. We can easily create backups of specific file types while maintaining their original format.