Add a Directory When Creating Tar Archive

add a directory when creating tar archive

You can create a directory on the fly inside the archive using --transform:

tar czf myproject.tgz --transform 's,^,myproject-1.0/,' file2 subdir1

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

Creating tar file with append

If the file does not exist, the behaviour of -c, --create and -r, --append is identical.

However, behaviour differs if the file already exists. In that case, -c, --create will overwrite the file, whereas -r, --append will append to the end of the file.

Note that appending might have some unexpected effects. If you execute your command above twice, you will end up with a tar archive that contains two files 'hello':

$ tar rf hello.tar hello
$ tar rf hello.tar hello
$ tar tf hello.tar
hello
hello

tar stands for "tape archiver" and comes from the times, when physical tapes were still commonly used as backup medium. Appending to a tape makes sense, but when dealing with tar-files instead of tapes, it only makes sense, if the files are really large, otherwise recreating the archive is often the better option.

Another use-case for -r, --append, as chepner pointed out, is when you want to add files from different directories, but don't want the archive to reflect the same directory structure you currently have on your file system. In this case, using -c, --create from the first directory and then switching directories and using -r, --append for adding additional files makes sense.

Create tar.gz archive and add all files and folders without parent folder

You can use the -C option:

tar -C admin -zcvf admin.tar.gz .

See man tar

-C, --directory=DIR
Change to DIR before performing any operations.
This option is order-sensitive, i.e. it affects all options that follow.

Python tar.add files but omit parent directories

You can use 2nd argument for TarFile.add, it specified the name inside the archive.

So assuming every path is sane something like this would work:

import tarfile
prefix = "some_dir/"
archive_path = "inside_dir/file.txt"
with tarfile.open("test.tar", "w") as tar:
tar.add(prefix+archive_path, archive_path)

Usage:

> cat some_dir/inside_dir/file.txt
test
> python2 test_tar.py
> tar --list -f ./test.tar
inside_dir/file.txt

In production, i'd advise to use appropriate module for path handling to make sure every slash and backslash is in right place.



Related Topics



Leave a reply



Submit