[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes the ls
command and its variants dir
and vdir
, which list information about files.
10.1 ls
: List directory contentsList directory contents. 10.2 dir
: Briefly list directory contentsBriefly ls. 10.3 vdir
: Verbosely list directory contentsVerbosely ls. 10.4 dircolors
: Color setup forls
Color setup for ls, etc.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
ls
: List directory contents
The ls
program lists information about files (of any type,
including directories). Options and file arguments can be intermixed
arbitrarily, as usual.
For non-option command-line arguments that are directories, by default
ls
lists the contents of directories, not recursively, and
omitting files with names beginning with `.'. For other non-option
arguments, by default ls
lists just the file name. If no
non-option argument is specified, ls
operates on the current
directory, acting as if it had been invoked with a single argument of `.'.
By default, the output is sorted alphabetically, according to the locale settings in effect. (2) If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is listed one per line and control characters are output as-is.
Because ls
is such a fundamental program, it has accumulated many
options over the years. They are described in the subsections below;
within each section, options are listed alphabetically (ignoring case).
The division of options into the subsections is not absolute, since some
options affect more than one aspect of ls
's operation.
Also see 2. Common options.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options determine which files ls
lists information for.
By default, any files and the contents of any directories on the command
line are shown.
$ ls --ignore='.??*' --ignore='.[^.]' --ignore='#*' |
The first option ignores names of length 3 or more that start with `.', the second ignores all two-character names that start with `.' except `..', and the third ignores names that start with `#'.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options affect the information that ls
displays. By
default, only file names are shown.
//DIRED// beg1 end1 beg2 end2 ... |
The begN and endN are unsigned integers that record the byte position of the beginning and end of each file name in the output. This makes it easy for Emacs to find the names, even when they contain unusual characters such as space or newline, without fancy searching.
If directories are being listed recursively (-R
), output a similar
line with offsets for each subdirectory name:
//SUBDIRED// beg1 end1 ... |
Finally, output a line of the form:
//DIRED-OPTIONS// --quoting-style=word |
Here is an actual example:
$ mkdir -p a/sub/deeper a/sub2 $ touch a/f1 a/f2 $ touch a/sub/deeper/file $ ls -gloRF --dired a a: total 8 -rw-r--r-- 1 0 Nov 9 18:30 f1 -rw-r--r-- 1 0 Nov 9 18:30 f2 drwxr-xr-x 3 4096 Nov 9 18:30 sub/ drwxr-xr-x 2 4096 Nov 9 18:30 sub2/ a/sub: total 4 drwxr-xr-x 2 4096 Nov 9 18:30 deeper/ a/sub/deeper: total 0 -rw-r--r-- 1 0 Nov 9 18:30 file a/sub2: total 0 //DIRED// 55 57 98 100 141 144 186 190 252 258 327 331 //SUBDIRED// 2 3 195 200 263 275 335 341 //DIRED-OPTIONS// --quoting-style=literal |
Note that the pairs of offsets on the `//DIRED//' line above delimit these names: `f1', `f2', `sub', `sub2', `deeper', `file'. The offsets on the `//SUBDIRED//' line delimit the following directory names: `a', `a/sub', `a/sub/deeper', `a/sub2'.
Here is an example of how to extract the fifth entry name, `deeper', corresponding to the pair of offsets, 252 and 258:
$ ls -gloRF --dired a > out $ dd bs=1 skip=252 count=6 < out 2>/dev/null; echo deeper |
Note that although the listing above includes a trailing slash
for the `deeper' entry, the offsets select the name without
the trailing slash. However, if you invoke ls
with `--dired'
along with an option like `--escape' (aka `-b') and operate
on a file whose name contains special characters, notice that the backslash
is included:
$ touch 'a b' $ ls -blog --dired 'a b' -rw-r--r-- 1 0 Nov 9 18:41 a\ b //DIRED// 40 44 //DIRED-OPTIONS// --quoting-style=escape |
If you use a quoting style that adds quote marks
(e.g., `--quoting-style=c'), then the offsets include the quote marks.
So beware that the user may select the quoting style via the environment
variable QUOTING_STYLE
. Hence, applications using `--dired'
should either specify an explicit `--quoting-style=literal' option
(aka `-N' or `--literal') on the command line, or else be
prepared to parse the escaped names.
ls
, so we
provide this option for compatibility.)
Normally the size is printed as a byte count without punctuation, but this can be overridden (see section 2.2 Block size). For example, `-h' prints an abbreviated, human-readable count, and `--block-size="'1"' prints a byte count with the thousands separator of the current locale.
For each directory that is listed, preface the files with a line `total blocks', where blocks is the total disk allocation for all files in that directory. The block size currently defaults to 1024 bytes, but this can be overridden (see section 2.2 Block size). The blocks computed counts each hard link separately; this is arguably a deficiency.
The permissions listed are similar to symbolic mode specifications
(see section 26.2 Symbolic Modes). But ls
combines multiple bits into the
third character of each set of permissions as follows:
Following the permission bits is a single character that specifies whether an alternate access method applies to the file. When that character is a space, there is no alternate access method. When it is a printing character (e.g., `+'), then there is such a method.
Normally the disk allocation is printed in units of 1024 bytes, but this can be overridden (see section 2.2 Block size).
For files that are NFS-mounted from an HP-UX system to a BSD system,
this option reports sizes that are half the correct values. On HP-UX
systems, it reports sizes that are twice the correct values for files
that are NFS-mounted from BSD systems. This is due to a flaw in HP-UX;
it also affects the HP-UX ls
program.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options change the order in which ls
sorts the information
it outputs. By default, sorting is done by character code
(e.g., ASCII order).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The version sort takes into account the fact that file names frequently include indices or version numbers. Standard sorting functions usually do not produce the ordering that people expect because comparisons are made on a character-by-character basis. The version sort addresses this problem, and is especially useful when browsing directories that contain many files with indices/version numbers in their names:
> ls -1 > ls -1v foo.zml-1.gz foo.zml-1.gz foo.zml-100.gz foo.zml-2.gz foo.zml-12.gz foo.zml-6.gz foo.zml-13.gz foo.zml-12.gz foo.zml-2.gz foo.zml-13.gz foo.zml-25.gz foo.zml-25.gz foo.zml-6.gz foo.zml-100.gz |
Note also that numeric parts with leading zeroes are considered as fractional one:
> ls -1 > ls -1v abc-1.007.tgz abc-1.007.tgz abc-1.012b.tgz abc-1.01a.tgz abc-1.01a.tgz abc-1.012b.tgz |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options affect the appearance of the overall output.
ls
when standard
output is not a terminal.
ls
if standard output is a terminal. It is always the default
for the dir
and d
programs.
GNU ls
uses variable width columns to display as many files as
possible in the fewest lines.
more
or
less
usually produces unreadable results. However, using
more -f
does seem to work.
ls
uses tabs where possible in the output, for efficiency. If
cols is zero, do not use tabs at all.
COLUMNS
is used if it is set; otherwise the default
is 80.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
By default, file timestamps are listed in abbreviated form. Most locales use a timestamp like `2002-03-30 23:45'. However, the default POSIX locale uses a date like `Mar 30 2002' for non-recent timestamps, and a date-without-year and time like `Mar 30 23:45' for recent timestamps.
A timestamp is considered to be recent if it is less than six
months old, and is not dated in the future. If a timestamp dated
today is not listed in recent form, the timestamp is in the future,
which means you probably have clock skew problems which may break
programs like make
that rely on file timestamps.
The following option changes how file timestamps are printed.
date
(see section 21.1 date
: Print or set system date and time).
For example, `--time-style="+%Y-%m-%d %H:%M:S"' causes
ls
to list timestamps like `2002-03-30 23:45:56'. As
with date
, format's interpretation is affected by the
LC_TIME
locale category.
If format contains two format strings separated by a newline, the former is used for non-recent files and the latter for recent files; if you want output columns to line up, you may need to insert spaces in one of the two formats.
This is useful because the time output includes all the information that
is available from the operating system. For example, this can help
explain make
's behavior, since GNU make
uses the full timestamp to determine whether a file is out of date.
ls
output fit within traditional 80-column output lines.
The following two ls
invocations are equivalent:
newline=' ' ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M" ls -l --time-style="iso" |
The LC_TIME
locale category specifies the timestamp format. The
default POSIX locale uses timestamps like `Mar 30 2002' and `Mar 30 23:45'; in this locale, the following two
ls
invocations are equivalent:
newline=' ' ls -l --time-style="+%b %e %Y$newline%b %e %H:%M" ls -l --time-style="locale" |
Other locales behave differently. For example, in a German locale, `--time-style="locale"' might be equivalent to `--time-style="+%e. %b %Y $newline%e. %b %H:%M"' and might generate timestamps like `30. Mär 2002 ' and `30. Mär 23:45'.
LC_TIME
locale
category is POSIX, style timestamps otherwise. For
example, the default style, which is `posix-long-iso', lists
timestamps like `Mar 30 2002' and `Mar 30 23:45' when in
the POSIX locale, and like `2002-03-30 23:45' otherwise.
You can specify the default value of the `--time-style' option
with the environment variable TIME_STYLE
; if TIME_STYLE
is not set
the default style is `posix-long-iso'. GNU Emacs 21 and
later can parse ISO dates, but older Emacs versions do not, so if
you are using an older version of Emacs and specify a non-POSIX
locale, you may need to set `TIME_STYLE="locale"'.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These options change how file names themselves are printed.
ls
.
You can specify the default value of the `--quoting-style' option
with the environment variable QUOTING_STYLE
. If that environment
variable is not set, the default value is `literal', but this
default may change to `shell' in a future version of this package.
ls
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
dir
: Briefly list directory contents
dir
(also installed as d
) is equivalent to ls -C
-b
; that is, by default files are listed in columns, sorted vertically,
and special characters are represented by backslash escape sequences.
See section ls
.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
vdir
: Verbosely list directory contents
vdir
(also installed as v
) is equivalent to ls -l
-b
; that is, by default files are listed in long format and special
characters are represented by backslash escape sequences.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
dircolors
: Color setup for ls
dircolors
outputs a sequence of shell commands to set up the
terminal for color output from ls
(and dir
, etc.).
Typical usage:
eval `dircolors [option]... [file]` |
If file is specified, dircolors
reads it to determine which
colors to use for which file types and extensions. Otherwise, a
precompiled database is used. For details on the format of these files,
run `dircolors --print-database'.
The output is a shell command to set the LS_COLORS
environment
variable. You can specify the shell syntax to use on the command line,
or dircolors
will guess it from the value of the SHELL
environment variable.
The program accepts the following options. Also see 2. Common options.
SHELL
environment variable is set and does not end with `csh' or
`tcsh'.
SHELL
ends with
csh
or tcsh
.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |