# LS COMMAND: TASK-BASED CHEATSHEET ==================================== ``` _ _____ _____ _ | | / ____| / ____| | | | | | (___ | | ___ _ __ ___ _ __ ___ __ _ _ __ __| | | | \___ \ | | / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` | | |_______) || |___| (_) | | | | | | | | | | | (_| | | | | (_| | |______\____/ \_____\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_| ``` ## EVERYDAY TASKS ---------------- ### 🔍 FINDING WHAT YOU NEED Find hidden configuration files: $ ls -a ~/.config List only directories: $ ls -d */ $ ls -l | grep "^d" Find executable files: $ ls -l | grep "^-..x" Find recently modified files: $ ls -lt | head Find largest files: $ ls -lS | head ### 📊 ANALYZING DISK USAGE Quick directory size analysis: $ ls -lh $ ls -la --block-size=M Compare file count in directories: $ for i in */; do echo -n "$i: "; ls -A "$i" | wc -l; done ### 🔒 SECURITY CHECKS Check for world-writable files: $ ls -l | grep "^.\{7\}w" Examine file permissions in bulk: $ ls -l | awk '{print $1, $9}' Check ownership of files: $ ls -l | grep "username" Find SUID/SGID files: $ ls -l | grep "^.....s" ### 📝 DOCUMENTATION & REPORTING Create file inventory: $ ls -la > directory_inventory.txt Generate CSV listing: $ ls -l | awk 'NR>1 {print $9","$5","$6" "$7}' > files.csv File count by extension: $ ls -l | grep -E "\.[a-z]{3}$" | awk '{print $NF}' | sed 's/.*\.//' | sort | uniq -c ## SPECIALIZED SCENARIOS ---------------------- ### 🔄 BACKUP VERIFICATION Compare two directories: $ diff <(ls -la dir1) <(ls -la dir2) Verify timestamps after restoration: $ ls -lt --time=ctime /backup/dir ### 🐋 CONTAINER & VIRTUALIZATION List files with context (containers): $ ls -laZ /var/lib/docker Size check before image build: $ ls -lah --block-size=M ./docker-context ### ☁️ CLOUD OPERATIONS Check upload candidates by size: $ ls -laSh | head -20 Verify download completion: $ ls -la --time-style=full-iso ./downloads ### 🔧 TROUBLESHOOTING Find zero-length files: $ ls -l | grep " 0 " Check for broken symlinks: $ ls -l | grep -E "^l.*->" List inode information (filesystem issues): $ ls -li Examine extended attributes: $ ls -la@ # macOS $ ls --context # Linux with SELinux ## POWER USER TECHNIQUES ---------------------- ### 🧰 ADVANCED FORMATTING Custom time format: $ ls -l --time-style="+%Y-%m-%d %H:%M:%S" Custom columns: $ ls -l | awk '{print $9 "\t" $5 "\t" $3}' JSON output (with helper): $ ls -la | awk 'BEGIN {print "["} NR>1 {print " {\"name\":\"" $9 "\", \"size\":" $5 "},"} END {print "]"}' | sed 's/,]/]/' ### 🔍 COMPLEX FILTERING Files modified in last 24 hours: $ find . -type f -mtime -1 -exec ls -l {} \; Files not accessed in 90+ days: $ find . -type f -atime +90 -exec ls -l {} \; Only certain file types: $ ls -l | grep -E "\.(jpg|png|gif)$" ## 🥇 ONE-LINERS FOR SPECIFIC TASKS --------------------------------- Disk usage by file type: $ ls -la | grep -v ^d | awk '{print $9}' | grep -o "\.[^.]*$" | sort | uniq -c | sort -nr Find duplicate files (by size, rough check): $ ls -l | awk '{print $5,$9}' | sort -n | uniq -D -f1 Extract creation dates for timeline: $ ls -la --time=birth | awk '{print $6, $7, $8, $9}' Build project file manifest: $ find . -type f -name "*.py" | sort | xargs ls -l | awk '{print $5, $9}' > manifest.txt ## 🔀 COMBINING WITH OTHER TOOLS ----------------------------- With xargs: $ ls -1 *.log | xargs wc -l With watch (monitor directory): $ watch -n 1 'ls -la /var/log' With stat (detailed info): $ ls -1 | xargs stat --format="%n: %s bytes, %y" With for loops: $ for file in $(ls *.txt); do echo "Processing $file"; cat "$file" | wc -l; done ## 🔧 ENVIRONMENT CUSTOMIZATION ---------------------------- Customize ls colors: $ export LS_COLORS="di=1;34:fi=0;37:ln=1;36:pi=1;33:so=1;35:bd=1;33:cd=1;33:ex=1;32" Create smart aliases: $ alias lm='ls -la | more' $ alias lsize='ls -laSh | head -20' $ alias lnew='ls -ltr | tail' $ alias lold='ls -ltr | head' Make it portable (POSIX compliance): $ ls -1rt # Works on most Unix-like systems ## 🔴 COMMON GOTCHAS & SOLUTIONS ----------------------------- "ls: Argument list too long" → Use find instead: $ find . -type f -name "*.log" -exec ls -la {} \; "Permission denied" everywhere: → Check current directory permissions: $ ls -ld . Hidden files confusion: → Remember -a shows dot files, but -A excludes . and .. $ ls -A # Show hidden files, but not . and .. Multi-user conflicts: → Check file owner vs current user: $ ls -la | grep $(whoami)