Dexirian
Dexirian

Reputation: 575

Script to zip complete file structure depending on file age

Alright so i have a web server running CentOS at work that is hosting a few websites internally only. It's our developpement server and thus has lots [read tons] of old junk websites and whatnot.

I was trying to elaborate a command that would find files that haven't been modified for over 6 months, group them all in a tarball and then delete them. Thus far i have tried many different type of find commands with arguments and whatnot. Our structure looks like such

/var/www/joomla/username/fileshere/temp /var/www/username/fileshere

So i tried something amongst the lines of :

find /var/www -mtime -900 ! -mtime -180 | xargs tar -cf test4.tar

Only to have a 10MB resulting tar, when the expected result would be over 50 GB's.

I tried using gzip instead, but i ended up zipping MY WHOLE SERVER thus making is unusable, had to transfer the whole filesystem and reinstall a complete new server and lots of shit and trouble and... you get the idea. So i want to find the perfect command that won't blow up our server but will find all FILES and DIRECTORIES that haven't been modified for over 6 months.

Upvotes: 0

Views: 126

Answers (2)

Gooseman
Gooseman

Reputation: 2231

Be careful with ctime.

  1. ctime is related to changes made to inodes (changing permissions, owner, etc)

  2. atime when a file was last accessed (check if your file system is using noatime or relatime options, in that case the atime option may not work in the expected way)

  3. mtime when data in a file was last modified.

Depending on what are you trying to do, the mtime option could be your best option.

Besides, you should check the print0 option. From man find:

-print0
          True;  print  the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).  This allows file names that contain newlines or
          other types of white space to be correctly interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

I do not know what are you trying to do but this command could be useful for you:

find /var/www -mtime +180 -print0 | xargs -0 tar -czf example.tar.gz

Upvotes: 1

vinaut
vinaut

Reputation: 2446

Try this:

find /var/www -ctime +180 | xargs tar cf test.tar

The ctime parameter tells you the difference between current time and each files modification times, and if you use the + instead of minus it will give you the "files modified in a date older than x days".

Then just pass it to tar with xargs and you should be set.

Upvotes: 0

Related Questions