Linux Command Line Cheat Sheet

cheatsheet linux bash command


Bash Commands

CommandDescription
uname -aShow system and kernel
head -n1 /etc/issueShow distribution
mountShow mounted filesystems
dateShow system date
uptimeShow uptime
whoamiShow your username
man commandShow manual for command

Bash Shortcuts

ShortcutDescription
CTRL-cStop current command
CTRL-zSleep program
CTRL-aGo to start of line
CTRL-eGo to end of line
CTRL-uCut from start of line
CTRL-kCut to end of line
CTRL-rSearch history
!!Repeat last command
!abcRun last command starting with abc
!abc:pPrint last command starting with abc
!$Last argument of previous command
ALT-.Last argument of previous command
!*All arguments of previous command
^abc^123Run previous command, replacing abc with 123

Bash Variables

CommandDescription
envShow environment variables
echo $NAMEOutput value of the $NAME variable

Bash Variables (cont.)

CommandDescription
export NAME=valueSet $NAME to value
$PATHExecutable search path
$HOMEHome directory
$SHELLCurrent shell

IO Redirection

CommandDescription
cmd < fileInput of cmd from file
cmd1 <(cmd2)Output of cmd2 as file input to cmd1
cmd > fileRedirect stdout of cmd to file
cmd > /dev/nullDiscard stdout of cmd
cmd >> fileAppend stdout to file
cmd 2> fileRedirect stderr of cmd to file
cmd 1>&2Send stdout to the same place as stderr
cmd 2>&1Send stderr to the same place as stdout
cmd &> fileRedirect every output of cmd to file

Pipes

CommandDescription
cmd1 | cmd2Pipe stdout of cmd1 to cmd2
cmd1 |& cmd2Pipe stderr of cmd1 to cmd2

Command Lists

