Getting Information About Files and Directories

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:

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

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:

  • ls -l path

    Use the -l option to the ls command ( -l for long form ).

    ls -l /

    shows you a long-form listing of your root directory ( Figure 5.21 ).

    Figure 5.21. Using ls -l to get information about files. (Your output may differ .)
     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$ 

    Figure 5.22 shows what the different parts of the output mean.

    Figure 5.22. This diagram shows what the output of ls -l means.


To show file sizes in kilobytes:

  • Use the -s and -k options together:

    ls -sk path

    The -s option means "List sizes," and the -k option means "List sizes in kilobytes." If you omit the k option, the size is listed in blocks, which vary in size depending on the version of Unix and how the disk was formatted. On our disk in Mac OS X, each block is .5 Kbyte.

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:

  • mdls path

    This displays the metadata associated with the file or directory whose path you supplied. For example:

     mdls /Developer/Examples/Dashboard/  Documentation/DashboardTutorial.pdf 

    Figure 5.23 shows this.

    Figure 5.23. Displaying a file's metadata using the mdls command.
     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).


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:

  • Examine the output from ls -l .

    The first character of each line tells you what kind of file it is. Table 5.4 summarizes the meaning of the first character in the output.

    Table 5.4. File Types from 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.


  • If you want information about a directory, then add the -d option:

    ls -ld directorypath

    The ls command reports information about the directory file itself, not its contents.

To see basic Unix file-type information:

  • Use the -F option to ls :

    ls -F path

    The -F (for file type ) option distinguishes directories, executable files (Unix commands), and symbolic links by adding / after directories, @ after symbolic links, and * after executables. Figure 5.24 compares ls / with ls -F / .

    Figure 5.24. The -F option to ls adds characters to some filenames showing file type. (Your output may differ.)
     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:

  • file path

    The file command attempts to figure out what kind of file each argument is. It uses a set of tests defined in the /usr/share/file/magic file. The file command can recognize more than 100 file types, but it is not 100 percent accurate. Some of the file types that file will try to recognize are image formats (such as JPEG, GIF, and PNG), programming languages (C, Perl, and Java), and compressed file formats (StuffIt archives, ZIP files, and Unix compress format).

    As with most commands that deal with filenames, you can supply as many paths as you like.

    file /etc/*

    applies the file command to everything in the /etc directory.

    Most of the files in the /etc directory are text files, but the file command is able to look inside them and make an educated guess about what kind of text file each one is ( Figure 5.25 ).

    Figure 5.25. The file command attempts to figure out what kinds of files are in the directory. (Again, your output may differ.)
     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) 

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:

  • /Developer/Tools/GetFileInfo path

    For example,

     /Developer/Tools/GetFileInfo  /usr/share/doc/bash/bash.pdf 

    is shown in Figure 5.26 .

    Figure 5.26. The GetFileInfo command reveals Mac-specific metadata.
     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$ 

    The attributes line lists a series of file attributes. Each letter represents one attribute that is either On (uppercase) or off (lowercase). See Table 5.5 for the meaning of each attribute.

    Table 5.5. File Attributes from GetFileInfo

    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 [*]


    [*] Note: Uppercase means on, lowercase means off. Options and attributes with * are allowed with folders.

    Unlike many other Unix commands, GetFileInfo does not handle multiple filenames as arguments.

    GetFileInfo is installed in a directory where your shell will not normally find it, so when you use the command, you must use its full path:

    /Developer/Tools/GetFileInfo

    For instructions on how to configure your shell to look in more places for commands, see Chapter 7.

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 letter path

    For example,

    /Developer/Tools/SetFile -a l "My File"

    unlocks the file My File .

     /Developer/Tools/SetFile -a T ~/  Documents/Letter 

    turns the file Letter into stationery.

    See Table 5.5 and man SetFile for the meaning of each option/attribute.

Tip

  • Check that the attribute was set with the GetFileInfo command described above.




Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net