Ls

From LQWiki
Jump to navigation Jump to search

ls

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 available, controlled in different ways depending on your distribution.

Ubuntu (and maybe Debian?)

This probably applies to any distribution using the GNU coreutils package to provide the ls and dircolors commands.

Colors are specified in the user's environment variable LS_COLORS, which is by default established by the .bashrc script in the user's home directory. The default colors are designed for use on a black background and may not work well on other backgrounds. You can change the defaults by creating a .dircolors file, or you can change the LS_COLORS environment variable directly (though it's tricky).

To change the default colors, it is advisable to use the dircolors command with the -p switch to create the .dircolors file:

dircolors -p >~/.dircolors

the resulting file contains explanations of the possible changes. You must make this file executable in order for it to be recognized by .bashrc

Fortunately, the modern vim editor, and possibly others, recognize the .dircolors file and will show you the colors you're specifying as you edit the file.

Other

As of April 2016, and according to older information here, there is at least one distribution where colors derive from a heirarchy of configuration files. The global configuration file is located at /etc/DIR_COLORS. The settings in this file can be overridden 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

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

External links