Score:2

Make a gzip-compressed tar archive of files and directories that have a specific letter

lu flag

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...

Score:1
in flag

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

Score:1
kz flag

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...

Fjor avatar
do flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.