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?
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.
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 ...
tar instead of gzip --stdout file. tar > file. tar. gz gzip will delete the tar file for you.
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.
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.
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.
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