CommandDescription
cmd1 ; cmd2Run cmd1 then cmd2
cmd1 && cmd2Run cmd2 if cmd1 is successful
`cmd1
cmd &Run cmd in a subshell

Directory Operations

CommandDescription
pwdShow current directory
mkdir dirCreate directory named dir
cd dirChange directory to dir
cd ..Go up one directory
lsList files

ls Options

OptionDescription
-aShow all files (including hidden)
-RRecursive listing
-rReverse order
-tSort by last modified
-SSort by file size
-lLong listing format
-1One file per line
-mComma-separated output
-QQuoted output

Search Files

CommandDescription
grep pattern filesSearch for pattern in files
grep -iCase insensitive search
grep -rRecursive search
grep -vInverted search
grep -oShow only the matched part of file
find /dir/ -name name*Find files starting with name in dir

Search Files (cont.)

CommandDescription
find /dir/ -user nameFind files owned by name in dir
find /dir/ -mmin numFind files modified less than num minutes ago in dir
whereis commandFind binary, source, or manual for command
locate fileQuick search for file using the system index

File Operations

CommandDescription
touch file1Create file file1
cat file1 file2Concatenate files and output them
less file1View and paginate file1
file file1Determine the file type of file1
cp file1 file2Copy file1 to file2
mv file1 file2Move or rename file1 to file2
rm file1Delete file1
head file1Show the first 10 lines of file1
tail file1Show the last 10 lines of file1
tail -F file1Continuously display new lines from file1

Text Processing

CommandDescription
sort fileSort lines alphabetically
sort -n fileSort numerically
sort -r fileSort in reverse
sort -u fileSort and remove duplicates
uniqFilter adjacent duplicate lines
uniq -cCount occurrences
wc fileCount lines, words, bytes
wc -l fileCount lines only
cut -d: -f1 fileCut first field (delimiter :)
tr 'a-z' 'A-Z'Translate lowercase to uppercase
tr -d '\n'Delete newlines

sed (Stream Editor)

CommandDescription
sed 's/old/new/' fileReplace first occurrence per line
sed 's/old/new/g' fileReplace all occurrences
sed -i 's/old/new/g' fileEdit file in place
sed -n '5,10p' filePrint lines 5-10
sed '/pattern/d' fileDelete lines matching pattern

awk

CommandDescription
awk '{print $1}' filePrint first column
awk -F: '{print $1}' filePrint first column (delimiter :)
awk '/pattern/ {print}' filePrint lines matching pattern
awk '{sum+=$1} END {print sum}'Sum first column
awk 'NR==5' filePrint line 5
awk 'NF>0' filePrint non-empty lines

Networking Commands

CommandDescription
ip addrShow IP addresses
ip -4 addrShow IPv4 only
ip routeShow routing table
ip linkShow network interfaces
ss -tulnShow listening ports (TCP/UDP)
ss -tunapShow all connections with process
ss -sSocket statistics summary

Network Diagnostics

CommandDescription
ping -c 4 hostSend 4 ICMP packets
traceroute hostTrace route to host
mtr hostContinuous traceroute
dig domainDNS lookup
dig +short domainDNS lookup (short output)
nslookup domainDNS lookup (alternative)
host domainDNS lookup (simple)
curl -I urlGet HTTP headers only
curl -o file urlDownload to file
curl -X POST -d 'data' urlPOST request
wget urlDownload file
wget -r urlRecursive download

Network Configuration

CommandDescription
nmcli device statusNetworkManager device status
nmcli connection showShow connections
hostnamectlView/set hostname
resolvectl statusDNS resolver status

System Administration

systemd (Service Management)

CommandDescription
systemctl status serviceCheck service status
systemctl start serviceStart service
systemctl stop serviceStop service
systemctl restart serviceRestart service
systemctl enable serviceEnable at boot
systemctl disable serviceDisable at boot
systemctl list-units --type=serviceList all services
systemctl list-units --failedList failed units
journalctl -u serviceView service logs
journalctl -fFollow system logs
journalctl --since "1 hour ago"Logs from last hour

Disk Usage

CommandDescription
df -hDisk space (human readable)
df -iInode usage
du -sh dirDirectory size
du -h --max-depth=1Size of immediate subdirs
lsblkList block devices
fdisk -lList disk partitions
mount | column -tShow mounts formatted

User Management

CommandDescription
idCurrent user info
id usernameUser info for username
groupsShow group membership
useradd usernameCreate user
usermod -aG group userAdd user to group
passwd usernameChange password
su - usernameSwitch user
sudo -iRoot shell
lastRecent logins
wWho is logged in

Watch a Command

CommandDescription
watch -n 5 'ntpq -p'Run the ntpq -p command every 5 seconds and display its output

Process Management

CommandDescription
psDisplay a snapshot of current processes
ps auxAll processes with details
ps aux | grep nameFind process by name
topShow real-time processes
htopInteractive process viewer
kill pidTerminate process with ID pid
kill -9 pidForce kill process
pkill nameTerminate process with name name
killall nameTerminate all processes starting with name
pgrep nameGet PID by name
nohup cmd &Run command immune to hangups
jobsList background jobs
fgBring job to foreground
bgResume job in background

Nano Shortcuts

Files

ShortcutDescription
Ctrl-RRead file
Ctrl-OSave file
Ctrl-XClose file

Cut and Paste

ShortcutDescription
ALT-AStart marking text
CTRL-KCut marked text or line
CTRL-UPaste text
ShortcutDescription
ALT-/Go to end of file
CTRL-AGo to beginning of line
CTRL-EGo to end of line
CTRL-CShow line number
CTRL-_Go to a specific line number

Search File

ShortcutDescription
CTRL-WFind
ALT-WFind next
CTRL-\Search and replace

More info: nano-editor.org


Screen Shortcuts

CommandDescription
screenStart a screen session
screen -rResume a screen session
screen -listList current screen sessions

Screen Shortcuts (cont.)

ShortcutDescription
CTRL-AActivate screen commands
CTRL-A cCreate a new terminal instance
CTRL-A nSwitch to the next terminal instance
CTRL-A pSwitch to the previous terminal instance
CTRL-A "Display a list of terminal instances
CTRL-A ARename the current terminal instance

More info: GNU Screen Manual


tmux Shortcuts

CommandDescription
tmuxStart new session
tmux new -s nameNew named session
tmux lsList sessions
tmux attach -t nameAttach to session
tmux kill-session -t nameKill session

Inside tmux (prefix: CTRL-b)

ShortcutDescription
CTRL-b cNew window
CTRL-b nNext window
CTRL-b pPrevious window
CTRL-b %Split vertical
CTRL-b "Split horizontal
CTRL-b arrowSwitch pane
CTRL-b dDetach
CTRL-b [Scroll mode (q to exit)

File Permissions

CommandDescription
chmod 775 fileChange mode of file to 775
chmod -R 600 folderRecursively change permissions of folder to 600
chown user:group fileChange owner and group of file

File Permission Numbers

Calculate permission digits by summing the following values:

  • 4 – read (r)
  • 2 – write (w)
  • 1 – execute (x)

Example: For permissions rwxr-xr--, calculate:

  • Owner: 4+2+1 = 7
  • Group: 4+0+1 = 5
  • Others: 4+0+0 = 4 Resulting in 754.

Common Permission Patterns

PermissionMeaning
644Owner read/write, others read (files)
755Owner full, others read/execute (scripts, dirs)
600Owner read/write only (private files)
700Owner full only (private dirs)
777Everyone full access (avoid on production)

Archive and Compression

CommandDescription
tar -cvf archive.tar dir/Create tar archive
tar -xvf archive.tarExtract tar archive
tar -czvf archive.tar.gz dir/Create gzipped tar
tar -xzvf archive.tar.gzExtract gzipped tar
tar -tvf archive.tarList contents
gzip fileCompress file
gunzip file.gzDecompress file
zip -r archive.zip dir/Create zip
unzip archive.zipExtract zip