exclude files & folders from copying in linux

How to Exclude Files & Folders From Copying in Linux

Linux provides various tools for copying files & folders from one location to another. But sometimes you may need to exclude a few files & folders from being copied in Linux. In this article, we will learn how to exclude files & folders from copying in Linux.


How to Exclude Files & Folders From Copying in Linux

We will look at how to exclude files & folders from copying in Linux, using rsync and cp commands.


1. Using rsync

rsync is a fast and powerful tool to copy files & folders within local system or between local and remote systems. It has a useful option –exclude that allows you to easily specify files & folders to be excluded from copying.

Let us say your folder /home/ubuntu has subfolders data1, data2, data3 and files file1.txt, file2.txt and file3.txt.

Let us say you want to copy all files and folders except file file1.txt

Here is the command to do so.

# rsync -av --exclude='/home/ubuntu/file1.txt' /home/ubuntu /home/data

Similarly, if you want to exclude folder data1 then here is the command for it.

# rsync -av --exclude='/home/ubuntu/data1' /home/ubuntu /home/data

If you want to exclude multiple files & folders use specify their paths in separate –exclude options, as shown below.

# rsync -av --exclude='/home/ubuntu/file1.txt' --exclude='/home/ubuntu/data' /home/ubuntu /home/data

Please note, while using rsync, it is advisable to use full paths to files & folders to be excluded, otherwise rsync will look for them relative to your present working folder and give error if they cannot be found.


2. Using cp

cp is perhaps the most commonly used command for copying files & folders. Unfortunately, it doesn’t provide a direct option to exclude files & folders from being copied. However, if you want to use cp command for this purpose, you can do so in conjunction with ls & grep command.

In this case, we get a list of files & folders to be copied using ls command, and use grep command to exclude files & folders. We pass this list to cp command for copying. Here exclusion is done by grep command and not cp command.

Let us say your folder /home/ubuntu has subfolders data1, data2, data3 and files file1.txt, file2.txt and file3.txt.

Let us say you want to copy all files and folders except file file1.txt

Here is the command to do so.

# cp -r `ls /home/ubuntu -A | grep -v "file1.txt"` /home/data

In the above command, ls will list all files and folders in specified folder, grep command will list it again by excluding file1.txt, which is passed to cp command for copying.

Similarly, if you want to exclude folder data1 then here is the command for it.

# cp -r `ls /home/ubuntu -A | grep -v "data1"` /home/data

If you want to exclude multiple files & folders use specify their paths in separate -v options, as shown below.

# cp -r `ls /home/ubuntu -A | grep -v "file1.txt" -v "data1"` /home/data

In this article, we have learnt how to exclude files and folders from copying in Linux. As you might have realized, rsync is a much easier command for this use case, compared to cp command. So it is advisable to use rsync command instead of cp command, if you want to exclude certain files & folders from copying.

Also read:

How to Copy Files to CD in Linux
How to Merge Folders & Directories in Python
How to Merge Folders & Directories in Linux
How to Split Folder into Subfolders in Linux
How to Split Large File into Smaller Files

Leave a Reply

Your email address will not be published. Required fields are marked *