2

I want ONLY directories and files that contain the letter. I've been able to recreate it from a previous question. But the tar is not a gzip.

And when trying to make it a gzip it...doesn't remove leading ´/' from hard link targets? which ruins it.

Here's the modified code (not gzip)

find /path/to/dir/ -type d -name "*x*" -o -type f -name "*x*" -exec tar rvf x.tar.gz {} \;

Gzip:

find /path/to/dir/ -type d -name "*x*" -o -type f -name "*x*" -exec tar cvzf x.tar.gz {} \;

The same amount of files appear during the process, but when checking the tar (tf) only a single file that starts with x appears...

Is it not possible to do with gzip, or am I missing an easier solution...

2 Answers 2

1

You have two issues here. First: the command behind the -exec switch is executed once for every occurrence. While appending to an existing uncompressed tar archive works like a charm, it does not for a compressed archive.

So you have two options here:

  • create the tar archive uncompressed, and compress it in a second step (as easy as gzip archive.tar)
  • or you have to build your command line. That is what the tool xargs was made for... it reads lines from standard input and appends them to your initial command line. (for example: cd /path/to/dir; find . -type f -name "*x*" | xargs tar -czf /path/archive.tar.gz)

The second issue with the removal of the trailing / is not a gzip issue, that is a tar issue. Basically, you NEVER want an absolute path to be inside an archive, since the guy extracting your archive wants to have a choice where to put the files - and this means a relative path to the location the extracting guy selects.

If you really know what you are doing, you could use tar's switch --absolute-names, which allows absolute paths to be inside the archive, but I really would not recommend that...

1
  • Other unrelated problem is the precedence of find boolean clauses: x -o y -exec z should be \( x -o y \) -exec z to execute z in both cases. And, as suggested in @Martin's answer, every launch of tar czvf tar-file.tgz file-with-x on every file or directory found will create a new tar-file.tgz, and not append to it if exists.
    – Fjor
    Oct 21, 2022 at 22:56
1

To removing the leading slash you can change your command like:

cd /
find path/to/dir/ -type ....

This will make find to display relative paths

You must log in to answer this question.

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