Ls
From Wikipedia, the free encyclopedia
- The correct title of this article is ls. The initial letter is shown capitalized due to technical restrictions.
- Ls is also the planetocentric longitude of Sun, see planetocentric longitude
- For other meanings, see LS.
ls is a command specified by POSIX and the Single UNIX Specification, and is thus implemented in Unix and Unix-like operating systems. ls, an abbreviation of "list segments", lists files.
Contents |
[edit] History
An ls utility appeared in the original version of AT&T UNIX. Its name derives from a similar command in Multics which meant "list segments". Today, two popular versions of ls are the Free Software Foundation's (part of the GNU coreutils package) and the one released by various BSD variants, such as FreeBSD, OpenBSD, NetBSD, and Apple Computer's Darwin. Both are free software and open source.
[edit] Behaviour
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.
ls when invoked without any arguments, lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed.
Files whose names start with "." are not listed, unless the -a flag is specified or the files are specified explicitly.
Without options, 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:
- -l long format, displaying Unix file type, permissions, number of hard links, owner, group, size, date, and filename
- -F appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix.
- -a lists all files in the given directory, including those whose names start with "." By default, these files are excluded from the list.
- -R recursively lists subdirectories. The command ls -R / would therefore list all files.
In some environments, providing the option --color (for GNU ls) or -G (FreeBSD ls) causes ls to highlight different types of files with different colors, instead of with characters as -F would. To determine what color to use for a file, GNU ls checks the Unix file type, the file permissions, and the file extension, while FreeBSD ls checks only the Unix file type and file permissions.
When the option to use color to indicate file types is selected, the output might look like:
brw-r--r-- 1 unixguy staff 64, 64 Jan 27 05:52 block
crw-r--r-- 1 unixguy staff 64, 255 Jan 26 13:57 character
-rw-r--r-- 1 unixguy staff 290 Jan 26 14:08 compressed.gz
-rw-r--r-- 1 unixguy staff 331836 Jan 26 14:06 data.ppm
drwxrwx--x 2 unixguy staff 48 Jan 26 11:28 dir
-rwxrwx--x 1 unixguy staff 29 Jan 26 14:03 executable
prw-r--r-- 1 unixguy staff 0 Jan 26 11:50 fifo
lrwxrwxrwx 1 unixguy staff 3 Jan 26 11:44 link -> dir
-rw-rw---- 1 unixguy staff 217 Jan 26 14:08 regularfile
ls has a large number of other options, as can be discovered in the man page. It is one of the most frequently used tools, and is virtually the de-facto interface to the file system via the command line.
[edit] Sample usage
The following example demonstrates the output of the ls command given two different arguments:
$ 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*
Here we can see that 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 a special permissions notation that describes what the user, group, and "world" (everyone else) can do to the file. The first character of the permissions line indicates type:
Symbol | Meaning |
---|---|
- | Plain file |
b | Block device |
c | Character device |
d | Directory |
l | Symbolic link |
p or = | Named pipe or FIFO |
s | Socket |
The rest of the line is split into blocks of three characters each, with the presence of a r, w, and x signifying the ability to read, write, or execute respectively. The first block represents the permissions for the user, the second for the group, and the third for world. For example, the user fred can read and write the edition-32 file, but not execute it; members of the group editors can only read this file and not write or execute it, as can everyone else.
[edit] Options to ls
These options are guaranteed by the Single Unix Specification. Other Unix or Unix-like operating systems may provide extra options
- -C: multi-column output sorted down columns
- -F: do not follow links in command line; append a '/' after each directory, a '*' after each executable, a '|' after each FIFO, and an '@' after each link.
- -H: display details about link pointers, not about the links specified on the command line
- -L: display details about link pointers, not about the links anywhere
- -R: act recursively
- -a: show hidden files ( i.e. files which begin with a dot) also
- -c: use last modified status information instead of last modified file when sorting
- -d: do not follow links; do not treat directories differently
- -f: force each argument as directory
- -g: do not follow links in command line; use long format, but omit owner
- -i: show file serial number
- -l: do not follow links in command line; use long format
- -m: list files separated by commas
- -n: show owner UID and GID information numerically
- -o: do not follow links in command line; use long format, but omit group
- -p: only append a '/' after directories
- -q: display nonprintable filename characters as '?'
- -r: reverse sort order
- -s: display block size
- -t: sort by time modified
- -u: use time of last access instead of time of last modification
- -x: multi-column output, sorted across columns
- -1: one entry per line
[edit] See also
- chown – Unix command to change the owner of a file
- chgrp – Unix command to change group of a file
- User ID
- Group ID
- dir – DOS command that lists files, similar to ls.
- List of Unix programs
[edit] External links
- ls at the LinuxQuestions.org wiki.
- ls.c source code of the Unix Seventh Edition source code. More recent than following:
- ls.c source code of the AT&T Version 5 UNIX source code.
- ls specified by the Single Unix Specification
- GNU ls source code (as part of coreutils)
- Computer Hope: Linux / UNIX, Information about the Linux / UNIX ls command.
- ls command at the linux-command.com wiki.
[edit] Documentation
Unix command line programs (more) | |||
File and file system management: | cat | cd | chmod | chown | chgrp | cp | du | df | file | fsck | ln | ls | lsof | mkdir | more | mount | mv | pwd | rm | rmdir | split | touch | tree | ||
Process management: | anacron | at | chroot | crontab | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | screen | time | timex | top | wait | ||
User Management/Environment: | env | finger | id | locale | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
Text processing: | awk | cut | diff | ex | head | iconv | join | less | more | nkf | paste | sed | sort | tail | tr | uniq | wc | xargs | ||
Shell programming: | echo | expr | printf | unset | Printing: | lp |
Communications: inetd | netstat | ping | rlogin | traceroute |
Searching: find | grep | strings |
Miscellaneous: banner | bc | cal | man | size | yes |