ls - Lists all files and directories in the present working directory
Variations:
ls -R - Lists files in sub-directories as well (recursive)ls -a - Shows hidden files (files starting with .)ls -al - Lists files with detailed information like permissions, size, owner, etc.Example: ls -al
cd directoryname - Changes the directory (navigate to a folder)
cd .. - Moves one level up (go to parent directory)cd ~ - Goes to home directorycd / - Goes to root directorycd - - Goes back to previous directoryExamples:
cd Documents # Go to Documents folder
cd .. # Go to parent folder
cd ~ # Go to home folder
pwd - Displays the present working directory (shows your current location)
Example output: /home/username/Documents
cat > filename - Creates a new file (type content, then Ctrl+D to save)
cat filename - Displays the file content
touch filename - Creates or modifies a file (updates timestamp)
View file options:
less filename - Views file page by page (scroll with arrow keys)head filename - Views first 10 lines of filetail filename - Views last 10 lines of fileCopy: cp source destination - Copies files from source to destination
Move: mv source destination - Moves/renames files
Delete: rm filename - Deletes a file (⚠️ permanent!)
Find: find / -name filename - Finds a file starting from root directory
file filename - Determines the file type
lsof - Shows which files are opened by which process
Examples:
cp file1.txt file2.txt # Copy file
mv file1.txt Documents/ # Move to folder
rm oldfile.txt # Delete file
find / -name "*.pdf" # Find all PDF files
du -h --max-depth=1 - Shows size of each directory in human-readable format
df - Shows disk usage of all mounted filesystems
df -h - Disk usage in human-readable format (MB, GB)
fdisk - Disk partition manipulation command
mkdir directoryname - Creates a new directory (folder)
rmdir directoryname - Deletes an empty directory
rm -r directoryname - Removes directory and all contents (recursive, careful!)
cp -r source destination - Copies directories recursively (with all contents)
mv oldname newname - Renames or moves directories
Examples:
mkdir myproject # Create folder
rmdir empty_folder # Delete empty folder
cp -r folder1 folder2 # Copy entire folder
mv oldname newname # Rename folder
ps - Displays currently active processes for the user
top - Displays all running processes in real-time (like Task Manager)
kill pid - Kills a process with given process ID
pkill name - Kills a process by name
bg - Resumes suspended jobs in background
fg - Brings the most recent job to foreground
fg n - Brings job number n to foreground
Examples:
ps aux # List all processes
top # View processes live
kill 1234 # Kill process with ID 1234
pkill firefox # Kill Firefox browser
command & # Run command in background
chmod octal filename - Changes file permissions (0-7 for each user type)
chown ownername filename - Changes file owner
chgrp groupname filename - Changes group owner
Permission Numbers:
Example: chmod 755 script.sh
7 = 4+2+1 (owner: read, write, execute)
5 = 4+1 (group: read, execute)
5 = 4+1 (others: read, execute)
ping host - Ping a host and check if it's reachable
ifconfig - Displays IP addresses of all network interfaces
ip address - Modern way to display IP addresses (newer systems)
ssh user@host - Remote login into a host as a user
scp file user@host - Transfers files between hosts over SSH
wget url - Downloads files from the web
curl url - Sends a request to URL and returns response
netstat -pnltu - Displays network connections and statistics
traceroute domain - Shows the route packets take to reach domain
whois domain - Get WHOIS information for domain
dig domain - Get DNS information for domain
Examples:
ping google.com # Check connectivity
ifconfig # Show IP addresses
ssh [email protected] # Remote login
wget https://example.com/file # Download file
tar cf file.tar files - Create a tar archive named file.tar
tar xf file.tar - Extract files from file.tar
tar -cvf archive.tar dirname/ - Create tar archive verbosely
tar -xvf archive.tar - Extract tar archive verbosely
gzip file - Compresses file and renames to file.gz
gzip -d file.gz - Decompresses file.gz back to original
zip -r file.zip files - Create a ZIP archive
unzip file.zip - Extract ZIP archive contents
tar -jcvf archive.tar.bz2 dirname/ - Create compressed bz2 archive
tar -jxvf archive.tar.bz2 - Extract bz2 archive
Examples:
tar cf backup.tar folder/ # Create tar
tar xf backup.tar # Extract tar
gzip largefile.txt # Compress
zip -r project.zip src/ # Create ZIP
grep pattern files - Search for pattern in files
grep -r pattern dir - Search recursively for pattern in directory
command | grep pattern - Pipe output to grep for searching
echo 'text' - Prints text to terminal
sed 's/string1/string2/g' file - Replaces string1 with string2
sed -i 's/string1/string2/g' file - Replace in-place (modifies file)
diff file1 file2 - Compares two files and shows differences
wc filename - Count lines, words, characters in file
cut -d':' -f1 /etc/passwd - Cut specific field from file
awk - Versatile programming language for working on files
Examples:
grep "error" logfile.txt # Find errors
grep -r "TODO" src/ # Find in directory
sed 's/old/new/g' file.txt # Replace text
wc -l script.sh # Count lines
ps aux | grep firefox # Find firefox process
date - Shows current date and time
cal - Shows calendar for current month
uptime - Shows how long system has been running
uname -a - Shows kernel information and system details
whoami - Shows current logged-in username
w - Displays who is currently online
free - Shows memory usage
free -m - Shows memory in MB format
df - Shows disk usage for mounted filesystems
du -sh - Shows disk usage of current directory
Examples:
date # Current date/time
uname -a # System info
uptime # Running time
free -m # Memory usage
whoami # Current user
Apt (Ubuntu/Debian):
sudo apt-get update - Updates package listssudo apt-get upgrade - Upgrades all packagessudo apt-get install pkgname - Installs a packagesudo apt-get remove pkgname - Removes a packagePython Pip:
pip install packagename - Installs Python packagepip uninstall packagename - Uninstalls Python packagepip freeze > requirements.txt - Saves installed packages to filepip install -r requirements.txt - Installs from requirements fileExamples:
sudo apt-get install python3 # Install Python
pip install numpy # Install Python library
sudo apt-get update # Update package lists
#!/bin/bash - Shebang line specifying script interpreter
$0, $1, ..., $9 - Script arguments (command-line parameters)
Conditional: if [condition]; then ... fi
Loop: for i in {1..10}; do ... done
While Loop: while [condition]; do ... done
Function: function name() {...}
Operators:
command1 ; command2 - Run command1 then command2command1 && command2 - Run command2 if command1 succeedscommand1 || command2 - Run command2 if command1 failscommand & - Run command in backgroundSimple script example:
#!/bin/bash
echo "Hello, $1"
for i in {1..5}; do
echo "Number: $i"
done
git init - Initialize a local git repository
git clone url - Clone a remote repository
git add filename - Stage file for commit
git commit -m "message" - Commit changes with message
git status - Check status of working directory
git push - Push changes to remote repository
git pull - Pull latest changes from remote
git branch - List all branches
git branch branchname - Create new branch
git checkout branchname - Switch to a branch
git merge branchname - Merge branch into current branch
git log - View commit history
git stash - Stash changes temporarily
Basic workflow:
git init # Create repo
git add file.txt # Stage file
git commit -m "Initial commit" # Commit
git push origin main # Push to remote
top - Interactive real-time process viewer (like Task Manager)
htop - More user-friendly alternative to top
iostat - Reports CPU and I/O statistics
vmstat - Reports memory, paging, and CPU statistics
ps aux - List all running processes with details
free -h - Show available memory in human-readable format
df -h - Show disk usage in human-readable format
Examples:
top # Real-time process monitoring
htop # Better visual process monitor
free -h # Memory status
df -h # Disk space status
env - Display all environment variables
echo $VARIABLE - Display value of a specific variable
export VARIABLE=value - Set environment variable
echo $PATH - Shows directories where executables are searched
export PATH=$PATH:/new/path - Add directory to PATH
alias new_command='old_command options' - Create command alias
Examples:
echo $HOME # Show home directory
export MYVAR=hello # Set variable
echo $MYVAR # Use variable
alias ll='ls -al' # Create shortcut
crontab -l - List all your cron jobs
crontab -e - Edit your cron jobs
crontab -r - Remove all your cron jobs
crontab -v - Display last time you edited cron jobs
crontab file - Install cron job from file
@reboot command - Schedule job to run at startup
Cron time format:
* * * * * command
| | | | |
| | | | └─ Day of week (0-6)
| | | └─── Month (1-12)
| | └───── Day (1-31)
| └─────── Hour (0-23)
└───────── Minute (0-59)
Example: 0 2 * * * backup.sh
This runs backup.sh every day at 2:00 AM
find / -name filename - Find file/directory starting from root
find /path -type f -name "*.txt" - Find all text files in path
locate filename - Quick file search (uses database)
whereis programname - Locate binary, source, and manual for command
which commandname - Shows full path of (shell) command
Examples:
find / -name "*.log" # Find all log files
locate apache2 # Quick search for apache2
which python3 # Full path to python3
whereis gcc # Locate gcc binary and docs
iostat - CPU and I/O statistics
vmstat - Virtual memory statistics
htop - Interactive process viewer
dmesg - Boot and system messages
journalctl - Query system logs
strace <command> - Trace system calls
lsof <file> - Show processes using file
watch -n 1 free - Watch memory usage every second
netstat -pnltu - Show network connections
ss - Socket statistics (modern alternative)
Examples:
top # Interactive system monitor
htop -p 1234 # Monitor specific process
pidstat # Show per-process statistics
mpstat -P ALL 1 # CPU usage per core
free -h # Memory usage in human format