Linux and Bash Commands
apt — Package Manager
| Command | Description | sudo? |
|---|---|---|
sudo apt update | Refresh package list | yes |
sudo apt upgrade | Upgrade all packages | yes |
sudo apt install <pkg> | Install package | yes |
sudo apt remove <pkg> | Remove package | yes |
apt search <query> | Search packages | no |
apt show <pkg> | Show package details | no |
apt list | List installed packages | no |
sudo apt update && sudo apt upgrade -y # update allPython 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
| Shortcut | Action |
|---|---|
Ctrl+r | Search command history |
Ctrl+c | Kill current process |
Ctrl+z | Suspend process → resume with fg |
Ctrl+d | EOF / close shell |
Ctrl+l | Clear screen |
TAB | Autocomplete |
Ctrl+a / Ctrl+e | Line beginning / end |
Ctrl+w | Delete 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 matches2. 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 43. 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 occurrences4. 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 updates5. 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 patternRedirection
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 stdoutsudo + tee pattern (when sudo echo > file fails):
echo "value" | sudo tee /path/to/root-owned-filessh
ssh user@hostname # connect
ssh user@192.168.1.100 # connect by IP
ssh user@host "command" # run command on remote hostrsync
rsync -av src/ user@host:dest/ # push to remote (verbose, archive)
rsync -av user@host:src/ dest/ # pull from remoteOnly 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 directorySee Also
- Bash Scripting topic
- linux-basics source, bash-essentials source