How to compress files in linux

How to Compress, Uncompress and view Compressed files in Linux

Compressing files helps you to conserve disk space. This section describes how to use the compress command to reduce the size of a file. You can use the compress command to reduce the size of a file. You can compress large files to reduce the use of disk space as well as the time required for file transfers over a network. The amount of compression depends on the type of file you compress. Typically, compression reduces a text file by 50 percent to 60 percent.

The syntax for the compress command is:

When you compress a file, the compress command replaces the original file with a new file that has a .Z extension. The ownership and modification time of the original file remain the same, but the contents of the file change.

Compressing a File

The following example shows you how to compress a file called files.tar, using the -v (verbose) option. Using this option with the compress command displays a message providing information about the percentage reduction or expansion of each file.

$ compress -v files.tar files.tar: Compression: 70.20% -- replaced with files.tar.Z 

The compressed file, which replaces the files.tar file, is called files.tar.Z. When a file has a .Z extension it is a compressed file, and you should not view or print such a file without first uncompressing it.

NOTE: When you compress a file that has already been compressed, the file size increases, instead of becoming smaller. When you rename a file that has already been compressed and you perform the compress command on it once again, the file size increases, instead of becoming smaller.

If you do not use the-v(verbose) option with the compress command, no output appears when you perform the command. For example:

Viewing Compressed Files by Using the zcat Command

You can print the uncompressed form of a compressed file to standard output. This section describes how to view the uncompressed form of a compressed file by using the zcat command. You can use the zcat command to view files that were compressed by using the compress command. The zcat command interprets the compressed data and displays the contents of a file as if it were not compressed. The zcat command does not change the contents of a compressed file.

The syntax for the zcat command is:

NOTE: The zcat filename command is functionally identical to the uncompress -c filename command.

Viewing the Contents of a Compressed File

The following example shows you how to view the contents of the compressed file called dante.Z.

$ zcat dante.Z | more This is a sample file to test the functionality of zcat on linux operating systems 

You can use the pipe (|) character with the zcat command to extract the contents of a compressed tar file without uncompressing the tar file first. For example:

Читайте также:  Alt linux запуск команды

The dash (-) at the end of the tar command indicates that the tar command should read the tar file from standard input instead of reading the tar file from a file or tape. In this example, the standard input comes from the output of the zcat command because of the pipe (|).

Uncompressing Files by Using the uncompress Command

You can restore a compressed file to its original state after it has been compressed. This section describes how you can uncompress files by using the uncompress command. The uncompress command restores a compressed file back to its original state. The syntax for the uncompress command is:

uncompress options filename 

Uncompressing a File

The following example shows you how to uncompress the files.tar.Z file and replace it with the original file named files.tar. The -v option causes the uncompress command to display messages about the action being performed.

$ uncompress -v files.tar.Z files.tar.Z: -- replaced with files.tar $ 

Viewing the Contents of a Compressed File

You can use the uncompress command with the -c option to send the contents of a compressed file to the screen (stdout), without changing the compressed (.Z) file. You can use the pipe (|) character to send the output of the uncompress command to another program. You can use the tar command to list the contents of the file that the uncompress command is reading.

$ uncompress -c files.tar.Z | tar tvf- tar: blocksize = 11 -rw-rw---- 1233/10 1610 May 7 14:12 2020 file1 -rw-rw---- 1233/10 105 May 7 14:12 2020 file2 -rw-rw---- 1233/10 218 May 7 14:12 2020 file3 

The dash (-) at the end of the command line indicates that the tar command reads the data from the piped output of the uncompress command rather than a tar file or a tape.

Compressing a File by Using the gzip Command

You can also use the gzip command to reduce the size of files. This section describes how to compress a file by using the gzip command. The gzip command reduces the size of files. When the gzip command successfully compresses a file, the original file is replaced by a file with the same name and a.gz extension. The files keep the same ownership modes, access, and modification times. The gzip command is used to compress regular files. The syntax for the gzip command is:

The following example shows you how to compress the file1, file2, file3, and file4 files with the gzip command.

$ gzip file1 file2 file3 file4 $ ls *.gz file1.gz file2.gz file3.gz file4.gz 

NOTE: The gzip command performs the same general function as the compress command, but the gzip command generally produces smaller files.

Restoring a gzip File by Using the gunzip Command

You can use the gunzip command to restore a file that has been compressed with the gzip command. The following example shows you how to uncompress the file1.gz file.

Viewing Files by Using the gzcat Command

You can use the gzcat command to look at a compressed file. This section describes how to view files by using the gzcat command. You can use the gzcat command to view files that were compressed with either the gzip command or the compress command.

Читайте также:  Linux create folder and files

The gzcat command interprets the compressed data and displays the contents of the file as if it were not compressed. The gzcat command does not change the contents of the compressed file. The compressed file remains on the disk in compressed form. The syntax for the gzcat command is:

Viewing the Contents of a Compressed File

