0

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?

1
  • 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 Commented Sep 20, 2018 at 0:29

1 Answer 1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.