ls -l data
# total 112
# -rw-r--r--@  1 mjfrigaard  staff   8798 May 15 21:59 README.md
# -rw-r--r--@  1 mjfrigaard  staff   6122 Apr 10 14:04 music_vids.tsv
# -rw-r--r--@  1 mjfrigaard  staff  12057 Apr 22 14:11 pwrds.tsv
# drwxr-xr-x@ 10 mjfrigaard  staff    320 May 15 21:55 raw
# -rw-r--r--@  1 mjfrigaard  staff   4417 Apr 10 14:01 trees.tsv
# -rw-r--r--   1 mjfrigaard  staff   4814 Apr 10 14:07 vg_hof.tsv
# -rw-r--r--@  1 mjfrigaard  staff    462 Apr 15 14:07 wu_tang.psv
# -rw-r--r--@  1 mjfrigaard  staff    263 Apr 10 09:39 wu_tang.tsvOptions
Unix command options (sometimes called flags) are often preceded by a hyphen (-) or two (-- for long-form options) and modify the behavior of a command. Options provide flexibility, control, and, in some cases, enable a vast array of additional functionalities.
Option types
Arguments can be short or long:
Short options are typically a single dash followed by a single letter (e.g., -l) and they modify the command behavior in a specific, often concise way.
Long options usually use two dashes followed by a word or compound words (e.g., --long-listing) and provide a more descriptive way to specify options, making scripts and commands more readable.
Short options
Example
ls -l data lists files in data in a long format, showing detailed information like permissions, owner, size, and modification date:
Long options
Example 1
tree data --sort size lists files in order according to size:
tree data --sort size
# βββ ajperlis_epigrams.txt
# βββ pwrds.csv
# βββ pwrds.tsv
# βββ README.md
# βββ music_vids.tsv
# βββ vg_hof.tsv
# βββ trees.tsv
# βββ roxanne.txt
# βββ wu_tang.dat
# βββ who_tb_data.tsv
# βββ who_tb_data.txt
# βββ wu_tang.txt
# βββ wu_tang.csv
# βββ wu_tang.tsv
# βββ roxanne_rev.txt
# βββ roxanne_orig.txt
# 
# 1 directory, 16 filesExample 2
grep --help displays usage information for the grep command, helping users understand available options and syntax.
grep --help
#> usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
#>  [-e pattern] [-f file] [--binary-files=value] [--color=when]
#>  [--context[=num]] [--directories=action] [--label] [--line-buffered]
#>  [--null] [pattern] [file ...]Example 3
The --version option is commonly used to get version information for various commands.
grep --version  
# grep (BSD grep, GNU compatible) 2.6.0-FreeBSDNote that not all commands support the long-form option syntax.
Modifying behavior
Some options modify the commands behavior.
Example
cp-n source.txt dest.txt does not overwrite the destination file if it already exists.
cat myfile.txt
# This is my filecp -n myfile.txt myfile2.txtThe -n option modifies the default behavior of the cp command.
cat myfile2.txt
# This is my 2nd fileModifying output
Other options modify a commands output.
Example 1
ls -a lists all files, including hidden ones (those starting with a dot). This option alters the commandβs output by showing files that are not listed by default.
ls -a data
# .
# ..
# README.md
# music_vids.tsv
# pwrds.tsv
# raw
# trees.tsv
# vg_hof.tsv
# wu_tang.psv
# wu_tang.tsvExample 2
df -h shows disk space usage in human-readable form (e.g., KB, MB, GB), modifying the default output to be more easily understood.
df -h
# Filesystem                          Size    Used   Avail Capacity iused ifree %iused  Mounted on
# /dev/disk1s1s1                     466Gi   9.5Gi    40Gi    20%    404k  422M    0%   /
# devfs                              201Ki   201Ki     0Bi   100%     696     0  100%   /dev
# /dev/disk1s3                       466Gi   2.4Gi    40Gi     6%    5.1k  422M    0%   /System/Volumes/Preboot
# /dev/disk1s5                       466Gi   1.0Gi    40Gi     3%       1  422M    0%   /System/Volumes/VM
# /dev/disk1s6                       466Gi    19Mi    40Gi     1%      19  422M    0%   /System/Volumes/Update
# /dev/disk1s2                       466Gi   411Gi    40Gi    92%    6.3M  422M    1%   /System/Volumes/Data
# map auto_home                        0Bi     0Bi     0Bi   100%       0     0     -   /System/Volumes/Data/homeEnvironment-specific options
The -u option in sort removes duplicate lines. -us behavior (considering case sensitivity) might vary depending on the locale and environment settings.
Example
sort -u data/roxanne.txt sorts the lines in data/roxanne.txt, removing duplicate lines.1
sort -u data/raw/roxanne.txt
# I have to tell you just how I feel
# I know my mind is made up
# I loved you since I knew you
# I won't share you with another boy
# I wouldn't talk down to you
# It's a bad way
# Ro...
# Roxanne
# Roxanne (Put on the red light)
# Roxanne (You don't have to put on the red light)
# So put away your make up
# Those days are over
# Told you once I won't tell you again
# Walk the streets for money
# You don't care if it's wrong or if it's right
# You don't have to put on the red light
# You don't have to sell your body to the night
# You don't have to wear that dress tonightCombining Options
Multiple short options can be combined after a single dash, without spaces (e.g., -lrt) allowing users to use multiple options at once, and reducing the need to type multiple dashes.
Multiple short options
Options can be combined with arguments when they are followed by a space and then the argument (e.g., -o filename).
Example
ls -lrt combines three options: -l (long listing format), -r (reverse order), and -t (sort by modification time), providing a detailed, reverse-chronological list of files.
ls -lrt data
# total 112
# -rw-r--r--@  1 mjfrigaard  staff    263 Apr 10 09:39 wu_tang.tsv
# -rw-r--r--@  1 mjfrigaard  staff   4417 Apr 10 14:01 trees.tsv
# -rw-r--r--@  1 mjfrigaard  staff   6122 Apr 10 14:04 music_vids.tsv
# -rw-r--r--   1 mjfrigaard  staff   4814 Apr 10 14:07 vg_hof.tsv
# -rw-r--r--@  1 mjfrigaard  staff    462 Apr 15 14:07 wu_tang.psv
# -rw-r--r--@  1 mjfrigaard  staff  12057 Apr 22 14:11 pwrds.tsv
# drwxr-xr-x@ 10 mjfrigaard  staff    320 May 15 21:55 raw
# -rw-r--r--@  1 mjfrigaard  staff   8798 May 15 21:59 README.mdOptions with values
Some options require or accept an argument to specify a value related to the optionβs action.
Example
grep-i "FILE" myfile.txt uses -i to ignore case when searching for "FILE" in myfile.txt:
grep -i "FILE" myfile.txt
# This is my fileThe "FILE" here is an argument for the -i option.
Recap
Options greatly enhance the power and versatility of Unix commands, allowing users to tailor operations to their specific needs and preferences.
Not all Unix-like systems or shells may support the same options for a given command, and behavior can vary between implementations. Itβs important to refer to a commandβs manual page (using man command or command --help) for the most accurate and comprehensive list of options and their effects.
rm myfile.txt
rm myfile2.txtdata/roxanne.txtcontains the lyrics to the 1978 song Roxanne by The Police.β©οΈ