Shell commands cheat sheet
Basic navigation and file operations
Directory navigation
pwd # Print working directory
ls # List files and directories
ls -la # List all files with details
cd /path/to/directory # Change directory
cd ~ # Go to home directory
cd .. # Go up one directory
cd - # Go to previous directory
File operations
touch filename.txt # Create empty file
mkdir directory_name # Create directory
mkdir -p path/to/dir # Create directory with parents
cp file.txt copy.txt # Copy file
cp -r dir1 dir2 # Copy directory recursively
mv oldname newname # Move/rename file
rm filename.txt # Delete file
rm -r directory # Delete directory recursively
rm -rf directory # Force delete directory
File viewing and editing
cat filename.txt # Display file contents
less filename.txt # View file with paging
head filename.txt # Show first 10 lines
tail filename.txt # Show last 10 lines
tail -f filename.txt # Follow file changes
nano filename.txt # Edit with nano
vim filename.txt # Edit with vim
File searching
find . -name "*.py" # Find Python files
find . -type f -name "pattern" # Find files by pattern
grep "pattern" file.txt # Search in file
grep -r "pattern" directory/ # Search recursively
Modern alternatives (if installed)
bat filename.txt # Better cat with syntax highlighting
rg "pattern" # Better grep (ripgrep)
fd "pattern" # Better find
eza # Better ls
File permissions and ownership
chmod 755 filename # Set file permissions
chmod +x script.sh # Make file executable
chown user:group file # Change file ownership
Process management
ps aux # List all processes
ps aux | grep python # Find Python processes
kill PID # Kill process by ID
killall python # Kill all Python processes
jobs # List active jobs
bg # Send job to background
fg # Bring job to foreground
nohup command & # Run command in background
Network and system info
whoami # Current username
id # User and group info
uname -a # System information
df -h # Disk usage
du -sh directory # Directory size
free -h # Memory usage
top # System processes
htop # Better top (if installed)
Archive and compression
tar -czf archive.tar.gz directory/ # Create compressed archive
tar -xzf archive.tar.gz # Extract compressed archive
zip -r archive.zip directory/ # Create zip archive
unzip archive.zip # Extract zip archive
Git shortcuts
git status # Check repository status
git add . # Stage all changes
git commit -m "msg" # Commit with message
git push # Push to remote
git pull # Pull from remote
git log --oneline # Show commit history
git diff # Show changes
git branch # List branches
git checkout branch # Switch branches
Text processing
wc -l filename.txt # Count lines
wc -w filename.txt # Count words
sort filename.txt # Sort lines
uniq filename.txt # Remove duplicate lines
cut -d',' -f1 file.csv # Extract first column from CSV
Advanced patterns
Pipes and redirection
command > file.txt # Redirect output to file
command >> file.txt # Append output to file
command1 | command2 # Pipe output to next command
command 2>&1 # Redirect stderr to stdout
Command chaining
command1 && command2 # Run command2 if command1 succeeds
command1 || command2 # Run command2 if command1 fails
command1; command2 # Run commands sequentially
Wildcards and patterns
*.txt # All .txt files
file?.txt # file1.txt, file2.txt, etc.
file[123].txt # file1.txt, file2.txt, file3.txt
Environment variables
echo $PATH # Show PATH variable
export VAR=value # Set environment variable
env # Show all environment variables
Time-saving tips
- Use
Tab
for auto-completion
- Use
Ctrl+R
to search command history
- Use
Ctrl+C
to cancel current command
- Use
Ctrl+Z
to suspend current command
- Use
!!
to repeat last command
- Use
!command
to repeat last command starting with "command"
- Use
history
to see command history