How to exclude certain file sizes from copying in Linux

A few days ago, we wrote how to do backing up your entire Linux system using rsync utility… One of the problems that you may encounter during the backup process is large files that can take a very long time to copy, for example movies larger than 3GB. So, it would be better if we could just exclude large files or less important files to complete the backup as soon as possible. Then we can copy the leftover files whenever you have free time. If you have ever been in the same situation, then this is not a problem. We will show you a simple trick to exclude some files of a given size.

In this quick tutorial, we will see how to exclude certain files with a specific size from being copied using rsync and teams find on Unix-like operating systems.

Exclude specific file size from being copied on Linux

We can do this either with rsync or find commands.

Exclude specific file sizes from being copied using the “rsync” command

When reading through the man page of the command rsync, we learned that it is possible to exclude certain size files from being copied. You can just omit them from copying. If you have some free time, you can copy the rest of the excluded files.

Let’s say you want to exclude more than 5GB in size. All you have to do is use the option “-Max size = SIZE” using the rsync command. This option will not transfer a file larger than the specified size

Let me show you a simple example.

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

Here,

  • rsync – fast, versatile, local and remote file copy tool
  • -rv – recursive and multi-threaded mode
  • -max size = 5m – will not transfer files larger than 5 MB.
  • / home / sk / Downloads / – source directory
  • / home / sk / andreyex / – destination directory.

This command will copy all files less than 5MB from the / home / sk / Downloads / directory to the / home / sk / grayex / directory. And it will exclude all files larger than 5 MB.

You can also use “-Min-SIZE = SIZE” so that transfer any file less than the specified size

For example, to transfer files that are less than 10 MB, run the following command:

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

Exclude some files with a specific size from copying using find command

We can also exclude certain files of a certain size from being copied using the find and cp commands.

Take a look at the following command:

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

This command will find all files less than 10 MB in the current directory and copy them to the / home / sk / grayex / directory.

Sidebar