Creating a tar archive without including the parent directory when files are stored in different directories

tar

I have the following four files in the file system:

/home/martin/cvs/ops/cisco/b/s1
/home/martin/cvs/ops/cisco/b/s2
/home/martin/cvs/ops/extreme/b/r3
/home/martin/cvs/ops/j/b/r5

I need to put those files into a tar archive, but I don't want to add directories. The best I could come up with was:

tar -C ~/cvs/ops/ -czvf archive.tgz cisco/b/s1 cisco/b/s2 extreme/b/r3 j/b/r5

This is still not perfect, because each file in the archive is two directories deep. Is there a better way? Or do I simply have to copy s1, s2, r3 and r5 files into one directory and create the archive with tar -czvf archive.tgz s1 s2 r3 r5?

Best Answer

You can use -C multiple times (moving from one directory to another):

tar czvf archive.tar.gz -C /home/martin/cvs/ops/cisco/b s1 s2 -C ../../extreme/b r3 -C ../../j/b r5

Note that each -C option is interpreted relative to the current directory at that point (or you can just use absolute paths).