Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [147]

By Root 884 0
the file permissions for each of user, group, and other: r for read, w for write, x for execute, and - if the permission is absent.

The second column contains the link counts: here, only /bin/zsh has a hard link to another file, but that other file is not shown in the output because its name does not match the argument pattern.

The third and fourth columns report the file owner and group, and the fifth column reports the file size in bytes.

The next three columns report the last-modification timestamp. In the historical form shown here, a month, day, and year are used for files older than six months, and otherwise, the year is replaced by a time of day:

$ ls -l /usr/local/bin/ksh

List a recent file

-rwxrwxr-x 1 jones devel 879740 Feb 23 07:33 /usr/local/bin/ksh

However, in modern implementations of ls, the timestamp is locale-dependent, and may take fewer columns. Here are tests with two different versions of ls on GNU/Linux:

$ LC_TIME=de_CH /usr/local/bin/ls -l /bin/tcsh

List timestamp in Swiss-German locale

-rwxr-xr-x 1 root root 365432 2002-08-08 02:34 /bin/tcsh

$ LC_TIME=fr_BE /bin/ls -l /bin/tcsh

List timestamp in Belgian-French locale

-rwxr-xr-x 1 root root 365432 aoÛ 8 2002 /bin/tcsh

Although the timestamps are supposedly internationalized, this system shows its English roots with its bad French report of the date le 8 aoÛt 2002.

The GNU version permits display of full time precision; this example from an SGI IRIX system shows microsecond granularity:

$ /usr/local/bin/ls -l --full-time /bin/tcsh

Show high-resolution timestamp

-r-xr-xr-x 1 root sys 425756 1999-11-04 13:08:46.282188000 -0700 /bin/tcsh

The ls sidebar shows more than a dozen options common to ls implementations, but most have many more: the GNU version has nearly 40 of them! This diversity reflects the demands that have been put on ls over its more than three decades of existence. You will use ls often, so it is worthwhile to reread its manual pages from time to time to refresh your memory. For portable shell scripting, limit yourself to the more common options, and set the environment variable LC_TIME to reduce locale variations.

Listing File Metadata

Whenever computers store data in a compact binary form, it is useful to be able to present that same data in a more verbose form that is easily readable both by humans and by simple computer programs. We use the octal dump utility, od, several times in this book to turn streams of unprintable bytes into text, and we will discuss a special filesystem in Section 13.7, that makes internal kernel data more accessible.

It is curious, however, that the metadata in filesystems, long available to the C programmer via the POSIX-standard fstat( ), lstat( ), and stat( ) library calls, remains largely inaccessible to programmers in the shell and scripting languages, except in the limited forms provided by the ls command.

In the late 1990s, SGI IRIX introduced a stat command, and around 2001, independent implementations of stat were written for BSD systems and the GNU coreutils package. Unfortunately, the output format of the three programs is quite different, as illustrated in Section B.6.5 in Appendix B. Each has numerous command-line options that can provide more control over what data is output, and in what format. The GNU version is the only one that builds on every flavor of Unix, so if you standardize on it, you can use its features in your local shell scripts.

Updating Modification Times with touch

We have used the touch command a few times to create empty files. For a previously nonexistent file, here are equivalent ways of doing the same thing:

cat /dev/null > some-file Copy empty file to some-file

printf "" > some-file Print empty string to some-file

cat /dev/null >> some-file Append empty file to some-file

printf "" >> some-file Append empty string to some-file

touch some-file Update timestamp of some-file

However, if the file exists already, the first two truncate the file to a zero size, whereas the last three effectively do nothing more than update

Return Main Page Previous Page Next Page

®Online Book Reader