Python Listing Files with ls & Options
The `ls` command lists directory contents. By default, it hides files starting with a dot (.) and shows only names. Adding flags unlocks detailed file permissions, owner, byte sizes in human-readable units, timestamps, and sorting criteria.
"Running plain `ls` is like glancing at the covers on a bookshelf; `ls -lah` is like opening an inventory catalog listing author, page count, publication date, and shelf location for every single book."
Deep Dive: How It Works
Hidden Files: Any file or folder starting with a leading dot (.) is hidden from default view.
Key Flags: `-l` (long format with permissions/owner/size/date), `-a` (all files including hidden), `-h` (human-readable sizes like 4K, 2M), `-t` (sort by modification time), `-S` (sort by file size), `-r` (reverse sort).
Directory Inode listing: `ls -i` reveals the unique inode number index on the filesystem.
Syntax Blueprint
ls -lah [path] ls -lt # Sort by newest modified first ls -lS # Sort by largest file size first
Use ls with flags to inspect file attributes and custom sort orders.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Assuming a folder is empty because plain `ls` returns nothing.Why it happens: Configuration files (like .env, .git, .bashrc) start with a dot and are hidden by default.
How to fix: Always run `ls -a` or `ls -la` to reveal hidden dotfiles.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
List All Files in a Directory
Create a folder `/tmp/ls_test`, create a file `doc.txt` and a hidden file `.secret`.
Run `ls -a /tmp/ls_test` to list all files.
Remove `/tmp/ls_test` at the end.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.