4

I want to tar (and gzip) files without directories and do not store directory paths. I want only flat files in my archive.

For example if I'll do something like this:

tar -czf archive.tgz /home/another/path/*

Then I'll have files with paths in my archive:

home/another/path/file1
home/another/path/file2
home/another/path/file3

But I'd like to have only this:

file1
file2
file3

This is an equivalent of zip's zip -D -j command.

For simplicity's sake lets assume that there are no subdirectories inside /home/another/path/ - it contains only files.

I tried wit -C option but it didn't seem to work. With command like this:

tar -C /home/another/path/ -czf archive.tgz *

The tar was trying to archive files in current dir instead of the dir passed to -C. I'm using (GNU tar) 1.19.

4

1 Answer 1

4

Try the following:

find . -type f -printf "%h\n%f\n" | xargs -L 2 tar -rf /tmp/files.tar -C 

or some variation depending on your needs. One should consider security when executing this command. And if you're willing to have "./" before the filename, the following might be a little more secure:

find -type f -execdir tar -rf /tmp/files.tar {} \;

or

find -type f -execdir tar -rf /tmp/files.tar {} +
2
  • find /home/another/path -maxdepth 1 -type f -execdir tar -rvf /root/tmp/tar/files.tar {} \; Worked for me. Thanks. I've added -maxdepth 1 to not descend into subdirectories of /home/another/path. Mar 30, 2015 at 11:00
  • I think the last command is missing a \ in front of the +
    – kdubs
    Mar 10, 2020 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .