Shell – Copy files and directory tree for filesize in specified range

file-copyfindshell-script

I would like to make a small script copying files from different directories example /pp/01 /pp/02/ etc… to another destination with the same directory design. But only those files within the directory with a size larger than 88MB but smaller than 93MB…

I would use a for loop and the find function. Are there more efficient ways? And how can I implement the find function exactly.

Best Answer

This command:

rsync -a --min-size 88m --max-size 93m pp /new_destination/

copies the structure from pp to /new_destination, but only files that are at least 88 MB and at most 93 MB in size.

Add more source directory trees before the final destination specifier if you like, such as:

rsync -a --min-size 88m --max-size 93m pp/01 pp/02 /target/

If you add a trailing slash to the source specification (pp/ as opposed to pp), rsync will not create that directory in the target.

In other words:pp in my first example, results in /new_destination/pp/01/..., whilst this:

rsync -a --min-size 88m --max-size 93m pp/ /new_destination/

instead results in /new_destination/01/..., /new_destination/02/....

The -a flag to rsync means "archive" which recurses directories, preserves metadata on the files, such as permissions and timestamps. (It is a convenience option that equals -rlptgoD which is a very popular combination of options).

Related Question