Tar compression without directory structure

directoryfilenamestar

Under my current directory, I have two sub-directories:

dir_1/
   - file1.png
   - file2.png
   ...
   - fileN.png

dir_2/
   - fileA.txt
   - ...
   - fileZ.txt

When I tar compress the two directories by :

tar -cvzf result.tar.gz dir_1/ dir_2/ 

I got result.tar.gz but it maintains the directory structure. I mean when I extract the result.tar.gz, I got dir_1 & dir_2 again.

How can I tar compress so that the directory structure is not remained, which means when I extract the tar.gz file, I only get files

result/
   file1.png 
   ... 
   fileN.png 
   fileA.txt
   ...
   fileZ.txt

Best Answer

I think you can do this with the -C option.

From the tar man page:

-C directory, --cd directory, --directory directory
    In c and r mode, this changes the directory before adding the following files.
    In x mode, change directories after opening the archive but before extracting
    entries from the archive.

This means that you should be able to run

tar cvzf result.tar.gz -C /path/to/dir1/ . -C /path/to/dir2/ .

to achieve what you want.

Related Question