Chances are you’ve come across archived or compressed files and downloaded them. To interact with them through the command line, the Linux shell comes standard with many compression / decompression and archive / extraction commands.

Let’s first talk about the compression commands which reduce file sizes and differ mainly by the algorithms behind them. Three popular compression commands are:

  • gzip: retains the original file’s ownership modes, access, and modification timestamps. Compressed files have the .gz extension.
  • bzip2: compressed files have the .bz2 extension.
  • xz: compressed files have the .xz extension.

The syntax to compress a file using one of these compression utilities is:

<compression_utility> [options] <file_name>

For example, the command gzip hello.txt will result in a compressed file named hello.txt.gz and delete the original file. Adding the -k option will retain the original file.

Multiple files can be compressed by adding all of their names as arguments or by using the wildcard symbol (*). For example:

bzip2 poem.txt riddle.txt

will result in two compressed files: poem.txt.bz2 and riddle.txt.bz2 whereas

xz *.txt

would compress all files in the current directory with the .txt extension.

The gzip command has the ability to compress all files in a directory using the -r option and providing the path to a directory.

Files can be decompressed using the -d option.

Note: For more information and options, you can refer to the manual or info pages for these commands or use the --help option for each command.

In the next exercise, we will learn about the popular archive utilities available on the Linux shell: zip and tar.



Run the command ls -R to see all the files in the current directory and subdirectories.


Compress the file poem.txt using gzip.


In one command, compress the files hello.txt and bye.txt (the two .txt files in the directory) using bzip2.


Compress the file codecademy.png with xz using the -k option.


Using bzip2 and the -d option, decompress all the files .bz2 files inside the directory riddles.

To refer to the files, you can use the path riddles/*.bz2!


Now, compress all the files in the riddles directory using gzip with the -r option.


Run the command ls -R again to see the resulting files.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?