0

This question already has an answer here:

Let's say I have two directories that I would to tar without retaining the directory structure:

/root/dir1
/root/dir2
  • Also, I would like to backup only files that begin with say f1 in /root/dir1 and a file called log.txt in /root/dir1
  • In /root/dir2 I only want files that begin with f2

How I can achieve that?

marked as duplicate by user88036, RalfFriedl, Archemar, roaima, Luciano Andress Martini Sep 20 '18 at 17:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • you can use the command find and tar together something like find mydir -maxdepth 1 -type f -name "f1" -print0 | xargs -0 tar cvf mydir.tar – user88036 Sep 20 '18 at 0:29
0

If you have GNU tar, you can use the --transform or --strip-components options to get a flat archive, without the directory tree. Something like:

tar cf foo.tar --transform='s:.*/::' /root/dir1/f1* /root/dir2/f2* /root/dir1/log.txt

GNU tar will warn about the leading / in the file names, but we're removing them anyway.

Alternately, with a couple of cds:

cd /root/dir1;
tar cf /some/where/foo.tar f1* log.txt
cd /root/dir2;
tar uf /some/where/foo.tar f2*

Here, we'll need to specify a path to the tar archive that won't be affected by the cds (so an absolute path, or since both are directories in /root, a path to the parent folder).

Not the answer you're looking for? Browse other questions tagged or ask your own question.