Linux/Unix – Concatenating files with tar…
Files Archives (Concatenating files)
An archive is a single file that contains a collection of other files, and often directories. Archives are usually used to transfer or make a backup copy of a collection of files and directories−−this way, you can work with only one file instead of many. This single file can be easily compressed as explained in the previous section, and the files in the archive retain the structure and permissions of the original files.
Use the tar tool to create, list, and extract files from archives. Archives made with tar are sometimes called “tar files,” “tar archives,” or−−because all the archived files are rolled into one−−−”tarballs.”
The following recipes show how to use tar to create an archive, list the contents of an archive, and extract the files from an archive. Two common options used with all three of these operations are `−f’ and `−v’: to specify the name of the archive file, use `−f’ followed by the file name; use the `−v’ (“verbose”) option to have tar output the names of files as they are processed. While the `−v’ option is not necessary, it lets you observe the progress of your tar operation.
NOTE: The name of this tool comes from “tape archive,” because it was originally made to write the archives directly to a magnetic tape device. It is still used for this purpose, but today, archives are almost always saved to a file on disk.
Creating a File Archive
To create an archive with tar, use the `−c’ (“create”) option, and specify the name of the archive file to create with the `−f’ option. It’s common practice to use a name with a `.tar’ extension, such as `my−backup.tar’.
Give as arguments the names of the files to be archived; to create an archive of a directory and all of the files and subdirectories it contains, give the directory’s name as an argument.
To create an archive called `project.tar’ from the contents of the `project’ directory, type:
$ tar −cvf project.tar project
This command creates an archive file called `project.tar’ containing the `project’ directory and all of its contents. The original `project’ directory remains unchanged. Use the `−z’ option to compress the archive as it is being written. This yields the same output as creating an uncompressed archive and then using gzip to compress it, but it eliminates the extra step.
To create a compressed archive called `project.tar.gz’ from the contents of the `project’ directory, type:
$ tar −zcvf project.tar.gz project
This command creates a compressed archive file, `project.tar.gz’, containing the `project’ directory and all of its contents. The original `project’ directory remains unchanged.
NOTE: When you use the `−z’ option, you should specify the archive name with a `.tar.gz’ extension and not a `.tar’ extension, so the file name shows that the archive is compressed. This is not a requirement, but it serves as a reminder and is the standard practice.
Listing the Contents of an Archive
To list the contents of a tar archive without extracting them, use tar with the `−t’ option.
To list the contents of an archive called `project.tar’, type:
$ tar −tvf project.tar
This command lists the contents of the `project.tar’ archive. Using the `−v’ option along with the `−t’ option causes tar to output the permissions and modification time of each file, along with its file name−−the same format used by the ls command with the `−l’ option. Include the `−z’ option to list the contents of a compressed archive.
To list the contents of a compressed archive called `project.tar.gz’, type:
$ tar −ztvf project.tar
Extracting Files from an Archive
To extract (or unpack) the contents of a tar archive, use tar with the `−x’ (“extract”) option.
To extract the contents of an archive called `project.tar’, type:
$ tar −xvf project.tar
This command extracts the contents of the `project.tar’ archive into the current directory. If an archive is compressed, which usually means it will have a `.tar.gz’ or `.tgz’ extension, include the `−z’ option.
To extract the contents of a compressed archive called `project.tar.gz’, type:
$ tar −zxvf project.tar.gz
NOTE: If there are files or subdirectories in the current directory with the same name as any of those in the archive, those files will be overwritten when the archive is extracted. If you don’t know what files are included in an archive, consider listing the contents of the archive first.
Another reason to list the contents of an archive before extracting them is to determine whether the files in the archive are contained in a directory. If not, and the current directory contains many unrelated files, you might confuse them with the files extracted from the archive.
To extract the files into a directory of their own, make a new directory, move the archive to that directory, and change to that directory, where you can then extract the files from the archive.