5

I have 100 files, n1.txt, n2.txt,… and I want to gzip each of them, but not in one file – finally I'd like to have 100 n*.txt.gz files.

How can I do that in Linux with Bash?

2
  • 2
    Did you check the man pages? They are great when you need to get help on the allowed syntax of a command.
    – mdpc
    Commented Feb 18, 2013 at 19:45
  • Note that gzip does not allow compressing multiple files into one archive, because it's not an archive format (only a compression format). For that, you need to use tar, and compress the resulting tar archive with gzip.
    – jpaugh
    Commented Jul 27, 2018 at 16:00

2 Answers 2

8

To compress all files, you can use:

gzip n*.txt

The n*.txt will expand to all file names as arguments for gzip.

2

While hymie's answer perfectly covers the solution, I would like to offer an alternative which may be a bit more flexible as it allows you to use the find command first to check if your regex matches what you want and only then gzip the files.

find . -name "*.txt" -exec gzip {} \;

This searches from your current working directory for all files ending in .txt and gzip's them one by one.

On gzip you may want to pass:
-v verbose
-k keep the original file
-9 zip level (9 most compressed, 6 default)

Alternatively you may want to use pigz which is present in most distros and which is a parallel drop-in for gzip.

# parallel gzips all text files in the subfolders below command
find . -name "*.txt" -exec pigz -v9 {} \;

Taken from here.

0

You must log in to answer this question.

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