View the Most Wanted LQ Wiki articles.    
LinuxQuestions.org > Linux Wiki > How to find files and zip them right where they were found

From LQWiki

Jump to: navigation, search

Find and zip found files to the location where they were found:

Straight forward example:

find /etc/*/*.log -mtime +4 -execdir zip '{}'.zip '{}' \;


Brief explanation:

find /etc/*/*.log

This search for files in all folders under the etc folder, that have a .log suffix.


-mtime +4

This will cause the find command to search for files that were last modified 4 days ago or more.


-execdir

This will make the following command, to be executed in the directory of the found file.


zip '{}'.zip '{}' \;''

This is the ZIP command, the first parameter is the name of the output file (the original file name + ZIP suffix).

The second parameter is received via the find command. It's the name of the file that need to be compressed.


After zipping the files, you might want to delete the original files, leaving only the compressed files.

You can delete the original files as follows:

find /etc/*/*.log -mtime +4 -delete


Note: Before you try this command!

Before you try the above commands, you might want to make sure that the find command, finds the files you want it to find.

You can do this as so:

find /etc/*/*.log -mtime +4 -exec ls -lt {} \;

This will display the list of files that will be affected.


That's it.

I intentionally tried to keep it as simple as possible.

I hope you've found this helpful.



Personal tools
Sponsored Links