How To Exclude Certain Size Files From Copying In Linux

Exclude Certain Size Files From Copying In Linux

A few days ago, I have explained how to backup your entire Linux system using Rsync utility. One problem I have encountered during the backup process was some large size files took really long time to copy. I had to copy some movies which are over 3GB in size. So, I thought that it would be better if I could simply exclude the big files or less important files in order to complete the backup as soon as possible. I can then copy the rest of files whenever I have free time. If you ever been in the same situation like me, no problem. I just found a simple trick to exclude some files that are over a certain size.

In this brief guide, we will see how to exclude certain size files from copying using rsync and find command in Unix-like operating systems.

Exclude Certain Size Files From Copying In Linux

We can do it either using rsync or find command.

Exclude Certain Size Files From Copying using ‘rsync’ command

While reading through the man pages of rsync command, I came to know that there is an option to exclude certain size files from copying. You can simply omit them from copying. When you have free time, you can then copy the rest of excluded files.

Let us say, you want to exclude bigger than 5 MB in size. All you have to do is use “–max-size=SIZE” option with rsync command. This option will not transfer any file larger than the specified size.

Allow me to show you a simple example.

rsync -rv --max-size=5m /home/sk/Downloads/ /home/sk/ostechnix/

Here,

  • rsync – a fast, versatile, local and remote file-copying tool
  • -rv – recursive and verbose mode
  • –max-size=5m – Won’t transfer files larger than 5 MB.
  • /home/sk/Downloads/ – Source directory
  • /home/sk/ostechnix/ – Destination directory.

The above command will copy all files below 5MB in /home/sk/Downloads/ directory to /home/sk/ostechnix/ directory. It will exclude all files bigger than 5MB.


Want to know what each argument in a Linux command will do? Refer the following link.


You can also use “–min-size=SIZE” to transfer any file smaller than the specified size.

For example, to transfer files which are smaller than 10 MB, run the following command:

rsync -rv --min-size=10m /home/sk/Downloads/ /home/sk/ostechnix/

Exclude Certain Size Files From Copying using ‘find’ command

We can also exclude certain size files from copying using find and cp commands.

Have a look at the following command:

find . -size -10M -exec cp -r {} /home/sk/ostechnix/ \;

The above command will find all files under 10 MB in the current directory and copy them to /home/sk/ostechnix/ directory.


Recommended read:


And, that’s all. Do you know any another simple way to copy only a certain size files and exclude the rest? Feel free to let us know in the comment section below. I will be soon here with another interesting guide. Until then, stay tuned with OSTechNix.

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...