Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tar to archive files from different directories without directory structure

Tags:

linux

tar

I have several hundred to thousand files in different subdirectories which all reside in one parent directory, e.g.

/home/dir1/file1
/home/dir2/file2
/home/dir3/file3
...
/home/dir10000/file10000

How can I use tar so my tar archive looks like this?

file1
file2
file3

I can ensure that the file names are unique. I do not want to include the original directory structure.

Thanks for you help folks!

like image 437
user1876422 Avatar asked Sep 07 '13 16:09

user1876422


1 Answers

The one way to solve this without cating or |ing to other programs:

tar --transform 's,.*/,,g' -cvf mytarfile.tar /path/to/parent_dir

# --transform accepts a regular expression similar to sed.
#   I used "," as a delimiter so I don't have to escape "/"
#   This will replace anything up to the final "/" with ""
#   /path/to/parent_dir/sub1/sub2/subsubsub/file.txt -> file.txt
#   /path/to/parent_dir/file2.txt -> file2.txt

Unlike Aaron Okano implies, there is no need to pipe to xargs if you have a long list of files within a single / few parent directories that you can specify on the command line.

like image 131
trs Avatar answered Oct 18 '22 09:10

trs