Tar a directory, but don't store full absolute paths in the archive
tar -cjf site1.tar.bz2 -C /var/www/site1 .
In the above example, tar will change to directory /var/www/site1
before doing its thing because the option -C /var/www/site1
was given.
From man tar
:
OTHER OPTIONS
-C, --directory DIR
change to directory DIR
How do I exclude absolute paths for tar?
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.
How to create a tar archive from an absolute path and package files as if they were relative?
You can use the -C
option to change the directory before performing any operations:
tar czf /home/user1/Documents/folder1.tar.gz -C /home/user1/Documents folder1
Now, the contents of your archive will look like this:
$ tar tf /home/user1/Documents/folder1.tar.gz
folder1/
folder1/file1
The message you get is not an error, by the way. It's tar telling you that it made the paths in the archive relative so as to avoid overwriting files, which could easily happen when unpacking an archive with absolute paths. You can turn off the leading slash removal with -P
, but you often don't want that.
How do I tar a directory of files and folders without including the directory itself?
cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd -
should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:
$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
tar file preserves full path. how to stop it?
you can cheat..
exec('cd /path/to/tmp_folder/ && tar -cvf /path/to/myfile.tar innerfolder/');
This would would give your users just the innerfolder when they extracted the tarball
Unix tar: do not preserve full pathnames
Use -C
to specify the directory from which the files look like you want, and then specify the files as seen from that directory:
tar -cvzf test.tar.gz -C /home/path test
How do I tar a directory without retaining the directory structure?
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
Tar files from different directories without paths
If you know the filenames and their directories you can use the -C
/--directory
option to have tar change its operating directory as it processes arguments.
So this command:
tar -cf files.tar.gz -C folder1/folder2 file1 file2 -C ../folder3/folder4 file3 file4
will create files.tar.gz
in the current directory with just the files in it.
You can automate creating the necessary command line arguments from find
output or similar with a little bit of scripting if necessary.
(You can't use globs like folder1/folder2/*
in the file argument positions because the paths in those filenames will cause tar
to fail to find them. You can use $(ls folder1/folder2/)
in that position but that's obviously not safe for many valid filenames.)
Related Topics
Check If a Variable Exists in a List in Bash
"Max Open Files" for Working Process
Nuget on Linux: Error Getting Response Stream
X86_64 Assembly Linux System Call Confusion
How to Modify the Source of Buildroot Packages for Package Development
Display Two Files Side by Side
How to Portably Extend a File Accessed Using Mmap()
How to Script a "Yes" Response for Installing Programs
Pack Shared Libraries into the Elf
How to Filter Out All Unique Lines in a File
Converting Serial Port Data to Tcp/Ip in a Linux Environment
How to Program Linux .Dts Device Tree Files
How to Install Svn Post-Commit Hook