tar
It is one of the very commonly used commands in Unix and Linux systems. It can pack multiple files and directories into an archive file, and supports compression and decompression functions. In this article, we'll cover tar
common usage and parameters of the command.
Pack files and directories as .tar archives
To pack files or directories into an archive, use the following command:
tar -cvf archive.tar file1 file2 dir/
Among them -c
, means to create an archive file, -v
means to display detailed information, -f
means to specify the archive file name. The above command will pack all the files and subdirectories under file1
the , file2
and directory into a file named .dir
archive.tar
unpack
To unpack an archive, use the following command:
tar -xvf archive.tar
This command will archive.tar
unpack the file into the current directory. If you want to unpack the archive to a specified directory, you can use -C
the parameter:
tar -xvf archive.tar -C /path/to/directory
The above command will archive.tar
unpack the file into /path/to/directory
the directory.
Compress files or directories in gzip format
To compress the archive into gzip format, the following command can be used:
tar -czvf archive.tar.gz file1 file2 dir/
Among them, -z
means to use the gzip compression algorithm. The above command will pack all the files and subdirectories under the file1
, file2
and directory into a compressed file named .dir
archive.tar.gz
unzip
To uncompress a gzip-formatted archive, use the following command:
tar -xzvf archive.tar.gz
This command will archive.tar.gz
extract the file to the current directory.
List archive contents
To list the files and directories contained in the archive, the following command can be used:
tar -tvf archive.tar
This command will list archive.tar
all files and directories contained in the file.
Add files to archive
To add a file to an existing archive, the following command can be used:
tar -rvf archive.tar file3
This command will file3
append the file to archive.tar
the end of the file.
Delete files from archive
To remove a file from an existing archive, the following command can be used:
tar -dvf archive.tar file2
This command will archive.tar
delete file2
the file from the file.