-->
Page 53
Specifying Other Directories
You can also use the ls command to view the contents of other directories by specifying the directory, or pathname, on the command line. For example, if you want to see all the files in the /usr/bin directory, use
# ls /usr/bin arch dd gzip netstat stty ash df hostname nisdomainname su awk dmesg kill ping sync basename dnsdomainname ksh ps tar bash doexec ln pwd tcsh bsh domainname login red touch cat echo ls rm true chgrp ed mail rmdir umount chmod egrep mkdir rpm uname chown false mknod sed ypdomainname cp fgrep more setserial zcat cpio gawk mount sh csh grep mt sleep date gunzip mv sort
The ls command also supports using wildcards, or regular expressions, which means you can use options similar to (and much more complex than) the examples you've seen with the find and locate commands. For example, if you only want to search for text files in the current directory, you can use
# ls *.txt
Finally, if you want to see all of the files on your system, you can use the ls -R option, which recursively descends directories to show you the contents. Although you can use this approach to search for files and build a catalog of the files on your system, you should
be warned that it might take several minutes to list your files. The listing may also include files you don't want listed, or files on other operating system filesystems, such as DOS or Windows, especially if you use
# ls -R /
A better approach might be to use the -d option with -R to list only a certain number of directory levels. For example, the following command will search three directory levels along the root or / directory:
# ls -Rd /*/*/*
However, there's a much better utility for getting a picture of the directory structure of your system, the tree command, which is discussed later in this hour.
Page 54
JUST A MINUTE |
Do you like how ls -aF shows your directories? Or would you prefer ls to use colors all the time? If you want the ls command to always show this sort of detail, see Hour 6. |
If you just can't get the hang of using ls to list your directories, you can use the dir or vdir commands. These commands have only about 45 command-line options, compared to ls's over 75, but they're just as capable. They work like ls, but with certain defaults.
The dir command works like the default ls command, listing the files in sorted columns, for example:
# dir News axhome nsmail search author.msg documents reading vultures.msg auto mail research
The vdir command works like the ls -l option, and presents a long format listing by default, for example:
# vdir total 15 drwxr-xr-x 2 bball bball 1024 Nov 12 08:20 News -rw-rw-r-- 1 bball bball 4766 Nov 12 07:41 author.msg drwxrwxr-x 2 bball bball 1024 Nov 5 10:04 auto drwxrwxr-x 3 bball bball 1024 Nov 12 13:54 axhome drwxrwxr-x 2 bball bball 1024 Nov 12 15:13 documents drwx------ 2 bball bball 1024 Nov 12 14:02 mail drwx------ 2 bball bball 1024 Sep 15 01:57 nsmail drwxrwxr-x 2 bball bball 1024 Oct 29 20:28 reading drwxrwxr-x 5 bball bball 1024 Nov 5 10:03 research -rwxrwxr-x 1 bball bball 200 Oct 24 13:24 search -rw-rw-r-- 1 bball bball 801 Nov 11 22:46 vultures.msg
Although you won't find separate manual pages for dir or vdir (they're mentioned in the ls man page), you can get help with each command by using the --help option.
You now know how to list the contents of your directories, but you may also be interested in the directory structure of your system, or the directory structure of a particular tree of your system (such as /usr/X11R6). For example, ls -R will recursively print out your directories, but how are these directories related to each other? If you would like a more direct, graphical view of your directories, a directory listing utility can help.
Steve Baker's tree utility will print a graphic view of any desired structure, and it has several handy features. First, tree's syntax, or command-line, options are similar to several of those for the ls command. Wildcards or expressions are supported. The tree command also
Page 55
supports color in its listings, like ls. Finally, tree has a -x option similar to the find command's -xdev option, so you don't have to get a directory picture of operating systems if you choose to start your listing with the root or / directory.
The tree command is easy to use. For example, if you would like to see the contents of the /var/lib directory, along with all files, try the following:
# tree /var/lib /var/lib |-- alien | |-- applix-english_4.3-2.diff.gz | |-- applix_4.2-2.diff.gz | `-- applix_4.3-2.diff.gz |-- games |-- locatedb |-- logrotate.status |-- rpm | |-- conflictsindex.rpm | |-- fileindex.rpm | |-- groupindex.rpm | |-- nameindex.rpm | |-- packages.rpm | |-- providesindex.rpm | `-- requiredby.rpm `-- texmf |-- fonts | `-- pk | `-- ljfour | `-- public | `-- cm | |-- cmbx10.600pk | |-- cmbx10.720pk | |-- cmbx10.840pk | |-- cmmi10.600pk | |-- cmmi10.720pk | |-- cmr10.420pk | |-- cmr10.480pk | |-- cmr10.600pk | |-- cmsy10.480pk | |-- cmsy10.600pk | |-- cmti10.600pk | `-- cmtt10.600pk `-- texfonts 10 directories, 24 files
The tree command also has a handy -d option to list only directories, and not files (unlike ls). This is one of the best ways to get an idea of what your Linux file system looks like from a directory standpoint. You can also use it to view the directory structure of installed software. For example, to see what is on your system after installing the Netscape Web browser, try
# tree -d /usr/local/netscape