Codecademy Logo

Linux Bash Utilities

Print Cheatsheet

Linux Documentation Utilities

Documentation on Linux is a great way to learn about installed utilities and commands. Linux provides many ways to access documentation:

  • The /usr/share/doc/ directory contains README files and other documents for installed commands.
  • The man command to access reference manual pages for all installed commands. Usage: man <command_name>.
  • The info command to access full detailed informational pages for all installed commands. Usage info <command_name>.

Linux Compression Utilities

Compression a file reduces its size. Linux Bash provides many utilities to compress and decompress files including gzip, bzip2, and xz. Files compressed with these commands have the extensions .gz, .bz2, and .xz respectively.

To compress, run <compression_utility> <file_name>.
To decompress, include the -d option: <compression_utility> -d <compressed_file_name>.

gzip also supports the -r option to compress all files in a directory.

Compress all `.mp4` files in current directory using `gzip`.
```
gzip *.mp4
```
Compress all files in the `videos` directory using `gzip`.
```
gzip -r videos/
```
Decompress a compressed file using `bzip2`
```
bzip2 -d background.png.bz2
```

Linux Archive Utilities

Archiving consolidates multiple files together into one file for easier storage and portability. Linux Bash has two commands for archiving and extracting files: zip and tar.

zip tar
Archive zip <archive_name>.zip <file1> <file2>.. tar -cf <archive_name>.tar <files or directory>
Extract unzip <archive_name>.zip tar -xf <archive_name>.tar

In Linux environments, tar is preferred as a file extension because a tarball can retain important metadata like file permissions.

Archiving the directory `riddles` using `zip`
```
zip riddles.zip -r riddles/
```
Archiving files using `tar`
```
tar -cf languages.tar english.txt french.txt
```

Compressing Archive Files

The zip bash command automatically compresses archived files while tar can be combined with compression utilities like gzip, bzip2, and xz. There are specific options for tar that must be used to match the compression command.

Here are the list of tar options:

  • -c : Creates an archive
  • -x : Extracts an archive
  • -f : Creates an archive with given filename
  • -z : Compress using gzip. Resulting file extension: .tar.gz.
  • -j : Compress using bzip2. Resulting file extension: .tar.bz2.
  • -J : Compress using xz. Resulting file extension: .tar.xz.
Archiving the media directory with tar using the xz compression:
$ tar -cf media.tar media/
$ xz media.tar
OR
$ tar -cJf media.tar.xz media/

Linux Network Utilities

The Linux shell has many commands to manage and communicate with networks:

  • ping <target domain or IP>: Checks connectivity between two devices on the same network by sending packets.
  • host <domain or IP>: Performs DNS resolution, the process of converting a domain name to an IP address or vice versa.
  • ifconfig: Shows network interface information.

Downloading a File in Linux

Two Linux terminal commands, curl and wget, can access, download a file from the internet, and even upload a file. Either of the following commands establishes a connection with a server and downloads either the HTML of a webpage or a file stored at the URL to the current working directory.

$ curl -O <URL>
$ wget <URL>