Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ ls -lrt *.extract.sys 
-rw-r--r--   1 sftwrk01   test       1729455 Dec 01 21:21 ar.ARAB-PI_3.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1684929 Dec 30 21:21 ar.ARAB-PI_5.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1696332 Dec 30 21:21 ar.ARAB-PI_1.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1726197 Dec 30 21:21 ar.ARAB-PI_7.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1692531 Dec 03 21:21 ar.ARAB-PI_2.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1606737 Dec 30 21:21 ar.ARAB-PI_0.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1803846 Dec 30 21:21 ar.ARAB-PI_8.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1689816 Jan 30 21:21 ar.ARAB-PI_9.20101231.extract.sys
-rw-r--r--   1 sftwrk01   test       1724025 Jan 30 21:21 ar.ARAB-PI_6.20101231.extract.sys
....

I need to zip a the files in Dec month.

So far I have tried below and it didn't work

$ ls -lrt *.extract.sys | grep Dec | gzip            
gzip: compressed data not written to a terminal. Use -f to force compression.
For help, type: gzip -h

Please help

share|improve this question

migrated from stackoverflow.com May 29 '11 at 9:59

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 1 down vote accepted

Assuming you want to compress the actual file contents and not just the listing, you need to make a tar archive before compressing with GZip. Also, using find may be more reliable than grepping ls output.

First make marker files with dates representing the start and end of December, then find all files with modification times between those two files and use xargs with tar to package them and send to GZip. Here's how it's done:

  touch -t 201012010000 start
  touch -t 201101010000 end
  find . -iname \*.extract.sys -newer start \! -newer end -print0 | xargs -0 tar czvf dec.tar.gz
  rm start
  rm end

Make sure there aren't important files called "start" and "end" in the directory! If so, use other names or place the marker files for example under /tmp.

Also note that any subdirectories with names matching "*.extract.sys" will be compressed as well. If this is not wanted, add these flags to find: -type f -depth 1

share|improve this answer
    
@jjrv: Suggest you use -print0 in the find command and -0 in the xargs command in order to handle whitespace in filenames correctly. –  Tom Shaw May 29 '11 at 9:46
    
@Tom Shaw: Thanks, added. OS X has -X to handle escaping things, GNU find apparently not. –  jjrv May 29 '11 at 9:53
    
@jjrv: Also this approach will fail if there are too many filenames, in which case xargs will run tar multiple times and overwrite the archive. A possibly better approach is to use pax, which allows the cpio functionality of reading filenames on stdin. –  Tom Shaw May 29 '11 at 9:55
    
what is this \! –  munish May 29 '11 at 10:07
    
@munish: That means "not", i.e. "not newer than" –  Tom Shaw May 29 '11 at 13:33

You are trying to compress filenames instead of files. Don't send gzip the names of the files on stdin, send them as arguments.

share|improve this answer
    
Yes i get it @sverre, how would you do that anyway –  munish May 29 '11 at 9:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.