Linux Cheat Sheet

2021-04-29 (Edited 2022-12-19)

This post decribes cheats that you can use within Linux distros.

There are tons of commands and tons of arguements, so this will help you recall the powerful commands you have used but have forgotten how to type.

apt

1. List All Available Versions of a Package on apt

apt list -a <package-name>

2. Install Specific Version of a Package

apt install <package-name>=<package-version>

3. List Installed Packages on the Device

apt list --installed

Additionally, If you want to list specific packages,
use grep, or:

apt list --installed <package-name>

du

du is used to show the disk usage of directories and files.
It can also show the disk usage recursively.

Show the disk usage of the current directory, recursively.

du -sh .

Show the disk usage of the childs of the current directory.
s flag summarizes, and h flag shows sizes as human readable.

du -sh *

However, the above command does not list hidden files, like .vimrc.
Use the below command to show all including hidden files.
The pattern .[^.]* will take items that start with . but not ..*, which contains a parent directory.

du -sh .[^.]* *

which

which is to locate where the executable is.
However, keep in mind that which does not work with aliased commmands. For example, it does not print out anything about python, but python3.

whereis

whereis is to locate the executable, source, and man pages are.
It may not work with aliases.

whatis

man ... is sometimes too long to read, and if you want to know the basic functionality of the command not all the details, use whatis.

whatis javac
# javac (1) - Reads Java class and interface definitions and compiles them into bytecode and class files.

nslookup

The command gets a domain name, query to a DNS server, and outputs an corresponding IP address. It accepts a DNS server IP address optionally. It also works on Windows Powershell.

nslookup <domain-name> <DNS-IP-address>

lsof

Get PIDs of listening to specific ports.

sudo lsof -i :<port>

netstat

Alternate of lsof, netstat shows protocol, local/foreign address and state, which is more expressive.

netstat --tcp -p | grep <port>

Useful flags are:

  • -n: Shows numerical addresses instead of symbolic addresses (e.g. localhost > 127.0.0.1)
  • -p: Shows PIDs and program names.
  • -l: Shows only listening sockets.
  • -a: Shows both listening and non-listening sockets.

kill

To kill a process with PID.

kill [-9] <PID>

Adding -9 flag will send SIGKILL instead of default SIGTERM signal, while SIGTERM is a gentle and able-to-handle signal to terminate a process, and SIGKILL is a forcible signal and cannot be ignored.
Check this GNU webpage link for more.

file

It determines each given file type and shows the information.

file 404.html placeholder.mp4
$ 404.html:  HTML document, ASCII text
$ placeholder.mp4:  ISO Media, MP4 Base Media v1 [ISO 14496-12:2003