General Commands
Essential general-purpose UNIX commands
ls - List Directory Contents
Directory listing and file information commands
# List files and directories
ls
# List with details
ls -l
# List all files (including hidden)
ls -a
# List with human-readable sizes
ls -lh
# List with recursive directory tree
ls -R
# List with color output
ls --color=auto
# List with specific pattern
ls *.txt
# List with sort by size
ls -lS
# List with sort by time
ls -lt
# List with reverse sort
ls -lr
Individual commands:
ls
ls -l
ls -a
ls -lh
ls -R
ls --color=auto
ls *.txt
ls -lS
ls -lt
ls -lr
cd/pwd - Directory Navigation
Directory navigation and current location commands
# Change to home directory
cd
# Change to specific directory
cd /path/to/directory
# Change to parent directory
cd ..
# Change to root directory
cd /
# Change to previous directory
cd -
# Show current directory
pwd
# Change to directory and list contents
cd /path && ls -la
# Change to directory with tilde expansion
cd ~/Documents
Individual commands:
cd
cd /path/to/directory
cd ..
cd /
cd -
pwd
cd /path && ls -la
cd ~/Documents
cp/mv/rm - File Operations
File copying, moving, and deletion operations
# Copy file
cp source.txt destination.txt
# Copy directory recursively
cp -r source_dir destination_dir
# Copy with preserve attributes
cp -p source.txt destination.txt
# Copy with verbose output
cp -v source.txt destination.txt
# Move/rename file
mv old_name.txt new_name.txt
# Move file to directory
mv file.txt /path/to/directory/
# Remove file
rm file.txt
# Remove directory recursively
rm -rf directory_name
# Remove with confirmation
rm -i file.txt
# Remove with verbose output
rm -v file.txt
Individual commands:
cp source.txt destination.txt
cp -r source_dir destination_dir
cp -p source.txt destination.txt
cp -v source.txt destination.txt
mv old_name.txt new_name.txt
mv file.txt /path/to/directory/
rm file.txt
rm -rf directory_name
rm -i file.txt
rm -v file.txt
mkdir/rmdir - Directory Operations
Directory creation and removal operations
# Create directory
mkdir new_directory
# Create multiple directories
mkdir dir1 dir2 dir3
# Create directory with parents
mkdir -p path/to/directory
# Create directory with specific permissions
mkdir -m 755 new_directory
# Remove empty directory
rmdir empty_directory
# Remove multiple empty directories
rmdir dir1 dir2 dir3
# Remove directory tree
rm -rf directory_tree
Individual commands:
mkdir new_directory
mkdir dir1 dir2 dir3
mkdir -p path/to/directory
mkdir -m 755 new_directory
rmdir empty_directory
rmdir dir1 dir2 dir3
rm -rf directory_tree
chmod/chown - Permissions
File permissions and ownership management
# Change file permissions (numeric)
chmod 755 file.txt
# Change file permissions (symbolic)
chmod u+x file.txt
chmod g-w file.txt
chmod o+r file.txt
# Change permissions recursively
chmod -R 755 directory/
# Change file ownership
chown user:group file.txt
# Change ownership recursively
chown -R user:group directory/
# Change only user ownership
chown user file.txt
# Change only group ownership
chgrp group file.txt
Individual commands:
chmod 755 file.txt
chmod u+x file.txt
chmod g-w file.txt
chmod o+r file.txt
chmod -R 755 directory/
chown user:group file.txt
chown -R user:group directory/
chown user file.txt
chgrp group file.txt
find - File Search
File and directory search operations
# Find files by name
find /path -name "*.txt"
# Find files by type
find /path -type f
# Find directories
find /path -type d
# Find files by size
find /path -size +100M
# Find files by modification time
find /path -mtime -7
# Find files by user
find /path -user username
# Find files and execute command
find /path -name "*.txt" -exec ls -l {} \;
# Find files and delete
find /path -name "*.tmp" -delete
# Find files with case-insensitive search
find /path -iname "*.TXT"
Individual commands:
find /path -name "*.txt"
find /path -type f
find /path -type d
find /path -size +100M
find /path -mtime -7
find /path -user username
find /path -name "*.txt" -exec ls -l {} \;
find /path -name "*.tmp" -delete
find /path -iname "*.TXT"
grep - Text Search
Text search and pattern matching in files
# Search for text in files
grep "pattern" file.txt
# Search recursively
grep -r "pattern" /path/
# Search with case-insensitive
grep -i "pattern" file.txt
# Search with line numbers
grep -n "pattern" file.txt
# Search with context lines
grep -A 3 -B 3 "pattern" file.txt
# Search for whole words only
grep -w "pattern" file.txt
# Search with regular expressions
grep -E "pattern1|pattern2" file.txt
# Search and show only filenames
grep -l "pattern" *.txt
# Search and invert match
grep -v "pattern" file.txt
Individual commands:
grep "pattern" file.txt
grep -r "pattern" /path/
grep -i "pattern" file.txt
grep -n "pattern" file.txt
grep -A 3 -B 3 "pattern" file.txt
grep -w "pattern" file.txt
grep -E "pattern1|pattern2" file.txt
grep -l "pattern" *.txt
grep -v "pattern" file.txt
sed - Stream Editor
Stream editing and text transformation operations
# Replace text in file
sed 's/old/new/g' file.txt
# Replace text in place
sed -i 's/old/new/g' file.txt
# Delete lines containing pattern
sed '/pattern/d' file.txt
# Print specific lines
sed -n '10,20p' file.txt
# Add text to beginning of line
sed 's/^/prefix /' file.txt
# Add text to end of line
sed 's/$/ suffix/' file.txt
# Delete empty lines
sed '/^$/d' file.txt
# Multiple substitutions
sed -e 's/old1/new1/g' -e 's/old2/new2/g' file.txt
Individual commands:
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt
sed '/pattern/d' file.txt
sed -n '10,20p' file.txt
sed 's/^/prefix /' file.txt
sed 's/$/ suffix/' file.txt
sed '/^$/d' file.txt
sed -e 's/old1/new1/g' -e 's/old2/new2/g' file.txt
awk - Text Processing
Advanced text processing and data manipulation
# Print specific columns
awk '{print $1, $3}' file.txt
# Print lines matching pattern
awk '/pattern/ {print}' file.txt
# Print with custom field separator
awk -F',' '{print $1, $2}' file.csv
# Calculate sum of column
awk '{sum += $1} END {print sum}' file.txt
# Print with line numbers
awk '{print NR, $0}' file.txt
# Print lines with specific length
awk 'length($0) > 50' file.txt
# Print unique values
awk '{print $1}' file.txt | sort | uniq
# Print with conditions
awk '$3 > 100 {print $1, $3}' file.txt
Individual commands:
awk '{print $1, $3}' file.txt
awk '/pattern/ {print}' file.txt
awk -F',' '{print $1, $2}' file.csv
awk '{sum += $1} END {print sum}' file.txt
awk '{print NR, $0}' file.txt
awk 'length($0) > 50' file.txt
awk '{print $1}' file.txt | sort | uniq
awk '$3 > 100 {print $1, $3}' file.txt
sort/uniq - Data Sorting
Data sorting and duplicate removal operations
# Sort file alphabetically
sort file.txt
# Sort numerically
sort -n numbers.txt
# Sort in reverse order
sort -r file.txt
# Sort by specific column
sort -k2 file.txt
# Sort and remove duplicates
sort file.txt | uniq
# Count unique lines
sort file.txt | uniq -c
# Show only unique lines
sort file.txt | uniq -u
# Show only duplicate lines
sort file.txt | uniq -d
# Sort with case-insensitive
sort -f file.txt
Individual commands:
sort file.txt
sort -n numbers.txt
sort -r file.txt
sort -k2 file.txt
sort file.txt | uniq
sort file.txt | uniq -c
sort file.txt | uniq -u
sort file.txt | uniq -d
sort -f file.txt