The ability to view the contents of a directory is a fundamental part of using the GNU/Linux command line. For this task, the
ls command is a fitting tool.
Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.
By default, the
ls command displays the contents of the current directory.
amnesia@amnesia:/boot$ ls config-5.4.0-3-amd64 grub
Often, the current directory value is in your terminal's command prompt (in the example above, the current directory is
/boot), but you can always determine it with the
amnesia@amnesia:/boot$ pwd /boot
You can pass the
ls command a specific directory as an argument to view that directory's contents.
amnesia@amnesia:~$ ls '/var' backups cache lib local lock log mail opt run spool tmp
Above, the current directory is represented by
~ (which is shorthand for the current user's home directory), but the contents of
/var is shown.
ls can accept multiple directories as arguments, as well (e.g.,
ls /tmp /var/tmp).
ls command has many useful options that enable you to modify the type of content you view and how it is displayed. For example, you can include hidden files (i.e., files that start with a
. and are not normally visible) in the
ls output by adding the
amnesia@amnesia:/tmp$ ls -a . .. 6yDLDkf_SS .font-unix .ICE-unix MN_foZiXYc pulse-PKdhtXMmr18n systemd-private-84c40c8cb57c4581be82509de05aabaa-colord.service-P9SBwZ systemd-private-84c40c8cb57c4581be82509de05aabaa-haveged.service-zQX6xW systemd-private-84c40c8cb57c4581be82509de05aabaa-htpdate.service-3hzp3E systemd-private-84c40c8cb57c4581be82509de05aabaa-ModemManager.service-Ne4AET systemd-private-84c40c8cb57c4581be82509de05aabaa-onion-grater.service-12SXKs systemd-private-84c40c8cb57c4581be82509de05aabaa-spice-vdagentd.service-1gdiTw systemd-private-84c40c8cb57c4581be82509de05aabaa-tails-shutdown-on-media-removal.service-DOwUKX systemd-private-84c40c8cb57c4581be82509de05aabaa-tails-tor-has-bootstrapped-flag-file.service-JIxJa0 systemd-private-84c40c8cb57c4581be82509de05aabaa-tails-wait-until-tor-has-bootstrapped.service-9WAiF3 firstname.lastname@example.orgOduxR systemd-private-84c40c8cb57c4581be82509de05aabaa-upower.service-bOqWqp .Test-unix .X11-unix .XIM-unix
.. are shorthand characters that represent the current directory (
.) and the current directory's parent directory (
One of the most useful
-l, causes directory contents to be displayed using a long listing format. This enables you to view metadata about file system objects.
amnesia@amnesia:~$ ls -l '/usr/include' total 13 -rw-r--r-- 1 root root 4546 Aug 29 2016 clif.h drwxr-xr-x 2 root root 32 Feb 10 13:08 iproute2 drwxr-xr-x 2 root root 89 Feb 10 13:08 python3.7m drwxr-xr-x 2 root root 63 Feb 10 13:08 reglib -rw-r--r-- 1 root root 8186 Feb 2 07:41 sudo_plugin.h drwxr-xr-x 2 root root 3 Feb 10 13:08 X11
ls -l command output is dense, so it helps to break it down.
- This represents the total disk allocation for all files in each directory listed in the current directory. The unit is given in blocks (a GNU/Linux file system consists of a pool of data blocks into which data is stored), and for this example is
- The first character in the long listing for an object represents the object's type. These are the possible values:
bBlock device file
cCharacter device file
pFIFO (First In, First Out; a named pipe)
- The next nine characters are the object's permission trios, which represent the permissions for the object's User Owner, Group Owner, and Other. The possible values are:
rPermission to read
wPermission to write
xPermission to execute
sSpecial Bit Permission (SUID and SGID)
tSticky Bit Permission
-for a permissions bit means no permissions.
- The next value in the long listing output is the object's reference counter,
1. This value represents how many file system references there are to this object, i.e., how many hard links.
- After the reference counter are the object's User Owner and Group Owner, in this case, both root.
- After the User Owner and Group owner is the size of the object, in bytes.
Aug 29 2016
- The penultimate value in an object's long listing is its last modification date.
- The final value in an object's long listing is its name.
Here is a summary of some of
ls's most useful options:
- Display hidden files too.
- Display listing for directory itself, instead of its contents.
- Display in human-readable format.
- Similar to
-L, but only works for symbolic links given as arguments.
- View inode information.
- Use long listing format (used to view object metadata).
- Always resolve symbolic links (displays metadata about the target, instead of the symbolic link itself, while still showing the name of the symbolic link).
- Omit color in the output.
- Equivalent to
- Mark file type by adding a suffix (nothing for plain file,
@for symbolic link).
- Reverse sort order.
- List recursively.
- Sort by size.
- Sort by modification time.
- Sort by extension.