Compilation of Linux commands and shortcuts I use often.
I’ve made this list for my own reference, but who knows, you might learn something from it too.
Terminal keyboard shortcuts#
- CTRL+L -> C[l]ears screen. Acts like
clear -x, so it places the current line at the top of the screen - CTRL+E -> Move cursor to [e]nd of line
- CTRL+A -> Move cursor to start of line
- CTRL+R -> [R]everse search in the command history. Press it again to cycle through matching commands
- CTRL+W -> Deletes [w]ord before the cursor
- CTRL+K -> Deletes from cursor to start of line
- CTRL+X+E -> Open line in default text [e]ditor. Useful for changing something in very long commands
- CTRL+Z -> Suspends current process and moves it to background. Bring it back using
fgor make it run in the background withbg - CTRL+D at an empty prompt -> Similar to
exit, useful for [d]isconnecting from SSH sessions - CTRL+D with text input -> [D]eletes character at cursor
- ALT+B -> Move cursor [b]ack one word
- ALT+F -> Move cursor [f]orward one word
Commands#
General#
sudo !!-> Runs previous command (!!) withsudoecho $((RANDOM % 100 + 1))-> Generates random number 0-100echo $((2#1111))-> Converts binary (1111) to decimalecho $((16#1a))-> Converts hexadecimal (1a) to decimaltr -dc '[:alnum:]' < /dev/urandom | head -c 32-> Generates random alphanumeric passwordtime [command]-> Shows time it took to execute a command[command] | tee output.txt-> Redirect output to terminal and file at the same timehostnamectl-> Displays hostname and other goodies
Processes#
systemctl list-units --type=service-> Lists all active servicessystemctl edit [service]-> Edit configuration of a servicesystemctl --failed-> Lists services that failed to startps aux --sort=-%cpu | head-> Display top CPU-consuming processes.w-> Displays users logged in and what they are doing
Networking#
ip a-> Show network interfaces infoip link set [interface] up|down-> Set interface up or downip addr add|del ip/mask dev [interface]-> Add or delete IP address to interfacecurl ifconfig.me-> Retrieve public IP address. Usecurl ifconfig.me/allfor moress -tunap-> Lists all TCP and UDP sockets with their state of connection, PID that owns it and local/remote addressesss -tunalp-> Lists all TCP and UDP sockets that are listening, along with PID and addressesnc -l -p [port]-> Listen on portip route show-> Shows kernel’s routing tablenslookup example.comanddig example.com-> DNS lookup utilities
Working with files/logs#
grep -r "pattern"-> Searches for a pattern recursively in all filestail -f [path]-> Outputs logs in real time. You can furthergrepthe output to get exactly what you’re looking fordd if=/dev/zero of=file bs=1M count=100-> Creates 100MB file filled with zerosrsync --dry-run[source] [destination] -> Simulates rsync operation without actually doing anything
Disks#
lsblk-> List block devices (disks)df -h-> See disk space usage in a human readable formatdu -sh *-> See disk space used by each file and directory in current locationparted /dev/sdX-> Disk partitioning toolfindmnt-> Displays info about mounted file systemssudo mkfs.ext4 /dev/sdX-> Creates ext4 filesystem. Replaceext4with filesystem of choice
LVM#
pvdisplay-> Display info about physical volumesvgdisplay-> Display info about volume groupslvdisplay-> Dsiplay info about logical volumessudo lvcreate -L 10G -n lv_name vg_name-> Create 10GB logical volumesudo lvextend -L +10G /dev/vg_name/lv_name-> Extend logical volume by 5GBsudo lvreduce -L -3G /dev/vg_name/lv_name-> Shrink logical volume by 3GB
Archives and compression#
Add v to any of these to make them verbose
tar cf output.tar file1 file2-> [c]reate archive and write it to [f]iletar czf output.tar.gz file1 file2-> [c]reate g[z]ipped compressed archive and write it to [f]iletar xf source.tar-> Extract archive [f]ile into current directorytar tf source.tar-> Lis[t] contents of tar [f]ilezip -r archive.zip file1 directory1-> Creates archive with specified files and directorieszip -r -[0-9] archive.zip file1 directory1-> Creates archive with specific compression level (0 is lowest, 9 is highest)zip -r --encrypt archive.zip file1 directory1-> Creates encrypted archive with passwordunzip archive.zip-> Extracts from archive into current directoryunzip -l archive.zip-> Lists contents of an archive. Does not extract anything
If you want to suggest any commands, put them in the comments, I would love to see them.