The Unix ls command
The ls command displays a list of files. There are several forms of the ls command
- ls
-
Lists the files in the current directory.
- ls filename
-
If filename is an ordinary file, then ls simply prints out the filename again. This is rather useless:
linux1% ls joke2 joke2
On the other hand, if filename is a directory, then ls will display the contents of the directory. This is much more useful:
linux1% ls cs104 hw01 hw02 hw03
- ls filename1 filename2 filename3 ...
-
Several filenames can be specified in a single ls command. This is useful in conjunction with wildcards. For example, in the following, the first ls command displays all the files in the current directory and the second ls command command displays just the C programs in the current directory:
linux1% ls a.out hello2.c hello3.c hello7.c hello.c hello.o inch.c typescript linux1% ls *.c hello2.c hello3.c hello7.c hello.c inch.c
If each of filename1 filename2 filename3 ... is a directory, then the files in each directory are displayed, like so:
linux1% ls hw01 hw02 hw03 hw01: joke1 joke2 joke3 hw02: bar foo hw03: hello.c inch.c myhello.c
- ls -a
-
The -a option tells ls to display all files, even ones whose filenames begin with a dot. (Those files are otherwise hidden.) The -a option can be used with the different forms of the ls command listed above.
linux1% ls -a . .. hw01 hw02 hw03
- ls -l
-
The -l option tells ls to display extra information about each file. This is the "long" form of the ls command:
drwx------ 5 chang faculty 2048 Sep 8 13:58 cs104 -rw------- 1 chang faculty 60 Sep 8 13:28 joke2 drwx------ 2 chang faculty 2048 Sep 8 12:57 Mail drwx------ 33 chang faculty 2048 Sep 8 12:57 OtherStuff -rw-r--r-- 1 chang faculty 1980 Sep 8 15:47 typescript lrwxr-xr-x 1 chang games 10 Sep 8 13:38 www -> ../pub/www
In the example above, cs104, Mail and OtherStuff are directories, so their listings begins with the character 'd'. The www file is a symbolic link to ../pub/www, so its listing begins with an 'l'. (Symbolic links are known as 'shortcuts' in Microsoft Windows and as 'aliases' in MacOS.) The -l option can be used with the different forms of the ls command listed above.
- ls -F
-
The -F option adds a character to the end of the displayed filename when the file is not an ordinary file. Directories are displayed with a blackslash '/' and symbolic links are displayed with an '@'.
The -F option is especially useful to distinguish directories and ordinary files. If you want to have ls use -F by default, you can use a shell alias:linux1% ls -F cs104/ joke2 Mail/ OtherStuff/ typescript www@
To revert to the original behavior, use unalias.linux1% ls cs104 joke2 Mail OtherStuff typescript www linux1% alias ls "ls -F" linux1% ls cs104/ joke2 Mail/ OtherStuff/ typescript www@
linux1% ls cs104/ joke2 Mail/ OtherStuff/ typescript www@ linux1% unalias ls linux1% ls cs104 joke2 Mail OtherStuff typescript www