gzip specific month in command line linux

fspalero

New Member
Hi,

Anyone can help on how to gzip a file on a specific month (Jan to Jun)?

files not compress:
344K -rw-r----- 1 sysadmin 340K Jan 11 00:51 filesys_2_0_280011584_280016896.1563382271044
63M -rw-r----- 1 sysadmin 63M Jan 16 00:51 filesys_0_0_0_0.1563382271055
200M -rw-r----- 1 sysadmin 200M Mar 19 00:58 filesys_3_0_316005326848_316008597248.1563382694108
63M -rw-r----- 1 sysadmin 63M May 10 00:58 filesys_0_0_0_0.1563382694272
72K -rw-r----- 1 sysadmin 68K Jun 28 01:04 filesys_1_0_51731008_51731968.1563383066812
63M -rw-r----- 1 sysadmin 63M Jul 22 01:04 filesys_0_0_0_0.1563383066848
...
...
...

files need to compress output
344K -rw-r----- 1 sysadmin 340K Jan 11 00:51 filesys_2_0_280011584_280016896.1563382271044.gz
63M -rw-r----- 1 sysadmin 63M Jan 16 00:51 filesys_0_0_0_0.1563382271055.gz
200M -rw-r----- 1 sysadmin 200M Mar 19 00:58 filesys_3_0_316005326848_316008597248.1563382694108.gz
63M -rw-r----- 1 sysadmin 63M May 10 00:58 filesys_0_0_0_0.1563382694272.gz
72K -rw-r----- 1 sysadmin 68K Jun 28 01:04 filesys_1_0_51731008_51731968.1563383066812.gz
63M -rw-r----- 1 sysadmin 63M Jul 22 01:04 filesys_0_0_0_0.1563383066848
...
...
...
Thank you in advance,
FSPalero
 


JasKinasis

Well-Known Member
Find has options which allow you to search for files by their create/modified/accessed timestamp.
Rather than trying to explain those options here - here's a link to an answer that somebody posted on stackoverflow:
https://stackoverflow.com/questions/18339307/find-files-in-created-between-a-date-range#23508622

Using some of those options with find, you can find the files you are looking for. And then you can use finds -exec option to gzip any files that are found. So you should be able to do it all in a single command.

Looking at your screenshots - the pattern for the files you're searching for is "filesys_*" and you're looking for files created between January and June.

So putting that into a find command, it should look something like this:
Bash:
find /path/to/search -type f -iname "filesys_*" -newerct "1 Jan 2019" ! -newerct "30 Jun 2019" -exec gzip {} \;
The above command will find and gzip any files which have filenames that begin with "filesys_" that were created between the 1st of January 2019 and the 30th of June.

NOTES: Replace /path/to/search with the path you will be searching for the files in.

The above also uses the assumption that all of the files you're looking for have the "filesys_" prefix in their file-names.
If this is NOT correct - then remove -iname "filesys_*" from the command and it will just search for ALL files created between the two dates.

You can also search by access time and modification time using finds -newerat and -newermt options. But I'm assuming that you want to do this by creation date.

Also the dates for finds -newer*t options can be specified in any date-string format that works with the date -d command.
So for example you could specify the start/end dates as -newerct 20190101 ! -newerct 20190630

See man date for more details. Also I think the link I've posted mentions this too!

Hopefully this helps!
 
Last edited:

Members online

No members online now.

Latest posts

Top