From LQWiki
Jump to: navigation, search


The ls command (short for list) will show a directory-listing. It is one of the most common ones used when interacting with a text interface to a Linux system. It is the UNIX equivalent to the dir command common to many operating systems such as MS-DOS.

Using ls

ls has a number of common options in its use.

The long option

ls, without any other flags, simply prints a list of files without any other information. It however may be important to get some other details about the file, such as permissions, types, and so on. The long option, -l, lists filenames, sizes, permissions, and other information.

All files

ls will normally omit hidden dotfiles unless specified. The -a option forces ls to show them.

Type information

Normally, the only way to get the type of a file is to use the above long option. However, it may be a little cluttering to get unnecessary data also. The -F option uses a normal listing, but instead places a special marker character after each filename to specify its type, otherwise, it is a normal file.

The final character added is * when the file is executable, / is a directory, @ is a symbolic link, | is a FIFO, = is a socket.

Default options

The default options for ls can be changed by either using a shell alias, or by defining an environment variable LS_OPTIONS e.g. adding to /etc/profile

  export LS_OPTIONS="--color -l"

enables color output (a Linux extension) and displaying in the long format by default.

How to

Customize colors

Color output for ls is controlled by a configuration file. The global configuration file is located at /etc/DIR_COLORS. The settings in this file can be overwridden by creating a file called .dir_colors in the user's home directory.

For example, the default ls colors aren't very friendly for terminals with dark background colors. To change the default dark blue coloring for directories, create a copy of /etc/DIR_COLORS at $HOME/.dir_colors and change the line that reads

DIR 01;34 	# directory

to read

DIR 01;36 	# directory

This will produce a cyan coloring for directories that is more readable on dark backgrounds.

sort by change date

To get the most-recently changed files at the end of the output, use e.g.

ls -ltr

get a full-qualified directory listing

If you want to get every filename fully qualified (e.g. /etc/services instead of services), do not use ls, but find.

get the sizes of all folders

If you want to get a listing of your files and directories including their size, you do not use ls but

du -csh *

get the date when a file was accessed

You do not use ls, but stat.

Only list directories

ls -d */

Provided by

Most (all?) Linux distributions incorporate this from the GNU Coreutils: man page

Related Commands

dir, vdir, dircolors, touch

External links