You can use the gzcat command to view the contents of a compressed tar file without having to first uncompress the file. The following example shows you how to view the file1.gz file.

$ gzip file1 $ ls file1* file1.gz $ gzcat file1.gz The compress utility reduces the size of files using adaptive Lempel-Ziv coding. Each file is renamed to the same name plus the extension .Z. A file argument with a .Z exten-sion will be ignored except it will cause an error exit after other arguments are processed. If compression would not reduce the size of a file, the file is ignored. 

Compressing and Archiving Multiple Files by Using the zip Command

You can compress and archive files with a single command. The following section describes how to perform this combined task by using the zip command. The zip command compresses multiple files into a single archive file. The zip command adds the .zip extension to the file name of the compressed archive file if you do not assign a new file name with an extension.

NOTE: You can perform the zip command or the unzip command on the command line to view a list of options used with each command.

The syntax for the zip command is:

zip target_filename source_filenames 

The following example shows you how to use the zip command to compress the file2 and file3 files into the file.zip archive file.

$ zip file.zip file2 file3 adding: file2 (deflated 16% adding: file3 (deflated 26%) $ ls file.zip file2 file3 

To list the files in a zip archive, perform the unzip -l command.

Restoring a zip File by Using the unzip Command

You can uncompress the contents of a zip file by using the unzip command. The following example shows you how to uncompress the file.zip archive file.

NOTE: The jar command and the zip command create files that are compatible with each other. The unzip command can uncompress a jar file, and the jar command can uncompress a zip file.

Источник

Linux tar Command – How to Compress Files in Linux

Zaira Hira

Zaira Hira

Linux tar Command – How to Compress Files in Linux

File compression is an essential utility across all platforms. It helps you reduce file size and share files efficiently. And compressed files are also easier to copy to remote servers.

You can also compress older and rarely used files and save them for future use which helps you conserve disk space.

In this post, we’ll look at how to compress files with the tar command in Linux, along with some examples of tar in action.

What is the tar command?

We use the tar command to compress and expand files from the command line. The syntax is shown below:

tar [flags] destinationFileName sourceFileName

The tar command uses the following flags to customize the command input:

Flag Explanation Usage
-c Create a new archive. We use this flag whenever we need to create a new archive.
-z Use gzip compression. When we specify this flag, it means that archive will be created using gzip compression.
-v Provide verbose output. Providing the -v flag shows details of the files compressed.
-f Archive file name. Archive file names are mapped using the -f flag.
-x Extract from a compressed file. We use this flag when files need to be extracted from an archive.
Читайте также:  Build with chrome linux

How to create an archive

We have a list of the following files which we’ll compress with tar .

image-1

To compress them, we’ll use tar like this:

tar -czvf logs_archive.tar.gz *

Let’s break down this command and look into each flag.

-c is creating and archive.

-z is using gzip compression.

-v is providing details of the files that have been archived.

-f is creating an archive with the name ‘logs_archive.tar.gz’ as supplied in the command above.

In the results below, we can see that the archive has been created successfully.

image-13

How to remove files after compression

Let’s say we don’t want to keep the original files after creating an archive. For that, we can use the —remove-files flag.

tar -czvf logs_archive.tar.gz * --remove-files

image-3

Here, the -czvf flags are working as demonstrated before, but the original files are also removed. Once we list the files, we only see the archive.

How to view the contents of an archive

You might need to view the contents of an archive without actually extracting it. You can do this with the -t flag.

tar -tvf logs_archive.tar.gz

In this command, -t flag specifies that we need to only view the contents of the archive. -f specifies the filename and -v displays the detailed contents.

image-4

How to extract an archive

To extract files from an archive, you use the -x flag like this:

tar -xzvf logs_archive.tar.gz

Let’s break down this command and look into each flag.

-x is extracting and archive.

-z specifies that the archive is gzip.

-v is providing details of the files that have been archived.

-f is extracting from the archive named ‘logs_archive.tar.gz’.

image-14

Here’s a useful tip: commands that take a long time to execute can continue in the background with & .

Adding files to an archive and extracting an archive can take a while. To keep the commands running in the background while you keep working, pair the command with & like this:

tar -xzvf logs_archive.tar.gz &

How to search in compressed log files

You might still need to access certain files once they’re archived. Luckily, there is a method you can use to search and view compressed log files without decompressing them and compromising disk space.

The command you can use to search in compressed files is zgrep :

image-6

We can search for a string in an archive using the below command:

 zgrep -Hna 'string-to-search' compressedFile.tar.gz

Let’s briefly look at the flags.

-H lists the file name that contains the match.

-n displays the line number that contains the matched string.

-a treats all files as text files.

image-15

Wrapping Up

File compression helps us save time and resources when sharing files. Servers are almost always rotating and archiving huge log files.

You can also schedule file compression via cron jobs to automate disk cleaning. I highly recommend that you take advantage of this utility.

Thanks for reading until the end. I would love to connect with you. You can find me here on Twitter. Do share your thoughts.

Источник

Оцените статью
Adblock
detector