# LINUX LS COMMAND CHEATSHEET =============================== ## BASIC USAGE ------------- ls List files and directories in current directory ls /path/to/directory List contents of a specific directory ls file1 file2 List information about specific files ls ~ List contents of your home directory ls .. List contents of parent directory ## COMMON OPTIONS --------------- -a, --all Show all files including hidden files (starting with .) -l Use long listing format (permissions, owner, size, date) -h, --human-readable Show file sizes in human-readable format (KB, MB, GB) -t Sort by modification time, newest first -S Sort by file size, largest first -r, --reverse Reverse the order of sorting -d, --directory List directory entries instead of contents -F, --classify Append indicator to entries (/ for directories, * for executables) -i, --inode Print the index number of each file -R, --recursive List subdirectories recursively ## USEFUL COMBINATIONS -------------------- ls -la Long listing including hidden files ls -lh Long listing with human-readable sizes ls -ltr Long listing sorted by time (oldest first) ls -lSh Long listing sorted by size (largest first) with human-readable sizes ls -ld */ List only directories in current directory ls -1 List one file per line ls -lR Recursively list all subdirectories with details ls -la --time=atime Show access time instead of modification time ls -lZ Show security context (SELinux) ## FILTERING & SORTING -------------------- ls *.txt List all files with .txt extension ls [abc]* List files starting with 'a', 'b', or 'c' ls -l | grep "^d" List only directories (using grep) ls -l | grep "^-" List only regular files (using grep) ls -l | sort -k 5 -n Sort output by file size (numerical) ls -l | sort -k 9 Sort output by filename ## FORMATTING OUTPUT ------------------ ls --color=auto Colorize the output ls --color=never Disable colorized output ls -l --time-style=long-iso Show timestamps in ISO format ls -l --time-style=full Show full timestamps ls --group-directories-first List directories before files ## SCRIPT-FRIENDLY OPTIONS ------------------------ ls -1q One entry per line, non-printable chars as ? ls -b Print C-style escapes for special characters ls --quoting-style=shell Quote filenames for use in shell commands ## PERFORMANCE TIPS ---------------- ls -U Do not sort (faster for large directories) ls -f Do not sort, enable -a (fastest option) ls --hide='*.tmp' Hide certain files (like .tmp files) ## COMBINING WITH OTHER COMMANDS ----------------------------- ls -la | grep "Jan" Find files modified in January ls -lS | head -10 Show 10 largest files ls | wc -l Count number of entries find . -type f | wc -l Count files recursively (alternative) ## ADVANCED USAGE -------------- ls -lu Sort by last access time ls -lc Sort by last status change time ls --context Display security context (SELinux) ls -L Follow symbolic links ls -Z Display SELinux security context ## ENVIRONMENT VARIABLES --------------------- $LS_COLORS Controls colorized output $COLUMNS Controls display width ## COMMON ALIASES -------------- ll Usually aliased to 'ls -l' la Usually aliased to 'ls -la' l Often aliased to 'ls -CF' TIP: Create your own aliases in ~/.bashrc or ~/.zshrc for frequently used options.