harcotlupus
harcotlupus

Reputation: 209

Statement that compress files older than X and after it removes old ones

Trying to do a bash script, that will compress files older than X, and after compressing removes uncompressed version. Tried something like this, but it doesn't work.

find /home/randomcat -mtime +11 -exec gzip {}\ | -exec rm;

Upvotes: 3

Views: 12202

Answers (3)

dburcu
dburcu

Reputation: 151

find /home/randomcat -mtime +11 -exec gzip {} +

This bash script compresses the files you find with the "find command". Instead of generating new files in gzip format, convert the files to gzip format. Let's say you have three files named older than X. And their names are a, b, c.

After running find /home/randomcat -mtime +11 -exec gzip {} + command, you will see a.gz, b.gz, c.gz instead of seeing a, b, c in /home/randomcat directory.

Upvotes: 2

Happy Basanta
Happy Basanta

Reputation: 5

find /location/location -type f -ctime +15 -exec mv {} /location/backup_location \;

This will help you find all the files and move to backup folder

Upvotes: 0

By default, gzip will remove the uncompressed file (since it replaces it with the compressed variant). And you don't want it to run on anything else than a plain file (not on directories or devices, not on symbolic links).

So you want at least

 find /home/randomcat -mtime +11 -type f -exec gzip {} \;

You could even want find(1) to avoid files with several hard links. And you might also want it to ask you before running the command. Then you could try:

find /home/randomcat -mtime +11 -type f -links 1 -ok gzip {} \;

The find command with -exec or -ok wants a semicolon (or a + sign), and you need to escape that semicolon ; from your shell. You could use ';' instead of \; to quote it...

If you use a + the find command will group several arguments (to a single gzip process), so will run less processes (but they would last longer). So you could try

 find /home/randomcat -mtime +11 -type f -links 1 -exec gzip -v {} +

You may want to read more about globbing and how a shell works.

BTW, you don't need any command pipeline (as suggested by the wrong use of | in your question).

You could even consider using GNU parallel to run things in parallel, or feed some shell (with background jobs) with e.g.

find /home/randomcat -mtime +11 -type f -links 1 \
  -exec printf "gzip %s &\n" {} \; | bash -x

but in practice you won't speed up a lot your processing.

Upvotes: 8

Related Questions