Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip path while archiving with TAR [closed]

Tags:

I have a file that contain list of files I want to archive with tar. Let's call it mylist.txt

It contains:

/path1/path2/file1.txt /path1/path2/file3.txt ... /path1/path2/file10.txt 

What I want to do is to archive this file into a tarball but excluding /path1/path2/. Currently by doing this:

tar -cvf allfiles.tar -T mylist.txt 

preserves the path after unarchiving.

I tried this but won't work too:

tar -cvf -C /path1/path2 allfiles.tar -T mylist.txt 

It archives all the files in /path1/path2 even those which are not in mylist.txt

Is there a way to do it?

like image 375
neversaint Avatar asked Nov 07 '11 22:11

neversaint


People also ask

How do I archive a directory in tar?

To create a tar archive, use the -c option followed by -f and the name of the archive. You can create archives from the contents of one or more directories or files. By default, directories are archived recursively unless --no-recursion option is specified.

How can I view the contents of a tar file without extracting it?

Use -t switch with tar command to list content of a archive. tar file without actually extracting. You can see that output is pretty similar to the result of ls -l command.

How do I unpack a tar file?

Simply right-click the item you want to compress, mouseover compress, and choose tar. gz. You can also right-click a tar. gz file, mouseover extract, and select an option to unpack the archive.


1 Answers

In your "Extraction phase" you can use the strip-components flag like

tar xvf tarname.tar --strip-components=n 

which will remove the first n leading components of the file name. Although if you have different file-path-components this will not work for all cases.

If you want to do it while archiving, only one thing comes to mind, and I will share

INPUT: list of files + full paths

1) for each line, split the path out of the filename

2) execute cd to that path and tar on that filename

3) repeat for each line

like image 176
hovanessyan Avatar answered Oct 14 '22 14:10

hovanessyan