supportpro
asked on
How to grep and gzip files in folder
how to grep and gzip files in a folder that are not used for some time..
my pupose is to reduce the size of folder by gziping some files in that folder that are not used for quite long time...
my pupose is to reduce the size of folder by gziping some files in that folder that are not used for quite long time...
How would you know if they are used or not? Files that are written to would update their timestamps, but what about a file that is only read? There is no easy way to know if a file is being accessed for read only.
If you want to base this on timestamps though, you could do something like:
ls -alt | grep -v *.gz | tail -10
To get a list of the 10 files with the oldest timestamps.
But then what if all the files in the directory have been recently used? You would have no way of knowing that by just using a simple grep statement. Instead, you'd have to write something more complex that would parse the dates on each file and compare them to the current date so that perhaps you are only looking for files older than 90 days.
Just food for thought.
If you want to base this on timestamps though, you could do something like:
ls -alt | grep -v *.gz | tail -10
To get a list of the 10 files with the oldest timestamps.
But then what if all the files in the directory have been recently used? You would have no way of knowing that by just using a simple grep statement. Instead, you'd have to write something more complex that would parse the dates on each file and compare them to the current date so that perhaps you are only looking for files older than 90 days.
Just food for thought.
OK... so scratch that... I like amnonnes solution better.
ASKER
How can find and gzip all files in a folder that are not accessed for more than 2 yrs
2 years is 730 days, attached.
find . -atime +730 -exec gzip {} \;
PS - replace "." with the folder name to be scanned.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@omarfarid,
gzip can't handle directories and it will ignore it automatically.. But basically you are right.. :-)
gzip can't handle directories and it will ignore it automatically.. But basically you are right.. :-)
hi rindi,
the selected answer does not take care of directoried while mine does. Thanks
the selected answer does not take care of directoried while mine does. Thanks
@omarfarid,
1. The OP did not ask to take care of directories.
2. Your answer does not take care of directories since gzip can not handle directories.
1. The OP did not ask to take care of directories.
2. Your answer does not take care of directories since gzip can not handle directories.
look to the -type f in the find command which will list files only and hence gzip will work on files only
Again... gzip will ignore the directories anyway... :-)
yes it will ignore, but it will generate messages and hence my solution is more acurate
You can change the +1 to whatever you want. You can also change to mtime to change to last MODIFIED.
Open in new window