February 11th, 2004
A little reminder (because I hadn't done it for a while and had to look it up) of how to tar and zip and unzip and untar files.
First up, a recap on tarring and zipping.
Tarring pulls a directory full of files and subdirectories together into a single file. Zipping a file compresses it.
> To tar a directory use the command:
tar cvf destinationfile.tar directory
This creates a file called
destinationfile.tar containing the entire contents of the directory called
directory (including any subdirectories). The
c flag means create (rather than append to an existing file). The
v flag means use verbose mode (i.e. print a notification of each file that is tarred). The
f flag means you're going to specify the name of the tar file rather than using the value of the TAPE environment variable or the default value defined in /
etc/default/tar.
The verbose details get printed to standard error, which is the screen by default. You can redirect standard error to a file if you want to capture the output. This is a good idea if you are tarring a lot of files, otherwise the details of any problems appear briefly on the screen before being replaced by more information. If you capture the verbose details you can check what happened when the process completes. To capture the verbose details for the previous example:
In the C shell:
(tar cvf destinationfile.tar directory) >& log.txt
In bash:
(tar cvf destinationfile.tar directory) 2> log.txt
> Once you've got your tar file, nine times out of ten you're going to want to zip it.
To zip the tar file, use gzip:
gzip destinationfile.tar
This replaces
destinationfile.tar with a compressed file called
destinationfile.tar.gz. If you want to keep the original tar file, take a copy of it before zipping it.
> To unzip a file zipped with gzip, use gunzip:
gunzip destinationfile.tar.gz
As you'd expect, this replaces destinationfile.tar.gz with a decompressed file called
destinationfile.tar.
> To untar your tar file:
tar xvf destinationfile.tar
This unpacks the tar file, recreating the original tarred directory as a subdirectory of the current directory. If a directory of the same name already exists the files from the tar file are added to it, overwriting any files of the same name. Existing files with different names are not removed. The tar file is not deleted.
> If you want to see what's in a tar file, without going to the lengths of untarring it, use the command:
tar tvf tarfile.tar
The
t flag lists the table of contents for the file. If you want to check whether the tar file contains a file called
myfile.fm, you can use the command:
tar tf tarfile.tar *myfile.fm
If you want to check for files called
alsfile.txt,
alistairsfile.fm,
bobsfile.doc, etc. you could use the command:
tar tvf tmp.tar | grep .*sfile\....
The
\. is the dot character. The final three dots is the regexp way of saying any three characters.
Using this method, you can untar selected files. For example, to untar just files whose names end
.fm use the command:
tar xvf tarfile.tar `tar tf tarfile.tar | grep \.fm$`
In this command the dollar sign is the regexp way of denoting the end of a line.
This command would unpack all the
.fm files, recreating the directory structure in which they originally lived.
If you are untarring a single file and you don't want to recreate all the directories that originally housed it, but you just want the file to be unpacked into the current directory, you can use the following syntax:
tar xvfO tarfile.tar *myfile.txt > myfile.txt
The
O flag (that's a capital "oh") isn't documented in the man page, but I just tried it and it had the desired effect. It sends the output of the process to standard output, which allows you to redirect it to a file.
However, beware! If you use a command such as:
tar xvfO tarfile.tar `tar tf tarfile.tar | grep .*file\.txt$` > myfile.txt
you are asking for trouble, because more than one file may be matched by grep and unpacked by tar, and the contents of all of the unpacked files will end up in the specified output file (
myfile.txt in this case).
Potentially similar posts