Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

Getting Information About Files and Directories

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:

(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.

Using the ls command to get file information

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 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:

To show file sizes in kilobytes:

Displaying file metadata

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:

Tips

Discerning different types of files

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:

To see basic Unix file-type information:

To guess file types from hundreds of possibilities:

Working with Classic Mac metadata

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:

Tip

To set the Mac metadata for a file:

Tip

Категории