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:
To list the contents of any 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$
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:
ls ls -a ls -A
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$
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.
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:
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:
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$
Tip
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:
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$