TExcelTAD
Home › Linux & Bash › ls: List Files

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:

bash · try it yourself
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/docs
README.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, ls arranges 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):

bash · try it yourself
mkdir app && cd app
touch index.html .env .gitignore
ls
echo "--- ls -A"
ls -A
echo "--- ls -a"
ls -a
index.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):

text
$ 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:

bash · try it yourself
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
bash · try it yourself
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 -X
huge.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)
bash · try it yourself
mkdir -p shop/images shop/css
touch shop/index.html shop/images/logo.png shop/css/site.css
ls -R shop
shop:
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 ls output in scripts. File names may contain spaces or even newlines. In scripts, loop over a glob (for f in *.log) or use find instead of for f in $(ls).
  • Forgetting hidden files. ls saying a directory is empty doesn't mean it is: check with ls -A.

Test yourself

Which command lists hidden files but not the . and .. entries?

  1. ls -a
  2. ls -A
  3. ls -h
  4. ls -l

What does ls -Sr do?

  1. Recursive listing sorted by size
  2. Sort by size, smallest first
  3. Sort by size, largest first
  4. Show sizes in human-readable form

In ls -F output, what does a trailing / mean?

  1. An executable
  2. A symbolic link
  3. A directory
  4. A hidden file