This article needs additional citations for verification. (June 2013) (Learn how and when to remove this template message)
ls is a command to list files in Unix and Unix-like operating systems.
ls is specified by POSIX and the Single UNIX Specification. When invoked without any arguments, ls lists the files in the current working directory.
Today, the two popular versions of
ls are the one provided with the GNU coreutils package, and that released by various BSD variants. Both are free software and open source, and have only minor syntax differences.
Unix and Unix-like operating systems maintain the idea of a current working directory, that is, where one is currently positioned in the hierarchy of directories. When invoked without any arguments,
ls lists the files in the current working directory. If another directory is specified, then
ls will list the files there, and in fact the user may specify any list of files and directories to be listed.
Files whose names start with "." are not listed, unless the
-a flag is specified, the
-A flag is specified, or the files are specified explicitly.
ls displays files in a bare format. This bare format however makes it difficult to establish the type, permissions, and size of the files. The most common options to reveal this information or change the list of files are:
-llong format, displaying Unix file types, permissions, number of hard links, owner, group, size, last-modified date and filename
-fdo not sort. Useful for directories containing large numbers of files.
-Fappends a character revealing the nature of a file, for example,
*for an executable, or
/for a directory. Regular files have no suffix.
-alists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list.
-Rrecursively lists subdirectories. The command
ls -R /would therefore list all files.
-dshows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory.
-tsort the list of files by modification time.
-hprint sizes in human readable format. (e.g., 1K, 234M, 2G, etc.) 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.
It's normally possible to highlight different types of files with different colors, instead of with characters as
-F would, but this is an area where the two main
ls versions differ:
--coloroption; checks the Unix file type, the file permissions, and the file extension; and uses its own database to control colors.
lsuses the or
-Goption; checks only the Unix file type and file permissions. and uses the termcap database
When the option to use color to indicate file types is selected, the output might look like:
-rw-r--r-- 1 unixguy staff 26650 Dec 20 11:16 audio.ogg brw-r--r-- 1 unixguy staff 64, 64 Jan 27 05:52 bd-block-device crw-r--r-- 1 unixguy staff 64, 255 Jan 26 13:57 cd-character-device -rw-r--r-- 1 unixguy staff 290 Jan 26 14:08 image.png drwxrwxr-x 2 unixguy staff 48 Jan 26 11:28 di-directory -rwxrwxr-x 1 unixguy staff 29 Jan 26 14:03 ex-executable -rw-r--r-- 1 unixhuy staff 0 Dec 20 09:39 fi-regular-file lrwxrwxrwx 1 unixguy staff 3 Jan 26 11:44 ln-soft-link -> dir lrwxrwxrwx 1 unixguy staff 15 Dec 20 10:57 or-orphan-link -> mi-missing-link drwxr-xrwx 2 unixguy staff 4096 Dec 20 10:58 ow-other-writeable-dir prw-r--r-- 1 unixguy staff 0 Jan 26 11:50 pi-pipe -rwxr-sr-x 1 unixguy staff 0 Dec 20 11:05 sg-setgid srw-rw-rw- 1 unixguy staff 0 Jan 26 12:00 so-socket drwxr-xr-t 2 unixguy staff 4096 Dec 20 10:58 st-sticky-dir -rwsr-xr-x 1 unixguy staff 0 Dec 20 11:09 su-setuid -rw-r--r-- 1 unixguy staff 10240 Dec 20 11:12 compressed.gz drwxrwxrwt 2 unixguy staff 4096 Dec 20 11:10 tw-sticky-other-writeable-dir
The following example demonstrates the output of the
ls command given two different arguments (
pwd is a command that shows the present working directory, or in other words, the folder you are currently in):
$ pwd /home/fred $ ls -l drwxr--r-- 1 fred editors 4096 drafts -rw-r--r-- 1 fred editors 30405 edition-32 -r-xr-xr-x 1 fred fred 8460 edit $ ls -F drafts/ edition-32 edit*
In this example, the user
fred has a directory named
drafts, a regular file called
edition-32, and an executable named
edit in his home directory.
ls uses Unix file permission notation to indicate which users or groups are allowed to access each file or directory.
drwxr--r-- 1 fred editors 4096 Mar 1 2007 drafts
In this example, drafts is a directory (denoted by the file descriptor d), and the characters after this indicate the permissions:
- rwx: the owner (fred) has the right to read (r), write (w) and execute (x)
- r--: group members (users part of the editors group) have read-only permissions; write and execute are not permitted, as denoted by the hyphen characters (-)
- r--: others (users aside from the owner or members of editors) have read-only permissions; write and execute are not permitted
- Multics manual page for ls or list command
- "A Brief History of the 'ls' command", Eric Fischer, The Linux Documentation Project
- "Multics programmer's manual - Commands and active functions" (PDF). p. 397.
- "(decode_switches): -h and -H override output units". coreutils.git. 1997-12-31.
- "[base] Log of /stable/10/bin/ls/ls.c". 2001-12-28.
- What's New in the Solaris 9 Operating Environment, Sun Microsystems, 2002
- "NCURSES — Frequently Asked Questions (FAQ): My terminal doesn't recognize color". Retrieved June 23, 2013.
- "FreeBSD Man Pages — ls". Retrieved June 23, 2013.
- The Single UNIX Specification, Issue 7 from The Open Group – Commands & Utilities Reference,
- FreeBSD General Commands Manual –
- NetBSD General Commands Manual –
- OpenBSD General Commands Manual –
- Solaris 10 User Commands Reference Manual –
- Linux User Commands Manual –
- Plan 9 Programmer's Manual, Volume 1 –
lssource code (as part of coreutils)
lsat the LinuxQuestions.org wiki