ls: List Files
ls (list) shows what is inside a directory. It is probably the command you will type most often, so it is worth knowing its most useful options.
Listing a directory
With no argument ls lists the current directory; give it one or more paths to list those instead:
mkdir -p project/src project/docs
touch project/README.md project/src/main.py project/src/utils.py project/docs/guide.md
ls project
echo "---"
ls project/src project/docsREADME.md
docs
src
---
project/docs:
guide.md
project/src:
main.py
utils.py
When several directories are listed, each gets a name: header. Names are sorted alphabetically. In this sandbox the sort follows the plain C order, where capital letters come before lower-case ones (README.md before docs); a desktop system with an English locale usually sorts case-insensitively instead.
Note: In a terminal,
lsarranges names in columns to fit the window. When its output goes into a pipe or a file (as in these examples), it prints one name per line, which is exactly what other tools want. You can force either layout with-C(columns) or-1(one per line).
Hidden files
Names starting with a dot are hidden: plain ls skips them. Configuration files such as .bashrc, .gitconfig and .env are hidden so they don't clutter your listings. Use -a to show everything, or -A to show everything except the special entries . (this directory) and .. (the parent):
mkdir app && cd app
touch index.html .env .gitignore
ls
echo "--- ls -A"
ls -A
echo "--- ls -a"
ls -aindex.html
--- ls -A
.env
.gitignore
index.html
--- ls -a
.
..
.env
.gitignore
index.html
Long format
ls -l shows one file per line with details: permissions, link count, owner, group, size, modification time and name. Its output looks like this (times and sizes vary):
$ ls -l /etc/hostname /etc/passwd
-rw-r--r-- 1 root root 4 Mar 3 09:12 /etc/hostname
-rw-r--r-- 1 root root 1342 Feb 18 14:55 /etc/passwd| Field | Example | Meaning |
|---|---|---|
| Type + permissions | -rw-r--r-- |
- file, d directory, l symlink; then read/write/execute for owner, group, others |
| Links | 1 |
Number of hard links |
| Owner, group | root root |
Who owns the file |
| Size | 1342 |
In bytes (-h makes it human-readable: 1.4K) |
| Time | Feb 18 14:55 |
Last modification |
Because the time column changes, the runnable examples in this course use stat to show the same details without it:
printf 'id,name\n1,Ada\n2,Linus\n' > users.csv
mkdir backups
ln -s users.csv latest.csv
stat -c '%A %U %G %5s %n' users.csv latest.csv
ls -F-rw-r--r-- learner learner 22 users.csv
lrwxrwxrwx learner learner 9 latest.csv
backups/
latest.csv@
users.csv
-F adds a marker after each name: / for directories, @ for symbolic links, * for executables. It's a quick way to see what is what without the long format.
Sorting
| Option | Sort by |
|---|---|
| (default) | Name |
-t |
Modification time, newest first |
-S |
Size, largest first |
-X |
Extension |
-r |
Reverse whatever order is chosen |
printf '%0500d' 0 > big.log
printf '%050d' 0 > small.log
printf '%5000d' 0 > huge.log
touch notes.txt
ls -S
echo "---"
ls -Sr
echo "---"
ls -Xhuge.log
big.log
small.log
notes.txt
---
notes.txt
small.log
big.log
huge.log
---
big.log
huge.log
small.log
notes.txt
Other handy options
| Option | Effect |
|---|---|
-h |
Human-readable sizes with -l (4.0K, 12M) |
-R |
Recurse into subdirectories |
-d dir |
List the directory itself, not its contents |
-i |
Show inode numbers |
--color=auto |
Colour by type (usually set up by an alias) |
mkdir -p shop/images shop/css
touch shop/index.html shop/images/logo.png shop/css/site.css
ls -R shopshop:
css
images
index.html
shop/css:
site.css
shop/images:
logo.png
For a nicer tree view many systems have the tree command: tree shop.
Common mistakes
- Parsing
lsoutput in scripts. File names may contain spaces or even newlines. In scripts, loop over a glob (for f in *.log) or usefindinstead offor f in $(ls). - Forgetting hidden files.
lssaying a directory is empty doesn't mean it is: check withls -A.
Test yourself
Which command lists hidden files but not the . and .. entries?
- ls -a
- ls -A
- ls -h
- ls -l
What does ls -Sr do?
- Recursive listing sorted by size
- Sort by size, smallest first
- Sort by size, largest first
- Show sizes in human-readable form
In ls -F output, what does a trailing / mean?
- An executable
- A symbolic link
- A directory
- A hidden file