Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


ls -- Listing Files and Directories

News Shells Recommended Links Options Examples Pipes AWK xargs
Environment find grep sort cut tee Exit Status Etc

creeping featurism
: /kree?ping fee?chr?izm/, n.
[common]
1. Describes a systematic tendency to load more chrome and features onto systems at the expense of whatever elegance they may have possessed when originally designed. See also feeping creaturism. “You know, the main problem with BSD Unix has always been creeping featurism.
2. More generally, the tendency for anything complicated to become even more complicated because people keep saying “Gee, it would be even better if it had this feature too”. (See feature.) The result is usually a patchwork because it grew one ad-hoc step at a time, rather than being planned. Planning is a lot of work, but it's easy to add just one extra little feature to help someone ... and then another ... and another.... When creeping featurism gets out of hand, it's like a cancer. The GNU hello program, intended to illustrate GNU command-line switch and coding conventions, is also a wonderful parody of creeping featurism; the distribution changelog is particularly funny. Usually this term is used to describe computer programs, but it could also be said of the federal government, the IRS 1040 form, and new cars. A similar phenomenon sometimes afflicts conscious redesigns; see second-system effect. See also creeping elegance.

The vast majority of UNIX commands manipulate files and directories. The basic command to list files and directories is ls. The way ls displays file and directory names depends on what version of UNIX you are using and the ls command-line options used.

-t option lists the files sorted according to each file's modification date. The most recently modified files are printed first.

ls -lt | awk  '{print $6,$7}' | uniq | top -3 # three dates of the last modification of files.

 

The default output of ls is device sensitive. If you use the ls command in a pipe (pipe is explained later in this chapter), the default is steam of filenames with each separate by a newline. On colsole the dafault is multicolumn output.

Each argument to the ls command is either the name of a file, the name of a directory, or one or more options. The options are used to control the ordering of the list of files and information that is printed for each file. For each file argument, the requested information is printed. For each directory argument the requested information is printed for all the files in the directory (unless the -d option is used).

Options used with the ls command can be listed separately or concatenated. This means the following commands are effectively identical:

ls -l -F
ls -lF
 

Options

Option Description
-a
Lists all entries. By default, ls does not show any files whose names begin with a period ("."), and the -a option forces these files to be displayed.
-d
Forces ls to only print the requested information for a directory and not its contents. Normally, all the directories named in the argument list are searched, and the requested information is printed for all the files in those directories.
-F
Marks directories with a trailing slash ("/"), executable files with a trailing asterisk ("*"), and symbolic links with a trailing at sign ("@").
-i
Prints each file's inode number in the first column of the listing. If you list files that are linked, notice that both files have the same inode number.
-l
The long-list option is used to print detailed information (such as owner and size in bytes) about each listed file. The default is to only print the filenames.
-n
Lists the user and group ID numbers, instead of names, associated with each file and directory.
-r
Reverses the order of sort.
-s
Gives the size of each file, including any indirect blocks used to map the file, in KB (Berkeley) or 512-byte blocks (System V).
-t
Lists the files sorted according to each file's modification date. The most recently modified files are printed first.
-u
Uses the last access time instead of modification time.

 

Note: you must specify any options before the filename. For example, ls -l testfile is valid, but ls testfile -l is not. 

If no files or directories are named in the arguments, then the contents of the current directory are listed. By default, the names are listed in alphabetical order. One thing that confuses many UNIX users, especially novices, is the difference between the ls command and the ls * command.

The ls command has no argument, so by default a list of files in the current directory is displayed. The ls * command uses the metacharacter (character that has a special meaning to the shell; see the "Special Characters" section later in the chapter) "*" to match the names of all files in the current directory, including directories. Therefore, this command provides the ls command with one argument for every file in the current directory. For ordinary files, the two commands produce the same output, but when subdirectories are present, the first command lists only the subdirectory name, whereas the second command lists the subdirectory's name and its contents (because the subdirectories are explicitly mentioned in the argument list). In the following example, passwd_old is a file, and the other three files are directories:

NOTE: In multicolumn output mode, the files are listed column-wise and not row-wise as you might expect. 

It is important to understand the information provided by the -l (lowercase L) option. This option is the only way to discover key information, such as file type, ownership, and security, associated with each file. The long-format output of the ls command contains, typically, seven fields:

CAUTION
Do not confuse the ls -l (lowercase L) command with the ls -1 (numeric one) command. On some systems, the -1 option is used to force a single-column output from ls.

NOTE: The output of the ls -l command on your system might be somewhat different from the example shown because the format varies slightly from system to system. For example, on Berkeley systems, the group information isn't shown unless the -g option is specified. n

The following is a sample output of an ls -l command:

% ls -l /etc/passwd
-rw-r--r--  1 root  adm    19797 Aug 20 05:58 /etc/passwd
%

It is interesting to note that the modification date field does not contain "year" information.

 

Filename Generation

When you give the shell abbreviated filenames that contain special characters, or metacharacters, the shell can generate filenames that match the names of existing files. These special characters are also referred to as wildcards because they act as the jokers in a deck of cards in certain card games. When one of these special characters appears in a command's argument list, the shell expands that argument into a list of filenames and passes the list to the program that the command line calls.

 


