Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude absolute paths for tar?

Tags:

linux

bash

tar

I am running a PHP script that gets me the absolute paths of files I want to tar up. This is the syntax I have:

tar -cf tarname.tar -C /www/path/path/file1.txt /www/path/path2/path3/file2.xls 

When I untar it, it creates the absolute path to the files. How do I get just /path with everything under it to show?

like image 248
Angel S. Moreno Avatar asked Jun 30 '10 21:06

Angel S. Moreno


People also ask

How do I exclude a directory in tar?

Sometimes it's nice if you are tar zipping a directory (let's say for a backup) and want to exclude a directory. For instance, if you are backing up a project and want to exclude the images directory (for size) or exclude the build or dist directory you can run the linux tar command with the --exclude option.

How do you change absolute paths?

To change directories using absolute pathnames, type cd /directory/directory; to change directories using relative pathnames, type cd directory to move one directory below, cd directory/directory to move two directories below, etc.; to jump from anywhere on the filesystem to your login directory, type cd; to change to ...

How do you delete a .TGZ file in Linux?

tar instead of gzip --stdout file. tar > file. tar. gz gzip will delete the tar file for you.

How do you tell if a path is an absolute path?

A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path.


2 Answers

If you want to remove the first n leading components of the file name, you need strip-components. So in your case, on extraction, do

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

The man page has a list of tar's many options, including this one. Some earlier versions of tar use --strip-path for this operation instead.

like image 137
ire_and_curses Avatar answered Oct 05 '22 00:10

ire_and_curses


You are incorrectly using the -C switch, which is used for changing directories. So what you need to do is:

tar -cf tarname.tar -C /www/path path/file1.txt path2/path3/file2.xls 

or if you want to package everything under /www/path do:

tar -cf tarname.tar -C /www/path . 

You can use -C switch multiple times.

like image 43
Johnny Baloney Avatar answered Oct 05 '22 02:10

Johnny Baloney