Classic Shell Scripting - Arnold Robbins [145]
Chapter 10. Working with Files
In this chapter, we discuss some of the more common commands for working with files: how to list files, modify their timestamps, create temporary files, find files in a directory hierarchy, apply commands to a list of files, determine the amount of filesystem space used, and compare files.
Listing Files
The echo command provides one simple way to list files that match a pattern:
$ echo /bin/*sh
Show shells in /bin
/bin/ash /bin/bash /bin/bsh /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh
The shell replaces the wildcard pattern with a list of matching files, and echo displays them in a space-separated list on a single line. However, echo does not interpret its arguments further, and thus does not associate them with files in the filesystem.-
* * *
ls
Usage
ls [ options ] [ file(s) ]
Purpose
List the contents of file directories.
Major options
1
Digit one. Force single-column output. In interactive mode, ls normally uses multiple columns of minimal width to fit the current window.
-a
Show all files, including hidden files (those whose names begin with a dot).
-d
Print information about directories themselves, rather than about files that they contain.
-F
Mark certain file types with special suffix characters.
-g
Group only: omit the owner name (implies -l (lowercase L)).
-i
List inode numbers.
-L
Follow symbolic links, listing the files that they point to.
-l
Lowercase L. List in long form, with type, protection, owner, group, byte count, last modification time, and filename.
-r
Reverse the default sort order.
-R
List recursively, descending into each subdirectory.
-S
Sort by descending file byte counts. GNU version only.
-s
List file size in (system-dependent) blocks.
-t
Sort by the last-modification timestamp.
—full-time
Show the complete timestamp. GNU version only.
Behavior
ls normally shows only the names of files: additional options are always needed to get information about file attributes. Files are sorted by default in lexicographical order, but that can be changed with the -S or -t options. Sorting order may also depend on the locale.
Caveats
Most implementations of ls offer many more options than we have shown here; consult your local manual pages for details.
* * *
The ls command can do much more because it knows that its arguments should be files. In the absence of command-line options, ls just verifies that its arguments exist, and displays them, either one per line if its output is not a terminal, or more compactly in multiple columns if it is. We can readily see the difference with three experiments:
$ ls /bin/*sh | cat
Show shells in output pipe
/bin/ash
/bin/bash
/bin/bsh
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
$ ls /bin/*sh
Show shells in 80-character terminal window
/bin/ash /bin/bash /bin/bsh /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh
$ ls /bin/*sh
Show shells in 40-character terminal window
/bin/ash /bin/csh /bin/tcsh
/bin/bash /bin/ksh /bin/zsh
/bin/bsh /bin/sh
For terminal output, ls uses as many columns as will fit, ordering data by columns. This is merely for human convenience; if you really want single-column output to the terminal, you can force it with ls -1 (digit one). However, programs that process the piped output of ls can expect to find just the simple case of one filename per line.
On BSD, GNU/Linux, Mac OS X, and OSF/1 systems, ls replaces nonprintable characters in filenames with question marks in terminal output, but reports filenames to nonterminal output without changes. Consider a file with the peculiar name one\ntwo, where \n is a newline. Here is what GNU ls does with it:
$ ls one*two
List peculiar filename
one?two
$ ls one*two | od