The ls command
list directory contents 37 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.
Options in the examples
-l- use a long listing format
-a, --all- do not ignore entries starting with .
-h, --human-readable- with -l and -s, print sizes like 1K 234M 2G etc.
-t- sort by time, newest first; see --time
-S- sort by file size, largest first
-d, --directory- list directories themselves, not their contents
-R, --recursive- list subdirectories recursively
-r, --reverse- reverse order while sorting
Examples
ls --help
ls
ls -lLong format (permissions, size, date)
ls -aShow hidden files (starting with .)
ls -laBoth combined
ls -lhHuman-readable file sizes (KB, MB)
ls -ltSort by modification time
ls -lrReverse order
ls -RRecursive (show subdirectories)
ls /etc
ls /var/log
ls ~/documents
ls *.txtSee what files match
ls /etc | wc -lHow many files in /etc?
ls -la file.txt
ls -la | grep '.txt'List only .txt files
ls -la | sort -k5 -nSort by file size
ls -lhS
ls /existsReturns 0 (success)
ls /notexistReturns 2 (error)
ls -l /var/www/html
ls -l /etc/nginx/sites-enabled
ls -t ~/backups/docs-*.tar.gzNewest first
ls -t ~/backups/docs-*.tar.gz | tail -n +4Everything after the 3rd line: the old ones
ls -t ~/backups/docs-*.tar.gz | tail -n +4 | xargs -r rm…deleted (-r: not when there are none)
ls -laS /var/logLargest files in a directory, sorted
ls -lh backup-*.tar.gz
ls -l ~/scripts/backup.shCan the script actually be executed?
ls -lh ~/backupsDid the backup file appear?
ls -l /var/log/appWho can read these files?
ls -lh /backupsWhich backups exist, and how fresh are they?
ls -la ~/projects/websiteCheck what came back
ls -l /etc/resolv.conf*Is there a known-good copy?
ls -la ~/.sshWho may read and write your SSH files?
ls -l /etc/worker/Who owns the config, who may read it?
ls -l /var/www/galleryWho owns the files? Who may read them?
ls -ld /var/www/gallery/photosThe folder itself (-d)
Lessons that use ls
- Your First Commands
- ls — Listing Files
- touch & mkdir — Creating Files and Directories
- rm — Deleting Files
- wc — Counting Words and Lines
- Understanding Permissions
- chown — Changing Ownership
- Pipes & Redirection
- df & du — Disk Usage
- Environment Variables
- Serving Your Own Pages
- Configuration & Server Blocks
- Project: Nightly Backup
- Mission: Disk Full
- Mission: Ship It
- Mission: The Silent Cron Job
- Mission: Leaked Credentials
- Mission: Restore From Backup
- Mission: The Internet Is Down?
- Mission: Locked Out of the Server
- Mission: The Service That Won't Stay Up
- Mission: 403 Forbidden