Besides a file's
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
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 Command
The
ls
command has more than two
|
To see file type,
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;
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 kMDItemTitleDocuments/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
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
|
|
c |
Character special files
represent devices that deal with streams of
|
|
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
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
(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.