Ls
- 5 min read
- 3 chapters
Related on Lably
Through Computing, Microsoft Windows and Numerical analysis
It is available in the EFI shell, as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities, or as part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.
The numerical computing environments MATLAB and GNU Octave include an ls function with similar functionality.
In other environments, such as DOS, OS/2, and Microsoft Windows, similar functionality is provided by the dir command.
History
An ls utility appeared in the first version of AT&T UNIX, the name inherited from a similar command in Multics also named 'ls', short for the word "list". ls is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and the Single Unix Specification.
Sign in to keep reading
2 more chapters are free with a Lably account.
Behavior
Unix and Unix-like operating systems maintain the idea of a working directory. When invoked without arguments, ls lists the files in the working directory. If a directory is specified as an argument, the files in that directory are listed; if a file is specified, that file is listed. Multiple directories and files may be specified.
In many Unix-like systems, names starting with a dot (.) are hidden. Examples are ., which refers to the working directory, and .., which refers to its parent directory. Hidden names are not shown by default. With -a, all names, including all hidden names, are shown. Using -A shows all names, including hidden names, except for . and ... File names specified explicitly (for example ls .secret) are always listed.
Without options, ls displays names only. The different implementations have different options, but common options include:
-lLong format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date-time and name. If the modified date is older than 6 months, the time is replaced with the year. Some implementations add additional flags to permissions. The file type can be one of 8 characters:-, regular file;d, directory;l, symbolic (soft) link;n, network files;s, socket;p, named pipe (FIFO);c, character special file;b, block special file.-hOutput sizes in human readable format (e.g., 1K (kilobytes), 234M (megabytes), 2G (gigabytes)). This option is not part of the POSIX standard, although implemented in several systems, e.g., GNU coreutils in 1997, FreeBSD 4.5 in 2002, and Solaris 9 in 2002.
Additional options controlling how items are displayed include:
-RRecursively list items in subdirectories.-tSort the list by modification time (default sort is alphabetically).-uSort the list by last access time.-cSort the list by last attribute (status) change time.-rReverse the order, for example most recent time last.--full-timeShow times down to the second and millisecond instead of just the minute.-1One entry per line.-mStream format; list items across the page, separated by commas.-gInclude group but not owner.-oInclude owner but not group (when combined with-gboth group and owner are suppressed).-dShow information about a directory or symbolic link, rather than the contents of a directory or the link's target.-FAppend a "/" to directory names and a "*" to executable files.
It may be possible to highlight different types of items with different colors. This is an area where implementations differ:
- GNU
lsuses the--coloroption; it checks the Unix file type, the file permissions and the file extension and uses its own database to control colors maintained using dircolors. - FreeBSD
lsuses the-Goption; it checks only the Unix file type and file permissions and uses the termcap database
When the option to use color to indicate item types is selected, the output might look like:
-rw-r--r-- 1 tsmitt nregion 26650 Dec 20 11:16 <span style="color:dodgerblue " id="mwpA">audio.ogg</span> brw-r--r-- 1 tsmitt nregion 64 Jan 27 05:52 <span style="color:chocolate " id="mwpQ">bd-block-device</span> crw-r--r-- 1 tsmitt nregion 255 Jan 26 13:57 <span style="color:chocolate " id="mwpg">cd-character-device</span> -rw-r--r-- 1 tsmitt nregion 290 Jan 26 14:08 <span style="color:magenta " id="mwpw">image.png</span> drwxrwxr-x 2 tsmitt nregion 48 Jan 26 11:28 <span style="color:blue " id="mwqA">di-directory</span> -rwxrwxr-x 1 tsmitt nregion 29 Jan 26 14:03 <span style="color:green " id="mwqQ">ex-executable</span> -rw-r--r-- 1 tsmitt nregion 0 Dec 20 09:39 <span style="color:dimgray " id="mwqg">fi-regular-file</span> lrwxrwxrwx 1 tsmitt nregion 3 Jan 26 11:44 <span style="color:dodgerblue " id="mwqw">ln-soft-link</span> -> <span style="color:blue" id="mwrA">dir</span> lrwxrwxrwx 1 tsmitt nregion 15 Dec 20 10:57 <span style="color:lightcoral " id="mwrQ">or-orphan-link</span> -> <span style="color:white;background:lightcoral" id="mwrg">mi-missing-link</span> drwxr-xrwx 2 tsmitt nregion 4096 Dec 20 10:58 <span style="color:blue;background:lime " id="mwrw">ow-other-writeable-dir</span> prw-r--r-- 1 tsmitt nregion 0 Jan 26 11:50 <span style="color:darkgoldenrod " id="mwsA">pi-pipe</span> -rwxr-sr-x 1 tsmitt nregion 0 Dec 20 11:05 <span style="color:black;background:yellow" id="mwsQ">sg-setgid</span> srw-rw-rw- 1 tsmitt nregion 0 Jan 26 12:00 <span style="color:magenta " id="mwsg">so-socket</span> drwxr-xr-t 2 tsmitt nregion 4096 Dec 20 10:58 <span style="color:white;background:blue " id="mwsw">st-sticky-dir</span> -rwsr-xr-x 1 tsmitt nregion 0 Dec 20 11:09 <span style="color:white;background:red " id="mwtA">su-setuid</span> -rw-r--r-- 1 tsmitt nregion 10240 Dec 20 11:12 <span style="color:red " id="mwtQ">compressed.gz</span> drwxrwxrwt 2 tsmitt nregion 4096 Dec 20 11:10 <span style="color:black;background:lime " id="mwtg">tw-sticky-other-writeable-dir</span>
Sample usage
The following example demonstrates the output of the command:
<span id="mwuw"></span><span class="gp" id="mwvA">$ </span>ls<span class="w" id="mwvQ"> </span>-l <span class="go" id="mwvg">drwxr--r-- 1 fjones editors 4096 Mar 2 12:52 drafts</span> <span class="go" id="mwvw">-rw-r--r-- 3 fjones editors 30405 Mar 2 12:52 edition-32</span> <span class="go" id="mwwA">-r-xr-xr-x 1 fjones bookkeepers 8460 Jan 16 2022 edit.sh</span>
Each line shows the d (directory) or - (file) indicator, Unix file permission notation, number of hard links (1 or 3), the file's owner, the file's group, the file size, the modification date/time, and the file name. In the working directory, the owner fjones has a directory named drafts, a regular file named edition-32, and an executable named edit.sh which is "old", i.e. modified more than 6 months ago as indicated by the display of the year.
<span id="mwzA"></span><span class="go" id="mwzQ">┌─────────── file (not a directory)</span> <span class="go" id="mwzg">|┌─────────── read-write (no execution) permissions for the owner</span> <span class="go" id="mwzw">|│ ┌───────── read-only permissions for the group</span> <span class="go" id="mw0A">|│ │ ┌─────── read-only permissions for others</span> <span class="go" id="mw0Q">|│ │ │ ┌── number of hard links</span> <span class="go" id="mw0g">|│ │ │ │ ┌── owner</span> <span class="go" id="mw0w">|│ │ │ │ │ ┌── user group</span> <span class="go" id="mw1A">|│ │ │ │ │ │ ┌── file size in bytes</span> <span class="go" id="mw1Q">|│ │ │ │ │ │ │ ┌── last modified on</span> <span class="go" id="mw1g">|│ │ │ │ │ │ │ │ ┌── filename</span> <span class="go" id="mw1w">-rw-r--r-- 3 fjones editors 30405 Mar 2 12:52 edition-32</span>

