Linux and Bash Commands

apt — Package Manager

CommandDescriptionsudo?
sudo apt updateRefresh package listyes
sudo apt upgradeUpgrade all packagesyes
sudo apt install <pkg>Install packageyes
sudo apt remove <pkg>Remove packageyes
apt search <query>Search packagesno
apt show <pkg>Show package detailsno
apt listList installed packagesno
sudo apt update && sudo apt upgrade -y   # update all

Python packages: named python3-<name> in apt (e.g., sudo apt install python3-numpy)

apt = modern replacement for apt-get — use apt in scripts/terminal.

Keyboard Shortcuts

ShortcutAction
Ctrl+rSearch command history
Ctrl+cKill current process
Ctrl+zSuspend process → resume with fg
Ctrl+dEOF / close shell
Ctrl+lClear screen
TABAutocomplete
Ctrl+a / Ctrl+eLine beginning / end
Ctrl+wDelete word before cursor

The Five Fingers of Death

1. find + grep

# find: locate files
find . -name '*.py'                    # all Python files
find . -name '*.log' -delete           # find and delete
find /home -type f -perm 644           # by permissions
 
# grep: search file contents
grep "pattern" file.txt                # basic
grep -i "word" file.txt                # case-insensitive
grep -n "word" file.txt                # show line numbers
grep -r "word" /path/                  # recursive
grep -v "word" file.txt                # invert (exclude matches)
grep -c "word" file.txt                # count matches

2. tr + cut

# tr: translate/delete characters
echo 'HELLO' | tr 'A-Z' 'a-z'         # lowercase → hello
echo '2024-08-12' | tr -d '-'          # delete chars → 20240812
echo 'a  b   c' | tr -s ' '            # squeeze spaces → a b c
 
# cut: extract fields
cut -f2 file.tsv                        # 2nd tab-separated field
cut -d',' -f2,4 file.csv               # CSV: 2nd and 4th fields
cut -f2-4 file.tsv                      # fields 2 through 4

3. sort + uniq

# sort
sort file.txt                           # alphabetical
sort -r file.txt                        # reverse
sort -n numbers.txt                     # numeric sort
sort -u file.txt                        # sort + deduplicate
 
# uniq (must sort first — only compares adjacent lines)
sort file.txt | uniq                    # remove duplicates
sort file.txt | uniq -d                 # show only duplicates
sort file.txt | uniq -u                 # show only unique lines
sort file.txt | uniq -c                 # count occurrences

4. head + tail

head file.txt                           # first 10 lines
head -n 5 file.txt                      # first 5 lines
tail file.txt                           # last 10 lines
tail -n 5 file.txt                      # last 5 lines
tail -f file.txt                        # follow live updates

5. tree + tee

tree                                    # directory tree
tree -L 2                               # limit depth to 2 levels
 
# tee: write to file AND stdout simultaneously
command | tee output.txt
echo "255" | sudo tee /sys/class/leds/usr_led0/brightness  # sudo + tee pattern

Redirection

command > file          # stdout → file (overwrite)
command >> file         # stdout → file (append)
command < file          # file → stdin
command 2> /dev/null    # discard stderr
command > /dev/null     # discard stdout
command >&2             # redirect stdout to stderr
commandA | commandB     # pipe A's stdout to B's stdin
commandA | tee file     # pipe to file AND stdout

sudo + tee pattern (when sudo echo > file fails):

echo "value" | sudo tee /path/to/root-owned-file

ssh

ssh user@hostname                   # connect
ssh user@192.168.1.100             # connect by IP
ssh user@host "command"            # run command on remote host

rsync

rsync -av src/ user@host:dest/     # push to remote (verbose, archive)
rsync -av user@host:src/ dest/     # pull from remote

Only transfers differences — efficient for repeated syncs. -a = archive (preserves permissions/timestamps).

tar + zip

# Create archives
tar -czf archive.tar.gz folder/    # compress with gzip
zip -r archive.zip folder/         # zip
 
# Extract archives
tar -xzf archive.tar.gz            # extract gzip
tar -xzf archive.tar.gz -C ~/out/ # extract to specific dir
unzip archive.zip -d ~/output/     # extract zip
 
# Options: -c create, -x extract, -z gzip, -j bzip2, -f file, -C directory

See Also