GZip on Linux to archive files specified in the text file - linux

I have a text file with paths to the list of files I want to compress into a singe archive. How can I pass this file to GZIP so it can create that archive with all files specified in the list?
Milan

gzip can only handle a single file at a time. You'll need to archive the files using tar first. Tar can do the compression at the same time (using the "z" argument).
tar cfz archive.tar.gz `cat file`

Well, in the first place, gzip doesn't compress multiple files into a single one, so you'll first tar. At least the GNU tar I checked has the option
-T, --files-from F
get names to extract or create from file F
so I suppose tar cfzvT target.tar.gz sourcelist would work.

gzip only compresses a single file. Use:
tar czf target.tar.gz `cat listoffile`

Related

Special characters in the extracted text file compressed with tar gzip

When i create the archive of a text file with this command:
tar -zcvf file.gz file.txt
and then i extract it get some strange characters in the begin and in the end of file like these:
\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000644\
How can i remove them? Is it any case to create archive of the one single text file without these special characters?
tar cfz creates a gzipped tar: file.tar.gz. It is the same as tar cf file.tar followed by gzip file.tar.
Extract such a file with tar xvfz (or gunzip file.tar.gz && tar xvf file.tar, but the z is shorter).
For single files use gzip and gunzip (possibly with -k or --keep to keep the original file).

How big can be tar command options

I want to use tar commands to archive multiple files in multiple directories. I just want one tar file as output.
tar -cvf file.tar /path1/inputfile1 /path2/inputfile2 ...
I want to know how long can be the arguments of create tar command. Does tar have some restrictions (or) unix command can only be some char long.
There's no limit in tar itself, but if you put the filenames on the command line you'll be limited by the operating system's command length limit. To get around this, you can use the -T option to get the list of filenames to archive from a file. So put all the filenames in a file filenames.txt (one filename per line), and then do:
tar -cvf file.tar -T filenames.txt

Unzip a single file in a tbz archive

I have the following archived directory:
itunes20140618.tbz
I want to extract single file from it called:
itunes20140618/video
How would I do this?
So far, I am doing
$ bzip2 -d /tmp/itunes20140618.tbz
But it seems to create a tar directory of everything. How would I extract just the single video file?
There are a few different versions of tar around, but on my machine I can do this:
tar xjf archive.tbz filename
To extract filename from archive.
If that doesn't work you can use:
bzip2 -dc archive.tbz | tar xvf - filename
Which uses bzip2 to extract to stdout and then pipe to tar.
In both cases you can replace the x option with t to get a list of files. Eg:
tar tjf archive.tbz
You can use the tar command and pass the path of the desired file or folder as an argument to it:
tar xjf test.tbz /path/to/file/in/archive

tar/gzip excluding certain files

I have a directory with many sub-directories. In some of those sub-directories I have files with *.asc extension and some with *.xdr.
I want to create a SINGLE tarball/gzip file which maintains the directory structure but excludes all files with the *.xdr extension.
How can I do this?
I did something like find . -depth -name *.asc -exec gzip -r9 {} + but this gzips every *.asc file individually which is not what I want to do.
You need to use the --exclude option:
tar -zc -f test.tar.gz --exclude='*.xdr' *
gzip will always handle files individually. If you want a bundled archive you will have to tar the files first and then gzip the result, hence you will end up with a .tar.gz file or .tgz for short.
To get a better control over what you are doing, you can first find the files using the command you already posted (with -print instead of the gzip command) and put them into a file, then use this file (=filelist.txt) to instruct tar with what to archive
tar -T filelist.txt -c -v -f myarchive.tar

How to gzip all files in all sub-directories into one compressed file in bash

Possible Duplicate:
gzipping up a set of directories and creating a tar compressed file
This post describes how to gzip each file individually within a directory structure. However, I need to do something slightly different. I need to produce one big gzip file for all files under a certain directory. I also need to be able to specify the output filename for the compressed file (e.g., files.gz) and overwrite the old compressed file file if one already exists.
tar -zcvf compressFileName.tar.gz folderToCompress
everything in folderToCompress will go to compressFileName
Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName
there are lots of compression methods that work recursively command line and its good to know who the end audience is.
i.e. if it is to be sent to someone running windows then zip would probably be best:
zip -r file.zip folder_to_zip
unzip filenname.zip
for other linux users or your self tar is great
tar -cvzf filename.tar.gz folder
tar -cvjf filename.tar.bz2 folder # even more compression
#change the -c to -x to above to extract
One must be careful with tar and how things are tarred up/extracted, for example if I run
cd ~
tar -cvzf passwd.tar.gz /etc/passwd
tar: Removing leading `/' from member names
/etc/passwd
pwd
/home/myusername
tar -xvzf passwd.tar.gz
this will create
/home/myusername/etc/passwd
unsure if all versions of tar do this:
Removing leading `/' from member names
#amitchhajer 's post works for GNU tar. If someone finds this post and needs it to work on a NON GNU system, they can do this:
tar cvf - folderToCompress | gzip > compressFileName
To expand the archive:
zcat compressFileName | tar xvf -

Resources