Skip to Content

Compress directory in Linux with tar command and zip command

Compress directory with tar command in Linux

To compress a directory in Linux, you can use the tar command to create an archive and then compress it with gzip.

Here’s how you can do it step by step:

1. Open a terminal window.
2. Navigate to the directory that contains the folder you want to compress using the cd command.
3. Use the tar command to create an archive of the directory and then compress it with gzip.

Here’s the command you would use:

tar -czvf name-of-archive.tar.gz /path/to/directory

Here’s what each option means:

  • – c: Create a new archive.
  • – z: Compress the archive with gzip.
  • – v: Verbosely list files processed (optional, you can omit this if you don’t want to see the list).
  • – f: Use the following argument as the name of the archive file.


Replace name-of-archive.tar.gz with whatever you want to name your compressed archive, and replace /path/to/directory with the path to the directory you want to compress.

To view the content of name-of-archive.tar.gz without decompression, run the following command:

$ tar -tf name-of-archive.tar.gz

To extract the files from the compressed file, you can use the tar -xvzf command followed by the name of the compressed file.

For example,

tar -xzvf name-of-archive.tar.gz

Compress directory with zip command in Linux

If you specifically want to use the zip command to compress a directory, the process is slightly different. The zip command both archives and compresses files and directories into a .zip file. Here’s how you would use it:

zip -r /path/to/directory

The options here are:

– -r: Recursively compress the directory and all files within it.

Again, replace with the desired name for your compressed file, and /path/to/directory with the path to the directory you want to compress.

we can use the unzip -l option to list the files in a Zip archive:

$ unzip -l

zipinfo is a utility command in Unix-like systems that provides detailed information about the contents of a ZIP file.

It is often used to list the files contained in a ZIP archive and to examine the properties of those files without extracting them.

zipinfo gives you a detailed look at the compression information, which can be useful for verifying the integrity and composition of a ZIP file.

Both of these commands will leave you with a compressed file of your directory that you can move, upload, or store as you see fit.

Tar command examples in Linux

The tar command is used to create an archive from a group of files or directories and -cvzf are options that tell tar to create an archive (c), list the files being archived (v for verbose), compress it with gzip (z), and specify the filename of the archive (f). Here are some examples of how to use tar -cvzf:

Example 1: Archive a Single Directory
To create a compressed archive of a single directory called mydir, you would use:

tar -cvzf mydir.tar.gz mydir/

This command will create a gzip-compressed archive named mydir.tar.gz from the contents of the mydir directory.

Example 2: Archive Multiple Directories
If you want to archive multiple directories, such as dir1 and dir2, into a single archive file, you can do:

tar -cvzf dirs.tar.gz dir1/ dir2/

This will create a single archive named dirs.tar.gz that contains both dir1 and dir2.

Example 3: Archive Files with Specific Extensions
To archive all .txt files in the current directory:

tar -cvzf texts.tar.gz *.txt

This command will create an archive file named texts.tar.gz containing all the .txt files in the current directory.

Example 4: Archive Files from a List
If you have a list of files you want to archive, you can use:

tar -cvzf archive.tar.gz -T filelist.txt

Here, filelist.txt contains a list of files, each on a separate line, that you want to include in archive.tar.gz.

Example 5: Exclude Files from the Archive
To create an archive of a directory but exclude certain files, use the –exclude option:

tar -cvzf mydir.tar.gz --exclude='*.tmp' mydir/

This will create an archive of mydir but will not include files that match the pattern *.tmp.

Example 6: Archive Files from a Find Command
You can combine find with tar to archive files that match certain criteria. For example, to archive all .jpg files modified in the last 7 days:

find . -mtime -7 -name "*.jpg" -print0 | tar -cvzf recent_photos.tar.gz --null -T -

This uses find to locate the files and then pipes the list to tar, which creates the archive.

Example 7: Create an Archive with a Different Compression
If you want to use a different compression algorithm, such as bzip2, you can change the z to j:

tar -cvjf mydir.tar.bz2 mydir/

This will create a bzip2-compressed archive named mydir.tar.bz2.

Remember to replace mydir/, dir1/, dir2/, *.txt, filelist.txt, and *.tmp with the actual names of the directories, file patterns, or files that you want to archive.

Zip command examples in Linux

The zip command is versatile and can be used in a variety of ways to create ZIP archives. Below are more examples illustrating different use cases:

Example 1: Basic ZIP Creation
To create a ZIP file of a single directory along with all its subdirectories and files:

zip -r folder_to_zip/

Example 2: ZIP Multiple Directories and Files
To zip multiple specific directories and files into a single ZIP file:

zip -r folder1/ folder2/ file1.txt file2.jpg

Example 3: Exclude Specific Files or Directories
To create a ZIP file but exclude certain files or directories:

zip -r folder_to_zip/ -x excluded_folder/* excluded_file.txt

Example 4: Zip Files with a Specific Pattern
To create a ZIP file of all .txt files in the current directory:

zip *.txt

Example 5: Create a ZIP File Without Extra Directory Paths
To zip files and directories but without including the hierarchy of directories leading up to these files:

zip -j /path/to/folder/file1.txt /another/path/to/folder/file2.jpg

Example 6: Split a ZIP File into Smaller Parts
To create a split ZIP file, where each part is a specific size (for example, 10 megabytes):

zip -r -s 10m folder_to_zip/

Example 7: Update a ZIP File
To add files to an existing ZIP file or update the files in the ZIP if they’ve changed:

zip -ur new_file.txt updated_file.txt