Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
We normally think of a directory as "containing" files, but in reality a directory is just a special kind of file. A directory contains a list of entries. Each entry is the name of a file or another directorythis is an important concept. In Unix, the only place where a file's name is stored is in the directory. Filenames are not stored inside the files themselves .
When you rename a file, you are changing the entry in a directory, but not changing the file. And when you remove a file, you are removing the file's name from a directory.
The operating system removes the file if there are no other directory entries for that file. This becomes very important when trying to understand hard links , which are described below in the section "About Links (the Unix Version of Aliases)." (Hard links are a way of having more than one directory entry that refers to the same actual file.)
To list the contents of the current directory:
- ls
You see all of the nonhidden entries in the current directory (see below for an explanation of hidden files and how to view them).
To list the contents of any directory:
- ls path
If path is a directory, then the contents of the directory are shown. The path is always optional with the ls command. If it is omitted, the default path is . (the current directory).
You can list the contents of several directories by specifying multiple paths on the command line. Figure 5.13 shows the output from the command line
ls /bin /sbin
(Each of the files listed is actually a command. Use the man command described in Chapter 4, "Useful Unix Utilities," to learn about each of them.)
Figure 5.13. Supplying multiple arguments to ls to see the contents of more than one directory.
localhost:~ vanilla$ ls /bin /sbin /bin: [ df launchctl pwd tcsh bash domainname link rcp test cat echo ln rm unlink chmod ed ls rmdir wait4path cp expr mkdir sh zsh csh hostname mv sleep zsh-4.2.3 date kill pax stty dd ksh ps sync /sbin: SystemStarter md5 newfs_msdos Autodiskmount mknod nfsd Clri mount nfsiod Disklabel mount_afp nologin Dmesg mount_autofs ping Dump mount_cd9660 ping6 Dumpfs mount_cddafs quotacheck dynamic_pager mount_devfs rdump fibreconfig mount_fdesc reboot fsck mount_ftp restore fsck_hfs mount_hfs route fsck_msdos mount_msdos routed halt mount_nfs rrestore ifconfig mount_ntfs rtsol ip6fw mount_smbfs service ipfw mount_synthfs shutdown kerberosautoconfig mount_udf slattach kextload mount_volfs tunefs kextunload mount_webdav umount launchd newfs launchd_debugd newfs_hfs localhost:~ vanilla$
Hidden files
Unix normally does not show you files whose names begin with a . (a period, or dot in Unix-speak). These dot files are typically configuration files that are used by programs such as your shell when they start up, and thus they are something like the preferences files that littered your System Folder in preOS X versions of the Mac OS. See the sidebar "More About Hidden Files" for details.
To see hidden files:
- Use the -a or -A option to ls .
Unix (and the Mac OS X Finder) hides files whose names begin with a . (dot).
Adding the -a option shows all dot files ( -a for all files ).
The -A option is the same as -a except that the two special directory names . and .. are not shown. In this case, it's -A for almost all .
Figure 5.14 compares the output of
ls ls -a ls -A
Figure 5.14. Using the -a and -A options to ls reveals dot files. Your output may be different.
localhost:~ vanilla$ ls Current Projects Library Pictures bin Desktop Movies Public system-status Documents Music Sites localhost:~ vanilla$ ls -a . .lpoptions Music .. .ssh Pictures .CFUserTextEncoding .viminfo Public .DS_Store Desktop Sites .Trash Documents Stuff .bash_history Library bin .bash_profile Movies localhost:~ vanilla$ ls -A .CFUserTextEncoding .ssh Music .DS_Store .viminfo Pictures .Trash Desktop Public .bash_history Documents Sites .bash_profile Library Stuff .lpoptions Movies bin localhost:~ vanilla$
Getting more information from ls
The -l option to ls gives the "long" form of its output, listing one line for each entry and giving information about the file's size , date of last change, and other information.
See the section "Getting Information About Files and Directories," later in this chapter, for more on the -l option.
Sorting the output of ls
The default for ls is to sort its output alphabetically . By using the -t option you can cause the output to be sorted according to the time the file was last changed.
To sort the list by time instead of name:
- Use the -t option to ls .
This is most useful when it's combined with the -l option (for example, ls -lt *.jpg ), because the -l option causes the modification date and time to be displayed.
Figure 5.15 compares the output of ls -l with ls -lt .
Figure 5.15. Adding the -t option causes ls to sort by the time of last modification.
localhost:~/Documents vanilla$ ls -l total 48 -rw-r--r-- 2 vanilla staff 55 Jan 2 17:37 foo -rw-r--r-- 1 vanilla staff 331 May 8 19:04 novel.txt -rw-r--r-- 1 vanilla staff 134 May 8 19:06 report.txt -rw-r--r-- 2 vanilla staff 55 Jan 2 17:37 test1.txt -rw-r--r-- 1 vanilla staff 87 Jan 2 17:38 test2.txt -rw-r--r-- 1 vanilla staff 62 Jan 2 17:38 test3.txt drwxr-xr-x 3 vanilla staff 264 May 5 13:39 vi-practice localhost:~/Documents vanilla$ ls -lt total 48 -rw-r--r-- 1 vanilla staff 134 May 8 19:06 report.txt -rw-r--r-- 1 vanilla staff 331 May 8 19:04 novel.txt drwxr-xr-x 3 vanilla staff 264 May 5 13:39 vi-practice -rw-r--r-- 1 vanilla staff 62 Jan 2 17:38 test3.txt -rw-r--r-- 1 vanilla staff 87 Jan 2 17:38 test2.txt -rw-r--r-- 2 vanilla staff 55 Jan 2 17:37 foo -rw-r--r-- 2 vanilla staff 55 Jan 2 17:37 test1.txt localhost:~/Documents vanilla$
To reverse the sort order:
- Use the -r option ( r for reverse ).
Figure 5.16 compares the output of ls with ls -r .
Figure 5.16. Using the -r option causes ls to reverse the sorted order of its output.
localhost:~/Public vanilla$ ls cgi-bin dancer images index.html upload.html localhost:~/Public vanilla$ ls -r upload.html index.html images dancer cgi-bin localhost:~/Public vanilla$
-
If you want to find out which files in a directory were most recently modified, use ls with the l , t , and r options:
ls -ltr
That puts the most recently modified files at the bottom of the list, so even if the list of files is very long, the last thing on your screen is the most recently modified file.
To list only files matching a pattern:
See the entry on command-line wildcards in Chapter 2 or pipe the output of ls tHRough grep (review Chapter 4 or man grep ).
Sometimes you will want to list everything in a directory, including what's in any subdirectories, and any subdirectories of those directories, and so on. This is known as a recursive listing. There is an easy way to do this:
To recursively list the contents of a directory:
- ls -R path
If path is a directory, then ls recursively lists the contents of path and its subdirectories (in this case, R is for recursive ). Figure 5.17 compares the output from
ls ~/Sites
and
ls -R ~/Sites
Notice how with the -R option, the listing shows the contents of the two subdirectories.
Figure 5.17. Using the -R option tells ls that you want a recursive directory listing.
localhost:~/Public vanilla$ ls ~/Sites images index.html test localhost:~/Public vanilla$ ls -R ~/Sites images index.html test /Users/vanilla/Sites/images: apache_pb.gif macosxlogo.gif web_share.gif localhost:~/Public vanilla$