Use gzip to compress the files in a directory except for already existing .gz files

jabbajac

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.

Anthon

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use gzip input codec on files without .gz extension in hadoop

From Dev

How to use gzip or gunzip in a pipeline with curl (For binary gz files)

From Dev

Sorting .gz files in a directory

From Dev

Tell gzip/bzip2/7z/etc not to compress already-compressed files?

From Dev

compress all fastq files to fastq.gz

From Dev

Cannot gzip compress static files in Nodejs

From Dev

Compress and moving the files to respective directory

From Dev

mass decompress gzip files without gz extension

From Dev

mass decompress gzip files without gz extension

From Dev

How to gzip multiple files into one gz file?

From Dev

zgrep multiple gz files in directory

From Dev

zgrep multiple gz files in directory

From Dev

gunzip all .gz files in directory

From Dev

selectively gzip large files in a directory

From Dev

selectively gzip large files in a directory

From Dev

Need to copy files to existing directory and remove files already there with the same name but different extension

From Dev

About the use of seek on gzip files

From Dev

imagemagick convert - compress all files in the directory

From Dev

How can I maximally compress .gz files in Nautilus?

From Dev

How can I maximally compress .gz files in Nautilus?

From Dev

How to compress separately with password hundreds of files in tar.gz?

From Dev

How to unzip .gz files in a new directory in hadoop?

From Dev

Count number of lines of .gz files recursively on directory?

From Dev

Count number of lines of .gz files recursively on directory?

From Dev

Need to check if .gz files exist in a directory

From Dev

Copying files and subdirectories to another directory with existing files

From Dev

Moving files accidentally to an not existing directory erases files?

From Dev

Moving files accidentally to an not existing directory erases files?

From Dev

Ability to add files to a directory but not remove existing files

Related Related

  1. 1

    Use gzip input codec on files without .gz extension in hadoop

  2. 2

    How to use gzip or gunzip in a pipeline with curl (For binary gz files)

  3. 3

    Sorting .gz files in a directory

  4. 4

    Tell gzip/bzip2/7z/etc not to compress already-compressed files?

  5. 5

    compress all fastq files to fastq.gz

  6. 6

    Cannot gzip compress static files in Nodejs

  7. 7

    Compress and moving the files to respective directory

  8. 8

    mass decompress gzip files without gz extension

  9. 9

    mass decompress gzip files without gz extension

  10. 10

    How to gzip multiple files into one gz file?

  11. 11

    zgrep multiple gz files in directory

  12. 12

    zgrep multiple gz files in directory

  13. 13

    gunzip all .gz files in directory

  14. 14

    selectively gzip large files in a directory

  15. 15

    selectively gzip large files in a directory

  16. 16

    Need to copy files to existing directory and remove files already there with the same name but different extension

  17. 17

    About the use of seek on gzip files

  18. 18

    imagemagick convert - compress all files in the directory

  19. 19

    How can I maximally compress .gz files in Nautilus?

  20. 20

    How can I maximally compress .gz files in Nautilus?

  21. 21

    How to compress separately with password hundreds of files in tar.gz?

  22. 22

    How to unzip .gz files in a new directory in hadoop?

  23. 23

    Count number of lines of .gz files recursively on directory?

  24. 24

    Count number of lines of .gz files recursively on directory?

  25. 25

    Need to check if .gz files exist in a directory

  26. 26

    Copying files and subdirectories to another directory with existing files

  27. 27

    Moving files accidentally to an not existing directory erases files?

  28. 28

    Moving files accidentally to an not existing directory erases files?

  29. 29

    Ability to add files to a directory but not remove existing files

HotTag

Archive