Other Tools and Information
The final piece to understanding shell scripting (and to understanding other people’s shell scripts) is comprehending the use (and abuse) of command-line tools. The scripts listed in this section are commonly used in shell scripts.
Each of these tools has its own syntax and its own quirks. It is impractical to explain them all in detail. However, this chapter briefly highlights some common tools and includes links to their manual pages for finding additional information about them.
General Tools
The tools in this section are general tools that don’t fit into any broad categories.
Text Processing Tools
The tools listed in this section are commonly used for text processing. Unless otherwise noted, these commands take input from standard input (if applicable) and print the result to standard output.
Many of these commands use regular expressions. The syntax of regular expressions is described in Regular Expressions Unfettered. For additional usage notes specific to individual applications, see the manual page for the command itself.
Tool | Description |
---|---|
Short for Aho, Weinberger, and Kernighan; a programming language in itself, used for text processing using regular expressions. This tool is described further in How AWK-ward. | |
Short for Global [search for] Regular Expressions and Print; prints lines matching an input pattern (optionally with a specified number of lines of leading and/or trailing context). The Common variants include | |
Prints the first few lines from a file (or standard input). The number of lines can be specified with the | |
A programming language whose scripts can be easily embedded in shell scripts using the | |
Short for stream editor; performs more complex text substitutions using regular expressions. | |
Sorts a series of lines. By default, | |
Prints the last few lines from of a file (or standard input). The number of lines can be specified with the | |
Copies standard input to standard output, saving a copy into a file (or multiple files). | |
Replaces one character with another. | |
Filters out adjacent lines that match. |
File Commands
These commands are used to manipulate files, including renaming, moving, and deleting files, changing permissions, creating directories, listing files, and so on.
Tool | Description |
---|---|
Changes the current working directory. The command | |
Changes flags on a file or directory. Most of these flags are relatively obscure. For changing permissions flags, use | |
Changes the group ID associated with a file or directory. | |
Changes modes (permission bits) or access control lists (ACLs) on a file or directory. | |
Changes the ownership of files or directories. This command can also change the group if desired. | |
Lists or searches for files in a directory and its subdirectories. | |
Creates symbolic links and hard links to files or directories. | |
Lists the files in the current directory. | |
Creates new directories. | |
Creates named pipes for communication. This tool is useful in situations where pipes cannot be established while executing the commands, such as connecting two tools in a circular fashion. | |
Moves or renames files and directories. | |
Removes files and directories | |
Prints detailed file status information, such as the type of file, last modification date, and so on. | |
These tools, installed as part of the Developer Tools installation, are useful for getting and manipulating things like extended attributes. Be aware that if you write a script that depends on these, it will require the Developer Tools to be installed. |
Disk Commands
The tools listed in this section perform operations on disks, file systems, partition tables, and disk images.
Tool | Description |
---|---|
Mounts and unmounts volumes and disks, checks disks for consistency, erases optical disks, wipes disks with a security wipe, partitions disks, manipulates RAID sets, and so on. This utility is the command-line counterpart to the Disk Utility application. | |
Checks a file system for consistency. | |
Creates and manipulates disk images, including attaching disk images for mounting. | |
(Also | Mounts and unmounts volumes. If you unmount automounted volumes behind the back of the disk arbitration system, you can cause strange behavior in the GUI. Use these commands with care, and if you are trying to unmount an automounted volume, use |
Archiving and Compression Commands
The tools in this section allow you to create archive files that contain copies of multiple files for ease of distribution, to extract the contents of archive files, and compress and decompress files to reduce disk space or network utilization.
The compression tools can also generally be used with pipes to compress data without storing it in a file. The archive tools can generally use standard input or output for reading or writing the archive itself, but not the contents thereof. The funzip
variant of the zip archiving tool can be used with two pipes, but can only extract the first file from an archive.
Tool | Description |
---|---|
Compresses and decompresses files using the Burrows-Wheeler block sorting text compression algorithm and Huffman coding. This compression tool takes somewhat longer than other tools such as Files created with this tool end with the | |
Compresses and decompresses files using the Lempel-Ziv-Welsh (LZW) compression algorithm. This compression format has largely fallen out of popularity. Files created by this tool end with the | |
Compresses, uncompresses, and prints the contents of files in the GNU Zip (LZ77-based) format. This compression scheme is popular with UNIX and Linux users. While based on the same underlying compression scheme, the GNU Zip and ZIP file formats are not the same. The ZIP file format can contain multiple files, while the Gzip file format can only contain a single file (though this single file may be a Files created by this tool end with the | |
Compresses and uncompresses files and directories using the ZIP file format (deflate, based on LZ77 and Huffman coding). This file format is commonly used for exchanging compressed files with Windows users. Files created by this tool end with the | |
Creates, appends to, and extracts multifile archives in the Files created by this tool end with the |
For More Information
There are a nearly unlimited number of tools that you might find useful when writing shell scripts. These are just a few of the more common ones. You can find out about the command-line tools that ship as part of OS X by looking in the man pages, either online (OS X Man Pages) or by using the man
command on the command line.
For help finding a command to perform a particular task, you can either search the online version of the man pages or use the apropos
command on the command line.
Happy scripting!
Copyright © 2003, 2014 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2014-03-10