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!
The one way to solve this without cat
ing 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With