Linux Tutorials - Herong's Tutorial Examples - v5.45, by Herong Yang
"tar -c" and "tar -x" - Create and Extract Archive Files
This section provides a tutorial example on how to use 'tar -c' command to compress multiple file and sub-directories into an archive file and use 'tar -x' command to extract files out of an archive file.
If you want to backup a directory tree with many files and sub-directories, you may want to use "tar -c" command archive the entire directory tree into a single file. Later, you can extract the entire directory tree out of the archive file using the "tar -x" command.
"tar", stands for "tape archive", command was originally developed in the early days of Unix for the purpose of backing up files to tape-based storage devices. Today it is still used to collect, distribute, and archive files, while preserving file system attributes such as user and group permissions, access and modification dates, and directory structures.
Here is what I did to create a backup of my GCC directory tree as an archive file.
1. Go to the parent directory where the directory tree is located. This is to ensure that only short relative pathnames are collected into the archive file.
herong$ cd /home/herong
2. Create the archive file using the "tar -c" command.
herong$ tar -c -f gcc-backup.tar gcc
3. List file names in the archive file using the "tar -t" command.
herong$ tar -t -f gcc-backup.tar gcc/ gcc/hello.c gcc/hello.cpp gcc/Makefile gcc/include/ gcc/include/myheader.h gcc/include/myfunc.h gcc/lib/ gcc/HeaderTest.cpp gcc/MyStatic.cpp gcc/MyStatic.o gcc/libMyStatic.a gcc/MyStaticTest.cpp gcc/MyDynamic.cpp gcc/MyDynamicTest.cpp gcc/a.out gcc/hello gcc/MyDynamicTest gcc/libMyDynamic.so gcc/hello.o
4. Extract all files and sub-directories from the archive file using the "tar -x" command.
herong$ mkdir tmp herong$ cd tmp herong$ tar -x -v -f ../gcc-backup.tar gcc/ gcc/hello.c gcc/hello.cpp gcc/Makefile gcc/include/ gcc/include/myheader.h gcc/include/myfunc.h gcc/lib/ gcc/HeaderTest.cpp gcc/MyStatic.cpp gcc/MyStatic.o gcc/libMyStatic.a gcc/MyStaticTest.cpp gcc/MyDynamic.cpp gcc/MyDynamicTest.cpp gcc/a.out gcc/hello gcc/MyDynamicTest gcc/libMyDynamic.so gcc/hello.o
5. If you want to compress the archive file with "compress" filter, you can use the "-Z" option.
herong$ cd /home/herong herong$ tar -c -Z -f gcc-backup.tar.Z gcc herong$ ls -l gcc-backup.* -rw-rw-r--. 1 herong herong 71680 Oct 31 05:41 gcc-backup.tar -rw-rw-r--. 1 herong herong 18289 Oct 31 05:54 gcc-backup.tar.Z
6. If you want to compress the archive file with "gzip" filter, you can use the "-z" option.
herong$ cd /home/herong herong$ tar -c -z -f gcc-backup.tar.gz gcc herong$ ls -l gcc-backup.* -rw-rw-r--. 1 herong herong 71680 Oct 31 05:41 gcc-backup.tar -rw-rw-r--. 1 herong herong 10019 Oct 31 06:01 gcc-backup.tar.gz -rw-rw-r--. 1 herong herong 18289 Oct 31 05:54 gcc-backup.tar.Z
7. Extract a compressed archive file using the "tar -x" command. No extra options are needed.
By the way, using the compress option with the "tar -c" command is equivalent to running the compress command "compress" or "gzip" on the output archive file separately.
Table of Contents
Cockpit - Web Portal for Administrator
"more", "head" and "cat" - Read Files
"split" and "cat" - Split and Join Files
"compress/uncompress" - Compressed *.Z Files
"gzip/gunzip" - Compressed *.gz Files
"xz/unxz" - Compressed *.xz or *.lzma Files
►"tar -c" and "tar -x" - Create and Extract Archive Files
"zip" and "unzip" - Create and Extract ZIP Files
"Operation not permitted" Error on macOS
SELinux - Security-Enhanced Linux
SSH Protocol and ssh/scp Commands
Software Package Manager on CentOS - DNF and YUM
vsftpd - Very Secure FTP Daemon