Post

Linux Quick Reference - Essential Commands

Linux Quick Reference - Essential Commands

Linux Quick Reference - Essential Commands

Print this or keep handy while learning


1
2
3
4
5
pwd                         # Show current directory
cd ~                        # Go to home directory
cd /path/to/dir             # Go to directory
cd ..                       # Go up one level
cd -                        # Go to previous directory

Listing Files

1
2
3
4
ls                          # List files
ls -la                      # List all files with details (including hidden)
ls -lrt                     # List sorted by time (oldest first) - USE THIS!
ls -lrth                    # Same but human-readable sizes (1.2M not 1234567)

File Operations

1
2
3
4
5
6
7
8
9
touch file.txt              # Create empty file
mkdir folder                # Create directory
mkdir -p a/b/c              # Create nested directories

cp file1 file2              # Copy file
cp -r dir1 dir2             # Copy directory
mv old new                  # Rename/move file
rm file                     # Delete file
rm -r folder                # Delete directory

Viewing Files

1
2
3
4
5
cat file                    # Display entire file
less file                   # View file page by page (q to quit)
head file                   # First 10 lines
tail file                   # Last 10 lines
tail -f file                # Follow file updates (Ctrl+C to stop)

Editing Files

1
2
3
4
5
nano file                   # Edit file
# Ctrl+O then Enter         # Save
# Ctrl+X                    # Exit
# Ctrl+K                    # Cut line
# Ctrl+U                    # Paste

File Permissions

1
2
3
4
chmod +x file               # Make file executable
chmod 755 file              # rwxr-xr-x (scripts)
chmod 644 file              # rw-r--r-- (regular files)
chmod 600 file              # rw------- (private files)

Permission numbers:

  • 7 = rwx (read, write, execute)
  • 6 = rw- (read, write)
  • 5 = r-x (read, execute)
  • 4 = r– (read only)

Searching

1
2
3
4
5
6
7
8
find . -name "*.txt"        # Find files by name
find . -type f              # Find files only
find . -type d              # Find directories only

grep "text" file            # Search in file
grep -i "text" file         # Case insensitive search
grep -r "text" .            # Search all files recursively
grep -n "text" file         # Show line numbers

Pipes & Redirection

1
2
3
4
5
6
7
cmd1 | cmd2                 # Pipe output of cmd1 to cmd2
cmd > file                  # Redirect output to file (overwrite)
cmd >> file                 # Append output to file
cmd 2>/dev/null             # Hide error messages

ls -l | grep ".txt"         # Example: List only .txt files
history | grep "ssh"        # Example: Find ssh commands

Process Management

1
2
3
4
5
6
ps aux                      # List all processes
ps aux | grep python        # Find specific processes
top                         # Monitor processes (q to quit)
kill PID                    # Stop process
kill -9 PID                 # Force kill process
pkill name                  # Kill by process name

System Information

1
2
3
4
5
6
7
df -h                       # Disk space usage
du -sh folder               # Size of folder
du -sh * | sort -hr         # Largest items in current dir
free -h                     # Memory usage
uptime                      # System uptime and load
whoami                      # Current username
uname -a                    # System information

Package Management (Ubuntu)

1
2
3
4
5
6
sudo apt update             # Update package list
sudo apt upgrade            # Upgrade all packages
sudo apt install package    # Install package
sudo apt remove package     # Remove package
sudo apt autoremove         # Remove unused packages
apt search keyword          # Search for packages

Archives & Compression

1
2
3
4
5
6
tar -czf archive.tar.gz dir/    # Create compressed archive
tar -xzf archive.tar.gz         # Extract archive
tar -tzf archive.tar.gz         # List archive contents

zip -r archive.zip dir/         # Create zip
unzip archive.zip               # Extract zip

Remember: czf = Create Zip File, xzf = eXtract Zip File


SSH & Remote Access

1
2
3
4
5
6
7
8
9
ssh user@server             # Connect to server
ssh-keygen                  # Generate SSH key
ssh-copy-id user@server     # Copy key to server
ssh user@server "command"   # Run command on remote server
exit                        # Disconnect

scp file user@server:~/     # Copy file to server
scp user@server:file ./     # Copy file from server
scp -r dir user@server:~/   # Copy directory to server

Useful Tricks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
!!                          # Repeat last command
sudo !!                     # Repeat last command with sudo
cd -                        # Go to previous directory
history                     # Show command history
clear                       # Clear screen (or Ctrl+L)

$(command)                  # Command substitution
mkdir $(date -I)            # Create folder named 2026-05-15
echo "I am $(whoami)"       # Use command output

Ctrl+C                      # Cancel current command
Ctrl+D                      # Exit shell/disconnect
Ctrl+R                      # Search command history
Ctrl+L                      # Clear screen
Tab                         # Auto-complete

Common Patterns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Find large files
du -ah . | sort -hr | head -20

# Count files in directory
ls | wc -l

# Find recently modified files
find . -mtime -1

# Watch log file for errors
tail -f /var/log/syslog | grep -i error

# Backup with date
tar -czf backup_$(date -I).tar.gz folder/

# Check if server is reachable
ping -c 3 server.com

# See what's using disk space
du -sh ~/* | sort -hr

# Find and kill process
ps aux | grep myapp
kill $(ps aux | grep myapp | awk '{print $2}')

WSL Specific

1
2
3
cd /mnt/c/Users/YourName/   # Access Windows files
explorer.exe .              # Open current folder in Windows Explorer
\\wsl$\Ubuntu\home\user     # Access Linux files from Windows

Get Help

1
2
3
4
man command                 # Manual page for command
command --help              # Quick help for command
which command               # Find command location
type command                # Show command type

Emergency

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Forgot where you are?
pwd

# Lost in too many files?
ls -lrt | tail

# Can't find a file?
find . -name "filename"

# Can't remember a command?
history | grep "what you remember"

# Process won't die?
kill -9 PID

# Need root access?
sudo -i

Keep this handy! Use man command for detailed help on any command.


This post is licensed under CC BY 4.0 by the author.