How to copy (or cp) files recursively with exclusions in Linux system
Problem
How to copy or cp
files from a source to a destination recursively with some exclusions in a Linux operating system?
For example, I have a directory named A
as follows:
Now, I want to copy directory A
to directory B
recursively, but I don’t want to copy the log files to B
.
Environment
- Linux or MacOS
Solution
We can use rsync
to resolve this problem:
After running the above command, let’s check the destination directory B
:
It works!
How it works?
The rsync
command can not only sync with a remote host’s directory but also sync with a local directory.
Let’s check the three options used in the above command:
The --exclude
option can be used with patterns:
Aside from --exclude
, you can also use --exclude-from
, --include
, and --include-from
:
For example, if you want to exclude some types of files, just create a file named exclude.list
:
Content of exclude.list
:
Then run this command:
Summary
In this post, we explored how to use the rsync
command to copy files recursively while excluding specific file types or patterns. The --exclude
option is particularly useful for filtering out unwanted files during the copy process. Additionally, we discussed other related options like --exclude-from
, --include
, and --include-from
for more advanced use cases. This method is efficient and works seamlessly on both Linux and MacOS systems.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!