Besides a file's name and contents, you probably want to know many other pieces of information about it. All Unix systems provide a way to get the following information about files:
File sizes
File types
Time and date last modified
Who owns the file
The permissions on files
(See Chapter 7 for more on permissions and ownership.)
Darwin (and thus Mac OS X) provides a way to get a great deal more information about files. In version 10.4 of Mac OS X, Apple introduced a brand new method for keeping track of additional data about files ( metadata ) and also a radical new method for updating indexes of file information (the metadata store and content index ). The metadata store and content index are the basis for the Spotlight feature of Mac OS X, and these indexes are instantly updated every time a file is created or changed. We covered searching the metadata store and content index with the mdfind command in Chapter 4, and also mentioned examining the metadata for a file with the mdls command. In this chapter we will go into a bit of detail about the mdls command.
But first we will describe the way to get file information that works on all Unix systems.
The ls command is the standard Unix command for "listing" information about files and directories. In this book we have already shown how to use ls to list the contents of directories. The command is also used to obtain many kinds of information besides filenames.
Options for the ls CommandThe ls command has more than two dozen available options and can show many kinds of information about files. The Unix manual page on ls ( man ls ) is where you can see all of them. The most commonly used option is -l , for long listing . Other commonly used options are -t , to sort the output by the last-modified time, and -a , to show files whose names begin with a . ( dot files ). Another interesting option is the -e option, to display the Access Control List (ACL) associated with a file (see Chapter 8, "Working with Permissions and Ownership," for more on ACLs). |
To see file type, size in bytes, date modified, owners , and permissions:
localhost:~ vanilla$ ls -l / total 9642 drwxrwxr-x 30 root admin 1020 Mar 26 09:29 Applications -rw-r--r-- 1 root admin 3584 Mar 26 09:58 Desktop DB -rw-r--r-- 1 root admin 2 Jan 14 18:48 Desktop DF drwxrwxr-x 13 root admin 442 Jan 17 14:38 Developer drwxrwxr-t 43 root admin 1462 Mar 26 09:29 Library drwxr-xr-x 1 root wheel 512 Mar 26 10:10 Network drwxr-xr-x 4 root wheel 136 Mar 26 09:26 System drwxrwxr-t 8 root admin 272 Mar 26 09:26 Users drwxrwxrwt 4 root admin 136 Mar 26 10:10 Volumes drwxr-xr-x 4 root admin 136 Jan 14 18:20 automount drwxr-xr-x 40 root wheel 1360 Mar 26 09:20 bin drwxrwxr-t 2 root admin 68 Nov 14 02:31 cores dr-xr-xr-x 2 root wheel 512 Mar 26 10:09 dev lrwxr-xr-x 1 root admin 11 Mar 26 09:20 etc -> private/etc lrwxr-xr-x 1 root admin 9 Mar 26 10:09 mach -> /mach.sym -r-r--r-- 1 root admin 597876 Mar 26 10:09 mach.sym -rw--r--r-- 1 root wheel 4312948 Mar 19 19:56 mach_kernel drwxr-xr-x 6 root wheel 204 Mar 26 10:09 private drwxr-xr-x 63 root wheel 2142 Mar 26 09:26 sbin lrwxr-xr-x 1 root admin 11 Mar 26 09:22 tmp -> private/tmp drwxr-xr-x 11 root wheel 374 Jan 17 14:29 usr lrwxr-xr-x 1 root admin 11 Mar 26 09:23 var -> private/var localhost:~ vanilla$
To show file sizes in kilobytes:
The mdls command lists the metadata associated with a file but is only available in the Darwin/Mac OS X version of Unix. It was introduced in Mac OS X 10.4 (along with the mdfind command, covered in Chapter 4). See the "Searching for Files" section of Chapter 4 for more details about metadata; briefly , the metadata displayed by mdls is the same metadata that the Spotlight feature of Mac OS X searches. For more on Spotlight see "Working with Spotlight" (http://developer.apple.com/macosx/spotlight.html).
To list the metadata attributes of a file:
mdls /Developer/Examples/Dashboard/ Documentation/DashboardTutorial.pdf
localhost:~ vanilla$ mdls /Developer/Examples/Dashboard/Documentation/DashboardTutorial.pdf /Developer/Examples/Dashboard/Documentation/DashboardTutorial.pdf - kMDItemAttributeChangeDate = 2005-04-14 13:19:33 -0700 kMDItemAuthors = ("Apple Computer, Inc.") kMDItemContentCreationDate = 2004-06-16 23:38:06 -0700 kMDItemContentModificationDate = 2004-06-16 23:38:06 -0700 kMDItemContentType = "com.adobe.pdf" kMDItemContentTypeTree = ( "com.adobe.pdf", "public.data", "public.item", "public.composite-content", "public.content" ) kMDItemCreator = "XEP 3.7.8 Client" kMDItemDisplayName = "DashboardTutorial.pdf" kMDItemEncodingApplications = ("XEP PDF Generator \U2013 RenderX, Inc.") kMDItemFSContentChangeDate = 2004-06-16 23:38:06 -0700 kMDItemFSCreationDate = 2004-06-16 23:38:06 -0700 kMDItemFSCreatorCode = 0 kMDItemFSFinderFlags = 0 kMDItemFSInvisible = 0 kMDItemFSLabel = 0 kMDItemFSName = "DashboardTutorial.pdf" kMDItemFSNodeCount = 0 kMDItemFSOwnerGroupID = 80 kMDItemFSOwnerUserID = 0 kMDItemFSSize = 346134 kMDItemFSTypeCode = 0 kMDItemID = 661176 kMDItemKind = "PDF Document" kMDItemLastUsedDate = 2004-06-16 23:38:06 -0700 kMDItemNumberOfPages = 40 kMDItemPageHeight = 792 kMDItemPageWidth = 612 kMDItemSecurityMethod = "None" kMDItemTitle = "Dashboard Tutorial" kMDItemUsedDates = (2004-06-16 23:38:06 -0700) kMDItemVersion = "1.3" localhost:~ vanilla$
Tips
You can restrict the output of mdls to a single metadata attribute using the -name optionfor example:
mdls -name kMDItemTitle Documents/Unix-VQP-05.doc
We provide a partial list of metadata attributes in Table 4.3 in Chapter 4, and a longer list is available in "Introduction to Spotlight Metadata Attributes Reference" (http://developer.apple.com/documentation/Carbon/Reference/MetadataAttributesRef).
Not all files are the same. For example, some files are directories, while others are special files the operating system uses to interact with disks and other devices.
Even among regular files there are differences. For example, some files are images, others consist of programming source code in various languages, and some are compiled applications. Among files created with Mac Classic applications, there are attributes such as stationery (if a Mac file is marked as stationery, then it is a sort of templatea read-only document you use as a starting place for creating new documents) and creator .
There are several ways to find out a file's type, each showing different kinds of information.
To see file types using ls -l:
F IRST C HARACTER | M EANING |
---|---|
- | Regular file. |
d | Directory. |
l | Symbolic link. A special kind of file that contains the path of another file, similar to a Mac alias. |
b | Block special files represent physical devices that deal with blocks of data, such as disks (or drives ). |
c | Character special files represent devices that deal with streams of characters , such as modems. |
s | Socket links (also called named pipes ) are special files that connect to programs. Writing data to the file actually "pipes" the data to the program. |
To see basic Unix file-type information:
localhost:~ vanilla$ ls / Applications Network bin mach.sym usr Desktop DB System cores mach_kernel var Desktop DF Users dev private Developer Volumes etc sbin Library automount mach tmp g4-cube:~ vanilla$ ls -F / Applications/ Network/ bin/ mach.sym usr/ Desktop DB System/ cores/ mach_kernel var@ Desktop DF Users/ dev/ private/ Developer/ Volumes/ etc@ sbin/ Library/ automount/ mach@ tmp@ localhost:~ vanilla$
To guess file types from hundreds of possibilities:
localhost:~ vanilla$ file /etc/* /etc/6to4.conf: ASCII text /etc/6to4.conf.applesaved: ASCII text /etc/6to4.conf.applesaved2: ASCII text /etc/6to4.conf.applesaved3: ASCII text /etc/X11: directory /etc/afpovertcp.cfg: ASCII English text /etc/aliases: symbolic link to `postfix/aliases' /etc/aliases.db: Berkeley DB 1.85 (Hash, version 2, native byte-order) /etc/appletalk.cfg: ASCII text /etc/appletalk.nvram.en0: data /etc/authorization: XML document text /etc/authorization.cac: XML document text /etc/bashrc: ASCII text /etc/crontab: ASCII English text (Partial output)
In Mac OS X 10.4 a new system of storing file metadata was introduced (see "Displaying file metadata" earlier in this chapter, and " mdfind " in Chapter 4); because of this we will refer to the kind of metadata used in the Mac OS prior to Mac OS X as Classic Mac metadata .
The challenges of integrating preMac OS X files with a Unix system are numerous and difficult. Apple has done quite an amazing job of providing backward compatibility for files created with older applications, while allowing thousands of Unix tools to operate . Still, there are some issues a command-line user should be aware of. (An excellent paper on some of the technical problems Apple has had to deal with is available online at USENIX 2000; www.wsanchez.net/papers/USENIX_2000/.)
Traditional Mac applications, and some Mac OS X applications, use an older system of storing information about each file that standard Unix commands do not understand. For example, the Finder can mark a file as "locked," and Unix command-line programs will not be able to alter the file, but neither will they tell you why you can't change themyou'll just get an error when trying to delete or rename a locked file.
(You can use the Darwin/Mac OS X version of the ls command with the -lo options:
ls -lo filename
to show if a file is "locked": the notation uchg [ unchangeable ] is added to the output for locked files.)
The GetFileInfo command (provided as part of the Mac OS X Developer Tools collection) will show you this Mac-specific information.
To see the Mac metadata for a file:
/Developer/Tools/GetFileInfo /usr/share/doc/bash/bash.pdf
localhost:~ vanilla$ /Developer/Tools/GetFileInfo /usr/share/doc/bash/bash.pdf file: "/usr/share/doc/bash/bash.pdf" type: "" creator: "" attributes: avbstclinmedz created: 03/13/2005 14:31:45 modified: 03/13/2005 14:31:45 localhost:~ vanilla$
O PTION /A TTRIBUTE | M EANING |
---|---|
a or A | Alias file |
v or V | Invisible [*] |
b or B | Bundle |
s or S | System (name locked) |
t or T | Stationery |
c or C | Custom icon [*] |
l or L | Locked |
i or I | Inited [*] |
n or N | No INIT resources |
m or M | Shared (can run multiple times) |
e or E | Hidden extension [*] |
d or D | Desktop [*] |
Unlike many other Unix commands, GetFileInfo does not handle multiple filenames as arguments.[*] Note: Uppercase means on, lowercase means off. Options and attributes with * are allowed with folders.
Tip
If a file is locked, you can unlock it from the command line using the SetFile command, described below. See also the chflags command ( man chflags ).
To set the Mac metadata for a file:
/Developer/Tools/SetFile -a T ~/ Documents/Letter
Tip
Check that the attribute was set with the GetFileInfo command described above.