I am trying to package the files:
file1
folder2/hello
folder2/world
file3
into a tar file so they are encapsulated in a custom defined folder, so the tar -tvf archive.tar
appears as:
mycustomfolder/file1
mycustomfolder/folder2/hello
mycustomfolder/folder2/world
mycustomfolder/file3
Currently to come around this I am:
- Creating an archive(x) of
file1
,folder2
andfile3
. - Creating a
mycustomfolder
folder. - Extract the archive(x) into
mycustomfolder
- Create an archive(y) from
mycustomfolder
I can't help it think their might be one step of doing this or a better way?
Best Answer
If you want the archive to extract into its own directory -- which is generally better, since ones that don't can make a mess -- just create the directory, then move/copy the content tree into it, so you have, as in your second example:
Then
tar -cvf myarchive.tar mycustomfolder
. To extract,tar -xvf myarchive.tar
.If you don't want to create the directory first, you can transform the files names and append a directory prefix:
The transformation (see
man tar
) usessed
syntax; I used%
instead of/
for the divider becauses/^/mycustomerfolder\//
creates a folder namedmycustomfolder\
(== odd behavior IMO), buts/^/mycustomfolder//
is (properly) an "Invalid transform expression".