Home Command line utilities How To Exclude Certain Size Files From Copying In Linux
Exclude Certain Size Files From Copying In Linux

How To Exclude Certain Size Files From Copying In Linux

By sk
125 Views

A few days ago, I 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 a 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 any unimportant files in order to complete the backup as soon as possible. So, I can then copy the rest of files whenever I have free time. If you ever been in the same situation like me, here is a simple trick to exclude certain size files from copying using rsync and/or find command in Unix-like operating systems.

Exclude Certain Size Files From Copying In Linux

Like I said, 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

3 comments

Shaun March 29, 2019 - 9:40 am

$ rsync -rv –max-size=5m //wont transfer any file larger than 5mb
$ rsync -rv –min-size=10m //will only transfer files smaller than 10mb

Hmm don’t think this is quite right….

Reply
jasimmk April 4, 2019 - 10:52 pm

From rsync man page

–max-size=SIZE
This tells rsync to avoid transferring any file that is larger than the specified SIZE. The SIZE value can be suffixed with a string to indicate a size multiplier, and may be a frac‐
tional value (e.g. “–max-size=1.5m

–min-size=SIZE
This tells rsync to avoid transferring any file that is smaller than the specified SIZE, which can help in not transferring small, junk files. See the –max-size option for a
description of SIZE and other information.

Reply
Kenneth April 11, 2019 - 1:29 am

Thx a lot. Saved a lot of time for me. Only added -size -10k to my find command for comfort 🙂

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More