Ubuntu 22.04 Compression: Difference between revisions
(Created page with "Tar is not a compression tool, but rather a file archiving utility commonly used on Linux systems. The tar command is used to create an archive of one or more files and directories, which can then be compressed using a compression tool like gzip, bzip2, xz, or zstd to reduce its size for storage or transfer purposes. Tar can also be used to extract files from an archive, or to list the contents of an archive without extracting it. There are several types of compression...") |
No edit summary |
||
Line 1: | Line 1: | ||
==Tar== | |||
Tar is not a compression tool, but rather a file archiving utility commonly used on Linux systems. The tar command is used to create an archive of one or more files and directories, which can then be compressed using a compression tool like gzip, bzip2, xz, or zstd to reduce its size for storage or transfer purposes. Tar can also be used to extract files from an archive, or to list the contents of an archive without extracting it. | Tar is not a compression tool, but rather a file archiving utility commonly used on Linux systems. The tar command is used to create an archive of one or more files and directories, which can then be compressed using a compression tool like gzip, bzip2, xz, or zstd to reduce its size for storage or transfer purposes. Tar can also be used to extract files from an archive, or to list the contents of an archive without extracting it. | ||
Line 16: | Line 17: | ||
==Gzip tar compression== | ===Gzip tar compression=== | ||
The tar command is a utility used on Linux systems to create and manage archives of files and directories. The -zcvf options are used to create a compressed archive of one or more files or directories using gzip compression. | The tar command is a utility used on Linux systems to create and manage archives of files and directories. The -zcvf options are used to create a compressed archive of one or more files or directories using gzip compression. | ||
Line 84: | Line 85: | ||
Also note that if you are extracting the archive on a different system or with a different user account, the file permissions may not be preserved exactly as they were in the original system, depending on the destination file system and user account settings. | Also note that if you are extracting the archive on a different system or with a different user account, the file permissions may not be preserved exactly as they were in the original system, depending on the destination file system and user account settings. | ||
==Gzip Tar Extraction== | ===Gzip Tar Extraction=== | ||
The basic syntax for extracting a tar archive compressed with gzip is as follows: | The basic syntax for extracting a tar archive compressed with gzip is as follows: |
Latest revision as of 19:42, 11 May 2023
Tar
Tar is not a compression tool, but rather a file archiving utility commonly used on Linux systems. The tar command is used to create an archive of one or more files and directories, which can then be compressed using a compression tool like gzip, bzip2, xz, or zstd to reduce its size for storage or transfer purposes. Tar can also be used to extract files from an archive, or to list the contents of an archive without extracting it.
There are several types of compression available on Linux, including:
- gzip: This is a widely used compression tool that uses the Lempel-Ziv algorithm. It is commonly used to compress individual files and is often used in combination with the tar command to create compressed archives.
- bzip2: This is another popular compression tool that uses the Burrows-Wheeler algorithm. It is often used for compressing larger files, as it typically achieves higher compression ratios than gzip.
- xz: This is a newer compression tool that uses the LZMA algorithm. It is often used to compress large files, as it can achieve very high compression ratios.
- lz4: This is a high-speed compression tool that is designed for use in real-time systems. It is often used for compressing data in network communications and storage systems.
- zstd: This is a relatively new compression tool that uses the Zstandard compression algorithm. It is designed to offer high compression ratios and fast compression and decompression speeds.
In addition to these tools, there are also several other compression tools available on Linux, including compress, pack, and rar, among others.
Gzip tar compression
The tar command is a utility used on Linux systems to create and manage archives of files and directories. The -zcvf options are used to create a compressed archive of one or more files or directories using gzip compression.
Syntax
tar -zcvf archive_name.tar.gz file_or_directory
tar -zcvf /path/store/archive.tar.gz /home/$USER/Documents/testDIR
Options
The -z option is used to compress the archive using gzip. Without this option, the archive will not be compressed.
The -c option is used to create a new archive. Without this option, the command will attempt to extract files from an existing archive.
The -v option is used to display the progress of the archive creation. Without this option, the command will run silently.
The -f option is used to specify the filename of the archive.
Compressing a single file
To create a compressed archive of a single file, you can use the following command:
tar -zcvf file_name.tar.gz file_name
This will create a new compressed archive called file_name.tar.gz in the current directory, and include the contents of file_name in the archive.
Compressing a directory
To create a compressed archive of a directory and its contents, you can use the following command:
tar -zcvf directory_name.tar.gz directory_name
This will create a new compressed archive called directory_name.tar.gz in the current directory, and include the contents of directory_name and its subdirectories in the archive.
Compressing multiple files and directories
To create a compressed archive of multiple files and directories, you can list them all as arguments to the tar command:
tar -zcvf archive_name.tar.gz file_or_directory_1 file_or_directory_2 ...
This will create a new compressed archive called archive_name.tar.gz in the current directory, and include the contents of all specified files and directories in the archive.
Compressing and excluding files or directories
You can use the --exclude option to exclude specific files or directories from the archive. For example, to exclude a directory named exclude_dir from an archive, you can use the following command:
tar -zcvf archive_name.tar.gz --exclude=exclude_dir file_or_directory
This will create a new compressed archive called archive_name.tar.gz in the current directory, and include the contents of file_or_directory in the archive while excluding the exclude_dir directory.
To exclude multiple files or directories using the --exclude option with the tar command, you can separate them with a space character. For example, to exclude two directories named exclude_dir1 and exclude_dir2 from an archive, you can use the following command:
tar -zcvf archive_name.tar.gz --exclude=exclude_dir1 --exclude=exclude_dir2 file_or_directory
This will create a new compressed archive called archive_name.tar.gz in the current directory, and include the contents of file_or_directory in the archive while excluding the exclude_dir1 and exclude_dir2 directories.
Preserve file permissions
To preserve file permissions when creating a compressed archive with tar, you can use the -p option. The -p option stands for "preserve permissions", and it tells tar to include the file permissions (ownership, group, and mode) in the archive.
So, to create a compressed archive of a file or directory and preserve its permissions, you can use the following command:
tar -zcvpf archive_name.tar.gz file_or_directory
Note that the -p option should be used with caution, as it can potentially create security vulnerabilities if you are extracting the archive as a privileged user. It's generally recommended to use the -p option only when necessary and with a clear understanding of its implications.
Also note that if you are extracting the archive on a different system or with a different user account, the file permissions may not be preserved exactly as they were in the original system, depending on the destination file system and user account settings.
Gzip Tar Extraction
The basic syntax for extracting a tar archive compressed with gzip is as follows:
tar -zxvf archive_name.tar.gz
Options:
The -z option is used to decompress the archive using gzip.
The -x option is used to extract the contents of the archive.
The -v option is used to display the progress of the extraction.
The -f option is used to specify the filename of the archive.
View the contents of a gzip archive without extracting it using zcat
The zcat command is similar to the cat command, but it is used specifically for viewing the contents of gzip compressed files.
Here's an example command to view the contents of a gzip archive without extracting it:
zcat archive_name.tar.gz | less
This command will display the contents of the gzip archive in the less pager, allowing you to scroll through the contents of the archive without extracting it.
If you want to search for a specific file or pattern within the archive, you can pipe the output of zcat to the grep command. Here's an example command to search for a file named file.txt within a gzip archive:
zcat archive_name.tar.gz | grep file.txt
This command will display any lines within the gzip archive that contain the string "file.txt".
Note that the zcat command is only used for viewing the contents of gzip compressed files. If you are working with a tar archive that has been compressed with gzip, you will need to use the tar command in conjunction with zcat to view the contents of the archive. Here's an example command to view the contents of a tar archive compressed with gzip without extracting it:
zcat archive_name.tar.gz | tar tvf -
This command will display a listing of the files and directories within the tar archive, allowing you to see its contents without extracting it.
Extracting a single file
To extract a single file from a compressed tar archive, you can use the following command:
tar -zxvf archive_name.tar.gz path/to/file
This will extract the specified file from the compressed archive.
Extracting a directory
To extract a directory and its contents from a compressed tar archive, you can use the following command:
tar -zxvf archive_name.tar.gz path/to/directory
This will extract the specified directory and its contents from the compressed archive.
Extracting multiple files and directories
To extract multiple files and directories from a compressed tar archive, you can list them all as arguments to the tar command:
tar -zxvf archive_name.tar.gz file_or_directory_1 file_or_directory_2 ...
This will extract the specified files and directories from the compressed archive.
Extracting to a specific directory
To extract the contents of a compressed tar archive to a specific directory, you can use the -C option followed by the directory path. For example:
tar -zxvf archive_name.tar.gz -C /path/to/directory
This will extract the contents of the compressed archive to the specified directory.