Skip to content

Listing Files

The ls command’s -l option prints a specified directory’s contents in a long listing format. If no directory is specified then, by default, the contents of the current directory are listed.

Terminal window
ls -l /etc

Example Output:

Terminal window
total 1204
drwxr-xr-x 3 root root 4096 Apr 21 03:44 acpi
-rw-r--r-- 1 root root 3028 Apr 21 03:38 adduser.conf
drwxr-xr-x 2 root root 4096 Jun 11 20:42 alternatives
...

The output first displays total, which indicates the total size in blocks of all the files in the listed directory. It then displays eight columns of information for each file in the listed directory. Below are the details for each column in the output:

Column No.ExampleDescription
1.1dFile type (see table below)
1.2rwxr-xr-xPermission string
23Number of hard links
3rootOwner name
4rootOwner group
54096File size in bytes
6Apr 21 03:44Modification time
7acpiFile name

The file type can be one of any of the following characters.

CharacterFile Type
-Regular file
bBlock special file
cCharacter special file
CHigh performance (“contiguous data”) file
dDirectory
DDoor (special IPC file in Solaris 2.5+ only)
lSymbolic link
MOff-line (“migrated”) file (Cray DMF)
nNetwork special file (HP-UX)
pFIFO (named pipe)
PPort (special system file in Solaris 10+ only)
sSocket
?Some other file type

The following will list up to ten of the most recently modified files in the current directory, using a long listing format (-l) and sorted by time (-t).

Terminal window
ls -lt | head

A dotfile is a file whose names begin with a .. These are normally hidden by ls and not listed unless requested.

For example the following output of ls:

Terminal window
$ ls
bin pki

The -a or --all option will list all files, including dotfiles.

Terminal window
$ ls -a
. .ansible .bash_logout .bashrc .lesshst .puppetlabs .viminfo
.. .bash_history .bash_profile bin pki .ssh

The -A or --almost-all option will list all files, including dotfiles, but does not list implied . and ... Note that . is the current directory and .. is the parent directory.

Terminal window
$ ls -A
.ansible .bash_logout .bashrc .lesshst .puppetlabs .viminfo
.bash_history .bash_profile bin pki .ssh

The ls command lists the contents of a specified directory, excluding dotfiles. If no directory is specified then, by default, the contents of the current directory are listed.

Listed files are sorted alphabetically, by default, and aligned in columns if they don’t fit on one line.

Terminal window
$ ls
apt configs Documents Fonts Music Programming Templates workspace
bin Desktop eclipse git Pictures Public Videos

Use the Bash shell’s filename expansion and brace expansion capabilities to obtain the filenames:

Terminal window
# display the files and directories that are in the current directory
printf "%s\n" *
# display only the directories in the current directory
printf "%s\n" */
# display only (some) image files
printf "%s\n" *.{gif,jpg,png}

To capture a list of files into a variable for processing, it is typically good practice to use a bash array:

Terminal window
files=( * )
# iterate over them
for file in "${files[@]}"; do
echo "$file"
done

The tree command lists the contents of a specified directory in a tree-like format. If no directory is specified then, by default, the contents of the current directory are listed.

Example Output:

Terminal window
$ tree /tmp
/tmp
├── 5037
├── adb.log
└── evince-20965
   └── image.FPWTJY.png

Use the tree command’s -L option to limit the display depth and the -d option to only list directories.

Example Output:

Terminal window
$ tree -L 1 -d /tmp
/tmp
└── evince-20965

The ls command’s -S option sorts the files in descending order of file size.

Terminal window
$ ls -l -S ./Fruits
total 444
-rw-rw-rw- 1 root root 295303 Jul 28 19:19 apples.jpg
-rw-rw-rw- 1 root root 102283 Jul 28 19:19 kiwis.jpg
-rw-rw-rw- 1 root root 50197 Jul 28 19:19 bananas.jpg

When used with the -r option the sort order is reversed.

Terminal window
$ ls -l -S -r /Fruits
total 444
-rw-rw-rw- 1 root root 50197 Jul 28 19:19 bananas.jpg
-rw-rw-rw- 1 root root 102283 Jul 28 19:19 kiwis.jpg
-rw-rw-rw- 1 root root 295303 Jul 28 19:19 apples.jpg
  • ls [OPTION]… [FILE]…
OptionDescription
-a, --allList all entries including ones that start with a dot
-A, --almost-allList all entries excluding . and ..
-cSort files by change time
-d, --directoryList directory entries
-h, --human-readableShow sizes in human readable format (i.e. K, M)
-HSame as above only with powers of 1000 instead of 1024
-lShow contents in long-listing format
-oLong -listing format without group info
-r, --reverseShow contents in reverse order
-s, --sizePrint size of each file in blocks
-SSort by file size
--sort=WORDSort contents by a word. (i.e size, version, status)
-tSort by modification time
-uSort by last access time
-vSort by version
-1List one file per line