0

The program "zip" has a -R feature which allows one to zip all files with a certain name in a directory tree: zip -r v/s zip -R

For example:

zip -R bigfile "bigfile" 

Will zip all of the following:
./bigfile
./a/bigfile
./a/b/bigfile
./a/b/c/bigfile
.......

The -R feature doesn't seem to be in gzip or xz though. I've tried it, and I've also checked the man pages.

So how may I emulate this behavior in gzip and xz?

1 Answer 1

3

Combining find, tar and the compression utilities:

With gzip:

find . -type f -name bigfile | tar cfz bigfile.tgz -T -

or with xz:

find . -type f -name bigfile | tar cfJ bigfile.txz -T -

find searches recursively for all files named bigfile under the current/working directory and the resulting pathnames are supplied to tar that creates a tarball and compresses it.

These commands are suited for the example supplied in the question. Different patterns supplied to zip -R will require corresponding arguments supplied to find.

Also keep in mind that this won't work for all possible filenames; you should consider the --null option and feed tar from find -print0.

Also tar's "-T" option is not available on every systems (for instance in HP-UX).

EDIT1

Unlike zip, rar or 7-zip, for example, gzip and xz are not capable of compressing multiple files into one.

Quoting the gzip manpage:

If you wish to create a single archive file with multiple members so that members can later be extracted independently, use an archiver such as tar or zip. GNU tar supports the -z option to invoke gzip transparently. gzip is designed as a complement to tar, not as a replacement.

See How to gzip multiple files into one gz file? and How do I compress multiple files into a .xz archive?.

EDIT2

If the goal of the OP is to make a gzip file for each file it finds that satisfies the search criteria the following command should be issued:

find . -type f -name bigfile | gzip > bigfile.gz

For a xz file:

find . -type f -name bigfile | xz > bigfile.xz

It will create a compressed file in the same directory for each file that satisfies the search criteria leaving the original file "untouched".

EDIT3

As suggested by @Kusalananda, if in bash you first do:

shopt -s globstar 

and then issue the command:

tar -c -zf bigfile.tgz ./**/bigfile

a single archive file will be created with the multiple files found in subdirectories that satisfies the search criteria.

If the goal is to create one compressed file for each file found in subdirectories that satisfies the search criteria, after issuing the shopt command, you can just issue:

gzip ./**/bigfile
10
  • Does it absolutely need to rely on yet another program (in this case "tar")? If there is a simpler way which does not need more programs than just the "find" command, I may prefer that answer! Jan 1, 2020 at 10:21
  • @user1271772 See my edition to the answer. Jan 1, 2020 at 21:20
  • I do not want to compress multiple files into one. "zip -R" will make a zip file for each file it finds that satisfies the search criteria. Jan 1, 2020 at 21:52
  • @user1271772 See my 2nd edition to the answer. Jan 2, 2020 at 9:33
  • 1
    @PauloTomé Not if you first do shopt -s globstar in bash, then it will match anything called bigfile anywhere below the current directory.
    – Kusalananda
    Jan 2, 2020 at 13:31

You must log in to answer this question.

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