NOTE: Filenames that contain metacharacters are sometimes called ambiguous file references because they do not refer to any one specific file. The process that the shell performs on these filenames is called pathname expansion or globbing. 


The five special characters ("*", "?", "[", "]", and "-") used in filename generation and pathname expansion are discussed in the following sections.

 

The "*" Wildcard

The asterisk ("*") is the most universal wildcard and is often used. It simply means any and all characters. For example, the string "a*" means all files beginning with the letter "a". You can use as many asterisks in a single expression as you need to define a set of files. For example, the expression *xx*.dat defines any filename with the extension dat that has xx anywhere in the rest of the name. Matches include the filenames abxx.dat, 1xx33.dat, xxyyzz.dat, and the simple name xx.dat.

 

The "?" Wildcard

The question mark ("?") represents a single occurrence of any character. Thus the string "???" represents all files whose names consist of just three characters. You can generate a list of files with three-character extensions with the string *.???. For example, if you're searching a directory containing graphics images as well as other data, the following command lists all files with extensions such as tif, jpg, and gif as well as any other files with three-character extensions:

ls *.???

The "[]" Expression

Sometimes you might need to be more selective in generating the file or pathnames than either of the general-purpose wildcards allows. Suppose you that want to select the files job1, job2, and job4 but not job3 or any other jobx. It is not possible to accomplish this using the "?" wildcard because it represents one occurrence of any character. You can, however, use job[123]. Like the question mark, items inside the square brackets ("[]") represent exactly one character--a specific character.

Using the square brackets, you can select a discrete series of permissible values such as [124], which allows only the characters 1, 2, or 4. You can also describe a range of characters such as [A-Z], which represents any characters between uppercase A and uppercase Z, inclusive. You can also specify a set of ranges such as [1-3, 5-8, a-e, A-E].

 


CAUTION
When using ranges, remember that what is included in the range depends on the character set used. Most UNIX systems use the ASCII character set, but some systems use the IBM EBCDIC character set. The range you specify is between the characters specified by the character's sort sequence.
For instance, [9-B] in the ASCII character set specifies the following characters:

 

9 : ; < = > ? @ A B

However, in the EBCDIC character set, it is an invalid range, and the result is unpredictable.


It is interesting to note that the hyphen ("-") loses its role as a metacharacter when it is used outside the square brackets. Conversely, the asterisk and the question mark lose their power as metacharacters when they are used within the square brackets. For example, -[*?]abc matches exactly two filenames: -*abc and -?abc.

 


TIP: Although possible, it is best to avoid creating filenames that contain dashes, asterisks, question marks, or other metacharacters.


Special Characters

Special characters, or metacharacters, are characters that have a special meaning to the shell. Avoid using any of these characters in a filename because files with metacharacters in their names are somewhat tricky to access, and certain programs might not be able to access them at all. The standard special characters are as follows:

& ; | * ? ` " ´ [ ] ( ) $ < > { } ^ # / \ % ! ~

 


NOTE: The ~/ combination can be used to reference your home directory. Therefore, cd ~/ changes your working directory to that of your home directory.
Similarly, ~username can be used to reference that user's home directory. For example, cd ~peter changes your current directory to the home directory of user peter. Do keep in mind that not all UNIX utilities support this shortcut. 


Although not considered special characters, Return, Spacebar, and Tab also have special meaning to the shell. Return usually ends a command line and initiates execution of a command. The Space and Tab characters separate elements on the command line and are collectively known as white spaces or blanks.

In you need to use one of the metacharacters as a regular character, you can quote it. Another often used term with the same meaning as quote is escape: You can escape a metacharacter. The shell treats a quoted metacharacter as a regular character. There are three ways to quote, or escape, a character:

 


CAUTION
The only way to quote control characters, such as Ctrl-h and Ctrl-m, is to precede each with a Ctrl-v. Quotation marks and backslashes don't work for control characters.


The most commonly used quoting method is to use backslashes.

Old News ;-)

Useless Use of Cat Award

Useless Use of ls *

Very clever. Usually this is seen as part of a for loop:

	for f in `ls *`; do
		command "$f"   # newbies will often forget the quotes, too
	done
Of course, the ls is not very useful. It will just waste an extra process doing absolutely nothing. The * glob will be expanded by the shell before ls even gets to see the file names (never mind that ls lists all files by default anyway, so naming the files you want listed is redundant here).

Here's a related but slightly more benign error (because echo is often built into the shell):

	for f in `echo *`; do
		command "$f"
	done
But of course the backticks are still useless, the glob itself already does the expansion of the file names. (See Useless Use of echo above.) What was meant here was obviously
	for f in *; do
		command "$f"
	done
Additionally, oftentimes the command in the loop doesn't even need to be run in a for loop, so you might be able to simplify further and say
	command *
A different issue is how to cope with a glob which expands into file names with spaces in them, but the for loop or the backticks won't help with that (and will even make things harder). The plain glob generates these file names just fine; click here for an example. See also Useless Use of Backticks

Finally, as Aaron Crane points out, the result of ls * will usually be the wrong thing if you do it in a directory with subdirectories; ls will list the contents of those directories, not just their names.

 

 


Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: September 15, 2008