I have a directory of logs that I would like to set up a job to compress using gzip. The issue is I don't want to recompress the logs I've already compressed.
I tried using ls | grep -v gz | gzip
, but that doesn't seem to work.
Is there a way to do this? Basically I want to gzip every file in the directory that does not end in .gz.
Best Answer
You can just do:
gzip will tell you it skips the files that already have a
.gz
ending.If that message gets in the way you can use:
What you tried did not work, because
gzip
doesn't read the filenames of the files to compress from stdin, for that to work you would have to use:You will exclude files with the pattern
gz
anywhere in the file name, not just at the end.¹ You also have to take note that parsing the output ofls
is dangerous when you have file names with spaces, newlines, etc., are involved.A more clean solution, not relying on
gzip
to skip files with a.gz
ending is, that also handles non-compressed files in subdirectories:¹ As
izkata
commented: using.gz
alone to improve this, would not work. You would need to usegrep -vF .gz
orgrep -v '\.gz$'
. That still leaves the danger of processingls
' output