1. Create .tar file (uncompressed)
If you want to create an uncompressed archive, you can use the following command:
tar -cvf Archive file name.tar File or directory1 File or directory2 ...
-
-c
: Create a new archive file. -
-v
: Display file information during compression (optional). -
-f
: Specify the name of the archive file.
Example:
tar -cvf /path/to/folder1 /path/to/file1
2. Create . or .tgz file (gzip compression)
When compressing archives with gzip, you can use the following command:
tar -czvf Archive file name. File or directory1 File or directory2 ...
-
-z
: means using gzip for compression.
Example:
tar -czvf /path/to/folder1 /path/to/file1
3. Create .tar.bz2 or .tbz2 file (bzip2 compression)
When compressing archives with bzip2, you can use the following command:
tar -cjvf Archive file name.tar.bz2 File or directory1 File or directory2 ...
-
-j
: means using bzip2 for compression.
Example:
tar -cjvf .bz2 /path/to/folder1 /path/to/file1
4. Create . file (xz compression)
When compressing archives with xz, you can use the following command:
tar -cJvf Archive file name. File or directory1 File or directory2 ...
-
-J
: means using xz for compression.
Example:
tar -cJvf /path/to/folder1 /path/to/file1
5. Exclude certain files or directories during compression
If you need to exclude certain files or directories during compression, you can use--exclude
Parameters:
tar -czvf Archive file name. File or directory --exclude="File or directory to be excluded"
Example:
tar -czvf /path/to/folder --exclude="/path/to/folder/exclude_dir"
6. Compress a single file
If you only need to compress a single file, you can directly specify the file path:
tar -czvf file name. File path
Example:
tar -czvf /path/to/
7. Compress all files in the current directory
If you want to compress all files and subdirectories in the current directory, you can use the following command:
tar -czvf Archive file name. .
Things to note
- File path:Make sure the specified file or directory path is correct.
-
Permissions issues:If a compressed file or directory requires administrator permission, please use
sudo
。 -
Compression format selection:
- gzip: The compression speed is fast, but the compression rate is relatively low.
- bzip2: High compression rate, but slower speed.
- xz: The highest compression rate, but the slowest speed, suitable for scenarios with high compression rate requirements.
-
View the contents of the compressed file:If you want to view the contents of the compressed file without decompressing, you can use
-t
Parameters:
tar -tzvf Archive file name.
This is the end of this article about the common usage of Linux file compression tar. For more related Linux file compression tar content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!