1
votes

I have following file structure

.
├── doc1.pdf
├── doc2.pdf
└── images
    ├── img1.jpg
    └── img2.jpg

I would like to tar images directory preserving parent images (without full path to it). What is more i would like skip other documents in directory (doc1, doc2) Currently I use:

tar -cf /sth_dir/images.tar -C /images/ . --exclude="doc*"

but i would like to not use --exclude because additional files, with different names might be added there.

2
your '-C images' already does the job, you don't have to --exclude anything. Given, that you stated the correct question ;) - asdmin

2 Answers

0
votes

This should work:

tar cf /sth_dir/images.tar images

tar produced this output when I included the -v argument (tar cvf ...):

a images
a images/img1.jpg
a images/img2.jpg

And the tar file contents looks like this:

images/
images/img1.jpg
images/img2.jpg
0
votes
tar cf /sth_dir/images.tar -C / images/images

Assuming that you have an images directory embedded in an other images directory.

man tar:

 -C, --directory DIR
       change to directory DIR

as an alternative, you can consider cpio. Assuming you are experienced with find, you can be very specific what would you like to archive. For example this duo gives you the possibility to archive a directory, without its content, which I found very useful in many use-cases.