Reputation: 457
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!
Upvotes: 6
Views: 12016
Reputation: 1127
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.
Upvotes: 6
Reputation: 2343
GNU tar will take a transform option, which is just a sed expression that transforms the file name in the archive. You will also probably want to pipe to xargs if your list of files is very large.
cat filelist | xargs tar -rvf archive.tar --transform='s|.*/||g'
Keep in mind that this is appending to a tar archive (it will create one if it does not exist yet) so you will want to delete the archive if it already exists before running that command.
Upvotes: 3
Reputation: 1
A possible solution might be to use the ordinary tar
command (spitting on its stdout) and then to pipe the archive into tardy, probably with its -No_Directories
option, i.e.
tar cf - /home/dir?/ | tardy -No-Directories > yourbig.tar
However, I am not sure it is a good idea. Having a tar ball which is extracting into hundred of thousands of files in the same directory is not a good idea (some filesystems behave badly with that).
Upvotes: 2
Reputation: 17
Open the command line and type
tar -cvf NAME.tar /home/dir1/file1 /home/dir1/file2 /home/dir1/file3
Hope this helps :)
Upvotes: -3