Linux Command Line Cheat Sheet
Abstract
The following examples may be typed in the terminal, but copy/paste will work fine (be sure to omit the prompt). * To copy in Firefox: press CTRL-C * To paste into a terminal: press SHIFT-CTRL-V (or Edit->Paste)
Many of these examples will use the "cat example.txt | command" syntax. This is safer than the equivalent syntax of "command < example.txt".
Why? Most everyone learning the Unix/Linux commandline has accidentally reversed the "<" sign (read) with the ">" sign (write), accidentally overwriting a file. The syntax of "cat example.txt | command" is therefore safer. Please feel free to use whatever syntax you are most comfortable with.
On a related note, "There is more than one way to do it," as Larry Wall once said. You may come up with different ways to perform the following, and perhaps better ways as well. Feel free to share your CLI Kung Fu with us for possible inclusion!
Where to Acquire
These tools are installed natively in most Unix/Linux distributions, as well as OS X.
Examples/Use Case
awk
Print the length of each line of a file (/etc/passwd in this case), followed by the line itself:
$ cat /etc/passwd | awk '{print length, $0;}'
$ cat /var/log/apache2/access.log | awk -F "Mozilla/" '{print $2}'
$ cat domains.txt | awk -F "." '{print $(NF)}'
checksums
Generate the MD5 checksum of a file:
$ md5sum /etc/passwd
$ sha1sum /etc/passwd
$ shasum /etc/passwd
$ shasum -a1 /etc/passwd
$ shasum -a256 /etc/passwd
$ shasum -a512 /etc/passwd
cut
Cut the 2nd field from a file, using the space as a delimiter:
$ cat /var/log/dpkg.log | cut -d' ' -f2
$ cat /etc/passwd | cut -d: -f6
$ cat /labs/honeytokens/pilots.csv | cut -d, -f2-3
$ cat /var/log/dpkg.log | cut -d' ' -f7-
$ cat /var/log/apache2/access.log | cut -d\" -f6
$ ifconfig | cut -c11-
file
Determine the file type, using the file's magic bytes:
$ file /usr/local/bin/*
grep
Search for lines containing the string "bash", case sensitive:
$ grep bash /etc/passwd
$ grep -i bash /etc/passwd
$ grep -vi bash /etc/passwd
$ grep -A5 root /etc/passwd
head
Print the first 10 lines of a file:
$ head -n 10 /etc/passwd
sed
grep for lines containing "Mozilla", then change "Mozilla" to "MosaicKilla":
$ grep Mozilla /var/log/apache2/access.log | sed "s/Mozilla/MosaicKilla/g"
$ grep Mozilla /var/log/apache2/access.log | sed "s/^.*Mozilla//g"
$ grep Mozilla /var/log/apache2/access.log | sed "s/^.*Mozilla/Mozilla/g"
sort
The following examples will run strings on a file, search for user-agent (ignore case), and use various sort options
Simple alphabetic sort (may include duplicates)
$ strings /pcaps/fraudpack.pcap | grep -i user-agent | sort
$ strings /pcaps/fraudpack.pcap | grep -i user-agent | sort -u
$ strings /pcaps/fraudpack.pcap | grep -i user-agent | sort | uniq
$ strings /pcaps/fraudpack.pcap | grep -i user-agent | sort | uniq -c
$ strings /pcaps/fraudpack.pcap | grep -i user-agent | sort | uniq -c | sort -n
$ strings /pcaps/fraudpack.pcap | grep -i user-agent | sort -u | awk '{print length, $0}' | sort -rn
$ cat /bonus/alexa/top-1m.csv sort -t, -k2
wc
Determine number of lines in a file (the flag is the letter "ell", not the number one):
$ wc -l /etc/passwd
xxd
xxd creates a hexdump, or converts a hexdump into binary. A lot of malware hex-encodes web traffic or malicious payloads (such as DOS executables) in order to avoid signature matching. Useful hex patterns to look for are 4d5a90 (the magic bytes for a DOS executable: "MZ<90>"), and "DOS mode" (444f53206d6f6465, see commands below).
xxd cannot natively handle percent-encoded hex, such as "%63%67%69%2D%62%69%6E", but can if the percent signs are removed (see below).
Convert the string "DOS mode" to hex, grouped in sets of 4 hex characters (default):
$ echo -n "DOS mode" | xxd
0000000: 444f 5320 6d6f 6465 DOS mode
$ echo -n "DOS mode" | xxd -g0
0000000: 444f53206d6f6465 DOS mode
$ echo 444f53206d6f6465 | xxd -r -p
DOS mode
echo "%63%67%69%2D%62%69%6E" | sed "s/\%//g" | xxd -r -p
cgi-bin
A printable PDF version of this cheatsheet is available here: LinuxCLI