Friday, November 25, 2011

Creating Archives using tar command (tape archive)

tar command is useful to combine 1 or more files/directories into a single file. It is good for restore/backup purpose. There are some compress utilities which compress the files to save space on the filesystem.

1. Create a tar file from your current directory ..
# tar -cvf /var/tmp/mytar.tar mytar/ # Relative path

[ # tar -cvf /var/tmp/mytar.tar /home/mytar/ #Absolute path. ]

2. View the content of the tar file.
# tar -tvf mytar.tar

3. Extract the content of the tar file.
# tar -xvf mytar.tar

If you want to compress,

# bzip2 -c mytar.tar > mytar.tar.bz2 ==>> creates a new file mytar.tar.bz2
# bzip2 mytar.tar ==>> Compress the original file and renames with .bz2
# gzip -c mytar.tar > mytar.tar.gz ==>> creates a new file mytar.tar.gz
# gzip myfile ==>> Compress the original file and renames with .gz

# compress myfile # Compresses the file with .Z extension.

4. To extract the tar file
# bzip2 -d mytar.tar.bz2
# gzip -d mytar.tar.gz
# gunzip mytar.tar.gz
# uncompress mytar.tar.Z
and
# tar -xvf mytar.tar

5. Reading/viewing the Compress file.

# bzcat mytar.tar.bz2
# zcat mytar.tar.gz
# tar -tvf mytar.tar

No comments:

Post a Comment