Avatar of supportpro
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...
LinuxLinux Networking

Avatar of undefined
Last Comment
omarfarid
Avatar of amnonnes
amnonnes
Flag of Israel image

This will find files in the current directory and any sub directory that were last ACCESSED 1 day ago and more, and automatically gzip them

You can change the +1 to whatever you want. You can also change to mtime to change to last MODIFIED.
find . -atime +1 -exec gzip {} \;

Open in new window

Avatar of dhoffman_98
dhoffman_98
Flag of United States of America image

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.
Avatar of dhoffman_98
dhoffman_98
Flag of United States of America image

OK... so scratch that... I like amnonnes solution better.
Avatar of supportpro
supportpro

ASKER

How can find and gzip all files in a folder that are not accessed for more than 2 yrs
Avatar of amnonnes
amnonnes
Flag of Israel image

2 years is 730 days, attached.
find . -atime +730 -exec gzip {} \;

Open in new window

Avatar of amnonnes
amnonnes
Flag of Israel image

PS - replace "." with the folder name to be scanned.
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of amnonnes
amnonnes
Flag of Israel image

@omarfarid,

gzip can't handle directories and it will ignore it automatically.. But basically you are right.. :-)
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

hi rindi,

the selected answer does not take care of directoried while mine does. Thanks
Avatar of amnonnes
amnonnes
Flag of Israel image

@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.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

look to the -type f in the find command which will list files only and hence gzip will work on files only
Avatar of amnonnes
amnonnes
Flag of Israel image

Again... gzip will ignore the directories anyway... :-)
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

yes it will ignore, but it will generate messages and hence my solution is more acurate
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo