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.

1 Answer 1


You can just do:

gzip *

gzip will tell you it skips the files that already have a .gz ending.
If that message gets in the way you can use:

gzip -q *

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:

ls | grep -v gz | xargs gzip

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 of ls 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:

find .  -type f ! -name "*.gz" -exec gzip {} \;

¹ As izkata commented: using .gz alone to improve this, would not work. You would need to use grep -vF .gz or grep -v '\.gz$'. That still leaves the danger of processing ls' output

  • 1
    "...that makes it less likely to match some internal part of a filename" - it's grep, just use $ so it's clearer. And you didn't escape the . anyway, so it's only going to exclude files that start with gz (since they don't have "any character before gz")
    – Izkata
    Oct 10, 2014 at 21:35
  • 1
    @Izkata: No, including the unescaped dot and not anchoring the regex with $ going to exclude (-v) all files that have names consisting of three or more characters including any character followed by "gz" anywhere in the filename. So it will include files with names that start with "gz". Oct 10, 2014 at 22:07
  • 1
    @DennisWilliamson My last comment wasn't clear, it's also going to exclude files with gz anywhere in the name (except the start). So it's better to anchor it anyway. Having not used $, it read as though you didn't realize what . means in a regex, hence the rest of the confusing comment.
    – Izkata
    Oct 10, 2014 at 23:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .