(Mode Structure, Symbolic Modes, Numeric Modes): Be more careful
[platform/upstream/coreutils.git] / doc / coreutils.texi
1 \input texinfo
2 @c %**start of header
3 @setfilename coreutils.info
4 @settitle @sc{gnu} Coreutils
5
6 @c %**end of header
7
8 @include version.texi
9 @include constants.texi
10
11 @c Define new indices.
12 @defcodeindex op
13 @defcodeindex fl
14
15 @c Put everything in one index (arbitrarily chosen to be the concept index).
16 @syncodeindex fl cp
17 @syncodeindex fn cp
18 @syncodeindex ky cp
19 @syncodeindex op cp
20 @syncodeindex pg cp
21 @syncodeindex vr cp
22
23 @dircategory Basics
24 @direntry
25 * Coreutils: (coreutils).       Core GNU (file, text, shell) utilities.
26 * Common options: (coreutils)Common options.      Common options.
27 * File permissions: (coreutils)File permissions.  Access modes.
28 * Date input formats: (coreutils)Date input formats.
29 @end direntry
30
31 @c FIXME: the following need documentation
32 @c * [: (coreutils)[ invocation.                   File/string tests.
33 @c * pinky: (coreutils)pinky invocation.           FIXME.
34 @c * uptime: (coreutils)uptime invocation.         FIXME.
35
36 @dircategory Individual utilities
37 @direntry
38 * base64: (coreutils)base64 invocation.         Base64 encode/decode data.
39 * basename: (coreutils)basename invocation.     Strip directory and suffix.
40 * cat: (coreutils)cat invocation.               Concatenate and write files.
41 * chgrp: (coreutils)chgrp invocation.           Change file groups.
42 * chmod: (coreutils)chmod invocation.           Change file permissions.
43 * chown: (coreutils)chown invocation.           Change file owners/groups.
44 * chroot: (coreutils)chroot invocation.         Specify the root directory.
45 * cksum: (coreutils)cksum invocation.           Print POSIX CRC checksum.
46 * comm: (coreutils)comm invocation.             Compare sorted files by line.
47 * cp: (coreutils)cp invocation.                 Copy files.
48 * csplit: (coreutils)csplit invocation.         Split by context.
49 * cut: (coreutils)cut invocation.               Print selected parts of lines.
50 * date: (coreutils)date invocation.             Print/set system date and time.
51 * dd: (coreutils)dd invocation.                 Copy and convert a file.
52 * df: (coreutils)df invocation.                 Report file system disk usage.
53 * dir: (coreutils)dir invocation.               List directories briefly.
54 * dircolors: (coreutils)dircolors invocation.   Color setup for ls.
55 * dirname: (coreutils)dirname invocation.       Strip non-directory suffix.
56 * du: (coreutils)du invocation.                 Report on disk usage.
57 * echo: (coreutils)echo invocation.             Print a line of text.
58 * env: (coreutils)env invocation.               Modify the environment.
59 * expand: (coreutils)expand invocation.         Convert tabs to spaces.
60 * expr: (coreutils)expr invocation.             Evaluate expressions.
61 * factor: (coreutils)factor invocation.         Print prime factors
62 * false: (coreutils)false invocation.           Do nothing, unsuccessfully.
63 * fmt: (coreutils)fmt invocation.               Reformat paragraph text.
64 * fold: (coreutils)fold invocation.             Wrap long input lines.
65 * groups: (coreutils)groups invocation.         Print group names a user is in.
66 * head: (coreutils)head invocation.             Output the first part of files.
67 * hostid: (coreutils)hostid invocation.         Print numeric host identifier.
68 * hostname: (coreutils)hostname invocation.     Print or set system name.
69 * id: (coreutils)id invocation.                 Print user identity.
70 * install: (coreutils)install invocation.       Copy and change attributes.
71 * join: (coreutils)join invocation.             Join lines on a common field.
72 * kill: (coreutils)kill invocation.             Send a signal to processes.
73 * link: (coreutils)link invocation.             Make hard links between files.
74 * ln: (coreutils)ln invocation.                 Make links between files.
75 * logname: (coreutils)logname invocation.       Print current login name.
76 * ls: (coreutils)ls invocation.                 List directory contents.
77 * md5sum: (coreutils)md5sum invocation.         Print or check MD5 digests.
78 * mkdir: (coreutils)mkdir invocation.           Create directories.
79 * mkfifo: (coreutils)mkfifo invocation.         Create FIFOs (named pipes).
80 * mknod: (coreutils)mknod invocation.           Create special files.
81 * mv: (coreutils)mv invocation.                 Rename files.
82 * nice: (coreutils)nice invocation.             Modify niceness.
83 * nl: (coreutils)nl invocation.                 Number lines and write files.
84 * nohup: (coreutils)nohup invocation.           Immunize to hangups.
85 * od: (coreutils)od invocation.                 Dump files in octal, etc.
86 * paste: (coreutils)paste invocation.           Merge lines of files.
87 * pathchk: (coreutils)pathchk invocation.       Check file name portability.
88 * pr: (coreutils)pr invocation.                 Paginate or columnate files.
89 * printenv: (coreutils)printenv invocation.     Print environment variables.
90 * printf: (coreutils)printf invocation.         Format and print data.
91 * ptx: (coreutils)ptx invocation.               Produce permuted indexes.
92 * pwd: (coreutils)pwd invocation.               Print working directory.
93 * readlink: (coreutils)readlink invocation.     Print referent of a symlink.
94 * rm: (coreutils)rm invocation.                 Remove files.
95 * rmdir: (coreutils)rmdir invocation.           Remove empty directories.
96 * seq: (coreutils)seq invocation.               Print numeric sequences
97 * sha1sum: (coreutils)sha1sum invocation.       Print or check SHA-1 digests.
98 * sha2: (coreutils)sha2 utilities.              Print or check SHA-2 digests.
99 * shred: (coreutils)shred invocation.           Remove files more securely.
100 * sleep: (coreutils)sleep invocation.           Delay for a specified time.
101 * sort: (coreutils)sort invocation.             Sort text files.
102 * split: (coreutils)split invocation.           Split into fixed-size pieces.
103 * stat: (coreutils)stat invocation.             Report file(system) status.
104 * stty: (coreutils)stty invocation.             Print/change terminal settings.
105 * su: (coreutils)su invocation.                 Modify user and group ID.
106 * sum: (coreutils)sum invocation.               Print traditional checksum.
107 * sync: (coreutils)sync invocation.             Synchronize memory and disk.
108 * tac: (coreutils)tac invocation.               Reverse files.
109 * tail: (coreutils)tail invocation.             Output the last part of files.
110 * tee: (coreutils)tee invocation.               Redirect to multiple files.
111 * test: (coreutils)test invocation.             File/string tests.
112 * touch: (coreutils)touch invocation.           Change file timestamps.
113 * tr: (coreutils)tr invocation.                 Translate characters.
114 * true: (coreutils)true invocation.             Do nothing, successfully.
115 * tsort: (coreutils)tsort invocation.           Topological sort.
116 * tty: (coreutils)tty invocation.               Print terminal name.
117 * uname: (coreutils)uname invocation.           Print system information.
118 * unexpand: (coreutils)unexpand invocation.     Convert spaces to tabs.
119 * uniq: (coreutils)uniq invocation.             Uniquify files.
120 * unlink: (coreutils)unlink invocation.         Removal via unlink(2).
121 * users: (coreutils)users invocation.           Print current user names.
122 * vdir: (coreutils)vdir invocation.             List directories verbosely.
123 * wc: (coreutils)wc invocation.                 Line, word, and byte counts.
124 * who: (coreutils)who invocation.               Print who is logged in.
125 * whoami: (coreutils)whoami invocation.         Print effective user ID.
126 * yes: (coreutils)yes invocation.               Print a string indefinitely.
127 @end direntry
128
129 @copying
130 This manual documents version @value{VERSION} of the @sc{gnu} core
131 utilities, including the standard programs for text and file manipulation.
132
133 Copyright @copyright{} 1994, 1995, 1996, 2000, 2001, 2002, 2003, 2004,
134 2005, 2006 Free Software Foundation, Inc.
135
136 @quotation
137 Permission is granted to copy, distribute and/or modify this document
138 under the terms of the GNU Free Documentation License, Version 1.1 or
139 any later version published by the Free Software Foundation; with no
140 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
141 Texts.  A copy of the license is included in the section entitled ``GNU
142 Free Documentation License''.
143 @end quotation
144 @end copying
145
146 @titlepage
147 @title @sc{gnu} @code{Coreutils}
148 @subtitle Core GNU utilities
149 @subtitle for version @value{VERSION}, @value{UPDATED}
150 @author David MacKenzie et al.
151
152 @page
153 @vskip 0pt plus 1filll
154 @insertcopying
155 @end titlepage
156
157
158 @ifnottex
159 @node Top
160 @top GNU Coreutils
161
162 @insertcopying
163 @end ifnottex
164
165 @cindex core utilities
166 @cindex text utilities
167 @cindex shell utilities
168 @cindex file utilities
169
170 @menu
171 * Introduction::                       Caveats, overview, and authors.
172 * Common options::                     Common options.
173 * Output of entire files::             cat tac nl od
174 * Formatting file contents::           fmt pr fold
175 * Output of parts of files::           head tail split csplit
176 * Summarizing files::                  wc sum cksum md5sum sha1sum sha2
177 * Operating on sorted files::          sort uniq comm ptx tsort
178 * Operating on fields within a line::  cut paste join
179 * Operating on characters::            tr expand unexpand
180 * Directory listing::                  ls dir vdir dircolors
181 * Basic operations::                   cp dd install mv rm shred
182 * Special file types::                 ln mkdir rmdir mkfifo mknod
183 * Changing file attributes::           chgrp chmod chown touch
184 * Disk usage::                         df du stat sync
185 * Printing text::                      echo printf yes
186 * Conditions::                         false true test expr
187 * Redirection::                        tee
188 * File name manipulation::             dirname basename pathchk
189 * Working context::                    pwd stty printenv tty
190 * User information::                   id logname whoami groups users who
191 * System context::                     date uname hostname hostid
192 * Modified command invocation::        chroot env nice nohup su
193 * Process control::                    kill
194 * Delaying::                           sleep
195 * Numeric operations::                 factor seq
196 * File permissions::                   Access modes.
197 * Date input formats::                 Specifying date strings.
198 * Opening the software toolbox::       The software tools philosophy.
199 * GNU Free Documentation License::     The license for this documentation.
200 * Index::                              General index.
201
202 @detailmenu
203  --- The Detailed Node Listing ---
204
205 Common Options
206
207 * Exit status::                 Indicating program success or failure.
208 * Backup options::              Backup options
209 * Block size::                  Block size
210 * Target directory::            Target directory
211 * Trailing slashes::            Trailing slashes
212 * Traversing symlinks::         Traversing symlinks to directories
213 * Treating / specially::        Treating / specially
214 * Standards conformance::       Standards conformance
215
216 Output of entire files
217
218 * cat invocation::              Concatenate and write files.
219 * tac invocation::              Concatenate and write files in reverse.
220 * nl invocation::               Number lines and write files.
221 * od invocation::               Write files in octal or other formats.
222 * base64 invocation::           Transform data into printable data.
223
224 Formatting file contents
225
226 * fmt invocation::              Reformat paragraph text.
227 * pr invocation::               Paginate or columnate files for printing.
228 * fold invocation::             Wrap input lines to fit in specified width.
229
230 Output of parts of files
231
232 * head invocation::             Output the first part of files.
233 * tail invocation::             Output the last part of files.
234 * split invocation::            Split a file into fixed-size pieces.
235 * csplit invocation::           Split a file into context-determined pieces.
236
237 Summarizing files
238
239 * wc invocation::               Print newline, word, and byte counts.
240 * sum invocation::              Print checksum and block counts.
241 * cksum invocation::            Print CRC checksum and byte counts.
242 * md5sum invocation::           Print or check MD5 digests.
243 * sha1sum invocation::          Print or check SHA-1 digests.
244 * sha2 utilities::              Print or check SHA-2 digests.
245
246 Operating on sorted files
247
248 * sort invocation::             Sort text files.
249 * uniq invocation::             Uniquify files.
250 * comm invocation::             Compare two sorted files line by line.
251 * ptx invocation::              Produce a permuted index of file contents.
252 * tsort invocation::            Topological sort.
253
254 @command{ptx}: Produce permuted indexes
255
256 * General options in ptx::      Options which affect general program behavior.
257 * Charset selection in ptx::    Underlying character set considerations.
258 * Input processing in ptx::     Input fields, contexts, and keyword selection.
259 * Output formatting in ptx::    Types of output format, and sizing the fields.
260 * Compatibility in ptx::        The @acronym{GNU} extensions to @command{ptx}
261
262 Operating on fields within a line
263
264 * cut invocation::              Print selected parts of lines.
265 * paste invocation::            Merge lines of files.
266 * join invocation::             Join lines on a common field.
267
268 Operating on characters
269
270 * tr invocation::               Translate, squeeze, and/or delete characters.
271 * expand invocation::           Convert tabs to spaces.
272 * unexpand invocation::         Convert spaces to tabs.
273
274 @command{tr}: Translate, squeeze, and/or delete characters
275
276 * Character sets::              Specifying sets of characters.
277 * Translating::                 Changing one set of characters to another.
278 * Squeezing::                   Squeezing repeats and deleting.
279
280 Directory listing
281
282 * ls invocation::               List directory contents
283 * dir invocation::              Briefly list directory contents
284 * vdir invocation::             Verbosely list directory contents
285 * dircolors invocation::        Color setup for @command{ls}
286
287 @command{ls}:  List directory contents
288
289 * Which files are listed::      Which files are listed
290 * What information is listed::  What information is listed
291 * Sorting the output::          Sorting the output
292 * More details about version sort::  More details about version sort
293 * General output formatting::   General output formatting
294 * Formatting the file names::   Formatting the file names
295
296 Basic operations
297
298 * cp invocation::               Copy files and directories
299 * dd invocation::               Convert and copy a file
300 * install invocation::          Copy files and set attributes
301 * mv invocation::               Move (rename) files
302 * rm invocation::               Remove files or directories
303 * shred invocation::            Remove files more securely
304
305 Special file types
306
307 * link invocation::             Make a hard link via the link syscall
308 * ln invocation::               Make links between files
309 * mkdir invocation::            Make directories
310 * mkfifo invocation::           Make FIFOs (named pipes)
311 * mknod invocation::            Make block or character special files
312 * readlink invocation::         Print the referent of a symbolic link
313 * rmdir invocation::            Remove empty directories
314 * unlink invocation::           Remove files via unlink syscall
315
316 Changing file attributes
317
318 * chown invocation::            Change file owner and group
319 * chgrp invocation::            Change group ownership
320 * chmod invocation::            Change access permissions
321 * touch invocation::            Change file timestamps
322
323 Disk usage
324
325 * df invocation::               Report file system disk space usage
326 * du invocation::               Estimate file space usage
327 * stat invocation::             Report file or file system status
328 * sync invocation::             Synchronize data on disk with memory
329
330 Printing text
331
332 * echo invocation::             Print a line of text
333 * printf invocation::           Format and print data
334 * yes invocation::              Print a string until interrupted
335
336 Conditions
337
338 * false invocation::              Do nothing, unsuccessfully
339 * true invocation::               Do nothing, successfully
340 * test invocation::               Check file types and compare values
341 * expr invocation::               Evaluate expressions
342
343 @command{test}: Check file types and compare values
344
345 * File type tests::               File type tests
346 * Access permission tests::       Access permission tests
347 * File characteristic tests::     File characteristic tests
348 * String tests::                  String tests
349 * Numeric tests::                 Numeric tests
350
351 @command{expr}: Evaluate expression
352
353 * String expressions::         + : match substr index length
354 * Numeric expressions::        + - * / %
355 * Relations for expr::         | & < <= = == != >= >
356 * Examples of expr::           Examples of using @command{expr}
357
358 Redirection
359
360 * tee invocation::               Redirect output to multiple files
361
362 File name manipulation
363
364 * basename invocation::          Strip directory and suffix from a file name
365 * dirname invocation::           Strip non-directory suffix from a file name
366 * pathchk invocation::           Check file name portability
367
368 Working context
369
370 * pwd invocation::               Print working directory
371 * stty invocation::              Print or change terminal characteristics
372 * printenv invocation::          Print all or some environment variables
373 * tty invocation::               Print file name of terminal on standard input
374
375 @command{stty}: Print or change terminal characteristics
376
377 * Control::                     Control settings
378 * Input::                       Input settings
379 * Output::                      Output settings
380 * Local::                       Local settings
381 * Combination::                 Combination settings
382 * Characters::                  Special characters
383 * Special::                     Special settings
384
385 User information
386
387 * id invocation::                Print user identity
388 * logname invocation::           Print current login name
389 * whoami invocation::            Print effective user ID
390 * groups invocation::            Print group names a user is in
391 * users invocation::             Print login names of users currently logged in
392 * who invocation::               Print who is currently logged in
393
394 System context
395
396 * date invocation::              Print or set system date and time
397 * uname invocation::             Print system information
398 * hostname invocation::          Print or set system name
399 * hostid invocation::            Print numeric host identifier.
400
401 @command{date}: Print or set system date and time
402
403 * Time conversion specifiers::   %[HIklMNpPrRsSTXzZ]
404 * Date conversion specifiers::   %[aAbBcCdDeFgGhjmuUVwWxyY]
405 * Literal conversion specifiers:: %[%nt]
406 * Padding and other flags::      Pad with zeros, spaces, etc.
407 * Setting the time::             Changing the system clock.
408 * Options for date::             Instead of the current time.
409 * Examples of date::             Examples.
410
411 Modified command invocation
412
413 * chroot invocation::            Run a command with a different root directory
414 * env invocation::               Run a command in a modified environment
415 * nice invocation::              Run a command with modified niceness
416 * nohup invocation::             Run a command immune to hangups
417 * su invocation::                Run a command with substitute user and group ID
418
419 Process control
420
421 * kill invocation::              Sending a signal to processes.
422
423 Delaying
424
425 * sleep invocation::             Delay for a specified time
426
427 Numeric operations
428
429 * factor invocation::            Print prime factors
430 * seq invocation::               Print numeric sequences
431
432 File permissions
433
434 * Mode Structure::               Structure of file mode bits.
435 * Symbolic Modes::               Mnemonic representation of file mode bits.
436 * Numeric Modes::                File mode bits as octal numbers.
437 * Directory Setuid and Setgid::  Set-user-ID and set-group-ID on directories.
438
439 Date input formats
440
441 * General date syntax::          Common rules.
442 * Calendar date items::          19 Dec 1994.
443 * Time of day items::            9:20pm.
444 * Time zone items::              @sc{est}, @sc{pdt}, @sc{gmt}.
445 * Day of week items::            Monday and others.
446 * Relative items in date strings:: next tuesday, 2 years ago.
447 * Pure numbers in date strings:: 19931219, 1440.
448 * Seconds since the Epoch::      @@1078100502.
449 * Specifying time zone rules::   TZ="America/New_York", TZ="UTC0".
450 * Authors of get_date::          Bellovin, Eggert, Salz, Berets, et al.
451
452 Opening the software toolbox
453
454 * Toolbox introduction::         Toolbox introduction
455 * I/O redirection::              I/O redirection
456 * The who command::              The @command{who} command
457 * The cut command::              The @command{cut} command
458 * The sort command::             The @command{sort} command
459 * The uniq command::             The @command{uniq} command
460 * Putting the tools together::   Putting the tools together
461
462 GNU Free Documentation License
463
464 * How to use this License for your documents::
465
466 @end detailmenu
467 @end menu
468
469
470 @node Introduction
471 @chapter Introduction
472
473 This manual is a work in progress: many sections make no attempt to explain
474 basic concepts in a way suitable for novices.  Thus, if you are interested,
475 please get involved in improving this manual.  The entire @sc{gnu} community
476 will benefit.
477
478 @cindex @acronym{POSIX}
479 The @sc{gnu} utilities documented here are mostly compatible with the
480 @acronym{POSIX} standard.
481 @cindex bugs, reporting
482 Please report bugs to @email{bug-coreutils@@gnu.org}.  Remember
483 to include the version number, machine architecture, input files, and
484 any other information needed to reproduce the bug: your input, what you
485 expected, what you got, and why it is wrong.  Diffs are welcome, but
486 please include a description of the problem as well, since this is
487 sometimes difficult to infer.  @xref{Bugs, , , gcc, Using and Porting GNU CC}.
488
489 @cindex Berry, K.
490 @cindex Paterson, R.
491 @cindex Stallman, R.
492 @cindex Pinard, F.
493 @cindex MacKenzie, D.
494 @cindex Meyering, J.
495 @cindex Youmans, B.
496 This manual was originally derived from the Unix man pages in the
497 distributions, which were written by David MacKenzie and updated by Jim
498 Meyering.  What you are reading now is the authoritative documentation
499 for these utilities; the man pages are no longer being maintained.  The
500 original @command{fmt} man page was written by Ross Paterson.  Fran@,{c}ois
501 Pinard did the initial conversion to Texinfo format.  Karl Berry did the
502 indexing, some reorganization, and editing of the results.  Brian
503 Youmans of the Free Software Foundation office staff combined the
504 manuals for textutils, fileutils, and sh-utils to produce the present
505 omnibus manual.  Richard Stallman contributed his usual invaluable
506 insights to the overall process.
507
508 @node Common options
509 @chapter Common options
510
511 @macro optBackup
512 @item -b
513 @itemx @w{@kbd{--backup}[=@var{method}]}
514 @opindex -b
515 @opindex --backup
516 @vindex VERSION_CONTROL
517 @cindex backups, making
518 @xref{Backup options}.
519 Make a backup of each file that would otherwise be overwritten or removed.
520 @end macro
521
522 @macro optBackupSuffix
523 @item -S @var{suffix}
524 @itemx --suffix=@var{suffix}
525 @opindex -S
526 @opindex --suffix
527 Append @var{suffix} to each backup file made with @option{-b}.
528 @xref{Backup options}.
529 @end macro
530
531 @macro optTargetDirectory
532 @item -t @var{directory}
533 @itemx @w{@kbd{--target-directory}=@var{directory}}
534 @opindex -t
535 @opindex --target-directory
536 @cindex target directory
537 @cindex destination directory
538 Specify the destination @var{directory}.
539 @xref{Target directory}.
540 @end macro
541
542 @macro optNoTargetDirectory
543 @item -T
544 @itemx --no-target-directory
545 @opindex -T
546 @opindex --no-target-directory
547 @cindex target directory
548 @cindex destination directory
549 Do not treat the last operand specially when it is a directory or a
550 symbolic link to a directory.  @xref{Target directory}.
551 @end macro
552
553 @macro optSi
554 @itemx --si
555 @opindex --si
556 @cindex SI output
557 Append an SI-style abbreviation to each size, such as @samp{MB} for
558 megabytes.  Powers of 1000 are used, not 1024; @samp{MB} stands for
559 1,000,000 bytes.  This option is equivalent to
560 @option{--block-size=si}.  Use the @option{-h} or
561 @option{--human-readable} option if
562 you prefer powers of 1024.
563 @end macro
564
565 @macro optHumanReadable
566 @item -h
567 @itemx --human-readable
568 @opindex -h
569 @opindex --human-readable
570 @cindex human-readable output
571 Append a size letter to each size, such as @samp{M} for mebibytes.
572 Powers of 1024 are used, not 1000; @samp{M} stands for 1,048,576 bytes.
573 Use the @option{--si} option if you prefer powers of 1000.
574 @end macro
575
576 @macro optStripTrailingSlashes
577 @itemx @w{@kbd{--strip-trailing-slashes}}
578 @opindex --strip-trailing-slashes
579 @cindex stripping trailing slashes
580 Remove any trailing slashes from each @var{source} argument.
581 @xref{Trailing slashes}.
582 @end macro
583
584 @cindex common options
585
586 Certain options are available in all of these programs.  Rather than
587 writing identical descriptions for each of the programs, they are
588 described here.  (In fact, every @sc{gnu} program accepts (or should accept)
589 these options.)
590
591 @vindex POSIXLY_CORRECT
592 Normally options and operands can appear in any order, and programs act
593 as if all the options appear before any operands.  For example,
594 @samp{sort -r passwd -t :} acts like @samp{sort -r -t : passwd}, since
595 @samp{:} is an option-argument of @option{-t}.  However, if the
596 @env{POSIXLY_CORRECT} environment variable is set, options must appear
597 before operands, unless otherwise specified for a particular command.
598
599 A few programs can usefully have trailing operands with leading
600 @samp{-}.  With such a program, options must precede operands even if
601 @env{POSIXLY_CORRECT} is not set, and this fact is noted in the
602 program description.  For example, the @command{env} command's options
603 must appear before its operands, since in some cases the operands
604 specify a command that itself contains options.
605
606 Some of these programs recognize the @option{--help} and @option{--version}
607 options only when one of them is the sole command line argument.
608
609 @table @samp
610
611 @item --help
612 @opindex --help
613 @cindex help, online
614 Print a usage message listing all available options, then exit successfully.
615
616 @item --version
617 @opindex --version
618 @cindex version number, finding
619 Print the version number, then exit successfully.
620
621 @item --
622 @opindex --
623 @cindex option delimiter
624 Delimit the option list.  Later arguments, if any, are treated as
625 operands even if they begin with @samp{-}.  For example, @samp{sort --
626 -r} reads from the file named @file{-r}.
627
628 @end table
629
630 @cindex standard input
631 @cindex standard output
632 A single @samp{-} operand is not really an option, though it looks like one.  It
633 stands for standard input, or for standard output if that is clear from
634 the context.  For example, @samp{sort -} reads from standard input,
635 and is equivalent to plain @samp{sort}, and @samp{tee -} writes an
636 extra copy of its input to standard output.  Unless otherwise
637 specified, @samp{-} can appear as any operand that requires a file
638 name.
639
640 @menu
641 * Exit status::                 Indicating program success or failure.
642 * Backup options::              -b -S, in some programs.
643 * Block size::                  BLOCK_SIZE and --block-size, in some programs.
644 * Target directory::            Specifying a target directory, in some programs.
645 * Trailing slashes::            --strip-trailing-slashes, in some programs.
646 * Traversing symlinks::         -H, -L, or -P, in some programs.
647 * Treating / specially::        --preserve-root and --no-preserve-root.
648 * Special built-in utilities::  @command{break}, @command{:}, @command{eval}, @dots{}
649 * Standards conformance::       Conformance to the @acronym{POSIX} standard.
650 @end menu
651
652
653 @node Exit status
654 @section Exit status
655
656 @macro exitstatus
657 An exit status of zero indicates success,
658 and a nonzero value indicates failure.
659 @end macro
660
661 Nearly every command invocation yields an integral @dfn{exit status}
662 that can be used to change how other commands work.
663 For the vast majority of commands, an exit status of zero indicates
664 success.  Failure is indicated by a nonzero value---typically
665 @samp{1}, though it may differ on unusual platforms as @acronym{POSIX}
666 requires only that it be nonzero.
667
668 However, some of the programs documented here do produce
669 other exit status values and a few associate different
670 meanings with the values @samp{0} and @samp{1}.
671 Here are some of the exceptions:
672 @command{chroot}, @command{env}, @command{expr},
673 @command{nice}, @command{nohup}, @command{printenv},
674 @command{sort}, @command{su}, @command{test}, @command{tty}.
675
676
677 @node Backup options
678 @section Backup options
679
680 @cindex backup options
681
682 Some @sc{gnu} programs (at least @command{cp}, @command{install},
683 @command{ln}, and @command{mv}) optionally make backups of files
684 before writing new versions.
685 These options control the details of these backups.  The options are also
686 briefly mentioned in the descriptions of the particular programs.
687
688 @table @samp
689
690 @item -b
691 @itemx @w{@kbd{--backup}[=@var{method}]}
692 @opindex -b
693 @opindex --backup
694 @vindex VERSION_CONTROL
695 @cindex backups, making
696 Make a backup of each file that would otherwise be overwritten or removed.
697 Without this option, the original versions are destroyed.
698 Use @var{method} to determine the type of backups to make.
699 When this option is used but @var{method} is not specified,
700 then the value of the @env{VERSION_CONTROL}
701 environment variable is used.  And if @env{VERSION_CONTROL} is not set,
702 the default backup type is @samp{existing}.
703
704 Note that the short form of this option, @option{-b} does not accept any
705 argument.  Using @option{-b} is equivalent to using @option{--backup=existing}.
706
707 @vindex version-control @r{Emacs variable}
708 This option corresponds to the Emacs variable @samp{version-control};
709 the values for @var{method} are the same as those used in Emacs.
710 This option also accepts more descriptive names.
711 The valid @var{method}s are (unique abbreviations are accepted):
712
713 @table @samp
714 @item none
715 @itemx off
716 @opindex none @r{backup method}
717 Never make backups.
718
719 @item numbered
720 @itemx t
721 @opindex numbered @r{backup method}
722 Always make numbered backups.
723
724 @item existing
725 @itemx nil
726 @opindex existing @r{backup method}
727 Make numbered backups of files that already have them, simple backups
728 of the others.
729
730 @item simple
731 @itemx never
732 @opindex simple @r{backup method}
733 Always make simple backups.  Please note @samp{never} is not to be
734 confused with @samp{none}.
735
736 @end table
737
738 @item -S @var{suffix}
739 @itemx --suffix=@var{suffix}
740 @opindex -S
741 @opindex --suffix
742 @cindex backup suffix
743 @vindex SIMPLE_BACKUP_SUFFIX
744 Append @var{suffix} to each backup file made with @option{-b}.  If this
745 option is not specified, the value of the @env{SIMPLE_BACKUP_SUFFIX}
746 environment variable is used.  And if @env{SIMPLE_BACKUP_SUFFIX} is not
747 set, the default is @samp{~}, just as in Emacs.
748
749 @end table
750
751 @node Block size
752 @section Block size
753
754 @cindex block size
755
756 Some @sc{gnu} programs (at least @command{df}, @command{du}, and
757 @command{ls}) display sizes in ``blocks''.  You can adjust the block size
758 and method of display to make sizes easier to read.  The block size
759 used for display is independent of any file system block size.
760 Fractional block counts are rounded up to the nearest integer.
761
762 @opindex --block-size=@var{size}
763 @vindex BLOCKSIZE
764 @vindex BLOCK_SIZE
765 @vindex DF_BLOCK_SIZE
766 @vindex DU_BLOCK_SIZE
767 @vindex LS_BLOCK_SIZE
768 @vindex POSIXLY_CORRECT@r{, and block size}
769
770 The default block size is chosen by examining the following environment
771 variables in turn; the first one that is set determines the block size.
772
773 @table @code
774
775 @item DF_BLOCK_SIZE
776 This specifies the default block size for the @command{df} command.
777 Similarly, @env{DU_BLOCK_SIZE} specifies the default for @command{du} and
778 @env{LS_BLOCK_SIZE} for @command{ls}.
779
780 @item BLOCK_SIZE
781 This specifies the default block size for all three commands, if the
782 above command-specific environment variables are not set.
783
784 @item BLOCKSIZE
785 This specifies the default block size for all values that are normally
786 printed as blocks, if neither @env{BLOCK_SIZE} nor the above
787 command-specific environment variables are set.  Unlike the other
788 environment variables, @env{BLOCKSIZE} does not affect values that are
789 normally printed as byte counts, e.g., the file sizes contained in
790 @code{ls -l} output.
791
792 @item POSIXLY_CORRECT
793 If neither @env{@var{command}_BLOCK_SIZE}, nor @env{BLOCK_SIZE}, nor
794 @env{BLOCKSIZE} is set, but this variable is set, the block size
795 defaults to 512.
796
797 @end table
798
799 If none of the above environment variables are set, the block size
800 currently defaults to 1024 bytes in most contexts, but this number may
801 change in the future.  For @command{ls} file sizes, the block size
802 defaults to 1 byte.
803
804 @cindex human-readable output
805 @cindex SI output
806
807 A block size specification can be a positive integer specifying the number
808 of bytes per block, or it can be @code{human-readable} or @code{si} to
809 select a human-readable format.  Integers may be followed by suffixes
810 that are upward compatible with the
811 @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI prefixes}
812 for decimal multiples and with the
813 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
814 prefixes for binary multiples}.
815
816 With human-readable formats, output sizes are followed by a size letter
817 such as @samp{M} for megabytes.  @code{BLOCK_SIZE=human-readable} uses
818 powers of 1024; @samp{M} stands for 1,048,576 bytes.
819 @code{BLOCK_SIZE=si} is similar, but uses powers of 1000 and appends
820 @samp{B}; @samp{MB} stands for 1,000,000 bytes.
821
822 @vindex LC_NUMERIC
823 A block size specification preceded by @samp{'} causes output sizes to
824 be displayed with thousands separators.  The @env{LC_NUMERIC} locale
825 specifies the thousands separator and grouping.  For example, in an
826 American English locale, @samp{--block-size="'1kB"} would cause a size
827 of 1234000 bytes to be displayed as @samp{1,234}.  In the default C
828 locale, there is no thousands separator so a leading @samp{'} has no
829 effect.
830
831 An integer block size can be followed by a suffix to specify a
832 multiple of that size.  A bare size letter,
833 or one followed by @samp{iB}, specifies
834 a multiple using powers of 1024.  A size letter followed by @samp{B}
835 specifies powers of 1000 instead.  For example, @samp{1M} and
836 @samp{1MiB} are equivalent to @samp{1048576}, whereas @samp{1MB} is
837 equivalent to @samp{1000000}.
838
839 A plain suffix without a preceding integer acts as if @samp{1} were
840 prepended, except that it causes a size indication to be appended to
841 the output.  For example, @samp{--block-size="kB"} displays 3000 as
842 @samp{3kB}.
843
844 The following suffixes are defined.  Large sizes like @code{1Y}
845 may be rejected by your computer due to limitations of its arithmetic.
846
847 @table @samp
848 @item kB
849 @cindex kilobyte, definition of
850 kilobyte: @math{10^3 = 1000}.
851 @item k
852 @itemx K
853 @itemx KiB
854 @cindex kibibyte, definition of
855 kibibyte: @math{2^10 = 1024}.  @samp{K} is special: the SI prefix is
856 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
857 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
858 @item MB
859 @cindex megabyte, definition of
860 megabyte: @math{10^6 = 1,000,000}.
861 @item M
862 @itemx MiB
863 @cindex mebibyte, definition of
864 mebibyte: @math{2^20 = 1,048,576}.
865 @item GB
866 @cindex gigabyte, definition of
867 gigabyte: @math{10^9 = 1,000,000,000}.
868 @item G
869 @itemx GiB
870 @cindex gibibyte, definition of
871 gibibyte: @math{2^30 = 1,073,741,824}.
872 @item TB
873 @cindex terabyte, definition of
874 terabyte:  @math{10^12 = 1,000,000,000,000}.
875 @item T
876 @itemx TiB
877 @cindex tebibyte, definition of
878 tebibyte: @math{2^40 = 1,099,511,627,776}.
879 @item PB
880 @cindex petabyte, definition of
881 petabyte: @math{10^15 = 1,000,000,000,000,000}.
882 @item P
883 @itemx PiB
884 @cindex pebibyte, definition of
885 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
886 @item EB
887 @cindex exabyte, definition of
888 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
889 @item E
890 @itemx EiB
891 @cindex exbibyte, definition of
892 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
893 @item ZB
894 @cindex zettabyte, definition of
895 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
896 @item Z
897 @itemx ZiB
898 @math{2^70 = 1,180,591,620,717,411,303,424}.
899 (@samp{Zi} is a @acronym{GNU} extension to IEC 60027-2.)
900 @item YB
901 @cindex yottabyte, definition of
902 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
903 @item Y
904 @itemx YiB
905 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
906 (@samp{Yi} is a @acronym{GNU} extension to IEC 60027-2.)
907 @end table
908
909 @opindex -k
910 @opindex -h
911 @opindex --block-size
912 @opindex --human-readable
913 @opindex --si
914
915 Block size defaults can be overridden by an explicit
916 @option{--block-size=@var{size}} option.  The @option{-k}
917 option is equivalent to @option{--block-size=1K}, which
918 is the default unless the @env{POSIXLY_CORRECT} environment variable is
919 set.  The @option{-h} or @option{--human-readable} option is equivalent to
920 @option{--block-size=human-readable}.  The @option{--si} option is
921 equivalent to @option{--block-size=si}.
922
923 @node Target directory
924 @section Target directory
925
926 @cindex target directory
927
928 The @command{cp}, @command{install}, @command{ln}, and @command{mv}
929 commands normally treat the last operand specially when it is a
930 directory or a symbolic link to a directory.  For example, @samp{cp
931 source dest} is equivalent to @samp{cp source dest/source} if
932 @file{dest} is a directory.  Sometimes this behavior is not exactly
933 what is wanted, so these commands support the following options to
934 allow more fine-grained control:
935
936 @table @samp
937
938 @item -T
939 @itemx --no-target-directory
940 @opindex --no-target-directory
941 @cindex target directory
942 @cindex destination directory
943 Do not treat the last operand specially when it is a directory or a
944 symbolic link to a directory.  This can help avoid race conditions in
945 programs that operate in a shared area.  For example, when the command
946 @samp{mv /tmp/source /tmp/dest} succeeds, there is no guarantee that
947 @file{/tmp/source} was renamed to @file{/tmp/dest}: it could have been
948 renamed to @file{/tmp/dest/source} instead, if some other process
949 created @file{/tmp/dest} as a directory.  However, if @file{mv
950 -T /tmp/source /tmp/dest} succeeds, there is no
951 question that @file{/tmp/source} was renamed to @file{/tmp/dest}.
952
953 In the opposite situation, where you want the last operand to be
954 treated as a directory and want a diagnostic otherwise, you can use
955 the @option{--target-directory} (@option{-t}) option.
956
957 @item -t @var{directory}
958 @itemx @w{@kbd{--target-directory}=@var{directory}}
959 @opindex --target-directory
960 @cindex target directory
961 @cindex destination directory
962 Use @var{directory} as the directory component of each destination
963 file name.
964
965 The interface for most programs is that after processing options and a
966 finite (possibly zero) number of fixed-position arguments, the remaining
967 argument list is either expected to be empty, or is a list of items
968 (usually files) that will all be handled identically.  The @command{xargs}
969 program is designed to work well with this convention.
970
971 The commands in the @command{mv}-family are unusual in that they take
972 a variable number of arguments with a special case at the @emph{end}
973 (namely, the target directory).  This makes it nontrivial to perform some
974 operations, e.g., ``move all files from here to ../d/'', because
975 @code{mv * ../d/} might exhaust the argument space, and @code{ls | xargs ...}
976 doesn't have a clean way to specify an extra final argument for each
977 invocation of the subject command.  (It can be done by going through a
978 shell command, but that requires more human labor and brain power than
979 it should.)
980
981 The @w{@kbd{--target-directory}} (@option{-t}) option allows the @command{cp},
982 @command{install}, @command{ln}, and @command{mv} programs to be used
983 conveniently with @command{xargs}.  For example, you can move the files
984 from the current directory to a sibling directory, @code{d} like this:
985
986 @smallexample
987 ls | xargs mv -t ../d --
988 @end smallexample
989
990 However, this doesn't move files whose names begin with @samp{.}.
991 If you use the @sc{gnu} @command{find} program, you can move those
992 files too, with this command:
993
994 @example
995 find . -mindepth 1 -maxdepth 1 \
996   | xargs mv -t ../d
997 @end example
998
999 But both of the above approaches fail if there are no files in the
1000 current directory, or if any file has a name containing a blank or
1001 some other special characters.
1002 The following example removes those limitations and requires both
1003 @sc{gnu} @command{find} and @sc{gnu} @command{xargs}:
1004
1005 @example
1006 find . -mindepth 1 -maxdepth 1 -print0 \
1007   | xargs --null --no-run-if-empty \
1008       mv -t ../d
1009 @end example
1010
1011 @end table
1012
1013 @noindent
1014 The @option{--target-directory} (@option{-t}) and
1015 @option{--no-target-directory} (@option{-T})
1016 options cannot be combined.
1017
1018 @node Trailing slashes
1019 @section Trailing slashes
1020
1021 @cindex trailing slashes
1022
1023 Some @sc{gnu} programs (at least @command{cp} and @command{mv}) allow you to
1024 remove any trailing slashes from each @var{source} argument before
1025 operating on it.  The @w{@kbd{--strip-trailing-slashes}} option enables
1026 this behavior.
1027
1028 This is useful when a @var{source} argument may have a trailing slash and
1029 @c FIXME: mv's behavior in this case is system-dependent
1030 specify a symbolic link to a directory.  This scenario is in fact rather
1031 common because some shells can automatically append a trailing slash when
1032 performing file name completion on such symbolic links.  Without this
1033 option, @command{mv}, for example, (via the system's rename function) must
1034 interpret a trailing slash as a request to dereference the symbolic link
1035 and so must rename the indirectly referenced @emph{directory} and not
1036 the symbolic link.  Although it may seem surprising that such behavior
1037 be the default, it is required by @acronym{POSIX} and is consistent with
1038 other parts of that standard.
1039
1040 @node Traversing symlinks
1041 @section Traversing symlinks
1042
1043 @cindex symbolic link to directory, controlling traversal of
1044
1045 The following options modify how @command{chown} and @command{chgrp}
1046 @c FIXME: note that `du' has these options, too, but they have slightly
1047 @c different meaning.
1048 traverse a hierarchy when the @option{--recursive} (@option{-R})
1049 option is also specified.
1050 If more than one of the following options is specified, only the final
1051 one takes effect.
1052 These options specify whether processing a symbolic link to a directory
1053 entails operating on just the symbolic link or on all files in the
1054 hierarchy rooted at that directory.
1055
1056 These options are independent of @option{--dereference} and
1057 @option{--no-dereference} (@option{-h}), which control whether to modify
1058 a symlink or its referent.
1059
1060 @table @samp
1061
1062 @macro choptH
1063 @item -H
1064 @opindex -H
1065 @cindex symbolic link to directory, traverse each that is specified on the command line
1066 If @option{--recursive} (@option{-R}) is specified and
1067 a command line argument is a symbolic link to a directory, traverse it.
1068 @end macro
1069 @choptH
1070
1071 @macro choptL
1072 @item -L
1073 @opindex -L
1074 @cindex symbolic link to directory, traverse each that is encountered
1075 In a recursive traversal, traverse every symbolic link to a directory
1076 that is encountered.
1077 @end macro
1078 @choptL
1079
1080 @macro choptP
1081 @item -P
1082 @opindex -P
1083 @cindex symbolic link to directory, never traverse
1084 Do not traverse any symbolic links.
1085 This is the default if none of @option{-H}, @option{-L},
1086 or @option{-P} is specified.
1087 @end macro
1088 @choptP
1089
1090 @end table
1091
1092
1093 @node Treating / specially
1094 @section Treating / specially
1095
1096 Certain commands can operate destructively on entire hierarchies.
1097 For example, if a user with appropriate privileges mistakenly runs
1098 @samp{rm -rf / tmp/junk} or @samp{cd /bin; rm -rf ../}, that may remove
1099 all files on the entire system.  Since there are so few
1100 @footnote{If you know of one, please write to @email{bug-coreutils@@gnu.org}.}
1101 legitimate uses for such a command,
1102 @sc{gnu} @command{rm} provides the @option{--preserve-root} option
1103 to make it so @command{rm} declines to operate on any directory
1104 that resolves to @file{/}.  The default is still to allow
1105 @samp{rm -rf /} to operate unimpeded.
1106 Another new option, @option{--no-preserve-root}, cancels the
1107 effect of any preceding @option{--preserve-root} option.
1108 Note that the @option{--preserve-root} behavior may become the default
1109 for @command{rm}.
1110
1111 The commands @command{chgrp}, @command{chmod} and @command{chown}
1112 can also operate destructively on entire hierarchies, so they too
1113 support these options.  Although, unlike @command{rm}, they don't
1114 actually unlink files, these commands are arguably more dangerous
1115 when operating recursively on @file{/}, since they often work much
1116 more quickly, and hence damage more files before an alert user can
1117 interrupt them.
1118
1119 @node Special built-in utilities
1120 @section Special built-in utilities
1121
1122 Some programs like @command{nice} can invoke other programs; for
1123 example, the command @samp{nice cat file} invokes the program
1124 @command{cat} by executing the command @samp{cat file}.  However,
1125 @dfn{special built-in utilities} like @command{exit} cannot be invoked
1126 this way.  For example, the command @samp{nice exit} does not have a
1127 well-defined behavior: it may generate an error message instead of
1128 exiting.
1129
1130 Here is a list of the special built-in utilities that are standardized
1131 by @acronym{POSIX} 1003.1-2004.
1132
1133 @quotation
1134 @t{.@: : break continue eval exec exit export readonly
1135 return set shift times trap unset}
1136 @end quotation
1137
1138 For example, because @samp{.}, @samp{:}, and @samp{exec} are special,
1139 the commands @samp{nice . foo.sh}, @samp{nice :}, and @samp{nice exec
1140 pwd} do not work as you might expect.
1141
1142 Many shells extend this list.  For example, Bash has several extra
1143 special built-in utilities like @command{history}, and
1144 @command{suspend}, and with Bash the command @samp{nice suspend}
1145 generates an error message instead of suspending.
1146
1147 @node Standards conformance
1148 @section Standards conformance
1149
1150 @vindex POSIXLY_CORRECT
1151 In a few cases, the @sc{gnu} utilities' default behavior is
1152 incompatible with the @acronym{POSIX} standard.  To suppress these
1153 incompatibilities, define the @env{POSIXLY_CORRECT} environment
1154 variable.  Unless you are checking for @acronym{POSIX} conformance, you
1155 probably do not need to define @env{POSIXLY_CORRECT}.
1156
1157 Newer versions of @acronym{POSIX} are occasionally incompatible with older
1158 versions.  For example, older versions of @acronym{POSIX} required the
1159 command @samp{sort +1} to sort based on the second and succeeding
1160 fields in each input line, but starting with @acronym{POSIX} 1003.1-2001
1161 the same command is required to sort the file named @file{+1}, and you
1162 must instead use the command @samp{sort -k 2} to get the field-based
1163 sort.
1164
1165 @vindex _POSIX2_VERSION
1166 The @sc{gnu} utilities normally conform to the version of @acronym{POSIX}
1167 that is standard for your system.  To cause them to conform to a
1168 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
1169 environment variable to a value of the form @var{yyyymm} specifying
1170 the year and month the standard was adopted.  Two values are currently
1171 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
1172 @acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX}
1173 1003.1-2001.  For example, if you have a newer system but are running software
1174 that assumes an older version of @acronym{POSIX} and uses @samp{sort +1}
1175 or @samp{tail +10}, you can work around any compatibility problems by setting
1176 @samp{_POSIX2_VERSION=199209} in your environment.
1177
1178 @node Output of entire files
1179 @chapter Output of entire files
1180
1181 @cindex output of entire files
1182 @cindex entire files, output of
1183
1184 These commands read and write entire files, possibly transforming them
1185 in some way.
1186
1187 @menu
1188 * cat invocation::              Concatenate and write files.
1189 * tac invocation::              Concatenate and write files in reverse.
1190 * nl invocation::               Number lines and write files.
1191 * od invocation::               Write files in octal or other formats.
1192 * base64 invocation::           Transform data into printable data.
1193 @end menu
1194
1195 @node cat invocation
1196 @section @command{cat}: Concatenate and write files
1197
1198 @pindex cat
1199 @cindex concatenate and write files
1200 @cindex copying files
1201
1202 @command{cat} copies each @var{file} (@samp{-} means standard input), or
1203 standard input if none are given, to standard output.  Synopsis:
1204
1205 @example
1206 cat [@var{option}] [@var{file}]@dots{}
1207 @end example
1208
1209 The program accepts the following options.  Also see @ref{Common options}.
1210
1211 @table @samp
1212
1213 @item -A
1214 @itemx --show-all
1215 @opindex -A
1216 @opindex --show-all
1217 Equivalent to @option{-vET}.
1218
1219 @item -b
1220 @itemx --number-nonblank
1221 @opindex -b
1222 @opindex --number-nonblank
1223 Number all nonblank output lines, starting with 1.
1224
1225 @item -e
1226 @opindex -e
1227 Equivalent to @option{-vE}.
1228
1229 @item -E
1230 @itemx --show-ends
1231 @opindex -E
1232 @opindex --show-ends
1233 Display a @samp{$} after the end of each line.
1234
1235 @item -n
1236 @itemx --number
1237 @opindex -n
1238 @opindex --number
1239 Number all output lines, starting with 1.
1240
1241 @item -s
1242 @itemx --squeeze-blank
1243 @opindex -s
1244 @opindex --squeeze-blank
1245 @cindex squeezing blank lines
1246 Replace multiple adjacent blank lines with a single blank line.
1247
1248 @item -t
1249 @opindex -t
1250 Equivalent to @option{-vT}.
1251
1252 @item -T
1253 @itemx --show-tabs
1254 @opindex -T
1255 @opindex --show-tabs
1256 Display TAB characters as @samp{^I}.
1257
1258 @item -u
1259 @opindex -u
1260 Ignored; for @acronym{POSIX} compatibility.
1261
1262 @item -v
1263 @itemx --show-nonprinting
1264 @opindex -v
1265 @opindex --show-nonprinting
1266 Display control characters except for LFD and TAB using
1267 @samp{^} notation and precede characters that have the high bit set with
1268 @samp{M-}.
1269
1270 @end table
1271
1272 On systems like MS-DOS that distinguish between text and binary files,
1273 @command{cat} normally reads and writes in binary mode.  However,
1274 @command{cat} reads in text mode if one of the options
1275 @option{-bensAE} is used or if @command{cat} is reading from standard
1276 input and standard input is a terminal.  Similarly, @command{cat}
1277 writes in text mode if one of the options @option{-bensAE} is used or
1278 if standard output is a terminal.
1279
1280 @exitstatus
1281
1282 Examples:
1283
1284 @smallexample
1285 # Output f's contents, then standard input, then g's contents.
1286 cat f - g
1287
1288 # Copy standard input to standard output.
1289 cat
1290 @end smallexample
1291
1292
1293 @node tac invocation
1294 @section @command{tac}: Concatenate and write files in reverse
1295
1296 @pindex tac
1297 @cindex reversing files
1298
1299 @command{tac} copies each @var{file} (@samp{-} means standard input), or
1300 standard input if none are given, to standard output, reversing the
1301 records (lines by default) in each separately.  Synopsis:
1302
1303 @example
1304 tac [@var{option}]@dots{} [@var{file}]@dots{}
1305 @end example
1306
1307 @dfn{Records} are separated by instances of a string (newline by
1308 default).  By default, this separator string is attached to the end of
1309 the record that it follows in the file.
1310
1311 The program accepts the following options.  Also see @ref{Common options}.
1312
1313 @table @samp
1314
1315 @item -b
1316 @itemx --before
1317 @opindex -b
1318 @opindex --before
1319 The separator is attached to the beginning of the record that it
1320 precedes in the file.
1321
1322 @item -r
1323 @itemx --regex
1324 @opindex -r
1325 @opindex --regex
1326 Treat the separator string as a regular expression.  Users of @command{tac}
1327 on MS-DOS/MS-Windows should note that, since @command{tac} reads files in
1328 binary mode, each line of a text file might end with a CR/LF pair
1329 instead of the Unix-style LF.
1330
1331 @item -s @var{separator}
1332 @itemx --separator=@var{separator}
1333 @opindex -s
1334 @opindex --separator
1335 Use @var{separator} as the record separator, instead of newline.
1336
1337 @end table
1338
1339 @exitstatus
1340
1341
1342 @node nl invocation
1343 @section @command{nl}: Number lines and write files
1344
1345 @pindex nl
1346 @cindex numbering lines
1347 @cindex line numbering
1348
1349 @command{nl} writes each @var{file} (@samp{-} means standard input), or
1350 standard input if none are given, to standard output, with line numbers
1351 added to some or all of the lines.  Synopsis:
1352
1353 @example
1354 nl [@var{option}]@dots{} [@var{file}]@dots{}
1355 @end example
1356
1357 @cindex logical pages, numbering on
1358 @command{nl} decomposes its input into (logical) pages; by default, the
1359 line number is reset to 1 at the top of each logical page.  @command{nl}
1360 treats all of the input files as a single document; it does not reset
1361 line numbers or logical pages between files.
1362
1363 @cindex headers, numbering
1364 @cindex body, numbering
1365 @cindex footers, numbering
1366 A logical page consists of three sections: header, body, and footer.
1367 Any of the sections can be empty.  Each can be numbered in a different
1368 style from the others.
1369
1370 The beginnings of the sections of logical pages are indicated in the
1371 input file by a line containing exactly one of these delimiter strings:
1372
1373 @table @samp
1374 @item \:\:\:
1375 start of header;
1376 @item \:\:
1377 start of body;
1378 @item \:
1379 start of footer.
1380 @end table
1381
1382 The two characters from which these strings are made can be changed from
1383 @samp{\} and @samp{:} via options (see below), but the pattern and
1384 length of each string cannot be changed.
1385
1386 A section delimiter is replaced by an empty line on output.  Any text
1387 that comes before the first section delimiter string in the input file
1388 is considered to be part of a body section, so @command{nl} treats a
1389 file that contains no section delimiters as a single body section.
1390
1391 The program accepts the following options.  Also see @ref{Common options}.
1392
1393 @table @samp
1394
1395 @item -b @var{style}
1396 @itemx --body-numbering=@var{style}
1397 @opindex -b
1398 @opindex --body-numbering
1399 Select the numbering style for lines in the body section of each
1400 logical page.  When a line is not numbered, the current line number
1401 is not incremented, but the line number separator character is still
1402 prepended to the line.  The styles are:
1403
1404 @table @samp
1405 @item a
1406 number all lines,
1407 @item t
1408 number only nonempty lines (default for body),
1409 @item n
1410 do not number lines (default for header and footer),
1411 @item p@var{bre}
1412 number only lines that contain a match for the basic regular
1413 expression @var{bre}.
1414 @xref{Regular Expressions, , Regular Expressions, grep, The GNU Grep Manual}.
1415 @end table
1416
1417 @item -d @var{cd}
1418 @itemx --section-delimiter=@var{cd}
1419 @opindex -d
1420 @opindex --section-delimiter
1421 @cindex section delimiters of pages
1422 Set the section delimiter characters to @var{cd}; default is
1423 @samp{\:}.  If only @var{c} is given, the second remains @samp{:}.
1424 (Remember to protect @samp{\} or other metacharacters from shell
1425 expansion with quotes or extra backslashes.)
1426
1427 @item -f @var{style}
1428 @itemx --footer-numbering=@var{style}
1429 @opindex -f
1430 @opindex --footer-numbering
1431 Analogous to @option{--body-numbering}.
1432
1433 @item -h @var{style}
1434 @itemx --header-numbering=@var{style}
1435 @opindex -h
1436 @opindex --header-numbering
1437 Analogous to @option{--body-numbering}.
1438
1439 @item -i @var{number}
1440 @itemx --page-increment=@var{number}
1441 @opindex -i
1442 @opindex --page-increment
1443 Increment line numbers by @var{number} (default 1).
1444
1445 @item -l @var{number}
1446 @itemx --join-blank-lines=@var{number}
1447 @opindex -l
1448 @opindex --join-blank-lines
1449 @cindex empty lines, numbering
1450 @cindex blank lines, numbering
1451 Consider @var{number} (default 1) consecutive empty lines to be one
1452 logical line for numbering, and only number the last one.  Where fewer
1453 than @var{number} consecutive empty lines occur, do not number them.
1454 An empty line is one that contains no characters, not even spaces
1455 or tabs.
1456
1457 @item -n @var{format}
1458 @itemx --number-format=@var{format}
1459 @opindex -n
1460 @opindex --number-format
1461 Select the line numbering format (default is @code{rn}):
1462
1463 @table @samp
1464 @item ln
1465 @opindex ln @r{format for @command{nl}}
1466 left justified, no leading zeros;
1467 @item rn
1468 @opindex rn @r{format for @command{nl}}
1469 right justified, no leading zeros;
1470 @item rz
1471 @opindex rz @r{format for @command{nl}}
1472 right justified, leading zeros.
1473 @end table
1474
1475 @item -p
1476 @itemx --no-renumber
1477 @opindex -p
1478 @opindex --no-renumber
1479 Do not reset the line number at the start of a logical page.
1480
1481 @item -s @var{string}
1482 @itemx --number-separator=@var{string}
1483 @opindex -s
1484 @opindex --number-separator
1485 Separate the line number from the text line in the output with
1486 @var{string} (default is the TAB character).
1487
1488 @item -v @var{number}
1489 @itemx --starting-line-number=@var{number}
1490 @opindex -v
1491 @opindex --starting-line-number
1492 Set the initial line number on each logical page to @var{number} (default 1).
1493
1494 @item -w @var{number}
1495 @itemx --number-width=@var{number}
1496 @opindex -w
1497 @opindex --number-width
1498 Use @var{number} characters for line numbers (default 6).
1499
1500 @end table
1501
1502 @exitstatus
1503
1504
1505 @node od invocation
1506 @section @command{od}: Write files in octal or other formats
1507
1508 @pindex od
1509 @cindex octal dump of files
1510 @cindex hex dump of files
1511 @cindex ASCII dump of files
1512 @cindex file contents, dumping unambiguously
1513
1514 @command{od} writes an unambiguous representation of each @var{file}
1515 (@samp{-} means standard input), or standard input if none are given.
1516 Synopses:
1517
1518 @smallexample
1519 od [@var{option}]@dots{} [@var{file}]@dots{}
1520 od [-abcdfilosx]@dots{} [@var{file}] [[+]@var{offset}[.][b]]
1521 od [@var{option}]@dots{} --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1522 @end smallexample
1523
1524 Each line of output consists of the offset in the input, followed by
1525 groups of data from the file.  By default, @command{od} prints the offset in
1526 octal, and each group of file data is a C @code{short int}'s worth of input
1527 printed as a single octal number.
1528
1529 If @var{offset} is given, it specifies how many input bytes to skip
1530 before formatting and writing.  By default, it is interpreted as an
1531 octal number, but the optional trailing decimal point causes it to be
1532 interpreted as decimal.  If no decimal is specified and the offset
1533 begins with @samp{0x} or @samp{0X} it is interpreted as a hexadecimal
1534 number.  If there is a trailing @samp{b}, the number of bytes skipped
1535 will be @var{offset} multiplied by 512.
1536
1537 If a command is of both the first and second forms, the second form is
1538 assumed if the last operand begins with @samp{+} or (if there are two
1539 operands) a digit.  For example, in @samp{od foo 10} and @samp{od +10}
1540 the @samp{10} is an offset, whereas in @samp{od 10} the @samp{10} is a
1541 file name.
1542
1543 The program accepts the following options.  Also see @ref{Common options}.
1544
1545 @table @samp
1546
1547 @item -A @var{radix}
1548 @itemx --address-radix=@var{radix}
1549 @opindex -A
1550 @opindex --address-radix
1551 @cindex radix for file offsets
1552 @cindex file offset radix
1553 Select the base in which file offsets are printed.  @var{radix} can
1554 be one of the following:
1555
1556 @table @samp
1557 @item d
1558 decimal;
1559 @item o
1560 octal;
1561 @item x
1562 hexadecimal;
1563 @item n
1564 none (do not print offsets).
1565 @end table
1566
1567 The default is octal.
1568
1569 @item -j @var{bytes}
1570 @itemx --skip-bytes=@var{bytes}
1571 @opindex -j
1572 @opindex --skip-bytes
1573 Skip @var{bytes} input bytes before formatting and writing.  If
1574 @var{bytes} begins with @samp{0x} or @samp{0X}, it is interpreted in
1575 hexadecimal; otherwise, if it begins with @samp{0}, in octal; otherwise,
1576 in decimal.  Appending @samp{b} multiplies @var{bytes} by 512, @samp{k}
1577 by 1024, and @samp{m} by 1048576.
1578
1579 @item -N @var{bytes}
1580 @itemx --read-bytes=@var{bytes}
1581 @opindex -N
1582 @opindex --read-bytes
1583 Output at most @var{bytes} bytes of the input.  Prefixes and suffixes on
1584 @code{bytes} are interpreted as for the @option{-j} option.
1585
1586 @item -S @var{n}
1587 @itemx --strings[=@var{n}]
1588 @opindex -S
1589 @opindex --strings
1590 @cindex string constants, outputting
1591 Instead of the normal output, output only @dfn{string constants}: at
1592 least @var{n} consecutive @acronym{ASCII} graphic characters,
1593 followed by a null (zero) byte.
1594
1595 If @var{n} is omitted with @option{--strings}, the default is 3.
1596
1597 @item -t @var{type}
1598 @itemx --format=@var{type}
1599 @opindex -t
1600 @opindex --format
1601 Select the format in which to output the file data.  @var{type} is a
1602 string of one or more of the below type indicator characters.  If you
1603 include more than one type indicator character in a single @var{type}
1604 string, or use this option more than once, @command{od} writes one copy
1605 of each output line using each of the data types that you specified,
1606 in the order that you specified.
1607
1608 Adding a trailing ``z'' to any type specification appends a display
1609 of the @acronym{ASCII} character representation of the printable characters
1610 to the output line generated by the type specification.
1611
1612 @table @samp
1613 @item a
1614 named character, ignoring high-order bit
1615 @item c
1616 @acronym{ASCII} character or backslash escape,
1617 @item d
1618 signed decimal
1619 @item f
1620 floating point
1621 @item o
1622 octal
1623 @item u
1624 unsigned decimal
1625 @item x
1626 hexadecimal
1627 @end table
1628
1629 The type @code{a} outputs things like @samp{sp} for space, @samp{nl} for
1630 newline, and @samp{nul} for a null (zero) byte.  Only the least significant
1631 seven bits of each byte is used; the high-order bit is ignored.
1632 Type @code{c} outputs
1633 @samp{ }, @samp{\n}, and @code{\0}, respectively.
1634
1635 @cindex type size
1636 Except for types @samp{a} and @samp{c}, you can specify the number
1637 of bytes to use in interpreting each number in the given data type
1638 by following the type indicator character with a decimal integer.
1639 Alternately, you can specify the size of one of the C compiler's
1640 built-in data types by following the type indicator character with
1641 one of the following characters.  For integers (@samp{d}, @samp{o},
1642 @samp{u}, @samp{x}):
1643
1644 @table @samp
1645 @item C
1646 char
1647 @item S
1648 short
1649 @item I
1650 int
1651 @item L
1652 long
1653 @end table
1654
1655 For floating point (@code{f}):
1656
1657 @table @asis
1658 @item F
1659 float
1660 @item D
1661 double
1662 @item L
1663 long double
1664 @end table
1665
1666 @item -v
1667 @itemx --output-duplicates
1668 @opindex -v
1669 @opindex --output-duplicates
1670 Output consecutive lines that are identical.  By default, when two or
1671 more consecutive output lines would be identical, @command{od} outputs only
1672 the first line, and puts just an asterisk on the following line to
1673 indicate the elision.
1674
1675 @item -w[@var{n}]
1676 @itemx --width[=@var{n}]
1677 @opindex -w
1678 @opindex --width
1679 Dump @code{n} input bytes per output line.  This must be a multiple of
1680 the least common multiple of the sizes associated with the specified
1681 output types.
1682
1683 If this option is not given at all, the default is 16.  If @var{n} is
1684 omitted, the default is 32.
1685
1686 @end table
1687
1688 The next several options are shorthands for format specifications.
1689 @sc{gnu} @command{od} accepts any combination of shorthands and format
1690 specification options.  These options accumulate.
1691
1692 @table @samp
1693
1694 @item -a
1695 @opindex -a
1696 Output as named characters.  Equivalent to @samp{-t a}.
1697
1698 @item -b
1699 @opindex -b
1700 Output as octal bytes.  Equivalent to @samp{-t o1}.
1701
1702 @item -c
1703 @opindex -c
1704 Output as @acronym{ASCII} characters or backslash escapes.  Equivalent to
1705 @samp{-t c}.
1706
1707 @item -d
1708 @opindex -d
1709 Output as unsigned decimal two-byte units.  Equivalent to @samp{-t u2}.
1710
1711 @item -f
1712 @opindex -f
1713 Output as floats.  Equivalent to @samp{-t fF}.
1714
1715 @item -i
1716 @opindex -i
1717 Output as decimal ints.  Equivalent to @samp{-t dI}.
1718
1719 @item -l
1720 @opindex -l
1721 Output as decimal long ints.  Equivalent to @samp{-t dL}.
1722
1723 @item -o
1724 @opindex -o
1725 Output as octal two-byte units.  Equivalent to @option{-t o2}.
1726
1727 @item -s
1728 @opindex -s
1729 Output as decimal two-byte units.  Equivalent to @option{-t d2}.
1730
1731 @item -x
1732 @opindex -x
1733 Output as hexadecimal two-byte units.  Equivalent to @samp{-t x2}.
1734
1735 @item --traditional
1736 @opindex --traditional
1737 Recognize the non-option label argument that traditional @command{od}
1738 accepted.  The following syntax:
1739
1740 @smallexample
1741 od --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1742 @end smallexample
1743
1744 @noindent
1745 can be used to specify at most one file and optional arguments
1746 specifying an offset and a pseudo-start address, @var{label}.
1747 The @var{label} argument is interpreted
1748 just like @var{offset}, but it specifies an initial pseudo-address.  The
1749 pseudo-addresses are displayed in parentheses following any normal
1750 address.
1751
1752 @end table
1753
1754 @exitstatus
1755
1756 @node base64 invocation
1757 @section @command{base64}: Transform data into printable data.
1758
1759 @pindex base64
1760 @cindex base64 encoding
1761
1762 @command{base64} transforms data read from a file, or standard input,
1763 into (or from) base64 encoded form.  The base64 encoded form uses
1764 printable @acronym{ASCII} characters to represent binary data, see
1765 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3548.txt, RFC 3548}.
1766 Synopses:
1767
1768 @smallexample
1769 base64 [@var{option}]@dots{} [@var{file}]
1770 base64 --decode [@var{option}]@dots{} [@var{file}]
1771 @end smallexample
1772
1773 The base64 encoding expands data to roughly 133% of the original.
1774
1775 The program accepts the following options.  Also see @ref{Common options}.
1776
1777 @table @samp
1778
1779 @item -w @var{COLS}
1780 @itemx --wrap=@var{COLS}
1781 @opindex -w
1782 @opindex --wrap
1783 @cindex wrap data
1784 @cindex column to wrap data after
1785 During encoding, wrap lines after @var{COLS} characters.  This must be
1786 a positive number.
1787
1788 The default is to wrap after 76 characters.  Use the value 0 to
1789 disable line wrapping altogether.
1790
1791 @item -d
1792 @itemx --decode
1793 @opindex -d
1794 @opindex --decode
1795 @cindex Decode base64 data
1796 @cindex Base64 decoding
1797 Change the mode of operation, from the default of encoding data, to
1798 decoding data.  Input is expected to be base64 encoded data, and the
1799 output will be the original data.
1800
1801 @item -i
1802 @itemx --ignore-garbage
1803 @opindex -i
1804 @opindex --ignore-garbage
1805 @cindex Ignore garbage in base64 stream
1806 During decoding, ignore unrecognized characters (including newline),
1807 to permit distorted data to be decoded.
1808
1809 @end table
1810
1811 @exitstatus
1812
1813
1814 @node Formatting file contents
1815 @chapter Formatting file contents
1816
1817 @cindex formatting file contents
1818
1819 These commands reformat the contents of files.
1820
1821 @menu
1822 * fmt invocation::              Reformat paragraph text.
1823 * pr invocation::               Paginate or columnate files for printing.
1824 * fold invocation::             Wrap input lines to fit in specified width.
1825 @end menu
1826
1827
1828 @node fmt invocation
1829 @section @command{fmt}: Reformat paragraph text
1830
1831 @pindex fmt
1832 @cindex reformatting paragraph text
1833 @cindex paragraphs, reformatting
1834 @cindex text, reformatting
1835
1836 @command{fmt} fills and joins lines to produce output lines of (at most)
1837 a given number of characters (75 by default).  Synopsis:
1838
1839 @example
1840 fmt [@var{option}]@dots{} [@var{file}]@dots{}
1841 @end example
1842
1843 @command{fmt} reads from the specified @var{file} arguments (or standard
1844 input if none are given), and writes to standard output.
1845
1846 By default, blank lines, spaces between words, and indentation are
1847 preserved in the output; successive input lines with different
1848 indentation are not joined; tabs are expanded on input and introduced on
1849 output.
1850
1851 @cindex line-breaking
1852 @cindex sentences and line-breaking
1853 @cindex Knuth, Donald E.
1854 @cindex Plass, Michael F.
1855 @command{fmt} prefers breaking lines at the end of a sentence, and tries to
1856 avoid line breaks after the first word of a sentence or before the last
1857 word of a sentence.  A @dfn{sentence break} is defined as either the end
1858 of a paragraph or a word ending in any of @samp{.?!}, followed by two
1859 spaces or end of line, ignoring any intervening parentheses or quotes.
1860 Like @TeX{}, @command{fmt} reads entire ``paragraphs'' before choosing line
1861 breaks; the algorithm is a variant of that given by Donald E. Knuth
1862 and Michael F. Plass in ``Breaking Paragraphs Into Lines'',
1863 @cite{Software---Practice & Experience} @b{11}, 11 (November 1981),
1864 1119--1184.
1865
1866 The program accepts the following options.  Also see @ref{Common options}.
1867
1868 @table @samp
1869
1870 @item -c
1871 @itemx --crown-margin
1872 @opindex -c
1873 @opindex --crown-margin
1874 @cindex crown margin
1875 @dfn{Crown margin} mode: preserve the indentation of the first two
1876 lines within a paragraph, and align the left margin of each subsequent
1877 line with that of the second line.
1878
1879 @item -t
1880 @itemx --tagged-paragraph
1881 @opindex -t
1882 @opindex --tagged-paragraph
1883 @cindex tagged paragraphs
1884 @dfn{Tagged paragraph} mode: like crown margin mode, except that if
1885 indentation of the first line of a paragraph is the same as the
1886 indentation of the second, the first line is treated as a one-line
1887 paragraph.
1888
1889 @item -s
1890 @itemx --split-only
1891 @opindex -s
1892 @opindex --split-only
1893 Split lines only.  Do not join short lines to form longer ones.  This
1894 prevents sample lines of code, and other such ``formatted'' text from
1895 being unduly combined.
1896
1897 @item -u
1898 @itemx --uniform-spacing
1899 @opindex -u
1900 @opindex --uniform-spacing
1901 Uniform spacing.  Reduce spacing between words to one space, and spacing
1902 between sentences to two spaces.
1903
1904 @item -@var{width}
1905 @itemx -w @var{width}
1906 @itemx --width=@var{width}
1907 @opindex -@var{width}
1908 @opindex -w
1909 @opindex --width
1910 Fill output lines up to @var{width} characters (default 75).  @command{fmt}
1911 initially tries to make lines about 7% shorter than this, to give it
1912 room to balance line lengths.
1913
1914 @item -p @var{prefix}
1915 @itemx --prefix=@var{prefix}
1916 Only lines beginning with @var{prefix} (possibly preceded by whitespace)
1917 are subject to formatting.  The prefix and any preceding whitespace are
1918 stripped for the formatting and then re-attached to each formatted output
1919 line.  One use is to format certain kinds of program comments, while
1920 leaving the code unchanged.
1921
1922 @end table
1923
1924 @exitstatus
1925
1926
1927 @node pr invocation
1928 @section @command{pr}: Paginate or columnate files for printing
1929
1930 @pindex pr
1931 @cindex printing, preparing files for
1932 @cindex multicolumn output, generating
1933 @cindex merging files in parallel
1934
1935 @command{pr} writes each @var{file} (@samp{-} means standard input), or
1936 standard input if none are given, to standard output, paginating and
1937 optionally outputting in multicolumn format; optionally merges all
1938 @var{file}s, printing all in parallel, one per column.  Synopsis:
1939
1940 @example
1941 pr [@var{option}]@dots{} [@var{file}]@dots{}
1942 @end example
1943
1944 @vindex LC_MESSAGES
1945 By default, a 5-line header is printed at each page: two blank lines;
1946 a line with the date, the file name, and the page count; and two more
1947 blank lines.  A footer of five blank lines is also printed.
1948 With the @option{-F}
1949 option, a 3-line header is printed: the leading two blank lines are
1950 omitted; no footer is used.  The default @var{page_length} in both cases is 66
1951 lines.  The default number of text lines changes from 56 (without @option{-F})
1952 to 63 (with @option{-F}).  The text line of the header takes the form
1953 @samp{@var{date} @var{string} @var{page}}, with spaces inserted around
1954 @var{string} so that the line takes up the full @var{page_width}.  Here,
1955 @var{date} is the date (see the @option{-D} or @option{--date-format}
1956 option for details), @var{string} is the centered header string, and
1957 @var{page} identifies the page number.  The @env{LC_MESSAGES} locale
1958 category affects the spelling of @var{page}; in the default C locale, it
1959 is @samp{Page @var{number}} where @var{number} is the decimal page
1960 number.
1961
1962 Form feeds in the input cause page breaks in the output.  Multiple form
1963 feeds produce empty pages.
1964
1965 Columns are of equal width, separated by an optional string (default
1966 is @samp{space}).  For multicolumn output, lines will always be truncated to
1967 @var{page_width} (default 72), unless you use the @option{-J} option.
1968 For single
1969 column output no line truncation occurs by default.  Use @option{-W} option to
1970 truncate lines in that case.
1971
1972 The following changes were made in version 1.22i and apply to later
1973 versions of @command{pr}:
1974 @c FIXME: this whole section here sounds very awkward to me. I
1975 @c made a few small changes, but really it all needs to be redone. - Brian
1976 @c OK, I fixed another sentence or two, but some of it I just don't understand.
1977 @ - Brian
1978 @itemize @bullet
1979
1980 @item
1981 Some small @var{letter options} (@option{-s}, @option{-w}) have been
1982 redefined for better @acronym{POSIX} compliance.  The output of some further
1983 cases has been adapted to other Unix systems.  These changes are not
1984 compatible with earlier versions of the program.
1985
1986 @item
1987 Some @var{new capital letter} options (@option{-J}, @option{-S}, @option{-W})
1988 have been introduced to turn off unexpected interferences of small letter
1989 options.  The @option{-N} option and the second argument @var{last_page}
1990 of @samp{+FIRST_PAGE} offer more flexibility.  The detailed handling of
1991 form feeds set in the input files requires the @option{-T} option.
1992
1993 @item
1994 Capital letter options override small letter ones.
1995
1996 @item
1997 Some of the option-arguments (compare @option{-s}, @option{-e},
1998 @option{-i}, @option{-n}) cannot be specified as separate arguments from the
1999 preceding option letter (already stated in the @acronym{POSIX} specification).
2000 @end itemize
2001
2002 The program accepts the following options.  Also see @ref{Common options}.
2003
2004 @table @samp
2005
2006 @item +@var{first_page}[:@var{last_page}]
2007 @itemx --pages=@var{first_page}[:@var{last_page}]
2008 @c The two following @opindex lines evoke warnings because they contain `:'
2009 @c The `info' spec does not permit that.  If we use those lines, we end
2010 @c up with truncated index entries that don't work.
2011 @c @opindex +@var{first_page}[:@var{last_page}]
2012 @c @opindex --pages=@var{first_page}[:@var{last_page}]
2013 @opindex +@var{page_range}
2014 @opindex --pages=@var{page_range}
2015 Begin printing with page @var{first_page} and stop with @var{last_page}.
2016 Missing @samp{:@var{last_page}} implies end of file.  While estimating
2017 the number of skipped pages each form feed in the input file results
2018 in a new page.  Page counting with and without @samp{+@var{first_page}}
2019 is identical.  By default, counting starts with the first page of input
2020 file (not first page printed).  Line numbering may be altered by @option{-N}
2021 option.
2022
2023 @item -@var{column}
2024 @itemx --columns=@var{column}
2025 @opindex -@var{column}
2026 @opindex --columns
2027 @cindex down columns
2028 With each single @var{file}, produce @var{column} columns of output
2029 (default is 1) and print columns down, unless @option{-a} is used.  The
2030 column width is automatically decreased as @var{column} increases; unless
2031 you use the @option{-W/-w} option to increase @var{page_width} as well.
2032 This option might well cause some lines to be truncated.  The number of
2033 lines in the columns on each page are balanced.  The options @option{-e}
2034 and @option{-i} are on for multiple text-column output.  Together with
2035 @option{-J} option column alignment and line truncation is turned off.
2036 Lines of full length are joined in a free field format and @option{-S}
2037 option may set field separators.  @option{-@var{column}} may not be used
2038 with @option{-m} option.
2039
2040 @item -a
2041 @itemx --across
2042 @opindex -a
2043 @opindex --across
2044 @cindex across columns
2045 With each single @var{file}, print columns across rather than down.  The
2046 @option{-@var{column}} option must be given with @var{column} greater than one.
2047 If a line is too long to fit in a column, it is truncated.
2048
2049 @item -c
2050 @itemx --show-control-chars
2051 @opindex -c
2052 @opindex --show-control-chars
2053 Print control characters using hat notation (e.g., @samp{^G}); print
2054 other nonprinting characters in octal backslash notation.  By default,
2055 nonprinting characters are not changed.
2056
2057 @item -d
2058 @itemx --double-space
2059 @opindex -d
2060 @opindex --double-space
2061 @cindex double spacing
2062 Double space the output.
2063
2064 @item -D @var{format}
2065 @itemx --date-format=@var{format}
2066 @cindex time formats
2067 @cindex formatting times
2068 Format header dates using @var{format}, using the same conventions as
2069 for the command @samp{date +@var{format}}; @xref{date invocation}.
2070 Except for directives, which start with
2071 @samp{%}, characters in @var{format} are printed unchanged.  You can use
2072 this option to specify an arbitrary string in place of the header date,
2073 e.g., @option{--date-format="Monday morning"}.
2074
2075 @vindex POSIXLY_CORRECT
2076 @vindex LC_TIME
2077 Normally the date
2078 format defaults to @samp{%Y-%m-%d %H:%M} (for example, @samp{2001-12-04
2079 23:59}); but if the @env{POSIXLY_CORRECT} environment variable is set
2080 and the @env{LC_TIME} locale category specifies the @acronym{POSIX}
2081 locale, the default is @samp{%b %e %H:%M %Y} (for example,
2082 @samp{Dec@ @ 4 23:59 2001}.
2083
2084 @vindex TZ
2085 Time stamps are listed according to the time zone rules specified by
2086 the @env{TZ} environment variable, or by the system default rules if
2087 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
2088 with @env{TZ}, libc, The GNU C Library}.
2089
2090 @item -e[@var{in-tabchar}[@var{in-tabwidth}]]
2091 @itemx --expand-tabs[=@var{in-tabchar}[@var{in-tabwidth}]]
2092 @opindex -e
2093 @opindex --expand-tabs
2094 @cindex input tabs
2095 Expand @var{tab}s to spaces on input.  Optional argument @var{in-tabchar} is
2096 the input tab character (default is the TAB character).  Second optional
2097 argument @var{in-tabwidth} is the input tab character's width (default
2098 is 8).
2099
2100 @item -f
2101 @itemx -F
2102 @itemx --form-feed
2103 @opindex -F
2104 @opindex -f
2105 @opindex --form-feed
2106 Use a form feed instead of newlines to separate output pages.  The default
2107 page length of 66 lines is not altered.  But the number of lines of text
2108 per page changes from default 56 to 63 lines.
2109
2110 @item -h @var{HEADER}
2111 @itemx --header=@var{HEADER}
2112 @opindex -h
2113 @opindex --header
2114 Replace the file name in the header with the centered string @var{header}.
2115 When using the shell, @var{header} should be quoted and should be
2116 separated from @option{-h} by a space.
2117
2118 @item -i[@var{out-tabchar}[@var{out-tabwidth}]]
2119 @itemx --output-tabs[=@var{out-tabchar}[@var{out-tabwidth}]]
2120 @opindex -i
2121 @opindex --output-tabs
2122 @cindex output tabs
2123 Replace spaces with @var{tab}s on output.  Optional argument @var{out-tabchar}
2124 is the output tab character (default is the TAB character).  Second optional
2125 argument @var{out-tabwidth} is the output tab character's width (default
2126 is 8).
2127
2128 @item -J
2129 @itemx --join-lines
2130 @opindex -J
2131 @opindex --join-lines
2132 Merge lines of full length.  Used together with the column options
2133 @option{-@var{column}}, @option{-a -@var{column}} or @option{-m}.  Turns off
2134 @option{-W/-w} line truncation;
2135 no column alignment used; may be used with
2136 @option{--sep-string[=@var{string}]}.  @option{-J} has been introduced
2137 (together with @option{-W} and @option{--sep-string})
2138 to disentangle the old (@acronym{POSIX}-compliant) options @option{-w} and
2139 @option{-s} along with the three column options.
2140
2141
2142 @item -l @var{page_length}
2143 @itemx --length=@var{page_length}
2144 @opindex -l
2145 @opindex --length
2146 Set the page length to @var{page_length} (default 66) lines, including
2147 the lines of the header [and the footer].  If @var{page_length} is less
2148 than or equal to 10 (or <= 3 with @option{-F}), the header and footer are
2149 omitted, and all form feeds set in input files are eliminated, as if
2150 the @option{-T} option had been given.
2151
2152 @item -m
2153 @itemx --merge
2154 @opindex -m
2155 @opindex --merge
2156 Merge and print all @var{file}s in parallel, one in each column.  If a
2157 line is too long to fit in a column, it is truncated, unless the @option{-J}
2158 option is used.  @option{--sep-string[=@var{string}]} may be used.
2159 Empty pages in
2160 some @var{file}s (form feeds set) produce empty columns, still marked
2161 by @var{string}.  The result is a continuous line numbering and column
2162 marking throughout the whole merged file.  Completely empty merged pages
2163 show no separators or line numbers.  The default header becomes
2164 @samp{@var{date} @var{page}} with spaces inserted in the middle; this
2165 may be used with the @option{-h} or @option{--header} option to fill up
2166 the middle blank part.
2167
2168 @item -n[@var{number-separator}[@var{digits}]]
2169 @itemx --number-lines[=@var{number-separator}[@var{digits}]]
2170 @opindex -n
2171 @opindex --number-lines
2172 Provide @var{digits} digit line numbering (default for @var{digits} is
2173 5).  With multicolumn output the number occupies the first @var{digits}
2174 column positions of each text column or only each line of @option{-m}
2175 output.  With single column output the number precedes each line just as
2176 @option{-m} does.  Default counting of the line numbers starts with the
2177 first line of the input file (not the first line printed, compare the
2178 @option{--page} option and @option{-N} option).
2179 Optional argument @var{number-separator} is the character appended to
2180 the line number to separate it from the text followed.  The default
2181 separator is the TAB character.  In a strict sense a TAB is always
2182 printed with single column output only.  The @var{TAB}-width varies
2183 with the @var{TAB}-position, e.g., with the left @var{margin} specified
2184 by @option{-o} option.  With multicolumn output priority is given to
2185 @samp{equal width of output columns} (a @acronym{POSIX} specification).
2186 The @var{TAB}-width is fixed to the value of the first column and does
2187 not change with different values of left @var{margin}.  That means a
2188 fixed number of spaces is always printed in the place of the
2189 @var{number-separator tab}.  The tabification depends upon the output
2190 position.
2191
2192 @item -N @var{line_number}
2193 @itemx --first-line-number=@var{line_number}
2194 @opindex -N
2195 @opindex --first-line-number
2196 Start line counting with the number @var{line_number} at first line of
2197 first page printed (in most cases not the first line of the input file).
2198
2199 @item -o @var{margin}
2200 @itemx --indent=@var{margin}
2201 @opindex -o
2202 @opindex --indent
2203 @cindex indenting lines
2204 @cindex left margin
2205 Indent each line with a margin @var{margin} spaces wide (default is zero).
2206 The total page width is the size of the margin plus the @var{page_width}
2207 set with the @option{-W/-w} option.  A limited overflow may occur with
2208 numbered single column output (compare @option{-n} option).
2209
2210 @item -r
2211 @itemx --no-file-warnings
2212 @opindex -r
2213 @opindex --no-file-warnings
2214 Do not print a warning message when an argument @var{file} cannot be
2215 opened.  (The exit status will still be nonzero, however.)
2216
2217 @item -s[@var{char}]
2218 @itemx --separator[=@var{char}]
2219 @opindex -s
2220 @opindex --separator
2221 Separate columns by a single character @var{char}.  The default for
2222 @var{char} is the TAB character without @option{-w} and @samp{no
2223 character} with @option{-w}.  Without @option{-s} the default separator
2224 @samp{space} is set.  @option{-s[char]} turns off line truncation of all
2225 three column options (@option{-COLUMN}|@option{-a -COLUMN}|@option{-m}) unless
2226 @option{-w} is set.  This is a @acronym{POSIX}-compliant formulation.
2227
2228
2229 @item -S@var{string}
2230 @itemx --sep-string[=@var{string}]
2231 @opindex -S
2232 @opindex --sep-string
2233 Use @var{string} to separate output columns.  The @option{-S} option doesn't
2234 affect the @option{-W/-w} option, unlike the @option{-s} option which does.  It
2235 does not affect line truncation or column alignment.
2236 Without @option{-S}, and with @option{-J}, @command{pr} uses the default output
2237 separator, TAB@.
2238 Without @option{-S} or @option{-J}, @command{pr} uses a @samp{space}
2239 (same as @option{-S"@w{ }"}).  @option{--sep-string} with no
2240 @samp{=@var{string}} is equivalent to @option{--sep-string=""}.
2241
2242 @item -t
2243 @itemx --omit-header
2244 @opindex -t
2245 @opindex --omit-header
2246 Do not print the usual header [and footer] on each page, and do not fill
2247 out the bottom of pages (with blank lines or a form feed).  No page
2248 structure is produced, but form feeds set in the input files are retained.
2249 The predefined pagination is not changed.  @option{-t} or @option{-T} may be
2250 useful together with other options; e.g.: @option{-t -e4}, expand TAB characters
2251 in the input file to 4 spaces but don't make any other changes.  Use of
2252 @option{-t} overrides @option{-h}.
2253
2254 @item -T
2255 @itemx --omit-pagination
2256 @opindex -T
2257 @opindex --omit-pagination
2258 Do not print header [and footer].  In addition eliminate all form feeds
2259 set in the input files.
2260
2261 @item -v
2262 @itemx --show-nonprinting
2263 @opindex -v
2264 @opindex --show-nonprinting
2265 Print nonprinting characters in octal backslash notation.
2266
2267 @item -w @var{page_width}
2268 @itemx --width=@var{page_width}
2269 @opindex -w
2270 @opindex --width
2271 Set page width to @var{page_width} characters for multiple text-column
2272 output only (default for @var{page_width} is 72).  @option{-s[CHAR]} turns
2273 off the default page width and any line truncation and column alignment.
2274 Lines of full length are merged, regardless of the column options
2275 set.  No @var{page_width} setting is possible with single column output.
2276 A @acronym{POSIX}-compliant formulation.
2277
2278 @item -W @var{page_width}
2279 @itemx --page_width=@var{page_width}
2280 @opindex -W
2281 @opindex --page_width
2282 Set the page width to @var{page_width} characters.  That's valid with and
2283 without a column option.  Text lines are truncated, unless @option{-J}
2284 is used.  Together with one of the three column options
2285 (@option{-@var{column}}, @option{-a -@var{column}} or @option{-m}) column
2286 alignment is always used.  The separator options @option{-S} or @option{-s}
2287 don't affect the @option{-W} option.  Default is 72 characters.  Without
2288 @option{-W @var{page_width}} and without any of the column options NO line
2289 truncation is used (defined to keep downward compatibility and to meet
2290 most frequent tasks).  That's equivalent to @option{-W 72 -J}.  The header
2291 line is never truncated.
2292
2293 @end table
2294
2295 @exitstatus
2296
2297
2298 @node fold invocation
2299 @section @command{fold}: Wrap input lines to fit in specified width
2300
2301 @pindex fold
2302 @cindex wrapping long input lines
2303 @cindex folding long input lines
2304
2305 @command{fold} writes each @var{file} (@option{-} means standard input), or
2306 standard input if none are given, to standard output, breaking long
2307 lines.  Synopsis:
2308
2309 @example
2310 fold [@var{option}]@dots{} [@var{file}]@dots{}
2311 @end example
2312
2313 By default, @command{fold} breaks lines wider than 80 columns.  The output
2314 is split into as many lines as necessary.
2315
2316 @cindex screen columns
2317 @command{fold} counts screen columns by default; thus, a tab may count more
2318 than one column, backspace decreases the column count, and carriage
2319 return sets the column to zero.
2320
2321 The program accepts the following options.  Also see @ref{Common options}.
2322
2323 @table @samp
2324
2325 @item -b
2326 @itemx --bytes
2327 @opindex -b
2328 @opindex --bytes
2329 Count bytes rather than columns, so that tabs, backspaces, and carriage
2330 returns are each counted as taking up one column, just like other
2331 characters.
2332
2333 @item -s
2334 @itemx --spaces
2335 @opindex -s
2336 @opindex --spaces
2337 Break at word boundaries: the line is broken after the last blank before
2338 the maximum line length.  If the line contains no such blanks, the line
2339 is broken at the maximum line length as usual.
2340
2341 @item -w @var{width}
2342 @itemx --width=@var{width}
2343 @opindex -w
2344 @opindex --width
2345 Use a maximum line length of @var{width} columns instead of 80.
2346
2347 For compatibility @command{fold} supports an obsolete option syntax
2348 @option{-@var{width}}.  New scripts should use @option{-w @var{width}}
2349 instead.
2350
2351 @end table
2352
2353 @exitstatus
2354
2355
2356 @node Output of parts of files
2357 @chapter Output of parts of files
2358
2359 @cindex output of parts of files
2360 @cindex parts of files, output of
2361
2362 These commands output pieces of the input.
2363
2364 @menu
2365 * head invocation::             Output the first part of files.
2366 * tail invocation::             Output the last part of files.
2367 * split invocation::            Split a file into fixed-size pieces.
2368 * csplit invocation::           Split a file into context-determined pieces.
2369 @end menu
2370
2371 @node head invocation
2372 @section @command{head}: Output the first part of files
2373
2374 @pindex head
2375 @cindex initial part of files, outputting
2376 @cindex first part of files, outputting
2377
2378 @command{head} prints the first part (10 lines by default) of each
2379 @var{file}; it reads from standard input if no files are given or
2380 when given a @var{file} of @option{-}.  Synopsis:
2381
2382 @example
2383 head [@var{option}]@dots{} [@var{file}]@dots{}
2384 @end example
2385
2386 If more than one @var{file} is specified, @command{head} prints a
2387 one-line header consisting of:
2388
2389 @example
2390 ==> @var{file name} <==
2391 @end example
2392
2393 @noindent
2394 before the output for each @var{file}.
2395
2396 The program accepts the following options.  Also see @ref{Common options}.
2397
2398 @table @samp
2399
2400 @item -c @var{n}
2401 @itemx --bytes=@var{n}
2402 @opindex -c
2403 @opindex --bytes
2404 Print the first @var{n} bytes, instead of initial lines.  Appending
2405 @samp{b} multiplies @var{n} by 512, @samp{k} by 1024, and @samp{m}
2406 by 1048576.
2407 However, if @var{n} starts with a @samp{-},
2408 print all but the last @var{n} bytes of each file.
2409
2410 @itemx -n @var{n}
2411 @itemx --lines=@var{n}
2412 @opindex -n
2413 @opindex --lines
2414 Output the first @var{n} lines.
2415 However, if @var{n} starts with a @samp{-},
2416 print all but the last @var{n} lines of each file.
2417
2418 @item -q
2419 @itemx --quiet
2420 @itemx --silent
2421 @opindex -q
2422 @opindex --quiet
2423 @opindex --silent
2424 Never print file name headers.
2425
2426 @item -v
2427 @itemx --verbose
2428 @opindex -v
2429 @opindex --verbose
2430 Always print file name headers.
2431
2432 @end table
2433
2434 For compatibility @command{head} also supports an obsolete option syntax
2435 @option{-@var{count}@var{options}}, which is recognized only if it is
2436 specified first.  @var{count} is a decimal number optionally followed
2437 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
2438 @samp{l} to mean count by lines, or other option letters (@samp{cqv}).
2439 Scripts intended for standard hosts should use @option{-c @var{count}}
2440 or @option{-n @var{count}} instead.  If your script must also run on
2441 hosts that support only the obsolete syntax, it is usually simpler to
2442 avoid @command{head}, e.g., by using @samp{sed 5q} instead of
2443 @samp{head -5}.
2444
2445 @exitstatus
2446
2447
2448 @node tail invocation
2449 @section @command{tail}: Output the last part of files
2450
2451 @pindex tail
2452 @cindex last part of files, outputting
2453
2454 @command{tail} prints the last part (10 lines by default) of each
2455 @var{file}; it reads from standard input if no files are given or
2456 when given a @var{file} of @samp{-}.  Synopsis:
2457
2458 @example
2459 tail [@var{option}]@dots{} [@var{file}]@dots{}
2460 @end example
2461
2462 If more than one @var{file} is specified, @command{tail} prints a
2463 one-line header consisting of:
2464
2465 @example
2466 ==> @var{file name} <==
2467 @end example
2468
2469 @noindent
2470 before the output for each @var{file}.
2471
2472 @cindex BSD @command{tail}
2473 @sc{gnu} @command{tail} can output any amount of data (some other versions of
2474 @command{tail} cannot).  It also has no @option{-r} option (print in
2475 reverse), since reversing a file is really a different job from printing
2476 the end of a file; BSD @command{tail} (which is the one with @option{-r}) can
2477 only reverse files that are at most as large as its buffer, which is
2478 typically 32 KiB@.  A more reliable and versatile way to reverse files is
2479 the @sc{gnu} @command{tac} command.
2480
2481 If any option-argument is a number @var{n} starting with a @samp{+},
2482 @command{tail} begins printing with the @var{n}th item from the start of
2483 each file, instead of from the end.
2484
2485 The program accepts the following options.  Also see @ref{Common options}.
2486
2487 @table @samp
2488
2489 @item -c @var{bytes}
2490 @itemx --bytes=@var{bytes}
2491 @opindex -c
2492 @opindex --bytes
2493 Output the last @var{bytes} bytes, instead of final lines.  Appending
2494 @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and @samp{m}
2495 by 1048576.
2496
2497 @item -f
2498 @itemx --follow[=@var{how}]
2499 @opindex -f
2500 @opindex --follow
2501 @cindex growing files
2502 @vindex name @r{follow option}
2503 @vindex descriptor @r{follow option}
2504 Loop forever trying to read more characters at the end of the file,
2505 presumably because the file is growing.
2506 If more than one file is given, @command{tail} prints a header whenever it
2507 gets output from a different file, to indicate which file that output is
2508 from.
2509
2510 There are two ways to specify how you'd like to track files with this option,
2511 but that difference is noticeable only when a followed file is removed or
2512 renamed.
2513 If you'd like to continue to track the end of a growing file even after
2514 it has been unlinked, use @option{--follow=descriptor}.  This is the default
2515 behavior, but it is not useful if you're tracking a log file that may be
2516 rotated (removed or renamed, then reopened).  In that case, use
2517 @option{--follow=name} to track the named file by reopening it periodically
2518 to see if it has been removed and recreated by some other program.
2519
2520 No matter which method you use, if the tracked file is determined to have
2521 shrunk, @command{tail} prints a message saying the file has been truncated
2522 and resumes tracking the end of the file from the newly-determined endpoint.
2523
2524 When a file is removed, @command{tail}'s behavior depends on whether it is
2525 following the name or the descriptor.  When following by name, tail can
2526 detect that a file has been removed and gives a message to that effect,
2527 and if @option{--retry} has been specified it will continue checking
2528 periodically to see if the file reappears.
2529 When following a descriptor, tail does not detect that the file has
2530 been unlinked or renamed and issues no message;  even though the file
2531 may no longer be accessible via its original name, it may still be
2532 growing.
2533
2534 The option values @samp{descriptor} and @samp{name} may be specified only
2535 with the long form of the option, not with @option{-f}.
2536
2537 @vindex POSIXLY_CORRECT
2538 If @env{POSIXLY_CORRECT} is set, the @option{-f} option is ignored if
2539 no @var{file} operand is specified and standard input is a pipe.
2540
2541 @item -F
2542 @opindex -F
2543 This option is the same as @option{--follow=name --retry}.  That is, tail
2544 will attempt to reopen a file when it is removed.  Should this fail, tail
2545 will keep trying until it becomes accessible again.
2546
2547 @itemx --retry
2548 @opindex --retry
2549 This option is useful mainly when following by name (i.e., with
2550 @option{--follow=name}).
2551 Without this option, when tail encounters a file that doesn't
2552 exist or is otherwise inaccessible, it reports that fact and
2553 never checks it again.
2554
2555 @itemx --sleep-interval=@var{number}
2556 @opindex --sleep-interval
2557 Change the number of seconds to wait between iterations (the default is 1.0).
2558 During one iteration, every specified file is checked to see if it has
2559 changed size.
2560 Historical implementations of @command{tail} have required that
2561 @var{number} be an integer.  However, GNU @command{tail} accepts
2562 an arbitrary floating point number (using a period before any
2563 fractional digits).
2564
2565 @itemx --pid=@var{pid}
2566 @opindex --pid
2567 When following by name or by descriptor, you may specify the process ID,
2568 @var{pid}, of the sole writer of all @var{file} arguments.  Then, shortly
2569 after that process terminates, tail will also terminate.  This will
2570 work properly only if the writer and the tailing process are running on
2571 the same machine.  For example, to save the output of a build in a file
2572 and to watch the file grow, if you invoke @command{make} and @command{tail}
2573 like this then the tail process will stop when your build completes.
2574 Without this option, you would have had to kill the @code{tail -f}
2575 process yourself.
2576
2577 @example
2578 $ make >& makerr & tail --pid=$! -f makerr
2579 @end example
2580
2581 If you specify a @var{pid} that is not in use or that does not correspond
2582 to the process that is writing to the tailed files, then @command{tail}
2583 may terminate long before any @var{file}s stop growing or it may not
2584 terminate until long after the real writer has terminated.
2585 Note that @option{--pid} cannot be supported on some systems; @command{tail}
2586 will print a warning if this is the case.
2587
2588 @itemx --max-unchanged-stats=@var{n}
2589 @opindex --max-unchanged-stats
2590 When tailing a file by name, if there have been @var{n} (default
2591 n=@value{DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS}) consecutive
2592 iterations for which the file has not changed, then
2593 @code{open}/@code{fstat} the file to determine if that file name is
2594 still associated with the same device/inode-number pair as before.
2595 When following a log file that is rotated, this is approximately the
2596 number of seconds between when tail prints the last pre-rotation lines
2597 and when it prints the lines that have accumulated in the new log file.
2598 This option is meaningful only when following by name.
2599
2600 @itemx -n @var{n}
2601 @itemx --lines=@var{n}
2602 @opindex -n
2603 @opindex --lines
2604 Output the last @var{n} lines.
2605
2606 @item -q
2607 @itemx --quiet
2608 @itemx --silent
2609 @opindex -q
2610 @opindex --quiet
2611 @opindex --silent
2612 Never print file name headers.
2613
2614 @item -v
2615 @itemx --verbose
2616 @opindex -v
2617 @opindex --verbose
2618 Always print file name headers.
2619
2620 @end table
2621
2622 For compatibility @command{tail} also supports an obsolete usage
2623 @samp{tail -[@var{count}][bcl][f] [@var{file}]}, which is recognized
2624 only if it does not conflict with the usage described
2625 above.  This obsolete form uses exactly one option and at most one
2626 file.  In the option, @var{count} is an optional decimal number optionally
2627 followed by a size letter (@samp{b}, @samp{c}, @samp{l}) to mean count
2628 by 512-byte blocks, bytes, or lines, optionally followed by @samp{f}
2629 which has the same meaning as @option{-f}.
2630
2631 @vindex _POSIX2_VERSION
2632 On older systems, the leading @samp{-} can be replaced by @samp{+} in
2633 the obsolete option syntax with the same meaning as in counts, and
2634 obsolete usage overrides normal usage when the two conflict.
2635 This obsolete behavior can be enabled or disabled with the
2636 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
2637 conformance}).
2638
2639 Scripts intended for use on standard hosts should avoid obsolete
2640 syntax and should use @option{-c @var{count}[b]}, @option{-n
2641 @var{count}}, and/or @option{-f} instead.  If your script must also
2642 run on hosts that support only the obsolete syntax, you can often
2643 rewrite it to avoid problematic usages, e.g., by using @samp{sed -n
2644 '$p'} rather than @samp{tail -1}.  If that's not possible, the script
2645 can use a test like @samp{if tail -c +1 </dev/null >/dev/null 2>&1;
2646 then @dots{}} to decide which syntax to use.
2647
2648 Even if your script assumes the standard behavior, you should still
2649 beware usages whose behaviors differ depending on the @acronym{POSIX}
2650 version.  For example, avoid @samp{tail - main.c}, since it might be
2651 interpreted as either @samp{tail main.c} or as @samp{tail -- -
2652 main.c}; avoid @samp{tail -c 4}, since it might mean either @samp{tail
2653 -c4} or @samp{tail -c 10 4}; and avoid @samp{tail +4}, since it might
2654 mean either @samp{tail ./+4} or @samp{tail -n +4}.
2655
2656 @exitstatus
2657
2658
2659 @node split invocation
2660 @section @command{split}: Split a file into fixed-size pieces
2661
2662 @pindex split
2663 @cindex splitting a file into pieces
2664 @cindex pieces, splitting a file into
2665
2666 @command{split} creates output files containing consecutive sections of
2667 @var{input} (standard input if none is given or @var{input} is
2668 @samp{-}).  Synopsis:
2669
2670 @example
2671 split [@var{option}] [@var{input} [@var{prefix}]]
2672 @end example
2673
2674 By default, @command{split} puts 1000 lines of @var{input} (or whatever is
2675 left over for the last section), into each output file.
2676
2677 @cindex output file name prefix
2678 The output files' names consist of @var{prefix} (@samp{x} by default)
2679 followed by a group of characters (@samp{aa}, @samp{ab}, @dots{} by
2680 default), such that concatenating the output files in traditional
2681 sorted order by file name produces
2682 the original input file.  If the output file names are exhausted,
2683 @command{split} reports an error without deleting the output files
2684 that it did create.
2685
2686 The program accepts the following options.  Also see @ref{Common options}.
2687
2688 @table @samp
2689
2690 @item -a @var{length}
2691 @itemx --suffix-length=@var{length}
2692 @opindex -a
2693 @opindex --suffix-length
2694 Use suffixes of length @var{length}.  The default @var{length} is 2.
2695
2696 @item -l @var{lines}
2697 @itemx --lines=@var{lines}
2698 @opindex -l
2699 @opindex --lines
2700 Put @var{lines} lines of @var{input} into each output file.
2701
2702 For compatibility @command{split} also supports an obsolete
2703 option syntax @option{-@var{lines}}.  New scripts should use @option{-l
2704 @var{lines}} instead.
2705
2706 @item -b @var{bytes}
2707 @itemx --bytes=@var{bytes}
2708 @opindex -b
2709 @opindex --bytes
2710 Put the first @var{bytes} bytes of @var{input} into each output file.
2711 Appending @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and
2712 @samp{m} by 1048576.
2713
2714 @item -C @var{bytes}
2715 @itemx --line-bytes=@var{bytes}
2716 @opindex -C
2717 @opindex --line-bytes
2718 Put into each output file as many complete lines of @var{input} as
2719 possible without exceeding @var{bytes} bytes.  For lines longer than
2720 @var{bytes} bytes, put @var{bytes} bytes into each output file until
2721 less than @var{bytes} bytes of the line are left, then continue
2722 normally.  @var{bytes} has the same format as for the @option{--bytes}
2723 option.
2724
2725 @item -d
2726 @itemx --numeric-suffixes
2727 @opindex -d
2728 @opindex --numeric-suffixes
2729 Use digits in suffixes rather than lower-case letters.
2730
2731 @itemx --verbose
2732 @opindex --verbose
2733 Write a diagnostic to standard error just before each output file is opened.
2734
2735 @end table
2736
2737 @exitstatus
2738
2739
2740 @node csplit invocation
2741 @section @command{csplit}: Split a file into context-determined pieces
2742
2743 @pindex csplit
2744 @cindex context splitting
2745 @cindex splitting a file into pieces by context
2746
2747 @command{csplit} creates zero or more output files containing sections of
2748 @var{input} (standard input if @var{input} is @samp{-}).  Synopsis:
2749
2750 @example
2751 csplit [@var{option}]@dots{} @var{input} @var{pattern}@dots{}
2752 @end example
2753
2754 The contents of the output files are determined by the @var{pattern}
2755 arguments, as detailed below.  An error occurs if a @var{pattern}
2756 argument refers to a nonexistent line of the input file (e.g., if no
2757 remaining line matches a given regular expression).  After every
2758 @var{pattern} has been matched, any remaining input is copied into one
2759 last output file.
2760
2761 By default, @command{csplit} prints the number of bytes written to each
2762 output file after it has been created.
2763
2764 The types of pattern arguments are:
2765
2766 @table @samp
2767
2768 @item @var{n}
2769 Create an output file containing the input up to but not including line
2770 @var{n} (a positive integer).  If followed by a repeat count, also
2771 create an output file containing the next @var{n} lines of the input
2772 file once for each repeat.
2773
2774 @item /@var{regexp}/[@var{offset}]
2775 Create an output file containing the current line up to (but not
2776 including) the next line of the input file that contains a match for
2777 @var{regexp}.  The optional @var{offset} is an integer.
2778 If it is given, the input up to (but not including) the
2779 matching line plus or minus @var{offset} is put into the output file,
2780 and the line after that begins the next section of input.
2781
2782 @item %@var{regexp}%[@var{offset}]
2783 Like the previous type, except that it does not create an output
2784 file, so that section of the input file is effectively ignored.
2785
2786 @item @{@var{repeat-count}@}
2787 Repeat the previous pattern @var{repeat-count} additional
2788 times.  The @var{repeat-count} can either be a positive integer or an
2789 asterisk, meaning repeat as many times as necessary until the input is
2790 exhausted.
2791
2792 @end table
2793
2794 The output files' names consist of a prefix (@samp{xx} by default)
2795 followed by a suffix.  By default, the suffix is an ascending sequence
2796 of two-digit decimal numbers from @samp{00} to @samp{99}.  In any case,
2797 concatenating the output files in sorted order by file name produces the
2798 original input file.
2799
2800 By default, if @command{csplit} encounters an error or receives a hangup,
2801 interrupt, quit, or terminate signal, it removes any output files
2802 that it has created so far before it exits.
2803
2804 The program accepts the following options.  Also see @ref{Common options}.
2805
2806 @table @samp
2807
2808 @item -f @var{prefix}
2809 @itemx --prefix=@var{prefix}
2810 @opindex -f
2811 @opindex --prefix
2812 @cindex output file name prefix
2813 Use @var{prefix} as the output file name prefix.
2814
2815 @item -b @var{suffix}
2816 @itemx --suffix=@var{suffix}
2817 @opindex -b
2818 @opindex --suffix
2819 @cindex output file name suffix
2820 Use @var{suffix} as the output file name suffix.  When this option is
2821 specified, the suffix string must include exactly one
2822 @code{printf(3)}-style conversion specification, possibly including
2823 format specification flags, a field width, a precision specifications,
2824 or all of these kinds of modifiers.  The format letter must convert a
2825 binary integer argument to readable form; thus, only @samp{d}, @samp{i},
2826 @samp{u}, @samp{o}, @samp{x}, and @samp{X} conversions are allowed.  The
2827 entire @var{suffix} is given (with the current output file number) to
2828 @code{sprintf(3)} to form the file name suffixes for each of the
2829 individual output files in turn.  If this option is used, the
2830 @option{--digits} option is ignored.
2831
2832 @item -n @var{digits}
2833 @itemx --digits=@var{digits}
2834 @opindex -n
2835 @opindex --digits
2836 Use output file names containing numbers that are @var{digits} digits
2837 long instead of the default 2.
2838
2839 @item -k
2840 @itemx --keep-files
2841 @opindex -k
2842 @opindex --keep-files
2843 Do not remove output files when errors are encountered.
2844
2845 @item -z
2846 @itemx --elide-empty-files
2847 @opindex -z
2848 @opindex --elide-empty-files
2849 Suppress the generation of zero-length output files.  (In cases where
2850 the section delimiters of the input file are supposed to mark the first
2851 lines of each of the sections, the first output file will generally be a
2852 zero-length file unless you use this option.)  The output file sequence
2853 numbers always run consecutively starting from 0, even when this option
2854 is specified.
2855
2856 @item -s
2857 @itemx -q
2858 @itemx --silent
2859 @itemx --quiet
2860 @opindex -s
2861 @opindex -q
2862 @opindex --silent
2863 @opindex --quiet
2864 Do not print counts of output file sizes.
2865
2866 @end table
2867
2868 @exitstatus
2869
2870
2871 @node Summarizing files
2872 @chapter Summarizing files
2873
2874 @cindex summarizing files
2875
2876 These commands generate just a few numbers representing entire
2877 contents of files.
2878
2879 @menu
2880 * wc invocation::               Print newline, word, and byte counts.
2881 * sum invocation::              Print checksum and block counts.
2882 * cksum invocation::            Print CRC checksum and byte counts.
2883 * md5sum invocation::           Print or check MD5 digests.
2884 * sha1sum invocation::          Print or check SHA-1 digests.
2885 * sha2 utilities::              Print or check SHA-2 digests.
2886 @end menu
2887
2888
2889 @node wc invocation
2890 @section @command{wc}: Print newline, word, and byte counts
2891
2892 @pindex wc
2893 @cindex byte count
2894 @cindex character count
2895 @cindex word count
2896 @cindex line count
2897
2898 @command{wc} counts the number of bytes, characters, whitespace-separated
2899 words, and newlines in each given @var{file}, or standard input if none
2900 are given or for a @var{file} of @samp{-}.  Synopsis:
2901
2902 @example
2903 wc [@var{option}]@dots{} [@var{file}]@dots{}
2904 @end example
2905
2906 @cindex total counts
2907 @command{wc} prints one line of counts for each file, and if the file was
2908 given as an argument, it prints the file name following the counts.  If
2909 more than one @var{file} is given, @command{wc} prints a final line
2910 containing the cumulative counts, with the file name @file{total}.  The
2911 counts are printed in this order: newlines, words, characters, bytes.
2912 Each count is printed right-justified in a field with at least one
2913 space between fields so that the numbers and file names normally line
2914 up nicely in columns.  The width of the count fields varies depending
2915 on the inputs, so you should not depend on a particular field width.
2916 However, as a @acronym{GNU} extension, if only one count is printed,
2917 it is guaranteed to be printed without leading spaces.
2918
2919 By default, @command{wc} prints three counts: the newline, words, and byte
2920 counts.  Options can specify that only certain counts be printed.
2921 Options do not undo others previously given, so
2922
2923 @example
2924 wc --bytes --words
2925 @end example
2926
2927 @noindent
2928 prints both the byte counts and the word counts.
2929
2930 With the @option{--max-line-length} option, @command{wc} prints the length
2931 of the longest line per file, and if there is more than one file it
2932 prints the maximum (not the sum) of those lengths.
2933
2934 The program accepts the following options.  Also see @ref{Common options}.
2935
2936 @table @samp
2937
2938 @item -c
2939 @itemx --bytes
2940 @opindex -c
2941 @opindex --bytes
2942 Print only the byte counts.
2943
2944 @item -m
2945 @itemx --chars
2946 @opindex -m
2947 @opindex --chars
2948 Print only the character counts.
2949
2950 @item -w
2951 @itemx --words
2952 @opindex -w
2953 @opindex --words
2954 Print only the word counts.
2955
2956 @item -l
2957 @itemx --lines
2958 @opindex -l
2959 @opindex --lines
2960 Print only the newline counts.
2961
2962 @item -L
2963 @itemx --max-line-length
2964 @opindex -L
2965 @opindex --max-line-length
2966 Print only the maximum line lengths.
2967
2968 @itemx --files0-from=@var{FILE}
2969 @opindex --files0-from=@var{FILE}
2970 @cindex including files from @command{du}
2971 Rather than processing files named on the command line, process those
2972 named in file @var{FILE}; each name is terminated by a null byte.
2973 This is useful when
2974 the list of file names is so long that it may exceed a command line
2975 length limitation.
2976 In such cases, running @command{wc} via @command{xargs} is undesirable
2977 because it splits the list into pieces and makes @command{wc} print a
2978 total for each sublist rather than for the entire list.
2979 One way to produce a list of null-byte-terminated file names is with @sc{gnu}
2980 @command{find}, using its @option{-print0} predicate.  For example, to find
2981 the length of the longest line in any @file{.c} or @file{.h} file in the
2982 current hierarchy, do this:
2983
2984 @example
2985 find . -name '*.[ch]' -print0 | wc -L --files0-from=- | tail -n1
2986 @end example
2987
2988 Do not specify any @var{FILE} on the command line when using this option.
2989
2990 @end table
2991
2992 @exitstatus
2993
2994
2995 @node sum invocation
2996 @section @command{sum}: Print checksum and block counts
2997
2998 @pindex sum
2999 @cindex 16-bit checksum
3000 @cindex checksum, 16-bit
3001
3002 @command{sum} computes a 16-bit checksum for each given @var{file}, or
3003 standard input if none are given or for a @var{file} of @samp{-}.  Synopsis:
3004
3005 @example
3006 sum [@var{option}]@dots{} [@var{file}]@dots{}
3007 @end example
3008
3009 @command{sum} prints the checksum for each @var{file} followed by the
3010 number of blocks in the file (rounded up).  If more than one @var{file}
3011 is given, file names are also printed (by default).  (With the
3012 @option{--sysv} option, corresponding file names are printed when there is
3013 at least one file argument.)
3014
3015 By default, @sc{gnu} @command{sum} computes checksums using an algorithm
3016 compatible with BSD @command{sum} and prints file sizes in units of
3017 1024-byte blocks.
3018
3019 The program accepts the following options.  Also see @ref{Common options}.
3020
3021 @table @samp
3022
3023 @item -r
3024 @opindex -r
3025 @cindex BSD @command{sum}
3026 Use the default (BSD compatible) algorithm.  This option is included for
3027 compatibility with the System V @command{sum}.  Unless @option{-s} was also
3028 given, it has no effect.
3029
3030 @item -s
3031 @itemx --sysv
3032 @opindex -s
3033 @opindex --sysv
3034 @cindex System V @command{sum}
3035 Compute checksums using an algorithm compatible with System V
3036 @command{sum}'s default, and print file sizes in units of 512-byte blocks.
3037
3038 @end table
3039
3040 @command{sum} is provided for compatibility; the @command{cksum} program (see
3041 next section) is preferable in new applications.
3042
3043 @exitstatus
3044
3045
3046 @node cksum invocation
3047 @section @command{cksum}: Print CRC checksum and byte counts
3048
3049 @pindex cksum
3050 @cindex cyclic redundancy check
3051 @cindex CRC checksum
3052
3053 @command{cksum} computes a cyclic redundancy check (CRC) checksum for each
3054 given @var{file}, or standard input if none are given or for a
3055 @var{file} of @samp{-}.  Synopsis:
3056
3057 @example
3058 cksum [@var{option}]@dots{} [@var{file}]@dots{}
3059 @end example
3060
3061 @command{cksum} prints the CRC checksum for each file along with the number
3062 of bytes in the file, and the file name unless no arguments were given.
3063
3064 @command{cksum} is typically used to ensure that files
3065 transferred by unreliable means (e.g., netnews) have not been corrupted,
3066 by comparing the @command{cksum} output for the received files with the
3067 @command{cksum} output for the original files (typically given in the
3068 distribution).
3069
3070 The CRC algorithm is specified by the @acronym{POSIX} standard.  It is not
3071 compatible with the BSD or System V @command{sum} algorithms (see the
3072 previous section); it is more robust.
3073
3074 The only options are @option{--help} and @option{--version}.  @xref{Common
3075 options}.
3076
3077 @exitstatus
3078
3079
3080 @node md5sum invocation
3081 @section @command{md5sum}: Print or check MD5 digests
3082
3083 @pindex md5sum
3084 @cindex MD5
3085 @cindex 128-bit checksum
3086 @cindex checksum, 128-bit
3087 @cindex fingerprint, 128-bit
3088 @cindex message-digest, 128-bit
3089
3090 @command{md5sum} computes a 128-bit checksum (or @dfn{fingerprint} or
3091 @dfn{message-digest}) for each specified @var{file}.
3092
3093 Note: The MD5 digest is more reliable than a simple CRC (provided by
3094 the @command{cksum} command) for detecting accidental file corruption,
3095 as the chances of accidentally having two files with identical MD5
3096 are vanishingly small.  However, it should not be considered truly
3097 secure against malicious tampering: although finding a file with a
3098 given MD5 fingerprint, or modifying a file so as to retain its MD5 are
3099 considered infeasible at the moment, it is known how to produce
3100 different files with identical MD5 (a ``collision''), something which
3101 can be a security issue in certain contexts.  For more secure hashes,
3102 consider using SHA-1 or SHA-2.  @xref{sha1sum invocation}, and
3103 @ref{sha2 utilities}.
3104
3105 If a @var{file} is specified as @samp{-} or if no files are given
3106 @command{md5sum} computes the checksum for the standard input.
3107 @command{md5sum} can also determine whether a file and checksum are
3108 consistent.  Synopsis:
3109
3110 @example
3111 md5sum [@var{option}]@dots{} [@var{file}]@dots{}
3112 @end example
3113
3114 For each @var{file}, @samp{md5sum} outputs the MD5 checksum, a flag
3115 indicating a binary or text input file, and the file name.
3116 If @var{file} is omitted or specified as @samp{-}, standard input is read.
3117
3118 The program accepts the following options.  Also see @ref{Common options}.
3119
3120 @table @samp
3121
3122 @item -b
3123 @itemx --binary
3124 @opindex -b
3125 @opindex --binary
3126 @cindex binary input files
3127 Treat each input file as binary, by reading it in binary mode and
3128 outputting a @samp{*} flag.  This is the inverse of @option{--text}.
3129 On systems like @acronym{GNU} that do not distinguish between binary
3130 and text files, this option merely flags each input file as binary:
3131 the MD5 checksum is unaffected.  This option is the default on systems
3132 like MS-DOS that distinguish between binary and text files, except
3133 for reading standard input when standard input is a terminal.
3134
3135 @item -c
3136 @itemx --check
3137 Read file names and checksum information (not data) from each
3138 @var{file} (or from stdin if no @var{file} was specified) and report
3139 whether the checksums match the contents of the named files.
3140 The input to this mode of @command{md5sum} is usually the output of
3141 a prior, checksum-generating run of @samp{md5sum}.
3142 Each valid line of input consists of an MD5 checksum, a binary/text
3143 flag, and then a file name.
3144 Binary files are marked with @samp{*}, text with @samp{ }.
3145 For each such line, @command{md5sum} reads the named file and computes its
3146 MD5 checksum.  Then, if the computed message digest does not match the
3147 one on the line with the file name, the file is noted as having
3148 failed the test.  Otherwise, the file passes the test.
3149 By default, for each valid line, one line is written to standard
3150 output indicating whether the named file passed the test.
3151 After all checks have been performed, if there were any failures,
3152 a warning is issued to standard error.
3153 Use the @option{--status} option to inhibit that output.
3154 If any listed file cannot be opened or read, if any valid line has
3155 an MD5 checksum inconsistent with the associated file, or if no valid
3156 line is found, @command{md5sum} exits with nonzero status.  Otherwise,
3157 it exits successfully.
3158
3159 @itemx --status
3160 @opindex --status
3161 @cindex verifying MD5 checksums
3162 This option is useful only when verifying checksums.
3163 When verifying checksums, don't generate the default one-line-per-file
3164 diagnostic and don't output the warning summarizing any failures.
3165 Failures to open or read a file still evoke individual diagnostics to
3166 standard error.
3167 If all listed files are readable and are consistent with the associated
3168 MD5 checksums, exit successfully.  Otherwise exit with a status code
3169 indicating there was a failure.
3170
3171 @item -t
3172 @itemx --text
3173 @opindex -t
3174 @opindex --text
3175 @cindex text input files
3176 Treat each input file as text, by reading it in text mode and
3177 outputting a @samp{ } flag.  This is the inverse of @option{--binary}.
3178 This option is the default on systems like @acronym{GNU} that do not
3179 distinguish between binary and text files.  On other systems, it is
3180 the default for reading standard input when standard input is a
3181 terminal.
3182
3183 @item -w
3184 @itemx --warn
3185 @opindex -w
3186 @opindex --warn
3187 @cindex verifying MD5 checksums
3188 When verifying checksums, warn about improperly formatted MD5 checksum lines.
3189 This option is useful only if all but a few lines in the checked input
3190 are valid.
3191
3192 @end table
3193
3194 @exitstatus
3195
3196
3197 @node sha1sum invocation
3198 @section @command{sha1sum}: Print or check SHA-1 digests
3199
3200 @pindex sha1sum
3201 @cindex SHA-1
3202 @cindex 160-bit checksum
3203 @cindex checksum, 160-bit
3204 @cindex fingerprint, 160-bit
3205 @cindex message-digest, 160-bit
3206
3207 @command{sha1sum} computes a 160-bit checksum for each specified
3208 @var{file}.  The usage and options of this command are precisely the
3209 same as for @command{md5sum}.  @xref{md5sum invocation}.
3210
3211 Note: The SHA-1 digest is more secure than MD5, and no collisions of
3212 it are known (different files having the same fingerprint).  However,
3213 it is known that they can be produced with considerable, but not
3214 unreasonable, resources.  For this reason, it is generally considered
3215 that SHA-1 should be gradually phased out in favor of the more secure
3216 SHA-2 hash algorithms.  @xref{sha2 utilities}.
3217
3218
3219 @node sha2 utilities
3220 @section sha2 utilities: Print or check SHA-2 digests
3221
3222 @pindex sha224sum
3223 @pindex sha256sum
3224 @pindex sha384sum
3225 @pindex sha512sum
3226 @cindex SHA-2
3227 @cindex 224-bit checksum
3228 @cindex 256-bit checksum
3229 @cindex 384-bit checksum
3230 @cindex 512-bit checksum
3231 @cindex checksum, 224-bit
3232 @cindex checksum, 256-bit
3233 @cindex checksum, 384-bit
3234 @cindex checksum, 512-bit
3235 @cindex fingerprint, 224-bit
3236 @cindex fingerprint, 256-bit
3237 @cindex fingerprint, 384-bit
3238 @cindex fingerprint, 512-bit
3239 @cindex message-digest, 224-bit
3240 @cindex message-digest, 256-bit
3241 @cindex message-digest, 384-bit
3242 @cindex message-digest, 512-bit
3243
3244 The commands @command{sha224sum}, @command{sha256sum},
3245 @command{sha384sum} and @command{sha512sum} compute checksums of
3246 various lengths (respectively 224, 256, 384 and 512 bits),
3247 collectively known as the SHA-2 hashes.  The usage and options of
3248 these commands are precisely the same as for @command{md5sum}.
3249 @xref{md5sum invocation}.
3250
3251 Note: The SHA384 and SHA512 digests are considerably slower to
3252 compute, especially on 32-bit computers, than SHA224 or SHA256.
3253
3254
3255 @node Operating on sorted files
3256 @chapter Operating on sorted files
3257
3258 @cindex operating on sorted files
3259 @cindex sorted files, operations on
3260
3261 These commands work with (or produce) sorted files.
3262
3263 @menu
3264 * sort invocation::             Sort text files.
3265 * uniq invocation::             Uniquify files.
3266 * comm invocation::             Compare two sorted files line by line.
3267 * ptx invocation::              Produce a permuted index of file contents.
3268 * tsort invocation::            Topological sort.
3269 * tsort background::            Where tsort came from.
3270 @end menu
3271
3272
3273 @node sort invocation
3274 @section @command{sort}: Sort text files
3275
3276 @pindex sort
3277 @cindex sorting files
3278
3279 @command{sort} sorts, merges, or compares all the lines from the given
3280 files, or standard input if none are given or for a @var{file} of
3281 @samp{-}.  By default, @command{sort} writes the results to standard
3282 output.  Synopsis:
3283
3284 @example
3285 sort [@var{option}]@dots{} [@var{file}]@dots{}
3286 @end example
3287
3288 @command{sort} has three modes of operation: sort (the default), merge,
3289 and check for sortedness.  The following options change the operation
3290 mode:
3291
3292 @table @samp
3293
3294 @item -c
3295 @itemx --check
3296 @opindex -c
3297 @opindex --check
3298 @cindex checking for sortedness
3299 Check whether the given files are already sorted: if they are not all
3300 sorted, print an error message and exit with a status of 1.
3301 Otherwise, exit successfully.
3302
3303 @item -m
3304 @itemx --merge
3305 @opindex -m
3306 @opindex --merge
3307 @cindex merging sorted files
3308 Merge the given files by sorting them as a group.  Each input file must
3309 always be individually sorted.  It always works to sort instead of
3310 merge; merging is provided because it is faster, in the case where it
3311 works.
3312
3313 @end table
3314
3315 @cindex sort stability
3316 @cindex sort's last-resort comparison
3317 A pair of lines is compared as follows:
3318 @command{sort} compares each pair of fields, in the
3319 order specified on the command line, according to the associated
3320 ordering options, until a difference is found or no fields are left.
3321 If no key fields are specified, @command{sort} uses a default key of
3322 the entire line.  Finally, as a last resort when all keys compare
3323 equal, @command{sort} compares entire lines as if no ordering options
3324 other than @option{--reverse} (@option{-r}) were specified.  The
3325 @option{--stable} (@option{-s}) option disables this @dfn{last-resort
3326 comparison} so that lines in which all fields compare equal are left
3327 in their original relative order.  The @option{--unique}
3328 (@option{-u}) option also disables the last-resort comparison.
3329
3330 @vindex LC_ALL
3331 @vindex LC_COLLATE
3332 Unless otherwise specified, all comparisons use the character collating
3333 sequence specified by the @env{LC_COLLATE} locale.@footnote{If you
3334 use a non-@acronym{POSIX} locale (e.g., by setting @env{LC_ALL}
3335 to @samp{en_US}), then @command{sort} may produce output that is sorted
3336 differently than you're accustomed to.  In that case, set the @env{LC_ALL}
3337 environment variable to @samp{C}.  Note that setting only @env{LC_COLLATE}
3338 has two problems.  First, it is ineffective if @env{LC_ALL} is also set.
3339 Second, it has undefined behavior if @env{LC_CTYPE} (or @env{LANG}, if
3340 @env{LC_CTYPE} is unset) is set to an incompatible value.  For example,
3341 you get undefined behavior if @env{LC_CTYPE} is @code{ja_JP.PCK} but
3342 @env{LC_COLLATE} is @code{en_US.UTF-8}.}
3343
3344 @sc{gnu} @command{sort} (as specified for all @sc{gnu} utilities) has no
3345 limit on input line length or restrictions on bytes allowed within lines.
3346 In addition, if the final byte of an input file is not a newline, @sc{gnu}
3347 @command{sort} silently supplies one.  A line's trailing newline is not
3348 part of the line for comparison purposes.
3349
3350 @cindex exit status of @command{sort}
3351 Exit status:
3352
3353 @display
3354 0 if no error occurred
3355 1 if invoked with @option{-c} and the input is not properly sorted
3356 2 if an error occurred
3357 @end display
3358
3359 @vindex TMPDIR
3360 If the environment variable @env{TMPDIR} is set, @command{sort} uses its
3361 value as the directory for temporary files instead of @file{/tmp}.  The
3362 @option{--temporary-directory} (@option{-T}) option in turn overrides
3363 the environment variable.
3364
3365
3366 The following options affect the ordering of output lines.  They may be
3367 specified globally or as part of a specific key field.  If no key
3368 fields are specified, global options apply to comparison of entire
3369 lines; otherwise the global options are inherited by key fields that do
3370 not specify any special options of their own.  In pre-@acronym{POSIX}
3371 versions of @command{sort}, global options affect only later key fields,
3372 so portable shell scripts should specify global options first.
3373
3374 @table @samp
3375
3376 @item -b
3377 @itemx --ignore-leading-blanks
3378 @opindex -b
3379 @opindex --ignore-leading-blanks
3380 @cindex blanks, ignoring leading
3381 @vindex LC_CTYPE
3382 Ignore leading blanks when finding sort keys in each line.
3383 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3384 can change this.
3385
3386 @item -d
3387 @itemx --dictionary-order
3388 @opindex -d
3389 @opindex --dictionary-order
3390 @cindex dictionary order
3391 @cindex phone directory order
3392 @cindex telephone directory order
3393 @vindex LC_CTYPE
3394 Sort in @dfn{phone directory} order: ignore all characters except
3395 letters, digits and blanks when sorting.
3396 By default letters and digits are those of @acronym{ASCII} and a blank
3397 is a space or a tab, but the @env{LC_CTYPE} locale can change this.
3398
3399 @item -f
3400 @itemx --ignore-case
3401 @opindex -f
3402 @opindex --ignore-case
3403 @cindex ignoring case
3404 @cindex case folding
3405 @vindex LC_CTYPE
3406 Fold lowercase characters into the equivalent uppercase characters when
3407 comparing so that, for example, @samp{b} and @samp{B} sort as equal.
3408 The @env{LC_CTYPE} locale determines character types.
3409
3410 @item -g
3411 @itemx --general-numeric-sort
3412 @opindex -g
3413 @opindex --general-numeric-sort
3414 @cindex general numeric sort
3415 @vindex LC_NUMERIC
3416 Sort numerically, using the standard C function @code{strtod} to convert
3417 a prefix of each line to a double-precision floating point number.
3418 This allows floating point numbers to be specified in scientific notation,
3419 like @code{1.0e-34} and @code{10e100}.
3420 The @env{LC_NUMERIC} locale determines the decimal-point character.
3421 Do not report overflow, underflow, or conversion errors.
3422 Use the following collating sequence:
3423
3424 @itemize @bullet
3425 @item
3426 Lines that do not start with numbers (all considered to be equal).
3427 @item
3428 NaNs (``Not a Number'' values, in IEEE floating point arithmetic)
3429 in a consistent but machine-dependent order.
3430 @item
3431 Minus infinity.
3432 @item
3433 Finite numbers in ascending numeric order (with @math{-0} and @math{+0} equal).
3434 @item
3435 Plus infinity.
3436 @end itemize
3437
3438 Use this option only if there is no alternative; it is much slower than
3439 @option{--numeric-sort} (@option{-n}) and it can lose information when
3440 converting to floating point.
3441
3442 @item -i
3443 @itemx --ignore-nonprinting
3444 @opindex -i
3445 @opindex --ignore-nonprinting
3446 @cindex nonprinting characters, ignoring
3447 @cindex unprintable characters, ignoring
3448 @vindex LC_CTYPE
3449 Ignore nonprinting characters.
3450 The @env{LC_CTYPE} locale determines character types.
3451 This option has no effect if the stronger @option{--dictionary-order}
3452 (@option{-d}) option is also given.
3453
3454 @item -M
3455 @itemx --month-sort
3456 @opindex -M
3457 @opindex --month-sort
3458 @cindex months, sorting by
3459 @vindex LC_TIME
3460 An initial string, consisting of any amount of blanks, followed
3461 by a month name abbreviation, is folded to UPPER case and
3462 compared in the order @samp{JAN} < @samp{FEB} < @dots{} < @samp{DEC}.
3463 Invalid names compare low to valid names.  The @env{LC_TIME} locale
3464 category determines the month spellings.
3465 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3466 can change this.
3467
3468 @item -n
3469 @itemx --numeric-sort
3470 @opindex -n
3471 @opindex --numeric-sort
3472 @cindex numeric sort
3473 @vindex LC_NUMERIC
3474 Sort numerically: the number begins each line; specifically, it consists
3475 of optional blanks, an optional @samp{-} sign, and zero or more
3476 digits possibly separated by thousands separators, optionally followed
3477 by a decimal-point character and zero or more digits.  A string of
3478 no digits is interpreted as @samp{0}.  The @env{LC_NUMERIC}
3479 locale specifies the decimal-point character and thousands separator.
3480 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3481 can change this.
3482
3483 Numeric sort uses what might be considered an unconventional method to
3484 compare strings representing floating point numbers.  Rather than first
3485 converting each string to the C @code{double} type and then comparing
3486 those values, @command{sort} aligns the decimal-point characters in the
3487 two strings and compares the strings a character at a time.  One benefit
3488 of using this approach is its speed.  In practice this is much more
3489 efficient than performing the two corresponding string-to-double (or
3490 even string-to-integer) conversions and then comparing doubles.  In
3491 addition, there is no corresponding loss of precision.  Converting each
3492 string to @code{double} before comparison would limit precision to about
3493 16 digits on most systems.
3494
3495 Neither a leading @samp{+} nor exponential notation is recognized.
3496 To compare such strings numerically, use the
3497 @option{--general-numeric-sort} (@option{-g}) option.
3498
3499 @item -r
3500 @itemx --reverse
3501 @opindex -r
3502 @opindex --reverse
3503 @cindex reverse sorting
3504 Reverse the result of comparison, so that lines with greater key values
3505 appear earlier in the output instead of later.
3506
3507 @item -R
3508 @itemx --random-sort
3509 @opindex -R
3510 @opindex --random-sort
3511 @cindex random sort
3512 Sort by hashing the input keys and then sorting the hash values.  This
3513 is much like a random shuffle of the inputs, except that keys with the
3514 same value sort together.  The hash function is chosen at random.
3515
3516 @end table
3517
3518 Other options are:
3519
3520 @table @samp
3521
3522 @item -k @var{pos1}[,@var{pos2}]
3523 @itemx --key=@var{pos1}[,@var{pos2}]
3524 @opindex -k
3525 @opindex --key
3526 @cindex sort field
3527 Specify a sort field that consists of the part of the line between
3528 @var{pos1} and @var{pos2} (or the end of the line, if @var{pos2} is
3529 omitted), @emph{inclusive}.  Fields and character positions are numbered
3530 starting with 1.  So to sort on the second field, you'd use
3531 @option{--key=2,2} (@option{-k 2,2}).  See below for more examples.
3532
3533 @item -o @var{output-file}
3534 @itemx --output=@var{output-file}
3535 @opindex -o
3536 @opindex --output
3537 @cindex overwriting of input, allowed
3538 Write output to @var{output-file} instead of standard output.
3539 Normally, @command{sort} reads all input before opening
3540 @var{output-file}, so you can safely sort a file in place by using
3541 commands like @code{sort -o F F} and @code{cat F | sort -o F}.
3542 However, @command{sort} with @option{--merge} (@option{-m}) can open
3543 the output file before reading all input, so a command like @code{cat
3544 F | sort -m -o F - G} is not safe as @command{sort} might start
3545 writing @file{F} before @command{cat} is done reading it.
3546
3547 @vindex POSIXLY_CORRECT
3548 On newer systems, @option{-o} cannot appear after an input file if
3549 @env{POSIXLY_CORRECT} is set, e.g., @samp{sort F -o F}.  Portable
3550 scripts should specify @option{-o @var{output-file}} before any input
3551 files.
3552
3553 @item -s
3554 @itemx --stable
3555 @opindex -s
3556 @opindex --stable
3557 @cindex sort stability
3558 @cindex sort's last-resort comparison
3559
3560 Make @command{sort} stable by disabling its last-resort comparison.
3561 This option has no effect if no fields or global ordering options
3562 other than @option{--reverse} (@option{-R}) are specified.
3563
3564 @item -S @var{size}
3565 @itemx --buffer-size=@var{size}
3566 @opindex -S
3567 @opindex --buffer-size
3568 @cindex size for main memory sorting
3569 Use a main-memory sort buffer of the given @var{size}.  By default,
3570 @var{size} is in units of 1024 bytes.  Appending @samp{%} causes
3571 @var{size} to be interpreted as a percentage of physical memory.
3572 Appending @samp{K} multiplies @var{size} by 1024 (the default),
3573 @samp{M} by 1,048,576, @samp{G} by 1,073,741,824, and so on for
3574 @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}.  Appending
3575 @samp{b} causes @var{size} to be interpreted as a byte count, with no
3576 multiplication.
3577
3578 This option can improve the performance of @command{sort} by causing it
3579 to start with a larger or smaller sort buffer than the default.
3580 However, this option affects only the initial buffer size.  The buffer
3581 grows beyond @var{size} if @command{sort} encounters input lines larger
3582 than @var{size}.
3583
3584 @item -t @var{separator}
3585 @itemx --field-separator=@var{separator}
3586 @opindex -t
3587 @opindex --field-separator
3588 @cindex field separator character
3589 Use character @var{separator} as the field separator when finding the
3590 sort keys in each line.  By default, fields are separated by the empty
3591 string between a non-blank character and a blank character.
3592 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3593 can change this.
3594
3595 That is, given the input line @w{@samp{ foo bar}}, @command{sort} breaks it
3596 into fields @w{@samp{ foo}} and @w{@samp{ bar}}.  The field separator is
3597 not considered to be part of either the field preceding or the field
3598 following, so with @samp{sort @w{-t " "}} the same input line has
3599 three fields: an empty field, @samp{foo}, and @samp{bar}.
3600 However, fields that extend to the end of the line,
3601 as @option{-k 2}, or fields consisting of a range, as @option{-k 2,3},
3602 retain the field separators present between the endpoints of the range.
3603
3604 To specify a null character (@acronym{ASCII} @sc{nul}) as
3605 the field separator, use the two-character string @samp{\0}, e.g.,
3606 @samp{sort -t '\0'}.
3607
3608 @item -T @var{tempdir}
3609 @itemx --temporary-directory=@var{tempdir}
3610 @opindex -T
3611 @opindex --temporary-directory
3612 @cindex temporary directory
3613 @vindex TMPDIR
3614 Use directory @var{tempdir} to store temporary files, overriding the
3615 @env{TMPDIR} environment variable.  If this option is given more than
3616 once, temporary files are stored in all the directories given.  If you
3617 have a large sort or merge that is I/O-bound, you can often improve
3618 performance by using this option to specify directories on different
3619 disks and controllers.
3620
3621 @item -u
3622 @itemx --unique
3623 @opindex -u
3624 @opindex --unique
3625 @cindex uniquifying output
3626
3627 Normally, output only the first of a sequence of lines that compare
3628 equal.  For the @option{--check} (@option{-c}) option,
3629 check that no pair of consecutive lines compares equal.
3630
3631 This option also disables the default last-resort comparison.
3632
3633 The commands @code{sort -u} and @code{sort | uniq} are equivalent, but
3634 this equivalence does not extend to arbitrary @command{sort} options.
3635 For example, @code{sort -n -u} inspects only the value of the initial
3636 numeric string when checking for uniqueness, whereas @code{sort -n |
3637 uniq} inspects the entire line.  @xref{uniq invocation}.
3638
3639 @item -z
3640 @itemx --zero-terminated
3641 @opindex -z
3642 @opindex --zero-terminated
3643 @cindex sort zero-terminated lines
3644 Treat the input as a set of lines, each terminated by a null character
3645 (@acronym{ASCII} @sc{nul}) instead of a line feed
3646 (@acronym{ASCII} @sc{lf}).
3647 This option can be useful in conjunction with @samp{perl -0} or
3648 @samp{find -print0} and @samp{xargs -0} which do the same in order to
3649 reliably handle arbitrary file names (even those containing blanks
3650 or other special characters).
3651
3652 @end table
3653
3654 Historical (BSD and System V) implementations of @command{sort} have
3655 differed in their interpretation of some options, particularly
3656 @option{-b}, @option{-f}, and @option{-n}.  @sc{gnu} sort follows the @acronym{POSIX}
3657 behavior, which is usually (but not always!) like the System V behavior.
3658 According to @acronym{POSIX}, @option{-n} no longer implies @option{-b}.  For
3659 consistency, @option{-M} has been changed in the same way.  This may
3660 affect the meaning of character positions in field specifications in
3661 obscure cases.  The only fix is to add an explicit @option{-b}.
3662
3663 A position in a sort field specified with the @option{-k}
3664 option has the form @samp{@var{f}.@var{c}}, where @var{f} is the number
3665 of the field to use and @var{c} is the number of the first character
3666 from the beginning of the field.  In a start position, an omitted
3667 @samp{.@var{c}} stands for the field's first character.  In an end
3668 position, an omitted or zero @samp{.@var{c}} stands for the field's
3669 last character.  If the start field falls after the end of the line
3670 or after the end field, the field is empty.  If the
3671 @option{-b} option was specified, the @samp{.@var{c}} part of a field
3672 specification is counted from the first nonblank character of the field.
3673
3674 A sort key position may also have any of the option letters @samp{Mbdfinr}
3675 appended to it, in which case the global ordering options are not used
3676 for that particular field.  The @option{-b} option may be independently
3677 attached to either or both of the start and
3678 end positions of a field specification, and if it is inherited
3679 from the global options it will be attached to both.
3680 If input lines can contain leading or adjacent blanks and @option{-t}
3681 is not used, then @option{-k} is typically combined with @option{-b},
3682 @option{-g}, @option{-M}, or @option{-n}; otherwise the varying
3683 numbers of leading blanks in fields can cause confusing results.
3684
3685 Keys can span multiple fields.
3686
3687 @vindex _POSIX2_VERSION
3688 @vindex POSIXLY_CORRECT
3689 On older systems, @command{sort} supports an obsolete origin-zero
3690 syntax @samp{+@var{pos1} [-@var{pos2}]} for specifying sort keys.
3691 This obsolete behavior can be enabled or disabled with the
3692 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
3693 conformance}); it can also be enabled when @env{POSIXLY_CORRECT} is
3694 not set by using the obsolete syntax with @samp{-@var{pos2}} present.
3695
3696 Scripts intended for use on standard hosts should avoid obsolete
3697 syntax and should use @option{-k} instead.  For example, avoid
3698 @samp{sort +2}, since it might be interpreted as either @samp{sort
3699 ./+2} or @samp{sort -k 3}.  If your script must also run on hosts that
3700 support only the obsolete syntax, it can use a test like @samp{if sort
3701 -k 1 </dev/null >/dev/null 2>&1; then @dots{}} to decide which syntax
3702 to use.
3703
3704 Here are some examples to illustrate various combinations of options.
3705
3706 @itemize @bullet
3707
3708 @item
3709 Sort in descending (reverse) numeric order.
3710
3711 @example
3712 sort -n -r
3713 @end example
3714
3715 @item
3716 Sort alphabetically, omitting the first and second fields
3717 and the blanks at the start of the third field.
3718 This uses a single key composed of the characters beginning
3719 at the start of the first nonblank character in field three
3720 and extending to the end of each line.
3721
3722 @example
3723 sort -k 3b
3724 @end example
3725
3726 @item
3727 Sort numerically on the second field and resolve ties by sorting
3728 alphabetically on the third and fourth characters of field five.
3729 Use @samp{:} as the field delimiter.
3730
3731 @example
3732 sort -t : -k 2,2n -k 5.3,5.4
3733 @end example
3734
3735 Note that if you had written @option{-k 2n} instead of @option{-k 2,2n}
3736 @command{sort} would have used all characters beginning in the second field
3737 and extending to the end of the line as the primary @emph{numeric}
3738 key.  For the large majority of applications, treating keys spanning
3739 more than one field as numeric will not do what you expect.
3740
3741 Also note that the @samp{n} modifier was applied to the field-end
3742 specifier for the first key.  It would have been equivalent to
3743 specify @option{-k 2n,2} or @option{-k 2n,2n}.  All modifiers except
3744 @samp{b} apply to the associated @emph{field}, regardless of whether
3745 the modifier character is attached to the field-start and/or the
3746 field-end part of the key specifier.
3747
3748 @item
3749 Sort the password file on the fifth field and ignore any
3750 leading blanks.  Sort lines with equal values in field five
3751 on the numeric user ID in field three.  Fields are separated
3752 by @samp{:}.
3753
3754 @example
3755 sort -t : -k 5b,5 -k 3,3n /etc/passwd
3756 sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
3757 sort -t : -b -k 5,5 -k 3,3n /etc/passwd
3758 @end example
3759
3760 These three commands have equivalent effect.  The first specifies that
3761 the first key's start position ignores leading blanks and the second
3762 key is sorted numerically.  The other two commands rely on global
3763 options being inherited by sort keys that lack modifiers.  The inheritance
3764 works in this case because @option{-k 5b,5b} and @option{-k 5b,5} are
3765 equivalent, as the location of a field-end lacking a @samp{.@var{c}}
3766 character position is not affected by whether initial blanks are
3767 skipped.
3768
3769 @item
3770 Sort a set of log files, primarily by IPv4 address and secondarily by
3771 time stamp.  If two lines' primary and secondary keys are identical,
3772 output the lines in the same order that they were input.  The log
3773 files contain lines that look like this:
3774
3775 @example
3776 4.150.156.3 - - [01/Apr/2004:06:31:51 +0000] message 1
3777 211.24.3.231 - - [24/Apr/2004:20:17:39 +0000] message 2
3778 @end example
3779
3780 Fields are separated by exactly one space.  Sort IPv4 addresses
3781 lexicographically, e.g., 212.61.52.2 sorts before 212.129.233.201
3782 because 61 is less than 129.
3783
3784 @example
3785 sort -s -t ' ' -k 4.9n -k 4.5M -k 4.2n -k 4.14,4.21 file*.log |
3786 sort -s -t '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n
3787 @end example
3788
3789 This example cannot be done with a single @command{sort} invocation,
3790 since IPv4 address components are separated by @samp{.} while dates
3791 come just after a space.  So it is broken down into two invocations of
3792 @command{sort}: the first sorts by time stamp and the second by IPv4
3793 address.  The time stamp is sorted by year, then month, then day, and
3794 finally by hour-minute-second field, using @option{-k} to isolate each
3795 field.  Except for hour-minute-second there's no need to specify the
3796 end of each key field, since the @samp{n} and @samp{M} modifiers sort
3797 based on leading prefixes that cannot cross field boundaries.  The
3798 IPv4 addresses are sorted lexicographically.  The second sort uses
3799 @samp{-s} so that ties in the primary key are broken by the secondary
3800 key; the first sort uses @samp{-s} so that the combination of the two
3801 sorts is stable.
3802
3803 @item
3804 Generate a tags file in case-insensitive sorted order.
3805
3806 @smallexample
3807 find src -type f -print0 | sort -z -f | xargs -0 etags --append
3808 @end smallexample
3809
3810 The use of @option{-print0}, @option{-z}, and @option{-0} in this case means
3811 that file names that contain blanks or other special characters are
3812 not broken up
3813 by the sort operation.
3814
3815 @c This example is a bit contrived and needs more explanation.
3816 @c @item
3817 @c Sort records separated by an arbitrary string by using a pipe to convert
3818 @c each record delimiter string to @samp{\0}, then using sort's -z option,
3819 @c and converting each @samp{\0} back to the original record delimiter.
3820 @c
3821 @c @example
3822 @c printf 'c\n\nb\n\na\n'|perl -0pe 's/\n\n/\n\0/g'|sort -z|perl -0pe 's/\0/\n/g'
3823 @c @end example
3824
3825 @item
3826 Shuffle a list of directories, but preserve the order of files within
3827 each directory.  For instance, one could use this to generate a music
3828 playlist in which albums are shuffled but the songs of each album are
3829 played in order.
3830
3831 @example
3832 ls */* | sort -t / -k 1,1R -k 2,2
3833 @end example
3834
3835 @end itemize
3836
3837
3838 @node uniq invocation
3839 @section @command{uniq}: Uniquify files
3840
3841 @pindex uniq
3842 @cindex uniquify files
3843
3844 @command{uniq} writes the unique lines in the given @file{input}, or
3845 standard input if nothing is given or for an @var{input} name of
3846 @samp{-}.  Synopsis:
3847
3848 @example
3849 uniq [@var{option}]@dots{} [@var{input} [@var{output}]]
3850 @end example
3851
3852 By default, @command{uniq} prints its input lines, except that
3853 it discards all but the first of adjacent repeated lines, so that
3854 no output lines are repeated.  Optionally, it can instead discard
3855 lines that are not repeated, or all repeated lines.
3856
3857 The input need not be sorted, but repeated input lines are detected
3858 only if they are adjacent.  If you want to discard non-adjacent
3859 duplicate lines, perhaps you want to use @code{sort -u}.
3860 @xref{sort invocation}.
3861
3862 @vindex LC_COLLATE
3863 Comparisons use the character collating sequence specified by the
3864 @env{LC_COLLATE} locale category.
3865
3866 If no @var{output} file is specified, @command{uniq} writes to standard
3867 output.
3868
3869 The program accepts the following options.  Also see @ref{Common options}.
3870
3871 @table @samp
3872
3873 @item -f @var{n}
3874 @itemx --skip-fields=@var{n}
3875 @opindex -f
3876 @opindex --skip-fields
3877 Skip @var{n} fields on each line before checking for uniqueness.  Use
3878 a null string for comparison if a line has fewer than @var{n} fields.  Fields
3879 are sequences of non-space non-tab characters that are separated from
3880 each other by at least one space or tab.
3881
3882 For compatibility @command{uniq} supports an obsolete option syntax
3883 @option{-@var{n}}.  New scripts should use @option{-f @var{n}} instead.
3884
3885 @item -s @var{n}
3886 @itemx --skip-chars=@var{n}
3887 @opindex -s
3888 @opindex --skip-chars
3889 Skip @var{n} characters before checking for uniqueness.  Use a null string
3890 for comparison if a line has fewer than @var{n} characters.  If you use both
3891 the field and character skipping options, fields are skipped over first.
3892
3893 @vindex _POSIX2_VERSION
3894 On older systems, @command{uniq} supports an obsolete option syntax
3895 @option{+@var{n}}.
3896 This obsolete behavior can be enabled or disabled with the
3897 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
3898 conformance}), but portable scripts should avoid commands whose
3899 behavior depends on this variable.
3900 For example, use @samp{uniq ./+10} or @samp{uniq -s 10} rather than
3901 the ambiguous @samp{uniq +10}.
3902
3903 @item -c
3904 @itemx --count
3905 @opindex -c
3906 @opindex --count
3907 Print the number of times each line occurred along with the line.
3908
3909 @item -i
3910 @itemx --ignore-case
3911 @opindex -i
3912 @opindex --ignore-case
3913 Ignore differences in case when comparing lines.
3914
3915 @item -d
3916 @itemx --repeated
3917 @opindex -d
3918 @opindex --repeated
3919 @cindex repeated lines, outputting
3920 Discard lines that are not repeated.  When used by itself, this option
3921 causes @command{uniq} to print the first copy of each repeated line,
3922 and nothing else.
3923
3924 @item -D
3925 @itemx --all-repeated[=@var{delimit-method}]
3926 @opindex -D
3927 @opindex --all-repeated
3928 @cindex all repeated lines, outputting
3929 Do not discard the second and subsequent repeated input lines,
3930 but discard lines that are not repeated.
3931 This option is useful mainly in conjunction with other options e.g.,
3932 to ignore case or to compare only selected fields.
3933 The optional @var{delimit-method} tells how to delimit
3934 groups of repeated lines, and must be one of the following:
3935
3936 @table @samp
3937
3938 @item none
3939 Do not delimit groups of repeated lines.
3940 This is equivalent to @option{--all-repeated} (@option{-D}).
3941
3942 @item prepend
3943 Output a newline before each group of repeated lines.
3944
3945 @item separate
3946 Separate groups of repeated lines with a single newline.
3947 This is the same as using @samp{prepend}, except that
3948 there is no newline before the first group, and hence
3949 may be better suited for output direct to users.
3950 @end table
3951
3952 Note that when groups are delimited and the input stream contains
3953 two or more consecutive blank lines, then the output is ambiguous.
3954 To avoid that, filter the input through @samp{tr -s '\n'} to replace
3955 each sequence of consecutive newlines with a single newline.
3956
3957 This is a @sc{gnu} extension.
3958 @c FIXME: give an example showing *how* it's useful
3959
3960 @item -u
3961 @itemx --unique
3962 @opindex -u
3963 @opindex --unique
3964 @cindex unique lines, outputting
3965 Discard the first repeated line.  When used by itself, this option
3966 causes @command{uniq} to print unique lines, and nothing else.
3967
3968 @item -w @var{n}
3969 @itemx --check-chars=@var{n}
3970 @opindex -w
3971 @opindex --check-chars
3972 Compare at most @var{n} characters on each line (after skipping any specified
3973 fields and characters).  By default the entire rest of the lines are
3974 compared.
3975
3976 @end table
3977
3978 @exitstatus
3979
3980
3981 @node comm invocation
3982 @section @command{comm}: Compare two sorted files line by line
3983
3984 @pindex comm
3985 @cindex line-by-line comparison
3986 @cindex comparing sorted files
3987
3988 @command{comm} writes to standard output lines that are common, and lines
3989 that are unique, to two input files; a file name of @samp{-} means
3990 standard input.  Synopsis:
3991
3992 @example
3993 comm [@var{option}]@dots{} @var{file1} @var{file2}
3994 @end example
3995
3996 @vindex LC_COLLATE
3997 Before @command{comm} can be used, the input files must be sorted using the
3998 collating sequence specified by the @env{LC_COLLATE} locale.
3999 If an input file ends in a non-newline
4000 character, a newline is silently appended.  The @command{sort} command with
4001 no options always outputs a file that is suitable input to @command{comm}.
4002
4003 @cindex differing lines
4004 @cindex common lines
4005 With no options, @command{comm} produces three-column output.  Column one
4006 contains lines unique to @var{file1}, column two contains lines unique
4007 to @var{file2}, and column three contains lines common to both files.
4008 Columns are separated by a single TAB character.
4009 @c FIXME: when there's an option to supply an alternative separator
4010 @c string, append `by default' to the above sentence.
4011
4012 @opindex -1
4013 @opindex -2
4014 @opindex -3
4015 The options @option{-1}, @option{-2}, and @option{-3} suppress printing of
4016 the corresponding columns.  Also see @ref{Common options}.
4017
4018 Unlike some other comparison utilities, @command{comm} has an exit
4019 status that does not depend on the result of the comparison.
4020 Upon normal completion @command{comm} produces an exit code of zero.
4021 If there is an error it exits with nonzero status.
4022
4023
4024 @node tsort invocation
4025 @section @command{tsort}: Topological sort
4026
4027 @pindex tsort
4028 @cindex topological sort
4029
4030 @command{tsort} performs a topological sort on the given @var{file}, or
4031 standard input if no input file is given or for a @var{file} of
4032 @samp{-}.  For more details and some history, see @ref{tsort background}.
4033 Synopsis:
4034
4035 @example
4036 tsort [@var{option}] [@var{file}]
4037 @end example
4038
4039 @command{tsort} reads its input as pairs of strings, separated by blanks,
4040 indicating a partial ordering.  The output is a total ordering that
4041 corresponds to the given partial ordering.
4042
4043 For example
4044
4045 @example
4046 tsort <<EOF
4047 a b c
4048 d
4049 e f
4050 b c d e
4051 EOF
4052 @end example
4053
4054 @noindent
4055 will produce the output
4056
4057 @example
4058 a
4059 b
4060 c
4061 d
4062 e
4063 f
4064 @end example
4065
4066 Consider a more realistic example.
4067 You have a large set of functions all in one file, and they may all be
4068 declared static except one.  Currently that one (say @code{main}) is the
4069 first function defined in the file, and the ones it calls directly follow
4070 it, followed by those they call, etc.  Let's say that you are determined
4071 to take advantage of prototypes, so you have to choose between declaring
4072 all of those functions (which means duplicating a lot of information from
4073 the definitions) and rearranging the functions so that as many as possible
4074 are defined before they are used.  One way to automate the latter process
4075 is to get a list for each function of the functions it calls directly.
4076 Many programs can generate such lists.  They describe a call graph.
4077 Consider the following list, in which a given line indicates that the
4078 function on the left calls the one on the right directly.
4079
4080 @example
4081 main parse_options
4082 main tail_file
4083 main tail_forever
4084 tail_file pretty_name
4085 tail_file write_header
4086 tail_file tail
4087 tail_forever recheck
4088 tail_forever pretty_name
4089 tail_forever write_header
4090 tail_forever dump_remainder
4091 tail tail_lines
4092 tail tail_bytes
4093 tail_lines start_lines
4094 tail_lines dump_remainder
4095 tail_lines file_lines
4096 tail_lines pipe_lines
4097 tail_bytes xlseek
4098 tail_bytes start_bytes
4099 tail_bytes dump_remainder
4100 tail_bytes pipe_bytes
4101 file_lines dump_remainder
4102 recheck pretty_name
4103 @end example
4104
4105 then you can use @command{tsort} to produce an ordering of those
4106 functions that satisfies your requirement.
4107
4108 @example
4109 example$ tsort call-graph | tac
4110 dump_remainder
4111 start_lines
4112 file_lines
4113 pipe_lines
4114 xlseek
4115 start_bytes
4116 pipe_bytes
4117 tail_lines
4118 tail_bytes
4119 pretty_name
4120 write_header
4121 tail
4122 recheck
4123 parse_options
4124 tail_file
4125 tail_forever
4126 main
4127 @end example
4128
4129 @command{tsort} detects any cycles in the input and writes the first cycle
4130 encountered to standard error.
4131
4132 Note that for a given partial ordering, generally there is no unique
4133 total ordering.  In the context of the call graph above, the function
4134 @code{parse_options} may be placed anywhere in the list as long as it
4135 precedes @code{main}.
4136
4137 The only options are @option{--help} and @option{--version}.  @xref{Common
4138 options}.
4139
4140 @node tsort background
4141 @section @command{tsort}: Background
4142
4143 @command{tsort} exists because very early versions of the Unix linker processed
4144 an archive file exactly once, and in order.  As @command{ld} read each object
4145 in the archive, it decided whether it was needed in the program based on
4146 whether it defined any symbols which were undefined at that point in
4147 the link.
4148
4149 This meant that dependencies within the archive had to be handled
4150 specially.  For example, @code{scanf} probably calls @code{read}.  That means
4151 that in a single pass through an archive, it was important for @code{scanf.o}
4152 to appear before read.o, because otherwise a program which calls
4153 @code{scanf} but not @code{read} might end up with an unexpected unresolved
4154 reference to @code{read}.
4155
4156 The way to address this problem was to first generate a set of
4157 dependencies of one object file on another.  This was done by a shell
4158 script called @command{lorder}.  The GNU tools don't provide a version of
4159 lorder, as far as I know, but you can still find it in BSD
4160 distributions.
4161
4162 Then you ran @command{tsort} over the @command{lorder} output, and you used the
4163 resulting sort to define the order in which you added objects to the archive.
4164
4165 This whole procedure has been obsolete since about 1980, because
4166 Unix archives now contain a symbol table (traditionally built by
4167 @command{ranlib}, now generally built by @command{ar} itself), and the Unix
4168 linker uses the symbol table to effectively make multiple passes over
4169 an archive file.
4170
4171 Anyhow, that's where tsort came from.  To solve an old problem with
4172 the way the linker handled archive files, which has since been solved
4173 in different ways.
4174
4175 @exitstatus
4176
4177
4178 @node ptx invocation
4179 @section @command{ptx}: Produce permuted indexes
4180
4181 @pindex ptx
4182
4183 @command{ptx} reads a text file and essentially produces a permuted index, with
4184 each keyword in its context.  The calling sketch is either one of:
4185
4186 @example
4187 ptx [@var{option} @dots{}] [@var{file} @dots{}]
4188 ptx -G [@var{option} @dots{}] [@var{input} [@var{output}]]
4189 @end example
4190
4191 The @option{-G} (or its equivalent: @option{--traditional}) option disables
4192 all @sc{gnu} extensions and reverts to traditional mode, thus introducing some
4193 limitations and changing several of the program's default option values.
4194 When @option{-G} is not specified, @sc{gnu} extensions are always enabled.
4195 @sc{gnu} extensions to @command{ptx} are documented wherever appropriate in this
4196 document.  For the full list, see @xref{Compatibility in ptx}.
4197
4198 Individual options are explained in the following sections.
4199
4200 When @sc{gnu} extensions are enabled, there may be zero, one or several
4201 @var{file}s after the options.  If there is no @var{file}, the program
4202 reads the standard input.  If there is one or several @var{file}s, they
4203 give the name of input files which are all read in turn, as if all the
4204 input files were concatenated.  However, there is a full contextual
4205 break between each file and, when automatic referencing is requested,
4206 file names and line numbers refer to individual text input files.  In
4207 all cases, the program outputs the permuted index to the standard
4208 output.
4209
4210 When @sc{gnu} extensions are @emph{not} enabled, that is, when the program
4211 operates in traditional mode, there may be zero, one or two parameters
4212 besides the options.  If there are no parameters, the program reads the
4213 standard input and outputs the permuted index to the standard output.
4214 If there is only one parameter, it names the text @var{input} to be read
4215 instead of the standard input.  If two parameters are given, they give
4216 respectively the name of the @var{input} file to read and the name of
4217 the @var{output} file to produce.  @emph{Be very careful} to note that,
4218 in this case, the contents of file given by the second parameter is
4219 destroyed.  This behavior is dictated by System V @command{ptx}
4220 compatibility; @sc{gnu} Standards normally discourage output parameters not
4221 introduced by an option.
4222
4223 Note that for @emph{any} file named as the value of an option or as an
4224 input text file, a single dash @kbd{-} may be used, in which case
4225 standard input is assumed.  However, it would not make sense to use this
4226 convention more than once per program invocation.
4227
4228 @menu
4229 * General options in ptx::      Options which affect general program behavior.
4230 * Charset selection in ptx::    Underlying character set considerations.
4231 * Input processing in ptx::     Input fields, contexts, and keyword selection.
4232 * Output formatting in ptx::    Types of output format, and sizing the fields.
4233 * Compatibility in ptx::
4234 @end menu
4235
4236
4237 @node General options in ptx
4238 @subsection General options
4239
4240 @table @samp
4241
4242 @item -G
4243 @itemx --traditional
4244 As already explained, this option disables all @sc{gnu} extensions to
4245 @command{ptx} and switches to traditional mode.
4246
4247 @item --help
4248 Print a short help on standard output, then exit without further
4249 processing.
4250
4251 @item --version
4252 Print the program version on standard output, then exit without further
4253 processing.
4254
4255 @end table
4256
4257 @exitstatus
4258
4259
4260 @node Charset selection in ptx
4261 @subsection Charset selection
4262
4263 @c FIXME:  People don't necessarily know what an IBM-PC was these days.
4264 As it is set up now, the program assumes that the input file is coded
4265 using 8-bit @acronym{ISO} 8859-1 code, also known as Latin-1 character set,
4266 @emph{unless} it is compiled for MS-DOS, in which case it uses the
4267 character set of the IBM-PC@.  (@sc{gnu} @command{ptx} is not known to work on
4268 smaller MS-DOS machines anymore.)  Compared to 7-bit @acronym{ASCII}, the set
4269 of characters which are letters is different; this alters the behavior
4270 of regular expression matching.  Thus, the default regular expression
4271 for a keyword allows foreign or diacriticized letters.  Keyword sorting,
4272 however, is still crude; it obeys the underlying character set ordering
4273 quite blindly.
4274
4275 @table @samp
4276
4277 @item -f
4278 @itemx --ignore-case
4279 Fold lower case letters to upper case for sorting.
4280
4281 @end table
4282
4283
4284 @node Input processing in ptx
4285 @subsection Word selection and input processing
4286
4287 @table @samp
4288
4289 @item -b @var{file}
4290 @item --break-file=@var{file}
4291
4292 This option provides an alternative (to @option{-W}) method of describing
4293 which characters make up words.  It introduces the name of a
4294 file which contains a list of characters which can@emph{not} be part of
4295 one word; this file is called the @dfn{Break file}.  Any character which
4296 is not part of the Break file is a word constituent.  If both options
4297 @option{-b} and @option{-W} are specified, then @option{-W} has precedence and
4298 @option{-b} is ignored.
4299
4300 When @sc{gnu} extensions are enabled, the only way to avoid newline as a
4301 break character is to write all the break characters in the file with no
4302 newline at all, not even at the end of the file.  When @sc{gnu} extensions
4303 are disabled, spaces, tabs and newlines are always considered as break
4304 characters even if not included in the Break file.
4305
4306 @item -i @var{file}
4307 @itemx --ignore-file=@var{file}
4308
4309 The file associated with this option contains a list of words which will
4310 never be taken as keywords in concordance output.  It is called the
4311 @dfn{Ignore file}.  The file contains exactly one word in each line; the
4312 end of line separation of words is not subject to the value of the
4313 @option{-S} option.
4314
4315 @item -o @var{file}
4316 @itemx --only-file=@var{file}
4317
4318 The file associated with this option contains a list of words which will
4319 be retained in concordance output; any word not mentioned in this file
4320 is ignored.  The file is called the @dfn{Only file}.  The file contains
4321 exactly one word in each line; the end of line separation of words is
4322 not subject to the value of the @option{-S} option.
4323
4324 There is no default for the Only file.  When both an Only file and an
4325 Ignore file are specified, a word is considered a keyword only
4326 if it is listed in the Only file and not in the Ignore file.
4327
4328 @item -r
4329 @itemx --references
4330
4331 On each input line, the leading sequence of non-white space characters will be
4332 taken to be a reference that has the purpose of identifying this input
4333 line in the resulting permuted index.  For more information about reference
4334 production, see @xref{Output formatting in ptx}.
4335 Using this option changes the default value for option @option{-S}.
4336
4337 Using this option, the program does not try very hard to remove
4338 references from contexts in output, but it succeeds in doing so
4339 @emph{when} the context ends exactly at the newline.  If option
4340 @option{-r} is used with @option{-S} default value, or when @sc{gnu} extensions
4341 are disabled, this condition is always met and references are completely
4342 excluded from the output contexts.
4343
4344 @item -S @var{regexp}
4345 @itemx --sentence-regexp=@var{regexp}
4346
4347 This option selects which regular expression will describe the end of a
4348 line or the end of a sentence.  In fact, this regular expression is not
4349 the only distinction between end of lines or end of sentences, and input
4350 line boundaries have no special significance outside this option.  By
4351 default, when @sc{gnu} extensions are enabled and if @option{-r} option is not
4352 used, end of sentences are used.  In this case, this @var{regex} is
4353 imported from @sc{gnu} Emacs:
4354
4355 @example
4356 [.?!][]\"')@}]*\\($\\|\t\\|  \\)[ \t\n]*
4357 @end example
4358
4359 Whenever @sc{gnu} extensions are disabled or if @option{-r} option is used, end
4360 of lines are used; in this case, the default @var{regexp} is just:
4361
4362 @example
4363 \n
4364 @end example
4365
4366 Using an empty @var{regexp} is equivalent to completely disabling end of
4367 line or end of sentence recognition.  In this case, the whole file is
4368 considered to be a single big line or sentence.  The user might want to
4369 disallow all truncation flag generation as well, through option @option{-F
4370 ""}.  @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4371 Manual}.
4372
4373 When the keywords happen to be near the beginning of the input line or
4374 sentence, this often creates an unused area at the beginning of the
4375 output context line; when the keywords happen to be near the end of the
4376 input line or sentence, this often creates an unused area at the end of
4377 the output context line.  The program tries to fill those unused areas
4378 by wrapping around context in them; the tail of the input line or
4379 sentence is used to fill the unused area on the left of the output line;
4380 the head of the input line or sentence is used to fill the unused area
4381 on the right of the output line.
4382
4383 As a matter of convenience to the user, many usual backslashed escape
4384 sequences from the C language are recognized and converted to the
4385 corresponding characters by @command{ptx} itself.
4386
4387 @item -W @var{regexp}
4388 @itemx --word-regexp=@var{regexp}
4389
4390 This option selects which regular expression will describe each keyword.
4391 By default, if @sc{gnu} extensions are enabled, a word is a sequence of
4392 letters; the @var{regexp} used is @samp{\w+}.  When @sc{gnu} extensions are
4393 disabled, a word is by default anything which ends with a space, a tab
4394 or a newline; the @var{regexp} used is @samp{[^ \t\n]+}.
4395
4396 An empty @var{regexp} is equivalent to not using this option.
4397 @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4398 Manual}.
4399
4400 As a matter of convenience to the user, many usual backslashed escape
4401 sequences, as found in the C language, are recognized and converted to
4402 the corresponding characters by @command{ptx} itself.
4403
4404 @end table
4405
4406
4407 @node Output formatting in ptx
4408 @subsection Output formatting
4409
4410 Output format is mainly controlled by the @option{-O} and @option{-T} options
4411 described in the table below.  When neither @option{-O} nor @option{-T} are
4412 selected, and if @sc{gnu} extensions are enabled, the program chooses an
4413 output format suitable for a dumb terminal.  Each keyword occurrence is
4414 output to the center of one line, surrounded by its left and right
4415 contexts.  Each field is properly justified, so the concordance output
4416 can be readily observed.  As a special feature, if automatic
4417 references are selected by option @option{-A} and are output before the
4418 left context, that is, if option @option{-R} is @emph{not} selected, then
4419 a colon is added after the reference; this nicely interfaces with @sc{gnu}
4420 Emacs @code{next-error} processing.  In this default output format, each
4421 white space character, like newline and tab, is merely changed to
4422 exactly one space, with no special attempt to compress consecutive
4423 spaces.  This might change in the future.  Except for those white space
4424 characters, every other character of the underlying set of 256
4425 characters is transmitted verbatim.
4426
4427 Output format is further controlled by the following options.
4428
4429 @table @samp
4430
4431 @item -g @var{number}
4432 @itemx --gap-size=@var{number}
4433
4434 Select the size of the minimum white space gap between the fields on the
4435 output line.
4436
4437 @item -w @var{number}
4438 @itemx --width=@var{number}
4439
4440 Select the maximum output width of each final line.  If references are
4441 used, they are included or excluded from the maximum output width
4442 depending on the value of option @option{-R}.  If this option is not
4443 selected, that is, when references are output before the left context,
4444 the maximum output width takes into account the maximum length of all
4445 references.  If this option is selected, that is, when references are
4446 output after the right context, the maximum output width does not take
4447 into account the space taken by references, nor the gap that precedes
4448 them.
4449
4450 @item -A
4451 @itemx --auto-reference
4452
4453 Select automatic references.  Each input line will have an automatic
4454 reference made up of the file name and the line ordinal, with a single
4455 colon between them.  However, the file name will be empty when standard
4456 input is being read.  If both @option{-A} and @option{-r} are selected, then
4457 the input reference is still read and skipped, but the automatic
4458 reference is used at output time, overriding the input reference.
4459
4460 @item -R
4461 @itemx --right-side-refs
4462
4463 In the default output format, when option @option{-R} is not used, any
4464 references produced by the effect of options @option{-r} or @option{-A} are
4465 placed to the far right of output lines, after the right context.  With
4466 default output format, when the @option{-R} option is specified, references
4467 are rather placed at the beginning of each output line, before the left
4468 context.  For any other output format, option @option{-R} is
4469 ignored, with one exception:  with @option{-R} the width of references
4470 is @emph{not} taken into account in total output width given by @option{-w}.
4471
4472 This option is automatically selected whenever @sc{gnu} extensions are
4473 disabled.
4474
4475 @item -F @var{string}
4476 @itemx --flac-truncation=@var{string}
4477
4478 This option will request that any truncation in the output be reported
4479 using the string @var{string}.  Most output fields theoretically extend
4480 towards the beginning or the end of the current line, or current
4481 sentence, as selected with option @option{-S}.  But there is a maximum
4482 allowed output line width, changeable through option @option{-w}, which is
4483 further divided into space for various output fields.  When a field has
4484 to be truncated because it cannot extend beyond the beginning or the end of
4485 the current line to fit in, then a truncation occurs.  By default,
4486 the string used is a single slash, as in @option{-F /}.
4487
4488 @var{string} may have more than one character, as in @option{-F ...}.
4489 Also, in the particular case when @var{string} is empty (@option{-F ""}),
4490 truncation flagging is disabled, and no truncation marks are appended in
4491 this case.
4492
4493 As a matter of convenience to the user, many usual backslashed escape
4494 sequences, as found in the C language, are recognized and converted to
4495 the corresponding characters by @command{ptx} itself.
4496
4497 @item -M @var{string}
4498 @itemx --macro-name=@var{string}
4499
4500 Select another @var{string} to be used instead of @samp{xx}, while
4501 generating output suitable for @command{nroff}, @command{troff} or @TeX{}.
4502
4503 @item -O
4504 @itemx --format=roff
4505
4506 Choose an output format suitable for @command{nroff} or @command{troff}
4507 processing.  Each output line will look like:
4508
4509 @smallexample
4510 .xx "@var{tail}" "@var{before}" "@var{keyword_and_after}" "@var{head}" "@var{ref}"
4511 @end smallexample
4512
4513 so it will be possible to write a @samp{.xx} roff macro to take care of
4514 the output typesetting.  This is the default output format when @sc{gnu}
4515 extensions are disabled.  Option @option{-M} can be used to change
4516 @samp{xx} to another macro name.
4517
4518 In this output format, each non-graphical character, like newline and
4519 tab, is merely changed to exactly one space, with no special attempt to
4520 compress consecutive spaces.  Each quote character: @kbd{"} is doubled
4521 so it will be correctly processed by @command{nroff} or @command{troff}.
4522
4523 @item -T
4524 @itemx --format=tex
4525
4526 Choose an output format suitable for @TeX{} processing.  Each output
4527 line will look like:
4528
4529 @smallexample
4530 \xx @{@var{tail}@}@{@var{before}@}@{@var{keyword}@}@{@var{after}@}@{@var{head}@}@{@var{ref}@}
4531 @end smallexample
4532
4533 @noindent
4534 so it will be possible to write a @code{\xx} definition to take care of
4535 the output typesetting.  Note that when references are not being
4536 produced, that is, neither option @option{-A} nor option @option{-r} is
4537 selected, the last parameter of each @code{\xx} call is inhibited.
4538 Option @option{-M} can be used to change @samp{xx} to another macro
4539 name.
4540
4541 In this output format, some special characters, like @kbd{$}, @kbd{%},
4542 @kbd{&}, @kbd{#} and @kbd{_} are automatically protected with a
4543 backslash.  Curly brackets @kbd{@{}, @kbd{@}} are protected with a
4544 backslash and a pair of dollar signs (to force mathematical mode).  The
4545 backslash itself produces the sequence @code{\backslash@{@}}.
4546 Circumflex and tilde diacritical marks produce the sequence @code{^\@{ @}} and
4547 @code{~\@{ @}} respectively.  Other diacriticized characters of the
4548 underlying character set produce an appropriate @TeX{} sequence as far
4549 as possible.  The other non-graphical characters, like newline and tab,
4550 and all other characters which are not part of @acronym{ASCII}, are merely
4551 changed to exactly one space, with no special attempt to compress
4552 consecutive spaces.  Let me know how to improve this special character
4553 processing for @TeX{}.
4554
4555 @end table
4556
4557
4558 @node Compatibility in ptx
4559 @subsection The @sc{gnu} extensions to @command{ptx}
4560
4561 This version of @command{ptx} contains a few features which do not exist in
4562 System V @command{ptx}.  These extra features are suppressed by using the
4563 @option{-G} command line option, unless overridden by other command line
4564 options.  Some @sc{gnu} extensions cannot be recovered by overriding, so the
4565 simple rule is to avoid @option{-G} if you care about @sc{gnu} extensions.
4566 Here are the differences between this program and System V @command{ptx}.
4567
4568 @itemize @bullet
4569
4570 @item
4571 This program can read many input files at once, it always writes the
4572 resulting concordance on standard output.  On the other hand, System V
4573 @command{ptx} reads only one file and sends the result to standard output
4574 or, if a second @var{file} parameter is given on the command, to that
4575 @var{file}.
4576
4577 Having output parameters not introduced by options is a dangerous
4578 practice which @sc{gnu} avoids as far as possible.  So, for using @command{ptx}
4579 portably between @sc{gnu} and System V, you should always use it with a
4580 single input file, and always expect the result on standard output.  You
4581 might also want to automatically configure in a @option{-G} option to
4582 @command{ptx} calls in products using @command{ptx}, if the configurator finds
4583 that the installed @command{ptx} accepts @option{-G}.
4584
4585 @item
4586 The only options available in System V @command{ptx} are options @option{-b},
4587 @option{-f}, @option{-g}, @option{-i}, @option{-o}, @option{-r}, @option{-t} and
4588 @option{-w}.  All other options are @sc{gnu} extensions and are not repeated in
4589 this enumeration.  Moreover, some options have a slightly different
4590 meaning when @sc{gnu} extensions are enabled, as explained below.
4591
4592 @item
4593 By default, concordance output is not formatted for @command{troff} or
4594 @command{nroff}.  It is rather formatted for a dumb terminal.  @command{troff}
4595 or @command{nroff} output may still be selected through option @option{-O}.
4596
4597 @item
4598 Unless @option{-R} option is used, the maximum reference width is
4599 subtracted from the total output line width.  With @sc{gnu} extensions
4600 disabled, width of references is not taken into account in the output
4601 line width computations.
4602
4603 @item
4604 All 256 bytes, even null bytes, are always read and processed from
4605 input file with no adverse effect, even if @sc{gnu} extensions are disabled.
4606 However, System V @command{ptx} does not accept 8-bit characters, a few
4607 control characters are rejected, and the tilde @kbd{~} is also rejected.
4608
4609 @item
4610 Input line length is only limited by available memory, even if @sc{gnu}
4611 extensions are disabled.  However, System V @command{ptx} processes only
4612 the first 200 characters in each line.
4613
4614 @item
4615 The break (non-word) characters default to be every character except all
4616 letters of the underlying character set, diacriticized or not.  When @sc{gnu}
4617 extensions are disabled, the break characters default to space, tab and
4618 newline only.
4619
4620 @item
4621 The program makes better use of output line width.  If @sc{gnu} extensions
4622 are disabled, the program rather tries to imitate System V @command{ptx},
4623 but still, there are some slight disposition glitches this program does
4624 not completely reproduce.
4625
4626 @item
4627 The user can specify both an Ignore file and an Only file.  This is not
4628 allowed with System V @command{ptx}.
4629
4630 @end itemize
4631
4632
4633 @node Operating on fields within a line
4634 @chapter Operating on fields within a line
4635
4636 @menu
4637 * cut invocation::              Print selected parts of lines.
4638 * paste invocation::            Merge lines of files.
4639 * join invocation::             Join lines on a common field.
4640 @end menu
4641
4642
4643 @node cut invocation
4644 @section @command{cut}: Print selected parts of lines
4645
4646 @pindex cut
4647 @command{cut} writes to standard output selected parts of each line of each
4648 input file, or standard input if no files are given or for a file name of
4649 @samp{-}.  Synopsis:
4650
4651 @example
4652 cut [@var{option}]@dots{} [@var{file}]@dots{}
4653 @end example
4654
4655 In the table which follows, the @var{byte-list}, @var{character-list},
4656 and @var{field-list} are one or more numbers or ranges (two numbers
4657 separated by a dash) separated by commas.  Bytes, characters, and
4658 fields are numbered starting at 1.  Incomplete ranges may be
4659 given: @option{-@var{m}} means @samp{1-@var{m}}; @samp{@var{n}-} means
4660 @samp{@var{n}} through end of line or last field.  The list elements
4661 can be repeated, can overlap, and can be specified in any order; but
4662 the selected input is written in the same order that it is read, and
4663 is written exactly once.
4664
4665 The program accepts the following options.  Also see @ref{Common
4666 options}.
4667
4668 @table @samp
4669
4670 @item -b @var{byte-list}
4671 @itemx --bytes=@var{byte-list}
4672 @opindex -b
4673 @opindex --bytes
4674 Select for printing only the bytes in positions listed in
4675 @var{byte-list}.  Tabs and backspaces are treated like any other
4676 character; they take up 1 byte.  If an output delimiter is specified,
4677 (see the description of @option{--output-delimiter}), then output that
4678 string between ranges of selected bytes.
4679
4680 @item -c @var{character-list}
4681 @itemx --characters=@var{character-list}
4682 @opindex -c
4683 @opindex --characters
4684 Select for printing only the characters in positions listed in
4685 @var{character-list}.  The same as @option{-b} for now, but
4686 internationalization will change that.  Tabs and backspaces are
4687 treated like any other character; they take up 1 character.  If an
4688 output delimiter is specified, (see the description of
4689 @option{--output-delimiter}), then output that string between ranges
4690 of selected bytes.
4691
4692 @item -f @var{field-list}
4693 @itemx --fields=@var{field-list}
4694 @opindex -f
4695 @opindex --fields
4696 Select for printing only the fields listed in @var{field-list}.
4697 Fields are separated by a TAB character by default.  Also print any
4698 line that contains no delimiter character, unless the
4699 @option{--only-delimited} (@option{-s}) option is specified
4700
4701 @item -d @var{input_delim_byte}
4702 @itemx --delimiter=@var{input_delim_byte}
4703 @opindex -d
4704 @opindex --delimiter
4705 With @option{-f}, use the first byte of @var{input_delim_byte} as
4706 the input fields separator (default is TAB).
4707
4708 @item -n
4709 @opindex -n
4710 Do not split multi-byte characters (no-op for now).
4711
4712 @item -s
4713 @itemx --only-delimited
4714 @opindex -s
4715 @opindex --only-delimited
4716 For @option{-f}, do not print lines that do not contain the field separator
4717 character.  Normally, any line without a field separator is printed verbatim.
4718
4719 @item --output-delimiter=@var{output_delim_string}
4720 @opindex --output-delimiter
4721 With @option{-f}, output fields are separated by @var{output_delim_string}.
4722 The default with @option{-f} is to use the input delimiter.
4723 When using @option{-b} or @option{-c} to select ranges of byte or
4724 character offsets (as opposed to ranges of fields),
4725 output @var{output_delim_string} between non-overlapping
4726 ranges of selected bytes.
4727
4728 @item --complement
4729 @opindex --complement
4730 This option is a @acronym{GNU} extension.
4731 Select for printing the complement of the bytes, characters or fields
4732 selected with the @option{-b}, @option{-c} or @option{-f} options.
4733 In other words, do @emph{not} print the bytes, characters or fields
4734 specified via those options.  This option is useful when you have
4735 many fields and want to print all but a few of them.
4736
4737 @end table
4738
4739 @exitstatus
4740
4741
4742 @node paste invocation
4743 @section @command{paste}: Merge lines of files
4744
4745 @pindex paste
4746 @cindex merging files
4747
4748 @command{paste} writes to standard output lines consisting of sequentially
4749 corresponding lines of each given file, separated by a TAB character.
4750 Standard input is used for a file name of @samp{-} or if no input files
4751 are given.
4752
4753 For example:
4754
4755 @example
4756 $ cat num2
4757 1
4758 2
4759 $ cat let3
4760 a
4761 b
4762 c
4763 $ paste num2 let3
4764 1       a
4765 2       b
4766        @ c
4767 @end example
4768
4769 Synopsis:
4770
4771 @example
4772 paste [@var{option}]@dots{} [@var{file}]@dots{}
4773 @end example
4774
4775 The program accepts the following options.  Also see @ref{Common options}.
4776
4777 @table @samp
4778
4779 @item -s
4780 @itemx --serial
4781 @opindex -s
4782 @opindex --serial
4783 Paste the lines of one file at a time rather than one line from each
4784 file.  Using the above example data:
4785
4786 @example
4787 $ paste -s num2 let3
4788 1       2
4789 a       b       c
4790 @end example
4791
4792 @item -d @var{delim-list}
4793 @itemx --delimiters=@var{delim-list}
4794 @opindex -d
4795 @opindex --delimiters
4796 Consecutively use the characters in @var{delim-list} instead of
4797 TAB to separate merged lines.  When @var{delim-list} is
4798 exhausted, start again at its beginning.  Using the above example data:
4799
4800 @example
4801 $ paste -d '%_' num2 let3 num2
4802 1%a_1
4803 2%b_2
4804 %c_
4805 @end example
4806
4807 @end table
4808
4809 @exitstatus
4810
4811
4812 @node join invocation
4813 @section @command{join}: Join lines on a common field
4814
4815 @pindex join
4816 @cindex common field, joining on
4817
4818 @command{join} writes to standard output a line for each pair of input
4819 lines that have identical join fields.  Synopsis:
4820
4821 @example
4822 join [@var{option}]@dots{} @var{file1} @var{file2}
4823 @end example
4824
4825 Either @var{file1} or @var{file2} (but not both) can be @samp{-},
4826 meaning standard input.  @var{file1} and @var{file2} should be
4827 sorted on the join fields.
4828
4829 @vindex LC_COLLATE
4830 Normally, the sort order is that of the
4831 collating sequence specified by the @env{LC_COLLATE} locale.  Unless
4832 the @option{-t} option is given, the sort comparison ignores blanks at
4833 the start of the join field, as in @code{sort -b}.  If the
4834 @option{--ignore-case} option is given, the sort comparison ignores
4835 the case of characters in the join field, as in @code{sort -f}.
4836
4837 The @command{sort} and @command{join} commands should use consistent
4838 locales and options if the output of @command{sort} is fed to
4839 @command{join}.  You can use a command like @samp{sort -k 1b,1} to
4840 sort a file on its default join field, but if you select a non-default
4841 locale, join field, separator, or comparison options, then you should
4842 do so consistently between @command{join} and @command{sort}.
4843
4844 As a @acronym{GNU} extension, if the input has no unpairable lines the
4845 sort order can be any order that considers two fields to be equal if and
4846 only if the sort comparison described above considers them to be equal.
4847 For example:
4848
4849 @example
4850 $ cat file1
4851 a a1
4852 c c1
4853 b b1
4854 $ cat file2
4855 a a2
4856 c c2
4857 b b2
4858 $ join file1 file2
4859 a a1 a2
4860 c c1 c2
4861 b b1 b2
4862 @end example
4863
4864 The defaults are:
4865 @itemize
4866 @item the join field is the first field in each line;
4867 @item fields in the input are separated by one or more blanks, with leading
4868       blanks on the line ignored;
4869 @item fields in the output are separated by a space;
4870 @item each output line consists of the join field, the remaining
4871 fields from @var{file1}, then the remaining fields from @var{file2}.
4872 @end itemize
4873
4874 The program accepts the following options.  Also see @ref{Common options}.
4875
4876 @table @samp
4877
4878 @item -a @var{file-number}
4879 @opindex -a
4880 Print a line for each unpairable line in file @var{file-number} (either
4881 @samp{1} or @samp{2}), in addition to the normal output.
4882
4883 @item -e @var{string}
4884 @opindex -e
4885 Replace those output fields that are missing in the input with
4886 @var{string}.
4887
4888 @item -i
4889 @itemx --ignore-case
4890 @opindex -i
4891 @opindex --ignore-case
4892 Ignore differences in case when comparing keys.
4893 With this option, the lines of the input files must be ordered in the same way.
4894 Use @samp{sort -f} to produce this ordering.
4895
4896 @item -1 @var{field}
4897 @opindex -1
4898 Join on field @var{field} (a positive integer) of file 1.
4899
4900 @item -2 @var{field}
4901 @opindex -2
4902 Join on field @var{field} (a positive integer) of file 2.
4903
4904 @item -j @var{field}
4905 Equivalent to @option{-1 @var{field} -2 @var{field}}.
4906
4907 @item -o @var{field-list}
4908 Construct each output line according to the format in @var{field-list}.
4909 Each element in @var{field-list} is either the single character @samp{0} or
4910 has the form @var{m.n} where the file number, @var{m}, is @samp{1} or
4911 @samp{2} and @var{n} is a positive field number.
4912
4913 A field specification of @samp{0} denotes the join field.
4914 In most cases, the functionality of the @samp{0} field spec
4915 may be reproduced using the explicit @var{m.n} that corresponds
4916 to the join field.  However, when printing unpairable lines
4917 (using either of the @option{-a} or @option{-v} options), there is no way
4918 to specify the join field using @var{m.n} in @var{field-list}
4919 if there are unpairable lines in both files.
4920 To give @command{join} that functionality, @acronym{POSIX} invented the @samp{0}
4921 field specification notation.
4922
4923 The elements in @var{field-list}
4924 are separated by commas or blanks.
4925 Blank separators typically need to be quoted for the shell.  For
4926 example, the commands @samp{join -o 1.2,2.2} and @samp{join -o '1.2
4927 2.2'} are equivalent.
4928
4929 All output lines---including those printed because of any -a or -v
4930 option---are subject to the specified @var{field-list}.
4931
4932 @item -t @var{char}
4933 Use character @var{char} as the input and output field separator.
4934 Treat as significant each occurrence of @var{char} in the input file.
4935 Use @samp{sort -t @var{char}}, without the @option{-b} option of
4936 @samp{sort}, to produce this ordering.
4937
4938 @item -v @var{file-number}
4939 Print a line for each unpairable line in file @var{file-number}
4940 (either @samp{1} or @samp{2}), instead of the normal output.
4941
4942 @end table
4943
4944 @exitstatus
4945
4946
4947 @node Operating on characters
4948 @chapter Operating on characters
4949
4950 @cindex operating on characters
4951
4952 This commands operate on individual characters.
4953
4954 @menu
4955 * tr invocation::               Translate, squeeze, and/or delete characters.
4956 * expand invocation::           Convert tabs to spaces.
4957 * unexpand invocation::         Convert spaces to tabs.
4958 @end menu
4959
4960
4961 @node tr invocation
4962 @section @command{tr}: Translate, squeeze, and/or delete characters
4963
4964 @pindex tr
4965
4966 Synopsis:
4967
4968 @example
4969 tr [@var{option}]@dots{} @var{set1} [@var{set2}]
4970 @end example
4971
4972 @command{tr} copies standard input to standard output, performing
4973 one of the following operations:
4974
4975 @itemize @bullet
4976 @item
4977 translate, and optionally squeeze repeated characters in the result,
4978 @item
4979 squeeze repeated characters,
4980 @item
4981 delete characters,
4982 @item
4983 delete characters, then squeeze repeated characters from the result.
4984 @end itemize
4985
4986 The @var{set1} and (if given) @var{set2} arguments define ordered
4987 sets of characters, referred to below as @var{set1} and @var{set2}.  These
4988 sets are the characters of the input that @command{tr} operates on.
4989 The @option{--complement} (@option{-c}, @option{-C}) option replaces
4990 @var{set1} with its
4991 complement (all of the characters that are not in @var{set1}).
4992
4993 Currently @command{tr} fully supports only single-byte characters.
4994 Eventually it will support multibyte characters; when it does, the
4995 @option{-C} option will cause it to complement the set of characters,
4996 whereas @option{-c} will cause it to complement the set of values.
4997 This distinction will matter only when some values are not characters,
4998 and this is possible only in locales using multibyte encodings when
4999 the input contains encoding errors.
5000
5001 The program accepts the @option{--help} and @option{--version}
5002 options.  @xref{Common options}.  Options must precede operands.
5003
5004 @exitstatus
5005
5006 @menu
5007 * Character sets::              Specifying sets of characters.
5008 * Translating::                 Changing one set of characters to another.
5009 * Squeezing::                   Squeezing repeats and deleting.
5010 @end menu
5011
5012
5013 @node Character sets
5014 @subsection Specifying sets of characters
5015
5016 @cindex specifying sets of characters
5017
5018 The format of the @var{set1} and @var{set2} arguments resembles
5019 the format of regular expressions; however, they are not regular
5020 expressions, only lists of characters.  Most characters simply
5021 represent themselves in these strings, but the strings can contain
5022 the shorthands listed below, for convenience.  Some of them can be
5023 used only in @var{set1} or @var{set2}, as noted below.
5024
5025 @table @asis
5026
5027 @item Backslash escapes
5028 @cindex backslash escapes
5029
5030 The following backslash escape sequences are recognized:
5031
5032 @table @samp
5033 @item \a
5034 Control-G.
5035 @item \b
5036 Control-H.
5037 @item \f
5038 Control-L.
5039 @item \n
5040 Control-J.
5041 @item \r
5042 Control-M.
5043 @item \t
5044 Control-I.
5045 @item \v
5046 Control-K.
5047 @item \@var{ooo}
5048 The character with the value given by @var{ooo}, which is 1 to 3
5049 octal digits,
5050 @item \\
5051 A backslash.
5052 @end table
5053
5054 While a backslash followed by a character not listed above is
5055 interpreted as that character, the backslash also effectively
5056 removes any special significance, so it is useful to escape
5057 @samp{[}, @samp{]}, @samp{*}, and @samp{-}.
5058
5059 @item Ranges
5060 @cindex ranges
5061
5062 The notation @samp{@var{m}-@var{n}} expands to all of the characters
5063 from @var{m} through @var{n}, in ascending order.  @var{m} should
5064 collate before @var{n}; if it doesn't, an error results.  As an example,
5065 @samp{0-9} is the same as @samp{0123456789}.
5066
5067 @sc{gnu} @command{tr} does not support the System V syntax that uses square
5068 brackets to enclose ranges.  Translations specified in that format
5069 sometimes work as expected, since the brackets are often transliterated
5070 to themselves.  However, they should be avoided because they sometimes
5071 behave unexpectedly.  For example, @samp{tr -d '[0-9]'} deletes brackets
5072 as well as digits.
5073
5074 Many historically common and even accepted uses of ranges are not
5075 portable.  For example, on @acronym{EBCDIC} hosts using the @samp{A-Z}
5076 range will not do what most would expect because @samp{A} through @samp{Z}
5077 are not contiguous as they are in @acronym{ASCII}.
5078 If you can rely on a @acronym{POSIX} compliant version of @command{tr}, then
5079 the best way to work around this is to use character classes (see below).
5080 Otherwise, it is most portable (and most ugly) to enumerate the members
5081 of the ranges.
5082
5083 @item Repeated characters
5084 @cindex repeated characters
5085
5086 The notation @samp{[@var{c}*@var{n}]} in @var{set2} expands to @var{n}
5087 copies of character @var{c}.  Thus, @samp{[y*6]} is the same as
5088 @samp{yyyyyy}.  The notation @samp{[@var{c}*]} in @var{string2} expands
5089 to as many copies of @var{c} as are needed to make @var{set2} as long as
5090 @var{set1}.  If @var{n} begins with @samp{0}, it is interpreted in
5091 octal, otherwise in decimal.
5092
5093 @item Character classes
5094 @cindex character classes
5095
5096 The notation @samp{[:@var{class}:]} expands to all of the characters in
5097 the (predefined) class @var{class}.  The characters expand in no
5098 particular order, except for the @code{upper} and @code{lower} classes,
5099 which expand in ascending order.  When the @option{--delete} (@option{-d})
5100 and @option{--squeeze-repeats} (@option{-s}) options are both given, any
5101 character class can be used in @var{set2}.  Otherwise, only the
5102 character classes @code{lower} and @code{upper} are accepted in
5103 @var{set2}, and then only if the corresponding character class
5104 (@code{upper} and @code{lower}, respectively) is specified in the same
5105 relative position in @var{set1}.  Doing this specifies case conversion.
5106 The class names are given below; an error results when an invalid class
5107 name is given.
5108
5109 @table @code
5110 @item alnum
5111 @opindex alnum
5112 Letters and digits.
5113 @item alpha
5114 @opindex alpha
5115 Letters.
5116 @item blank
5117 @opindex blank
5118 Horizontal whitespace.
5119 @item cntrl
5120 @opindex cntrl
5121 Control characters.
5122 @item digit
5123 @opindex digit
5124 Digits.
5125 @item graph
5126 @opindex graph
5127 Printable characters, not including space.
5128 @item lower
5129 @opindex lower
5130 Lowercase letters.
5131 @item print
5132 @opindex print
5133 Printable characters, including space.
5134 @item punct
5135 @opindex punct
5136 Punctuation characters.
5137 @item space
5138 @opindex space
5139 Horizontal or vertical whitespace.
5140 @item upper
5141 @opindex upper
5142 Uppercase letters.
5143 @item xdigit
5144 @opindex xdigit
5145 Hexadecimal digits.
5146 @end table
5147
5148 @item Equivalence classes
5149 @cindex equivalence classes
5150
5151 The syntax @samp{[=@var{c}=]} expands to all of the characters that are
5152 equivalent to @var{c}, in no particular order.  Equivalence classes are
5153 a relatively recent invention intended to support non-English alphabets.
5154 But there seems to be no standard way to define them or determine their
5155 contents.  Therefore, they are not fully implemented in @sc{gnu} @command{tr};
5156 each character's equivalence class consists only of that character,
5157 which is of no particular use.
5158
5159 @end table
5160
5161
5162 @node Translating
5163 @subsection Translating
5164
5165 @cindex translating characters
5166
5167 @command{tr} performs translation when @var{set1} and @var{set2} are
5168 both given and the @option{--delete} (@option{-d}) option is not given.
5169 @command{tr} translates each character of its input that is in @var{set1}
5170 to the corresponding character in @var{set2}.  Characters not in
5171 @var{set1} are passed through unchanged.  When a character appears more
5172 than once in @var{set1} and the corresponding characters in @var{set2}
5173 are not all the same, only the final one is used.  For example, these
5174 two commands are equivalent:
5175
5176 @example
5177 tr aaa xyz
5178 tr a z
5179 @end example
5180
5181 A common use of @command{tr} is to convert lowercase characters to
5182 uppercase.  This can be done in many ways.  Here are three of them:
5183
5184 @example
5185 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
5186 tr a-z A-Z
5187 tr '[:lower:]' '[:upper:]'
5188 @end example
5189
5190 @noindent
5191 But note that using ranges like @code{a-z} above is not portable.
5192
5193 When @command{tr} is performing translation, @var{set1} and @var{set2}
5194 typically have the same length.  If @var{set1} is shorter than
5195 @var{set2}, the extra characters at the end of @var{set2} are ignored.
5196
5197 On the other hand, making @var{set1} longer than @var{set2} is not
5198 portable; @acronym{POSIX} says that the result is undefined.  In this situation,
5199 BSD @command{tr} pads @var{set2} to the length of @var{set1} by repeating
5200 the last character of @var{set2} as many times as necessary.  System V
5201 @command{tr} truncates @var{set1} to the length of @var{set2}.
5202
5203 By default, @sc{gnu} @command{tr} handles this case like BSD @command{tr}.
5204 When the @option{--truncate-set1} (@option{-t}) option is given,
5205 @sc{gnu} @command{tr} handles this case like the System V @command{tr}
5206 instead.  This option is ignored for operations other than translation.
5207
5208 Acting like System V @command{tr} in this case breaks the relatively common
5209 BSD idiom:
5210
5211 @example
5212 tr -cs A-Za-z0-9 '\012'
5213 @end example
5214
5215 @noindent
5216 because it converts only zero bytes (the first element in the
5217 complement of @var{set1}), rather than all non-alphanumerics, to
5218 newlines.
5219
5220 @noindent
5221 By the way, the above idiom is not portable because it uses ranges, and
5222 it assumes that the octal code for newline is 012.
5223 Assuming a @acronym{POSIX} compliant @command{tr}, here is a better way to write it:
5224
5225 @example
5226 tr -cs '[:alnum:]' '[\n*]'
5227 @end example
5228
5229
5230 @node Squeezing
5231 @subsection Squeezing repeats and deleting
5232
5233 @cindex squeezing repeat characters
5234 @cindex deleting characters
5235
5236 When given just the @option{--delete} (@option{-d}) option, @command{tr}
5237 removes any input characters that are in @var{set1}.
5238
5239 When given just the @option{--squeeze-repeats} (@option{-s}) option,
5240 @command{tr} replaces each input sequence of a repeated character that
5241 is in @var{set1} with a single occurrence of that character.
5242
5243 When given both @option{--delete} and @option{--squeeze-repeats}, @command{tr}
5244 first performs any deletions using @var{set1}, then squeezes repeats
5245 from any remaining characters using @var{set2}.
5246
5247 The @option{--squeeze-repeats} option may also be used when translating,
5248 in which case @command{tr} first performs translation, then squeezes
5249 repeats from any remaining characters using @var{set2}.
5250
5251 Here are some examples to illustrate various combinations of options:
5252
5253 @itemize @bullet
5254
5255 @item
5256 Remove all zero bytes:
5257
5258 @example
5259 tr -d '\0'
5260 @end example
5261
5262 @item
5263 Put all words on lines by themselves.  This converts all
5264 non-alphanumeric characters to newlines, then squeezes each string
5265 of repeated newlines into a single newline:
5266
5267 @example
5268 tr -cs '[:alnum:]' '[\n*]'
5269 @end example
5270
5271 @item
5272 Convert each sequence of repeated newlines to a single newline:
5273
5274 @example
5275 tr -s '\n'
5276 @end example
5277
5278 @item
5279 Find doubled occurrences of words in a document.
5280 @c Separate the following two "the"s, so typo checkers don't complain.
5281 For example, people often write ``the @w{}the'' with the repeated words
5282 separated by a newline.  The Bourne shell script below works first
5283 by converting each sequence of punctuation and blank characters to a
5284 single newline.  That puts each ``word'' on a line by itself.
5285 Next it maps all uppercase characters to lower case, and finally it
5286 runs @command{uniq} with the @option{-d} option to print out only the words
5287 that were repeated.
5288
5289 @example
5290 #!/bin/sh
5291 cat -- "$@@" \
5292   | tr -s '[:punct:][:blank:]' '[\n*]' \
5293   | tr '[:upper:]' '[:lower:]' \
5294   | uniq -d
5295 @end example
5296
5297 @item
5298 Deleting a small set of characters is usually straightforward.  For example,
5299 to remove all @samp{a}s, @samp{x}s, and @samp{M}s you would do this:
5300
5301 @example
5302 tr -d axM
5303 @end example
5304
5305 However, when @samp{-} is one of those characters, it can be tricky because
5306 @samp{-} has special meanings.  Performing the same task as above but also
5307 removing all @samp{-} characters, we might try @code{tr -d -axM}, but
5308 that would fail because @command{tr} would try to interpret @option{-a} as
5309 a command-line option.  Alternatively, we could try putting the hyphen
5310 inside the string, @code{tr -d a-xM}, but that wouldn't work either because
5311 it would make @command{tr} interpret @code{a-x} as the range of characters
5312 @samp{a}@dots{}@samp{x} rather than the three.
5313 One way to solve the problem is to put the hyphen at the end of the list
5314 of characters:
5315
5316 @example
5317 tr -d axM-
5318 @end example
5319
5320 Or you can use @samp{--} to terminate option processing:
5321
5322 @example
5323 tr -d -- -axM
5324 @end example
5325
5326 More generally, use the character class notation @code{[=c=]}
5327 with @samp{-} (or any other character) in place of the @samp{c}:
5328
5329 @example
5330 tr -d '[=-=]axM'
5331 @end example
5332
5333 Note how single quotes are used in the above example to protect the
5334 square brackets from interpretation by a shell.
5335
5336 @end itemize
5337
5338
5339 @node expand invocation
5340 @section @command{expand}: Convert tabs to spaces
5341
5342 @pindex expand
5343 @cindex tabs to spaces, converting
5344 @cindex converting tabs to spaces
5345
5346 @command{expand} writes the contents of each given @var{file}, or standard
5347 input if none are given or for a @var{file} of @samp{-}, to standard
5348 output, with tab characters converted to the appropriate number of
5349 spaces.  Synopsis:
5350
5351 @example
5352 expand [@var{option}]@dots{} [@var{file}]@dots{}
5353 @end example
5354
5355 By default, @command{expand} converts all tabs to spaces.  It preserves
5356 backspace characters in the output; they decrement the column count for
5357 tab calculations.  The default action is equivalent to @option{-t 8} (set
5358 tabs every 8 columns).
5359
5360 The program accepts the following options.  Also see @ref{Common options}.
5361
5362 @table @samp
5363
5364 @item -t @var{tab1}[,@var{tab2}]@dots{}
5365 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5366 @opindex -t
5367 @opindex --tabs
5368 @cindex tab stops, setting
5369 If only one tab stop is given, set the tabs @var{tab1} spaces apart
5370 (default is 8).  Otherwise, set the tabs at columns @var{tab1},
5371 @var{tab2}, @dots{} (numbered from 0), and replace any tabs beyond the
5372 last tab stop given with single spaces.  Tab stops can be separated by
5373 blanks as well as by commas.
5374
5375 For compatibility, GNU @command{expand} also accepts the obsolete
5376 option syntax, @option{-@var{t1}[,@var{t2}]@dots{}}.  New scripts
5377 should use @option{-t @var{t1}[,@var{t2}]@dots{}} instead.
5378
5379 @item -i
5380 @itemx --initial
5381 @opindex -i
5382 @opindex --initial
5383 @cindex initial tabs, converting
5384 Only convert initial tabs (those that precede all non-space or non-tab
5385 characters) on each line to spaces.
5386
5387 @end table
5388
5389 @exitstatus
5390
5391
5392 @node unexpand invocation
5393 @section @command{unexpand}: Convert spaces to tabs
5394
5395 @pindex unexpand
5396
5397 @command{unexpand} writes the contents of each given @var{file}, or
5398 standard input if none are given or for a @var{file} of @samp{-}, to
5399 standard output, converting blanks at the beginning of each line into
5400 as many tab characters as needed.  In the default @acronym{POSIX}
5401 locale, a @dfn{blank} is a space or a tab; other locales may specify
5402 additional blank characters.  Synopsis:
5403
5404 @example
5405 unexpand [@var{option}]@dots{} [@var{file}]@dots{}
5406 @end example
5407
5408 By default, @command{unexpand} converts only initial blanks (those
5409 that precede all non-blank characters) on each line.  It
5410 preserves backspace characters in the output; they decrement the column
5411 count for tab calculations.  By default, tabs are set at every 8th
5412 column.
5413
5414 The program accepts the following options.  Also see @ref{Common options}.
5415
5416 @table @samp
5417
5418 @item -t @var{tab1}[,@var{tab2}]@dots{}
5419 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5420 @opindex -t
5421 @opindex --tabs
5422 If only one tab stop is given, set the tabs @var{tab1} columns apart
5423 instead of the default 8.  Otherwise, set the tabs at columns
5424 @var{tab1}, @var{tab2}, @dots{} (numbered from 0), and leave blanks
5425 beyond the tab stops given unchanged.  Tab stops can be separated by
5426 blanks as well as by commas.  This option implies the @option{-a} option.
5427
5428 For compatibility, GNU @command{unexpand} supports the obsolete option syntax,
5429 @option{-@var{tab1}[,@var{tab2}]@dots{}}, where tab stops must be
5430 separated by commas.  (Unlike @option{-t}, this obsolete option does
5431 not imply @option{-a}.)  New scripts should use @option{--first-only -t
5432 @var{tab1}[,@var{tab2}]@dots{}} instead.
5433
5434 @item -a
5435 @itemx --all
5436 @opindex -a
5437 @opindex --all
5438 Also convert all sequences of two or more blanks just before a tab stop,
5439 even if they occur after non-blank characters in a line.
5440
5441 @end table
5442
5443 @exitstatus
5444
5445
5446 @node Directory listing
5447 @chapter Directory listing
5448
5449 This chapter describes the @command{ls} command and its variants @command{dir}
5450 and @command{vdir}, which list information about files.
5451
5452 @menu
5453 * ls invocation::               List directory contents.
5454 * dir invocation::              Briefly ls.
5455 * vdir invocation::             Verbosely ls.
5456 * dircolors invocation::        Color setup for ls, etc.
5457 @end menu
5458
5459
5460 @node ls invocation
5461 @section @command{ls}: List directory contents
5462
5463 @pindex ls
5464 @cindex directory listing
5465
5466 The @command{ls} program lists information about files (of any type,
5467 including directories).  Options and file arguments can be intermixed
5468 arbitrarily, as usual.
5469
5470 For non-option command-line arguments that are directories, by default
5471 @command{ls} lists the contents of directories, not recursively, and
5472 omitting files with names beginning with @samp{.}.  For other non-option
5473 arguments, by default @command{ls} lists just the file name.  If no
5474 non-option argument is specified, @command{ls} operates on the current
5475 directory, acting as if it had been invoked with a single argument of @samp{.}.
5476
5477 @vindex LC_ALL
5478 By default, the output is sorted alphabetically, according to the locale
5479 settings in effect.@footnote{If you use a non-@acronym{POSIX}
5480 locale (e.g., by setting @env{LC_ALL} to @samp{en_US}), then @command{ls} may
5481 produce output that is sorted differently than you're accustomed to.
5482 In that case, set the @env{LC_ALL} environment variable to @samp{C}.}
5483 If standard output is
5484 a terminal, the output is in columns (sorted vertically) and control
5485 characters are output as question marks; otherwise, the output is listed
5486 one per line and control characters are output as-is.
5487
5488 Because @command{ls} is such a fundamental program, it has accumulated many
5489 options over the years.  They are described in the subsections below;
5490 within each section, options are listed alphabetically (ignoring case).
5491 The division of options into the subsections is not absolute, since some
5492 options affect more than one aspect of @command{ls}'s operation.
5493
5494 @cindex exit status of @command{ls}
5495 Exit status:
5496
5497 @display
5498 0 success
5499 1 minor problems (e.g., a subdirectory was not found)
5500 2 serious trouble (e.g., memory exhausted)
5501 @end display
5502
5503 Also see @ref{Common options}.
5504
5505 @menu
5506 * Which files are listed::
5507 * What information is listed::
5508 * Sorting the output::
5509 * More details about version sort::
5510 * General output formatting::
5511 * Formatting file timestamps::
5512 * Formatting the file names::
5513 @end menu
5514
5515
5516 @node Which files are listed
5517 @subsection Which files are listed
5518
5519 These options determine which files @command{ls} lists information for.
5520 By default, @command{ls} lists files and the contents of any
5521 directories on the command line, except that in directories it ignores
5522 files whose names start with @samp{.}.
5523
5524 @table @samp
5525
5526 @item -a
5527 @itemx --all
5528 @opindex -a
5529 @opindex --all
5530 In directories, do not ignore file names that start with @samp{.}.
5531
5532 @item -A
5533 @itemx --almost-all
5534 @opindex -A
5535 @opindex --almost-all
5536 In directories, do not ignore all file names that start with @samp{.};
5537 ignore only @file{.} and @file{..}.  The @option{--all} (@option{-a})
5538 option overrides this option.
5539
5540 @item -B
5541 @itemx --ignore-backups
5542 @opindex -B
5543 @opindex --ignore-backups
5544 @cindex backup files, ignoring
5545 In directories, ignore files that end with @samp{~}.  This option is
5546 equivalent to @samp{--ignore='*~' --ignore='.*~'}.
5547
5548 @item -d
5549 @itemx --directory
5550 @opindex -d
5551 @opindex --directory
5552 List just the names of directories, as with other types of files, rather
5553 than listing their contents.
5554 @c The following sentence is the same as the one for -F.
5555 Do not follow symbolic links listed on the
5556 command line unless the @option{--dereference-command-line} (@option{-H}),
5557 @option{--dereference} (@option{-L}), or
5558 @option{--dereference-command-line-symlink-to-dir} options are specified.
5559
5560 @item -H
5561 @itemx --dereference-command-line
5562 @opindex -H
5563 @opindex --dereference-command-line
5564 @cindex symbolic links, dereferencing
5565 If a command line argument specifies a symbolic link, show information
5566 for the file the link references rather than for the link itself.
5567
5568 @itemx --dereference-command-line-symlink-to-dir
5569 @opindex --dereference-command-line-symlink-to-dir
5570 @cindex symbolic links, dereferencing
5571 Do not dereference symbolic links, with one exception:
5572 if a command line argument specifies a symbolic link that refers to
5573 a directory, show information for that directory rather than for the
5574 link itself.
5575 This is the default behavior when no other dereferencing-related
5576 option has been specified (@option{--classify} (@option{-F}),
5577 @option{--directory} (@option{-d}),
5578 (@option{-l}),
5579 @option{--dereference} (@option{-L}), or
5580 @option{--dereference-command-line} (@option{-H})).
5581
5582 @item --group-directories-first
5583 @opindex --group-directories-first
5584 Group all the directories before the files and then sort the
5585 directories and the files separately using the selected sort key
5586 (see --sort option).
5587 That is, this option specifies a primary sort key,
5588 and the --sort option specifies a secondary key.
5589
5590 @item --hide=PATTERN
5591 @opindex --hide=@var{pattern}
5592 In directories, ignore files whose names match the shell pattern
5593 @var{pattern}, unless the @option{--all} (@option{-a}) or
5594 @option{--almost-all} (@option{-A}) is also given.  This
5595 option acts like @option{--ignore=@var{pattern}} except that it has no
5596 effect if @option{--all} (@option{-a}) or @option{--almost-all}
5597 (@option{-A}) is also given.
5598
5599 This option can be useful in shell aliases.  For example, if
5600 @command{lx} is an alias for @samp{ls --hide='*~'} and @command{ly} is
5601 an alias for @samp{ls --ignore='*~'}, then the command @samp{lx -A}
5602 lists the file @file{README~} even though @samp{ly -A} would not.
5603
5604 @item -I @var{pattern}
5605 @itemx --ignore=@var{pattern}
5606 @opindex -I
5607 @opindex --ignore=@var{pattern}
5608 In directories, ignore files whose names match the shell pattern
5609 (not regular expression) @var{pattern}.  As
5610 in the shell, an initial @samp{.} in a file name does not match a
5611 wildcard at the start of @var{pattern}.  Sometimes it is useful
5612 to give this option several times.  For example,
5613
5614 @smallexample
5615 $ ls --ignore='.??*' --ignore='.[^.]' --ignore='#*'
5616 @end smallexample
5617
5618 The first option ignores names of length 3 or more that start with @samp{.},
5619 the second ignores all two-character names that start with @samp{.}
5620 except @samp{..}, and the third ignores names that start with @samp{#}.
5621
5622 @item -L
5623 @itemx --dereference
5624 @opindex -L
5625 @opindex --dereference
5626 @cindex symbolic links, dereferencing
5627 When showing file information for a symbolic link, show information
5628 for the file the link references rather than the link itself.
5629 However, even with this option, @command{ls} still prints the name
5630 of the link itself, not the name of the file that the link points to.
5631
5632 @item -R
5633 @itemx --recursive
5634 @opindex -R
5635 @opindex --recursive
5636 @cindex recursive directory listing
5637 @cindex directory listing, recursive
5638 List the contents of all directories recursively.
5639
5640 @end table
5641
5642
5643 @node What information is listed
5644 @subsection What information is listed
5645
5646 These options affect the information that @command{ls} displays.  By
5647 default, only file names are shown.
5648
5649 @table @samp
5650
5651 @item --author
5652 @opindex --author
5653 @cindex hurd, author, printing
5654 List each file's author when producing long format directory listings.
5655 In GNU/Hurd, file authors can differ from their owners, but in other
5656 operating systems the two are the same.
5657
5658 @item -D
5659 @itemx --dired
5660 @opindex -D
5661 @opindex --dired
5662 @cindex dired Emacs mode support
5663 With the long listing (@option{-l}) format, print an additional line after
5664 the main output:
5665
5666 @example
5667 //DIRED// @var{beg1} @var{end1} @var{beg2} @var{end2} @dots{}
5668 @end example
5669
5670 @noindent
5671 The @var{begN} and @var{endN} are unsigned integers that record the
5672 byte position of the beginning and end of each file name in the output.
5673 This makes it easy for Emacs to find the names, even when they contain
5674 unusual characters such as space or newline, without fancy searching.
5675
5676 If directories are being listed recursively (@option{-R}), output a similar
5677 line with offsets for each subdirectory name:
5678
5679 @example
5680 //SUBDIRED// @var{beg1} @var{end1} @dots{}
5681 @end example
5682
5683 Finally, output a line of the form:
5684
5685 @example
5686 //DIRED-OPTIONS// --quoting-style=@var{word}
5687 @end example
5688
5689 @noindent
5690 where @var{word} is the quoting style (@pxref{Formatting the file names}).
5691
5692 Here is an actual example:
5693
5694 @example
5695 $ mkdir -p a/sub/deeper a/sub2
5696 $ touch a/f1 a/f2
5697 $ touch a/sub/deeper/file
5698 $ ls -gloRF --dired a
5699   a:
5700   total 8
5701   -rw-r--r-- 1    0 Jun 10 12:27 f1
5702   -rw-r--r-- 1    0 Jun 10 12:27 f2
5703   drwxr-xr-x 3 4096 Jun 10 12:27 sub/
5704   drwxr-xr-x 2 4096 Jun 10 12:27 sub2/
5705
5706   a/sub:
5707   total 4
5708   drwxr-xr-x 2 4096 Jun 10 12:27 deeper/
5709
5710   a/sub/deeper:
5711   total 0
5712   -rw-r--r-- 1 0 Jun 10 12:27 file
5713
5714   a/sub2:
5715   total 0
5716 //DIRED// 48 50 84 86 120 123 158 162 217 223 282 286
5717 //SUBDIRED// 2 3 167 172 228 240 290 296
5718 //DIRED-OPTIONS// --quoting-style=literal
5719 @end example
5720
5721 Note that the pairs of offsets on the @samp{//DIRED//} line above delimit
5722 these names: @file{f1}, @file{f2}, @file{sub}, @file{sub2}, @file{deeper},
5723 @file{file}.
5724 The offsets on the @samp{//SUBDIRED//} line delimit the following
5725 directory names: @file{a}, @file{a/sub}, @file{a/sub/deeper}, @file{a/sub2}.
5726
5727 Here is an example of how to extract the fifth entry name, @samp{deeper},
5728 corresponding to the pair of offsets, 222 and 228:
5729
5730 @example
5731 $ ls -gloRF --dired a > out
5732 $ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
5733 deeper
5734 @end example
5735
5736 Note that although the listing above includes a trailing slash
5737 for the @samp{deeper} entry, the offsets select the name without
5738 the trailing slash.  However, if you invoke @command{ls} with @option{--dired}
5739 along with an option like @option{--escape} (aka @option{-b}) and operate
5740 on a file whose name contains special characters, notice that the backslash
5741 @emph{is} included:
5742
5743 @example
5744 $ touch 'a b'
5745 $ ls -blog --dired 'a b'
5746   -rw-r--r-- 1 0 Jun 10 12:28 a\ b
5747 //DIRED// 30 34
5748 //DIRED-OPTIONS// --quoting-style=escape
5749 @end example
5750
5751 If you use a quoting style that adds quote marks
5752 (e.g., @option{--quoting-style=c}), then the offsets include the quote marks.
5753 So beware that the user may select the quoting style via the environment
5754 variable @env{QUOTING_STYLE}.  Hence, applications using @option{--dired}
5755 should either specify an explicit @option{--quoting-style=literal} option
5756 (aka @option{-N} or @option{--literal}) on the command line, or else be
5757 prepared to parse the escaped names.
5758
5759 @item --full-time
5760 @opindex --full-time
5761 Produce long format directory listings, and list times in full.  It is
5762 equivalent to using @option{--format=long} with
5763 @option{--time-style=full-iso} (@pxref{Formatting file timestamps}).
5764
5765 @item -g
5766 @opindex -g
5767 Produce long format directory listings, but don't display owner information.
5768
5769 @item -G
5770 @itemx --no-group
5771 @opindex -G
5772 @opindex --no-group
5773 Inhibit display of group information in a long format directory listing.
5774 (This is the default in some non-@sc{gnu} versions of @command{ls}, so we
5775 provide this option for compatibility.)
5776
5777 @optHumanReadable
5778
5779 @item -i
5780 @itemx --inode
5781 @opindex -i
5782 @opindex --inode
5783 @cindex inode number, printing
5784 Print the inode number (also called the file serial number and index
5785 number) of each file to the left of the file name.  (This number
5786 uniquely identifies each file within a particular file system.)
5787
5788 @item -l
5789 @itemx --format=long
5790 @itemx --format=verbose
5791 @opindex -l
5792 @opindex --format
5793 @opindex long ls @r{format}
5794 @opindex verbose ls @r{format}
5795 In addition to the name of each file, print the file type, file mode bits,
5796 number of hard links, owner name, group name, size, and
5797 timestamp (@pxref{Formatting file timestamps}), normally
5798 the modification time.
5799
5800 Normally the size is printed as a byte count without punctuation, but
5801 this can be overridden (@pxref{Block size}).  For example, @option{-h}
5802 prints an abbreviated, human-readable count, and
5803 @samp{--block-size="'1"} prints a byte count with the thousands
5804 separator of the current locale.
5805
5806 For each directory that is listed, preface the files with a line
5807 @samp{total @var{blocks}}, where @var{blocks} is the total disk allocation
5808 for all files in that directory.  The block size currently defaults to 1024
5809 bytes, but this can be overridden (@pxref{Block size}).
5810 The @var{blocks} computed counts each hard link separately;
5811 this is arguably a deficiency.
5812
5813 The file type is one of the following characters:
5814
5815 @c The commented-out entries are ones we're not sure about.
5816
5817 @table @samp
5818 @item -
5819 regular file
5820 @item b
5821 block special file
5822 @item c
5823 character special file
5824 @item C
5825 high performance (``contiguous data'') file
5826 @item d
5827 directory
5828 @item D
5829 door (Solaris 2.5 and up)
5830 @c @item F
5831 @c semaphore, if this is a distinct file type
5832 @item l
5833 symbolic link
5834 @c @item m
5835 @c multiplexed file (7th edition Unix; obsolete)
5836 @item M
5837 off-line (``migrated'') file (Cray DMF)
5838 @item n
5839 network special file (HP-UX)
5840 @item p
5841 FIFO (named pipe)
5842 @item P
5843 port (Solaris 10 and up)
5844 @c @item Q
5845 @c message queue, if this is a distinct file type
5846 @item s
5847 socket
5848 @c @item S
5849 @c shared memory object, if this is a distinct file type
5850 @c @item T
5851 @c typed memory object, if this is a distinct file type
5852 @c @item w
5853 @c whiteout (4.4BSD; not implemented)
5854 @item ?
5855 some other file type
5856 @end table
5857
5858 @cindex permissions, output by @command{ls}
5859 The file mode bits listed are similar to symbolic mode specifications
5860 (@pxref{Symbolic Modes}).  But @command{ls} combines multiple bits into the
5861 third character of each set of permissions as follows:
5862
5863 @table @samp
5864 @item s
5865 If the set-user-ID or set-group-ID bit and the corresponding executable bit
5866 are both set.
5867
5868 @item S
5869 If the set-user-ID or set-group-ID bit is set but the corresponding
5870 executable bit is not set.
5871
5872 @item t
5873 If the restricted deletion flag or sticky bit, and the
5874 other-executable bit, are both set.  The restricted deletion flag is
5875 another name for the sticky bit.  @xref{Mode Structure}.
5876
5877 @item T
5878 If the restricted deletion flag or sticky bit is set but the
5879 other-executable bit is not set.
5880
5881 @item x
5882 If the executable bit is set and none of the above apply.
5883
5884 @item -
5885 Otherwise.
5886 @end table
5887
5888 Following the file mode bits is a single character that specifies
5889 whether an alternate access method such as an access control list
5890 applies to the file.  When the character following the file mode bits is a
5891 space, there is no alternate access method.  When it is a printing
5892 character, then there is such a method.
5893
5894 For a file with an extended access control list, a @samp{+} character is
5895 listed.  Basic access control lists are equivalent to the permissions
5896 listed, and are not considered an alternate access method.
5897
5898 @item -n
5899 @itemx --numeric-uid-gid
5900 @opindex -n
5901 @opindex --numeric-uid-gid
5902 @cindex numeric uid and gid
5903 @cindex numeric user and group IDs
5904 Produce long format directory listings, but
5905 display numeric user and group IDs instead of the owner and group names.
5906
5907 @item -o
5908 @opindex -o
5909 Produce long format directory listings, but don't display group information.
5910 It is equivalent to using @option{--format=long} with @option{--no-group} .
5911
5912 @item -s
5913 @itemx --size
5914 @opindex -s
5915 @opindex --size
5916 @cindex disk allocation
5917 @cindex size of files, reporting
5918 Print the disk allocation of each file to the left of the file name.
5919 This is the amount of disk space used by the file, which is usually a
5920 bit more than the file's size, but it can be less if the file has holes.
5921
5922 Normally the disk allocation is printed in units of
5923 1024 bytes, but this can be overridden (@pxref{Block size}).
5924
5925 @cindex NFS mounts from BSD to HP-UX
5926 For files that are NFS-mounted from an HP-UX system to a BSD system,
5927 this option reports sizes that are half the correct values.  On HP-UX
5928 systems, it reports sizes that are twice the correct values for files
5929 that are NFS-mounted from BSD systems.  This is due to a flaw in HP-UX;
5930 it also affects the HP-UX @command{ls} program.
5931
5932 @optSi
5933
5934 @end table
5935
5936
5937 @node Sorting the output
5938 @subsection Sorting the output
5939
5940 @cindex sorting @command{ls} output
5941 These options change the order in which @command{ls} sorts the information
5942 it outputs.  By default, sorting is done by character code
5943 (e.g., @acronym{ASCII} order).
5944
5945 @table @samp
5946
5947 @item -c
5948 @itemx --time=ctime
5949 @itemx --time=status
5950 @opindex -c
5951 @opindex --time
5952 @opindex ctime@r{, printing or sorting by}
5953 @opindex status time@r{, printing or sorting by}
5954 @opindex use time@r{, printing or sorting files by}
5955 If the long listing format (e.g., @option{-l}, @option{-o}) is being used,
5956 print the status change time (the @samp{ctime} in the inode) instead of
5957 the modification time.
5958 When explicitly sorting by time (@option{--sort=time} or @option{-t})
5959 or when not using a long listing format,
5960 sort according to the status change time.
5961
5962 @item -f
5963 @opindex -f
5964 @cindex unsorted directory listing
5965 @cindex directory order, listing by
5966 Primarily, like @option{-U}---do not sort; list the files in whatever
5967 order they are stored in the directory.  But also enable @option{-a} (list
5968 all files) and disable @option{-l}, @option{--color}, and @option{-s} (if they
5969 were specified before the @option{-f}).
5970
5971 @item -r
5972 @itemx --reverse
5973 @opindex -r
5974 @opindex --reverse
5975 @cindex reverse sorting
5976 Reverse whatever the sorting method is---e.g., list files in reverse
5977 alphabetical order, youngest first, smallest first, or whatever.
5978
5979 @item -S
5980 @itemx --sort=size
5981 @opindex -S
5982 @opindex --sort
5983 @opindex size of files@r{, sorting files by}
5984 Sort by file size, largest first.
5985
5986 @item -t
5987 @itemx --sort=time
5988 @opindex -t
5989 @opindex --sort
5990 @opindex modification time@r{, sorting files by}
5991 Sort by modification time (the @samp{mtime} in the inode), newest first.
5992
5993 @item -u
5994 @itemx --time=atime
5995 @itemx --time=access
5996 @itemx --time=use
5997 @opindex -u
5998 @opindex --time
5999 @opindex use time@r{, printing or sorting files by}
6000 @opindex atime@r{, printing or sorting files by}
6001 @opindex access time@r{, printing or sorting files by}
6002 If the long listing format (e.g., @option{--format=long}) is being used,
6003 print the last access time (the @samp{atime} in the inode).
6004 When explicitly sorting by time (@option{--sort=time} or @option{-t})
6005 or when not using a long listing format, sort according to the access time.
6006
6007 @item -U
6008 @itemx --sort=none
6009 @opindex -U
6010 @opindex --sort
6011 @opindex none@r{, sorting option for @command{ls}}
6012 Do not sort; list the files in whatever order they are
6013 stored in the directory.  (Do not do any of the other unrelated things
6014 that @option{-f} does.)  This is especially useful when listing very large
6015 directories, since not doing any sorting can be noticeably faster.
6016
6017 @item -v
6018 @itemx --sort=version
6019 @opindex -v
6020 @opindex --sort
6021 @opindex version@r{, sorting option for @command{ls}}
6022 Sort by version name and number, lowest first.  It behaves like a default
6023 sort, except that each sequence of decimal digits is treated numerically
6024 as an index/version number.  (@xref{More details about version sort}.)
6025
6026 @item -X
6027 @itemx --sort=extension
6028 @opindex -X
6029 @opindex --sort
6030 @opindex extension@r{, sorting files by}
6031 Sort directory contents alphabetically by file extension (characters
6032 after the last @samp{.}); files with no extension are sorted first.
6033
6034 @end table
6035
6036
6037 @node More details about version sort
6038 @subsection More details about version sort
6039
6040 The version sort takes into account the fact that file names frequently include
6041 indices or version numbers.  Standard sorting functions usually do not produce
6042 the ordering that people expect because comparisons are made on a
6043 character-by-character basis.  The version
6044 sort addresses this problem, and is especially useful when browsing
6045 directories that contain many files with indices/version numbers in their
6046 names:
6047
6048 @example
6049 $ ls -1            $ ls -1v
6050 foo.zml-1.gz       foo.zml-1.gz
6051 foo.zml-100.gz     foo.zml-2.gz
6052 foo.zml-12.gz      foo.zml-6.gz
6053 foo.zml-13.gz      foo.zml-12.gz
6054 foo.zml-2.gz       foo.zml-13.gz
6055 foo.zml-25.gz      foo.zml-25.gz
6056 foo.zml-6.gz       foo.zml-100.gz
6057 @end example
6058
6059 Note also that numeric parts with leading zeros are considered as
6060 fractional one:
6061
6062 @example
6063 $ ls -1            $ ls -1v
6064 abc-1.007.tgz      abc-1.007.tgz
6065 abc-1.012b.tgz     abc-1.01a.tgz
6066 abc-1.01a.tgz      abc-1.012b.tgz
6067 @end example
6068
6069 This functionality is implemented using the @code{strverscmp} function.
6070 @xref{String/Array Comparison, , , libc, The GNU C Library Reference Manual}.
6071 One result of that implementation decision is that @code{ls -v} does not
6072 use the locale category, @env{LC_COLLATE}.  As a result, non-numeric prefixes
6073 are sorted as if @env{LC_COLLATE} were set to @code{C}.
6074
6075 @node General output formatting
6076 @subsection General output formatting
6077
6078 These options affect the appearance of the overall output.
6079
6080 @table @samp
6081
6082 @item -1
6083 @itemx --format=single-column
6084 @opindex -1
6085 @opindex --format
6086 @opindex single-column @r{output of files}
6087 List one file per line.  This is the default for @command{ls} when standard
6088 output is not a terminal.
6089
6090 @item -C
6091 @itemx --format=vertical
6092 @opindex -C
6093 @opindex --format
6094 @opindex vertical @r{sorted files in columns}
6095 List files in columns, sorted vertically.  This is the default for
6096 @command{ls} if standard output is a terminal.  It is always the default
6097 for the @command{dir} program.
6098 @sc{gnu} @command{ls} uses variable width columns to display as many files as
6099 possible in the fewest lines.
6100
6101 @item --color [=@var{when}]
6102 @opindex --color
6103 @cindex color, distinguishing file types with
6104 Specify whether to use color for distinguishing file types.  @var{when}
6105 may be omitted, or one of:
6106 @itemize @bullet
6107 @item none
6108 @vindex none @r{color option}
6109 - Do not use color at all.  This is the default.
6110 @item auto
6111 @vindex auto @r{color option}
6112 @cindex terminal, using color iff
6113 - Only use color if standard output is a terminal.
6114 @item always
6115 @vindex always @r{color option}
6116 - Always use color.
6117 @end itemize
6118 Specifying @option{--color} and no @var{when} is equivalent to
6119 @option{--color=always}.
6120 Piping a colorized listing through a pager like @command{more} or
6121 @command{less} usually produces unreadable results.  However, using
6122 @code{more -f} does seem to work.
6123
6124 @item -F
6125 @itemx --classify
6126 @itemx --indicator-style=classify
6127 @opindex -F
6128 @opindex --classify
6129 @opindex --indicator-style
6130 @cindex file type and executables, marking
6131 @cindex executables and file type, marking
6132 Append a character to each file name indicating the file type.  Also,
6133 for regular files that are executable, append @samp{*}.  The file type
6134 indicators are @samp{/} for directories, @samp{@@} for symbolic links,
6135 @samp{|} for FIFOs, @samp{=} for sockets, @samp{>} for doors,
6136 and nothing for regular files.
6137 @c The following sentence is the same as the one for -d.
6138 Do not follow symbolic links listed on the
6139 command line unless the @option{--dereference-command-line} (@option{-H}),
6140 @option{--dereference} (@option{-L}), or
6141 @option{--dereference-command-line-symlink-to-dir} options are specified.
6142
6143 @item --file-type
6144 @itemx --indicator-style=file-type
6145 @opindex --file-type
6146 @opindex --indicator-style
6147 @cindex file type, marking
6148 Append a character to each file name indicating the file type.  This is
6149 like @option{-F}, except that executables are not marked.
6150
6151 @item --indicator-style=@var{word}
6152 @opindex --indicator-style
6153 Append a character indicator with style @var{word} to entry names,
6154 as follows:
6155
6156 @table @samp
6157 @item none
6158 Do not append any character indicator; this is the default.
6159 @item slash
6160 Append @samp{/} for directories.  This is the same as the @option{-p}
6161 option.
6162 @item file-type
6163 Append @samp{/} for directories, @samp{@@} for symbolic links, @samp{|}
6164 for FIFOs, @samp{=} for sockets, and nothing for regular files.  This is
6165 the same as the @option{--file-type} option.
6166 @item classify
6167 Append @samp{*} for executable regular files, otherwise behave as for
6168 @samp{file-type}.  This is the same as the @option{-F} or
6169 @option{--classify} option.
6170 @end table
6171
6172 @item -k
6173 @opindex -k
6174 Print file sizes in 1024-byte blocks, overriding the default block
6175 size (@pxref{Block size}).
6176 This option is equivalent to @option{--block-size=1K}.
6177
6178 @item -m
6179 @itemx --format=commas
6180 @opindex -m
6181 @opindex --format
6182 @opindex commas@r{, outputting between files}
6183 List files horizontally, with as many as will fit on each line,
6184 separated by @samp{, } (a comma and a space).
6185
6186 @item -p
6187 @itemx --indicator-style=slash
6188 @opindex -p
6189 @opindex --indicator-style
6190 @cindex file type, marking
6191 Append a @samp{/} to directory names.
6192
6193 @item -x
6194 @itemx --format=across
6195 @itemx --format=horizontal
6196 @opindex -x
6197 @opindex --format
6198 @opindex across@r{, listing files}
6199 @opindex horizontal@r{, listing files}
6200 List the files in columns, sorted horizontally.
6201
6202 @item -T @var{cols}
6203 @itemx --tabsize=@var{cols}
6204 @opindex -T
6205 @opindex --tabsize
6206 Assume that each tab stop is @var{cols} columns wide.  The default is 8.
6207 @command{ls} uses tabs where possible in the output, for efficiency.  If
6208 @var{cols} is zero, do not use tabs at all.
6209
6210 @item -w
6211 @itemx --width=@var{cols}
6212 @opindex -w
6213 @opindex --width
6214 @vindex COLUMNS
6215 Assume the screen is @var{cols} columns wide.  The default is taken
6216 from the terminal settings if possible; otherwise the environment
6217 variable @env{COLUMNS} is used if it is set; otherwise the default
6218 is 80.
6219
6220 @end table
6221
6222
6223 @node Formatting file timestamps
6224 @subsection Formatting file timestamps
6225
6226 By default, file timestamps are listed in abbreviated form.  Most
6227 locales use a timestamp like @samp{2002-03-30 23:45}.  However, the
6228 default @acronym{POSIX} locale uses a date like @samp{Mar 30@ @ 2002}
6229 for non-recent timestamps, and a date-without-year and time like
6230 @samp{Mar 30 23:45} for recent timestamps.
6231
6232 A timestamp is considered to be @dfn{recent} if it is less than six
6233 months old, and is not dated in the future.  If a timestamp dated
6234 today is not listed in recent form, the timestamp is in the future,
6235 which means you probably have clock skew problems which may break
6236 programs like @command{make} that rely on file timestamps.
6237
6238 @vindex TZ
6239 Time stamps are listed according to the time zone rules specified by
6240 the @env{TZ} environment variable, or by the system default rules if
6241 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
6242 with @env{TZ}, libc, The GNU C Library}.
6243
6244 The following option changes how file timestamps are printed.
6245
6246 @table @samp
6247 @item --time-style=@var{style}
6248 @opindex --time-style
6249 @cindex time style
6250 List timestamps in style @var{style}.  The @var{style} should
6251 be one of the following:
6252
6253 @table @samp
6254 @item +@var{format}
6255 @vindex LC_TIME
6256 List timestamps using @var{format}, where @var{format} is interpreted
6257 like the format argument of @command{date} (@pxref{date invocation}).
6258 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
6259 @command{ls} to list timestamps like @samp{2002-03-30 23:45:56}.  As
6260 with @command{date}, @var{format}'s interpretation is affected by the
6261 @env{LC_TIME} locale category.
6262
6263 If @var{format} contains two format strings separated by a newline,
6264 the former is used for non-recent files and the latter for recent
6265 files; if you want output columns to line up, you may need to insert
6266 spaces in one of the two formats.
6267
6268 @item full-iso
6269 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
6270 format with nanosecond precision, e.g., @samp{2002-03-30
6271 23:45:56.477817180 -0700}.  This style is equivalent to
6272 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
6273
6274 This is useful because the time output includes all the information that
6275 is available from the operating system.  For example, this can help
6276 explain @command{make}'s behavior, since @acronym{GNU} @command{make}
6277 uses the full timestamp to determine whether a file is out of date.
6278
6279 @item long-iso
6280 List @acronym{ISO} 8601 date and time in minutes, e.g.,
6281 @samp{2002-03-30 23:45}.  These timestamps are shorter than
6282 @samp{full-iso} timestamps, and are usually good enough for everyday
6283 work.  This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
6284
6285 @item iso
6286 List @acronym{ISO} 8601 dates for non-recent timestamps (e.g.,
6287 @samp{2002-03-30@ }), and @acronym{ISO} 8601 month, day, hour, and
6288 minute for recent timestamps (e.g., @samp{03-30 23:45}).  These
6289 timestamps are uglier than @samp{long-iso} timestamps, but they carry
6290 nearly the same information in a smaller space and their brevity helps
6291 @command{ls} output fit within traditional 80-column output lines.
6292 The following two @command{ls} invocations are equivalent:
6293
6294 @example
6295 newline='
6296 '
6297 ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
6298 ls -l --time-style="iso"
6299 @end example
6300
6301 @item locale
6302 @vindex LC_TIME
6303 List timestamps in a locale-dependent form.  For example, a Finnish
6304 locale might list non-recent timestamps like @samp{maalis 30@ @ 2002}
6305 and recent timestamps like @samp{maalis 30 23:45}.  Locale-dependent
6306 timestamps typically consume more space than @samp{iso} timestamps and
6307 are harder for programs to parse because locale conventions vary so
6308 widely, but they are easier for many people to read.
6309
6310 The @env{LC_TIME} locale category specifies the timestamp format.  The
6311 default @acronym{POSIX} locale uses timestamps like @samp{Mar 30@
6312 @ 2002} and @samp{Mar 30 23:45}; in this locale, the following two
6313 @command{ls} invocations are equivalent:
6314
6315 @example
6316 newline='
6317 '
6318 ls -l --time-style="+%b %e  %Y$newline%b %e %H:%M"
6319 ls -l --time-style="locale"
6320 @end example
6321
6322 Other locales behave differently.  For example, in a German locale,
6323 @option{--time-style="locale"} might be equivalent to
6324 @option{--time-style="+%e. %b %Y $newline%e. %b %H:%M"}
6325 and might generate timestamps like @samp{30. M@"ar 2002@ } and
6326 @samp{30. M@"ar 23:45}.
6327
6328 @item posix-@var{style}
6329 @vindex LC_TIME
6330 List @acronym{POSIX}-locale timestamps if the @env{LC_TIME} locale
6331 category is @acronym{POSIX}, @var{style} timestamps otherwise.  For
6332 example, the @samp{posix-long-iso} style lists
6333 timestamps like @samp{Mar 30@ @ 2002} and @samp{Mar 30 23:45} when in
6334 the @acronym{POSIX} locale, and like @samp{2002-03-30 23:45} otherwise.
6335 @end table
6336 @end table
6337
6338 @vindex TIME_STYLE
6339 You can specify the default value of the @option{--time-style} option
6340 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
6341 the default style is @samp{locale}.  @acronym{GNU} Emacs 21.3 and
6342 later use the @option{--dired} option and therefore can parse any date
6343 format, but if you are using Emacs 21.1 or 21.2 and specify a
6344 non-@acronym{POSIX} locale you may need to set
6345 @samp{TIME_STYLE="posix-long-iso"}.
6346
6347 To avoid certain denial-of-service attacks, timestamps that would be
6348 longer than 1000 bytes may be treated as errors.
6349
6350
6351 @node Formatting the file names
6352 @subsection Formatting the file names
6353
6354 These options change how file names themselves are printed.
6355
6356 @table @samp
6357
6358 @item -b
6359 @itemx --escape
6360 @itemx --quoting-style=escape
6361 @opindex -b
6362 @opindex --escape
6363 @opindex --quoting-style
6364 @cindex backslash sequences for file names
6365 Quote nongraphic characters in file names using alphabetic and octal
6366 backslash sequences like those used in C.
6367
6368 @item -N
6369 @itemx --literal
6370 @itemx --quoting-style=literal
6371 @opindex -N
6372 @opindex --literal
6373 @opindex --quoting-style
6374 Do not quote file names.  However, with @command{ls} nongraphic
6375 characters are still printed as question marks if the output is a
6376 terminal and you do not specify the @option{--show-control-chars}
6377 option.
6378
6379 @item -q
6380 @itemx --hide-control-chars
6381 @opindex -q
6382 @opindex --hide-control-chars
6383 Print question marks instead of nongraphic characters in file names.
6384 This is the default if the output is a terminal and the program is
6385 @command{ls}.
6386
6387 @item -Q
6388 @itemx --quote-name
6389 @itemx --quoting-style=c
6390 @opindex -Q
6391 @opindex --quote-name
6392 @opindex --quoting-style
6393 Enclose file names in double quotes and quote nongraphic characters as
6394 in C.
6395
6396 @item --quoting-style=@var{word}
6397 @opindex --quoting-style
6398 @cindex quoting style
6399 Use style @var{word} to quote file names and other strings that may
6400 contain arbitrary characters.  The @var{word} should
6401 be one of the following:
6402
6403 @table @samp
6404 @item literal
6405 Output strings as-is; this is the same as the @option{-N} or
6406 @option{--literal} option.
6407 @item shell
6408 Quote strings for the shell if they contain shell metacharacters or would
6409 cause ambiguous output.
6410 The quoting is suitable for @acronym{POSIX}-compatible shells like
6411 @command{bash}, but it does not always work for incompatible shells
6412 like @command{csh}.
6413 @item shell-always
6414 Quote strings for the shell, even if they would normally not require quoting.
6415 @item c
6416 Quote strings as for C character string literals, including the
6417 surrounding double-quote characters; this is the same as the
6418 @option{-Q} or @option{--quote-name} option.
6419 @item escape
6420 Quote strings as for C character string literals, except omit the
6421 surrounding double-quote
6422 characters; this is the same as the @option{-b} or @option{--escape} option.
6423 @item clocale
6424 Quote strings as for C character string literals, except use
6425 surrounding quotation marks appropriate for the
6426 locale.
6427 @item locale
6428 @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
6429 Quote strings as for C character string literals, except use
6430 surrounding quotation marks appropriate for the locale, and quote
6431 @t{`like this'} instead of @t{"like
6432 this"} in the default C locale.  This looks nicer on many displays.
6433 @end table
6434
6435 You can specify the default value of the @option{--quoting-style} option
6436 with the environment variable @env{QUOTING_STYLE}.  If that environment
6437 variable is not set, the default value is @samp{literal}, but this
6438 default may change to @samp{shell} in a future version of this package.
6439
6440 @item --show-control-chars
6441 @opindex --show-control-chars
6442 Print nongraphic characters as-is in file names.
6443 This is the default unless the output is a terminal and the program is
6444 @command{ls}.
6445
6446 @end table
6447
6448
6449 @node dir invocation
6450 @section @command{dir}: Briefly list directory contents
6451
6452 @pindex dir
6453 @cindex directory listing, brief
6454
6455 @command{dir} is equivalent to @code{ls -C
6456 -b}; that is, by default files are listed in columns, sorted vertically,
6457 and special characters are represented by backslash escape sequences.
6458
6459 @xref{ls invocation, @command{ls}}.
6460
6461
6462 @node vdir invocation
6463 @section @command{vdir}: Verbosely list directory contents
6464
6465 @pindex vdir
6466 @cindex directory listing, verbose
6467
6468 @command{vdir} is equivalent to @code{ls -l
6469 -b}; that is, by default files are listed in long format and special
6470 characters are represented by backslash escape sequences.
6471
6472 @node dircolors invocation
6473 @section @command{dircolors}: Color setup for @command{ls}
6474
6475 @pindex dircolors
6476 @cindex color setup
6477 @cindex setup for color
6478
6479 @command{dircolors} outputs a sequence of shell commands to set up the
6480 terminal for color output from @command{ls} (and @command{dir}, etc.).
6481 Typical usage:
6482
6483 @example
6484 eval "`dircolors [@var{option}]@dots{} [@var{file}]`"
6485 @end example
6486
6487 If @var{file} is specified, @command{dircolors} reads it to determine which
6488 colors to use for which file types and extensions.  Otherwise, a
6489 precompiled database is used.  For details on the format of these files,
6490 run @samp{dircolors --print-database}.
6491
6492 @vindex LS_COLORS
6493 @vindex SHELL @r{environment variable, and color}
6494 The output is a shell command to set the @env{LS_COLORS} environment
6495 variable.  You can specify the shell syntax to use on the command line,
6496 or @command{dircolors} will guess it from the value of the @env{SHELL}
6497 environment variable.
6498
6499 The program accepts the following options.  Also see @ref{Common options}.
6500
6501 @table @samp
6502 @item -b
6503 @itemx --sh
6504 @itemx --bourne-shell
6505 @opindex -b
6506 @opindex --sh
6507 @opindex --bourne-shell
6508 @cindex Bourne shell syntax for color setup
6509 @cindex @command{sh} syntax for color setup
6510 Output Bourne shell commands.  This is the default if the @env{SHELL}
6511 environment variable is set and does not end with @samp{csh} or
6512 @samp{tcsh}.
6513
6514 @item -c
6515 @itemx --csh
6516 @itemx --c-shell
6517 @opindex -c
6518 @opindex --csh
6519 @opindex --c-shell
6520 @cindex C shell syntax for color setup
6521 @cindex @command{csh} syntax for color setup
6522 Output C shell commands.  This is the default if @code{SHELL} ends with
6523 @command{csh} or @command{tcsh}.
6524
6525 @item -p
6526 @itemx --print-database
6527 @opindex -p
6528 @opindex --print-database
6529 @cindex color database, printing
6530 @cindex database for color setup, printing
6531 @cindex printing color database
6532 Print the (compiled-in) default color configuration database.  This
6533 output is itself a valid configuration file, and is fairly descriptive
6534 of the possibilities.
6535
6536 @end table
6537
6538 @exitstatus
6539
6540
6541 @node Basic operations
6542 @chapter Basic operations
6543
6544 @cindex manipulating files
6545
6546 This chapter describes the commands for basic file manipulation:
6547 copying, moving (renaming), and deleting (removing).
6548
6549 @menu
6550 * cp invocation::               Copy files.
6551 * dd invocation::               Convert and copy a file.
6552 * install invocation::          Copy files and set attributes.
6553 * mv invocation::               Move (rename) files.
6554 * rm invocation::               Remove files or directories.
6555 * shred invocation::            Remove files more securely.
6556 @end menu
6557
6558
6559 @node cp invocation
6560 @section @command{cp}: Copy files and directories
6561
6562 @pindex cp
6563 @cindex copying files and directories
6564 @cindex files, copying
6565 @cindex directories, copying
6566
6567 @command{cp} copies files (or, optionally, directories).  The copy is
6568 completely independent of the original.  You can either copy one file to
6569 another, or copy arbitrarily many files to a destination directory.
6570 Synopses:
6571
6572 @example
6573 cp [@var{option}]@dots{} [-T] @var{source} @var{dest}
6574 cp [@var{option}]@dots{} @var{source}@dots{} @var{directory}
6575 cp [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
6576 @end example
6577
6578 @itemize @bullet
6579 @item
6580 If two file names are given, @command{cp} copies the first file to the
6581 second.
6582
6583 @item
6584 If the @option{--target-directory} (@option{-t}) option is given, or
6585 failing that if the last file is a directory and the
6586 @option{--no-target-directory} (@option{-T}) option is not given,
6587 @command{cp} copies each @var{source} file to the specified directory,
6588 using the @var{source}s' names.
6589 @end itemize
6590
6591 Generally, files are written just as they are read.  For exceptions,
6592 see the @option{--sparse} option below.
6593
6594 By default, @command{cp} does not copy directories.  However, the
6595 @option{-R}, @option{-a}, and @option{-r} options cause @command{cp} to
6596 copy recursively by descending into source directories and copying files
6597 to corresponding destination directories.
6598
6599 By default, @command{cp} follows symbolic links only when not copying
6600 recursively.  This default can be overridden with the
6601 @option{--archive} (@option{-a}), @option{-d}, @option{--dereference}
6602 (@option{-L}), @option{--no-dereference} (@option{-P}), and
6603 @option{-H} options.  If more than one of these options is specified,
6604 the last one silently overrides the others.
6605
6606 By default, @command{cp} copies the contents of special files only
6607 when not copying recursively.  This default can be overridden with the
6608 @option{--copy-contents} option.
6609
6610 @cindex self-backups
6611 @cindex backups, making only
6612 @command{cp} generally refuses to copy a file onto itself, with the
6613 following exception: if @option{--force --backup} is specified with
6614 @var{source} and @var{dest} identical, and referring to a regular file,
6615 @command{cp} will make a backup file, either regular or numbered, as
6616 specified in the usual ways (@pxref{Backup options}).  This is useful when
6617 you simply want to make a backup of an existing file before changing it.
6618
6619 The program accepts the following options.  Also see @ref{Common options}.
6620
6621 @table @samp
6622 @item -a
6623 @itemx --archive
6624 @opindex -a
6625 @opindex --archive
6626 Preserve as much as possible of the structure and attributes of the
6627 original files in the copy (but do not attempt to preserve internal
6628 directory structure; i.e., @samp{ls -U} may list the entries in a copied
6629 directory in a different order).
6630 Equivalent to @option{-dpPR}.
6631
6632 @item -b
6633 @itemx @w{@kbd{--backup}[=@var{method}]}
6634 @opindex -b
6635 @opindex --backup
6636 @vindex VERSION_CONTROL
6637 @cindex backups, making
6638 @xref{Backup options}.
6639 Make a backup of each file that would otherwise be overwritten or removed.
6640 As a special case, @command{cp} makes a backup of @var{source} when the force
6641 and backup options are given and @var{source} and @var{dest} are the same
6642 name for an existing, regular file.  One useful application of this
6643 combination of options is this tiny Bourne shell script:
6644
6645 @example
6646 #!/bin/sh
6647 # Usage: backup FILE...
6648 # Create a @sc{gnu}-style backup of each listed FILE.
6649 for i; do
6650   cp --backup --force -- "$i" "$i"
6651 done
6652 @end example
6653
6654 @item --copy-contents
6655 @cindex directories, copying recursively
6656 @cindex copying directories recursively
6657 @cindex recursively copying directories
6658 @cindex non-directories, copying as special files
6659 If copying recursively, copy the contents of any special files (e.g.,
6660 FIFOs and device files) as if they were regular files.  This means
6661 trying to read the data in each source file and writing it to the
6662 destination.  It is usually a mistake to use this option, as it
6663 normally has undesirable effects on special files like FIFOs and the
6664 ones typically found in the @file{/dev} directory.  In most cases,
6665 @code{cp -R --copy-contents} will hang indefinitely trying to read
6666 from FIFOs and special files like @file{/dev/console}, and it will
6667 fill up your destination disk if you use it to copy @file{/dev/zero}.
6668 This option has no effect unless copying recursively, and it does not
6669 affect the copying of symbolic links.
6670
6671 @item -d
6672 @opindex -d
6673 @cindex symbolic links, copying
6674 @cindex hard links, preserving
6675 Copy symbolic links as symbolic links rather than copying the files that
6676 they point to, and preserve hard links between source files in the copies.
6677 Equivalent to @option{--no-dereference --preserve=links}.
6678
6679 @item -f
6680 @itemx --force
6681 @opindex -f
6682 @opindex --force
6683 When copying without this option and an existing destination file cannot
6684 be opened for writing, the copy fails.  However, with @option{--force}),
6685 when a destination file cannot be opened, @command{cp} then unlinks it and
6686 tries to open it again.  Contrast this behavior with that enabled by
6687 @option{--link} and @option{--symbolic-link}, whereby the destination file
6688 is never opened but rather is unlinked unconditionally.  Also see the
6689 description of @option{--remove-destination}.
6690
6691 @item -H
6692 @opindex -H
6693 If a command line argument specifies a symbolic link, then copy the
6694 file it points to rather than the symbolic link itself.  However,
6695 copy (preserving its nature) any symbolic link that is encountered
6696 via recursive traversal.
6697
6698 @item -i
6699 @itemx --interactive
6700 @opindex -i
6701 @opindex --interactive
6702 Prompt whether to overwrite existing regular destination files.
6703
6704 @item -l
6705 @itemx --link
6706 @opindex -l
6707 @opindex --link
6708 Make hard links instead of copies of non-directories.
6709
6710 @item -L
6711 @itemx --dereference
6712 @opindex -L
6713 @opindex --dereference
6714 Always follow symbolic links.
6715
6716 @item -P
6717 @itemx --no-dereference
6718 @opindex -P
6719 @opindex --no-dereference
6720 @cindex symbolic links, copying
6721 Copy symbolic links as symbolic links rather than copying the files that
6722 they point to.
6723
6724 @item -p
6725 @itemx @w{@kbd{--preserve}[=@var{attribute_list}]}
6726 @opindex -p
6727 @opindex --preserve
6728 @cindex file information, preserving
6729 Preserve the specified attributes of the original files.
6730 If specified, the @var{attribute_list} must be a comma-separated list
6731 of one or more of the following strings:
6732
6733 @table @samp
6734 @itemx mode
6735 Preserve the file mode bits and access control lists.
6736 @itemx ownership
6737 Preserve the owner and group.  On most modern systems,
6738 only users with appropriate privileges may change the owner of a file,
6739 and ordinary users
6740 may preserve the group ownership of a file only if they happen to be
6741 a member of the desired group.
6742 @itemx timestamps
6743 Preserve the times of last access and last modification.
6744 @itemx links
6745 Preserve in the destination files
6746 any links between corresponding source files.
6747 @c Give examples illustrating how hard links are preserved.
6748 @c Also, show how soft links map to hard links with -L and -H.
6749 @itemx all
6750 Preserve all file attributes.
6751 Equivalent to specifying all of the above.
6752 @end table
6753
6754 Using @option{--preserve} with no @var{attribute_list} is equivalent
6755 to @option{--preserve=mode,ownership,timestamps}.
6756
6757 In the absence of this option, each destination file is created with the
6758 mode bits of the corresponding source file, minus the bits set in the
6759 umask and minus the set-user-ID and set-group-ID bits.
6760 @xref{File permissions}.
6761
6762 @itemx @w{@kbd{--no-preserve}=@var{attribute_list}}
6763 @cindex file information, preserving
6764 Do not preserve the specified attributes.  The @var{attribute_list}
6765 has the same form as for @option{--preserve}.
6766
6767 @itemx --parents
6768 @opindex --parents
6769 @cindex parent directories and @command{cp}
6770 Form the name of each destination file by appending to the target
6771 directory a slash and the specified name of the source file.  The last
6772 argument given to @command{cp} must be the name of an existing directory.
6773 For example, the command:
6774
6775 @example
6776 cp --parents a/b/c existing_dir
6777 @end example
6778
6779 @noindent
6780 copies the file @file{a/b/c} to @file{existing_dir/a/b/c}, creating
6781 any missing intermediate directories.
6782
6783 @itemx @w{@kbd{--reply}=@var{how}}
6784 @opindex --reply
6785 @cindex interactivity
6786 @c FIXME: remove in 2008
6787 @strong{Deprecated: to be removed in 2008.}@*
6788 Using @option{--reply=yes} makes @command{cp} act as if @samp{yes} were
6789 given as a response to every prompt about a destination file.  That effectively
6790 cancels any preceding @option{--interactive} or @option{-i} option.
6791 Specify @option{--reply=no} to make @command{cp} act as if @samp{no} were
6792 given as a response to every prompt about a destination file.
6793 Specify @option{--reply=query} to make @command{cp} prompt the user
6794 about each existing destination file.
6795
6796 @item -R
6797 @itemx -r
6798 @itemx --recursive
6799 @opindex -R
6800 @opindex -r
6801 @opindex --recursive
6802 @cindex directories, copying recursively
6803 @cindex copying directories recursively
6804 @cindex recursively copying directories
6805 @cindex non-directories, copying as special files
6806 Copy directories recursively.  Symbolic links are not followed by
6807 default; see the @option{--archive} (@option{-a}), @option{-d},
6808 @option{--dereference} (@option{-L}), @option{--no-dereference}
6809 (@option{-P}), and @option{-H} options.  Special files are copied by
6810 creating a destination file of the same type as the source; see the
6811 @option{--copy-contents} option.  It is not portable to use
6812 @option{-r} to copy symbolic links or special files.  On some
6813 non-@sc{gnu} systems, @option{-r} implies the equivalent of
6814 @option{-L} and @option{--copy-contents} for historical reasons.
6815 Also, it is not portable to use @option{-R} to copy symbolic links
6816 unless you also specify @option{-P}, as @acronym{POSIX} allows
6817 implementations that dereference symbolic links by default.
6818
6819 @item --remove-destination
6820 @opindex --remove-destination
6821 Remove each existing destination file before attempting to open it
6822 (contrast with @option{-f} above).
6823
6824 @item --sparse=@var{when}
6825 @opindex --sparse=@var{when}
6826 @cindex sparse files, copying
6827 @cindex holes, copying files with
6828 @findex read @r{system call, and holes}
6829 A @dfn{sparse file} contains @dfn{holes}---a sequence of zero bytes that
6830 does not occupy any physical disk blocks; the @samp{read} system call
6831 reads these as zeros.  This can both save considerable disk space and
6832 increase speed, since many binary files contain lots of consecutive zero
6833 bytes.  By default, @command{cp} detects holes in input source files via a crude
6834 heuristic and makes the corresponding output file sparse as well.
6835 Only regular files may be sparse.
6836
6837 The @var{when} value can be one of the following:
6838
6839 @table @samp
6840 @item auto
6841 The default behavior: if the input file is sparse, attempt to make
6842 the output file sparse, too.  However, if an output file exists but
6843 refers to a non-regular file, then do not attempt to make it sparse.
6844
6845 @item always
6846 For each sufficiently long sequence of zero bytes in the input file,
6847 attempt to create a corresponding hole in the output file, even if the
6848 input file does not appear to be sparse.
6849 This is useful when the input file resides on a file system
6850 that does not support sparse files
6851 (for example,  @samp{efs} file systems in SGI IRIX 5.3 and earlier),
6852 but the output file is on a type of file system that does support them.
6853 Holes may be created only in regular files, so if the destination file
6854 is of some other type, @command{cp} does not even try to make it sparse.
6855
6856 @item never
6857 Never make the output file sparse.
6858 This is useful in creating a file for use with the @command{mkswap} command,
6859 since such a file must not have any holes.
6860 @end table
6861
6862 @optStripTrailingSlashes
6863
6864 @item -s
6865 @itemx --symbolic-link
6866 @opindex -s
6867 @opindex --symbolic-link
6868 @cindex symbolic links, copying with
6869 Make symbolic links instead of copies of non-directories.  All source
6870 file names must be absolute (starting with @samp{/}) unless the
6871 destination files are in the current directory.  This option merely
6872 results in an error message on systems that do not support symbolic links.
6873
6874 @optBackupSuffix
6875
6876 @optTargetDirectory
6877
6878 @optNoTargetDirectory
6879
6880 @item -u
6881 @itemx --update
6882 @opindex -u
6883 @opindex --update
6884 @cindex newer files, copying only
6885 Do not copy a non-directory that has an existing destination with the
6886 same or newer modification time.  If time stamps are being preserved,
6887 the comparison is to the source time stamp truncated to the
6888 resolutions of the destination file system and of the system calls
6889 used to update time stamps; this avoids duplicate work if several
6890 @samp{cp -pu} commands are executed with the same source and
6891 destination.
6892
6893 @item -v
6894 @itemx --verbose
6895 @opindex -v
6896 @opindex --verbose
6897 Print the name of each file before copying it.
6898
6899 @item -x
6900 @itemx --one-file-system
6901 @opindex -x
6902 @opindex --one-file-system
6903 @cindex file systems, omitting copying to different
6904 Skip subdirectories that are on different file systems from the one that
6905 the copy started on.
6906 However, mount point directories @emph{are} copied.
6907
6908 @end table
6909
6910 @exitstatus
6911
6912
6913 @node dd invocation
6914 @section @command{dd}: Convert and copy a file
6915
6916 @pindex dd
6917 @cindex converting while copying a file
6918
6919 @command{dd} copies a file (from standard input to standard output, by
6920 default) with a changeable I/O block size, while optionally performing
6921 conversions on it.  Synopses:
6922
6923 @example
6924 dd [@var{operand}]@dots{}
6925 dd @var{option}
6926 @end example
6927
6928 The only options are @option{--help} and @option{--version}.
6929 @xref{Common options}.  @command{dd} accepts the following operands.
6930
6931 @table @samp
6932
6933 @item if=@var{file}
6934 @opindex if
6935 Read from @var{file} instead of standard input.
6936
6937 @item of=@var{file}
6938 @opindex of
6939 Write to @var{file} instead of standard output.  Unless
6940 @samp{conv=notrunc} is given, @command{dd} truncates @var{file} to zero
6941 bytes (or the size specified with @samp{seek=}).
6942
6943 @item ibs=@var{bytes}
6944 @opindex ibs
6945 @cindex block size of input
6946 @cindex input block size
6947 Read @var{bytes} bytes at a time.
6948
6949 @item obs=@var{bytes}
6950 @opindex obs
6951 @cindex block size of output
6952 @cindex output block size
6953 Write @var{bytes} bytes at a time.
6954
6955 @item bs=@var{bytes}
6956 @opindex bs
6957 @cindex block size
6958 Both read and write @var{bytes} bytes at a time.  This overrides
6959 @samp{ibs} and @samp{obs}.
6960
6961 @item cbs=@var{bytes}
6962 @opindex cbs
6963 @cindex block size of conversion
6964 @cindex conversion block size
6965 Convert @var{bytes} bytes at a time.
6966
6967 @item skip=@var{blocks}
6968 @opindex skip
6969 Skip @var{blocks} @samp{ibs}-byte blocks in the input file before copying.
6970
6971 @item seek=@var{blocks}
6972 @opindex seek
6973 Skip @var{blocks} @samp{obs}-byte blocks in the output file before copying.
6974
6975 @item count=@var{blocks}
6976 @opindex count
6977 Copy @var{blocks} @samp{ibs}-byte blocks from the input file, instead
6978 of everything until the end of the file.
6979
6980 @item conv=@var{conversion}[,@var{conversion}]@dots{}
6981 @opindex conv
6982 Convert the file as specified by the @var{conversion} argument(s).
6983 (No spaces around any comma(s).)
6984
6985 Conversions:
6986
6987 @table @samp
6988
6989 @item ascii
6990 @opindex ascii@r{, converting to}
6991 Convert @acronym{EBCDIC} to @acronym{ASCII},
6992 using the conversion table specified by @acronym{POSIX}.
6993 This provides a 1:1 translation for all 256 bytes.
6994
6995 @item ebcdic
6996 @opindex ebcdic@r{, converting to}
6997 Convert @acronym{ASCII} to @acronym{EBCDIC}.
6998 This is the inverse of the @samp{ascii} conversion.
6999
7000 @item ibm
7001 @opindex alternate ebcdic@r{, converting to}
7002 Convert @acronym{ASCII} to alternate @acronym{EBCDIC},
7003 using the alternate conversion table specified by @acronym{POSIX}.
7004 This is not a 1:1 translation, but reflects common historical practice
7005 for @samp{~}, @samp{[}, and @samp{]}.
7006
7007 The @samp{ascii}, @samp{ebcdic}, and @samp{ibm} conversions are
7008 mutually exclusive.
7009
7010 @item block
7011 @opindex block @r{(space-padding)}
7012 For each line in the input, output @samp{cbs} bytes, replacing the
7013 input newline with a space and padding with spaces as necessary.
7014
7015 @item unblock
7016 @opindex unblock
7017 Replace trailing spaces in each @samp{cbs}-sized input block with a
7018 newline.
7019
7020 The @samp{block} and @samp{unblock} conversions are mutually exclusive.
7021
7022 @item lcase
7023 @opindex lcase@r{, converting to}
7024 Change uppercase letters to lowercase.
7025
7026 @item ucase
7027 @opindex ucase@r{, converting to}
7028 Change lowercase letters to uppercase.
7029
7030 The @samp{lcase} and @samp{ucase} conversions are mutually exclusive.
7031
7032 @item swab
7033 @opindex swab @r{(byte-swapping)}
7034 @cindex byte-swapping
7035 Swap every pair of input bytes.  @sc{gnu} @command{dd}, unlike others, works
7036 when an odd number of bytes are read---the last byte is simply copied
7037 (since there is nothing to swap it with).
7038
7039 @item noerror
7040 @opindex noerror
7041 @cindex read errors, ignoring
7042 Continue after read errors.
7043
7044 @item nocreat
7045 @opindex nocreat
7046 @cindex creating output file, avoiding
7047 Do not create the output file; the output file must already exist.
7048
7049 @item excl
7050 @opindex excl
7051 @cindex creating output file, requiring
7052 Fail if the output file already exists; @command{dd} must create the
7053 output file itself.
7054
7055 The @samp{excl} and @samp{nocreat} conversions are mutually exclusive.
7056
7057 @item notrunc
7058 @opindex notrunc
7059 @cindex truncating output file, avoiding
7060 Do not truncate the output file.
7061
7062 @item sync
7063 @opindex sync @r{(padding with nulls)}
7064 Pad every input block to size of @samp{ibs} with trailing zero bytes.
7065 When used with @samp{block} or @samp{unblock}, pad with spaces instead of
7066 zero bytes.
7067
7068 @item fdatasync
7069 @opindex fdatasync
7070 @cindex synchronized data writes, before finishing
7071 Synchronize output data just before finishing.  This forces a physical
7072 write of output data.
7073
7074 @item fsync
7075 @opindex fsync
7076 @cindex synchronized data and metadata writes, before finishing
7077 Synchronize output data and metadata just before finishing.  This
7078 forces a physical write of output data and metadata.
7079
7080 @end table
7081
7082 @item iflag=@var{flag}[,@var{flag}]@dots{}
7083 @opindex iflag
7084 Access the input file using the flags specified by the @var{flag}
7085 argument(s).  (No spaces around any comma(s).)
7086
7087 @item oflag=@var{flag}[,@var{flag}]@dots{}
7088 @opindex oflag
7089 Access the output file using the flags specified by the @var{flag}
7090 argument(s).  (No spaces around any comma(s).)
7091
7092 Here are the flags.  Not every flag is supported on every operating
7093 system.
7094
7095 @table @samp
7096
7097 @item append
7098 @opindex append
7099 @cindex appending to the output file
7100 Write in append mode, so that even if some other process is writing to
7101 this file, every @command{dd} write will append to the current
7102 contents of the file.  This flag makes sense only for output.
7103
7104 @item direct
7105 @opindex direct
7106 @cindex direct I/O
7107 Use direct I/O for data, avoiding the buffer cache.
7108
7109 @item directory
7110 @opindex directory
7111 @cindex directory I/O
7112
7113 Fail unless the file is a directory.  Most operating systems do not
7114 allow I/O to a directory, so this flag has limited utility.
7115
7116 @item dsync
7117 @opindex dsync
7118 @cindex synchronized data reads
7119 Use synchronized I/O for data.  For the output file, this forces a
7120 physical write of output data on each write.  For the input file,
7121 this flag can matter when reading from a remote file that has been
7122 written to synchronously by some other process.  Metadata (e.g.,
7123 last-access and last-modified time) is not necessarily synchronized.
7124
7125 @item sync
7126 @opindex sync
7127 @cindex synchronized data and metadata I/O
7128 Use synchronized I/O for both data and metadata.
7129
7130 @item nonblock
7131 @opindex nonblock
7132 @cindex nonblocking I/O
7133 Use non-blocking I/O.
7134
7135 @item noatime
7136 @opindex noatime
7137 @cindex access time
7138 Do not update the file's access time.
7139
7140 @item noctty
7141 @opindex noctty
7142 @cindex controlling terminal
7143 Do not assign the file to be a controlling terminal for @command{dd}.
7144 This has no effect when the file is not a terminal.
7145 On many hosts (e.g., @acronym{GNU}/Linux hosts), this option has no effect
7146 at all.
7147
7148 @item nofollow
7149 @opindex nofollow
7150 @cindex symbolic links, following
7151 Do not follow symbolic links.
7152
7153 @item nolinks
7154 @opindex nolinks
7155 @cindex hard links
7156 Fail if the file has multiple hard links.
7157
7158 @item binary
7159 @opindex binary
7160 @cindex binary I/O
7161 Use binary I/O.  This option has an effect only on nonstandard
7162 platforms that distinguish binary from text I/O.
7163
7164 @item text
7165 @opindex text
7166 @cindex text I/O
7167 Use text I/O.  Like @samp{binary}, this option has no effect on
7168 standard platforms.
7169
7170 @end table
7171
7172 These flags are not supported on all systems, and @samp{dd} rejects
7173 attempts to use them when they are not supported.  When reading from
7174 standard input or writing to standard output, the @samp{nofollow} and
7175 @samp{noctty} flags should not be specified, and the other flags
7176 (e.g., @samp{nonblock}) can affect how other processes behave with the
7177 affected file descriptors, even after @command{dd} exits.
7178
7179 @end table
7180
7181 @cindex multipliers after numbers
7182 The numeric-valued strings above (@var{bytes} and @var{blocks}) can be
7183 followed by a multiplier: @samp{b}=512, @samp{c}=1,
7184 @samp{w}=2, @samp{x@var{m}}=@var{m}, or any of the
7185 standard block size suffixes like @samp{k}=1024 (@pxref{Block size}).
7186
7187 Use different @command{dd} invocations to use different block sizes for
7188 skipping and I/O@.  For example, the following shell commands copy data
7189 in 512 KiB blocks between a disk and a tape, but do not save or restore a
7190 4 KiB label at the start of the disk:
7191
7192 @example
7193 disk=/dev/rdsk/c0t1d0s2
7194 tape=/dev/rmt/0
7195
7196 # Copy all but the label from disk to tape.
7197 (dd bs=4k skip=1 count=0 && dd bs=512k) <$disk >$tape
7198
7199 # Copy from tape back to disk, but leave the disk label alone.
7200 (dd bs=4k seek=1 count=0 && dd bs=512k) <$tape >$disk
7201 @end example
7202
7203 Sending an @samp{INFO} signal to a running @command{dd}
7204 process makes it print I/O statistics to standard error
7205 and then resume copying.  In the example below,
7206 @command{dd} is run in the background to copy 10 million blocks.
7207 The @command{kill} command makes it output intermediate I/O statistics,
7208 and when @command{dd} completes, it outputs the final statistics.
7209
7210 @example
7211 $ dd if=/dev/zero of=/dev/null count=10MB & pid=$!
7212 $ kill -s INFO $pid; wait $pid
7213 3385223+0 records in
7214 3385223+0 records out
7215 1733234176 bytes (1.7 GB) copied, 6.42173 seconds, 270 MB/s
7216 10000000+0 records in
7217 10000000+0 records out
7218 5120000000 bytes (5.1 GB) copied, 18.913 seconds, 271 MB/s
7219 @end example
7220
7221 @vindex POSIXLY_CORRECT
7222 On systems lacking the @samp{INFO} signal @command{dd} responds to the
7223 @samp{USR1} signal instead, unless the @env{POSIXLY_CORRECT}
7224 environment variable is set.
7225
7226 @exitstatus
7227
7228
7229 @node install invocation
7230 @section @command{install}: Copy files and set attributes
7231
7232 @pindex install
7233 @cindex copying files and setting attributes
7234
7235 @command{install} copies files while setting their file mode bits and, if
7236 possible, their owner and group.  Synopses:
7237
7238 @example
7239 install [@var{option}]@dots{} [-T] @var{source} @var{dest}
7240 install [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7241 install [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7242 install [@var{option}]@dots{} -d @var{directory}@dots{}
7243 @end example
7244
7245 @itemize @bullet
7246 @item
7247 If two file names are given, @command{install} copies the first file to the
7248 second.
7249
7250 @item
7251 If the @option{--target-directory} (@option{-t}) option is given, or
7252 failing that if the last file is a directory and the
7253 @option{--no-target-directory} (@option{-T}) option is not given,
7254 @command{install} copies each @var{source} file to the specified
7255 directory, using the @var{source}s' names.
7256
7257 @item
7258 If the @option{--directory} (@option{-d}) option is given,
7259 @command{install} creates each @var{directory} and any missing parent
7260 directories.  Parent directories are created with mode
7261 @samp{u=rwx,go=rx} (755), regardless of the @option{-m} option or the
7262 current umask.
7263 @end itemize
7264
7265 @cindex Makefiles, installing programs in
7266 @command{install} is similar to @command{cp}, but allows you to control the
7267 attributes of destination files.  It is typically used in Makefiles to
7268 copy programs into their destination directories.  It refuses to copy
7269 files onto themselves.
7270
7271 The program accepts the following options.  Also see @ref{Common options}.
7272
7273 @table @samp
7274
7275 @optBackup
7276
7277 @item -c
7278 @opindex -c
7279 Ignored; for compatibility with old Unix versions of @command{install}.
7280
7281 @item -d
7282 @itemx --directory
7283 @opindex -d
7284 @opindex --directory
7285 @cindex directories, creating with given attributes
7286 @cindex parent directories, creating missing
7287 @cindex leading directories, creating missing
7288 Create any missing parent directories, giving them the default
7289 attributes.  Then create each given directory, setting their owner,
7290 group and mode as given on the command line or to the defaults.
7291
7292 @item -g @var{group}
7293 @itemx --group=@var{group}
7294 @opindex -g
7295 @opindex --group
7296 @cindex group ownership of installed files, setting
7297 Set the group ownership of installed files or directories to
7298 @var{group}.  The default is the process's current group.  @var{group}
7299 may be either a group name or a numeric group ID.
7300
7301 @item -m @var{mode}
7302 @itemx --mode=@var{mode}
7303 @opindex -m
7304 @opindex --mode
7305 @cindex permissions of installed files, setting
7306 Set the file mode bits for the installed file or directory to @var{mode},
7307 which can be either an octal number, or a symbolic mode as in
7308 @command{chmod}, with @samp{a=} (no access allowed to anyone) as the
7309 point of departure (@pxref{File permissions}).
7310 The default mode is @samp{u=rwx,go=rx,a-s} (0755)---read, write, and
7311 execute for the owner, read and execute for group and other, and with
7312 set-user-ID and set-group-ID disabled.
7313
7314 @item -o @var{owner}
7315 @itemx --owner=@var{owner}
7316 @opindex -o
7317 @opindex --owner
7318 @cindex ownership of installed files, setting
7319 @cindex appropriate privileges
7320 @vindex root @r{as default owner}
7321 If @command{install} has appropriate privileges (is run as root), set the
7322 ownership of installed files or directories to @var{owner}.  The default
7323 is @code{root}.  @var{owner} may be either a user name or a numeric user
7324 ID.
7325
7326 @item -p
7327 @itemx --preserve-timestamps
7328 @opindex -p
7329 @opindex --preserve-timestamps
7330 @cindex timestamps of installed files, preserving
7331 Set the time of last access and the time of last modification of each
7332 installed file to match those of each corresponding original file.
7333 When a file is installed without this option, its last access and
7334 last modification times are both set to the time of installation.
7335 This option is useful if you want to use the last modification times
7336 of installed files to keep track of when they were last built as opposed
7337 to when they were last installed.
7338
7339 @item -s
7340 @itemx --strip
7341 @opindex -s
7342 @opindex --strip
7343 @cindex symbol table information, stripping
7344 @cindex stripping symbol table information
7345 Strip the symbol tables from installed binary executables.
7346
7347 @optBackupSuffix
7348
7349 @optTargetDirectory
7350
7351 @optNoTargetDirectory
7352
7353 @item -v
7354 @itemx --verbose
7355 @opindex -v
7356 @opindex --verbose
7357 Print the name of each file before copying it.
7358
7359 @end table
7360
7361 @exitstatus
7362
7363
7364 @node mv invocation
7365 @section @command{mv}: Move (rename) files
7366
7367 @pindex mv
7368
7369 @command{mv} moves or renames files (or directories).  Synopses:
7370
7371 @example
7372 mv [@var{option}]@dots{} [-T] @var{source} @var{dest}
7373 mv [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7374 mv [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7375 @end example
7376
7377 @itemize @bullet
7378 @item
7379 If two file names are given, @command{mv} moves the first file to the
7380 second.
7381
7382 @item
7383 If the @option{--target-directory} (@option{-t}) option is given, or
7384 failing that if the last file is a directory and the
7385 @option{--no-target-directory} (@option{-T}) option is not given,
7386 @command{mv} moves each @var{source} file to the specified
7387 directory, using the @var{source}s' names.
7388 @end itemize
7389
7390 @command{mv} can move any type of file from one file system to another.
7391 Prior to version @code{4.0} of the fileutils,
7392 @command{mv} could move only regular files between file systems.
7393 For example, now @command{mv} can move an entire directory hierarchy
7394 including special device files from one partition to another.  It first
7395 uses some of the same code that's used by @code{cp -a} to copy the
7396 requested directories and files, then (assuming the copy succeeded)
7397 it removes the originals.  If the copy fails, then the part that was
7398 copied to the destination partition is removed.  If you were to copy
7399 three directories from one partition to another and the copy of the first
7400 directory succeeded, but the second didn't, the first would be left on
7401 the destination partition and the second and third would be left on the
7402 original partition.
7403
7404 @cindex prompting, and @command{mv}
7405 If a destination file exists but is normally unwritable, standard input
7406 is a terminal, and the @option{-f} or @option{--force} option is not given,
7407 @command{mv} prompts the user for whether to replace the file.  (You might
7408 own the file, or have write permission on its directory.)  If the
7409 response is not affirmative, the file is skipped.
7410
7411 @emph{Warning}: If you try to move a symlink that points to a directory,
7412 and you specify the symlink with a trailing slash, then @command{mv}
7413 doesn't move the symlink but instead moves the directory referenced
7414 by the symlink.  @xref{Trailing slashes}.
7415
7416 The program accepts the following options.  Also see @ref{Common options}.
7417
7418 @table @samp
7419
7420 @optBackup
7421
7422 @item -f
7423 @itemx --force
7424 @opindex -f
7425 @opindex --force
7426 @cindex prompts, omitting
7427 Do not prompt the user before removing a destination file.
7428
7429 @item -i
7430 @itemx --interactive
7431 @opindex -i
7432 @opindex --interactive
7433 @cindex prompts, forcing
7434 Prompt whether to overwrite each existing destination file, regardless
7435 of its permissions.
7436 If the response is not affirmative, the file is skipped.
7437
7438 @itemx @w{@kbd{--reply}=@var{how}}
7439 @opindex --reply
7440 @cindex interactivity
7441 @c FIXME: remove in 2008
7442 @strong{Deprecated: to be removed in 2008.}@*
7443 Specifying @option{--reply=yes} is equivalent to using @option{--force}.
7444 Specify @option{--reply=no} to make @command{mv} act as if @samp{no} were
7445 given as a response to every prompt about a destination file.
7446 Specify @option{--reply=query} to make @command{mv} prompt the user
7447 about each existing destination file.
7448 Note that @option{--reply=no} has an effect only when @command{mv} would prompt
7449 without @option{-i} or equivalent, i.e., when a destination file exists and is
7450 not writable, standard input is a terminal, and no @option{-f} (or equivalent)
7451 option is specified.
7452
7453 @item -u
7454 @itemx --update
7455 @opindex -u
7456 @opindex --update
7457 @cindex newer files, moving only
7458 Do not move a non-directory that has an existing destination with the
7459 same or newer modification time.
7460 If the move is across file system boundaries, the comparison is to the
7461 source time stamp truncated to the resolutions of the destination file
7462 system and of the system calls used to update time stamps; this avoids
7463 duplicate work if several @samp{mv -u} commands are executed with the
7464 same source and destination.
7465
7466 @item -v
7467 @itemx --verbose
7468 @opindex -v
7469 @opindex --verbose
7470 Print the name of each file before moving it.
7471
7472 @optStripTrailingSlashes
7473
7474 @optBackupSuffix
7475
7476 @optTargetDirectory
7477
7478 @optNoTargetDirectory
7479
7480 @end table
7481
7482 @exitstatus
7483
7484
7485 @node rm invocation
7486 @section @command{rm}: Remove files or directories
7487
7488 @pindex rm
7489 @cindex removing files or directories
7490
7491 @command{rm} removes each given @var{file}.  By default, it does not remove
7492 directories.  Synopsis:
7493
7494 @example
7495 rm [@var{option}]@dots{} [@var{file}]@dots{}
7496 @end example
7497
7498 @cindex prompting, and @command{rm}
7499 If the @option{-I} or @option{--interactive=once} option is given,
7500 and there are more than three files or the @option{-r}, @option{-R},
7501 or @option{--recursive} are given, then @command{rm} prompts the user
7502 for whether to proceed with the entire operation.  If the response is
7503 not affirmative, the entire command is aborted.
7504
7505 Otherwise, if a file is unwritable, standard input is a terminal, and
7506 the @option{-f} or @option{--force} option is not given, or the
7507 @option{-i} or @option{--interactive=always} option @emph{is} given,
7508 @command{rm} prompts the user for whether to remove the file.
7509 If the response is not affirmative, the file is skipped.
7510
7511 @emph{Warning}: If you use @command{rm} to remove a file, it is usually
7512 possible to recover the contents of that file.  If you want more assurance
7513 that the contents are truly unrecoverable, consider using @command{shred}.
7514
7515 The program accepts the following options.  Also see @ref{Common options}.
7516
7517 @table @samp
7518
7519 @item -f
7520 @itemx --force
7521 @opindex -f
7522 @opindex --force
7523 Ignore nonexistent files and never prompt the user.
7524 Ignore any previous @option{--interactive} (@option{-i}) option.
7525
7526 @item -i
7527 @opindex -i
7528 Prompt whether to remove each file.
7529 If the response is not affirmative, the file is skipped.
7530 Ignore any previous @option{--force} (@option{-f}) option.
7531 Equivalent to @option{--interactive=always}.
7532
7533 @item -I
7534 @opindex -I
7535 Prompt once whether to proceed with the command, if more than three
7536 files are named or if a recursive removal is requested.  Ignore any
7537 previous @option{--force} (@option{-f}) option.  Equivalent to
7538 @option{--interactive=once}.
7539
7540 @itemx --interactive [=@var{when}]
7541 @opindex --interactive
7542 Specify when to issue an interactive prompt.  @var{when} may be
7543 omitted, or one of:
7544 @itemize @bullet
7545 @item never
7546 @vindex never @r{interactive option}
7547 - Do not prompt at all.
7548 @item once
7549 @vindex once @r{interactive option}
7550 - Prompt once if more than three files are named or if a recursive
7551 removal is requested.  Equivalent to @option{-I}.
7552 @item always
7553 @vindex always @r{interactive option}
7554 - Prompt for every file being removed.  Equivalent to @option{-i}.
7555 @end itemize
7556 Specifying @option{--interactive} and no @var{when} is equivalent to
7557 @option{--interactive=always}.
7558
7559 @itemx --preserve-root
7560 @opindex --preserve-root
7561 @cindex root directory, disallow recursive destruction
7562 Fail upon any attempt to remove the file system root, @file{/},
7563 when used with the @option{--recursive} option.
7564 Without @option{--recursive}, this option has no effect.
7565 @xref{Treating / specially}.
7566
7567 @itemx --no-preserve-root
7568 @opindex --no-preserve-root
7569 @cindex root directory, allow recursive destruction
7570 Cancel the effect of any preceding @option{--preserve-root} option.
7571 @xref{Treating / specially}.
7572
7573 @item -r
7574 @itemx -R
7575 @itemx --recursive
7576 @opindex -r
7577 @opindex -R
7578 @opindex --recursive
7579 @cindex directories, removing (recursively)
7580 Remove the listed directories and their contents recursively.
7581
7582 @item -v
7583 @itemx --verbose
7584 @opindex -v
7585 @opindex --verbose
7586 Print the name of each file before removing it.
7587
7588 @end table
7589
7590 @cindex files beginning with @samp{-}, removing
7591 @cindex @samp{-}, removing files beginning with
7592 One common question is how to remove files whose names begin with a
7593 @samp{-}.  @sc{gnu} @command{rm}, like every program that uses the @code{getopt}
7594 function to parse its arguments, lets you use the @samp{--} option to
7595 indicate that all following arguments are non-options.  To remove a file
7596 called @file{-f} in the current directory, you could type either:
7597
7598 @example
7599 rm -- -f
7600 @end example
7601
7602 @noindent
7603 or:
7604
7605 @example
7606 rm ./-f
7607 @end example
7608
7609 @opindex - @r{and Unix @command{rm}}
7610 The Unix @command{rm} program's use of a single @samp{-} for this purpose
7611 predates the development of the getopt standard syntax.
7612
7613 @exitstatus
7614
7615
7616 @node shred invocation
7617 @section @command{shred}: Remove files more securely
7618
7619 @pindex shred
7620 @cindex data, erasing
7621 @cindex erasing data
7622
7623 @command{shred} overwrites devices or files, to help prevent even
7624 very expensive hardware from recovering the data.
7625
7626 Ordinarily when you remove a file (@pxref{rm invocation}), the data is
7627 not actually destroyed.  Only the index listing where the file is
7628 stored is destroyed, and the storage is made available for reuse.
7629 There are undelete utilities that will attempt to reconstruct the index
7630 and can bring the file back if the parts were not reused.
7631
7632 On a busy system with a nearly-full drive, space can get reused in a few
7633 seconds.  But there is no way to know for sure.  If you have sensitive
7634 data, you may want to be sure that recovery is not possible by actually
7635 overwriting the file with non-sensitive data.
7636
7637 However, even after doing that, it is possible to take the disk back
7638 to a laboratory and use a lot of sensitive (and expensive) equipment
7639 to look for the faint ``echoes'' of the original data underneath the
7640 overwritten data.  If the data has only been overwritten once, it's not
7641 even that hard.
7642
7643 The best way to remove something irretrievably is to destroy the media
7644 it's on with acid, melt it down, or the like.  For cheap removable media
7645 like floppy disks, this is the preferred method.  However, hard drives
7646 are expensive and hard to melt, so the @command{shred} utility tries
7647 to achieve a similar effect non-destructively.
7648
7649 This uses many overwrite passes, with the data patterns chosen to
7650 maximize the damage they do to the old data.  While this will work on
7651 floppies, the patterns are designed for best effect on hard drives.
7652 For more details, see the source code and Peter Gutmann's paper
7653 @uref{http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html,
7654 @cite{Secure Deletion of Data from Magnetic and Solid-State Memory}},
7655 from the proceedings of the Sixth @acronym{USENIX} Security Symposium (San Jose,
7656 California, July 22--25, 1996).
7657
7658 @strong{Please note} that @command{shred} relies on a very important assumption:
7659 that the file system overwrites data in place.  This is the traditional
7660 way to do things, but many modern file system designs do not satisfy this
7661 assumption.  Exceptions include:
7662
7663 @itemize @bullet
7664
7665 @item
7666 Log-structured or journaled file systems, such as those supplied with
7667 AIX and Solaris, and JFS, ReiserFS, XFS, Ext3 (in @code{data=journal} mode),
7668 BFS, NTFS, etc.@: when they are configured to journal @emph{data}.
7669
7670 @item
7671 File systems that write redundant data and carry on even if some writes
7672 fail, such as RAID-based file systems.
7673
7674 @item
7675 File systems that make snapshots, such as Network Appliance's NFS server.
7676
7677 @item
7678 File systems that cache in temporary locations, such as NFS version 3
7679 clients.
7680
7681 @item
7682 Compressed file systems.
7683 @end itemize
7684
7685 In the particular case of ext3 file systems, the above disclaimer applies (and
7686 @command{shred} is thus of limited effectiveness) only in @code{data=journal}
7687 mode, which journals file data in addition to just metadata. In both
7688 the @code{data=ordered} (default) and @code{data=writeback} modes,
7689 @command{shred} works as usual.  Ext3 journaling modes can be changed
7690 by adding the @code{data=something} option to the mount options for a
7691 particular file system in the @file{/etc/fstab} file, as documented in
7692 the mount man page (man mount).
7693
7694 If you are not sure how your file system operates, then you should assume
7695 that it does not overwrite data in place, which means that shred cannot
7696 reliably operate on regular files in your file system.
7697
7698 Generally speaking, it is more reliable to shred a device than a file,
7699 since this bypasses the problem of file system design mentioned above.
7700 However, even shredding devices is not always completely reliable.  For
7701 example, most disks map out bad sectors invisibly to the application; if
7702 the bad sectors contain sensitive data, @command{shred} won't be able to
7703 destroy it.
7704
7705 @command{shred} makes no attempt to detect or report this problem, just as
7706 it makes no attempt to do anything about backups.  However, since it is
7707 more reliable to shred devices than files, @command{shred} by default does
7708 not truncate or remove the output file.  This default is more suitable
7709 for devices, which typically cannot be truncated and should not be
7710 removed.
7711
7712 Finally, consider the risk of backups and mirrors.
7713 File system backups and remote mirrors may contain copies of the
7714 file that cannot be removed, and that will allow a shredded file
7715 to be recovered later.  So if you keep any data you may later want
7716 to destroy using @command{shred}, be sure that it is not backed up or mirrored.
7717
7718 @example
7719 shred [@var{option}]@dots{} @var{file}[@dots{}]
7720 @end example
7721
7722 The program accepts the following options.  Also see @ref{Common options}.
7723
7724 @table @samp
7725
7726 @item -f
7727 @itemx --force
7728 @opindex -f
7729 @opindex --force
7730 @cindex force deletion
7731 Override file permissions if necessary to allow overwriting.
7732
7733 @item -@var{NUMBER}
7734 @itemx -n @var{NUMBER}
7735 @itemx --iterations=@var{NUMBER}
7736 @opindex -n @var{NUMBER}
7737 @opindex --iterations=@var{NUMBER}
7738 @cindex iterations, selecting the number of
7739 By default, @command{shred} uses 25 passes of overwrite.  This is enough
7740 for all of the useful overwrite patterns to be used at least once.
7741 You can reduce this to save time, or increase it if you have a lot of
7742 time to waste.
7743
7744 @item -s @var{BYTES}
7745 @itemx --size=@var{BYTES}
7746 @opindex -s @var{BYTES}
7747 @opindex --size=@var{BYTES}
7748 @cindex size of file to shred
7749 Shred the first @var{BYTES} bytes of the file.  The default is to shred
7750 the whole file.  @var{BYTES} can be followed by a size specification like
7751 @samp{K}, @samp{M}, or @samp{G} to specify a multiple.  @xref{Block size}.
7752
7753 @item -u
7754 @itemx --remove
7755 @opindex -u
7756 @opindex --remove
7757 @cindex removing files after shredding
7758 After shredding a file, truncate it (if possible) and then remove it.
7759 If a file has multiple links, only the named links will be removed.
7760
7761 @item -v
7762 @itemx --verbose
7763 @opindex -v
7764 @opindex --verbose
7765 Display status updates as sterilization proceeds.
7766
7767 @item -x
7768 @itemx --exact
7769 @opindex -x
7770 @opindex --exact
7771 By default, @command{shred} rounds the size of a regular file up to the next
7772 multiple of the file system block size to fully erase the last block of the file.
7773 Use @option{--exact} to suppress that behavior.
7774 Thus, by default if you shred a 10-byte regular file on a system with 512-byte
7775 blocks, the resulting file will be 512 bytes long.  With this option,
7776 shred does not increase the apparent size of the file.
7777
7778 @item -z
7779 @itemx --zero
7780 @opindex -z
7781 @opindex --zero
7782 Normally, the last pass that @command{shred} writes is made up of
7783 random data.  If this would be conspicuous on your hard drive (for
7784 example, because it looks like encrypted data), or you just think
7785 it's tidier, the @option{--zero} option adds an additional overwrite pass with
7786 all zero bits.  This is in addition to the number of passes specified
7787 by the @option{--iterations} option.
7788
7789 @end table
7790
7791 You might use the following command to erase all trace of the
7792 file system you'd created on the floppy disk in your first drive.
7793 That command takes about 20 minutes to erase a ``1.44MB'' (actually
7794 1440 KiB) floppy.
7795
7796 @example
7797 shred --verbose /dev/fd0
7798 @end example
7799
7800 Similarly, to erase all data on a selected partition of
7801 your hard disk, you could give a command like this:
7802
7803 @example
7804 shred --verbose /dev/sda5
7805 @end example
7806
7807 A @var{file} of @samp{-} denotes standard output.
7808 The intended use of this is to shred a removed temporary file.
7809 For example:
7810
7811 @example
7812 i=`tempfile -m 0600`
7813 exec 3<>"$i"
7814 rm -- "$i"
7815 echo "Hello, world" >&3
7816 shred - >&3
7817 exec 3>-
7818 @end example
7819
7820 However, the command @samp{shred - >file} does not shred the contents
7821 of @var{file}, since the shell truncates @var{file} before invoking
7822 @command{shred}.  Use the command @samp{shred file} or (if using a
7823 Bourne-compatible shell) the command @samp{shred - 1<>file} instead.
7824
7825 @exitstatus
7826
7827
7828 @node Special file types
7829 @chapter Special file types
7830
7831 @cindex special file types
7832 @cindex file types, special
7833
7834 This chapter describes commands which create special types of files (and
7835 @command{rmdir}, which removes directories, one special file type).
7836
7837 @cindex special file types
7838 @cindex file types
7839 Although Unix-like operating systems have markedly fewer special file
7840 types than others, not @emph{everything} can be treated only as the
7841 undifferentiated byte stream of @dfn{normal files}.  For example, when a
7842 file is created or removed, the system must record this information,
7843 which it does in a @dfn{directory}---a special type of file.  Although
7844 you can read directories as normal files, if you're curious, in order
7845 for the system to do its job it must impose a structure, a certain
7846 order, on the bytes of the file.  Thus it is a ``special'' type of file.
7847
7848 Besides directories, other special file types include named pipes
7849 (FIFOs), symbolic links, sockets, and so-called @dfn{special files}.
7850
7851 @menu
7852 * link invocation::             Make a hard link via the link syscall
7853 * ln invocation::               Make links between files.
7854 * mkdir invocation::            Make directories.
7855 * mkfifo invocation::           Make FIFOs (named pipes).
7856 * mknod invocation::            Make block or character special files.
7857 * readlink invocation::         Print the referent of a symbolic link.
7858 * rmdir invocation::            Remove empty directories.
7859 * unlink invocation::           Remove files via the unlink syscall
7860 @end menu
7861
7862
7863 @node link invocation
7864 @section @command{link}: Make a hard link via the link syscall
7865
7866 @pindex link
7867 @cindex links, creating
7868 @cindex hard links, creating
7869 @cindex creating links (hard only)
7870
7871 @command{link} creates a single hard link at a time.
7872 It is a minimalist interface to the system-provided
7873 @code{link} function.  @xref{Hard Links, , , libc,
7874 The GNU C Library Reference Manual}.
7875 It avoids the bells and whistles of the more commonly-used
7876 @command{ln} command (@pxref{ln invocation}).
7877 Synopsis:
7878
7879 @example
7880 link @var{filename} @var{linkname}
7881 @end example
7882
7883 @var{filename} must specify an existing file, and @var{linkname}
7884 must specify a nonexistent entry in an existing directory.
7885 @command{link} simply calls @code{link (@var{filename}, @var{linkname})}
7886 to create the link.
7887
7888 On a @acronym{GNU} system, this command acts like @samp{ln --directory
7889 --no-target-directory @var{filename} @var{linkname}}.  However, the
7890 @option{--directory} and @option{--no-target-directory} options are
7891 not specified by @acronym{POSIX}, and the @command{link} command is
7892 more portable in practice.
7893
7894 @exitstatus
7895
7896
7897 @node ln invocation
7898 @section @command{ln}: Make links between files
7899
7900 @pindex ln
7901 @cindex links, creating
7902 @cindex hard links, creating
7903 @cindex symbolic (soft) links, creating
7904 @cindex creating links (hard or soft)
7905
7906 @cindex file systems and hard links
7907 @command{ln} makes links between files.  By default, it makes hard links;
7908 with the @option{-s} option, it makes symbolic (or @dfn{soft}) links.
7909 Synopses:
7910
7911 @example
7912 ln [@var{option}]@dots{} [-T] @var{target} @var{linkname}
7913 ln [@var{option}]@dots{} @var{target}
7914 ln [@var{option}]@dots{} @var{target}@dots{} @var{directory}
7915 ln [@var{option}]@dots{} -t @var{directory} @var{target}@dots{}
7916 @end example
7917
7918 @itemize @bullet
7919
7920 @item
7921 If two file names are given, @command{ln} creates a link to the first
7922 file from the second.
7923
7924 @item
7925 If one @var{target} is given, @command{ln} creates a link to that file
7926 in the current directory.
7927
7928 @item
7929 If the @option{--target-directory} (@option{-t}) option is given, or
7930 failing that if the last file is a directory and the
7931 @option{--no-target-directory} (@option{-T}) option is not given,
7932 @command{ln} creates a link to each @var{target} file in the specified
7933 directory, using the @var{target}s' names.
7934
7935 @end itemize
7936
7937 Normally @command{ln} does not remove existing files.  Use the
7938 @option{--force} (@option{-f}) option to remove them unconditionally,
7939 the @option{--interactive} (@option{-i}) option to remove them
7940 conditionally, and the @option{--backup} (@option{-b}) option to
7941 rename them.
7942
7943 @cindex hard link, defined
7944 @cindex inode, and hard links
7945 A @dfn{hard link} is another name for an existing file; the link and the
7946 original are indistinguishable.  Technically speaking, they share the
7947 same inode, and the inode contains all the information about a
7948 file---indeed, it is not incorrect to say that the inode @emph{is} the
7949 file.  On all existing implementations, you cannot make a hard link to
7950 a directory, and hard links cannot cross file system boundaries.  (These
7951 restrictions are not mandated by @acronym{POSIX}, however.)
7952
7953 @cindex dereferencing symbolic links
7954 @cindex symbolic link, defined
7955 @dfn{Symbolic links} (@dfn{symlinks} for short), on the other hand, are
7956 a special file type (which not all kernels support: System V release 3
7957 (and older) systems lack symlinks) in which the link file actually
7958 refers to a different file, by name.  When most operations (opening,
7959 reading, writing, and so on) are passed the symbolic link file, the
7960 kernel automatically @dfn{dereferences} the link and operates on the
7961 target of the link.  But some operations (e.g., removing) work on the
7962 link file itself, rather than on its target.  @xref{Symbolic Links,,,
7963 libc, The GNU C Library Reference Manual}.
7964
7965 The program accepts the following options.  Also see @ref{Common options}.
7966
7967 @table @samp
7968
7969 @optBackup
7970
7971 @item -d
7972 @itemx -F
7973 @itemx --directory
7974 @opindex -d
7975 @opindex -F
7976 @opindex --directory
7977 @cindex hard links to directories
7978 Allow users with appropriate privileges to attempt to make hard links
7979 to directories.
7980 However, note that this will probably fail due to
7981 system restrictions, even for the super-user.
7982
7983 @item -f
7984 @itemx --force
7985 @opindex -f
7986 @opindex --force
7987 Remove existing destination files.
7988
7989 @item -i
7990 @itemx --interactive
7991 @opindex -i
7992 @opindex --interactive
7993 @cindex prompting, and @command{ln}
7994 Prompt whether to remove existing destination files.
7995
7996 @item -n
7997 @itemx --no-dereference
7998 @opindex -n
7999 @opindex --no-dereference
8000 Do not treat the last operand specially when it is a symbolic link to
8001 a directory.  Instead, treat it as if it were a normal file.
8002
8003 When the destination is an actual directory (not a symlink to one),
8004 there is no ambiguity.  The link is created in that directory.
8005 But when the specified destination is a symlink to a directory,
8006 there are two ways to treat the user's request.  @command{ln} can
8007 treat the destination just as it would a normal directory and create
8008 the link in it.  On the other hand, the destination can be viewed as a
8009 non-directory---as the symlink itself.  In that case, @command{ln}
8010 must delete or backup that symlink before creating the new link.
8011 The default is to treat a destination that is a symlink to a directory
8012 just like a directory.
8013
8014 This option is weaker than the @option{--no-target-directory}
8015 (@option{-T}) option, so it has no effect if both options are given.
8016
8017 @item -s
8018 @itemx --symbolic
8019 @opindex -s
8020 @opindex --symbolic
8021 Make symbolic links instead of hard links.  This option merely produces
8022 an error message on systems that do not support symbolic links.
8023
8024 @optBackupSuffix
8025
8026 @optTargetDirectory
8027
8028 @optNoTargetDirectory
8029
8030 @item -v
8031 @itemx --verbose
8032 @opindex -v
8033 @opindex --verbose
8034 Print the name of each file after linking it successfully.
8035
8036 @end table
8037
8038 @exitstatus
8039
8040 Examples:
8041
8042 @smallexample
8043 Bad Example:
8044
8045 # Create link ../a pointing to a in that directory.
8046 # Not really useful because it points to itself.
8047 ln -s a ..
8048
8049 Better Example:
8050
8051 # Change to the target before creating symlinks to avoid being confused.
8052 cd ..
8053 ln -s adir/a .
8054
8055 Bad Example:
8056
8057 # Hard coded file names don't move well.
8058 ln -s $(pwd)/a /some/dir/
8059
8060 Better Example:
8061
8062 # Relative file names survive directory moves and also
8063 # work across networked file systems.
8064 ln -s afile anotherfile
8065 ln -s ../adir/afile yetanotherfile
8066 @end smallexample
8067
8068
8069 @node mkdir invocation
8070 @section @command{mkdir}: Make directories
8071
8072 @pindex mkdir
8073 @cindex directories, creating
8074 @cindex creating directories
8075
8076 @command{mkdir} creates directories with the specified names.  Synopsis:
8077
8078 @example
8079 mkdir [@var{option}]@dots{} @var{name}@dots{}
8080 @end example
8081
8082 @command{mkdir} creates each directory @var{name} in the order given.
8083 It reports an error if @var{name} already exists, unless the
8084 @option{-p} option is given and @var{name} is a directory.
8085
8086 The program accepts the following options.  Also see @ref{Common options}.
8087
8088 @table @samp
8089
8090 @item -m @var{mode}
8091 @itemx --mode=@var{mode}
8092 @opindex -m
8093 @opindex --mode
8094 @cindex modes of created directories, setting
8095 Set the file permission bits of created directories to @var{mode},
8096 which uses the same syntax as
8097 in @command{chmod} and uses @samp{a=rwx} (read, write and execute allowed for
8098 everyone) for the point of the departure.  @xref{File permissions}.
8099
8100 Normally the directory has the desired file mode bits at the moment it
8101 is created.  As a @acronym{GNU} extension, @var{mode} may also mention
8102 special mode bits, but in this case there may be a temporary window
8103 during which the directory exists but its special mode bits are
8104 incorrect.
8105
8106 @item -p
8107 @itemx --parents
8108 @opindex -p
8109 @opindex --parents
8110 @cindex parent directories, creating
8111 Make any missing parent directories for each argument.  The file permission
8112 bits of parent directories are set to the umask modified by @samp{u+wx}.
8113 Ignore arguments corresponding to existing directories, and do not
8114 change their file mode bits.
8115
8116 @item -v
8117 @item --verbose
8118 @opindex -v
8119 @opindex --verbose
8120 Print a message for each created directory.  This is most useful with
8121 @option{--parents}.
8122 @end table
8123
8124 @exitstatus
8125
8126
8127 @node mkfifo invocation
8128 @section @command{mkfifo}: Make FIFOs (named pipes)
8129
8130 @pindex mkfifo
8131 @cindex FIFOs, creating
8132 @cindex named pipes, creating
8133 @cindex creating FIFOs (named pipes)
8134
8135 @command{mkfifo} creates FIFOs (also called @dfn{named pipes}) with the
8136 specified names.  Synopsis:
8137
8138 @example
8139 mkfifo [@var{option}] @var{name}@dots{}
8140 @end example
8141
8142 A @dfn{FIFO} is a special file type that permits independent processes
8143 to communicate.  One process opens the FIFO file for writing, and
8144 another for reading, after which data can flow as with the usual
8145 anonymous pipe in shells or elsewhere.
8146
8147 The program accepts the following option.  Also see @ref{Common options}.
8148
8149 @table @samp
8150
8151 @item -m @var{mode}
8152 @itemx --mode=@var{mode}
8153 @opindex -m
8154 @opindex --mode
8155 @cindex modes of created FIFOs, setting
8156 Set the mode of created FIFOs to @var{mode}, which is symbolic as in
8157 @command{chmod} and uses @samp{a=rw} (read and write allowed for everyone)
8158 for the point of departure.  @var{mode} should specify only file
8159 permission bits.  @xref{File permissions}.
8160
8161 @end table
8162
8163 @exitstatus
8164
8165
8166 @node mknod invocation
8167 @section @command{mknod}: Make block or character special files
8168
8169 @pindex mknod
8170 @cindex block special files, creating
8171 @cindex character special files, creating
8172
8173 @command{mknod} creates a FIFO, character special file, or block special
8174 file with the specified name.  Synopsis:
8175
8176 @example
8177 mknod [@var{option}]@dots{} @var{name} @var{type} [@var{major} @var{minor}]
8178 @end example
8179
8180 @cindex special files
8181 @cindex block special files
8182 @cindex character special files
8183 Unlike the phrase ``special file type'' above, the term @dfn{special
8184 file} has a technical meaning on Unix: something that can generate or
8185 receive data.  Usually this corresponds to a physical piece of hardware,
8186 e.g., a printer or a disk.  (These files are typically created at
8187 system-configuration time.)  The @command{mknod} command is what creates
8188 files of this type.  Such devices can be read either a character at a
8189 time or a ``block'' (many characters) at a time, hence we say there are
8190 @dfn{block special} files and @dfn{character special} files.
8191
8192 The arguments after @var{name} specify the type of file to make:
8193
8194 @table @samp
8195
8196 @item p
8197 @opindex p @r{for FIFO file}
8198 for a FIFO
8199
8200 @item b
8201 @opindex b @r{for block special file}
8202 for a block special file
8203
8204 @item c
8205 @c Don't document the `u' option -- it's just a synonym for `c'.
8206 @c Do *any* versions of mknod still use it?
8207 @c @itemx u
8208 @opindex c @r{for character special file}
8209 @c @opindex u @r{for character special file}
8210 for a character special file
8211
8212 @end table
8213
8214 When making a block or character special file, the major and minor
8215 device numbers must be given after the file type.
8216 If a major or minor device number begins with @samp{0x} or @samp{0X},
8217 it is interpreted as hexadecimal; otherwise, if it begins with @samp{0},
8218 as octal; otherwise, as decimal.
8219
8220 The program accepts the following option.  Also see @ref{Common options}.
8221
8222 @table @samp
8223
8224 @item -m @var{mode}
8225 @itemx --mode=@var{mode}
8226 @opindex -m
8227 @opindex --mode
8228 Set the mode of created files to @var{mode}, which is symbolic as in
8229 @command{chmod} and uses @samp{a=rw} as the point of departure.
8230 @var{mode} should specify only file permission bits.
8231 @xref{File permissions}.
8232
8233 @end table
8234
8235 @exitstatus
8236
8237
8238 @node readlink invocation
8239 @section @command{readlink}: Print the referent of a symbolic link
8240
8241 @pindex readlink
8242 @cindex displaying value of a symbolic link
8243
8244 @command{readlink} may work in one of two supported modes:
8245
8246 @table @samp
8247
8248 @item Readlink mode
8249
8250 @command{readlink} outputs the value of the given symbolic link.
8251 If @command{readlink} is invoked with an argument other than the name
8252 of a symbolic link, it produces no output and exits with a nonzero exit code.
8253
8254 @item Canonicalize mode
8255
8256 @command{readlink} outputs the absolute name of the given file which contains
8257 no @file{.}, @file{..} components nor any repeated separators
8258 (@file{/}) or symbolic links.
8259
8260 @end table
8261
8262 @example
8263 readlink [@var{option}] @var{file}
8264 @end example
8265
8266 By default, @command{readlink} operates in readlink mode.
8267
8268 The program accepts the following options.  Also see @ref{Common options}.
8269
8270 @table @samp
8271
8272 @item -f
8273 @itemx --canonicalize
8274 @opindex -f
8275 @opindex --canonicalize
8276 Activate canonicalize mode.
8277 If any component of the file name except the last one is missing or unavailable,
8278 @command{readlink} produces no output and exits with a nonzero exit code.
8279
8280 @item -e
8281 @itemx --canonicalize-existing
8282 @opindex -e
8283 @opindex --canonicalize-existing
8284 Activate canonicalize mode.
8285 If any component is missing or unavailable, @command{readlink} produces
8286 no output and exits with a nonzero exit code.
8287
8288 @item -m
8289 @itemx --canonicalize-missing
8290 @opindex -m
8291 @opindex --canonicalize-missing
8292 Activate canonicalize mode.
8293 If any component is missing or unavailable, @command{readlink} treats it
8294 as a directory.
8295
8296 @item -n
8297 @itemx --no-newline
8298 @opindex -n
8299 @opindex --no-newline
8300 Do not output the trailing newline.
8301
8302 @item -s
8303 @itemx -q
8304 @itemx --silent
8305 @itemx --quiet
8306 @opindex -s
8307 @opindex -q
8308 @opindex --silent
8309 @opindex --quiet
8310 Suppress most error messages.
8311
8312 @item -v
8313 @itemx --verbose
8314 @opindex -v
8315 @opindex --verbose
8316 Report error messages.
8317
8318 @end table
8319
8320 The @command{readlink} utility first appeared in OpenBSD 2.1.
8321
8322 @exitstatus
8323
8324
8325 @node rmdir invocation
8326 @section @command{rmdir}: Remove empty directories
8327
8328 @pindex rmdir
8329 @cindex removing empty directories
8330 @cindex directories, removing empty
8331
8332 @command{rmdir} removes empty directories.  Synopsis:
8333
8334 @example
8335 rmdir [@var{option}]@dots{} @var{directory}@dots{}
8336 @end example
8337
8338 If any @var{directory} argument does not refer to an existing empty
8339 directory, it is an error.
8340
8341 The program accepts the following option.  Also see @ref{Common options}.
8342
8343 @table @samp
8344
8345 @item --ignore-fail-on-non-empty
8346 @opindex --ignore-fail-on-non-empty
8347 @cindex directory deletion, ignoring failures
8348 Ignore each failure to remove a directory that is solely because
8349 the directory is non-empty.
8350
8351 @item -p
8352 @itemx --parents
8353 @opindex -p
8354 @opindex --parents
8355 @cindex parent directories, removing
8356 Remove @var{directory}, then try to remove each component of @var{directory}.
8357 So, for example, @samp{rmdir -p a/b/c} is similar to @samp{rmdir a/b/c a/b a}.
8358 As such, it fails if any of those directories turns out not to be empty.
8359 Use the @option{--ignore-fail-on-non-empty} option to make it so such
8360 a failure does not evoke a diagnostic and does not cause @command{rmdir} to
8361 exit unsuccessfully.
8362
8363 @item -v
8364 @item --verbose
8365 @opindex -v
8366 @opindex --verbose
8367 @cindex directory deletion, reporting
8368 Give a diagnostic for each successful removal.
8369 @var{directory} is removed.
8370
8371 @end table
8372
8373 @xref{rm invocation}, for how to remove non-empty directories (recursively).
8374
8375 @exitstatus
8376
8377
8378 @node unlink invocation
8379 @section @command{unlink}: Remove files via the unlink syscall
8380
8381 @pindex unlink
8382 @cindex removing files or directories (via the unlink syscall)
8383
8384 @command{unlink} deletes a single specified file name.
8385 It is a minimalist interface to the system-provided
8386 @code{unlink} function.  @xref{Deleting Files, , , libc,
8387 The GNU C Library Reference Manual}.  Synopsis:
8388 It avoids the bells and whistles of the more commonly-used
8389 @command{rm} command (@pxref{rm invocation}).
8390
8391 @example
8392 unlink @var{filename}
8393 @end example
8394
8395 On some systems @code{unlink} can be used to delete the name of a
8396 directory.  On others, it can be used that way only by a privileged user.
8397 In the GNU system @code{unlink} can never delete the name of a directory.
8398
8399 The @command{unlink} command honors the @option{--help} and
8400 @option{--version} options.  To remove a file whose name begins with
8401 @samp{-}, prefix the name with @samp{./}, e.g., @samp{unlink ./--help}.
8402
8403 @exitstatus
8404
8405
8406 @node Changing file attributes
8407 @chapter Changing file attributes
8408
8409 @cindex changing file attributes
8410 @cindex file attributes, changing
8411 @cindex attributes, file
8412
8413 A file is not merely its contents, a name, and a file type
8414 (@pxref{Special file types}).  A file also has an owner (a user ID), a
8415 group (a group ID), permissions (what the owner can do with the file,
8416 what people in the group can do, and what everyone else can do), various
8417 timestamps, and other information.  Collectively, we call these a file's
8418 @dfn{attributes}.
8419
8420 These commands change file attributes.
8421
8422 @menu
8423 * chgrp invocation::            Change file groups.
8424 * chmod invocation::            Change access permissions.
8425 * chown invocation::            Change file owners and groups.
8426 * touch invocation::            Change file timestamps.
8427 @end menu
8428
8429
8430 @node chown invocation
8431 @section @command{chown}: Change file owner and group
8432
8433 @pindex chown
8434 @cindex file ownership, changing
8435 @cindex group ownership, changing
8436 @cindex changing file ownership
8437 @cindex changing group ownership
8438
8439 @command{chown} changes the user and/or group ownership of each given @var{file}
8440 to @var{new-owner} or to the user and group of an existing reference file.
8441 Synopsis:
8442
8443 @example
8444 chown [@var{option}]@dots{} @{@var{new-owner} | --reference=@var{ref_file}@} @var{file}@dots{}
8445 @end example
8446
8447 If used, @var{new-owner} specifies the new owner and/or group as follows
8448 (with no embedded white space):
8449
8450 @example
8451 [@var{owner}] [ : [@var{group}] ]
8452 @end example
8453
8454 Specifically:
8455
8456 @table @var
8457 @item owner
8458 If only an @var{owner} (a user name or numeric user ID) is given, that
8459 user is made the owner of each given file, and the files' group is not
8460 changed.
8461
8462 @item owner@samp{:}group
8463 If the @var{owner} is followed by a colon and a @var{group} (a
8464 group name or numeric group ID), with no spaces between them, the group
8465 ownership of the files is changed as well (to @var{group}).
8466
8467 @item owner@samp{:}
8468 If a colon but no group name follows @var{owner}, that user is
8469 made the owner of the files and the group of the files is changed to
8470 @var{owner}'s login group.
8471
8472 @item @samp{:}group
8473 If the colon and following @var{group} are given, but the owner
8474 is omitted, only the group of the files is changed; in this case,
8475 @command{chown} performs the same function as @command{chgrp}.
8476
8477 @item @samp{:}
8478 If only a colon is given, or if @var{new-owner} is empty, neither the
8479 owner nor the group is changed.
8480
8481 @end table
8482
8483 Some older scripts may still use @samp{.} in place of the @samp{:} separator.
8484 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not
8485 require support for that, but for backward compatibility @acronym{GNU}
8486 @command{chown} supports @samp{.} so long as no ambiguity results.
8487 New scripts should avoid the use of @samp{.} because it is not
8488 portable, and because it has undesirable results if the entire
8489 @var{owner@samp{.}group} happens to identify a user whose name
8490 contains @samp{.}.
8491
8492 The @command{chown} command sometimes clears the set-user-ID or
8493 set-group-ID permission bits.  This behavior depends on the policy and
8494 functionality of the underlying @code{chown} system call, which may
8495 make system-dependent file mode modifications outside the control of
8496 the @command{chown} command.  For example, the @command{chown} command
8497 might not affect those bits when invoked by a user with appropriate
8498 privileges, or when the
8499 bits signify some function other than executable permission (e.g.,
8500 mandatory locking).
8501 When in doubt, check the underlying system behavior.
8502
8503 The program accepts the following options.  Also see @ref{Common options}.
8504
8505 @table @samp
8506
8507 @item -c
8508 @itemx --changes
8509 @opindex -c
8510 @opindex --changes
8511 @cindex changed owners, verbosely describing
8512 Verbosely describe the action for each @var{file} whose ownership
8513 actually changes.
8514
8515 @item -f
8516 @itemx --silent
8517 @itemx --quiet
8518 @opindex -f
8519 @opindex --silent
8520 @opindex --quiet
8521 @cindex error messages, omitting
8522 Do not print error messages about files whose ownership cannot be
8523 changed.
8524
8525 @itemx @w{@kbd{--from}=@var{old-owner}}
8526 @opindex --from
8527 @cindex symbolic links, changing owner
8528 Change a @var{file}'s ownership only if it has current attributes specified
8529 by @var{old-owner}.  @var{old-owner} has the same form as @var{new-owner}
8530 described above.
8531 This option is useful primarily from a security standpoint in that
8532 it narrows considerably the window of potential abuse.
8533 For example, to reflect a user ID numbering change for one user's files
8534 without an option like this, @code{root} might run
8535
8536 @smallexample
8537 find / -owner OLDUSER -print0 | xargs -0 chown -h NEWUSER
8538 @end smallexample
8539
8540 But that is dangerous because the interval between when the @command{find}
8541 tests the existing file's owner and when the @command{chown} is actually run
8542 may be quite large.
8543 One way to narrow the gap would be to invoke chown for each file
8544 as it is found:
8545
8546 @example
8547 find / -owner OLDUSER -exec chown -h NEWUSER @{@} \;
8548 @end example
8549
8550 But that is very slow if there are many affected files.
8551 With this option, it is safer (the gap is narrower still)
8552 though still not perfect:
8553
8554 @example
8555 chown -h -R --from=OLDUSER NEWUSER /
8556 @end example
8557
8558 @item --dereference
8559 @opindex --dereference
8560 @cindex symbolic links, changing owner
8561 @findex lchown
8562 Do not act on symbolic links themselves but rather on what they point to.
8563 This is the default.
8564
8565 @item -h
8566 @itemx --no-dereference
8567 @opindex -h
8568 @opindex --no-dereference
8569 @cindex symbolic links, changing owner
8570 @findex lchown
8571 Act on symbolic links themselves instead of what they point to.
8572 This mode relies on the @code{lchown} system call.
8573 On systems that do not provide the @code{lchown} system call,
8574 @command{chown} fails when a file specified on the command line
8575 is a symbolic link.
8576 By default, no diagnostic is issued for symbolic links encountered
8577 during a recursive traversal, but see @option{--verbose}.
8578
8579 @itemx --preserve-root
8580 @opindex --preserve-root
8581 @cindex root directory, disallow recursive modification
8582 Fail upon any attempt to recursively change the file system root, @file{/}.
8583 Without @option{--recursive}, this option has no effect.
8584 @xref{Treating / specially}.
8585
8586 @itemx --no-preserve-root
8587 @opindex --no-preserve-root
8588 @cindex root directory, allow recursive modification
8589 Cancel the effect of any preceding @option{--preserve-root} option.
8590 @xref{Treating / specially}.
8591
8592 @item --reference=@var{ref_file}
8593 @opindex --reference
8594 Change the user and group of each @var{file} to be the same as those of
8595 @var{ref_file}.  If @var{ref_file} is a symbolic link, do not use the
8596 user and group of the symbolic link, but rather those of the file it
8597 refers to.
8598
8599 @item -v
8600 @itemx --verbose
8601 @opindex -v
8602 @opindex --verbose
8603 Output a diagnostic for every file processed.
8604 If a symbolic link is encountered during a recursive traversal
8605 on a system without the @code{lchown} system call, and @option{--no-dereference}
8606 is in effect, then issue a diagnostic saying neither the symbolic link nor
8607 its referent is being changed.
8608
8609 @item -R
8610 @itemx --recursive
8611 @opindex -R
8612 @opindex --recursive
8613 @cindex recursively changing file ownership
8614 Recursively change ownership of directories and their contents.
8615
8616 @choptH
8617 @xref{Traversing symlinks}.
8618
8619 @choptL
8620 @xref{Traversing symlinks}.
8621
8622 @choptP
8623 @xref{Traversing symlinks}.
8624
8625 @end table
8626
8627 @exitstatus
8628
8629 Examples:
8630
8631 @smallexample
8632 # Change the owner of /u to "root".
8633 chown root /u
8634
8635 # Likewise, but also change its group to "staff".
8636 chown root:staff /u
8637
8638 # Change the owner of /u and subfiles to "root".
8639 chown -hR root /u
8640 @end smallexample
8641
8642
8643 @node chgrp invocation
8644 @section @command{chgrp}: Change group ownership
8645
8646 @pindex chgrp
8647 @cindex group ownership, changing
8648 @cindex changing group ownership
8649
8650 @command{chgrp} changes the group ownership of each given @var{file}
8651 to @var{group} (which can be either a group name or a numeric group ID)
8652 or to the group of an existing reference file.  Synopsis:
8653
8654 @example
8655 chgrp [@var{option}]@dots{} @{@var{group} | --reference=@var{ref_file}@} @var{file}@dots{}
8656 @end example
8657
8658 The program accepts the following options.  Also see @ref{Common options}.
8659
8660 @table @samp
8661
8662 @item -c
8663 @itemx --changes
8664 @opindex -c
8665 @opindex --changes
8666 @cindex changed files, verbosely describing
8667 Verbosely describe the action for each @var{file} whose group actually
8668 changes.
8669
8670 @item -f
8671 @itemx --silent
8672 @itemx --quiet
8673 @opindex -f
8674 @opindex --silent
8675 @opindex --quiet
8676 @cindex error messages, omitting
8677 Do not print error messages about files whose group cannot be
8678 changed.
8679
8680 @item --dereference
8681 @opindex --dereference
8682 @cindex symbolic links, changing owner
8683 @findex lchown
8684 Do not act on symbolic links themselves but rather on what they point to.
8685 This is the default.
8686
8687 @item -h
8688 @itemx --no-dereference
8689 @opindex -h
8690 @opindex --no-dereference
8691 @cindex symbolic links, changing group
8692 @findex lchown
8693 Act on symbolic links themselves instead of what they point to.
8694 This mode relies on the @code{lchown} system call.
8695 On systems that do not provide the @code{lchown} system call,
8696 @command{chgrp} fails when a file specified on the command line
8697 is a symbolic link.
8698 By default, no diagnostic is issued for symbolic links encountered
8699 during a recursive traversal, but see @option{--verbose}.
8700
8701 @itemx --preserve-root
8702 @opindex --preserve-root
8703 @cindex root directory, disallow recursive modification
8704 Fail upon any attempt to recursively change the file system root, @file{/}.
8705 Without @option{--recursive}, this option has no effect.
8706 @xref{Treating / specially}.
8707
8708 @itemx --no-preserve-root
8709 @opindex --no-preserve-root
8710 @cindex root directory, allow recursive modification
8711 Cancel the effect of any preceding @option{--preserve-root} option.
8712 @xref{Treating / specially}.
8713
8714 @item --reference=@var{ref_file}
8715 @opindex --reference
8716 Change the group of each @var{file} to be the same as that of
8717 @var{ref_file}.  If @var{ref_file} is a symbolic link, do not use the
8718 group of the symbolic link, but rather that of the file it refers to.
8719
8720 @item -v
8721 @itemx --verbose
8722 @opindex -v
8723 @opindex --verbose
8724 Output a diagnostic for every file processed.
8725 If a symbolic link is encountered during a recursive traversal
8726 on a system without the @code{lchown} system call, and @option{--no-dereference}
8727 is in effect, then issue a diagnostic saying neither the symbolic link nor
8728 its referent is being changed.
8729
8730 @item -R
8731 @itemx --recursive
8732 @opindex -R
8733 @opindex --recursive
8734 @cindex recursively changing group ownership
8735 Recursively change the group ownership of directories and their contents.
8736
8737 @choptH
8738 @xref{Traversing symlinks}.
8739
8740 @choptL
8741 @xref{Traversing symlinks}.
8742
8743 @choptP
8744 @xref{Traversing symlinks}.
8745
8746 @end table
8747
8748 @exitstatus
8749
8750 Examples:
8751
8752 @smallexample
8753 # Change the group of /u to "staff".
8754 chgrp staff /u
8755
8756 # Change the group of /u and subfiles to "staff".
8757 chgrp -hR staff /u
8758 @end smallexample
8759
8760
8761 @node chmod invocation
8762 @section @command{chmod}: Change access permissions
8763
8764 @pindex chmod
8765 @cindex changing access permissions
8766 @cindex access permissions, changing
8767 @cindex permissions, changing access
8768
8769 @command{chmod} changes the access permissions of the named files.  Synopsis:
8770
8771 @example
8772 chmod [@var{option}]@dots{} @{@var{mode} | --reference=@var{ref_file}@} @var{file}@dots{}
8773 @end example
8774
8775 @cindex symbolic links, permissions of
8776 @command{chmod} never changes the permissions of symbolic links, since
8777 the @command{chmod} system call cannot change their permissions.
8778 This is not a problem since the permissions of symbolic links are
8779 never used.  However, for each symbolic link listed on the command
8780 line, @command{chmod} changes the permissions of the pointed-to file.
8781 In contrast, @command{chmod} ignores symbolic links encountered during
8782 recursive directory traversals.
8783
8784 A successful use of @command{chmod} clears the set-group-ID bit of a
8785 regular file if the file's group ID does not match the user's
8786 effective group ID or one of the user's supplementary group IDs,
8787 unless the user has appropriate privileges.  Additional restrictions
8788 may cause the set-user-ID and set-group-ID bits of @var{mode} or
8789 @var{ref_file} to be ignored.  This behavior depends on the policy and
8790 functionality of the underlying @code{chmod} system call.  When in
8791 doubt, check the underlying system behavior.
8792
8793 If used, @var{mode} specifies the new file mode bits.
8794 For details, see the section on @ref{File permissions}.
8795 If you really want @var{mode} to have a leading @samp{-}, you should
8796 use @option{--} first, e.g., @samp{chmod -- -w file}.  Typically,
8797 though, @samp{chmod a-w file} is preferable, and @command{chmod -w
8798 file} (without the @option{--}) complains if it behaves differently
8799 from what @samp{chmod a-w file} would do.
8800
8801 The program accepts the following options.  Also see @ref{Common options}.
8802
8803 @table @samp
8804
8805 @item -c
8806 @itemx --changes
8807 @opindex -c
8808 @opindex --changes
8809 Verbosely describe the action for each @var{file} whose permissions
8810 actually changes.
8811
8812 @item -f
8813 @itemx --silent
8814 @itemx --quiet
8815 @opindex -f
8816 @opindex --silent
8817 @opindex --quiet
8818 @cindex error messages, omitting
8819 Do not print error messages about files whose permissions cannot be
8820 changed.
8821
8822 @itemx --preserve-root
8823 @opindex --preserve-root
8824 @cindex root directory, disallow recursive modification
8825 Fail upon any attempt to recursively change the file system root, @file{/}.
8826 Without @option{--recursive}, this option has no effect.
8827 @xref{Treating / specially}.
8828
8829 @itemx --no-preserve-root
8830 @opindex --no-preserve-root
8831 @cindex root directory, allow recursive modification
8832 Cancel the effect of any preceding @option{--preserve-root} option.
8833 @xref{Treating / specially}.
8834
8835 @item -v
8836 @itemx --verbose
8837 @opindex -v
8838 @opindex --verbose
8839 Verbosely describe the action or non-action taken for every @var{file}.
8840
8841 @item --reference=@var{ref_file}
8842 @opindex --reference
8843 Change the mode of each @var{file} to be the same as that of @var{ref_file}.
8844 @xref{File permissions}.
8845 If @var{ref_file} is a symbolic link, do not use the mode
8846 of the symbolic link, but rather that of the file it refers to.
8847
8848 @item -R
8849 @itemx --recursive
8850 @opindex -R
8851 @opindex --recursive
8852 @cindex recursively changing access permissions
8853 Recursively change permissions of directories and their contents.
8854
8855 @end table
8856
8857 @exitstatus
8858
8859
8860 @node touch invocation
8861 @section @command{touch}: Change file timestamps
8862
8863 @pindex touch
8864 @cindex changing file timestamps
8865 @cindex file timestamps, changing
8866 @cindex timestamps, changing file
8867
8868 @command{touch} changes the access and/or modification times of the
8869 specified files.  Synopsis:
8870
8871 @example
8872 touch [@var{option}]@dots{} @var{file}@dots{}
8873 @end example
8874
8875 @cindex empty files, creating
8876 Any @var{file} that does not exist is created empty.
8877
8878 A @var{file} of @samp{-} causes @command{touch} to change the
8879 times of the file associated with standard output.
8880
8881 @cindex permissions, for changing file timestamps
8882 If changing both the access and modification times to the current
8883 time, @command{touch} can change the timestamps for files that the user
8884 running it does not own but has write permission for.  Otherwise, the
8885 user must own the files.
8886
8887 Although @command{touch} provides options for changing two of the times---the
8888 times of last access and modification---of a file, there is actually
8889 a third one as well: the inode change time.  This is often referred to
8890 as a file's @code{ctime}.
8891 The inode change time represents the time when the file's meta-information
8892 last changed.  One common example of this is when the permissions of a
8893 file change.  Changing the permissions doesn't access the file, so
8894 the atime doesn't change, nor does it modify the file, so the mtime
8895 doesn't change.  Yet, something about the file itself has changed,
8896 and this must be noted somewhere.  This is the job of the ctime field.
8897 This is necessary, so that, for example, a backup program can make a
8898 fresh copy of the file, including the new permissions value.
8899 Another operation that modifies a file's ctime without affecting
8900 the others is renaming.  In any case, it is not possible, in normal
8901 operations, for a user to change the ctime field to a user-specified value.
8902
8903 @vindex TZ
8904 Time stamps assume the time zone rules specified by the @env{TZ}
8905 environment variable, or by the system default rules if @env{TZ} is
8906 not set.  @xref{TZ Variable,, Specifying the Time Zone with @env{TZ},
8907 libc, The GNU C Library}.  You can avoid avoid ambiguities during
8908 daylight saving transitions by using @sc{utc} time stamps.
8909
8910 The program accepts the following options.  Also see @ref{Common options}.
8911
8912 @table @samp
8913
8914 @item -a
8915 @itemx --time=atime
8916 @itemx --time=access
8917 @itemx --time=use
8918 @opindex -a
8919 @opindex --time
8920 @opindex atime@r{, changing}
8921 @opindex access @r{time, changing}
8922 @opindex use @r{time, changing}
8923 Change the access time only.
8924
8925 @item -c
8926 @itemx --no-create
8927 @opindex -c
8928 @opindex --no-create
8929 Do not create files that do not exist.
8930
8931 @item -d
8932 @itemx --date=@var{time}
8933 @opindex -d
8934 @opindex --date
8935 @opindex time
8936 Use @var{time} instead of the current time.  It can contain month names,
8937 time zones, @samp{am} and @samp{pm}, @samp{yesterday}, etc.  For
8938 example, @option{--date="2004-02-27 14:19:13.489392193 +0530"}
8939 specifies the instant of time that is 489,392,193 nanoseconds after
8940 February 27, 2004 at 2:19:13 PM in a time zone that is 5 hours and 30
8941 minutes east of @acronym{UTC}.  @xref{Date input formats}.
8942 File systems that do not support high-resolution time stamps
8943 silently ignore any excess precision here.
8944
8945 @item -f
8946 @opindex -f
8947 @cindex BSD @command{touch} compatibility
8948 Ignored; for compatibility with BSD versions of @command{touch}.
8949
8950 @item -m
8951 @itemx --time=mtime
8952 @itemx --time=modify
8953 @opindex -m
8954 @opindex --time
8955 @opindex mtime@r{, changing}
8956 @opindex modify @r{time, changing}
8957 Change the modification time only.
8958
8959 @item -r @var{file}
8960 @itemx --reference=@var{file}
8961 @opindex -r
8962 @opindex --reference
8963 Use the times of the reference @var{file} instead of the current time.
8964 If this option is combined with the @option{--date=@var{time}}
8965 (@option{-d @var{time}}) option, the reference @var{file}'s time is
8966 the origin for any relative @var{time}s given, but is otherwise ignored.
8967 For example, @samp{-r foo -d '-5 seconds'} specifies a time stamp
8968 equal to five seconds before the corresponding time stamp for @file{foo}.
8969
8970 @item -t [[@var{CC}]@var{YY}]@var{MMDDhhmm}[.@var{ss}]
8971 Use the argument (optional four-digit or two-digit years, months,
8972 days, hours, minutes, optional seconds) instead of the current time.
8973 If the year is specified with only two digits, then @var{CC}
8974 is 20 for years in the range 0 @dots{} 68, and 19 for years in
8975 69 @dots{} 99.  If no digits of the year are specified,
8976 the argument is interpreted as a date in the current year.
8977
8978 @end table
8979
8980 @vindex _POSIX2_VERSION
8981 On older systems, @command{touch} supports an obsolete syntax, as follows.
8982 If no timestamp is given with any of the @option{-d}, @option{-r}, or
8983 @option{-t} options, and if there are two or more @var{file}s and the
8984 first @var{file} is of the form @samp{@var{MMDDhhmm}[@var{YY}]} and this
8985 would be a valid argument to the @option{-t} option (if the @var{YY}, if
8986 any, were moved to the front), and if the represented year
8987 is in the range 1969--1999, that argument is interpreted as the time
8988 for the other files instead of as a file name.
8989 This obsolete behavior can be enabled or disabled with the
8990 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
8991 conformance}), but portable scripts should avoid commands whose
8992 behavior depends on this variable.
8993 For example, use @samp{touch ./12312359 main.c} or @samp{touch -t
8994 12312359 main.c} rather than the ambiguous @samp{touch 12312359 main.c}.
8995
8996 @exitstatus
8997
8998
8999 @node Disk usage
9000 @chapter Disk usage
9001
9002 @cindex disk usage
9003
9004 No disk can hold an infinite amount of data.  These commands report on
9005 how much disk storage is in use or available.  (This has nothing much to
9006 do with how much @emph{main memory}, i.e., RAM, a program is using when
9007 it runs; for that, you want @command{ps} or @command{pstat} or @command{swap}
9008 or some such command.)
9009
9010 @menu
9011 * df invocation::               Report file system disk space usage.
9012 * du invocation::               Estimate file space usage.
9013 * stat invocation::             Report file or file system status.
9014 * sync invocation::             Synchronize memory and disk.
9015 @end menu
9016
9017
9018 @node df invocation
9019 @section @command{df}: Report file system disk space usage
9020
9021 @pindex df
9022 @cindex file system disk usage
9023 @cindex disk usage by file system
9024
9025 @command{df} reports the amount of disk space used and available on
9026 file systems.  Synopsis:
9027
9028 @example
9029 df [@var{option}]@dots{} [@var{file}]@dots{}
9030 @end example
9031
9032 With no arguments, @command{df} reports the space used and available on all
9033 currently mounted file systems (of all types).  Otherwise, @command{df}
9034 reports on the file system containing each argument @var{file}.
9035
9036 Normally the disk space is printed in units of
9037 1024 bytes, but this can be overridden (@pxref{Block size}).
9038 Non-integer quantities are rounded up to the next higher unit.
9039
9040 @cindex disk device file
9041 @cindex device file, disk
9042 If an argument @var{file} is a disk device file containing a mounted
9043 file system, @command{df} shows the space available on that file system
9044 rather than on the file system containing the device node (i.e., the root
9045 file system).  @sc{gnu} @command{df} does not attempt to determine the disk usage
9046 on unmounted file systems, because on most kinds of systems doing so
9047 requires extremely nonportable intimate knowledge of file system
9048 structures.
9049
9050 The program accepts the following options.  Also see @ref{Common options}.
9051
9052 @table @samp
9053
9054 @item -a
9055 @itemx --all
9056 @opindex -a
9057 @opindex --all
9058 @cindex automounter file systems
9059 @cindex ignore file systems
9060 Include in the listing dummy file systems, which
9061 are omitted by default.  Such file systems are typically special-purpose
9062 pseudo-file-systems, such as automounter entries.
9063
9064 @item -B @var{size}
9065 @itemx --block-size=@var{size}
9066 @opindex -B
9067 @opindex --block-size
9068 @cindex file system sizes
9069 Scale sizes by @var{size} before printing them (@pxref{Block size}).
9070 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
9071
9072 @optHumanReadable
9073
9074 @item -H
9075 @opindex -H
9076 Equivalent to @option{--si}.
9077
9078 @item -i
9079 @itemx --inodes
9080 @opindex -i
9081 @opindex --inodes
9082 @cindex inode usage
9083 List inode usage information instead of block usage.  An inode (short
9084 for index node) contains information about a file such as its owner,
9085 permissions, timestamps, and location on the disk.
9086
9087 @item -k
9088 @opindex -k
9089 @cindex kibibytes for file system sizes
9090 Print sizes in 1024-byte blocks, overriding the default block size
9091 (@pxref{Block size}).
9092 This option is equivalent to @option{--block-size=1K}.
9093
9094 @item -l
9095 @itemx --local
9096 @opindex -l
9097 @opindex --local
9098 @cindex file system types, limiting output to certain
9099 Limit the listing to local file systems.  By default, remote file systems
9100 are also listed.
9101
9102 @item --no-sync
9103 @opindex --no-sync
9104 @cindex file system space, retrieving old data more quickly
9105 Do not invoke the @code{sync} system call before getting any usage data.
9106 This may make @command{df} run significantly faster on systems with many
9107 disks, but on some systems (notably SunOS) the results may be slightly
9108 out of date.  This is the default.
9109
9110 @item -P
9111 @itemx --portability
9112 @opindex -P
9113 @opindex --portability
9114 @cindex one-line output format
9115 @cindex @acronym{POSIX} output format
9116 @cindex portable output format
9117 @cindex output format, portable
9118 Use the @acronym{POSIX} output format.  This is like the default format except
9119 for the following:
9120
9121 @enumerate
9122 @item
9123 The information about each file system is always printed on exactly
9124 one line; a mount device is never put on a line by itself.  This means
9125 that if the mount device name is more than 20 characters long (e.g., for
9126 some network mounts), the columns are misaligned.
9127
9128 @item
9129 The labels in the header output line are changed to conform to @acronym{POSIX}.
9130 @end enumerate
9131
9132 @optSi
9133
9134 @item --sync
9135 @opindex --sync
9136 @cindex file system space, retrieving current data more slowly
9137 Invoke the @code{sync} system call before getting any usage data.  On
9138 some systems (notably SunOS), doing this yields more up to date results,
9139 but in general this option makes @command{df} much slower, especially when
9140 there are many or very busy file systems.
9141
9142 @item -t @var{fstype}
9143 @itemx --type=@var{fstype}
9144 @opindex -t
9145 @opindex --type
9146 @cindex file system types, limiting output to certain
9147 Limit the listing to file systems of type @var{fstype}.  Multiple
9148 file system types can be specified by giving multiple @option{-t} options.
9149 By default, nothing is omitted.
9150
9151 @item -T
9152 @itemx --print-type
9153 @opindex -T
9154 @opindex --print-type
9155 @cindex file system types, printing
9156 Print each file system's type.  The types printed here are the same ones
9157 you can include or exclude with @option{-t} and @option{-x}.  The particular
9158 types printed are whatever is supported by the system.  Here are some of
9159 the common names (this list is certainly not exhaustive):
9160
9161 @table @samp
9162
9163 @item nfs
9164 @cindex @acronym{NFS} file system type
9165 An @acronym{NFS} file system, i.e., one mounted over a network from another
9166 machine.  This is the one type name which seems to be used uniformly by
9167 all systems.
9168
9169 @item 4.2@r{, }ufs@r{, }efs@dots{}
9170 @cindex Linux file system types
9171 @cindex local file system types
9172 @opindex 4.2 @r{file system type}
9173 @opindex ufs @r{file system type}
9174 @opindex efs @r{file system type}
9175 A file system on a locally-mounted hard disk.  (The system might even
9176 support more than one type here; Linux does.)
9177
9178 @item hsfs@r{, }cdfs
9179 @cindex CD-ROM file system type
9180 @cindex High Sierra file system
9181 @opindex hsfs @r{file system type}
9182 @opindex cdfs @r{file system type}
9183 A file system on a CD-ROM drive.  HP-UX uses @samp{cdfs}, most other
9184 systems use @samp{hsfs} (@samp{hs} for ``High Sierra'').
9185
9186 @item pcfs
9187 @cindex PC file system
9188 @cindex DOS file system
9189 @cindex MS-DOS file system
9190 @cindex diskette file system
9191 @opindex pcfs
9192 An MS-DOS file system, usually on a diskette.
9193
9194 @end table
9195
9196 @item -x @var{fstype}
9197 @itemx --exclude-type=@var{fstype}
9198 @opindex -x
9199 @opindex --exclude-type
9200 Limit the listing to file systems not of type @var{fstype}.
9201 Multiple file system types can be eliminated by giving multiple
9202 @option{-x} options.  By default, no file system types are omitted.
9203
9204 @item -v
9205 Ignored; for compatibility with System V versions of @command{df}.
9206
9207 @end table
9208
9209 @exitstatus
9210
9211
9212 @node du invocation
9213 @section @command{du}: Estimate file space usage
9214
9215 @pindex du
9216 @cindex file space usage
9217 @cindex disk usage for files
9218
9219 @command{du} reports the amount of disk space used by the specified files
9220 and for each subdirectory (of directory arguments).  Synopsis:
9221
9222 @example
9223 du [@var{option}]@dots{} [@var{file}]@dots{}
9224 @end example
9225
9226 With no arguments, @command{du} reports the disk space for the current
9227 directory.  Normally the disk space is printed in units of
9228 1024 bytes, but this can be overridden (@pxref{Block size}).
9229 Non-integer quantities are rounded up to the next higher unit.
9230
9231 The program accepts the following options.  Also see @ref{Common options}.
9232
9233 @table @samp
9234
9235 @item -a
9236 @itemx --all
9237 @opindex -a
9238 @opindex --all
9239 Show counts for all files, not just directories.
9240
9241 @itemx --apparent-size
9242 @opindex --apparent-size
9243 Print apparent sizes, rather than disk usage.  The apparent size of a
9244 file is the number of bytes reported by @code{wc -c} on regular files,
9245 or more generally, @code{ls -l --block-size=1} or @code{stat --format=%s}.
9246 For example, a file containing the word @samp{zoo} with no newline would,
9247 of course, have an apparent size of 3.  Such a small file may require
9248 anywhere from 0 to 16 KiB or more of disk space, depending on
9249 the type and configuration of the file system on which the file resides.
9250 However, a sparse file created with this command:
9251
9252 @example
9253 dd bs=1 seek=2GiB if=/dev/null of=big
9254 @end example
9255
9256 @noindent
9257 has an apparent size of 2 GiB, yet on most modern
9258 systems, it actually uses almost no disk space.
9259
9260 @item -b
9261 @itemx --bytes
9262 @opindex -b
9263 @opindex --bytes
9264 Equivalent to @code{--apparent-size --block-size=1}.
9265
9266 @item -B @var{size}
9267 @itemx --block-size=@var{size}
9268 @opindex -B
9269 @opindex --block-size
9270 @cindex file sizes
9271 Scale sizes by @var{size} before printing them (@pxref{Block size}).
9272 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
9273
9274 @item -c
9275 @itemx --total
9276 @opindex -c
9277 @opindex --total
9278 @cindex grand total of disk space
9279 Print a grand total of all arguments after all arguments have
9280 been processed.  This can be used to find out the total disk usage of
9281 a given set of files or directories.
9282
9283 @item -D
9284 @itemx --dereference-args
9285 @opindex -D
9286 @opindex --dereference-args
9287 Dereference symbolic links that are command line arguments.
9288 Does not affect other symbolic links.  This is helpful for finding
9289 out the disk usage of directories, such as @file{/usr/tmp}, which
9290 are often symbolic links.
9291
9292 @itemx --files0-from=@var{FILE}
9293 @opindex --files0-from=@var{FILE}
9294 @cindex including files from @command{du}
9295 Rather than processing files named on the command line, process those
9296 named in file @var{FILE}; each name is terminated by a null byte.
9297 This is useful with the @option{--total} (@option{-c}) option when
9298 the list of file names is so long that it may exceed a command line
9299 length limitation.
9300 In such cases, running @command{du} via @command{xargs} is undesirable
9301 because it splits the list into pieces and makes @command{du} print a
9302 total for each sublist rather than for the entire list.
9303 One way to produce a list of null-byte-terminated file names is with @sc{gnu}
9304 @command{find}, using its @option{-print0} predicate.
9305 Do not specify any @var{FILE} on the command line when using this option.
9306
9307 @optHumanReadable
9308
9309 @item -H
9310 @opindex -H
9311 Currently, @option{-H} is the same as @option{--si},
9312 except that @option{-H} evokes a warning.
9313 This option will be changed to be equivalent to
9314 @option{--dereference-args} (@option{-D}).
9315
9316 @item -k
9317 @opindex -k
9318 @cindex kibibytes for file sizes
9319 Print sizes in 1024-byte blocks, overriding the default block size
9320 (@pxref{Block size}).
9321 This option is equivalent to @option{--block-size=1K}.
9322
9323 @item -l
9324 @itemx --count-links
9325 @opindex -l
9326 @opindex --count-links
9327 @cindex hard links, counting in @command{du}
9328 Count the size of all files, even if they have appeared already (as a
9329 hard link).
9330
9331 @item -L
9332 @itemx --dereference
9333 @opindex -L
9334 @opindex --dereference
9335 @cindex symbolic links, dereferencing in @command{du}
9336 Dereference symbolic links (show the disk space used by the file
9337 or directory that the link points to instead of the space used by
9338 the link).
9339
9340 @item -m
9341 @opindex -m
9342 @cindex mebibytes for file sizes
9343 Print sizes in 1,048,576-byte blocks, overriding the default block size
9344 (@pxref{Block size}).
9345 This option is equivalent to @option{--block-size=1M}.
9346
9347 @item -P
9348 @itemx --no-dereference
9349 @opindex -P
9350 @opindex --no-dereference
9351 @cindex symbolic links, dereferencing in @command{du}
9352 For each symbolic links encountered by @command{du},
9353 consider the disk space used by the symbolic link.
9354
9355 @item --max-depth=@var{DEPTH}
9356 @opindex --max-depth=@var{DEPTH}
9357 @cindex limiting output of @command{du}
9358 Show the total for each directory (and file if --all) that is at
9359 most MAX_DEPTH levels down from the root of the hierarchy.  The root
9360 is at level 0, so @code{du --max-depth=0} is equivalent to @code{du -s}.
9361
9362 @item -0
9363 @opindex -0
9364 @itemx --null
9365 @opindex --null
9366 @cindex output null-byte-terminated lines
9367 Output a null byte at the end of each line, rather than a newline.
9368 This option enables other programs to parse the output of @command{du}
9369 even when that output would contain file names with embedded newlines.
9370
9371 @itemx --si
9372 @opindex --si
9373 @cindex SI output
9374 Append an SI-style abbreviation to each size, such as @samp{MB} for
9375 megabytes.  Powers of 1000 are used, not 1024; @samp{MB} stands for
9376 1,000,000 bytes.  Use the @option{-h} or @option{--human-readable} option if
9377 you prefer powers of 1024.
9378
9379 @item -s
9380 @itemx --summarize
9381 @opindex -s
9382 @opindex --summarize
9383 Display only a total for each argument.
9384
9385 @item -S
9386 @itemx --separate-dirs
9387 @opindex -S
9388 @opindex --separate-dirs
9389 Report the size of each directory separately, not including the sizes
9390 of subdirectories.
9391
9392 @itemx --time
9393 @opindex --time
9394 @cindex last modified dates, displaying in @command{du}
9395 Show time of the most recent modification of any file in the directory,
9396 or any of its subdirectories.
9397
9398 @itemx --time=ctime
9399 @itemx --time=status
9400 @itemx --time=use
9401 @opindex --time
9402 @opindex ctime@r{, show the most recent}
9403 @opindex status time@r{, show the most recent}
9404 @opindex use time@r{, show the most recent}
9405 Show the most recent status change time (the @samp{ctime} in the inode) of
9406 any file in the directory, instead of the modification time.
9407
9408 @itemx --time=atime
9409 @itemx --time=access
9410 @opindex --time
9411 @opindex atime@r{, show the most recent}
9412 @opindex access time@r{, show the most recent}
9413 Show the most recent access time (the @samp{atime} in the inode) of
9414 any file in the directory, instead of the modification time.
9415
9416 @item --time-style=@var{style}
9417 @opindex --time-style
9418 @cindex time style
9419 List timestamps in style @var{style}.  This option has an effect only if
9420 the @option{--time} option is also specified.  The @var{style} should
9421 be one of the following:
9422
9423 @table @samp
9424 @item +@var{format}
9425 @vindex LC_TIME
9426 List timestamps using @var{format}, where @var{format} is interpreted
9427 like the format argument of @command{date} (@pxref{date invocation}).
9428 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
9429 @command{du} to list timestamps like @samp{2002-03-30 23:45:56}.  As
9430 with @command{date}, @var{format}'s interpretation is affected by the
9431 @env{LC_TIME} locale category.
9432
9433 @item full-iso
9434 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
9435 format with nanosecond precision, e.g., @samp{2002-03-30
9436 23:45:56.477817180 -0700}.  This style is equivalent to
9437 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
9438
9439 @item long-iso
9440 List @acronym{ISO} 8601 date and time in minutes, e.g.,
9441 @samp{2002-03-30 23:45}.  These timestamps are shorter than
9442 @samp{full-iso} timestamps, and are usually good enough for everyday
9443 work.  This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
9444
9445 @item iso
9446 List @acronym{ISO} 8601 dates for timestamps, e.g., @samp{2002-03-30}.
9447 This style is equivalent to @samp{+%Y-%m-%d}.
9448 @end table
9449
9450 @vindex TIME_STYLE
9451 You can specify the default value of the @option{--time-style} option
9452 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
9453 the default style is @samp{long-iso}.  For compatibility with @command{ls},
9454 if @env{TIME_STYLE} begins with @samp{+} and contains a newline,
9455 the newline and any later characters are ignored; if @env{TIME_STYLE}
9456 begins with @samp{posix-} the @samp{posix-} is ignored; and if
9457 @env{TIME_STYLE} is @samp{locale} it is ignored.
9458
9459 @item -x
9460 @itemx --one-file-system
9461 @opindex -x
9462 @opindex --one-file-system
9463 @cindex one file system, restricting @command{du} to
9464 Skip directories that are on different file systems from the one that
9465 the argument being processed is on.
9466
9467 @item --exclude=@var{PATTERN}
9468 @opindex --exclude=@var{PATTERN}
9469 @cindex excluding files from @command{du}
9470 When recursing, skip subdirectories or files matching @var{PATTERN}.
9471 For example, @code{du --exclude='*.o'} excludes files whose names
9472 end in @samp{.o}.
9473
9474 @item -X @var{FILE}
9475 @itemx --exclude-from=@var{FILE}
9476 @opindex -X @var{FILE}
9477 @opindex --exclude-from=@var{FILE}
9478 @cindex excluding files from @command{du}
9479 Like @option{--exclude}, except take the patterns to exclude from @var{FILE},
9480 one per line.  If @var{FILE} is @samp{-}, take the patterns from standard
9481 input.
9482
9483 @end table
9484
9485 @cindex NFS mounts from BSD to HP-UX
9486 On BSD systems, @command{du} reports sizes that are half the correct
9487 values for files that are NFS-mounted from HP-UX systems.  On HP-UX
9488 systems, it reports sizes that are twice the correct values for
9489 files that are NFS-mounted from BSD systems.  This is due to a flaw
9490 in HP-UX; it also affects the HP-UX @command{du} program.
9491
9492 @exitstatus
9493
9494
9495 @node stat invocation
9496 @section @command{stat}: Report file or file system status
9497
9498 @pindex stat
9499 @cindex file status
9500 @cindex file system status
9501
9502 @command{stat} displays information about the specified file(s).  Synopsis:
9503
9504 @example
9505 stat [@var{option}]@dots{} [@var{file}]@dots{}
9506 @end example
9507
9508 With no option, @command{stat} reports all information about the given files.
9509 But it also can be used to report the information of the file systems the
9510 given files are located on.  If the files are links, @command{stat} can
9511 also give information about the files the links point to.
9512
9513
9514 @table @samp
9515
9516 @item -L
9517 @itemx --dereference
9518 @opindex -L
9519 @opindex --dereference
9520 @cindex symbolic links, dereferencing in @command{stat}
9521 Change how @command{stat} treats symbolic links.
9522 With this option, @command{stat} acts on the file referenced
9523 by each symbolic link argument.
9524 Without it, @command{stat} acts on any symbolic link argument directly.
9525
9526 @item -f
9527 @itemx --file-system
9528 @opindex -f
9529 @opindex --file-system
9530 @cindex file systems
9531 Report information about the file systems where the given files are located
9532 instead of information about the files themselves.
9533
9534 @item -c
9535 @itemx --format=@var{format}
9536 @opindex -c
9537 @opindex --format=@var{format}
9538 @cindex output format
9539 Use @var{format} rather than the default format.
9540 @var{format} is automatically newline-terminated, so
9541 running a command like the following with two or more @var{file}
9542 operands produces a line of output for each operand:
9543 @example
9544 $ stat --format=%d:%i / /usr
9545 2050:2
9546 2057:2
9547 @end example
9548
9549 @itemx --printf=@var{format}
9550 @opindex --printf=@var{format}
9551 @cindex output format
9552 Use @var{format} rather than the default format.
9553 Like like @option{--format}, but interpret backslash escapes,
9554 and do not output a mandatory trailing newline.
9555 If you want a newline, include @samp{\n} in the @var{format}.
9556 Here's how you would use @option{--printf} to print the device
9557 and inode numbers of @file{/} and @file{/usr}:
9558 @example
9559 $ stat --printf='%d:%i\n' / /usr
9560 2050:2
9561 2057:2
9562 @end example
9563
9564 @item -t
9565 @itemx --terse
9566 @opindex -t
9567 @opindex --terse
9568 @cindex terse output
9569 Print the information in terse form, suitable for parsing by other programs.
9570
9571 The valid format sequences for files are:
9572
9573 @itemize @bullet
9574 @item %a - Access rights in octal
9575 @item %A - Access rights in human readable form
9576 @item %b - Number of blocks allocated (see @samp{%B})
9577 @item %B - The size in bytes of each block reported by @samp{%b}
9578 @item %d - Device number in decimal
9579 @item %D - Device number in hex
9580 @item %f - Raw mode in hex
9581 @item %F - File type
9582 @item %g - Group ID of owner
9583 @item %G - Group name of owner
9584 @item %h - Number of hard links
9585 @item %i - Inode number
9586 @item %n - File name
9587 @item %N - Quoted file name with dereference if symbolic link
9588 @item %o - I/O block size
9589 @item %s - Total size, in bytes
9590 @item %t - Major device type in hex
9591 @item %T - Minor device type in hex
9592 @item %u - User ID of owner
9593 @item %U - User name of owner
9594 @item %x - Time of last access
9595 @item %X - Time of last access as seconds since Epoch
9596 @item %y - Time of last modification
9597 @item %Y - Time of last modification as seconds since Epoch
9598 @item %z - Time of last change
9599 @item %Z - Time of last change as seconds since Epoch
9600 @end itemize
9601
9602 The valid format sequences for file systems are:
9603
9604 @itemize @bullet
9605 @item %a - Free blocks available to non-super-user
9606 @item %b - Total data blocks in file system
9607 @item %c - Total file nodes in file system
9608 @item %d - Free file nodes in file system
9609 @item %f - Free blocks in file system
9610 @item %i - File System ID in hex
9611 @item %l - Maximum length of file names
9612 @item %n - File name
9613 @item %s - Block size (for faster transfers)
9614 @item %S - Fundamental block size (for block counts)
9615 @item %t - Type in hex
9616 @item %T - Type in human readable form
9617 @end itemize
9618
9619 @vindex TZ
9620 Time stamps are listed according to the time zone rules specified by
9621 the @env{TZ} environment variable, or by the system default rules if
9622 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
9623 with @env{TZ}, libc, The GNU C Library}.
9624 @end table
9625
9626 @exitstatus
9627
9628
9629 @node sync invocation
9630 @section @command{sync}: Synchronize data on disk with memory
9631
9632 @pindex sync
9633 @cindex synchronize disk and memory
9634
9635 @cindex superblock, writing
9636 @cindex inodes, written buffered
9637 @command{sync} writes any data buffered in memory out to disk.  This can
9638 include (but is not limited to) modified superblocks, modified inodes,
9639 and delayed reads and writes.  This must be implemented by the kernel;
9640 The @command{sync} program does nothing but exercise the @code{sync} system
9641 call.
9642
9643 @cindex crashes and corruption
9644 The kernel keeps data in memory to avoid doing (relatively slow) disk
9645 reads and writes.  This improves performance, but if the computer
9646 crashes, data may be lost or the file system corrupted as a
9647 result.  The @command{sync} command ensures everything in memory
9648 is written to disk.
9649
9650 Any arguments are ignored, except for a lone @option{--help} or
9651 @option{--version} (@pxref{Common options}).
9652
9653 @exitstatus
9654
9655
9656 @node Printing text
9657 @chapter Printing text
9658
9659 @cindex printing text, commands for
9660 @cindex commands for printing text
9661
9662 This section describes commands that display text strings.
9663
9664 @menu
9665 * echo invocation::             Print a line of text.
9666 * printf invocation::           Format and print data.
9667 * yes invocation::              Print a string until interrupted.
9668 @end menu
9669
9670
9671 @node echo invocation
9672 @section @command{echo}: Print a line of text
9673
9674 @pindex echo
9675 @cindex displaying text
9676 @cindex printing text
9677 @cindex text, displaying
9678 @cindex arbitrary text, displaying
9679
9680 @command{echo} writes each given @var{string} to standard output, with a
9681 space between each and a newline after the last one.  Synopsis:
9682
9683 @example
9684 echo [@var{option}]@dots{} [@var{string}]@dots{}
9685 @end example
9686
9687 The program accepts the following options.  Also see @ref{Common options}.
9688 Options must precede operands, and the normally-special argument
9689 @samp{--} has no special meaning and is treated like any other
9690 @var{string}.
9691
9692 @table @samp
9693 @item -n
9694 @opindex -n
9695 Do not output the trailing newline.
9696
9697 @item -e
9698 @opindex -e
9699 @cindex backslash escapes
9700 Enable interpretation of the following backslash-escaped characters in
9701 each @var{string}:
9702
9703 @table @samp
9704 @item \a
9705 alert (bell)
9706 @item \b
9707 backspace
9708 @item \c
9709 suppress trailing newline
9710 @item \f
9711 form feed
9712 @item \n
9713 new line
9714 @item \r
9715 carriage return
9716 @item \t
9717 horizontal tab
9718 @item \v
9719 vertical tab
9720 @item \\
9721 backslash
9722 @item \0@var{nnn}
9723 the eight-bit value that is the octal number @var{nnn}
9724 (zero to three octal digits)
9725 @item \@var{nnn}
9726 the eight-bit value that is the octal number @var{nnn}
9727 (one to three octal digits)
9728 @item \x@var{hh}
9729 the eight-bit value that is the hexadecimal number @var{hh}
9730 (one or two hexadecimal digits)
9731 @end table
9732
9733 @item -E
9734 @opindex -E
9735 @cindex backslash escapes
9736 Disable interpretation of backslash escapes in each @var{string}.
9737 This is the default.  If @option{-e} and @option{-E} are both
9738 specified, the last one given takes effect.
9739
9740 @end table
9741
9742 @vindex POSIXLY_CORRECT
9743 If the @env{POSIXLY_CORRECT} environment variable is set, then when
9744 @command{echo}'s first argument is not @option{-n} it outputs
9745 option-like arguments instead of treating them as options.  For
9746 example, @code{echo -ne hello} outputs @samp{-ne hello} instead of
9747 plain @samp{hello}.
9748
9749 @acronym{POSIX} does not require support for any options, and says
9750 that the behavior of @command{echo} is implementation-defined if any
9751 @var{string} contains a backslash or if the first argument is
9752 @option{-n}.  Portable programs can use the @command{printf} command
9753 if they need to omit trailing newlines or output control characters or
9754 backslashes.  @xref{printf invocation}.
9755
9756 @exitstatus
9757
9758
9759 @node printf invocation
9760 @section @command{printf}: Format and print data
9761
9762 @pindex printf
9763 @command{printf} does formatted printing of text.  Synopsis:
9764
9765 @example
9766 printf @var{format} [@var{argument}]@dots{}
9767 @end example
9768
9769 @command{printf} prints the @var{format} string, interpreting @samp{%}
9770 directives and @samp{\} escapes to format numeric and string arguments
9771 in a way that is mostly similar to the C @samp{printf} function.  The
9772 differences are as follows:
9773
9774 @itemize @bullet
9775
9776 @item
9777 The @var{format} argument is reused as necessary to convert all the
9778 given @var{argument}s.  For example, the command @samp{printf %s a b}
9779 outputs @samp{ab}.
9780
9781 @item
9782 Missing @var{argument}s are treated as null strings or as zeros,
9783 depending on whether the context expects a string or a number.  For
9784 example, the command @samp{printf %sx%d} prints @samp{x0}.
9785
9786 @item
9787 @kindex \c
9788 An additional escape, @samp{\c}, causes @command{printf} to produce no
9789 further output.  For example, the command @samp{printf 'A%sC\cD%sF' B
9790 E} prints @samp{ABC}.
9791
9792 @item
9793 The hexadecimal escape sequence @samp{\x@var{hh}} has at most two
9794 digits, as opposed to C where it can have an unlimited number of
9795 digits.  For example, the command @samp{printf '\x07e'} prints two
9796 bytes, whereas the C statement @samp{printf ("\x07e")} prints just
9797 one.
9798
9799 @item
9800 @kindex %b
9801 @command{printf} has an additional directive, @samp{%b}, which prints its
9802 argument string with @samp{\} escapes interpreted in the same way as in
9803 the @var{format} string, except that octal escapes are of the form
9804 @samp{\0@var{ooo}} where @var{ooo} is 0 to 3 octal digits.
9805 If a precision is also given, it limits the number of bytes printed
9806 from the converted string.
9807
9808 @item
9809 Numeric arguments must be single C constants, possibly with leading
9810 @samp{+} or @samp{-}.  For example, @samp{printf %.4d -3} outputs
9811 @samp{-0003}.
9812
9813 @item
9814 @vindex POSIXLY_CORRECT
9815 If the leading character of a numeric argument is @samp{"} or @samp{'}
9816 then its value is the numeric value of the immediately following
9817 character.  Any remaining characters are silently ignored if the
9818 @env{POSIXLY_CORRECT} environment variable is set; otherwise, a
9819 warning is printed.  For example, @samp{printf "%d" "'a"} outputs
9820 @samp{97} on hosts that use the @acronym{ASCII} character set, since
9821 @samp{a} has the numeric value 97 in @acronym{ASCII}.
9822
9823 @end itemize
9824
9825 @vindex LC_NUMERIC
9826 A floating-point argument must use a period before any fractional
9827 digits, but is printed according to the @env{LC_NUMERIC} category of the
9828 current locale.  For example, in a locale whose radix character is a
9829 comma, the command @samp{printf %g 3.14} outputs @samp{3,14} whereas
9830 the command @samp{printf %g 3,14} is an error.
9831
9832 @kindex \@var{ooo}
9833 @kindex \x@var{hh}
9834 @command{printf} interprets @samp{\@var{ooo}} in @var{format} as an octal number
9835 (if @var{ooo} is 1 to 3 octal digits) specifying a character to print,
9836 and @samp{\x@var{hh}} as a hexadecimal number (if @var{hh} is 1 to 2 hex
9837 digits) specifying a character to print.
9838
9839 @kindex \uhhhh
9840 @kindex \Uhhhhhhhh
9841 @cindex Unicode
9842 @cindex ISO/IEC 10646
9843 @vindex LC_CTYPE
9844 @command{printf} interprets two character syntaxes introduced in
9845 @acronym{ISO} C 99:
9846 @samp{\u} for 16-bit Unicode (@acronym{ISO}/@acronym{IEC} 10646)
9847 characters, specified as
9848 four hexadecimal digits @var{hhhh}, and @samp{\U} for 32-bit Unicode
9849 characters, specified as eight hexadecimal digits @var{hhhhhhhh}.
9850 @command{printf} outputs the Unicode characters
9851 according to the @env{LC_CTYPE} locale.
9852
9853 The processing of @samp{\u} and @samp{\U} requires a full-featured
9854 @code{iconv} facility.  It is activated on systems with glibc 2.2 (or newer),
9855 or when @code{libiconv} is installed prior to this package.  Otherwise
9856 @samp{\u} and @samp{\U} will print as-is.
9857
9858 The only options are a lone @option{--help} or
9859 @option{--version}.  @xref{Common options}.
9860 Options must precede operands.
9861
9862 The Unicode character syntaxes are useful for writing strings in a locale
9863 independent way.  For example, a string containing the Euro currency symbol
9864
9865 @example
9866 $ /usr/local/bin/printf '\u20AC 14.95'
9867 @end example
9868
9869 @noindent
9870 will be output correctly in all locales supporting the Euro symbol
9871 (@acronym{ISO}-8859-15, UTF-8, and others).  Similarly, a Chinese string
9872
9873 @example
9874 $ /usr/local/bin/printf '\u4e2d\u6587'
9875 @end example
9876
9877 @noindent
9878 will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).
9879
9880 Note that in these examples, the full name of @command{printf} has been
9881 given, to distinguish it from the GNU @code{bash} built-in function
9882 @command{printf}.
9883
9884 For larger strings, you don't need to look up the hexadecimal code
9885 values of each character one by one.  @acronym{ASCII} characters mixed with \u
9886 escape sequences is also known as the JAVA source file encoding.  You can
9887 use GNU recode 3.5c (or newer) to convert strings to this encoding.  Here
9888 is how to convert a piece of text into a shell script which will output
9889 this text in a locale-independent way:
9890
9891 @smallexample
9892 $ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
9893     '\u4e2d\u6587\n' > sample.txt
9894 $ recode BIG5..JAVA < sample.txt \
9895     | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
9896     > sample.sh
9897 @end smallexample
9898
9899 @exitstatus
9900
9901
9902 @node yes invocation
9903 @section @command{yes}: Print a string until interrupted
9904
9905 @pindex yes
9906 @cindex repeated output of a string
9907
9908 @command{yes} prints the command line arguments, separated by spaces and
9909 followed by a newline, forever until it is killed.  If no arguments are
9910 given, it prints @samp{y} followed by a newline forever until killed.
9911
9912 Upon a write error, @command{yes} exits with status @samp{1}.
9913
9914 The only options are a lone @option{--help} or @option{--version}.
9915 To output an argument that begins with
9916 @samp{-}, precede it with @option{--}, e.g., @samp{yes -- --help}.
9917 @xref{Common options}.
9918
9919
9920 @node Conditions
9921 @chapter Conditions
9922
9923 @cindex conditions
9924 @cindex commands for exit status
9925 @cindex exit status commands
9926
9927 This section describes commands that are primarily useful for their exit
9928 status, rather than their output.  Thus, they are often used as the
9929 condition of shell @code{if} statements, or as the last command in a
9930 pipeline.
9931
9932 @menu
9933 * false invocation::            Do nothing, unsuccessfully.
9934 * true invocation::             Do nothing, successfully.
9935 * test invocation::             Check file types and compare values.
9936 * expr invocation::             Evaluate expressions.
9937 @end menu
9938
9939
9940 @node false invocation
9941 @section @command{false}: Do nothing, unsuccessfully
9942
9943 @pindex false
9944 @cindex do nothing, unsuccessfully
9945 @cindex failure exit status
9946 @cindex exit status of @command{false}
9947
9948 @command{false} does nothing except return an exit status of 1, meaning
9949 @dfn{failure}.  It can be used as a place holder in shell scripts
9950 where an unsuccessful command is needed.
9951 In most modern shells, @command{false} is a built-in command, so when
9952 you use @samp{false} in a script, you're probably using the built-in
9953 command, not the one documented here.
9954
9955 @command{false} honors the @option{--help} and @option{--version} options.
9956
9957 This version of @command{false} is implemented as a C program, and is thus
9958 more secure and faster than a shell script implementation, and may safely
9959 be used as a dummy shell for the purpose of disabling accounts.
9960
9961 Note that @command{false} (unlike all other programs documented herein)
9962 exits unsuccessfully, even when invoked with
9963 @option{--help} or @option{--version}.
9964
9965 Portable programs should not assume that the exit status of
9966 @command{false} is 1, as it is greater than 1 on some
9967 non-@acronym{GNU} hosts.
9968
9969
9970 @node true invocation
9971 @section @command{true}: Do nothing, successfully
9972
9973 @pindex true
9974 @cindex do nothing, successfully
9975 @cindex no-op
9976 @cindex successful exit
9977 @cindex exit status of @command{true}
9978
9979 @command{true} does nothing except return an exit status of 0, meaning
9980 @dfn{success}.  It can be used as a place holder in shell scripts
9981 where a successful command is needed, although the shell built-in
9982 command @code{:} (colon) may do the same thing faster.
9983 In most modern shells, @command{true} is a built-in command, so when
9984 you use @samp{true} in a script, you're probably using the built-in
9985 command, not the one documented here.
9986
9987 @command{true} honors the @option{--help} and @option{--version} options.
9988
9989 Note, however, that it is possible to cause @command{true}
9990 to exit with nonzero status: with the @option{--help} or @option{--version}
9991 option, and with standard
9992 output already closed or redirected to a file that evokes an I/O error.
9993 For example, using a Bourne-compatible shell:
9994
9995 @example
9996 $ ./true --version >&-
9997 ./true: write error: Bad file number
9998 $ ./true --version > /dev/full
9999 ./true: write error: No space left on device
10000 @end example
10001
10002 This version of @command{true} is implemented as a C program, and is thus
10003 more secure and faster than a shell script implementation, and may safely
10004 be used as a dummy shell for the purpose of disabling accounts.
10005
10006 @node test invocation
10007 @section @command{test}: Check file types and compare values
10008
10009 @pindex test
10010 @cindex check file types
10011 @cindex compare values
10012 @cindex expression evaluation
10013
10014 @command{test} returns a status of 0 (true) or 1 (false) depending on the
10015 evaluation of the conditional expression @var{expr}.  Each part of the
10016 expression must be a separate argument.
10017
10018 @command{test} has file status checks, string operators, and numeric
10019 comparison operators.
10020
10021 @command{test} has an alternate form that uses opening and closing
10022 square brackets instead a leading @samp{test}.  For example, instead
10023 of @samp{test -d /}, you can write @samp{[ -d / ]}.  The square
10024 brackets must be separate arguments; for example, @samp{[-d /]} does
10025 not have the desired effect.  Since @samp{test @var{expr}} and @samp{[
10026 @var{expr} ]} have the same meaning, only the former form is discussed
10027 below.
10028
10029 Synopses:
10030
10031 @example
10032 test @var{expression}
10033 test
10034 [ @var{expression} ]
10035 [ ]
10036 [ @var{option}
10037 @end example
10038
10039 @cindex conflicts with shell built-ins
10040 @cindex built-in shell commands, conflicts with
10041 Because most shells have a built-in @command{test} command, using an
10042 unadorned @command{test} in a script or interactively may get you
10043 different functionality than that described here.
10044
10045 If @var{expression} is omitted, @command{test} returns false.
10046 If @var{expression} is a single argument,
10047 @command{test} returns false if the argument is null and true otherwise.  The argument
10048 can be any string, including strings like @samp{-d}, @samp{-1},
10049 @samp{--}, @samp{--help}, and @samp{--version} that most other
10050 programs would treat as options.  To get help and version information,
10051 invoke the commands @samp{[ --help} and @samp{[ --version}, without
10052 the usual closing brackets.  @xref{Common options}.
10053
10054 @cindex exit status of @command{test}
10055 Exit status:
10056
10057 @display
10058 0 if the expression is true,
10059 1 if the expression is false,
10060 2 if an error occurred.
10061 @end display
10062
10063 @menu
10064 * File type tests::             -[bcdfhLpSt]
10065 * Access permission tests::     -[gkruwxOG]
10066 * File characteristic tests::   -e -s -nt -ot -ef
10067 * String tests::                -z -n = !=
10068 * Numeric tests::               -eq -ne -lt -le -gt -ge
10069 * Connectives for test::        ! -a -o
10070 @end menu
10071
10072
10073 @node File type tests
10074 @subsection File type tests
10075
10076 @cindex file type tests
10077
10078 These options test for particular types of files.  (Everything's a file,
10079 but not all files are the same!)
10080
10081 @table @samp
10082
10083 @item -b @var{file}
10084 @opindex -b
10085 @cindex block special check
10086 True if @var{file} exists and is a block special device.
10087
10088 @item -c @var{file}
10089 @opindex -c
10090 @cindex character special check
10091 True if @var{file} exists and is a character special device.
10092
10093 @item -d @var{file}
10094 @opindex -d
10095 @cindex directory check
10096 True if @var{file} exists and is a directory.
10097
10098 @item -f @var{file}
10099 @opindex -f
10100 @cindex regular file check
10101 True if @var{file} exists and is a regular file.
10102
10103 @item -h @var{file}
10104 @itemx -L @var{file}
10105 @opindex -L
10106 @opindex -h
10107 @cindex symbolic link check
10108 True if @var{file} exists and is a symbolic link.
10109 Unlike all other file-related tests, this test does not dereference
10110 @var{file} if it is a symbolic link.
10111
10112 @item -p @var{file}
10113 @opindex -p
10114 @cindex named pipe check
10115 True if @var{file} exists and is a named pipe.
10116
10117 @item -S @var{file}
10118 @opindex -S
10119 @cindex socket check
10120 True if @var{file} exists and is a socket.
10121
10122 @item -t @var{fd}
10123 @opindex -t
10124 @cindex terminal check
10125 True if @var{fd} is a file descriptor that is associated with a
10126 terminal.
10127
10128 @end table
10129
10130
10131 @node Access permission tests
10132 @subsection Access permission tests
10133
10134 @cindex access permission tests
10135 @cindex permission tests
10136
10137 These options test for particular access permissions.
10138
10139 @table @samp
10140
10141 @item -g @var{file}
10142 @opindex -g
10143 @cindex set-group-ID check
10144 True if @var{file} exists and has its set-group-ID bit set.
10145
10146 @item -k @var{file}
10147 @opindex -k
10148 @cindex sticky bit check
10149 True if @var{file} exists and has its @dfn{sticky} bit set.
10150
10151 @item -r @var{file}
10152 @opindex -r
10153 @cindex readable file check
10154 True if @var{file} exists and read permission is granted.
10155
10156 @item -u @var{file}
10157 @opindex -u
10158 @cindex set-user-ID check
10159 True if @var{file} exists and has its set-user-ID bit set.
10160
10161 @item -w @var{file}
10162 @opindex -w
10163 @cindex writable file check
10164 True if @var{file} exists and write permission is granted.
10165
10166 @item -x @var{file}
10167 @opindex -x
10168 @cindex executable file check
10169 True if @var{file} exists and execute permission is granted
10170 (or search permission, if it is a directory).
10171
10172 @item -O @var{file}
10173 @opindex -O
10174 @cindex owned by effective user ID check
10175 True if @var{file} exists and is owned by the current effective user ID.
10176
10177 @item -G @var{file}
10178 @opindex -G
10179 @cindex owned by effective group ID check
10180 True if @var{file} exists and is owned by the current effective group ID.
10181
10182 @end table
10183
10184 @node File characteristic tests
10185 @subsection File characteristic tests
10186
10187 @cindex file characteristic tests
10188
10189 These options test other file characteristics.
10190
10191 @table @samp
10192
10193 @item -e @var{file}
10194 @opindex -e
10195 @cindex existence-of-file check
10196 True if @var{file} exists.
10197
10198 @item -s @var{file}
10199 @opindex -s
10200 @cindex nonempty file check
10201 True if @var{file} exists and has a size greater than zero.
10202
10203 @item @var{file1} -nt @var{file2}
10204 @opindex -nt
10205 @cindex newer-than file check
10206 True if @var{file1} is newer (according to modification date) than
10207 @var{file2}, or if @var{file1} exists and @var{file2} does not.
10208
10209 @item @var{file1} -ot @var{file2}
10210 @opindex -ot
10211 @cindex older-than file check
10212 True if @var{file1} is older (according to modification date) than
10213 @var{file2}, or if @var{file2} exists and @var{file1} does not.
10214
10215 @item @var{file1} -ef @var{file2}
10216 @opindex -ef
10217 @cindex same file check
10218 @cindex hard link check
10219 True if @var{file1} and @var{file2} have the same device and inode
10220 numbers, i.e., if they are hard links to each other.
10221
10222 @end table
10223
10224
10225 @node String tests
10226 @subsection String tests
10227
10228 @cindex string tests
10229
10230 These options test string characteristics.  You may need to quote
10231 @var{string} arguments for the shell.  For example:
10232
10233 @example
10234 test -n "$V"
10235 @end example
10236
10237 The quotes here prevent the wrong arguments from being passed to
10238 @command{test} if @samp{$V} is empty or contains special characters.
10239
10240 @table @samp
10241
10242 @item -z @var{string}
10243 @opindex -z
10244 @cindex zero-length string check
10245 True if the length of @var{string} is zero.
10246
10247 @item -n @var{string}
10248 @itemx @var{string}
10249 @opindex -n
10250 @cindex nonzero-length string check
10251 True if the length of @var{string} is nonzero.
10252
10253 @item @var{string1} = @var{string2}
10254 @opindex =
10255 @cindex equal string check
10256 True if the strings are equal.
10257
10258 @item @var{string1} != @var{string2}
10259 @opindex !=
10260 @cindex not-equal string check
10261 True if the strings are not equal.
10262
10263 @end table
10264
10265
10266 @node Numeric tests
10267 @subsection Numeric tests
10268
10269 @cindex numeric tests
10270 @cindex arithmetic tests
10271
10272 Numeric relationals.  The arguments must be entirely numeric (possibly
10273 negative), or the special expression @w{@code{-l @var{string}}}, which
10274 evaluates to the length of @var{string}.
10275
10276 @table @samp
10277
10278 @item @var{arg1} -eq @var{arg2}
10279 @itemx @var{arg1} -ne @var{arg2}
10280 @itemx @var{arg1} -lt @var{arg2}
10281 @itemx @var{arg1} -le @var{arg2}
10282 @itemx @var{arg1} -gt @var{arg2}
10283 @itemx @var{arg1} -ge @var{arg2}
10284 @opindex -eq
10285 @opindex -ne
10286 @opindex -lt
10287 @opindex -le
10288 @opindex -gt
10289 @opindex -ge
10290 These arithmetic binary operators return true if @var{arg1} is equal,
10291 not-equal, less-than, less-than-or-equal, greater-than, or
10292 greater-than-or-equal than @var{arg2}, respectively.
10293
10294 @end table
10295
10296 For example:
10297
10298 @example
10299 test -1 -gt -2 && echo yes
10300 @result{} yes
10301 test -l abc -gt 1 && echo yes
10302 @result{} yes
10303 test 0x100 -eq 1
10304 @error{} test: integer expression expected before -eq
10305 @end example
10306
10307
10308 @node Connectives for test
10309 @subsection Connectives for @command{test}
10310
10311 @cindex logical connectives
10312 @cindex connectives, logical
10313
10314 The usual logical connectives.
10315
10316 @table @samp
10317
10318 @item ! @var{expr}
10319 @opindex !
10320 True if @var{expr} is false.
10321
10322 @item @var{expr1} -a @var{expr2}
10323 @opindex -a
10324 @cindex logical and operator
10325 @cindex and operator
10326 True if both @var{expr1} and @var{expr2} are true.
10327
10328 @item @var{expr1} -o @var{expr2}
10329 @opindex -o
10330 @cindex logical or operator
10331 @cindex or operator
10332 True if either @var{expr1} or @var{expr2} is true.
10333
10334 @end table
10335
10336
10337 @node expr invocation
10338 @section @command{expr}: Evaluate expressions
10339
10340 @pindex expr
10341 @cindex expression evaluation
10342 @cindex evaluation of expressions
10343
10344 @command{expr} evaluates an expression and writes the result on standard
10345 output.  Each token of the expression must be a separate argument.
10346
10347 Operands are either integers or strings.  Integers consist of one or
10348 more decimal digits, with an optional leading @samp{-}.
10349 @command{expr} converts
10350 anything appearing in an operand position to an integer or a string
10351 depending on the operation being applied to it.
10352
10353 Strings are not quoted for @command{expr} itself, though you may need to
10354 quote them to protect characters with special meaning to the shell,
10355 e.g., spaces.  However, regardless of whether it is quoted, a string
10356 operand should not be a parenthesis or any of @command{expr}'s
10357 operators like @code{+}, so you cannot safely pass an arbitrary string
10358 @code{$str} to expr merely by quoting it to the shell.  One way to
10359 work around this is to use the @sc{gnu} extension @code{+},
10360 (e.g., @code{+ "$str" = foo}); a more portable way is to use
10361 @code{@w{" $str"}} and to adjust the rest of the expression to take
10362 the leading space into account (e.g., @code{@w{" $str" = " foo"}}).
10363
10364 You should not pass a negative integer or a string with leading
10365 @samp{-} as @command{expr}'s first argument, as it might be
10366 misinterpreted as an option; this can be avoided by parenthesization.
10367 Also, portable scripts should not use a string operand that happens to
10368 take the form of an integer; this can be worked around by inserting
10369 leading spaces as mentioned above.
10370
10371 @cindex parentheses for grouping
10372 Operators may be given as infix symbols or prefix keywords.  Parentheses
10373 may be used for grouping in the usual manner.  You must quote
10374 parentheses and many operators to avoid the shell evaluating them,
10375 however.
10376
10377 The only options are @option{--help} and @option{--version}.  @xref{Common
10378 options}.  Options must precede operands.
10379
10380 @cindex exit status of @command{expr}
10381 Exit status:
10382
10383 @display
10384 0 if the expression is neither null nor 0,
10385 1 if the expression is null or 0,
10386 2 if the expression is invalid,
10387 3 if an internal error occurred (e.g., arithmetic overflow).
10388 @end display
10389
10390 @menu
10391 * String expressions::          + : match substr index length
10392 * Numeric expressions::         + - * / %
10393 * Relations for expr::          | & < <= = == != >= >
10394 * Examples of expr::            Examples.
10395 @end menu
10396
10397
10398 @node String expressions
10399 @subsection String expressions
10400
10401 @cindex string expressions
10402 @cindex expressions, string
10403
10404 @command{expr} supports pattern matching and other string operators.  These
10405 have lower precedence than both the numeric and relational operators (in
10406 the next sections).
10407
10408 @table @samp
10409
10410 @item @var{string} : @var{regex}
10411 @cindex pattern matching
10412 @cindex regular expression matching
10413 @cindex matching patterns
10414 Perform pattern matching.  The arguments are converted to strings and the
10415 second is considered to be a (basic, a la GNU @code{grep}) regular
10416 expression, with a @code{^} implicitly prepended.  The first argument is
10417 then matched against this regular expression.
10418
10419 If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
10420 @code{:} expression returns the part of @var{string} that matched the
10421 subexpression; otherwise, it returns the number of characters matched.
10422
10423 If the match fails, the @code{:} operator returns the null string if
10424 @samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
10425
10426 @kindex \( @r{regexp operator}
10427 Only the first @samp{\( @dots{} \)} pair is relevant to the return
10428 value; additional pairs are meaningful only for grouping the regular
10429 expression operators.
10430
10431 @kindex \+ @r{regexp operator}
10432 @kindex \? @r{regexp operator}
10433 @kindex \| @r{regexp operator}
10434 In the regular expression, @code{\+}, @code{\?}, and @code{\|} are
10435 operators which respectively match one or more, zero or one, or separate
10436 alternatives.  SunOS and other @command{expr}'s treat these as regular
10437 characters.  (@acronym{POSIX} allows either behavior.)
10438 @xref{Top, , Regular Expression Library, regex, Regex}, for details of
10439 regular expression syntax.  Some examples are in @ref{Examples of expr}.
10440
10441 @item match @var{string} @var{regex}
10442 @findex match
10443 An alternative way to do pattern matching.  This is the same as
10444 @w{@samp{@var{string} : @var{regex}}}.
10445
10446 @item substr @var{string} @var{position} @var{length}
10447 @findex substr
10448 Returns the substring of @var{string} beginning at @var{position}
10449 with length at most @var{length}.  If either @var{position} or
10450 @var{length} is negative, zero, or non-numeric, returns the null string.
10451
10452 @item index @var{string} @var{charset}
10453 @findex index
10454 Returns the first position in @var{string} where the first character in
10455 @var{charset} was found.  If no character in @var{charset} is found in
10456 @var{string}, return 0.
10457
10458 @item length @var{string}
10459 @findex length
10460 Returns the length of @var{string}.
10461
10462 @item + @var{token}
10463 @kindex +
10464 Interpret @var{token} as a string, even if it is a keyword like @var{match}
10465 or an operator like @code{/}.
10466 This makes it possible to test @code{expr length + "$x"} or
10467 @code{expr + "$x" : '.*/\(.\)'} and have it do the right thing even if
10468 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
10469 This operator is a @acronym{GNU} extension.  Portable shell scripts should use
10470 @code{@w{" $token"} : @w{' \(.*\)'}} instead of @code{+ "$token"}.
10471
10472 @end table
10473
10474 To make @command{expr} interpret keywords as strings, you must use the
10475 @code{quote} operator.
10476
10477
10478 @node Numeric expressions
10479 @subsection Numeric expressions
10480
10481 @cindex numeric expressions
10482 @cindex expressions, numeric
10483
10484 @command{expr} supports the usual numeric operators, in order of increasing
10485 precedence.  The string operators (previous section) have lower precedence,
10486 the connectives (next section) have higher.
10487
10488 @table @samp
10489
10490 @item + -
10491 @kindex +
10492 @kindex -
10493 @cindex addition
10494 @cindex subtraction
10495 Addition and subtraction.  Both arguments are converted to integers;
10496 an error occurs if this cannot be done.
10497
10498 @item * / %
10499 @kindex *
10500 @kindex /
10501 @kindex %
10502 @cindex multiplication
10503 @cindex division
10504 @cindex remainder
10505 Multiplication, division, remainder.  Both arguments are converted to
10506 integers; an error occurs if this cannot be done.
10507
10508 @end table
10509
10510
10511 @node Relations for expr
10512 @subsection Relations for @command{expr}
10513
10514 @cindex connectives, logical
10515 @cindex logical connectives
10516 @cindex relations, numeric or string
10517
10518 @command{expr} supports the usual logical connectives and relations.  These
10519 are higher precedence than either the string or numeric operators
10520 (previous sections).  Here is the list, lowest-precedence operator first.
10521
10522 @table @samp
10523
10524 @item |
10525 @kindex |
10526 @cindex logical or operator
10527 @cindex or operator
10528 Returns its first argument if that is neither null nor zero, otherwise
10529 its second argument if it is neither null nor zero, otherwise 0.  It
10530 does not evaluate its second argument if its first argument is neither
10531 null nor zero.
10532
10533 @item &
10534 @kindex &
10535 @cindex logical and operator
10536 @cindex and operator
10537 Return its first argument if neither argument is null or zero, otherwise
10538 0.  It does not evaluate its second argument if its first argument is
10539 null or zero.
10540
10541 @item < <= = == != >= >
10542 @kindex <
10543 @kindex <=
10544 @kindex =
10545 @kindex ==
10546 @kindex >
10547 @kindex >=
10548 @cindex comparison operators
10549 @vindex LC_COLLATE
10550 Compare the arguments and return 1 if the relation is true, 0 otherwise.
10551 @code{==} is a synonym for @code{=}.  @command{expr} first tries to convert
10552 both arguments to integers and do a numeric comparison; if either
10553 conversion fails, it does a lexicographic comparison using the character
10554 collating sequence specified by the @env{LC_COLLATE} locale.
10555
10556 @end table
10557
10558
10559 @node Examples of expr
10560 @subsection Examples of using @command{expr}
10561
10562 @cindex examples of @command{expr}
10563 Here are a few examples, including quoting for shell metacharacters.
10564
10565 To add 1 to the shell variable @code{foo}, in Bourne-compatible shells:
10566
10567 @example
10568 foo=`expr $foo + 1`
10569 @end example
10570
10571 To print the non-directory part of the file name stored in
10572 @code{$fname}, which need not contain a @code{/}:
10573
10574 @example
10575 expr $fname : '.*/\(.*\)' '|' $fname
10576 @end example
10577
10578 An example showing that @code{\+} is an operator:
10579
10580 @example
10581 expr aaa : 'a\+'
10582 @result{} 3
10583 @end example
10584
10585 @example
10586 expr abc : 'a\(.\)c'
10587 @result{} b
10588 expr index abcdef cz
10589 @result{} 3
10590 expr index index a
10591 @error{} expr: syntax error
10592 expr index quote index a
10593 @result{} 0
10594 @end example
10595
10596
10597 @node Redirection
10598 @chapter Redirection
10599
10600 @cindex redirection
10601 @cindex commands for redirection
10602
10603 Unix shells commonly provide several forms of @dfn{redirection}---ways
10604 to change the input source or output destination of a command.  But one
10605 useful redirection is performed by a separate command, not by the shell;
10606 it's described here.
10607
10608 @menu
10609 * tee invocation::              Redirect output to multiple files.
10610 @end menu
10611
10612
10613 @node tee invocation
10614 @section @command{tee}: Redirect output to multiple files
10615
10616 @pindex tee
10617 @cindex pipe fitting
10618 @cindex destinations, multiple output
10619 @cindex read from stdin and write to stdout and files
10620
10621 The @command{tee} command copies standard input to standard output and also
10622 to any files given as arguments.  This is useful when you want not only
10623 to send some data down a pipe, but also to save a copy.  Synopsis:
10624
10625 @example
10626 tee [@var{option}]@dots{} [@var{file}]@dots{}
10627 @end example
10628
10629 If a file being written to does not already exist, it is created.  If a
10630 file being written to already exists, the data it previously contained
10631 is overwritten unless the @option{-a} option is used.
10632
10633 A @var{file} of @samp{-} causes @command{tee} to send another copy of
10634 input to standard output, but this is typically not that useful as the
10635 copies are interleaved.
10636
10637 The program accepts the following options.  Also see @ref{Common options}.
10638
10639 @table @samp
10640 @item -a
10641 @itemx --append
10642 @opindex -a
10643 @opindex --append
10644 Append standard input to the given files rather than overwriting
10645 them.
10646
10647 @item -i
10648 @itemx --ignore-interrupts
10649 @opindex -i
10650 @opindex --ignore-interrupts
10651 Ignore interrupt signals.
10652
10653 @end table
10654
10655 @exitstatus
10656
10657
10658 @node File name manipulation
10659 @chapter File name manipulation
10660
10661 @cindex file name manipulation
10662 @cindex manipulation of file names
10663 @cindex commands for file name manipulation
10664
10665 This section describes commands that manipulate file names.
10666
10667 @menu
10668 * basename invocation::         Strip directory and suffix from a file name.
10669 * dirname invocation::          Strip non-directory suffix from a file name.
10670 * pathchk invocation::          Check file name portability.
10671 @end menu
10672
10673
10674 @node basename invocation
10675 @section @command{basename}: Strip directory and suffix from a file name
10676
10677 @pindex basename
10678 @cindex strip directory and suffix from file names
10679 @cindex directory, stripping from file names
10680 @cindex suffix, stripping from file names
10681 @cindex file names, stripping directory and suffix
10682 @cindex leading directory components, stripping
10683
10684 @command{basename} removes any leading directory components from
10685 @var{name}.  Synopsis:
10686
10687 @example
10688 basename @var{name} [@var{suffix}]
10689 @end example
10690
10691 If @var{suffix} is specified and is identical to the end of @var{name},
10692 it is removed from @var{name} as well.  Note that since trailing slashes
10693 are removed prior to suffix matching, @var{suffix} will do nothing if it
10694 contains slashes.  @command{basename} prints the result on standard
10695 output.
10696
10697 @c This test is used both here and in the section on dirname.
10698 @macro basenameAndDirname
10699 Together, @command{basename} and @command{dirname} are designed such
10700 that if @samp{ls "$name"} succeeds, then the command sequence @samp{cd
10701 "$(dirname "$name")"; ls "$(basename "$name")"} will, too.  This works
10702 for everything except file names containing a trailing newline.
10703 @end macro
10704 @basenameAndDirname
10705
10706 @acronym{POSIX} allows the implementation to define the results if
10707 @var{name} is empty or @samp{//}.  In the former case, @acronym{GNU}
10708 @command{basename} returns the empty string.  In the latter case, the
10709 result is @samp{//} on platforms where @var{//} is distinct from
10710 @var{/}, and @samp{/} on platforms where there is no difference.
10711
10712 The only options are @option{--help} and @option{--version}.  @xref{Common
10713 options}.  Options must precede operands.
10714
10715 @exitstatus
10716
10717 Examples:
10718
10719 @smallexample
10720 # Output "sort".
10721 basename /usr/bin/sort
10722
10723 # Output "stdio".
10724 basename include/stdio.h .h
10725 @end smallexample
10726
10727
10728 @node dirname invocation
10729 @section @command{dirname}: Strip non-directory suffix from a file name
10730
10731 @pindex dirname
10732 @cindex directory components, printing
10733 @cindex stripping non-directory suffix
10734 @cindex non-directory suffix, stripping
10735
10736 @command{dirname} prints all but the final slash-delimited component of
10737 a string (presumably a file name).  Synopsis:
10738
10739 @example
10740 dirname @var{name}
10741 @end example
10742
10743 If @var{name} is a single component, @command{dirname} prints @samp{.}
10744 (meaning the current directory).
10745
10746 @basenameAndDirname
10747
10748 @acronym{POSIX} allows the implementation to define the results if
10749 @var{name} is @samp{//}.  With @acronym{GNU} @command{dirname}, the
10750 result is @samp{//} on platforms where @var{//} is distinct from
10751 @var{/}, and @samp{/} on platforms where there is no difference.
10752
10753 The only options are @option{--help} and @option{--version}.  @xref{Common
10754 options}.
10755
10756 @exitstatus
10757
10758 Examples:
10759
10760 @smallexample
10761 # Output "/usr/bin".
10762 dirname /usr/bin/sort
10763
10764 # Output ".".
10765 dirname stdio.h
10766 @end smallexample
10767
10768
10769 @node pathchk invocation
10770 @section @command{pathchk}: Check file name portability
10771
10772 @pindex pathchk
10773 @cindex file names, checking validity and portability
10774 @cindex valid file names, checking for
10775 @cindex portable file names, checking for
10776
10777 @command{pathchk} checks portability of file names.  Synopsis:
10778
10779 @example
10780 pathchk [@var{option}]@dots{} @var{name}@dots{}
10781 @end example
10782
10783 For each @var{name}, @command{pathchk} prints a message if any of
10784 these conditions is true:
10785
10786 @enumerate
10787 @item
10788 One of the existing directories in @var{name} does not have search
10789 (execute) permission,
10790 @item
10791 The length of @var{name} is larger than the maximum supported by the
10792 operating system.
10793 @item
10794 The length of one component of @var{name} is longer than
10795 its file system's maximum.
10796 @end enumerate
10797
10798 A nonexistent @var{name} is not an error, so long a file with that
10799 name could be created under the above conditions.
10800
10801 The program accepts the following options.  Also see @ref{Common options}.
10802 Options must precede operands.
10803
10804 @table @samp
10805
10806 @item -p
10807 @opindex -p
10808 Instead of performing checks based on the underlying file system,
10809 print a message if any of these conditions is true:
10810
10811 @enumerate
10812 @item
10813 A file name is empty.
10814
10815 @item
10816 The length of a file name or one of its components exceeds the
10817 @acronym{POSIX} minimum limits for portability.
10818
10819 @item
10820 A file name contains a character outside the portable file name
10821 character set, namely, the ASCII letters and digits, @samp{-},
10822 @samp{.}, @samp{/}, and @samp{_}.
10823 @end enumerate
10824
10825 @item -P
10826 @opindex -P
10827 Print a message if a file name is empty, or if it contains a component
10828 that begins with @samp{-}.
10829
10830 @item --portability
10831 @opindex --portability
10832 Print a message if a file name is not portable to all @acronym{POSIX}
10833 hosts.  This option is equivalent to @samp{-p -P}.
10834
10835 @end table
10836
10837 @cindex exit status of @command{pathchk}
10838 Exit status:
10839
10840 @display
10841 0 if all specified file names passed all checks,
10842 1 otherwise.
10843 @end display
10844
10845
10846 @node Working context
10847 @chapter Working context
10848
10849 @cindex working context
10850 @cindex commands for printing the working context
10851
10852 This section describes commands that display or alter the context in
10853 which you are working: the current directory, the terminal settings, and
10854 so forth.  See also the user-related commands in the next section.
10855
10856 @menu
10857 * pwd invocation::              Print working directory.
10858 * stty invocation::             Print or change terminal characteristics.
10859 * printenv invocation::         Print environment variables.
10860 * tty invocation::              Print file name of terminal on standard input.
10861 @end menu
10862
10863
10864 @node pwd invocation
10865 @section @command{pwd}: Print working directory
10866
10867 @pindex pwd
10868 @cindex print name of current directory
10869 @cindex current working directory, printing
10870 @cindex working directory, printing
10871
10872 @cindex symbolic links and @command{pwd}
10873 @command{pwd} prints the fully resolved name of the current directory.
10874 That is, all components of the printed name will be actual directory
10875 names---none will be symbolic links.
10876
10877 @cindex conflicts with shell built-ins
10878 @cindex built-in shell commands, conflicts with
10879 Because most shells have a built-in @command{pwd} command, using an
10880 unadorned @command{pwd} in a script or interactively may get you
10881 different functionality than that described here.
10882
10883 The only options are a lone @option{--help} or
10884 @option{--version}.  @xref{Common options}.
10885
10886 @exitstatus
10887
10888
10889 @node stty invocation
10890 @section @command{stty}: Print or change terminal characteristics
10891
10892 @pindex stty
10893 @cindex change or print terminal settings
10894 @cindex terminal settings
10895 @cindex line settings of terminal
10896
10897 @command{stty} prints or changes terminal characteristics, such as baud rate.
10898 Synopses:
10899
10900 @example
10901 stty [@var{option}] [@var{setting}]@dots{}
10902 stty [@var{option}]
10903 @end example
10904
10905 If given no line settings, @command{stty} prints the baud rate, line
10906 discipline number (on systems that support it), and line settings
10907 that have been changed from the values set by @samp{stty sane}.
10908 By default, mode reading and setting are performed on the tty line
10909 connected to standard input, although this can be modified by the
10910 @option{--file} option.
10911
10912 @command{stty} accepts many non-option arguments that change aspects of
10913 the terminal line operation, as described below.
10914
10915 The program accepts the following options.  Also see @ref{Common options}.
10916
10917 @table @samp
10918 @item -a
10919 @itemx --all
10920 @opindex -a
10921 @opindex --all
10922 Print all current settings in human-readable form.  This option may not
10923 be used in combination with any line settings.
10924
10925 @item -F @var{device}
10926 @itemx --file=@var{device}
10927 @opindex -F
10928 @opindex --file
10929 Set the line opened by the file name specified in @var{device} instead of
10930 the tty line connected to standard input.  This option is necessary
10931 because opening a @acronym{POSIX} tty requires use of the @code{O_NONDELAY} flag to
10932 prevent a @acronym{POSIX} tty from blocking until the carrier detect line is high if
10933 the @code{clocal} flag is not set.  Hence, it is not always possible
10934 to allow the shell to open the device in the traditional manner.
10935
10936 @item -g
10937 @itemx --save
10938 @opindex -g
10939 @opindex --save
10940 @cindex machine-readable @command{stty} output
10941 Print all current settings in a form that can be used as an argument to
10942 another @command{stty} command to restore the current settings.  This option
10943 may not be used in combination with any line settings.
10944
10945 @end table
10946
10947 Many settings can be turned off by preceding them with a @samp{-}.
10948 Such arguments are marked below with ``May be negated'' in their
10949 description.  The descriptions themselves refer to the positive
10950 case, that is, when @emph{not} negated (unless stated otherwise,
10951 of course).
10952
10953 Some settings are not available on all @acronym{POSIX} systems, since they use
10954 extensions.  Such arguments are marked below with ``Non-@acronym{POSIX}'' in their
10955 description.  On non-@acronym{POSIX} systems, those or other settings also may not
10956 be available, but it's not feasible to document all the variations: just
10957 try it and see.
10958
10959 @exitstatus
10960
10961 @menu
10962 * Control::                     Control settings
10963 * Input::                       Input settings
10964 * Output::                      Output settings
10965 * Local::                       Local settings
10966 * Combination::                 Combination settings
10967 * Characters::                  Special characters
10968 * Special::                     Special settings
10969 @end menu
10970
10971
10972 @node Control
10973 @subsection Control settings
10974
10975 @cindex control settings
10976 Control settings:
10977
10978 @table @samp
10979 @item parenb
10980 @opindex parenb
10981 @cindex two-way parity
10982 Generate parity bit in output and expect parity bit in input.
10983 May be negated.
10984
10985 @item parodd
10986 @opindex parodd
10987 @cindex odd parity
10988 @cindex even parity
10989 Set odd parity (even if negated).  May be negated.
10990
10991 @item cs5
10992 @itemx cs6
10993 @itemx cs7
10994 @itemx cs8
10995 @opindex cs@var{n}
10996 @cindex character size
10997 @cindex eight-bit characters
10998 Set character size to 5, 6, 7, or 8 bits.
10999
11000 @item hup
11001 @itemx hupcl
11002 @opindex hup[cl]
11003 Send a hangup signal when the last process closes the tty.  May be
11004 negated.
11005
11006 @item cstopb
11007 @opindex cstopb
11008 @cindex stop bits
11009 Use two stop bits per character (one if negated).  May be negated.
11010
11011 @item cread
11012 @opindex cread
11013 Allow input to be received.  May be negated.
11014
11015 @item clocal
11016 @opindex clocal
11017 @cindex modem control
11018 Disable modem control signals.  May be negated.
11019
11020 @item crtscts
11021 @opindex crtscts
11022 @cindex hardware flow control
11023 @cindex flow control, hardware
11024 @cindex RTS/CTS flow control
11025 Enable RTS/CTS flow control.  Non-@acronym{POSIX}.  May be negated.
11026 @end table
11027
11028
11029 @node Input
11030 @subsection Input settings
11031
11032 @cindex input settings
11033
11034 @table @samp
11035 @item ignbrk
11036 @opindex ignbrk
11037 @cindex breaks, ignoring
11038 Ignore break characters.  May be negated.
11039
11040 @item brkint
11041 @opindex brkint
11042 @cindex breaks, cause interrupts
11043 Make breaks cause an interrupt signal.  May be negated.
11044
11045 @item ignpar
11046 @opindex ignpar
11047 @cindex parity, ignoring
11048 Ignore characters with parity errors.  May be negated.
11049
11050 @item parmrk
11051 @opindex parmrk
11052 @cindex parity errors, marking
11053 Mark parity errors (with a 255-0-character sequence).  May be negated.
11054
11055 @item inpck
11056 @opindex inpck
11057 Enable input parity checking.  May be negated.
11058
11059 @item istrip
11060 @opindex istrip
11061 @cindex eight-bit input
11062 Clear high (8th) bit of input characters.  May be negated.
11063
11064 @item inlcr
11065 @opindex inlcr
11066 @cindex newline, translating to return
11067 Translate newline to carriage return.  May be negated.
11068
11069 @item igncr
11070 @opindex igncr
11071 @cindex return, ignoring
11072 Ignore carriage return.  May be negated.
11073
11074 @item icrnl
11075 @opindex icrnl
11076 @cindex return, translating to newline
11077 Translate carriage return to newline.  May be negated.
11078
11079 @item iutf8
11080 @opindex iutf8
11081 @cindex input encoding, UTF-8
11082 Assume input characters are UTF-8 encoded.  May be negated.
11083
11084 @item ixon
11085 @opindex ixon
11086 @kindex C-s/C-q flow control
11087 @cindex XON/XOFF flow control
11088 Enable XON/XOFF flow control (that is, @kbd{CTRL-S}/@kbd{CTRL-Q}).  May
11089 be negated.
11090
11091 @item ixoff
11092 @itemx tandem
11093 @opindex ixoff
11094 @opindex tandem
11095 @cindex software flow control
11096 @cindex flow control, software
11097 Enable sending of @code{stop} character when the system input buffer
11098 is almost full, and @code{start} character when it becomes almost
11099 empty again.  May be negated.
11100
11101 @item iuclc
11102 @opindex iuclc
11103 @cindex uppercase, translating to lowercase
11104 Translate uppercase characters to lowercase.  Non-@acronym{POSIX}.  May be
11105 negated.
11106
11107 @item ixany
11108 @opindex ixany
11109 Allow any character to restart output (only the start character
11110 if negated).  Non-@acronym{POSIX}.  May be negated.
11111
11112 @item imaxbel
11113 @opindex imaxbel
11114 @cindex beeping at input buffer full
11115 Enable beeping and not flushing input buffer if a character arrives
11116 when the input buffer is full.  Non-@acronym{POSIX}.  May be negated.
11117 @end table
11118
11119
11120 @node Output
11121 @subsection Output settings
11122
11123 @cindex output settings
11124 These arguments specify output-related operations.
11125
11126 @table @samp
11127 @item opost
11128 @opindex opost
11129 Postprocess output.  May be negated.
11130
11131 @item olcuc
11132 @opindex olcuc
11133 @cindex lowercase, translating to output
11134 Translate lowercase characters to uppercase.  Non-@acronym{POSIX}.  May be
11135 negated.
11136
11137 @item ocrnl
11138 @opindex ocrnl
11139 @cindex return, translating to newline
11140 Translate carriage return to newline.  Non-@acronym{POSIX}.  May be negated.
11141
11142 @item onlcr
11143 @opindex onlcr
11144 @cindex newline, translating to crlf
11145 Translate newline to carriage return-newline.  Non-@acronym{POSIX}.  May be
11146 negated.
11147
11148 @item onocr
11149 @opindex onocr
11150 Do not print carriage returns in the first column.  Non-@acronym{POSIX}.
11151 May be negated.
11152
11153 @item onlret
11154 @opindex onlret
11155 Newline performs a carriage return.  Non-@acronym{POSIX}.  May be negated.
11156
11157 @item ofill
11158 @opindex ofill
11159 @cindex pad instead of timing for delaying
11160 Use fill (padding) characters instead of timing for delays.  Non-@acronym{POSIX}.
11161 May be negated.
11162
11163 @item ofdel
11164 @opindex ofdel
11165 @cindex pad character
11166 Use delete characters for fill instead of null characters.  Non-@acronym{POSIX}.
11167 May be negated.
11168
11169 @item nl1
11170 @itemx nl0
11171 @opindex nl@var{n}
11172 Newline delay style.  Non-@acronym{POSIX}.
11173
11174 @item cr3
11175 @itemx cr2
11176 @itemx cr1
11177 @itemx cr0
11178 @opindex cr@var{n}
11179 Carriage return delay style.  Non-@acronym{POSIX}.
11180
11181 @item tab3
11182 @itemx tab2
11183 @itemx tab1
11184 @itemx tab0
11185 @opindex tab@var{n}
11186 Horizontal tab delay style.  Non-@acronym{POSIX}.
11187
11188 @item bs1
11189 @itemx bs0
11190 @opindex bs@var{n}
11191 Backspace delay style.  Non-@acronym{POSIX}.
11192
11193 @item vt1
11194 @itemx vt0
11195 @opindex vt@var{n}
11196 Vertical tab delay style.  Non-@acronym{POSIX}.
11197
11198 @item ff1
11199 @itemx ff0
11200 @opindex ff@var{n}
11201 Form feed delay style.  Non-@acronym{POSIX}.
11202 @end table
11203
11204
11205 @node Local
11206 @subsection Local settings
11207
11208 @cindex local settings
11209
11210 @table @samp
11211 @item isig
11212 @opindex isig
11213 Enable @code{interrupt}, @code{quit}, and @code{suspend} special
11214 characters.  May be negated.
11215
11216 @item icanon
11217 @opindex icanon
11218 Enable @code{erase}, @code{kill}, @code{werase}, and @code{rprnt}
11219 special characters.  May be negated.
11220
11221 @item iexten
11222 @opindex iexten
11223 Enable non-@acronym{POSIX} special characters.  May be negated.
11224
11225 @item echo
11226 @opindex echo
11227 Echo input characters.  May be negated.
11228
11229 @item echoe
11230 @itemx crterase
11231 @opindex echoe
11232 @opindex crterase
11233 Echo @code{erase} characters as backspace-space-backspace.  May be
11234 negated.
11235
11236 @item echok
11237 @opindex echok
11238 @cindex newline echoing after @code{kill}
11239 Echo a newline after a @code{kill} character.  May be negated.
11240
11241 @item echonl
11242 @opindex echonl
11243 @cindex newline, echoing
11244 Echo newline even if not echoing other characters.  May be negated.
11245
11246 @item noflsh
11247 @opindex noflsh
11248 @cindex flushing, disabling
11249 Disable flushing after @code{interrupt} and @code{quit} special
11250 characters.  May be negated.
11251
11252 @item xcase
11253 @opindex xcase
11254 @cindex case translation
11255 Enable input and output of uppercase characters by preceding their
11256 lowercase equivalents with @samp{\}, when @code{icanon} is set.
11257 Non-@acronym{POSIX}.  May be negated.
11258
11259 @item tostop
11260 @opindex tostop
11261 @cindex background jobs, stopping at terminal write
11262 Stop background jobs that try to write to the terminal.  Non-@acronym{POSIX}.
11263 May be negated.
11264
11265 @item echoprt
11266 @itemx prterase
11267 @opindex echoprt
11268 @opindex prterase
11269 Echo erased characters backward, between @samp{\} and @samp{/}.
11270 Non-@acronym{POSIX}.  May be negated.
11271
11272 @item echoctl
11273 @itemx ctlecho
11274 @opindex echoctl
11275 @opindex ctlecho
11276 @cindex control characters, using @samp{^@var{c}}
11277 @cindex hat notation for control characters
11278 Echo control characters in hat notation (@samp{^@var{c}}) instead
11279 of literally.  Non-@acronym{POSIX}.  May be negated.
11280
11281 @item echoke
11282 @itemx crtkill
11283 @opindex echoke
11284 @opindex crtkill
11285 Echo the @code{kill} special character by erasing each character on
11286 the line as indicated by the @code{echoprt} and @code{echoe} settings,
11287 instead of by the @code{echoctl} and @code{echok} settings.  Non-@acronym{POSIX}.
11288 May be negated.
11289 @end table
11290
11291
11292 @node Combination
11293 @subsection Combination settings
11294
11295 @cindex combination settings
11296 Combination settings:
11297
11298 @table @samp
11299 @item evenp
11300 @opindex evenp
11301 @itemx parity
11302 @opindex parity
11303 Same as @code{parenb -parodd cs7}.  May be negated.  If negated, same
11304 as @code{-parenb cs8}.
11305
11306 @item oddp
11307 @opindex oddp
11308 Same as @code{parenb parodd cs7}.  May be negated.  If negated, same
11309 as @code{-parenb cs8}.
11310
11311 @item nl
11312 @opindex nl
11313 Same as @code{-icrnl -onlcr}.  May be negated.  If negated, same as
11314 @code{icrnl -inlcr -igncr onlcr -ocrnl -onlret}.
11315
11316 @item ek
11317 @opindex ek
11318 Reset the @code{erase} and @code{kill} special characters to their default
11319 values.
11320
11321 @item sane
11322 @opindex sane
11323 Same as:
11324
11325 @c This is too long to write inline.
11326 @example
11327 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff
11328 -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr
11329 -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
11330 ff0 isig icanon iexten echo echoe echok -echonl
11331 -noflsh -xcase -tostop -echoprt echoctl echoke
11332 @end example
11333
11334 @noindent
11335 and also sets all special characters to their default values.
11336
11337 @item cooked
11338 @opindex cooked
11339 Same as @code{brkint ignpar istrip icrnl ixon opost isig icanon}, plus
11340 sets the @code{eof} and @code{eol} characters to their default values
11341 if they are the same as the @code{min} and @code{time} characters.
11342 May be negated.  If negated, same as @code{raw}.
11343
11344 @item raw
11345 @opindex raw
11346 Same as:
11347
11348 @example
11349 -ignbrk -brkint -ignpar -parmrk -inpck -istrip
11350 -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
11351 -imaxbel -opost -isig -icanon -xcase min 1 time 0
11352 @end example
11353
11354 @noindent
11355 May be negated.  If negated, same as @code{cooked}.
11356
11357 @item cbreak
11358 @opindex cbreak
11359 Same as @option{-icanon}.  May be negated.  If negated, same as
11360 @code{icanon}.
11361
11362 @item pass8
11363 @opindex pass8
11364 @cindex eight-bit characters
11365 Same as @code{-parenb -istrip cs8}.  May be negated.  If negated,
11366 same as @code{parenb istrip cs7}.
11367
11368 @item litout
11369 @opindex litout
11370 Same as @option{-parenb -istrip -opost cs8}.  May be negated.
11371 If negated, same as @code{parenb istrip opost cs7}.
11372
11373 @item decctlq
11374 @opindex decctlq
11375 Same as @option{-ixany}.  Non-@acronym{POSIX}.  May be negated.
11376
11377 @item tabs
11378 @opindex tabs
11379 Same as @code{tab0}.  Non-@acronym{POSIX}.  May be negated.  If negated, same
11380 as @code{tab3}.
11381
11382 @item lcase
11383 @itemx LCASE
11384 @opindex lcase
11385 @opindex LCASE
11386 Same as @code{xcase iuclc olcuc}.  Non-@acronym{POSIX}.  May be negated.
11387
11388 @item crt
11389 @opindex crt
11390 Same as @code{echoe echoctl echoke}.
11391
11392 @item dec
11393 @opindex dec
11394 Same as @code{echoe echoctl echoke -ixany intr ^C erase ^? kill C-u}.
11395 @end table
11396
11397
11398 @node Characters
11399 @subsection Special characters
11400
11401 @cindex special characters
11402 @cindex characters, special
11403
11404 The special characters' default values vary from system to system.
11405 They are set with the syntax @samp{name value}, where the names are
11406 listed below and the value can be given either literally, in hat
11407 notation (@samp{^@var{c}}), or as an integer which may start with
11408 @samp{0x} to indicate hexadecimal, @samp{0} to indicate octal, or
11409 any other digit to indicate decimal.
11410
11411 @cindex disabling special characters
11412 @kindex u@r{, and disabling special characters}
11413 For GNU stty, giving a value of @code{^-} or @code{undef} disables that
11414 special character.  (This is incompatible with Ultrix @command{stty},
11415 which uses  a value of @samp{u} to disable a special character.  GNU
11416 @command{stty} treats a value @samp{u} like any other, namely to set that
11417 special character to @key{U}.)
11418
11419 @table @samp
11420
11421 @item intr
11422 @opindex intr
11423 Send an interrupt signal.
11424
11425 @item quit
11426 @opindex quit
11427 Send a quit signal.
11428
11429 @item erase
11430 @opindex erase
11431 Erase the last character typed.
11432
11433 @item kill
11434 @opindex kill
11435 Erase the current line.
11436
11437 @item eof
11438 @opindex eof
11439 Send an end of file (terminate the input).
11440
11441 @item eol
11442 @opindex eol
11443 End the line.
11444
11445 @item eol2
11446 @opindex eol2
11447 Alternate character to end the line.  Non-@acronym{POSIX}.
11448
11449 @item swtch
11450 @opindex swtch
11451 Switch to a different shell layer.  Non-@acronym{POSIX}.
11452
11453 @item start
11454 @opindex start
11455 Restart the output after stopping it.
11456
11457 @item stop
11458 @opindex stop
11459 Stop the output.
11460
11461 @item susp
11462 @opindex susp
11463 Send a terminal stop signal.
11464
11465 @item dsusp
11466 @opindex dsusp
11467 Send a terminal stop signal after flushing the input.  Non-@acronym{POSIX}.
11468
11469 @item rprnt
11470 @opindex rprnt
11471 Redraw the current line.  Non-@acronym{POSIX}.
11472
11473 @item werase
11474 @opindex werase
11475 Erase the last word typed.  Non-@acronym{POSIX}.
11476
11477 @item lnext
11478 @opindex lnext
11479 Enter the next character typed literally, even if it is a special
11480 character.  Non-@acronym{POSIX}.
11481 @end table
11482
11483
11484 @node Special
11485 @subsection Special settings
11486
11487 @cindex special settings
11488
11489 @table @samp
11490 @item min @var{n}
11491 @opindex min
11492 Set the minimum number of characters that will satisfy a read until
11493 the time value has expired, when @option{-icanon} is set.
11494
11495 @item time @var{n}
11496 @opindex time
11497 Set the number of tenths of a second before reads time out if the minimum
11498 number of characters have not been read, when @option{-icanon} is set.
11499
11500 @item ispeed @var{n}
11501 @opindex ispeed
11502 Set the input speed to @var{n}.
11503
11504 @item ospeed @var{n}
11505 @opindex ospeed
11506 Set the output speed to @var{n}.
11507
11508 @item rows @var{n}
11509 @opindex rows
11510 Tell the tty kernel driver that the terminal has @var{n} rows.  Non-@acronym{POSIX}.
11511
11512 @item cols @var{n}
11513 @itemx columns @var{n}
11514 @opindex cols
11515 @opindex columns
11516 Tell the kernel that the terminal has @var{n} columns.  Non-@acronym{POSIX}.
11517
11518 @item size
11519 @opindex size
11520 @vindex LINES
11521 @vindex COLUMNS
11522 Print the number of rows and columns that the kernel thinks the
11523 terminal has.  (Systems that don't support rows and columns in the kernel
11524 typically use the environment variables @env{LINES} and @env{COLUMNS}
11525 instead; however, GNU @command{stty} does not know anything about them.)
11526 Non-@acronym{POSIX}.
11527
11528 @item line @var{n}
11529 @opindex line
11530 Use line discipline @var{n}.  Non-@acronym{POSIX}.
11531
11532 @item speed
11533 @opindex speed
11534 Print the terminal speed.
11535
11536 @item @var{n}
11537 @cindex baud rate, setting
11538 @c FIXME: Is this still true that the baud rate can't be set
11539 @c higher than 38400?
11540 Set the input and output speeds to @var{n}.  @var{n} can be one
11541 of: 0 50 75 110 134 134.5 150 200 300 600 1200 1800 2400 4800 9600
11542 19200 38400 @code{exta} @code{extb}.  @code{exta} is the same as
11543 19200; @code{extb} is the same as 38400.  0 hangs up the line if
11544 @option{-clocal} is set.
11545 @end table
11546
11547
11548 @node printenv invocation
11549 @section @command{printenv}: Print all or some environment variables
11550
11551 @pindex printenv
11552 @cindex printing all or some environment variables
11553 @cindex environment variables, printing
11554
11555 @command{printenv} prints environment variable values.  Synopsis:
11556
11557 @example
11558 printenv [@var{option}] [@var{variable}]@dots{}
11559 @end example
11560
11561 If no @var{variable}s are specified, @command{printenv} prints the value of
11562 every environment variable.  Otherwise, it prints the value of each
11563 @var{variable} that is set, and nothing for those that are not set.
11564
11565 The only options are a lone @option{--help} or @option{--version}.
11566 @xref{Common options}.
11567
11568 @cindex exit status of @command{printenv}
11569 Exit status:
11570
11571 @display
11572 0 if all variables specified were found
11573 1 if at least one specified variable was not found
11574 2 if a write error occurred
11575 @end display
11576
11577
11578 @node tty invocation
11579 @section @command{tty}: Print file name of terminal on standard input
11580
11581 @pindex tty
11582 @cindex print terminal file name
11583 @cindex terminal file name, printing
11584
11585 @command{tty} prints the file name of the terminal connected to its standard
11586 input.  It prints @samp{not a tty} if standard input is not a terminal.
11587 Synopsis:
11588
11589 @example
11590 tty [@var{option}]@dots{}
11591 @end example
11592
11593 The program accepts the following option.  Also see @ref{Common options}.
11594
11595 @table @samp
11596
11597 @item -s
11598 @itemx --silent
11599 @itemx --quiet
11600 @opindex -s
11601 @opindex --silent
11602 @opindex --quiet
11603 Print nothing; only return an exit status.
11604
11605 @end table
11606
11607 @cindex exit status of @command{tty}
11608 Exit status:
11609
11610 @display
11611 0 if standard input is a terminal
11612 1 if standard input is not a terminal
11613 2 if given incorrect arguments
11614 3 if a write error occurs
11615 @end display
11616
11617
11618 @node User information
11619 @chapter User information
11620
11621 @cindex user information, commands for
11622 @cindex commands for printing user information
11623
11624 This section describes commands that print user-related information:
11625 logins, groups, and so forth.
11626
11627 @menu
11628 * id invocation::               Print user identity.
11629 * logname invocation::          Print current login name.
11630 * whoami invocation::           Print effective user ID.
11631 * groups invocation::           Print group names a user is in.
11632 * users invocation::            Print login names of users currently logged in.
11633 * who invocation::              Print who is currently logged in.
11634 @end menu
11635
11636
11637 @node id invocation
11638 @section @command{id}: Print user identity
11639
11640 @pindex id
11641 @cindex real user and group IDs, printing
11642 @cindex effective user and group IDs, printing
11643 @cindex printing real and effective user and group IDs
11644
11645 @command{id} prints information about the given user, or the process
11646 running it if no user is specified.  Synopsis:
11647
11648 @example
11649 id [@var{option}]@dots{} [@var{username}]
11650 @end example
11651
11652 By default, it prints the real user ID, real group ID, effective user ID
11653 if different from the real user ID, effective group ID if different from
11654 the real group ID, and supplemental group IDs.
11655
11656 Each of these numeric values is preceded by an identifying string and
11657 followed by the corresponding user or group name in parentheses.
11658
11659 The options cause @command{id} to print only part of the above information.
11660 Also see @ref{Common options}.
11661
11662 @table @samp
11663 @item -g
11664 @itemx --group
11665 @opindex -g
11666 @opindex --group
11667 Print only the group ID.
11668
11669 @item -G
11670 @itemx --groups
11671 @opindex -G
11672 @opindex --groups
11673 Print only the group ID and the supplementary groups.
11674
11675 @item -n
11676 @itemx --name
11677 @opindex -n
11678 @opindex --name
11679 Print the user or group name instead of the ID number.  Requires
11680 @option{-u}, @option{-g}, or @option{-G}.
11681
11682 @item -r
11683 @itemx --real
11684 @opindex -r
11685 @opindex --real
11686 Print the real, instead of effective, user or group ID.  Requires
11687 @option{-u}, @option{-g}, or @option{-G}.
11688
11689 @item -u
11690 @itemx --user
11691 @opindex -u
11692 @opindex --user
11693 Print only the user ID.
11694
11695 @end table
11696
11697 @exitstatus
11698
11699
11700 @node logname invocation
11701 @section @command{logname}: Print current login name
11702
11703 @pindex logname
11704 @cindex printing user's login name
11705 @cindex login name, printing
11706 @cindex user name, printing
11707
11708 @flindex utmp
11709 @command{logname} prints the calling user's name, as found in a
11710 system-maintained file (often @file{/var/run/utmp} or
11711 @file{/etc/utmp}), and exits with a status of 0.  If there is no entry
11712 for the calling process, @command{logname} prints
11713 an error message and exits with a status of 1.
11714
11715 The only options are @option{--help} and @option{--version}.  @xref{Common
11716 options}.
11717
11718 @exitstatus
11719
11720
11721 @node whoami invocation
11722 @section @command{whoami}: Print effective user ID
11723
11724 @pindex whoami
11725 @cindex effective user ID, printing
11726 @cindex printing the effective user ID
11727
11728 @command{whoami} prints the user name associated with the current
11729 effective user ID.  It is equivalent to the command @samp{id -un}.
11730
11731 The only options are @option{--help} and @option{--version}.  @xref{Common
11732 options}.
11733
11734 @exitstatus
11735
11736
11737 @node groups invocation
11738 @section @command{groups}: Print group names a user is in
11739
11740 @pindex groups
11741 @cindex printing groups a user is in
11742 @cindex supplementary groups, printing
11743
11744 @command{groups} prints the names of the primary and any supplementary
11745 groups for each given @var{username}, or the current process if no names
11746 are given.  If names are given, the name of each user is printed before
11747 the list of that user's groups.  Synopsis:
11748
11749 @example
11750 groups [@var{username}]@dots{}
11751 @end example
11752
11753 The group lists are equivalent to the output of the command @samp{id -Gn}.
11754
11755 The only options are @option{--help} and @option{--version}.  @xref{Common
11756 options}.
11757
11758 @exitstatus
11759
11760
11761 @node users invocation
11762 @section @command{users}: Print login names of users currently logged in
11763
11764 @pindex users
11765 @cindex printing current usernames
11766 @cindex usernames, printing current
11767
11768 @cindex login sessions, printing users with
11769 @command{users} prints on a single line a blank-separated list of user
11770 names of users currently logged in to the current host.  Each user name
11771 corresponds to a login session, so if a user has more than one login
11772 session, that user's name will appear the same number of times in the
11773 output.  Synopsis:
11774
11775 @example
11776 users [@var{file}]
11777 @end example
11778
11779 @flindex utmp
11780 @flindex wtmp
11781 With no @var{file} argument, @command{users} extracts its information from
11782 a system-maintained file (often @file{/var/run/utmp} or
11783 @file{/etc/utmp}).  If a file argument is given, @command{users} uses
11784 that file instead.  A common choice is @file{/var/log/wtmp}.
11785
11786 The only options are @option{--help} and @option{--version}.  @xref{Common
11787 options}.
11788
11789 @exitstatus
11790
11791
11792 @node who invocation
11793 @section @command{who}: Print who is currently logged in
11794
11795 @pindex who
11796 @cindex printing current user information
11797 @cindex information, about current users
11798
11799 @command{who} prints information about users who are currently logged on.
11800 Synopsis:
11801
11802 @example
11803 @command{who} [@var{option}] [@var{file}] [am i]
11804 @end example
11805
11806 @cindex terminal lines, currently used
11807 @cindex login time
11808 @cindex remote hostname
11809 If given no non-option arguments, @command{who} prints the following
11810 information for each user currently logged on: login name, terminal
11811 line, login time, and remote hostname or X display.
11812
11813 @flindex utmp
11814 @flindex wtmp
11815 If given one non-option argument, @command{who} uses that instead of
11816 a default system-maintained file (often @file{/var/run/utmp} or
11817 @file{/etc/utmp}) as the name of the file containing the record of
11818 users logged on.  @file{/var/log/wtmp} is commonly given as an argument
11819 to @command{who} to look at who has previously logged on.
11820
11821 @opindex am i
11822 @opindex who am i
11823 If given two non-option arguments, @command{who} prints only the entry
11824 for the user running it (determined from its standard input), preceded
11825 by the hostname.  Traditionally, the two arguments given are @samp{am
11826 i}, as in @samp{who am i}.
11827
11828 @vindex TZ
11829 Time stamps are listed according to the time zone rules specified by
11830 the @env{TZ} environment variable, or by the system default rules if
11831 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
11832 with @env{TZ}, libc, The GNU C Library}.
11833
11834 The program accepts the following options.  Also see @ref{Common options}.
11835
11836 @table @samp
11837
11838 @item -a
11839 @itemx --all
11840 @opindex -a
11841 @opindex --all
11842 Same as @samp{-b -d --login -p -r -t -T -u}.
11843
11844 @item -b
11845 @itemx --boot
11846 @opindex -b
11847 @opindex --boot
11848 Print the date and time of last system boot.
11849
11850 @item -d
11851 @itemx --dead
11852 @opindex -d
11853 @opindex --dead
11854 Print information corresponding to dead processes.
11855
11856 @item -H
11857 @itemx --heading
11858 @opindex -H
11859 @opindex --heading
11860 Print column headings.
11861
11862 @item -m
11863 @opindex -m
11864 Same as @samp{who am i}.
11865
11866 @item -q
11867 @itemx --count
11868 @opindex -q
11869 @opindex --count
11870 Print only the login names and the number of users logged on.
11871 Overrides all other options.
11872
11873 @item -s
11874 @opindex -s
11875 Ignored; for compatibility with other versions of @command{who}.
11876
11877 @itemx -u
11878 @opindex -u
11879 @cindex idle time
11880 After the login time, print the number of hours and minutes that the
11881 user has been idle.  @samp{.} means the user was active in the last minute.
11882 @samp{old} means the user has been idle for more than 24 hours.
11883
11884 @item -l
11885 @itemx --login
11886 @opindex -l
11887 @opindex --login
11888 List only the entries that correspond to processes via which the
11889 system is waiting for a user to login.  The user name is always @samp{LOGIN}.
11890
11891 @itemx --lookup
11892 @opindex --lookup
11893 Attempt to canonicalize hostnames found in utmp through a DNS lookup.  This
11894 is not the default because it can cause significant delays on systems with
11895 automatic dial-up internet access.
11896
11897 @item -H
11898 @itemx --heading
11899 @opindex -H
11900 @opindex --heading
11901 Print a line of column headings.
11902
11903 @item -w
11904 @itemx -T
11905 @itemx --mesg
11906 @itemx --message
11907 @itemx --writable
11908 @opindex -w
11909 @opindex -T
11910 @opindex --mesg
11911 @opindex --message
11912 @opindex --writable
11913 @cindex message status
11914 @pindex write@r{, allowed}
11915 After each login name print a character indicating the user's message status:
11916
11917 @display
11918 @samp{+} allowing @code{write} messages
11919 @samp{-} disallowing @code{write} messages
11920 @samp{?} cannot find terminal device
11921 @end display
11922
11923 @end table
11924
11925 @exitstatus
11926
11927
11928 @node System context
11929 @chapter System context
11930
11931 @cindex system context
11932 @cindex context, system
11933 @cindex commands for system context
11934
11935 This section describes commands that print or change system-wide
11936 information.
11937
11938 @menu
11939 * date invocation::             Print or set system date and time.
11940 * uname invocation::            Print system information.
11941 * hostname invocation::         Print or set system name.
11942 * hostid invocation::           Print numeric host identifier.
11943 @end menu
11944
11945
11946 @node date invocation
11947 @section @command{date}: Print or set system date and time
11948
11949 @pindex date
11950 @cindex time, printing or setting
11951 @cindex printing the current time
11952
11953 Synopses:
11954
11955 @example
11956 date [@var{option}]@dots{} [+@var{format}]
11957 date [-u|--utc|--universal] @c this avoids a newline in the output
11958 [ MMDDhhmm[[CC]YY][.ss] ]
11959 @end example
11960
11961 @vindex LC_TIME
11962 Invoking @command{date} with no @var{format} argument is equivalent to invoking
11963 it with a default format that depends on the @env{LC_TIME} locale category.
11964 In the default C locale, this format is @samp{'+%a %b %e %H:%M:%S %Z %Y'},
11965 so the output looks like @samp{Thu Mar @ 3 13:47:51 PST 2005}.
11966
11967 @vindex TZ
11968 Normally, @command{date} uses the time zone rules indicated by the
11969 @env{TZ} environment variable, or the system default rules if @env{TZ}
11970 is not set.  @xref{TZ Variable,, Specifying the Time Zone with
11971 @env{TZ}, libc, The GNU C Library}.
11972
11973 @findex strftime @r{and @command{date}}
11974 @cindex time formats
11975 @cindex formatting times
11976 If given an argument that starts with a @samp{+}, @command{date} prints the
11977 current date and time (or the date and time specified by the
11978 @option{--date} option, see below) in the format defined by that argument,
11979 which is similar to that of the @code{strftime} function.  Except for
11980 conversion specifiers, which start with @samp{%}, characters in the
11981 format string are printed unchanged.  The conversion specifiers are
11982 described below.
11983
11984 @exitstatus
11985
11986 @menu
11987 * Time conversion specifiers::     %[HIklMNpPrRsSTXzZ]
11988 * Date conversion specifiers::     %[aAbBcCdDeFgGhjmuUVwWxyY]
11989 * Literal conversion specifiers::  %[%nt]
11990 * Padding and other flags::        Pad with zeros, spaces, etc.
11991 * Setting the time::               Changing the system clock.
11992 * Options for date::               Instead of the current time.
11993 * Examples of date::               Examples.
11994 @end menu
11995
11996 @node Time conversion specifiers
11997 @subsection Time conversion specifiers
11998
11999 @cindex time conversion specifiers
12000 @cindex conversion specifiers, time
12001
12002 @command{date} conversion specifiers related to times.
12003
12004 @table @samp
12005 @item %H
12006 hour (@samp{00}@dots{}@samp{23})
12007 @item %I
12008 hour (@samp{01}@dots{}@samp{12})
12009 @item %k
12010 hour (@samp{ 0}@dots{}@samp{23}).
12011 This is a @acronym{GNU} extension.
12012 @item %l
12013 hour (@samp{ 1}@dots{}@samp{12}).
12014 This is a @acronym{GNU} extension.
12015 @item %M
12016 minute (@samp{00}@dots{}@samp{59})
12017 @item %N
12018 nanoseconds (@samp{000000000}@dots{}@samp{999999999}).
12019 This is a @acronym{GNU} extension.
12020 @item %p
12021 locale's equivalent of either @samp{AM} or @samp{PM};
12022 blank in many locales.
12023 Noon is treated as @samp{PM} and midnight as @samp{AM}.
12024 @item %P
12025 like @samp{%p}, except lower case.
12026 This is a @acronym{GNU} extension.
12027 @item %r
12028 locale's 12-hour clock time (e.g., @samp{11:11:04 PM})
12029 @item %R
12030 24-hour hour and minute.  Same as @samp{%H:%M}.
12031 This is a @acronym{GNU} extension.
12032 @item %s
12033 @cindex epoch, seconds since
12034 @cindex seconds since the epoch
12035 @cindex beginning of time
12036 seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
12037 Leap seconds are not counted unless leap second support is available.
12038 @xref{%s-examples}, for examples.
12039 This is a @acronym{GNU} extension.
12040 @item %S
12041 second (@samp{00}@dots{}@samp{60}).
12042 This may be @samp{60} if leap seconds are supported.
12043 @item %T
12044 24-hour hour, minute, and second.  Same as @samp{%H:%M:%S}.
12045 @item %X
12046 locale's time representation (e.g., @samp{23:13:48})
12047 @item %z
12048 @w{@acronym{RFC} 2822/@acronym{ISO} 8601} style numeric time zone
12049 (e.g., @samp{-0600} or @samp{+0530}), or nothing if no
12050 time zone is determinable.  This value reflects the numeric time zone
12051 appropriate for the current time, using the time zone rules specified
12052 by the @env{TZ} environment variable.
12053 The time (and optionally, the time zone rules) can be overridden
12054 by the @option{--date} option.
12055 This is a @acronym{GNU} extension.
12056 @item %:z
12057 @w{@acronym{RFC} 3339/@acronym{ISO} 8601} style numeric time zone with
12058 @samp{:} (e.g., @samp{-06:00} or @samp{+05:30}), or nothing if no time
12059 zone is determinable.
12060 This is a @acronym{GNU} extension.
12061 @item %::z
12062 Numeric time zone to the nearest second with @samp{:} (e.g.,
12063 @samp{-06:00:00} or @samp{+05:30:00}), or nothing if no time zone is
12064 determinable.
12065 This is a @acronym{GNU} extension.
12066 @item %:::z
12067 Numeric time zone with @samp{:} using the minimum necessary precision
12068 (e.g., @samp{-06}, @samp{+05:30}, or @samp{-04:56:02}), or nothing if
12069 no time zone is determinable.
12070 This is a @acronym{GNU} extension.
12071 @item %Z
12072 alphabetic time zone abbreviation (e.g., @samp{EDT}), or nothing if no
12073 time zone is determinable.  See @samp{%z} for how it is determined.
12074 @end table
12075
12076
12077 @node Date conversion specifiers
12078 @subsection Date conversion specifiers
12079
12080 @cindex date conversion specifiers
12081 @cindex conversion specifiers, date
12082
12083 @command{date} conversion specifiers related to dates.
12084
12085 @table @samp
12086 @item %a
12087 locale's abbreviated weekday name (e.g., @samp{Sun})
12088 @item %A
12089 locale's full weekday name, variable length (e.g., @samp{Sunday})
12090 @item %b
12091 locale's abbreviated month name (e.g., @samp{Jan})
12092 @item %B
12093 locale's full month name, variable length (e.g., @samp{January})
12094 @item %c
12095 locale's date and time (e.g., @samp{Thu Mar @ 3 23:05:25 2005})
12096 @item %C
12097 century.  This is like @samp{%Y}, except the last two digits are omitted.
12098 For example, it is @samp{20} if @samp{%Y} is @samp{2000},
12099 and is @samp{-0} if @samp{%Y} is @samp{-001}.
12100 It is normally at least two characters, but it may be more.
12101 @item %d
12102 day of month (e.g., @samp{01})
12103 @item %D
12104 date; same as @samp{%m/%d/%y}
12105 @item %e
12106 day of month, space padded; same as @samp{%_d}
12107 @item %F
12108 full date in @acronym{ISO} 8601 format; same as @samp{%Y-%m-%d}.
12109 This is a good choice for a date format, as it is standard and
12110 is easy to sort in the usual case where years are in the range
12111 0000@dots{}9999.
12112 This is a @acronym{GNU} extension.
12113 @item %g
12114 year corresponding to the @acronym{ISO} week number, but without the century
12115 (range @samp{00} through @samp{99}).  This has the same format and value
12116 as @samp{%y}, except that if the @acronym{ISO} week number (see
12117 @samp{%V}) belongs
12118 to the previous or next year, that year is used instead.
12119 This is a @acronym{GNU} extension.
12120 @item %G
12121 year corresponding to the @acronym{ISO} week number.  This has the
12122 same format and value as @samp{%Y}, except that if the @acronym{ISO}
12123 week number (see
12124 @samp{%V}) belongs to the previous or next year, that year is used
12125 instead.
12126 It is normally useful only if @samp{%V} is also used;
12127 for example, the format @samp{%G-%m-%d} is probably a mistake,
12128 since it combines the ISO week number year with the conventional month and day.
12129 This is a @acronym{GNU} extension.
12130 @item %h
12131 same as @samp{%b}
12132 @item %j
12133 day of year (@samp{001}@dots{}@samp{366})
12134 @item %m
12135 month (@samp{01}@dots{}@samp{12})
12136 @item %u
12137 day of week (@samp{1}@dots{}@samp{7}) with @samp{1} corresponding to Monday
12138 @item %U
12139 week number of year, with Sunday as the first day of the week
12140 (@samp{00}@dots{}@samp{53}).
12141 Days in a new year preceding the first Sunday are in week zero.
12142 @item %V
12143 @acronym{ISO} week number, that is, the
12144 week number of year, with Monday as the first day of the week
12145 (@samp{01}@dots{}@samp{53}).
12146 If the week containing January 1 has four or more days in
12147 the new year, then it is considered week 1; otherwise, it is week 53 of
12148 the previous year, and the next week is week 1.  (See the @acronym{ISO} 8601
12149 standard.)
12150 @item %w
12151 day of week (@samp{0}@dots{}@samp{6}) with 0 corresponding to Sunday
12152 @item %W
12153 week number of year, with Monday as first day of week
12154 (@samp{00}@dots{}@samp{53}).
12155 Days in a new year preceding the first Monday are in week zero.
12156 @item %x
12157 locale's date representation (e.g., @samp{12/31/99})
12158 @item %y
12159 last two digits of year (@samp{00}@dots{}@samp{99})
12160 @item %Y
12161 year.  This is normally at least four characters, but it may be more.
12162 Year @samp{0000} precedes year @samp{0001}, and year @samp{-001}
12163 precedes year @samp{0000}.
12164 @end table
12165
12166
12167 @node Literal conversion specifiers
12168 @subsection Literal conversion specifiers
12169
12170 @cindex literal conversion specifiers
12171 @cindex conversion specifiers, literal
12172
12173 @command{date} conversion specifiers that produce literal strings.
12174
12175 @table @samp
12176 @item %%
12177 a literal %
12178 @item %n
12179 a newline
12180 @item %t
12181 a horizontal tab
12182 @end table
12183
12184
12185 @node Padding and other flags
12186 @subsection Padding and other flags
12187
12188 @cindex numeric field padding
12189 @cindex padding of numeric fields
12190 @cindex fields, padding numeric
12191
12192 Unless otherwise specified, @command{date} normally pads numeric fields
12193 with zeros, so that, for
12194 example, numeric months are always output as two digits.
12195 Seconds since the epoch are not padded, though,
12196 since there is no natural width for them.
12197
12198 As a @acronym{GNU} extension, @command{date} recognizes any of the
12199 following optional flags after the @samp{%}:
12200
12201 @table @samp
12202 @item -
12203 (hyphen) Do not pad the field; useful if the output is intended for
12204 human consumption.
12205 @item _
12206 (underscore) Pad with spaces; useful if you need a fixed
12207 number of characters in the output, but zeros are too distracting.
12208 @item 0
12209 (zero) Pad with zeros even if the conversion specifier
12210 would normally pad with spaces.
12211 @item ^
12212 Use upper case characters if possible.
12213 @item #
12214 Use opposite case characters if possible.
12215 A field that is normally upper case becomes lower case, and vice versa.
12216 @end table
12217
12218 @noindent
12219 Here are some examples of padding:
12220
12221 @example
12222 date +%d/%m -d "Feb 1"
12223 @result{} 01/02
12224 date +%-d/%-m -d "Feb 1"
12225 @result{} 1/2
12226 date +%_d/%_m -d "Feb 1"
12227 @result{}  1/ 2
12228 @end example
12229
12230 As a @acronym{GNU} extension, you can specify the field width
12231 (after any flag, if present) as a decimal number.  If the natural size of the
12232 output is of the field has less than the specified number of characters,
12233 the result is written right adjusted and padded to the given
12234 size.  For example, @samp{%9B} prints the right adjusted month name in
12235 a field of width 9.
12236
12237 An optional modifier can follow the optional flag and width
12238 specification.  The modifiers are:
12239
12240 @table @samp
12241 @item E
12242 Use the locale's alternate representation for date and time.  This
12243 modifier applies to the @samp{%c}, @samp{%C}, @samp{%x}, @samp{%X},
12244 @samp{%y} and @samp{%Y} conversion specifiers.  In a Japanese locale, for
12245 example, @samp{%Ex} might yield a date format based on the Japanese
12246 Emperors' reigns.
12247
12248 @item O
12249 Use the locale's alternate numeric symbols for numbers.  This modifier
12250 applies only to numeric conversion specifiers.
12251 @end table
12252
12253 If the format supports the modifier but no alternate representation
12254 is available, it is ignored.
12255
12256
12257 @node Setting the time
12258 @subsection Setting the time
12259
12260 @cindex setting the time
12261 @cindex time setting
12262 @cindex appropriate privileges
12263
12264 If given an argument that does not start with @samp{+}, @command{date} sets
12265 the system clock to the date and time specified by that argument (as
12266 described below).  You must have appropriate privileges to set the
12267 system clock.  The @option{--date} and @option{--set} options may not be
12268 used with such an argument.  The @option{--universal} option may be used
12269 with such an argument to indicate that the specified date and time are
12270 relative to Coordinated Universal Time rather than to the local time
12271 zone.
12272
12273 The argument must consist entirely of digits, which have the following
12274 meaning:
12275
12276 @table @samp
12277 @item MM
12278 month
12279 @item DD
12280 day within month
12281 @item hh
12282 hour
12283 @item mm
12284 minute
12285 @item CC
12286 first two digits of year (optional)
12287 @item YY
12288 last two digits of year (optional)
12289 @item ss
12290 second (optional)
12291 @end table
12292
12293 The @option{--set} option also sets the system clock; see the next section.
12294
12295
12296 @node Options for date
12297 @subsection Options for @command{date}
12298
12299 @cindex @command{date} options
12300 @cindex options for @command{date}
12301
12302 The program accepts the following options.  Also see @ref{Common options}.
12303
12304 @table @samp
12305
12306 @item -d @var{datestr}
12307 @itemx --date=@var{datestr}
12308 @opindex -d
12309 @opindex --date
12310 @cindex parsing date strings
12311 @cindex date strings, parsing
12312 @cindex arbitrary date strings, parsing
12313 @opindex yesterday
12314 @opindex tomorrow
12315 @opindex next @var{day}
12316 @opindex last @var{day}
12317 Display the date and time specified in @var{datestr} instead of the
12318 current date and time.  @var{datestr} can be in almost any common
12319 format.  It can contain month names, time zones, @samp{am} and @samp{pm},
12320 @samp{yesterday}, etc.  For example, @option{--date="2004-02-27
12321 14:19:13.489392193 +0530"} specifies the instant of time that is
12322 489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
12323 time zone that is 5 hours and 30 minutes east of @acronym{UTC}.
12324 @xref{Date input formats}.
12325
12326 @item -f @var{datefile}
12327 @itemx --file=@var{datefile}
12328 @opindex -f
12329 @opindex --file
12330 Parse each line in @var{datefile} as with @option{-d} and display the
12331 resulting date and time.  If @var{datefile} is @samp{-}, use standard
12332 input.  This is useful when you have many dates to process, because the
12333 system overhead of starting up the @command{date} executable many times can
12334 be considerable.
12335
12336 @item -r @var{file}
12337 @itemx --reference=@var{file}
12338 @opindex -r
12339 @opindex --reference
12340 Display the date and time of the last modification of @var{file},
12341 instead of the current date and time.
12342
12343 @item -R
12344 @itemx --rfc-822
12345 @itemx --rfc-2822
12346 @opindex -R
12347 @opindex --rfc-822
12348 @opindex --rfc-2822
12349 Display the date and time using the format @samp{%a, %d %b %Y %H:%M:%S
12350 %z}, evaluated in the C locale so abbreviations are always in English.
12351 For example:
12352
12353 @example
12354 Fri, 09 Sep 2005 13:51:39 -0700
12355 @end example
12356
12357 This format conforms to
12358 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc2822.txt, Internet
12359 @acronym{RFCs} 2822} and
12360 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc822.txt, 822}, the
12361 current and previous standards for Internet email.
12362
12363 @item --rfc-3339=@var{timespec}
12364 @opindex --rfc-3339=@var{timespec}
12365 Display the date using a format specified by
12366 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3339.txt, Internet
12367 @acronym{RFC} 3339}.  This is a subset of the @acronym{ISO} 8601
12368 format, except that it also permits applications to use a space rather
12369 than a @samp{T} to separate dates from times.  Unlike the other
12370 standard formats, @acronym{RFC} 3339 format is always suitable as
12371 input for the @option{--date} (@option{-d}) and @option{--file}
12372 (@option{-f}) options, regardless of the current locale.
12373
12374 The argument @var{timespec} specifies how much of the time to include.
12375 It can be one of the following:
12376
12377 @table @samp
12378 @item date
12379 Print just the full-date, e.g., @samp{2005-09-14}.
12380 This is equivalent to the format @samp{%Y-%m-%d}.
12381
12382 @item seconds
12383 Print the full-date and full-time separated by a space, e.g.,
12384 @samp{2005-09-14 00:56:06+05:30}.  The output ends with a numeric
12385 time-offset; here the @samp{+05:30} means that local time is five
12386 hours and thirty minutes east of @acronym{UTC}.  This is equivalent to
12387 the format @samp{%Y-%m-%d %H:%M:%S%:z}.
12388
12389 @item ns
12390 Like @samp{seconds}, but also print nanoseconds, e.g.,
12391 @samp{2005-09-14 00:56:06.998458565+05:30}.
12392 This is equivalent to the format @samp{%Y-%m-%d %H:%M:%S.%N%:z}.
12393
12394 @end table
12395
12396 @item -s @var{datestr}
12397 @itemx --set=@var{datestr}
12398 @opindex -s
12399 @opindex --set
12400 Set the date and time to @var{datestr}.  See @option{-d} above.
12401
12402 @item -u
12403 @itemx --utc
12404 @itemx --universal
12405 @opindex -u
12406 @opindex --utc
12407 @opindex --universal
12408 @cindex Coordinated Universal Time
12409 @cindex UTC
12410 @cindex Greenwich Mean Time
12411 @cindex GMT
12412 @vindex TZ
12413 Use Coordinated Universal Time (@acronym{UTC}) by operating as if the
12414 @env{TZ} environment variable were set to the string @samp{UTC0}.
12415 Coordinated
12416 Universal Time is often called ``Greenwich Mean Time'' (@sc{gmt}) for
12417 historical reasons.
12418 @end table
12419
12420
12421 @node Examples of date
12422 @subsection Examples of @command{date}
12423
12424 @cindex examples of @command{date}
12425
12426 Here are a few examples.  Also see the documentation for the @option{-d}
12427 option in the previous section.
12428
12429 @itemize @bullet
12430
12431 @item
12432 To print the date of the day before yesterday:
12433
12434 @example
12435 date --date='2 days ago'
12436 @end example
12437
12438 @item
12439 To print the date of the day three months and one day hence:
12440
12441 @example
12442 date --date='3 months 1 day'
12443 @end example
12444
12445 @item
12446 To print the day of year of Christmas in the current year:
12447
12448 @example
12449 date --date='25 Dec' +%j
12450 @end example
12451
12452 @item
12453 To print the current full month name and the day of the month:
12454
12455 @example
12456 date '+%B %d'
12457 @end example
12458
12459 But this may not be what you want because for the first nine days of
12460 the month, the @samp{%d} expands to a zero-padded two-digit field,
12461 for example @samp{date -d 1may '+%B %d'} will print @samp{May 01}.
12462
12463 @item
12464 To print a date without the leading zero for one-digit days
12465 of the month, you can use the (@acronym{GNU} extension)
12466 @samp{-} flag to suppress
12467 the padding altogether:
12468
12469 @example
12470 date -d 1may '+%B %-d
12471 @end example
12472
12473 @item
12474 To print the current date and time in the format required by many
12475 non-@acronym{GNU} versions of @command{date} when setting the system clock:
12476
12477 @example
12478 date +%m%d%H%M%Y.%S
12479 @end example
12480
12481 @item
12482 To set the system clock forward by two minutes:
12483
12484 @example
12485 date --set='+2 minutes'
12486 @end example
12487
12488 @item
12489 To print the date in @acronym{RFC} 2822 format,
12490 use @samp{date --rfc-2822}.  Here is some example output:
12491
12492 @example
12493 Fri, 09 Sep 2005 13:51:39 -0700
12494 @end example
12495
12496 @anchor{%s-examples}
12497 @item
12498 To convert a date string to the number of seconds since the epoch
12499 (which is 1970-01-01 00:00:00 UTC), use the @option{--date} option with
12500 the @samp{%s} format.  That can be useful in sorting and/or graphing
12501 and/or comparing data by date.  The following command outputs the
12502 number of the seconds since the epoch for the time two minutes after the
12503 epoch:
12504
12505 @example
12506 date --date='1970-01-01 00:02:00 +0000' +%s
12507 120
12508 @end example
12509
12510 If you do not specify time zone information in the date string,
12511 @command{date} uses your computer's idea of the time zone when
12512 interpreting the string.  For example, if your computer's time zone is
12513 that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
12514 seconds) behind UTC:
12515
12516 @example
12517 # local time zone used
12518 date --date='1970-01-01 00:02:00' +%s
12519 18120
12520 @end example
12521
12522 @item
12523 If you're sorting or graphing dated data, your raw date values may be
12524 represented as seconds since the epoch.  But few people can look at
12525 the date @samp{946684800} and casually note ``Oh, that's the first second
12526 of the year 2000 in Greenwich, England.''
12527
12528 @example
12529 date --date='2000-01-01 UTC' +%s
12530 946684800
12531 @end example
12532
12533 An alternative is to use the @option{--utc} (@option{-u}) option.
12534 Then you may omit @samp{UTC} from the date string.  Although this
12535 produces the same result for @samp{%s} and many other format sequences,
12536 with a time zone offset different from zero, it would give a different
12537 result for zone-dependent formats like @samp{%z}.
12538
12539 @example
12540 date -u --date=2000-01-01 +%s
12541 946684800
12542 @end example
12543
12544 To convert such an unwieldy number of seconds back to
12545 a more readable form, use a command like this:
12546
12547 @smallexample
12548 # local time zone used
12549 date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
12550 1999-12-31 19:00:00 -0500
12551 @end smallexample
12552
12553 Or if you do not mind depending on the @samp{@@} feature present since
12554 coreutils 5.3.0, you could shorten this to:
12555
12556 @smallexample
12557 date -d @@946684800 +"%F %T %z"
12558 1999-12-31 19:00:00 -0500
12559 @end smallexample
12560
12561 Often it is better to output UTC-relative date and time:
12562
12563 @smallexample
12564 date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
12565 2000-01-01 00:00:00 +0000
12566 @end smallexample
12567
12568 @end itemize
12569
12570
12571 @node uname invocation
12572 @section @command{uname}: Print system information
12573
12574 @pindex uname
12575 @cindex print system information
12576 @cindex system information, printing
12577
12578 @command{uname} prints information about the machine and operating system
12579 it is run on.  If no options are given, @command{uname} acts as if the
12580 @option{-s} option were given.  Synopsis:
12581
12582 @example
12583 uname [@var{option}]@dots{}
12584 @end example
12585
12586 If multiple options or @option{-a} are given, the selected information is
12587 printed in this order:
12588
12589 @example
12590 @var{kernel-name} @var{nodename} @var{kernel-release} @var{kernel-version}
12591 @var{machine} @var{processor} @var{hardware-platform} @var{operating-system}
12592 @end example
12593
12594 The information may contain internal spaces, so such output cannot be
12595 parsed reliably.  In the following example, @var{release} is
12596 @samp{2.2.18ss.e820-bda652a #4 SMP Tue Jun 5 11:24:08 PDT 2001}:
12597
12598 @smallexample
12599 uname -a
12600 @result{} Linux dum 2.2.18 #4 SMP Tue Jun 5 11:24:08 PDT 2001 i686 unknown unknown GNU/Linux
12601 @end smallexample
12602
12603
12604 The program accepts the following options.  Also see @ref{Common options}.
12605
12606 @table @samp
12607
12608 @item -a
12609 @itemx --all
12610 @opindex -a
12611 @opindex --all
12612 Print all of the below information, except omit the processor type
12613 and the hardware platform name if they are unknown.
12614
12615 @item -i
12616 @itemx --hardware-platform
12617 @opindex -i
12618 @opindex --hardware-platform
12619 @cindex implementation, hardware
12620 @cindex hardware platform
12621 @cindex platform, hardware
12622 Print the hardware platform name
12623 (sometimes called the hardware implementation).
12624 Print @samp{unknown} if the kernel does not make this information
12625 easily available, as is the case with Linux kernels.
12626
12627 @item -m
12628 @itemx --machine
12629 @opindex -m
12630 @opindex --machine
12631 @cindex machine type
12632 @cindex hardware class
12633 @cindex hardware type
12634 Print the machine hardware name (sometimes called the hardware class
12635 or hardware type).
12636
12637 @item -n
12638 @itemx --nodename
12639 @opindex -n
12640 @opindex --nodename
12641 @cindex hostname
12642 @cindex node name
12643 @cindex network node name
12644 Print the network node hostname.
12645
12646 @item -p
12647 @itemx --processor
12648 @opindex -p
12649 @opindex --processor
12650 @cindex host processor type
12651 Print the processor type (sometimes called the instruction set
12652 architecture or ISA).
12653 Print @samp{unknown} if the kernel does not make this information
12654 easily available, as is the case with Linux kernels.
12655
12656 @item -o
12657 @itemx --operating-system
12658 @opindex -o
12659 @opindex --operating-system
12660 @cindex operating system name
12661 Print the name of the operating system.
12662
12663 @item -r
12664 @itemx --kernel-release
12665 @opindex -r
12666 @opindex --kernel-release
12667 @cindex kernel release
12668 @cindex release of kernel
12669 Print the kernel release.
12670
12671 @item -s
12672 @itemx --kernel-name
12673 @opindex -s
12674 @opindex --kernel-name
12675 @cindex kernel name
12676 @cindex name of kernel
12677 Print the kernel name.
12678 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) calls this
12679 ``the implementation of the operating system'', because the
12680 @acronym{POSIX} specification itself has no notion of ``kernel''.
12681 The kernel name might be the same as the operating system name printed
12682 by the @option{-o} or @option{--operating-system} option, but it might
12683 differ.  Some operating systems (e.g., FreeBSD, HP-UX) have the same
12684 name as their underlying kernels; others (e.g., GNU/Linux, Solaris)
12685 do not.
12686
12687 @item -v
12688 @itemx --kernel-version
12689 @opindex -v
12690 @opindex --kernel-version
12691 @cindex kernel version
12692 @cindex version of kernel
12693 Print the kernel version.
12694
12695 @end table
12696
12697 @exitstatus
12698
12699
12700 @node hostname invocation
12701 @section @command{hostname}: Print or set system name
12702
12703 @pindex hostname
12704 @cindex setting the hostname
12705 @cindex printing the hostname
12706 @cindex system name, printing
12707 @cindex appropriate privileges
12708
12709 With no arguments, @command{hostname} prints the name of the current host
12710 system.  With one argument, it sets the current host name to the
12711 specified string.  You must have appropriate privileges to set the host
12712 name.  Synopsis:
12713
12714 @example
12715 hostname [@var{name}]
12716 @end example
12717
12718 The only options are @option{--help} and @option{--version}.  @xref{Common
12719 options}.
12720
12721 @exitstatus
12722
12723
12724 @node hostid invocation
12725 @section @command{hostid}: Print numeric host identifier.
12726
12727 @pindex hostid
12728 @cindex printing the host identifier
12729
12730 @command{hostid} prints the numeric identifier of the current host
12731 in hexadecimal.  This command accepts no arguments.
12732 The only options are @option{--help} and @option{--version}.
12733 @xref{Common options}.
12734
12735 For example, here's what it prints on one system I use:
12736
12737 @example
12738 $ hostid
12739 1bac013d
12740 @end example
12741
12742 On that system, the 32-bit quantity happens to be closely
12743 related to the system's Internet address, but that isn't always
12744 the case.
12745
12746 @exitstatus
12747
12748
12749 @node Modified command invocation
12750 @chapter Modified command invocation
12751
12752 @cindex modified command invocation
12753 @cindex invocation of commands, modified
12754 @cindex commands for invoking other commands
12755
12756 This section describes commands that run other commands in some context
12757 different than the current one: a modified environment, as a different
12758 user, etc.
12759
12760 @menu
12761 * chroot invocation::           Modify the root directory.
12762 * env invocation::              Modify environment variables.
12763 * nice invocation::             Modify niceness.
12764 * nohup invocation::            Immunize to hangups.
12765 * su invocation::               Modify user and group ID.
12766 @end menu
12767
12768
12769 @node chroot invocation
12770 @section @command{chroot}: Run a command with a different root directory
12771
12772 @pindex chroot
12773 @cindex running a program in a specified root directory
12774 @cindex root directory, running a program in a specified
12775
12776 @command{chroot} runs a command with a specified root directory.
12777 On many systems, only the super-user can do this.
12778 Synopses:
12779
12780 @example
12781 chroot @var{newroot} [@var{command} [@var{args}]@dots{}]
12782 chroot @var{option}
12783 @end example
12784
12785 Ordinarily, file names are looked up starting at the root of the
12786 directory structure, i.e., @file{/}.  @command{chroot} changes the root to
12787 the directory @var{newroot} (which must exist) and then runs
12788 @var{command} with optional @var{args}.  If @var{command} is not
12789 specified, the default is the value of the @env{SHELL} environment
12790 variable or @command{/bin/sh} if not set, invoked with the @option{-i} option.
12791 @var{command} must not be a special built-in utility
12792 (@pxref{Special built-in utilities}).
12793
12794 The only options are @option{--help} and @option{--version}.  @xref{Common
12795 options}.  Options must precede operands.
12796
12797 Here are a few tips to help avoid common problems in using chroot.
12798 To start with a simple example, make @var{command} refer to a statically
12799 linked binary.  If you were to use a dynamically linked executable, then
12800 you'd have to arrange to have the shared libraries in the right place under
12801 your new root directory.
12802
12803 For example, if you create a statically linked @command{ls} executable,
12804 and put it in @file{/tmp/empty}, you can run this command as root:
12805
12806 @example
12807 $ chroot /tmp/empty /ls -Rl /
12808 @end example
12809
12810 Then you'll see output like this:
12811
12812 @example
12813 /:
12814 total 1023
12815 -rwxr-xr-x 1 0 0 1041745 Aug 16 11:17 ls
12816 @end example
12817
12818 If you want to use a dynamically linked executable, say @command{bash},
12819 then first run @samp{ldd bash} to see what shared objects it needs.
12820 Then, in addition to copying the actual binary, also copy the listed
12821 files to the required positions under your intended new root directory.
12822 Finally, if the executable requires any other files (e.g., data, state,
12823 device files), copy them into place, too.
12824
12825 @cindex exit status of @command{chroot}
12826 Exit status:
12827
12828 @display
12829 1   if @command{chroot} itself fails
12830 126 if @var{command} is found but cannot be invoked
12831 127 if @var{command} cannot be found
12832 the exit status of @var{command} otherwise
12833 @end display
12834
12835
12836 @node env invocation
12837 @section @command{env}: Run a command in a modified environment
12838
12839 @pindex env
12840 @cindex environment, running a program in a modified
12841 @cindex modified environment, running a program in a
12842 @cindex running a program in a modified environment
12843
12844 @command{env} runs a command with a modified environment.  Synopses:
12845
12846 @example
12847 env [@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c
12848 [@var{command} [@var{args}]@dots{}]
12849 env
12850 @end example
12851
12852 Operands of the form @samp{@var{variable}=@var{value}} set
12853 the environment variable @var{variable} to value @var{value}.
12854 @var{value} may be empty (@samp{@var{variable}=}).  Setting a variable
12855 to an empty value is different from unsetting it.
12856 These operands are evaluated left-to-right, so if two operands
12857 mention the same variable the earlier is ignored.
12858
12859 Environment variable names can be empty, and can contain any
12860 characters other than @samp{=} and the null character (@acronym{ASCII}
12861 @sc{nul}).  However, it is wise to limit yourself to names that
12862 consist solely of underscores, digits, and @acronym{ASCII} letters,
12863 and that begin with a non-digit, as applications like the shell do not
12864 work well with other names.
12865
12866 @vindex PATH
12867 The first operand that does not contain the character @samp{=}
12868 specifies the program to invoke; it is
12869 searched for according to the @env{PATH} environment variable.  Any
12870 remaining arguments are passed as arguments to that program.
12871 The program should not be a special built-in utility
12872 (@pxref{Special built-in utilities}).
12873
12874 @cindex environment, printing
12875
12876 If no command name is specified following the environment
12877 specifications, the resulting environment is printed.  This is like
12878 specifying the @command{printenv} program.
12879
12880 The program accepts the following options.  Also see @ref{Common options}.
12881 Options must precede operands.
12882
12883 @table @samp
12884
12885 @item -u @var{name}
12886 @itemx --unset=@var{name}
12887 @opindex -u
12888 @opindex --unset
12889 Remove variable @var{name} from the environment, if it was in the
12890 environment.
12891
12892 @item -
12893 @itemx -i
12894 @itemx --ignore-environment
12895 @opindex -
12896 @opindex -i
12897 @opindex --ignore-environment
12898 Start with an empty environment, ignoring the inherited environment.
12899
12900 @end table
12901
12902 @cindex exit status of @command{env}
12903 Exit status:
12904
12905 @display
12906 0   if no @var{command} is specified and the environment is output
12907 1   if @command{env} itself fails
12908 126 if @var{command} is found but cannot be invoked
12909 127 if @var{command} cannot be found
12910 the exit status of @var{command} otherwise
12911 @end display
12912
12913
12914 @node nice invocation
12915 @section @command{nice}: Run a command with modified niceness
12916
12917 @pindex nice
12918 @cindex niceness
12919 @cindex scheduling, affecting
12920 @cindex appropriate privileges
12921
12922 @command{nice} prints or modifies a process's @dfn{niceness},
12923 a parameter that affects whether the process is scheduled favorably.
12924 Synopsis:
12925
12926 @example
12927 nice [@var{option}]@dots{} [@var{command} [@var{arg}]@dots{}]
12928 @end example
12929
12930 If no arguments are given, @command{nice} prints the current niceness.
12931 Otherwise, @command{nice} runs the given @var{command} with its
12932 niceness adjusted.  By default, its niceness is incremented by 10.
12933
12934 Nicenesses range at least from @minus{}20 (resulting in the most
12935 favorable scheduling) through 19 (the least favorable).  Some systems
12936 may have a wider range of nicenesses; conversely, other systems may
12937 enforce more restrictive limits.  An attempt to set the niceness
12938 outside the supported range is treated as an attempt to use the
12939 minimum or maximum supported value.
12940
12941 A niceness should not be confused with a scheduling priority, which
12942 lets applications determine the order in which threads are scheduled
12943 to run.  Unlike a priority, a niceness is merely advice to the
12944 scheduler, which the scheduler is free to ignore.  Also, as a point of
12945 terminology, @acronym{POSIX} defines the behavior of @command{nice} in
12946 terms of a @dfn{nice value}, which is the nonnegative difference
12947 between a niceness and the minimum niceness.  Though @command{nice}
12948 conforms to @acronym{POSIX}, its documentation and diagnostics use the
12949 term ``niceness'' for compatibility with historical practice.
12950
12951 @var{command} must not be a special built-in utility (@pxref{Special
12952 built-in utilities}).
12953
12954 @cindex conflicts with shell built-ins
12955 @cindex built-in shell commands, conflicts with
12956 Because many shells have a built-in @command{nice} command, using an
12957 unadorned @command{nice} in a script or interactively may get you
12958 different functionality than that described here.
12959
12960 The program accepts the following option.  Also see @ref{Common options}.
12961 Options must precede operands.
12962
12963 @table @samp
12964 @item -n @var{adjustment}
12965 @itemx --adjustment=@var{adjustment}
12966 @opindex -n
12967 @opindex --adjustment
12968 Add @var{adjustment} instead of 10 to the command's niceness.  If
12969 @var{adjustment} is negative and you lack appropriate privileges,
12970 @command{nice} issues a warning but otherwise acts as if you specified
12971 a zero adjustment.
12972
12973 For compatibility @command{nice} also supports an obsolete
12974 option syntax @option{-@var{adjustment}}.  New scripts should use
12975 @option{-n @var{adjustment}} instead.
12976
12977 @end table
12978
12979 @cindex exit status of @command{nice}
12980 Exit status:
12981
12982 @display
12983 0   if no @var{command} is specified and the niceness is output
12984 1   if @command{nice} itself fails
12985 126 if @var{command} is found but cannot be invoked
12986 127 if @var{command} cannot be found
12987 the exit status of @var{command} otherwise
12988 @end display
12989
12990 It is sometimes useful to run a non-interactive program with reduced niceness.
12991
12992 @example
12993 $ nice factor 4611686018427387903
12994 @end example
12995
12996 Since @command{nice} prints the current niceness,
12997 you can invoke it through itself to demonstrate how it works.
12998
12999 The default behavior is to increase the niceness by @samp{10}:
13000
13001 @example
13002 $ nice
13003 0
13004 $ nice nice
13005 10
13006 $ nice -n 10 nice
13007 10
13008 @end example
13009
13010 The @var{adjustment} is relative to the current niceness.  In the
13011 next example, the first @command{nice} invocation runs the second one
13012 with niceness 10, and it in turn runs the final one with a niceness
13013 that is 3 more:
13014
13015 @example
13016 $ nice nice -n 3 nice
13017 13
13018 @end example
13019
13020 Specifying a niceness larger than the supported range
13021 is the same as specifying the maximum supported value:
13022
13023 @example
13024 $ nice -n 10000000000 nice
13025 19
13026 @end example
13027
13028 Only a privileged user may run a process with lower niceness:
13029
13030 @example
13031 $ nice -n -1 nice
13032 nice: cannot set niceness: Permission denied
13033 0
13034 $ sudo nice -n -1 nice
13035 -1
13036 @end example
13037
13038
13039 @node nohup invocation
13040 @section @command{nohup}: Run a command immune to hangups
13041
13042 @pindex nohup
13043 @cindex hangups, immunity to
13044 @cindex immunity to hangups
13045 @cindex logging out and continuing to run
13046
13047 @flindex nohup.out
13048 @command{nohup} runs the given @var{command} with hangup signals ignored,
13049 so that the command can continue running in the background after you log
13050 out.  Synopsis:
13051
13052 @example
13053 nohup @var{command} [@var{arg}]@dots{}
13054 @end example
13055
13056 If standard input is a terminal, it is redirected from
13057 @file{/dev/null} so that terminal sessions do not mistakenly consider
13058 the terminal to be used by the command.  This is a @acronym{GNU}
13059 extension; programs intended to be portable to non-@acronym{GNU} hosts
13060 should use @samp{nohup @var{command} [@var{arg}]@dots{} </dev/null}
13061 instead.
13062
13063 @flindex nohup.out
13064 If standard output is a terminal, the command's standard output is appended
13065 to the file @file{nohup.out}; if that cannot be written to, it is appended
13066 to the file @file{$HOME/nohup.out}; and if that cannot be written to, the
13067 command is not run.
13068 Any @file{nohup.out} or @file{$HOME/nohup.out} file created by
13069 @command{nohup} is made readable and writable only to the user,
13070 regardless of the current umask settings.
13071
13072 If standard error is a terminal, it is normally redirected to the same file
13073 descriptor as the (possibly-redirected) standard output.
13074 However, if standard output is closed, standard error terminal output
13075 is instead appended to the file @file{nohup.out} or
13076 @file{$HOME/nohup.out} as above.
13077
13078 @command{nohup} does not automatically put the command it runs in the
13079 background; you must do that explicitly, by ending the command line
13080 with an @samp{&}.  Also, @command{nohup} does not alter the
13081 niceness of @var{command}; use @command{nice} for that,
13082 e.g., @samp{nohup nice @var{command}}.
13083
13084 @var{command} must not be a special built-in utility (@pxref{Special
13085 built-in utilities}).
13086
13087 The only options are @option{--help} and @option{--version}.  @xref{Common
13088 options}.  Options must precede operands.
13089
13090 @cindex exit status of @command{nohup}
13091 Exit status:
13092
13093 @display
13094 126 if @var{command} is found but cannot be invoked
13095 127 if @command{nohup} itself fails or if @var{command} cannot be found
13096 the exit status of @var{command} otherwise
13097 @end display
13098
13099
13100 @node su invocation
13101 @section @command{su}: Run a command with substitute user and group ID
13102
13103 @pindex su
13104 @cindex substitute user and group IDs
13105 @cindex user ID, switching
13106 @cindex super-user, becoming
13107 @cindex root, becoming
13108
13109 @command{su} allows one user to temporarily become another user.  It runs a
13110 command (often an interactive shell) with the real and effective user
13111 ID, group ID, and supplemental groups of a given @var{user}.  Synopsis:
13112
13113 @example
13114 su [@var{option}]@dots{} [@var{user} [@var{arg}]@dots{}]
13115 @end example
13116
13117 @cindex passwd entry, and @command{su} shell
13118 @flindex /bin/sh
13119 @flindex /etc/passwd
13120 If no @var{user} is given, the default is @code{root}, the super-user.
13121 The shell to use is taken from @var{user}'s @code{passwd} entry, or
13122 @file{/bin/sh} if none is specified there.  If @var{user} has a
13123 password, @command{su} prompts for the password unless run by a user with
13124 effective user ID of zero (the super-user).
13125
13126 @vindex HOME
13127 @vindex SHELL
13128 @vindex USER
13129 @vindex LOGNAME
13130 @cindex login shell
13131 By default, @command{su} does not change the current directory.
13132 It sets the environment variables @env{HOME} and @env{SHELL}
13133 from the password entry for @var{user}, and if @var{user} is not
13134 the super-user, sets @env{USER} and @env{LOGNAME} to @var{user}.
13135 By default, the shell is not a login shell.
13136
13137 Any additional @var{arg}s are passed as additional arguments to the
13138 shell.
13139
13140 @cindex @option{-su}
13141 GNU @command{su} does not treat @file{/bin/sh} or any other shells specially
13142 (e.g., by setting @code{argv[0]} to @option{-su}, passing @option{-c} only
13143 to certain shells, etc.).
13144
13145 @findex syslog
13146 @command{su} can optionally be compiled to use @code{syslog} to report
13147 failed, and optionally successful, @command{su} attempts.  (If the system
13148 supports @code{syslog}.)  However, GNU @command{su} does not check if the
13149 user is a member of the @code{wheel} group; see below.
13150
13151 The program accepts the following options.  Also see @ref{Common options}.
13152
13153 @table @samp
13154 @item -c @var{command}
13155 @itemx --command=@var{command}
13156 @opindex -c
13157 @opindex --command
13158 Pass @var{command}, a single command line to run, to the shell with
13159 a @option{-c} option instead of starting an interactive shell.
13160
13161 @item -f
13162 @itemx --fast
13163 @opindex -f
13164 @opindex --fast
13165 @flindex .cshrc
13166 @cindex file name pattern expansion, disabled
13167 @cindex globbing, disabled
13168 Pass the @option{-f} option to the shell.  This probably only makes sense
13169 if the shell run is @command{csh} or @command{tcsh}, for which the @option{-f}
13170 option prevents reading the startup file (@file{.cshrc}).  With
13171 Bourne-like shells, the @option{-f} option disables file name pattern
13172 expansion (globbing), which is not likely to be useful.
13173
13174 @item -
13175 @itemx -l
13176 @itemx --login
13177 @opindex -
13178 @opindex -l
13179 @opindex --login
13180 @c other variables already indexed above
13181 @vindex TERM
13182 @vindex PATH
13183 @cindex login shell, creating
13184 Make the shell a login shell.  This means the following.  Unset all
13185 environment variables except @env{TERM}, @env{HOME}, and @env{SHELL}
13186 (which are set as described above), and @env{USER} and @env{LOGNAME}
13187 (which are set, even for the super-user, as described above), and set
13188 @env{PATH} to a compiled-in default value.  Change to @var{user}'s home
13189 directory.  Prepend @samp{-} to the shell's name, intended to make it
13190 read its login startup file(s).
13191
13192 @item -m
13193 @itemx -p
13194 @itemx --preserve-environment
13195 @opindex -m
13196 @opindex -p
13197 @opindex --preserve-environment
13198 @cindex environment, preserving
13199 @flindex /etc/shells
13200 @cindex restricted shell
13201 Do not change the environment variables @env{HOME}, @env{USER},
13202 @env{LOGNAME}, or @env{SHELL}.  Run the shell given in the environment
13203 variable @env{SHELL} instead of the shell from @var{user}'s passwd
13204 entry, unless the user running @command{su} is not the super-user and
13205 @var{user}'s shell is restricted.  A @dfn{restricted shell} is one that
13206 is not listed in the file @file{/etc/shells}, or in a compiled-in list
13207 if that file does not exist.  Parts of what this option does can be
13208 overridden by @option{--login} and @option{--shell}.
13209
13210 @item -s @var{shell}
13211 @itemx --shell=@var{shell}
13212 @opindex -s
13213 @opindex --shell
13214 Run @var{shell} instead of the shell from @var{user}'s passwd entry,
13215 unless the user running @command{su} is not the super-user and @var{user}'s
13216 shell is restricted (see @option{-m} just above).
13217
13218 @end table
13219
13220 @cindex exit status of @command{su}
13221 Exit status:
13222
13223 @display
13224 1   if @command{su} itself fails
13225 126 if subshell is found but cannot be invoked
13226 127 if subshell cannot be found
13227 the exit status of the subshell otherwise
13228 @end display
13229
13230 @cindex wheel group, not supported
13231 @cindex group wheel, not supported
13232 @cindex fascism
13233 @subsection Why GNU @command{su} does not support the @samp{wheel} group
13234
13235 (This section is by Richard Stallman.)
13236
13237 @cindex Twenex
13238 @cindex MIT AI lab
13239 Sometimes a few of the users try to hold total power over all the
13240 rest.  For example, in 1984, a few users at the MIT AI lab decided to
13241 seize power by changing the operator password on the Twenex system and
13242 keeping it secret from everyone else.  (I was able to thwart this coup
13243 and give power back to the users by patching the kernel, but I
13244 wouldn't know how to do that in Unix.)
13245
13246 However, occasionally the rulers do tell someone.  Under the usual
13247 @command{su} mechanism, once someone learns the root password who
13248 sympathizes with the ordinary users, he or she can tell the rest.  The
13249 ``wheel group'' feature would make this impossible, and thus cement the
13250 power of the rulers.
13251
13252 I'm on the side of the masses, not that of the rulers.  If you are
13253 used to supporting the bosses and sysadmins in whatever they do, you
13254 might find this idea strange at first.
13255
13256
13257 @node Process control
13258 @chapter Process control
13259
13260 @cindex processes, commands for controlling
13261 @cindex commands for controlling processes
13262
13263 @menu
13264 * kill invocation::             Sending a signal to processes.
13265 @end menu
13266
13267
13268 @node kill invocation
13269 @section @command{kill}: Send a signal to processes
13270
13271 @pindex kill
13272 @cindex send a signal to processes
13273
13274 The @command{kill} command sends a signal to processes, causing them
13275 to terminate or otherwise act upon receiving the signal in some way.
13276 Alternatively, it lists information about signals.  Synopses:
13277
13278 @example
13279 kill [-s @var{signal} | --signal @var{signal} | -@var{signal}] @var{pid}@dots{}
13280 kill [-l | --list | -t | --table] [@var{signal}]@dots{}
13281 @end example
13282
13283 The first form of the @command{kill} command sends a signal to all
13284 @var{pid} arguments.  The default signal to send if none is specified
13285 is @samp{TERM}.  The special signal number @samp{0} does not denote a
13286 valid signal, but can be used to test whether the @var{pid} arguments
13287 specify processes to which a signal could be sent.
13288
13289 If @var{pid} is positive, the signal is sent to the process with the
13290 process ID @var{pid}.  If @var{pid} is zero, the signal is sent to all
13291 processes in the process group of the current process.  If @var{pid}
13292 is @minus{}1, the signal is sent to all processes for which the user has
13293 permission to send a signal.  If @var{pid} is less than @minus{}1, the signal
13294 is sent to all processes in the process group that equals the absolute
13295 value of @var{pid}.
13296
13297 If @var{pid} is not positive, a system-dependent set of system
13298 processes is excluded from the list of processes to which the signal
13299 is sent.
13300
13301 If a negative @var{PID} argument is desired as the first one, it
13302 should be preceded by @option{--}.  However, as a common extension to
13303 @acronym{POSIX}, @option{--} is not required with @samp{kill
13304 -@var{signal} -@var{pid}}.  The following commands are equivalent:
13305
13306 @example
13307 kill -15 -1
13308 kill -TERM -1
13309 kill -s TERM -- -1
13310 kill -- -1
13311 @end example
13312
13313 The first form of the @command{kill} command succeeds if every @var{pid}
13314 argument specifies at least one process that the signal was sent to.
13315
13316 The second form of the @command{kill} command lists signal information.
13317 Either the @option{-l} or @option{--list} option, or the @option{-t}
13318 or @option{--table} option must be specified.  Without any
13319 @var{signal} argument, all supported signals are listed.  The output
13320 of @option{-l} or @option{--list} is a list of the signal names, one
13321 per line; if @var{signal} is already a name, the signal number is
13322 printed instead.  The output of @option{-t} or @option{--table} is a
13323 table of signal numbers, names, and descriptions.  This form of the
13324 @command{kill} command succeeds if all @var{signal} arguments are valid
13325 and if there is no output error.
13326
13327 The @command{kill} command also supports the @option{--help} and
13328 @option{--version} options.  @xref{Common options}.
13329
13330 A @var{signal} may be a signal name like @samp{HUP}, or a signal
13331 number like @samp{1}, or an exit status of a process terminated by the
13332 signal.  A signal name can be given in canonical form or prefixed by
13333 @samp{SIG}.  The case of the letters is ignored, except for the
13334 @option{-@var{signal}} option which must use upper case to avoid
13335 ambiguity with lower case option letters.  The following signal names
13336 and numbers are supported on all @acronym{POSIX} compliant systems:
13337
13338 @table @samp
13339 @item HUP
13340 1.  Hangup.
13341 @item INT
13342 2.  Terminal interrupt.
13343 @item QUIT
13344 3.  Terminal quit.
13345 @item ABRT
13346 6.  Process abort.
13347 @item KILL
13348 9.  Kill (cannot be caught or ignored).
13349 @item ALRM
13350 14.  Alarm Clock.
13351 @item TERM
13352 15.  Termination.
13353 @end table
13354
13355 @noindent
13356 Other supported signal names have system-dependent corresponding
13357 numbers.  All systems conforming to @acronym{POSIX} 1003.1-2001 also
13358 support the following signals:
13359
13360 @table @samp
13361 @item BUS
13362 Access to an undefined portion of a memory object.
13363 @item CHLD
13364 Child process terminated, stopped, or continued.
13365 @item CONT
13366 Continue executing, if stopped.
13367 @item FPE
13368 Erroneous arithmetic operation.
13369 @item ILL
13370 Illegal Instruction.
13371 @item PIPE
13372 Write on a pipe with no one to read it.
13373 @item SEGV
13374 Invalid memory reference.
13375 @item STOP
13376 Stop executing (cannot be caught or ignored).
13377 @item TSTP
13378 Terminal stop.
13379 @item TTIN
13380 Background process attempting read.
13381 @item TTOU
13382 Background process attempting write.
13383 @item URG
13384 High bandwidth data is available at a socket.
13385 @item USR1
13386 User-defined signal 1.
13387 @item USR2
13388 User-defined signal 2.
13389 @end table
13390
13391 @noindent
13392 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XSI} extension
13393 also support the following signals:
13394
13395 @table @samp
13396 @item POLL
13397 Pollable event.
13398 @item PROF
13399 Profiling timer expired.
13400 @item SYS
13401 Bad system call.
13402 @item TRAP
13403 Trace/breakpoint trap.
13404 @item VTALRM
13405 Virtual timer expired.
13406 @item XCPU
13407 CPU time limit exceeded.
13408 @item XFSZ
13409 File size limit exceeded.
13410 @end table
13411
13412 @noindent
13413 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XRT} extension
13414 also support at least eight real-time signals called @samp{RTMIN},
13415 @samp{RTMIN+1}, @dots{}, @samp{RTMAX-1}, @samp{RTMAX}.
13416
13417
13418 @node Delaying
13419 @chapter Delaying
13420
13421 @cindex delaying commands
13422 @cindex commands for delaying
13423
13424 @c Perhaps @command{wait} or other commands should be described here also?
13425
13426 @menu
13427 * sleep invocation::            Delay for a specified time.
13428 @end menu
13429
13430
13431 @node sleep invocation
13432 @section @command{sleep}: Delay for a specified time
13433
13434 @pindex sleep
13435 @cindex delay for a specified time
13436
13437 @command{sleep} pauses for an amount of time specified by the sum of
13438 the values of the command line arguments.
13439 Synopsis:
13440
13441 @example
13442 sleep @var{number}[smhd]@dots{}
13443 @end example
13444
13445 @cindex time units
13446 Each argument is a number followed by an optional unit; the default
13447 is seconds.  The units are:
13448
13449 @table @samp
13450 @item s
13451 seconds
13452 @item m
13453 minutes
13454 @item h
13455 hours
13456 @item d
13457 days
13458 @end table
13459
13460 Historical implementations of @command{sleep} have required that
13461 @var{number} be an integer, and only accepted a single argument
13462 without a suffix.  However, GNU @command{sleep} accepts
13463 arbitrary floating point numbers (using a period before any fractional
13464 digits).
13465
13466 The only options are @option{--help} and @option{--version}.  @xref{Common
13467 options}.
13468
13469 @exitstatus
13470
13471
13472 @node Numeric operations
13473 @chapter Numeric operations
13474
13475 @cindex numeric operations
13476 These programs do numerically-related operations.
13477
13478 @menu
13479 * factor invocation::              Show factors of numbers.
13480 * seq invocation::                 Print sequences of numbers.
13481 @end menu
13482
13483
13484 @node factor invocation
13485 @section @command{factor}: Print prime factors
13486
13487 @pindex factor
13488 @cindex prime factors
13489
13490 @command{factor} prints prime factors.  Synopses:
13491
13492 @example
13493 factor [@var{number}]@dots{}
13494 factor @var{option}
13495 @end example
13496
13497 If no @var{number} is specified on the command line, @command{factor} reads
13498 numbers from standard input, delimited by newlines, tabs, or spaces.
13499
13500 The only options are @option{--help} and @option{--version}.  @xref{Common
13501 options}.
13502
13503 The algorithm it uses is not very sophisticated, so for some inputs
13504 @command{factor} runs for a long time.  The hardest numbers to factor are
13505 the products of large primes.  Factoring the product of the two largest 32-bit
13506 prime numbers takes about 80 seconds of CPU time on a 1.6 GHz Athlon.
13507
13508 @example
13509 $ p=`echo '4294967279 * 4294967291'|bc`
13510 $ factor $p
13511 18446743979220271189: 4294967279 4294967291
13512 @end example
13513
13514 Similarly, it takes about 80 seconds for GNU factor (from coreutils-5.1.2)
13515 to ``factor'' the largest 64-bit prime:
13516
13517 @example
13518 $ factor 18446744073709551557
13519   18446744073709551557: 18446744073709551557
13520 @end example
13521
13522 In contrast, @command{factor} factors the largest 64-bit number in just
13523 over a tenth of a second:
13524
13525 @example
13526 $ factor `echo '2^64-1'|bc`
13527 18446744073709551615: 3 5 17 257 641 65537 6700417
13528 @end example
13529
13530 @exitstatus
13531
13532
13533 @node seq invocation
13534 @section @command{seq}: Print numeric sequences
13535
13536 @pindex seq
13537 @cindex numeric sequences
13538 @cindex sequence of numbers
13539
13540 @command{seq} prints a sequence of numbers to standard output.  Synopses:
13541
13542 @example
13543 seq [@var{option}]@dots{} @var{last}
13544 seq [@var{option}]@dots{} @var{first} @var{last}
13545 seq [@var{option}]@dots{} @var{first} @var{increment} @var{last}
13546 @end example
13547
13548 @command{seq} prints the numbers from @var{first} to @var{last} by
13549 @var{increment}.  By default, each number is printed on a separate line.
13550 When @var{increment} is not specified, it defaults to @samp{1},
13551 even when @var{first} is larger than @var{last}.
13552 @var{first} also defaults to @samp{1}.  So @code{seq 1} prints
13553 @samp{1}, but @code{seq 0} and @code{seq 10 5} produce no output.
13554 Floating-point numbers
13555 may be specified (using a period before any fractional digits).
13556
13557 The program accepts the following options.  Also see @ref{Common options}.
13558 Options must precede operands.
13559
13560 @table @samp
13561 @item -f @var{format}
13562 @itemx --format=@var{format}
13563 @opindex -f @var{format}
13564 @opindex --format=@var{format}
13565 @cindex formatting of numbers in @command{seq}
13566 Print all numbers using @var{format}.
13567 @var{format} must contain exactly one of the @samp{printf}-style
13568 floating point conversion specifications @samp{%a}, @samp{%e},
13569 @samp{%f}, @samp{%g}, @samp{%A}, @samp{%E}, @samp{%F}, @samp{%G}.
13570
13571 The default format is derived from @var{first}, @var{step}, and
13572 @var{last}.  If these all use a fixed point decimal representation,
13573 the default format is @samp{%.@var{p}f}, where @var{p} is the minimum
13574 precision that can represent the output numbers exactly.  Otherwise,
13575 the default format is @samp{%g}.
13576
13577 @item -s @var{string}
13578 @itemx --separator=@var{string}
13579 @cindex separator for numbers in @command{seq}
13580 Separate numbers with @var{string}; default is a newline.
13581 The output always terminates with a newline.
13582
13583 @item -w
13584 @itemx --equal-width
13585 Print all numbers with the same width, by padding with leading zeros.
13586 @var{first}, @var{step}, and @var{last} should all use a fixed point
13587 decimal representation.
13588 (To have other kinds of padding, use @option{--format}).
13589
13590 @end table
13591
13592 If you want hexadecimal integer output, you can use @command{printf}
13593 to perform the conversion:
13594
13595 @example
13596 $ printf '%x\n' `seq 1048575 1024 1050623`
13597 fffff
13598 1003ff
13599 1007ff
13600 @end example
13601
13602 For very long lists of numbers, use xargs to avoid
13603 system limitations on the length of an argument list:
13604
13605 @example
13606 $ seq 1000000 | xargs printf '%x\n' | tail -n 3
13607 f423e
13608 f423f
13609 f4240
13610 @end example
13611
13612 To generate octal output, use the printf @code{%o} format instead
13613 of @code{%x}.
13614
13615 On most systems, seq can produce whole-number output for values up to
13616 at least @code{2^53}.  Larger integers are approximated.  The details
13617 differ depending on your floating-point implementation, but a common
13618 case is that @command{seq} works with integers through @code{2^64},
13619 and larger integers may not be numerically correct:
13620
13621 @example
13622 $ seq 18446744073709551616 1 18446744073709551618
13623 18446744073709551616
13624 18446744073709551616
13625 18446744073709551618
13626 @end example
13627
13628 Be careful when using @command{seq} with a fractional @var{increment};
13629 otherwise you may see surprising results.  Most people would expect to
13630 see @code{0.000003} printed as the last number in this example:
13631
13632 @example
13633 $ seq -s ' ' 0 0.000001 0.000003
13634 0.000000 0.000001 0.000002
13635 @end example
13636
13637 But that doesn't happen on many systems because @command{seq} is
13638 implemented using binary floating point arithmetic (via the C
13639 @code{long double} type)---which means decimal fractions like @code{0.000001}
13640 cannot be represented exactly.  That in turn means some nonintuitive
13641 conditions like @w{@code{0.000001 * 3 > 0.000003}} will end up being true.
13642
13643 To work around that in the above example, use a slightly larger number as
13644 the @var{last} value:
13645
13646 @example
13647 $ seq -s ' ' 0 0.000001 0.0000031
13648 0.000000 0.000001 0.000002 0.000003
13649 @end example
13650
13651 In general, when using an @var{increment} with a fractional part, where
13652 (@var{last} - @var{first}) / @var{increment} is (mathematically) a whole
13653 number, specify a slightly larger (or smaller, if @var{increment} is negative)
13654 value for @var{last} to ensure that @var{last} is the final value printed
13655 by seq.
13656
13657 @exitstatus
13658
13659
13660 @node File permissions
13661 @chapter File permissions
13662 @include perm.texi
13663
13664 @include getdate.texi
13665
13666 @c              What's GNU?
13667 @c              Arnold Robbins
13668 @node Opening the software toolbox
13669 @chapter Opening the Software Toolbox
13670
13671 An earlier version of this chapter appeared in
13672 @uref{http://www.linuxjournal.com/article.php?sid=2762, the
13673 @cite{What's GNU?} column of @cite{Linux Journal}, 2 (June, 1994)}.
13674 It was written by Arnold Robbins.
13675
13676 @menu
13677 * Toolbox introduction::        Toolbox introduction
13678 * I/O redirection::             I/O redirection
13679 * The who command::             The @command{who} command
13680 * The cut command::             The @command{cut} command
13681 * The sort command::            The @command{sort} command
13682 * The uniq command::            The @command{uniq} command
13683 * Putting the tools together::  Putting the tools together
13684 @end menu
13685
13686
13687 @node Toolbox introduction
13688 @unnumberedsec Toolbox Introduction
13689
13690 This month's column is only peripherally related to the GNU Project, in
13691 that it describes a number of the GNU tools on your GNU/Linux system and how they
13692 might be used.  What it's really about is the ``Software Tools'' philosophy
13693 of program development and usage.
13694
13695 The software tools philosophy was an important and integral concept
13696 in the initial design and development of Unix (of which Linux and GNU are
13697 essentially clones).  Unfortunately, in the modern day press of
13698 Internetworking and flashy GUIs, it seems to have fallen by the
13699 wayside.  This is a shame, since it provides a powerful mental model
13700 for solving many kinds of problems.
13701
13702 Many people carry a Swiss Army knife around in their pants pockets (or
13703 purse).  A Swiss Army knife is a handy tool to have: it has several knife
13704 blades, a screwdriver, tweezers, toothpick, nail file, corkscrew, and perhaps
13705 a number of other things on it.  For the everyday, small miscellaneous jobs
13706 where you need a simple, general purpose tool, it's just the thing.
13707
13708 On the other hand, an experienced carpenter doesn't build a house using
13709 a Swiss Army knife.  Instead, he has a toolbox chock full of specialized
13710 tools---a saw, a hammer, a screwdriver, a plane, and so on.  And he knows
13711 exactly when and where to use each tool; you won't catch him hammering nails
13712 with the handle of his screwdriver.
13713
13714 The Unix developers at Bell Labs were all professional programmers and trained
13715 computer scientists.  They had found that while a one-size-fits-all program
13716 might appeal to a user because there's only one program to use, in practice
13717 such programs are
13718
13719 @enumerate a
13720 @item
13721 difficult to write,
13722
13723 @item
13724 difficult to maintain and
13725 debug, and
13726
13727 @item
13728 difficult to extend to meet new situations.
13729 @end enumerate
13730
13731 Instead, they felt that programs should be specialized tools.  In short, each
13732 program ``should do one thing well.''  No more and no less.  Such programs are
13733 simpler to design, write, and get right---they only do one thing.
13734
13735 Furthermore, they found that with the right machinery for hooking programs
13736 together, that the whole was greater than the sum of the parts.  By combining
13737 several special purpose programs, you could accomplish a specific task
13738 that none of the programs was designed for, and accomplish it much more
13739 quickly and easily than if you had to write a special purpose program.
13740 We will see some (classic) examples of this further on in the column.
13741 (An important additional point was that, if necessary, take a detour
13742 and build any software tools you may need first, if you don't already
13743 have something appropriate in the toolbox.)
13744
13745 @node I/O redirection
13746 @unnumberedsec I/O Redirection
13747
13748 Hopefully, you are familiar with the basics of I/O redirection in the
13749 shell, in particular the concepts of ``standard input,'' ``standard output,''
13750 and ``standard error''.  Briefly, ``standard input'' is a data source, where
13751 data comes from.  A program should not need to either know or care if the
13752 data source is a disk file, a keyboard, a magnetic tape, or even a punched
13753 card reader.  Similarly, ``standard output'' is a data sink, where data goes
13754 to.  The program should neither know nor care where this might be.
13755 Programs that only read their standard input, do something to the data,
13756 and then send it on, are called @dfn{filters}, by analogy to filters in a
13757 water pipeline.
13758
13759 With the Unix shell, it's very easy to set up data pipelines:
13760
13761 @smallexample
13762 program_to_create_data | filter1 | ... | filterN > final.pretty.data
13763 @end smallexample
13764
13765 We start out by creating the raw data; each filter applies some successive
13766 transformation to the data, until by the time it comes out of the pipeline,
13767 it is in the desired form.
13768
13769 This is fine and good for standard input and standard output.  Where does the
13770 standard error come in to play?  Well, think about @command{filter1} in
13771 the pipeline above.  What happens if it encounters an error in the data it
13772 sees?  If it writes an error message to standard output, it will just
13773 disappear down the pipeline into @command{filter2}'s input, and the
13774 user will probably never see it.  So programs need a place where they can send
13775 error messages so that the user will notice them.  This is standard error,
13776 and it is usually connected to your console or window, even if you have
13777 redirected standard output of your program away from your screen.
13778
13779 For filter programs to work together, the format of the data has to be
13780 agreed upon.  The most straightforward and easiest format to use is simply
13781 lines of text.  Unix data files are generally just streams of bytes, with
13782 lines delimited by the @acronym{ASCII} @sc{lf} (Line Feed) character,
13783 conventionally called a ``newline'' in the Unix literature.  (This is
13784 @code{'\n'} if you're a C programmer.)  This is the format used by all
13785 the traditional filtering programs.  (Many earlier operating systems
13786 had elaborate facilities and special purpose programs for managing
13787 binary data.  Unix has always shied away from such things, under the
13788 philosophy that it's easiest to simply be able to view and edit your
13789 data with a text editor.)
13790
13791 OK, enough introduction.  Let's take a look at some of the tools, and then
13792 we'll see how to hook them together in interesting ways.   In the following
13793 discussion, we will only present those command line options that interest
13794 us.  As you should always do, double check your system documentation
13795 for the full story.
13796
13797 @node The who command
13798 @unnumberedsec The @command{who} Command
13799
13800 The first program is the @command{who} command.  By itself, it generates a
13801 list of the users who are currently logged in.  Although I'm writing
13802 this on a single-user system, we'll pretend that several people are
13803 logged in:
13804
13805 @example
13806 $ who
13807 @print{} arnold   console Jan 22 19:57
13808 @print{} miriam   ttyp0   Jan 23 14:19(:0.0)
13809 @print{} bill     ttyp1   Jan 21 09:32(:0.0)
13810 @print{} arnold   ttyp2   Jan 23 20:48(:0.0)
13811 @end example
13812
13813 Here, the @samp{$} is the usual shell prompt, at which I typed @samp{who}.
13814 There are three people logged in, and I am logged in twice.  On traditional
13815 Unix systems, user names are never more than eight characters long.  This
13816 little bit of trivia will be useful later.  The output of @command{who} is nice,
13817 but the data is not all that exciting.
13818
13819 @node The cut command
13820 @unnumberedsec The @command{cut} Command
13821
13822 The next program we'll look at is the @command{cut} command.  This program
13823 cuts out columns or fields of input data.  For example, we can tell it
13824 to print just the login name and full name from the @file{/etc/passwd}
13825 file.  The @file{/etc/passwd} file has seven fields, separated by
13826 colons:
13827
13828 @example
13829 arnold:xyzzy:2076:10:Arnold D. Robbins:/home/arnold:/bin/bash
13830 @end example
13831
13832 To get the first and fifth fields, we would use @command{cut} like this:
13833
13834 @example
13835 $ cut -d: -f1,5 /etc/passwd
13836 @print{} root:Operator
13837 @dots{}
13838 @print{} arnold:Arnold D. Robbins
13839 @print{} miriam:Miriam A. Robbins
13840 @dots{}
13841 @end example
13842
13843 With the @option{-c} option, @command{cut} will cut out specific characters
13844 (i.e., columns) in the input lines.  This is useful for input data
13845 that has fixed width fields, and does not have a field separator.  For
13846 example, list the Monday dates for the current month:
13847
13848 @c Is using cal ok?  Looked at gcal, but I don't like it.
13849 @example
13850 $ cal | cut -c 3-5
13851 @print{}Mo
13852 @print{}
13853 @print{}  6
13854 @print{} 13
13855 @print{} 20
13856 @print{} 27
13857 @end example
13858
13859 @node The sort command
13860 @unnumberedsec The @command{sort} Command
13861
13862 Next we'll look at the @command{sort} command.  This is one of the most
13863 powerful commands on a Unix-style system; one that you will often find
13864 yourself using when setting up fancy data plumbing.
13865
13866 The @command{sort}
13867 command reads and sorts each file named on the command line.  It then
13868 merges the sorted data and writes it to standard output.  It will read
13869 standard input if no files are given on the command line (thus
13870 making it into a filter).  The sort is based on the character collating
13871 sequence or based on user-supplied ordering criteria.
13872
13873
13874 @node The uniq command
13875 @unnumberedsec The @command{uniq} Command
13876
13877 Finally (at least for now), we'll look at the @command{uniq} program.  When
13878 sorting data, you will often end up with duplicate lines, lines that
13879 are identical.  Usually, all you need is one instance of each line.
13880 This is where @command{uniq} comes in.  The @command{uniq} program reads its
13881 standard input.  It prints only one
13882 copy of each repeated line.  It does have several options.  Later on,
13883 we'll use the @option{-c} option, which prints each unique line, preceded
13884 by a count of the number of times that line occurred in the input.
13885
13886
13887 @node Putting the tools together
13888 @unnumberedsec Putting the Tools Together
13889
13890 Now, let's suppose this is a large ISP server system with dozens of users
13891 logged in.  The management wants the system administrator to write a program that will
13892 generate a sorted list of logged in users.  Furthermore, even if a user
13893 is logged in multiple times, his or her name should only show up in the
13894 output once.
13895
13896 The administrator could sit down with the system documentation and write a C
13897 program that did this.  It would take perhaps a couple of hundred lines
13898 of code and about two hours to write it, test it, and debug it.
13899 However, knowing the software toolbox, the administrator can instead start out
13900 by generating just a list of logged on users:
13901
13902 @example
13903 $ who | cut -c1-8
13904 @print{} arnold
13905 @print{} miriam
13906 @print{} bill
13907 @print{} arnold
13908 @end example
13909
13910 Next, sort the list:
13911
13912 @example
13913 $ who | cut -c1-8 | sort
13914 @print{} arnold
13915 @print{} arnold
13916 @print{} bill
13917 @print{} miriam
13918 @end example
13919
13920 Finally, run the sorted list through @command{uniq}, to weed out duplicates:
13921
13922 @example
13923 $ who | cut -c1-8 | sort | uniq
13924 @print{} arnold
13925 @print{} bill
13926 @print{} miriam
13927 @end example
13928
13929 The @command{sort} command actually has a @option{-u} option that does what
13930 @command{uniq} does.  However, @command{uniq} has other uses for which one
13931 cannot substitute @samp{sort -u}.
13932
13933 The administrator puts this pipeline into a shell script, and makes it available for
13934 all the users on the system (@samp{#} is the system administrator,
13935 or @code{root}, prompt):
13936
13937 @example
13938 # cat > /usr/local/bin/listusers
13939 who | cut -c1-8 | sort | uniq
13940 ^D
13941 # chmod +x /usr/local/bin/listusers
13942 @end example
13943
13944 There are four major points to note here.  First, with just four
13945 programs, on one command line, the administrator was able to save about two
13946 hours worth of work.  Furthermore, the shell pipeline is just about as
13947 efficient as the C program would be, and it is much more efficient in
13948 terms of programmer time.  People time is much more expensive than
13949 computer time, and in our modern ``there's never enough time to do
13950 everything'' society, saving two hours of programmer time is no mean
13951 feat.
13952
13953 Second, it is also important to emphasize that with the
13954 @emph{combination} of the tools, it is possible to do a special
13955 purpose job never imagined by the authors of the individual programs.
13956
13957 Third, it is also valuable to build up your pipeline in stages, as we did here.
13958 This allows you to view the data at each stage in the pipeline, which helps
13959 you acquire the confidence that you are indeed using these tools correctly.
13960
13961 Finally, by bundling the pipeline in a shell script, other users can use
13962 your command, without having to remember the fancy plumbing you set up for
13963 them.  In terms of how you run them, shell scripts and compiled programs are
13964 indistinguishable.
13965
13966 After the previous warm-up exercise, we'll look at two additional, more
13967 complicated pipelines.  For them, we need to introduce two more tools.
13968
13969 The first is the @command{tr} command, which stands for ``transliterate.''
13970 The @command{tr} command works on a character-by-character basis, changing
13971 characters.  Normally it is used for things like mapping upper case to
13972 lower case:
13973
13974 @example
13975 $ echo ThIs ExAmPlE HaS MIXED case! | tr '[:upper:]' '[:lower:]'
13976 @print{} this example has mixed case!
13977 @end example
13978
13979 There are several options of interest:
13980
13981 @table @code
13982 @item -c
13983 work on the complement of the listed characters, i.e.,
13984 operations apply to characters not in the given set
13985
13986 @item -d
13987 delete characters in the first set from the output
13988
13989 @item -s
13990 squeeze repeated characters in the output into just one character.
13991 @end table
13992
13993 We will be using all three options in a moment.
13994
13995 The other command we'll look at is @command{comm}.  The @command{comm}
13996 command takes two sorted input files as input data, and prints out the
13997 files' lines in three columns.  The output columns are the data lines
13998 unique to the first file, the data lines unique to the second file, and
13999 the data lines that are common to both.  The @option{-1}, @option{-2}, and
14000 @option{-3} command line options @emph{omit} the respective columns.  (This is
14001 non-intuitive and takes a little getting used to.)  For example:
14002
14003 @example
14004 $ cat f1
14005 @print{} 11111
14006 @print{} 22222
14007 @print{} 33333
14008 @print{} 44444
14009 $ cat f2
14010 @print{} 00000
14011 @print{} 22222
14012 @print{} 33333
14013 @print{} 55555
14014 $ comm f1 f2
14015 @print{}         00000
14016 @print{} 11111
14017 @print{}                 22222
14018 @print{}                 33333
14019 @print{} 44444
14020 @print{}         55555
14021 @end example
14022
14023 The file name @file{-} tells @command{comm} to read standard input
14024 instead of a regular file.
14025
14026 Now we're ready to build a fancy pipeline.  The first application is a word
14027 frequency counter.  This helps an author determine if he or she is over-using
14028 certain words.
14029
14030 The first step is to change the case of all the letters in our input file
14031 to one case.  ``The'' and ``the'' are the same word when doing counting.
14032
14033 @example
14034 $ tr '[:upper:]' '[:lower:]' < whats.gnu | ...
14035 @end example
14036
14037 The next step is to get rid of punctuation.  Quoted words and unquoted words
14038 should be treated identically; it's easiest to just get the punctuation out of
14039 the way.
14040
14041 @smallexample
14042 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' | ...
14043 @end smallexample
14044
14045 The second @command{tr} command operates on the complement of the listed
14046 characters, which are all the letters, the digits, the underscore, and
14047 the blank.  The @samp{\n} represents the newline character; it has to
14048 be left alone.  (The @acronym{ASCII} tab character should also be included for
14049 good measure in a production script.)
14050
14051 At this point, we have data consisting of words separated by blank space.
14052 The words only contain alphanumeric characters (and the underscore).  The
14053 next step is break the data apart so that we have one word per line.  This
14054 makes the counting operation much easier, as we will see shortly.
14055
14056 @smallexample
14057 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14058 > tr -s ' ' '\n' | ...
14059 @end smallexample
14060
14061 This command turns blanks into newlines.  The @option{-s} option squeezes
14062 multiple newline characters in the output into just one.  This helps us
14063 avoid blank lines.  (The @samp{>} is the shell's ``secondary prompt.''
14064 This is what the shell prints when it notices you haven't finished
14065 typing in all of a command.)
14066
14067 We now have data consisting of one word per line, no punctuation, all one
14068 case.  We're ready to count each word:
14069
14070 @smallexample
14071 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14072 > tr -s ' ' '\n' | sort | uniq -c | ...
14073 @end smallexample
14074
14075 At this point, the data might look something like this:
14076
14077 @example
14078      60 a
14079       2 able
14080       6 about
14081       1 above
14082       2 accomplish
14083       1 acquire
14084       1 actually
14085       2 additional
14086 @end example
14087
14088 The output is sorted by word, not by count!  What we want is the most
14089 frequently used words first.  Fortunately, this is easy to accomplish,
14090 with the help of two more @command{sort} options:
14091
14092 @table @code
14093 @item -n
14094 do a numeric sort, not a textual one
14095
14096 @item -r
14097 reverse the order of the sort
14098 @end table
14099
14100 The final pipeline looks like this:
14101
14102 @smallexample
14103 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14104 > tr -s ' ' '\n' | sort | uniq -c | sort -n -r
14105 @print{}    156 the
14106 @print{}     60 a
14107 @print{}     58 to
14108 @print{}     51 of
14109 @print{}     51 and
14110 @dots{}
14111 @end smallexample
14112
14113 Whew!  That's a lot to digest.  Yet, the same principles apply.  With six
14114 commands, on two lines (really one long one split for convenience), we've
14115 created a program that does something interesting and useful, in much
14116 less time than we could have written a C program to do the same thing.
14117
14118 A minor modification to the above pipeline can give us a simple spelling
14119 checker!  To determine if you've spelled a word correctly, all you have to
14120 do is look it up in a dictionary.  If it is not there, then chances are
14121 that your spelling is incorrect.  So, we need a dictionary.
14122 The conventional location for a dictionary is @file{/usr/dict/words}.
14123 On my GNU/Linux system,@footnote{Redhat Linux 6.1, for the November 2000
14124 revision of this article.}
14125 this is a is a sorted, 45,402 word dictionary.
14126
14127 Now, how to compare our file with the dictionary?  As before, we generate
14128 a sorted list of words, one per line:
14129
14130 @smallexample
14131 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14132 > tr -s ' ' '\n' | sort -u | ...
14133 @end smallexample
14134
14135 Now, all we need is a list of words that are @emph{not} in the
14136 dictionary.  Here is where the @command{comm} command comes in.
14137
14138 @smallexample
14139 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14140 > tr -s ' ' '\n' | sort -u |
14141 > comm -23 - /usr/dict/words
14142 @end smallexample
14143
14144 The @option{-2} and @option{-3} options eliminate lines that are only in the
14145 dictionary (the second file), and lines that are in both files.  Lines
14146 only in the first file (standard input, our stream of words), are
14147 words that are not in the dictionary.  These are likely candidates for
14148 spelling errors.  This pipeline was the first cut at a production
14149 spelling checker on Unix.
14150
14151 There are some other tools that deserve brief mention.
14152
14153 @table @command
14154 @item grep
14155 search files for text that matches a regular expression
14156
14157 @item wc
14158 count lines, words, characters
14159
14160 @item tee
14161 a T-fitting for data pipes, copies data to files and to standard output
14162
14163 @item sed
14164 the stream editor, an advanced tool
14165
14166 @item awk
14167 a data manipulation language, another advanced tool
14168 @end table
14169
14170 The software tools philosophy also espoused the following bit of
14171 advice: ``Let someone else do the hard part.''  This means, take
14172 something that gives you most of what you need, and then massage it the
14173 rest of the way until it's in the form that you want.
14174
14175 To summarize:
14176
14177 @enumerate 1
14178 @item
14179 Each program should do one thing well.  No more, no less.
14180
14181 @item
14182 Combining programs with appropriate plumbing leads to results where
14183 the whole is greater than the sum of the parts.  It also leads to novel
14184 uses of programs that the authors might never have imagined.
14185
14186 @item
14187 Programs should never print extraneous header or trailer data, since these
14188 could get sent on down a pipeline.  (A point we didn't mention earlier.)
14189
14190 @item
14191 Let someone else do the hard part.
14192
14193 @item
14194 Know your toolbox!  Use each program appropriately.  If you don't have an
14195 appropriate tool, build one.
14196 @end enumerate
14197
14198 As of this writing, all the programs we've discussed are available via
14199 anonymous @command{ftp} from: @*
14200 @uref{ftp://gnudist.gnu.org/textutils/textutils-1.22.tar.gz}.  (There may
14201 be more recent versions available now.)
14202
14203 None of what I have presented in this column is new.  The Software Tools
14204 philosophy was first introduced in the book @cite{Software Tools}, by
14205 Brian Kernighan and P.J. Plauger (Addison-Wesley, ISBN 0-201-03669-X).
14206 This book showed how to write and use software tools.  It was written in
14207 1976, using a preprocessor for FORTRAN named @command{ratfor} (RATional
14208 FORtran).  At the time, C was not as ubiquitous as it is now; FORTRAN
14209 was.  The last chapter presented a @command{ratfor} to FORTRAN
14210 processor, written in @command{ratfor}.  @command{ratfor} looks an awful
14211 lot like C; if you know C, you won't have any problem following the
14212 code.
14213
14214 In 1981, the book was updated and made available as @cite{Software Tools
14215 in Pascal} (Addison-Wesley, ISBN 0-201-10342-7).  Both books are
14216 still in print and are well worth
14217 reading if you're a programmer.  They certainly made a major change in
14218 how I view programming.
14219
14220 The programs in both books are available from
14221 @uref{http://cm.bell-labs.com/who/bwk, Brian Kernighan's home page}.
14222 For a number of years, there was an active
14223 Software Tools Users Group, whose members had ported the original
14224 @command{ratfor} programs to essentially every computer system with a
14225 FORTRAN compiler.  The popularity of the group waned in the middle 1980s
14226 as Unix began to spread beyond universities.
14227
14228 With the current proliferation of GNU code and other clones of Unix programs,
14229 these programs now receive little attention; modern C versions are
14230 much more efficient and do more than these programs do.  Nevertheless, as
14231 exposition of good programming style, and evangelism for a still-valuable
14232 philosophy, these books are unparalleled, and I recommend them highly.
14233
14234 Acknowledgment: I would like to express my gratitude to Brian Kernighan
14235 of Bell Labs, the original Software Toolsmith, for reviewing this column.
14236
14237 @include doclicense.texi
14238
14239 @node Index
14240 @unnumbered Index
14241
14242 @printindex cp
14243
14244 @shortcontents
14245 @contents
14246 @bye
14247
14248 @c Local variables:
14249 @c texinfo-column-for-description: 32
14250 @c End: