https://www.cyberciti.biz/faq/how-to-tar-a-file-in-linux-using-command-line/
Author: Vivek GiteIam a new Linux user. How can I create a tar file in Linux operating systems using the command line option?
Introduction: A Linux tarball ( “tar.gz” or “tar.bz2” file ) is nothing but a system file format that combines and compresses multiple files. Tarballs are common file format on a Linux operating systems. Tarballs are often use for distribution of software/media or backup purposes. This page shows how to tar a file in Linux using the Linux tar command line option.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux terminal |
Category | Commands |
Prerequisites | tar command |
OS compatibility | *BSD • Linux • macOS • Unix • WSL |
Est. reading time | 3 minutes |
The procedure is as follows to tar a file in Linux:
- Open the terminal app in Linux
- Compress an entire directory by running tar -zcvf file.tar.gz /path/to/dir/ command in Linux.
- To compress a single file by running tar -zcvf file.tar.gz /path/to/filename command on Linux.
- Tar and compress multiple directories file by running tar -zcvf file.tar.gz dir1 dir2 dir3 command in Linux.
![How to tar a file in Linux using command line](https://www.cyberciti.biz/media/new/faq/2013/05/How-to-tar-a-file-in-Linux-using-command-line.png)
How to create tar a file in Linux
Say you want to compress an entire directory named /home/vivek/data/, then type:$ tar -czvf file.tar.gz /home/vivek/data/
To compress multiple directories and files, execute:$ tar -czvf file.tar.gz /home/vivek/data/ /home/vivek/pics/ /home/vivek/.accounting.db
One can use bzip2 compression instead of gzip by passing the -j option to the tar command:$ tar -cjvf file.tar.bz2 /home/vivek/data/
Where,
- -c : Create a new archive
- -v : Verbose output
- -f file.tar.gz : Use archive file
- -z : Filter the archive through gzip
- -j : Filter the archive through bzip2
How to exclude directories and files when using tar
You can exclude certain files when creating a tarball. The syntax is:$ tar -zcvf archive.tar.gz --exclude='dir1' --exclude='regex' dir1
For example, exclude ~/Downloads/ directory:$ tar -czvf /nfs/backup.tar.gz --exclude="Downloads" /home/vivek/
How do I view files stored in an archive?
Now you have an archive, to list the contents of a tar or tar.gz file using the tar command:$ tar -ztvf file.tar.gz
$ tar -jtvf file.tar.bz2
The above command would list all files verbosely.
How do I extracting an archive?
You can extract an archive or tarball with the tar command. The syntax is:$ tar -xzvf file.tar.gz
$ tar -xjvf file.tar.bz2
Want to extract the contents of the archive into a specific directory such as /home/vivek/backups/? Try passing the -C DIR option:$ tar -xzvf my.tar.gz -C /home/vivek/backups/
$ tar -xjvf archive.tar.bz2 -C /tmp/
- -x : Extract files from an archive
- -t : List the contents of an archive
- -v : Verbose output
- -f file.tar.gz : Use archive file
- -C DIR : Change to DIR before performing any operations
- --exclude : Exclude files matching PATTERN/DIR/FILENAME
Conclusion
You learned how to tar a file in Linux using tar command. For more info please tar command help page here or read it locally using the man command:$ man tar
About the author: Vivek Gite is the founder of nixCraft, the oldest running blog about Linux and open source. He wrote more than 7k+ posts and helped numerous readers to master IT topics
标签:tar,gz,How,command,file,Linux,archive From: https://www.cnblogs.com/kungfupanda/p/17020997.html