tar with –include pattern

GNU tar has a -T or –files-from option that can take a file containing a list of files to include. The file specified with this option can be “-” for stdin. So, you can pass an arbitrary list of files for tar to archive from stdin using -files-from –. Using find patterns to generate a … Read more

how to make tar exclude hidden directories

You can use –exclude=”.*” $ tar -czvf test.tgz test/ test/ test/seen test/.hidden $ tar –exclude=”.*” -czvf test.tgz test/ test/ test/seen Be careful if you are taring the current directory, since it will also be excluded by this pattern matching. $ cd test $ tar –exclude=”.*” -czvf test.tgz ./ $ tar -czvf test.tgz ./ ./ ./seen … Read more

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.

Programmatically extract tar.gz in a single step (on Windows with 7-Zip)

Old question, but I was struggling with it today so here’s my 2c. The 7zip commandline tool “7z.exe” (I have v9.22 installed) can write to stdout and read from stdin so you can do without the intermediate tar file by using a pipe: 7z x “somename.tar.gz” -so | 7z x -aoa -si -ttar -o”somename” Where: … Read more

python write string directly to tarfile

I would say it’s possible, by playing with TarInfo e TarFile.addfile passing a StringIO as a fileobject. Very rough, but works import tarfile import StringIO tar = tarfile.TarFile(“test.tar”,”w”) string = StringIO.StringIO() string.write(“hello”) string.seek(0) info = tarfile.TarInfo(name=”foo”) info.size=len(string.buf) tar.addfile(tarinfo=info, fileobj=string) tar.close()

Extract files contained in archive.tar.gz to new directory named archive

Update since GNU tar 1.28: use –one-top-level, see https://www.gnu.org/software/tar/manual/tar.html#index-one_002dtop_002dlevel_002c-summary Older versions need to script this. You can specify the directory that the extract is placed in by using the tar -C option. The script below assumes that the directories do not exist and must be created. If the directories do exist the script will still … Read more