Tizen 2.0 Release
[external/tizen-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 * shuf: (coreutils)shuf invocation.             Shuffling text files.
101 * sleep: (coreutils)sleep invocation.           Delay for a specified time.
102 * sort: (coreutils)sort invocation.             Sort text files.
103 * split: (coreutils)split invocation.           Split into fixed-size pieces.
104 * stat: (coreutils)stat invocation.             Report file(system) status.
105 * stty: (coreutils)stty invocation.             Print/change terminal settings.
106 * su: (coreutils)su invocation.                 Modify user and group ID.
107 * sum: (coreutils)sum invocation.               Print traditional checksum.
108 * sync: (coreutils)sync invocation.             Synchronize memory and disk.
109 * tac: (coreutils)tac invocation.               Reverse files.
110 * tail: (coreutils)tail invocation.             Output the last part of files.
111 * tee: (coreutils)tee invocation.               Redirect to multiple files.
112 * test: (coreutils)test invocation.             File/string tests.
113 * touch: (coreutils)touch invocation.           Change file timestamps.
114 * tr: (coreutils)tr invocation.                 Translate characters.
115 * true: (coreutils)true invocation.             Do nothing, successfully.
116 * tsort: (coreutils)tsort invocation.           Topological sort.
117 * tty: (coreutils)tty invocation.               Print terminal name.
118 * uname: (coreutils)uname invocation.           Print system information.
119 * unexpand: (coreutils)unexpand invocation.     Convert spaces to tabs.
120 * uniq: (coreutils)uniq invocation.             Uniquify files.
121 * unlink: (coreutils)unlink invocation.         Removal via unlink(2).
122 * users: (coreutils)users invocation.           Print current user names.
123 * vdir: (coreutils)vdir invocation.             List directories verbosely.
124 * wc: (coreutils)wc invocation.                 Line, word, and byte counts.
125 * who: (coreutils)who invocation.               Print who is logged in.
126 * whoami: (coreutils)whoami invocation.         Print effective user ID.
127 * yes: (coreutils)yes invocation.               Print a string indefinitely.
128 @end direntry
129
130 @copying
131 This manual documents version @value{VERSION} of the @sc{gnu} core
132 utilities, including the standard programs for text and file manipulation.
133
134 Copyright @copyright{} 1994, 1995, 1996, 2000, 2001, 2002, 2003, 2004,
135 2005, 2006 Free Software Foundation, Inc.
136
137 @quotation
138 Permission is granted to copy, distribute and/or modify this document
139 under the terms of the GNU Free Documentation License, Version 1.2 or
140 any later version published by the Free Software Foundation; with no
141 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
142 Texts.  A copy of the license is included in the section entitled ``GNU
143 Free Documentation License''.
144 @end quotation
145 @end copying
146
147 @titlepage
148 @title @sc{gnu} @code{Coreutils}
149 @subtitle Core GNU utilities
150 @subtitle for version @value{VERSION}, @value{UPDATED}
151 @author David MacKenzie et al.
152
153 @page
154 @vskip 0pt plus 1filll
155 @insertcopying
156 @end titlepage
157
158
159 @ifnottex
160 @node Top
161 @top GNU Coreutils
162
163 @insertcopying
164 @end ifnottex
165
166 @cindex core utilities
167 @cindex text utilities
168 @cindex shell utilities
169 @cindex file utilities
170
171 @menu
172 * Introduction::                       Caveats, overview, and authors.
173 * Common options::                     Common options.
174 * Output of entire files::             cat tac nl od
175 * Formatting file contents::           fmt pr fold
176 * Output of parts of files::           head tail split csplit
177 * Summarizing files::                  wc sum cksum md5sum sha1sum sha2
178 * Operating on sorted files::          sort shuf uniq comm ptx tsort
179 * Operating on fields within a line::  cut paste join
180 * Operating on characters::            tr expand unexpand
181 * Directory listing::                  ls dir vdir dircolors
182 * Basic operations::                   cp dd install mv rm shred
183 * Special file types::                 ln mkdir rmdir mkfifo mknod
184 * Changing file attributes::           chgrp chmod chown touch
185 * Disk usage::                         df du stat sync
186 * Printing text::                      echo printf yes
187 * Conditions::                         false true test expr
188 * Redirection::                        tee
189 * File name manipulation::             dirname basename pathchk
190 * Working context::                    pwd stty printenv tty
191 * User information::                   id logname whoami groups users who
192 * System context::                     date uname hostname hostid
193 * Modified command invocation::        chroot env nice nohup su
194 * Process control::                    kill
195 * Delaying::                           sleep
196 * Numeric operations::                 factor seq
197 * File permissions::                   Access modes.
198 * Date input formats::                 Specifying date strings.
199 * Opening the software toolbox::       The software tools philosophy.
200 * Copying This Manual::                License for copying this manual.
201 * Index::                              General index.
202
203 @detailmenu
204  --- The Detailed Node Listing ---
205
206 Common Options
207
208 * Exit status::                 Indicating program success or failure.
209 * Backup options::              Backup options
210 * Block size::                  Block size
211 * Disambiguating names and IDs:: chgrp and chown owner and group syntax
212 * Random sources::              Sources of random data
213 * Target directory::            Target directory
214 * Trailing slashes::            Trailing slashes
215 * Traversing symlinks::         Traversing symlinks to directories
216 * Treating / specially::        Treating / specially
217 * Standards conformance::       Standards conformance
218
219 Output of entire files
220
221 * cat invocation::              Concatenate and write files.
222 * tac invocation::              Concatenate and write files in reverse.
223 * nl invocation::               Number lines and write files.
224 * od invocation::               Write files in octal or other formats.
225 * base64 invocation::           Transform data into printable data.
226
227 Formatting file contents
228
229 * fmt invocation::              Reformat paragraph text.
230 * pr invocation::               Paginate or columnate files for printing.
231 * fold invocation::             Wrap input lines to fit in specified width.
232
233 Output of parts of files
234
235 * head invocation::             Output the first part of files.
236 * tail invocation::             Output the last part of files.
237 * split invocation::            Split a file into fixed-size pieces.
238 * csplit invocation::           Split a file into context-determined pieces.
239
240 Summarizing files
241
242 * wc invocation::               Print newline, word, and byte counts.
243 * sum invocation::              Print checksum and block counts.
244 * cksum invocation::            Print CRC checksum and byte counts.
245 * md5sum invocation::           Print or check MD5 digests.
246 * sha1sum invocation::          Print or check SHA-1 digests.
247 * sha2 utilities::              Print or check SHA-2 digests.
248
249 Operating on sorted files
250
251 * sort invocation::             Sort text files.
252 * shuf invocation::             Shuffle text files.
253 * uniq invocation::             Uniquify files.
254 * comm invocation::             Compare two sorted files line by line.
255 * ptx invocation::              Produce a permuted index of file contents.
256 * tsort invocation::            Topological sort.
257
258 @command{ptx}: Produce permuted indexes
259
260 * General options in ptx::      Options which affect general program behavior.
261 * Charset selection in ptx::    Underlying character set considerations.
262 * Input processing in ptx::     Input fields, contexts, and keyword selection.
263 * Output formatting in ptx::    Types of output format, and sizing the fields.
264 * Compatibility in ptx::        The @acronym{GNU} extensions to @command{ptx}
265
266 Operating on fields within a line
267
268 * cut invocation::              Print selected parts of lines.
269 * paste invocation::            Merge lines of files.
270 * join invocation::             Join lines on a common field.
271
272 Operating on characters
273
274 * tr invocation::               Translate, squeeze, and/or delete characters.
275 * expand invocation::           Convert tabs to spaces.
276 * unexpand invocation::         Convert spaces to tabs.
277
278 @command{tr}: Translate, squeeze, and/or delete characters
279
280 * Character sets::              Specifying sets of characters.
281 * Translating::                 Changing one set of characters to another.
282 * Squeezing::                   Squeezing repeats and deleting.
283
284 Directory listing
285
286 * ls invocation::               List directory contents
287 * dir invocation::              Briefly list directory contents
288 * vdir invocation::             Verbosely list directory contents
289 * dircolors invocation::        Color setup for @command{ls}
290
291 @command{ls}:  List directory contents
292
293 * Which files are listed::      Which files are listed
294 * What information is listed::  What information is listed
295 * Sorting the output::          Sorting the output
296 * More details about version sort::  More details about version sort
297 * General output formatting::   General output formatting
298 * Formatting the file names::   Formatting the file names
299
300 Basic operations
301
302 * cp invocation::               Copy files and directories
303 * dd invocation::               Convert and copy a file
304 * install invocation::          Copy files and set attributes
305 * mv invocation::               Move (rename) files
306 * rm invocation::               Remove files or directories
307 * shred invocation::            Remove files more securely
308
309 Special file types
310
311 * link invocation::             Make a hard link via the link syscall
312 * ln invocation::               Make links between files
313 * mkdir invocation::            Make directories
314 * mkfifo invocation::           Make FIFOs (named pipes)
315 * mknod invocation::            Make block or character special files
316 * readlink invocation::         Print the referent of a symbolic link
317 * rmdir invocation::            Remove empty directories
318 * unlink invocation::           Remove files via unlink syscall
319
320 Changing file attributes
321
322 * chown invocation::            Change file owner and group
323 * chgrp invocation::            Change group ownership
324 * chmod invocation::            Change access permissions
325 * touch invocation::            Change file timestamps
326
327 Disk usage
328
329 * df invocation::               Report file system disk space usage
330 * du invocation::               Estimate file space usage
331 * stat invocation::             Report file or file system status
332 * sync invocation::             Synchronize data on disk with memory
333
334 Printing text
335
336 * echo invocation::             Print a line of text
337 * printf invocation::           Format and print data
338 * yes invocation::              Print a string until interrupted
339
340 Conditions
341
342 * false invocation::              Do nothing, unsuccessfully
343 * true invocation::               Do nothing, successfully
344 * test invocation::               Check file types and compare values
345 * expr invocation::               Evaluate expressions
346
347 @command{test}: Check file types and compare values
348
349 * File type tests::               File type tests
350 * Access permission tests::       Access permission tests
351 * File characteristic tests::     File characteristic tests
352 * String tests::                  String tests
353 * Numeric tests::                 Numeric tests
354
355 @command{expr}: Evaluate expression
356
357 * String expressions::         + : match substr index length
358 * Numeric expressions::        + - * / %
359 * Relations for expr::         | & < <= = == != >= >
360 * Examples of expr::           Examples of using @command{expr}
361
362 Redirection
363
364 * tee invocation::               Redirect output to multiple files
365
366 File name manipulation
367
368 * basename invocation::          Strip directory and suffix from a file name
369 * dirname invocation::           Strip non-directory suffix from a file name
370 * pathchk invocation::           Check file name portability
371
372 Working context
373
374 * pwd invocation::               Print working directory
375 * stty invocation::              Print or change terminal characteristics
376 * printenv invocation::          Print all or some environment variables
377 * tty invocation::               Print file name of terminal on standard input
378
379 @command{stty}: Print or change terminal characteristics
380
381 * Control::                     Control settings
382 * Input::                       Input settings
383 * Output::                      Output settings
384 * Local::                       Local settings
385 * Combination::                 Combination settings
386 * Characters::                  Special characters
387 * Special::                     Special settings
388
389 User information
390
391 * id invocation::                Print user identity
392 * logname invocation::           Print current login name
393 * whoami invocation::            Print effective user ID
394 * groups invocation::            Print group names a user is in
395 * users invocation::             Print login names of users currently logged in
396 * who invocation::               Print who is currently logged in
397
398 System context
399
400 * date invocation::              Print or set system date and time
401 * uname invocation::             Print system information
402 * hostname invocation::          Print or set system name
403 * hostid invocation::            Print numeric host identifier.
404
405 @command{date}: Print or set system date and time
406
407 * Time conversion specifiers::   %[HIklMNpPrRsSTXzZ]
408 * Date conversion specifiers::   %[aAbBcCdDeFgGhjmuUVwWxyY]
409 * Literal conversion specifiers:: %[%nt]
410 * Padding and other flags::      Pad with zeros, spaces, etc.
411 * Setting the time::             Changing the system clock.
412 * Options for date::             Instead of the current time.
413 * Examples of date::             Examples.
414
415 Modified command invocation
416
417 * chroot invocation::            Run a command with a different root directory
418 * env invocation::               Run a command in a modified environment
419 * nice invocation::              Run a command with modified niceness
420 * nohup invocation::             Run a command immune to hangups
421 * su invocation::                Run a command with substitute user and group ID
422
423 Process control
424
425 * kill invocation::              Sending a signal to processes.
426
427 Delaying
428
429 * sleep invocation::             Delay for a specified time
430
431 Numeric operations
432
433 * factor invocation::            Print prime factors
434 * seq invocation::               Print numeric sequences
435
436 File permissions
437
438 * Mode Structure::               Structure of file mode bits.
439 * Symbolic Modes::               Mnemonic representation of file mode bits.
440 * Numeric Modes::                File mode bits as octal numbers.
441 * Directory Setuid and Setgid::  Set-user-ID and set-group-ID on directories.
442
443 Date input formats
444
445 * General date syntax::          Common rules.
446 * Calendar date items::          19 Dec 1994.
447 * Time of day items::            9:20pm.
448 * Time zone items::              @sc{est}, @sc{pdt}, @sc{gmt}.
449 * Day of week items::            Monday and others.
450 * Relative items in date strings:: next tuesday, 2 years ago.
451 * Pure numbers in date strings:: 19931219, 1440.
452 * Seconds since the Epoch::      @@1078100502.
453 * Specifying time zone rules::   TZ="America/New_York", TZ="UTC0".
454 * Authors of get_date::          Bellovin, Eggert, Salz, Berets, et al.
455
456 Opening the software toolbox
457
458 * Toolbox introduction::         Toolbox introduction
459 * I/O redirection::              I/O redirection
460 * The who command::              The @command{who} command
461 * The cut command::              The @command{cut} command
462 * The sort command::             The @command{sort} command
463 * The uniq command::             The @command{uniq} command
464 * Putting the tools together::   Putting the tools together
465
466 Copying This Manual
467
468 * GNU Free Documentation License::  License for copying this manual.
469
470 @end detailmenu
471 @end menu
472
473
474 @node Introduction
475 @chapter Introduction
476
477 This manual is a work in progress: many sections make no attempt to explain
478 basic concepts in a way suitable for novices.  Thus, if you are interested,
479 please get involved in improving this manual.  The entire @sc{gnu} community
480 will benefit.
481
482 @cindex @acronym{POSIX}
483 The @sc{gnu} utilities documented here are mostly compatible with the
484 @acronym{POSIX} standard.
485 @cindex bugs, reporting
486 Please report bugs to @email{bug-coreutils@@gnu.org}.  Remember
487 to include the version number, machine architecture, input files, and
488 any other information needed to reproduce the bug: your input, what you
489 expected, what you got, and why it is wrong.  Diffs are welcome, but
490 please include a description of the problem as well, since this is
491 sometimes difficult to infer.  @xref{Bugs, , , gcc, Using and Porting GNU CC}.
492
493 @cindex Berry, K.
494 @cindex Paterson, R.
495 @cindex Stallman, R.
496 @cindex Pinard, F.
497 @cindex MacKenzie, D.
498 @cindex Meyering, J.
499 @cindex Youmans, B.
500 This manual was originally derived from the Unix man pages in the
501 distributions, which were written by David MacKenzie and updated by Jim
502 Meyering.  What you are reading now is the authoritative documentation
503 for these utilities; the man pages are no longer being maintained.  The
504 original @command{fmt} man page was written by Ross Paterson.  Fran@,{c}ois
505 Pinard did the initial conversion to Texinfo format.  Karl Berry did the
506 indexing, some reorganization, and editing of the results.  Brian
507 Youmans of the Free Software Foundation office staff combined the
508 manuals for textutils, fileutils, and sh-utils to produce the present
509 omnibus manual.  Richard Stallman contributed his usual invaluable
510 insights to the overall process.
511
512 @node Common options
513 @chapter Common options
514
515 @macro optBackup
516 @item -b
517 @itemx @w{@kbd{--backup}[=@var{method}]}
518 @opindex -b
519 @opindex --backup
520 @vindex VERSION_CONTROL
521 @cindex backups, making
522 @xref{Backup options}.
523 Make a backup of each file that would otherwise be overwritten or removed.
524 @end macro
525
526 @macro optBackupSuffix
527 @item -S @var{suffix}
528 @itemx --suffix=@var{suffix}
529 @opindex -S
530 @opindex --suffix
531 Append @var{suffix} to each backup file made with @option{-b}.
532 @xref{Backup options}.
533 @end macro
534
535 @macro optTargetDirectory
536 @item -t @var{directory}
537 @itemx @w{@kbd{--target-directory}=@var{directory}}
538 @opindex -t
539 @opindex --target-directory
540 @cindex target directory
541 @cindex destination directory
542 Specify the destination @var{directory}.
543 @xref{Target directory}.
544 @end macro
545
546 @macro optNoTargetDirectory
547 @item -T
548 @itemx --no-target-directory
549 @opindex -T
550 @opindex --no-target-directory
551 @cindex target directory
552 @cindex destination directory
553 Do not treat the last operand specially when it is a directory or a
554 symbolic link to a directory.  @xref{Target directory}.
555 @end macro
556
557 @macro optSi
558 @itemx --si
559 @opindex --si
560 @cindex SI output
561 Append an SI-style abbreviation to each size, such as @samp{M} for
562 megabytes.  Powers of 1000 are used, not 1024; @samp{M} stands for
563 1,000,000 bytes.  This option is equivalent to
564 @option{--block-size=si}.  Use the @option{-h} or
565 @option{--human-readable} option if
566 you prefer powers of 1024.
567 @end macro
568
569 @macro optHumanReadable
570 @item -h
571 @itemx --human-readable
572 @opindex -h
573 @opindex --human-readable
574 @cindex human-readable output
575 Append a size letter to each size, such as @samp{M} for mebibytes.
576 Powers of 1024 are used, not 1000; @samp{M} stands for 1,048,576 bytes.
577 Use the @option{--si} option if you prefer powers of 1000.
578 @end macro
579
580 @macro optStripTrailingSlashes
581 @itemx @w{@kbd{--strip-trailing-slashes}}
582 @opindex --strip-trailing-slashes
583 @cindex stripping trailing slashes
584 Remove any trailing slashes from each @var{source} argument.
585 @xref{Trailing slashes}.
586 @end macro
587
588 @cindex common options
589
590 Certain options are available in all of these programs.  Rather than
591 writing identical descriptions for each of the programs, they are
592 described here.  (In fact, every @sc{gnu} program accepts (or should accept)
593 these options.)
594
595 @vindex POSIXLY_CORRECT
596 Normally options and operands can appear in any order, and programs act
597 as if all the options appear before any operands.  For example,
598 @samp{sort -r passwd -t :} acts like @samp{sort -r -t : passwd}, since
599 @samp{:} is an option-argument of @option{-t}.  However, if the
600 @env{POSIXLY_CORRECT} environment variable is set, options must appear
601 before operands, unless otherwise specified for a particular command.
602
603 A few programs can usefully have trailing operands with leading
604 @samp{-}.  With such a program, options must precede operands even if
605 @env{POSIXLY_CORRECT} is not set, and this fact is noted in the
606 program description.  For example, the @command{env} command's options
607 must appear before its operands, since in some cases the operands
608 specify a command that itself contains options.
609
610 Some of these programs recognize the @option{--help} and @option{--version}
611 options only when one of them is the sole command line argument.
612
613 @table @samp
614
615 @item --help
616 @opindex --help
617 @cindex help, online
618 Print a usage message listing all available options, then exit successfully.
619
620 @item --version
621 @opindex --version
622 @cindex version number, finding
623 Print the version number, then exit successfully.
624
625 @item --
626 @opindex --
627 @cindex option delimiter
628 Delimit the option list.  Later arguments, if any, are treated as
629 operands even if they begin with @samp{-}.  For example, @samp{sort --
630 -r} reads from the file named @file{-r}.
631
632 @end table
633
634 @cindex standard input
635 @cindex standard output
636 A single @samp{-} operand is not really an option, though it looks like one.  It
637 stands for standard input, or for standard output if that is clear from
638 the context.  For example, @samp{sort -} reads from standard input,
639 and is equivalent to plain @samp{sort}, and @samp{tee -} writes an
640 extra copy of its input to standard output.  Unless otherwise
641 specified, @samp{-} can appear as any operand that requires a file
642 name.
643
644 @menu
645 * Exit status::                 Indicating program success or failure.
646 * Backup options::              -b -S, in some programs.
647 * Block size::                  BLOCK_SIZE and --block-size, in some programs.
648 * Disambiguating names and IDs:: chgrp and chown owner and group syntax
649 * Random sources::              --random-source, in some programs.
650 * Target directory::            Specifying a target directory, in some programs.
651 * Trailing slashes::            --strip-trailing-slashes, in some programs.
652 * Traversing symlinks::         -H, -L, or -P, in some programs.
653 * Treating / specially::        --preserve-root and --no-preserve-root.
654 * Special built-in utilities::  @command{break}, @command{:}, @command{eval}, @dots{}
655 * Standards conformance::       Conformance to the @acronym{POSIX} standard.
656 @end menu
657
658
659 @node Exit status
660 @section Exit status
661
662 @macro exitstatus
663 An exit status of zero indicates success,
664 and a nonzero value indicates failure.
665 @end macro
666
667 Nearly every command invocation yields an integral @dfn{exit status}
668 that can be used to change how other commands work.
669 For the vast majority of commands, an exit status of zero indicates
670 success.  Failure is indicated by a nonzero value---typically
671 @samp{1}, though it may differ on unusual platforms as @acronym{POSIX}
672 requires only that it be nonzero.
673
674 However, some of the programs documented here do produce
675 other exit status values and a few associate different
676 meanings with the values @samp{0} and @samp{1}.
677 Here are some of the exceptions:
678 @command{chroot}, @command{env}, @command{expr},
679 @command{nice}, @command{nohup}, @command{printenv},
680 @command{sort}, @command{su}, @command{test}, @command{tty}.
681
682
683 @node Backup options
684 @section Backup options
685
686 @cindex backup options
687
688 Some @sc{gnu} programs (at least @command{cp}, @command{install},
689 @command{ln}, and @command{mv}) optionally make backups of files
690 before writing new versions.
691 These options control the details of these backups.  The options are also
692 briefly mentioned in the descriptions of the particular programs.
693
694 @table @samp
695
696 @item -b
697 @itemx @w{@kbd{--backup}[=@var{method}]}
698 @opindex -b
699 @opindex --backup
700 @vindex VERSION_CONTROL
701 @cindex backups, making
702 Make a backup of each file that would otherwise be overwritten or removed.
703 Without this option, the original versions are destroyed.
704 Use @var{method} to determine the type of backups to make.
705 When this option is used but @var{method} is not specified,
706 then the value of the @env{VERSION_CONTROL}
707 environment variable is used.  And if @env{VERSION_CONTROL} is not set,
708 the default backup type is @samp{existing}.
709
710 Note that the short form of this option, @option{-b} does not accept any
711 argument.  Using @option{-b} is equivalent to using @option{--backup=existing}.
712
713 @vindex version-control @r{Emacs variable}
714 This option corresponds to the Emacs variable @samp{version-control};
715 the values for @var{method} are the same as those used in Emacs.
716 This option also accepts more descriptive names.
717 The valid @var{method}s are (unique abbreviations are accepted):
718
719 @table @samp
720 @item none
721 @itemx off
722 @opindex none @r{backup method}
723 Never make backups.
724
725 @item numbered
726 @itemx t
727 @opindex numbered @r{backup method}
728 Always make numbered backups.
729
730 @item existing
731 @itemx nil
732 @opindex existing @r{backup method}
733 Make numbered backups of files that already have them, simple backups
734 of the others.
735
736 @item simple
737 @itemx never
738 @opindex simple @r{backup method}
739 Always make simple backups.  Please note @samp{never} is not to be
740 confused with @samp{none}.
741
742 @end table
743
744 @item -S @var{suffix}
745 @itemx --suffix=@var{suffix}
746 @opindex -S
747 @opindex --suffix
748 @cindex backup suffix
749 @vindex SIMPLE_BACKUP_SUFFIX
750 Append @var{suffix} to each backup file made with @option{-b}.  If this
751 option is not specified, the value of the @env{SIMPLE_BACKUP_SUFFIX}
752 environment variable is used.  And if @env{SIMPLE_BACKUP_SUFFIX} is not
753 set, the default is @samp{~}, just as in Emacs.
754
755 @end table
756
757 @node Block size
758 @section Block size
759
760 @cindex block size
761
762 Some @sc{gnu} programs (at least @command{df}, @command{du}, and
763 @command{ls}) display sizes in ``blocks''.  You can adjust the block size
764 and method of display to make sizes easier to read.  The block size
765 used for display is independent of any file system block size.
766 Fractional block counts are rounded up to the nearest integer.
767
768 @opindex --block-size=@var{size}
769 @vindex BLOCKSIZE
770 @vindex BLOCK_SIZE
771 @vindex DF_BLOCK_SIZE
772 @vindex DU_BLOCK_SIZE
773 @vindex LS_BLOCK_SIZE
774 @vindex POSIXLY_CORRECT@r{, and block size}
775
776 The default block size is chosen by examining the following environment
777 variables in turn; the first one that is set determines the block size.
778
779 @table @code
780
781 @item DF_BLOCK_SIZE
782 This specifies the default block size for the @command{df} command.
783 Similarly, @env{DU_BLOCK_SIZE} specifies the default for @command{du} and
784 @env{LS_BLOCK_SIZE} for @command{ls}.
785
786 @item BLOCK_SIZE
787 This specifies the default block size for all three commands, if the
788 above command-specific environment variables are not set.
789
790 @item BLOCKSIZE
791 This specifies the default block size for all values that are normally
792 printed as blocks, if neither @env{BLOCK_SIZE} nor the above
793 command-specific environment variables are set.  Unlike the other
794 environment variables, @env{BLOCKSIZE} does not affect values that are
795 normally printed as byte counts, e.g., the file sizes contained in
796 @code{ls -l} output.
797
798 @item POSIXLY_CORRECT
799 If neither @env{@var{command}_BLOCK_SIZE}, nor @env{BLOCK_SIZE}, nor
800 @env{BLOCKSIZE} is set, but this variable is set, the block size
801 defaults to 512.
802
803 @end table
804
805 If none of the above environment variables are set, the block size
806 currently defaults to 1024 bytes in most contexts, but this number may
807 change in the future.  For @command{ls} file sizes, the block size
808 defaults to 1 byte.
809
810 @cindex human-readable output
811 @cindex SI output
812
813 A block size specification can be a positive integer specifying the number
814 of bytes per block, or it can be @code{human-readable} or @code{si} to
815 select a human-readable format.  Integers may be followed by suffixes
816 that are upward compatible with the
817 @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI prefixes}
818 for decimal multiples and with the
819 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
820 prefixes for binary multiples}.
821
822 With human-readable formats, output sizes are followed by a size letter
823 such as @samp{M} for megabytes.  @code{BLOCK_SIZE=human-readable} uses
824 powers of 1024; @samp{M} stands for 1,048,576 bytes.
825 @code{BLOCK_SIZE=si} is similar, but uses powers of 1000 and appends
826 @samp{B}; @samp{MB} stands for 1,000,000 bytes.
827
828 @vindex LC_NUMERIC
829 A block size specification preceded by @samp{'} causes output sizes to
830 be displayed with thousands separators.  The @env{LC_NUMERIC} locale
831 specifies the thousands separator and grouping.  For example, in an
832 American English locale, @samp{--block-size="'1kB"} would cause a size
833 of 1234000 bytes to be displayed as @samp{1,234}.  In the default C
834 locale, there is no thousands separator so a leading @samp{'} has no
835 effect.
836
837 An integer block size can be followed by a suffix to specify a
838 multiple of that size.  A bare size letter,
839 or one followed by @samp{iB}, specifies
840 a multiple using powers of 1024.  A size letter followed by @samp{B}
841 specifies powers of 1000 instead.  For example, @samp{1M} and
842 @samp{1MiB} are equivalent to @samp{1048576}, whereas @samp{1MB} is
843 equivalent to @samp{1000000}.
844
845 A plain suffix without a preceding integer acts as if @samp{1} were
846 prepended, except that it causes a size indication to be appended to
847 the output.  For example, @samp{--block-size="kB"} displays 3000 as
848 @samp{3kB}.
849
850 The following suffixes are defined.  Large sizes like @code{1Y}
851 may be rejected by your computer due to limitations of its arithmetic.
852
853 @table @samp
854 @item kB
855 @cindex kilobyte, definition of
856 kilobyte: @math{10^3 = 1000}.
857 @item k
858 @itemx K
859 @itemx KiB
860 @cindex kibibyte, definition of
861 kibibyte: @math{2^10 = 1024}.  @samp{K} is special: the SI prefix is
862 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
863 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
864 @item MB
865 @cindex megabyte, definition of
866 megabyte: @math{10^6 = 1,000,000}.
867 @item M
868 @itemx MiB
869 @cindex mebibyte, definition of
870 mebibyte: @math{2^20 = 1,048,576}.
871 @item GB
872 @cindex gigabyte, definition of
873 gigabyte: @math{10^9 = 1,000,000,000}.
874 @item G
875 @itemx GiB
876 @cindex gibibyte, definition of
877 gibibyte: @math{2^30 = 1,073,741,824}.
878 @item TB
879 @cindex terabyte, definition of
880 terabyte:  @math{10^12 = 1,000,000,000,000}.
881 @item T
882 @itemx TiB
883 @cindex tebibyte, definition of
884 tebibyte: @math{2^40 = 1,099,511,627,776}.
885 @item PB
886 @cindex petabyte, definition of
887 petabyte: @math{10^15 = 1,000,000,000,000,000}.
888 @item P
889 @itemx PiB
890 @cindex pebibyte, definition of
891 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
892 @item EB
893 @cindex exabyte, definition of
894 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
895 @item E
896 @itemx EiB
897 @cindex exbibyte, definition of
898 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
899 @item ZB
900 @cindex zettabyte, definition of
901 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
902 @item Z
903 @itemx ZiB
904 @math{2^70 = 1,180,591,620,717,411,303,424}.
905 (@samp{Zi} is a @acronym{GNU} extension to IEC 60027-2.)
906 @item YB
907 @cindex yottabyte, definition of
908 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
909 @item Y
910 @itemx YiB
911 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
912 (@samp{Yi} is a @acronym{GNU} extension to IEC 60027-2.)
913 @end table
914
915 @opindex -k
916 @opindex -h
917 @opindex --block-size
918 @opindex --human-readable
919 @opindex --si
920
921 Block size defaults can be overridden by an explicit
922 @option{--block-size=@var{size}} option.  The @option{-k}
923 option is equivalent to @option{--block-size=1K}, which
924 is the default unless the @env{POSIXLY_CORRECT} environment variable is
925 set.  The @option{-h} or @option{--human-readable} option is equivalent to
926 @option{--block-size=human-readable}.  The @option{--si} option is
927 equivalent to @option{--block-size=si}.
928
929 @node Disambiguating names and IDs
930 @section chown and chgrp: Disambiguating user names and IDs
931 @cindex user names, disambiguating
932 @cindex user IDs, disambiguating
933 @cindex group names, disambiguating
934 @cindex group IDs, disambiguating
935 @cindex disambiguating group names and IDs
936
937 Since the @var{owner} and @var{group} arguments to @command{chown} and
938 @command{chgrp} may be specified as names or numeric IDs, there is an
939 apparent ambiguity.
940 What if a user or group @emph{name} is a string of digits?
941 @footnote{Using a number as a user name is common in some environments.}
942 Should the command interpret it as a user name or as an ID?
943 @acronym{POSIX} requires that @command{chown} and @command{chgrp}
944 first attempt to resolve the specified string as a name, and
945 only once that fails, then try to interpret it as an ID.
946 This is troublesome when you want to specify a numeric ID, say 42,
947 and it must work even in a pathological situation where
948 @samp{42} is a user name that maps to some other user ID, say 1000.
949 Simply invoking @code{chown 42 F}, will set @file{F}s owner ID to
950 1000---not what you intended.
951
952 GNU @command{chown} and @command{chgrp} provide a way to work around this,
953 that at the same time may result in a significant performance improvement
954 by eliminating a database look-up.
955 Simply precede each numeric user ID and/or group ID with a @samp{+},
956 in order to force its interpretation as an integer:
957
958 @example
959 chown +42 F
960 chgrp +$numeric_group_id another-file
961 chown +0:+0 /
962 @end example
963
964 GNU @command{chown} and @command{chgrp}
965 skip the name look-up process for each @samp{+}-prefixed string,
966 because a string containing @samp{+} is never a valid user or group name.
967 This syntax is accepted on most common Unix systems, but not on Solaris 10.
968
969 @node Random sources
970 @section Sources of random data
971
972 @cindex random sources
973
974 The @command{shuf}, @command{shred}, and @command{sort} commands
975 sometimes need random data to do their work.  For example, @samp{sort
976 -R} must choose a hash function at random, and it needs random data to
977 make this selection.
978
979 Normally these commands use the device file @file{/dev/urandom} as the
980 source of random data.  Typically, this device gathers environmental
981 noise from device drivers and other sources into an entropy pool, and
982 uses the pool to generate random bits.  If the pool is short of data,
983 the device reuses the internal pool to produce more bits, using a
984 cryptographically secure pseudorandom number generator.
985
986 @file{/dev/urandom} suffices for most practical uses, but applications
987 requiring high-value or long-term protection of private data may
988 require an alternate data source like @file{/dev/random} or
989 @file{/dev/arandom}.  The set of available sources depends on your
990 operating system.
991
992 To use such a source, specify the @option{--random-source=@var{file}}
993 option, e.g., @samp{shuf --random-source=/dev/random}.  The contents
994 of @var{file} should be as random as possible.  An error is reported
995 if @var{file} does not contain enough bytes to randomize the input
996 adequately.
997
998 To reproduce the results of an earlier invocation of a command, you
999 can save some random data into a file and then use that file as the
1000 random source in earlier and later invocations of the command.
1001
1002 Some old-fashioned or stripped-down operating systems lack support for
1003 @command{/dev/urandom}.  On these systems commands like @command{shuf}
1004 by default fall back on an internal pseudorandom generator initialized
1005 by a small amount of entropy.
1006
1007 @node Target directory
1008 @section Target directory
1009
1010 @cindex target directory
1011
1012 The @command{cp}, @command{install}, @command{ln}, and @command{mv}
1013 commands normally treat the last operand specially when it is a
1014 directory or a symbolic link to a directory.  For example, @samp{cp
1015 source dest} is equivalent to @samp{cp source dest/source} if
1016 @file{dest} is a directory.  Sometimes this behavior is not exactly
1017 what is wanted, so these commands support the following options to
1018 allow more fine-grained control:
1019
1020 @table @samp
1021
1022 @item -T
1023 @itemx --no-target-directory
1024 @opindex --no-target-directory
1025 @cindex target directory
1026 @cindex destination directory
1027 Do not treat the last operand specially when it is a directory or a
1028 symbolic link to a directory.  This can help avoid race conditions in
1029 programs that operate in a shared area.  For example, when the command
1030 @samp{mv /tmp/source /tmp/dest} succeeds, there is no guarantee that
1031 @file{/tmp/source} was renamed to @file{/tmp/dest}: it could have been
1032 renamed to @file{/tmp/dest/source} instead, if some other process
1033 created @file{/tmp/dest} as a directory.  However, if @file{mv
1034 -T /tmp/source /tmp/dest} succeeds, there is no
1035 question that @file{/tmp/source} was renamed to @file{/tmp/dest}.
1036
1037 In the opposite situation, where you want the last operand to be
1038 treated as a directory and want a diagnostic otherwise, you can use
1039 the @option{--target-directory} (@option{-t}) option.
1040
1041 @item -t @var{directory}
1042 @itemx @w{@kbd{--target-directory}=@var{directory}}
1043 @opindex --target-directory
1044 @cindex target directory
1045 @cindex destination directory
1046 Use @var{directory} as the directory component of each destination
1047 file name.
1048
1049 The interface for most programs is that after processing options and a
1050 finite (possibly zero) number of fixed-position arguments, the remaining
1051 argument list is either expected to be empty, or is a list of items
1052 (usually files) that will all be handled identically.  The @command{xargs}
1053 program is designed to work well with this convention.
1054
1055 The commands in the @command{mv}-family are unusual in that they take
1056 a variable number of arguments with a special case at the @emph{end}
1057 (namely, the target directory).  This makes it nontrivial to perform some
1058 operations, e.g., ``move all files from here to ../d/'', because
1059 @code{mv * ../d/} might exhaust the argument space, and @code{ls | xargs ...}
1060 doesn't have a clean way to specify an extra final argument for each
1061 invocation of the subject command.  (It can be done by going through a
1062 shell command, but that requires more human labor and brain power than
1063 it should.)
1064
1065 The @w{@kbd{--target-directory}} (@option{-t}) option allows the @command{cp},
1066 @command{install}, @command{ln}, and @command{mv} programs to be used
1067 conveniently with @command{xargs}.  For example, you can move the files
1068 from the current directory to a sibling directory, @code{d} like this:
1069
1070 @smallexample
1071 ls | xargs mv -t ../d --
1072 @end smallexample
1073
1074 However, this doesn't move files whose names begin with @samp{.}.
1075 If you use the @sc{gnu} @command{find} program, you can move those
1076 files too, with this command:
1077
1078 @example
1079 find . -mindepth 1 -maxdepth 1 \
1080   | xargs mv -t ../d
1081 @end example
1082
1083 But both of the above approaches fail if there are no files in the
1084 current directory, or if any file has a name containing a blank or
1085 some other special characters.
1086 The following example removes those limitations and requires both
1087 @sc{gnu} @command{find} and @sc{gnu} @command{xargs}:
1088
1089 @example
1090 find . -mindepth 1 -maxdepth 1 -print0 \
1091   | xargs --null --no-run-if-empty \
1092       mv -t ../d
1093 @end example
1094
1095 @end table
1096
1097 @noindent
1098 The @option{--target-directory} (@option{-t}) and
1099 @option{--no-target-directory} (@option{-T})
1100 options cannot be combined.
1101
1102 @node Trailing slashes
1103 @section Trailing slashes
1104
1105 @cindex trailing slashes
1106
1107 Some @sc{gnu} programs (at least @command{cp} and @command{mv}) allow you to
1108 remove any trailing slashes from each @var{source} argument before
1109 operating on it.  The @w{@kbd{--strip-trailing-slashes}} option enables
1110 this behavior.
1111
1112 This is useful when a @var{source} argument may have a trailing slash and
1113 @c FIXME: mv's behavior in this case is system-dependent
1114 specify a symbolic link to a directory.  This scenario is in fact rather
1115 common because some shells can automatically append a trailing slash when
1116 performing file name completion on such symbolic links.  Without this
1117 option, @command{mv}, for example, (via the system's rename function) must
1118 interpret a trailing slash as a request to dereference the symbolic link
1119 and so must rename the indirectly referenced @emph{directory} and not
1120 the symbolic link.  Although it may seem surprising that such behavior
1121 be the default, it is required by @acronym{POSIX} and is consistent with
1122 other parts of that standard.
1123
1124 @node Traversing symlinks
1125 @section Traversing symlinks
1126
1127 @cindex symbolic link to directory, controlling traversal of
1128
1129 The following options modify how @command{chown} and @command{chgrp}
1130 @c FIXME: note that `du' has these options, too, but they have slightly
1131 @c different meaning.
1132 traverse a hierarchy when the @option{--recursive} (@option{-R})
1133 option is also specified.
1134 If more than one of the following options is specified, only the final
1135 one takes effect.
1136 These options specify whether processing a symbolic link to a directory
1137 entails operating on just the symbolic link or on all files in the
1138 hierarchy rooted at that directory.
1139
1140 These options are independent of @option{--dereference} and
1141 @option{--no-dereference} (@option{-h}), which control whether to modify
1142 a symlink or its referent.
1143
1144 @table @samp
1145
1146 @macro choptH
1147 @item -H
1148 @opindex -H
1149 @cindex symbolic link to directory, traverse each that is specified on the command line
1150 If @option{--recursive} (@option{-R}) is specified and
1151 a command line argument is a symbolic link to a directory, traverse it.
1152 @end macro
1153 @choptH
1154
1155 @macro choptL
1156 @item -L
1157 @opindex -L
1158 @cindex symbolic link to directory, traverse each that is encountered
1159 In a recursive traversal, traverse every symbolic link to a directory
1160 that is encountered.
1161 @end macro
1162 @choptL
1163
1164 @macro choptP
1165 @item -P
1166 @opindex -P
1167 @cindex symbolic link to directory, never traverse
1168 Do not traverse any symbolic links.
1169 This is the default if none of @option{-H}, @option{-L},
1170 or @option{-P} is specified.
1171 @end macro
1172 @choptP
1173
1174 @end table
1175
1176
1177 @node Treating / specially
1178 @section Treating / specially
1179
1180 Certain commands can operate destructively on entire hierarchies.
1181 For example, if a user with appropriate privileges mistakenly runs
1182 @samp{rm -rf / tmp/junk}, that may remove
1183 all files on the entire system.  Since there are so few
1184 legitimate uses for such a command,
1185 @sc{gnu} @command{rm} normally declines to operate on any directory
1186 that resolves to @file{/}.  If you really want to try to remove all
1187 the files on your system, you can use the @option{--no-preserve-root}
1188 option, but the default behavior, specified by the
1189 @option{--preserve-option}, is safer for most purposes.
1190
1191 The commands @command{chgrp}, @command{chmod} and @command{chown}
1192 can also operate destructively on entire hierarchies, so they too
1193 support these options.  Although, unlike @command{rm}, they don't
1194 actually unlink files, these commands are arguably more dangerous
1195 when operating recursively on @file{/}, since they often work much
1196 more quickly, and hence damage more files before an alert user can
1197 interrupt them.  Tradition and @acronym{POSIX} require these commands
1198 to operate recursively on @file{/}, so they default to
1199 @option{--no-preserve-root}, but using the @option{--preserve-root}
1200 option makes them safer for most purposes.  For convenience you can
1201 specify @option{--preserve-root} in an alias or in a shell function.
1202
1203 Note that the @option{--preserve-root} option also ensures
1204 that @command{chgrp} and @command{chown} do not modify @file{/}
1205 even when dereferencing a symlink pointing to @file{/}.
1206
1207 @node Special built-in utilities
1208 @section Special built-in utilities
1209
1210 Some programs like @command{nice} can invoke other programs; for
1211 example, the command @samp{nice cat file} invokes the program
1212 @command{cat} by executing the command @samp{cat file}.  However,
1213 @dfn{special built-in utilities} like @command{exit} cannot be invoked
1214 this way.  For example, the command @samp{nice exit} does not have a
1215 well-defined behavior: it may generate an error message instead of
1216 exiting.
1217
1218 Here is a list of the special built-in utilities that are standardized
1219 by @acronym{POSIX} 1003.1-2004.
1220
1221 @quotation
1222 @t{.@: : break continue eval exec exit export readonly
1223 return set shift times trap unset}
1224 @end quotation
1225
1226 For example, because @samp{.}, @samp{:}, and @samp{exec} are special,
1227 the commands @samp{nice . foo.sh}, @samp{nice :}, and @samp{nice exec
1228 pwd} do not work as you might expect.
1229
1230 Many shells extend this list.  For example, Bash has several extra
1231 special built-in utilities like @command{history}, and
1232 @command{suspend}, and with Bash the command @samp{nice suspend}
1233 generates an error message instead of suspending.
1234
1235 @node Standards conformance
1236 @section Standards conformance
1237
1238 @vindex POSIXLY_CORRECT
1239 In a few cases, the @sc{gnu} utilities' default behavior is
1240 incompatible with the @acronym{POSIX} standard.  To suppress these
1241 incompatibilities, define the @env{POSIXLY_CORRECT} environment
1242 variable.  Unless you are checking for @acronym{POSIX} conformance, you
1243 probably do not need to define @env{POSIXLY_CORRECT}.
1244
1245 Newer versions of @acronym{POSIX} are occasionally incompatible with older
1246 versions.  For example, older versions of @acronym{POSIX} required the
1247 command @samp{sort +1} to sort based on the second and succeeding
1248 fields in each input line, but starting with @acronym{POSIX} 1003.1-2001
1249 the same command is required to sort the file named @file{+1}, and you
1250 must instead use the command @samp{sort -k 2} to get the field-based
1251 sort.
1252
1253 @vindex _POSIX2_VERSION
1254 The @sc{gnu} utilities normally conform to the version of @acronym{POSIX}
1255 that is standard for your system.  To cause them to conform to a
1256 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
1257 environment variable to a value of the form @var{yyyymm} specifying
1258 the year and month the standard was adopted.  Two values are currently
1259 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
1260 @acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX}
1261 1003.1-2001.  For example, if you have a newer system but are running software
1262 that assumes an older version of @acronym{POSIX} and uses @samp{sort +1}
1263 or @samp{tail +10}, you can work around any compatibility problems by setting
1264 @samp{_POSIX2_VERSION=199209} in your environment.
1265
1266 @node Output of entire files
1267 @chapter Output of entire files
1268
1269 @cindex output of entire files
1270 @cindex entire files, output of
1271
1272 These commands read and write entire files, possibly transforming them
1273 in some way.
1274
1275 @menu
1276 * cat invocation::              Concatenate and write files.
1277 * tac invocation::              Concatenate and write files in reverse.
1278 * nl invocation::               Number lines and write files.
1279 * od invocation::               Write files in octal or other formats.
1280 * base64 invocation::           Transform data into printable data.
1281 @end menu
1282
1283 @node cat invocation
1284 @section @command{cat}: Concatenate and write files
1285
1286 @pindex cat
1287 @cindex concatenate and write files
1288 @cindex copying files
1289
1290 @command{cat} copies each @var{file} (@samp{-} means standard input), or
1291 standard input if none are given, to standard output.  Synopsis:
1292
1293 @example
1294 cat [@var{option}] [@var{file}]@dots{}
1295 @end example
1296
1297 The program accepts the following options.  Also see @ref{Common options}.
1298
1299 @table @samp
1300
1301 @item -A
1302 @itemx --show-all
1303 @opindex -A
1304 @opindex --show-all
1305 Equivalent to @option{-vET}.
1306
1307 @item -b
1308 @itemx --number-nonblank
1309 @opindex -b
1310 @opindex --number-nonblank
1311 Number all nonblank output lines, starting with 1.
1312
1313 @item -e
1314 @opindex -e
1315 Equivalent to @option{-vE}.
1316
1317 @item -E
1318 @itemx --show-ends
1319 @opindex -E
1320 @opindex --show-ends
1321 Display a @samp{$} after the end of each line.
1322
1323 @item -n
1324 @itemx --number
1325 @opindex -n
1326 @opindex --number
1327 Number all output lines, starting with 1.
1328
1329 @item -s
1330 @itemx --squeeze-blank
1331 @opindex -s
1332 @opindex --squeeze-blank
1333 @cindex squeezing blank lines
1334 Replace multiple adjacent blank lines with a single blank line.
1335
1336 @item -t
1337 @opindex -t
1338 Equivalent to @option{-vT}.
1339
1340 @item -T
1341 @itemx --show-tabs
1342 @opindex -T
1343 @opindex --show-tabs
1344 Display TAB characters as @samp{^I}.
1345
1346 @item -u
1347 @opindex -u
1348 Ignored; for @acronym{POSIX} compatibility.
1349
1350 @item -v
1351 @itemx --show-nonprinting
1352 @opindex -v
1353 @opindex --show-nonprinting
1354 Display control characters except for LFD and TAB using
1355 @samp{^} notation and precede characters that have the high bit set with
1356 @samp{M-}.
1357
1358 @end table
1359
1360 On systems like MS-DOS that distinguish between text and binary files,
1361 @command{cat} normally reads and writes in binary mode.  However,
1362 @command{cat} reads in text mode if one of the options
1363 @option{-bensAE} is used or if @command{cat} is reading from standard
1364 input and standard input is a terminal.  Similarly, @command{cat}
1365 writes in text mode if one of the options @option{-bensAE} is used or
1366 if standard output is a terminal.
1367
1368 @exitstatus
1369
1370 Examples:
1371
1372 @smallexample
1373 # Output f's contents, then standard input, then g's contents.
1374 cat f - g
1375
1376 # Copy standard input to standard output.
1377 cat
1378 @end smallexample
1379
1380
1381 @node tac invocation
1382 @section @command{tac}: Concatenate and write files in reverse
1383
1384 @pindex tac
1385 @cindex reversing files
1386
1387 @command{tac} copies each @var{file} (@samp{-} means standard input), or
1388 standard input if none are given, to standard output, reversing the
1389 records (lines by default) in each separately.  Synopsis:
1390
1391 @example
1392 tac [@var{option}]@dots{} [@var{file}]@dots{}
1393 @end example
1394
1395 @dfn{Records} are separated by instances of a string (newline by
1396 default).  By default, this separator string is attached to the end of
1397 the record that it follows in the file.
1398
1399 The program accepts the following options.  Also see @ref{Common options}.
1400
1401 @table @samp
1402
1403 @item -b
1404 @itemx --before
1405 @opindex -b
1406 @opindex --before
1407 The separator is attached to the beginning of the record that it
1408 precedes in the file.
1409
1410 @item -r
1411 @itemx --regex
1412 @opindex -r
1413 @opindex --regex
1414 Treat the separator string as a regular expression.  Users of @command{tac}
1415 on MS-DOS/MS-Windows should note that, since @command{tac} reads files in
1416 binary mode, each line of a text file might end with a CR/LF pair
1417 instead of the Unix-style LF.
1418
1419 @item -s @var{separator}
1420 @itemx --separator=@var{separator}
1421 @opindex -s
1422 @opindex --separator
1423 Use @var{separator} as the record separator, instead of newline.
1424
1425 @end table
1426
1427 @exitstatus
1428
1429
1430 @node nl invocation
1431 @section @command{nl}: Number lines and write files
1432
1433 @pindex nl
1434 @cindex numbering lines
1435 @cindex line numbering
1436
1437 @command{nl} writes each @var{file} (@samp{-} means standard input), or
1438 standard input if none are given, to standard output, with line numbers
1439 added to some or all of the lines.  Synopsis:
1440
1441 @example
1442 nl [@var{option}]@dots{} [@var{file}]@dots{}
1443 @end example
1444
1445 @cindex logical pages, numbering on
1446 @command{nl} decomposes its input into (logical) pages; by default, the
1447 line number is reset to 1 at the top of each logical page.  @command{nl}
1448 treats all of the input files as a single document; it does not reset
1449 line numbers or logical pages between files.
1450
1451 @cindex headers, numbering
1452 @cindex body, numbering
1453 @cindex footers, numbering
1454 A logical page consists of three sections: header, body, and footer.
1455 Any of the sections can be empty.  Each can be numbered in a different
1456 style from the others.
1457
1458 The beginnings of the sections of logical pages are indicated in the
1459 input file by a line containing exactly one of these delimiter strings:
1460
1461 @table @samp
1462 @item \:\:\:
1463 start of header;
1464 @item \:\:
1465 start of body;
1466 @item \:
1467 start of footer.
1468 @end table
1469
1470 The two characters from which these strings are made can be changed from
1471 @samp{\} and @samp{:} via options (see below), but the pattern and
1472 length of each string cannot be changed.
1473
1474 A section delimiter is replaced by an empty line on output.  Any text
1475 that comes before the first section delimiter string in the input file
1476 is considered to be part of a body section, so @command{nl} treats a
1477 file that contains no section delimiters as a single body section.
1478
1479 The program accepts the following options.  Also see @ref{Common options}.
1480
1481 @table @samp
1482
1483 @item -b @var{style}
1484 @itemx --body-numbering=@var{style}
1485 @opindex -b
1486 @opindex --body-numbering
1487 Select the numbering style for lines in the body section of each
1488 logical page.  When a line is not numbered, the current line number
1489 is not incremented, but the line number separator character is still
1490 prepended to the line.  The styles are:
1491
1492 @table @samp
1493 @item a
1494 number all lines,
1495 @item t
1496 number only nonempty lines (default for body),
1497 @item n
1498 do not number lines (default for header and footer),
1499 @item p@var{bre}
1500 number only lines that contain a match for the basic regular
1501 expression @var{bre}.
1502 @xref{Regular Expressions, , Regular Expressions, grep, The GNU Grep Manual}.
1503 @end table
1504
1505 @item -d @var{cd}
1506 @itemx --section-delimiter=@var{cd}
1507 @opindex -d
1508 @opindex --section-delimiter
1509 @cindex section delimiters of pages
1510 Set the section delimiter characters to @var{cd}; default is
1511 @samp{\:}.  If only @var{c} is given, the second remains @samp{:}.
1512 (Remember to protect @samp{\} or other metacharacters from shell
1513 expansion with quotes or extra backslashes.)
1514
1515 @item -f @var{style}
1516 @itemx --footer-numbering=@var{style}
1517 @opindex -f
1518 @opindex --footer-numbering
1519 Analogous to @option{--body-numbering}.
1520
1521 @item -h @var{style}
1522 @itemx --header-numbering=@var{style}
1523 @opindex -h
1524 @opindex --header-numbering
1525 Analogous to @option{--body-numbering}.
1526
1527 @item -i @var{number}
1528 @itemx --page-increment=@var{number}
1529 @opindex -i
1530 @opindex --page-increment
1531 Increment line numbers by @var{number} (default 1).
1532
1533 @item -l @var{number}
1534 @itemx --join-blank-lines=@var{number}
1535 @opindex -l
1536 @opindex --join-blank-lines
1537 @cindex empty lines, numbering
1538 @cindex blank lines, numbering
1539 Consider @var{number} (default 1) consecutive empty lines to be one
1540 logical line for numbering, and only number the last one.  Where fewer
1541 than @var{number} consecutive empty lines occur, do not number them.
1542 An empty line is one that contains no characters, not even spaces
1543 or tabs.
1544
1545 @item -n @var{format}
1546 @itemx --number-format=@var{format}
1547 @opindex -n
1548 @opindex --number-format
1549 Select the line numbering format (default is @code{rn}):
1550
1551 @table @samp
1552 @item ln
1553 @opindex ln @r{format for @command{nl}}
1554 left justified, no leading zeros;
1555 @item rn
1556 @opindex rn @r{format for @command{nl}}
1557 right justified, no leading zeros;
1558 @item rz
1559 @opindex rz @r{format for @command{nl}}
1560 right justified, leading zeros.
1561 @end table
1562
1563 @item -p
1564 @itemx --no-renumber
1565 @opindex -p
1566 @opindex --no-renumber
1567 Do not reset the line number at the start of a logical page.
1568
1569 @item -s @var{string}
1570 @itemx --number-separator=@var{string}
1571 @opindex -s
1572 @opindex --number-separator
1573 Separate the line number from the text line in the output with
1574 @var{string} (default is the TAB character).
1575
1576 @item -v @var{number}
1577 @itemx --starting-line-number=@var{number}
1578 @opindex -v
1579 @opindex --starting-line-number
1580 Set the initial line number on each logical page to @var{number} (default 1).
1581
1582 @item -w @var{number}
1583 @itemx --number-width=@var{number}
1584 @opindex -w
1585 @opindex --number-width
1586 Use @var{number} characters for line numbers (default 6).
1587
1588 @end table
1589
1590 @exitstatus
1591
1592
1593 @node od invocation
1594 @section @command{od}: Write files in octal or other formats
1595
1596 @pindex od
1597 @cindex octal dump of files
1598 @cindex hex dump of files
1599 @cindex ASCII dump of files
1600 @cindex file contents, dumping unambiguously
1601
1602 @command{od} writes an unambiguous representation of each @var{file}
1603 (@samp{-} means standard input), or standard input if none are given.
1604 Synopses:
1605
1606 @smallexample
1607 od [@var{option}]@dots{} [@var{file}]@dots{}
1608 od [-abcdfilosx]@dots{} [@var{file}] [[+]@var{offset}[.][b]]
1609 od [@var{option}]@dots{} --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1610 @end smallexample
1611
1612 Each line of output consists of the offset in the input, followed by
1613 groups of data from the file.  By default, @command{od} prints the offset in
1614 octal, and each group of file data is a C @code{short int}'s worth of input
1615 printed as a single octal number.
1616
1617 If @var{offset} is given, it specifies how many input bytes to skip
1618 before formatting and writing.  By default, it is interpreted as an
1619 octal number, but the optional trailing decimal point causes it to be
1620 interpreted as decimal.  If no decimal is specified and the offset
1621 begins with @samp{0x} or @samp{0X} it is interpreted as a hexadecimal
1622 number.  If there is a trailing @samp{b}, the number of bytes skipped
1623 will be @var{offset} multiplied by 512.
1624
1625 If a command is of both the first and second forms, the second form is
1626 assumed if the last operand begins with @samp{+} or (if there are two
1627 operands) a digit.  For example, in @samp{od foo 10} and @samp{od +10}
1628 the @samp{10} is an offset, whereas in @samp{od 10} the @samp{10} is a
1629 file name.
1630
1631 The program accepts the following options.  Also see @ref{Common options}.
1632
1633 @table @samp
1634
1635 @item -A @var{radix}
1636 @itemx --address-radix=@var{radix}
1637 @opindex -A
1638 @opindex --address-radix
1639 @cindex radix for file offsets
1640 @cindex file offset radix
1641 Select the base in which file offsets are printed.  @var{radix} can
1642 be one of the following:
1643
1644 @table @samp
1645 @item d
1646 decimal;
1647 @item o
1648 octal;
1649 @item x
1650 hexadecimal;
1651 @item n
1652 none (do not print offsets).
1653 @end table
1654
1655 The default is octal.
1656
1657 @item -j @var{bytes}
1658 @itemx --skip-bytes=@var{bytes}
1659 @opindex -j
1660 @opindex --skip-bytes
1661 Skip @var{bytes} input bytes before formatting and writing.  If
1662 @var{bytes} begins with @samp{0x} or @samp{0X}, it is interpreted in
1663 hexadecimal; otherwise, if it begins with @samp{0}, in octal; otherwise,
1664 in decimal.  Appending @samp{b} multiplies @var{bytes} by 512, @samp{k}
1665 by 1024, and @samp{m} by 1048576.
1666
1667 @item -N @var{bytes}
1668 @itemx --read-bytes=@var{bytes}
1669 @opindex -N
1670 @opindex --read-bytes
1671 Output at most @var{bytes} bytes of the input.  Prefixes and suffixes on
1672 @code{bytes} are interpreted as for the @option{-j} option.
1673
1674 @item -S @var{n}
1675 @itemx --strings[=@var{n}]
1676 @opindex -S
1677 @opindex --strings
1678 @cindex string constants, outputting
1679 Instead of the normal output, output only @dfn{string constants}: at
1680 least @var{n} consecutive @acronym{ASCII} graphic characters,
1681 followed by a null (zero) byte.
1682
1683 If @var{n} is omitted with @option{--strings}, the default is 3.
1684
1685 @item -t @var{type}
1686 @itemx --format=@var{type}
1687 @opindex -t
1688 @opindex --format
1689 Select the format in which to output the file data.  @var{type} is a
1690 string of one or more of the below type indicator characters.  If you
1691 include more than one type indicator character in a single @var{type}
1692 string, or use this option more than once, @command{od} writes one copy
1693 of each output line using each of the data types that you specified,
1694 in the order that you specified.
1695
1696 Adding a trailing ``z'' to any type specification appends a display
1697 of the @acronym{ASCII} character representation of the printable characters
1698 to the output line generated by the type specification.
1699
1700 @table @samp
1701 @item a
1702 named character, ignoring high-order bit
1703 @item c
1704 @acronym{ASCII} character or backslash escape,
1705 @item d
1706 signed decimal
1707 @item f
1708 floating point
1709 @item o
1710 octal
1711 @item u
1712 unsigned decimal
1713 @item x
1714 hexadecimal
1715 @end table
1716
1717 The type @code{a} outputs things like @samp{sp} for space, @samp{nl} for
1718 newline, and @samp{nul} for a null (zero) byte.  Only the least significant
1719 seven bits of each byte is used; the high-order bit is ignored.
1720 Type @code{c} outputs
1721 @samp{ }, @samp{\n}, and @code{\0}, respectively.
1722
1723 @cindex type size
1724 Except for types @samp{a} and @samp{c}, you can specify the number
1725 of bytes to use in interpreting each number in the given data type
1726 by following the type indicator character with a decimal integer.
1727 Alternately, you can specify the size of one of the C compiler's
1728 built-in data types by following the type indicator character with
1729 one of the following characters.  For integers (@samp{d}, @samp{o},
1730 @samp{u}, @samp{x}):
1731
1732 @table @samp
1733 @item C
1734 char
1735 @item S
1736 short
1737 @item I
1738 int
1739 @item L
1740 long
1741 @end table
1742
1743 For floating point (@code{f}):
1744
1745 @table @asis
1746 @item F
1747 float
1748 @item D
1749 double
1750 @item L
1751 long double
1752 @end table
1753
1754 @item -v
1755 @itemx --output-duplicates
1756 @opindex -v
1757 @opindex --output-duplicates
1758 Output consecutive lines that are identical.  By default, when two or
1759 more consecutive output lines would be identical, @command{od} outputs only
1760 the first line, and puts just an asterisk on the following line to
1761 indicate the elision.
1762
1763 @item -w[@var{n}]
1764 @itemx --width[=@var{n}]
1765 @opindex -w
1766 @opindex --width
1767 Dump @code{n} input bytes per output line.  This must be a multiple of
1768 the least common multiple of the sizes associated with the specified
1769 output types.
1770
1771 If this option is not given at all, the default is 16.  If @var{n} is
1772 omitted, the default is 32.
1773
1774 @end table
1775
1776 The next several options are shorthands for format specifications.
1777 @sc{gnu} @command{od} accepts any combination of shorthands and format
1778 specification options.  These options accumulate.
1779
1780 @table @samp
1781
1782 @item -a
1783 @opindex -a
1784 Output as named characters.  Equivalent to @samp{-t a}.
1785
1786 @item -b
1787 @opindex -b
1788 Output as octal bytes.  Equivalent to @samp{-t o1}.
1789
1790 @item -c
1791 @opindex -c
1792 Output as @acronym{ASCII} characters or backslash escapes.  Equivalent to
1793 @samp{-t c}.
1794
1795 @item -d
1796 @opindex -d
1797 Output as unsigned decimal two-byte units.  Equivalent to @samp{-t u2}.
1798
1799 @item -f
1800 @opindex -f
1801 Output as floats.  Equivalent to @samp{-t fF}.
1802
1803 @item -i
1804 @opindex -i
1805 Output as decimal ints.  Equivalent to @samp{-t dI}.
1806
1807 @item -l
1808 @opindex -l
1809 Output as decimal long ints.  Equivalent to @samp{-t dL}.
1810
1811 @item -o
1812 @opindex -o
1813 Output as octal two-byte units.  Equivalent to @option{-t o2}.
1814
1815 @item -s
1816 @opindex -s
1817 Output as decimal two-byte units.  Equivalent to @option{-t d2}.
1818
1819 @item -x
1820 @opindex -x
1821 Output as hexadecimal two-byte units.  Equivalent to @samp{-t x2}.
1822
1823 @item --traditional
1824 @opindex --traditional
1825 Recognize the non-option label argument that traditional @command{od}
1826 accepted.  The following syntax:
1827
1828 @smallexample
1829 od --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1830 @end smallexample
1831
1832 @noindent
1833 can be used to specify at most one file and optional arguments
1834 specifying an offset and a pseudo-start address, @var{label}.
1835 The @var{label} argument is interpreted
1836 just like @var{offset}, but it specifies an initial pseudo-address.  The
1837 pseudo-addresses are displayed in parentheses following any normal
1838 address.
1839
1840 @end table
1841
1842 @exitstatus
1843
1844 @node base64 invocation
1845 @section @command{base64}: Transform data into printable data.
1846
1847 @pindex base64
1848 @cindex base64 encoding
1849
1850 @command{base64} transforms data read from a file, or standard input,
1851 into (or from) base64 encoded form.  The base64 encoded form uses
1852 printable @acronym{ASCII} characters to represent binary data, see
1853 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3548.txt, RFC 3548}.
1854 Synopses:
1855
1856 @smallexample
1857 base64 [@var{option}]@dots{} [@var{file}]
1858 base64 --decode [@var{option}]@dots{} [@var{file}]
1859 @end smallexample
1860
1861 The base64 encoding expands data to roughly 133% of the original.
1862
1863 The program accepts the following options.  Also see @ref{Common options}.
1864
1865 @table @samp
1866
1867 @item -w @var{COLS}
1868 @itemx --wrap=@var{COLS}
1869 @opindex -w
1870 @opindex --wrap
1871 @cindex wrap data
1872 @cindex column to wrap data after
1873 During encoding, wrap lines after @var{COLS} characters.  This must be
1874 a positive number.
1875
1876 The default is to wrap after 76 characters.  Use the value 0 to
1877 disable line wrapping altogether.
1878
1879 @item -d
1880 @itemx --decode
1881 @opindex -d
1882 @opindex --decode
1883 @cindex Decode base64 data
1884 @cindex Base64 decoding
1885 Change the mode of operation, from the default of encoding data, to
1886 decoding data.  Input is expected to be base64 encoded data, and the
1887 output will be the original data.
1888
1889 @item -i
1890 @itemx --ignore-garbage
1891 @opindex -i
1892 @opindex --ignore-garbage
1893 @cindex Ignore garbage in base64 stream
1894 When decoding, newlines are always accepted.
1895 During decoding, ignore unrecognized bytes,
1896 to permit distorted data to be decoded.
1897
1898 @end table
1899
1900 @exitstatus
1901
1902
1903 @node Formatting file contents
1904 @chapter Formatting file contents
1905
1906 @cindex formatting file contents
1907
1908 These commands reformat the contents of files.
1909
1910 @menu
1911 * fmt invocation::              Reformat paragraph text.
1912 * pr invocation::               Paginate or columnate files for printing.
1913 * fold invocation::             Wrap input lines to fit in specified width.
1914 @end menu
1915
1916
1917 @node fmt invocation
1918 @section @command{fmt}: Reformat paragraph text
1919
1920 @pindex fmt
1921 @cindex reformatting paragraph text
1922 @cindex paragraphs, reformatting
1923 @cindex text, reformatting
1924
1925 @command{fmt} fills and joins lines to produce output lines of (at most)
1926 a given number of characters (75 by default).  Synopsis:
1927
1928 @example
1929 fmt [@var{option}]@dots{} [@var{file}]@dots{}
1930 @end example
1931
1932 @command{fmt} reads from the specified @var{file} arguments (or standard
1933 input if none are given), and writes to standard output.
1934
1935 By default, blank lines, spaces between words, and indentation are
1936 preserved in the output; successive input lines with different
1937 indentation are not joined; tabs are expanded on input and introduced on
1938 output.
1939
1940 @cindex line-breaking
1941 @cindex sentences and line-breaking
1942 @cindex Knuth, Donald E.
1943 @cindex Plass, Michael F.
1944 @command{fmt} prefers breaking lines at the end of a sentence, and tries to
1945 avoid line breaks after the first word of a sentence or before the last
1946 word of a sentence.  A @dfn{sentence break} is defined as either the end
1947 of a paragraph or a word ending in any of @samp{.?!}, followed by two
1948 spaces or end of line, ignoring any intervening parentheses or quotes.
1949 Like @TeX{}, @command{fmt} reads entire ``paragraphs'' before choosing line
1950 breaks; the algorithm is a variant of that given by Donald E. Knuth
1951 and Michael F. Plass in ``Breaking Paragraphs Into Lines'',
1952 @cite{Software---Practice & Experience} @b{11}, 11 (November 1981),
1953 1119--1184.
1954
1955 The program accepts the following options.  Also see @ref{Common options}.
1956
1957 @table @samp
1958
1959 @item -c
1960 @itemx --crown-margin
1961 @opindex -c
1962 @opindex --crown-margin
1963 @cindex crown margin
1964 @dfn{Crown margin} mode: preserve the indentation of the first two
1965 lines within a paragraph, and align the left margin of each subsequent
1966 line with that of the second line.
1967
1968 @item -t
1969 @itemx --tagged-paragraph
1970 @opindex -t
1971 @opindex --tagged-paragraph
1972 @cindex tagged paragraphs
1973 @dfn{Tagged paragraph} mode: like crown margin mode, except that if
1974 indentation of the first line of a paragraph is the same as the
1975 indentation of the second, the first line is treated as a one-line
1976 paragraph.
1977
1978 @item -s
1979 @itemx --split-only
1980 @opindex -s
1981 @opindex --split-only
1982 Split lines only.  Do not join short lines to form longer ones.  This
1983 prevents sample lines of code, and other such ``formatted'' text from
1984 being unduly combined.
1985
1986 @item -u
1987 @itemx --uniform-spacing
1988 @opindex -u
1989 @opindex --uniform-spacing
1990 Uniform spacing.  Reduce spacing between words to one space, and spacing
1991 between sentences to two spaces.
1992
1993 @item -@var{width}
1994 @itemx -w @var{width}
1995 @itemx --width=@var{width}
1996 @opindex -@var{width}
1997 @opindex -w
1998 @opindex --width
1999 Fill output lines up to @var{width} characters (default 75).  @command{fmt}
2000 initially tries to make lines about 7% shorter than this, to give it
2001 room to balance line lengths.
2002
2003 @item -p @var{prefix}
2004 @itemx --prefix=@var{prefix}
2005 Only lines beginning with @var{prefix} (possibly preceded by whitespace)
2006 are subject to formatting.  The prefix and any preceding whitespace are
2007 stripped for the formatting and then re-attached to each formatted output
2008 line.  One use is to format certain kinds of program comments, while
2009 leaving the code unchanged.
2010
2011 @end table
2012
2013 @exitstatus
2014
2015
2016 @node pr invocation
2017 @section @command{pr}: Paginate or columnate files for printing
2018
2019 @pindex pr
2020 @cindex printing, preparing files for
2021 @cindex multicolumn output, generating
2022 @cindex merging files in parallel
2023
2024 @command{pr} writes each @var{file} (@samp{-} means standard input), or
2025 standard input if none are given, to standard output, paginating and
2026 optionally outputting in multicolumn format; optionally merges all
2027 @var{file}s, printing all in parallel, one per column.  Synopsis:
2028
2029 @example
2030 pr [@var{option}]@dots{} [@var{file}]@dots{}
2031 @end example
2032
2033 @vindex LC_MESSAGES
2034 By default, a 5-line header is printed at each page: two blank lines;
2035 a line with the date, the file name, and the page count; and two more
2036 blank lines.  A footer of five blank lines is also printed.
2037 With the @option{-F}
2038 option, a 3-line header is printed: the leading two blank lines are
2039 omitted; no footer is used.  The default @var{page_length} in both cases is 66
2040 lines.  The default number of text lines changes from 56 (without @option{-F})
2041 to 63 (with @option{-F}).  The text line of the header takes the form
2042 @samp{@var{date} @var{string} @var{page}}, with spaces inserted around
2043 @var{string} so that the line takes up the full @var{page_width}.  Here,
2044 @var{date} is the date (see the @option{-D} or @option{--date-format}
2045 option for details), @var{string} is the centered header string, and
2046 @var{page} identifies the page number.  The @env{LC_MESSAGES} locale
2047 category affects the spelling of @var{page}; in the default C locale, it
2048 is @samp{Page @var{number}} where @var{number} is the decimal page
2049 number.
2050
2051 Form feeds in the input cause page breaks in the output.  Multiple form
2052 feeds produce empty pages.
2053
2054 Columns are of equal width, separated by an optional string (default
2055 is @samp{space}).  For multicolumn output, lines will always be truncated to
2056 @var{page_width} (default 72), unless you use the @option{-J} option.
2057 For single
2058 column output no line truncation occurs by default.  Use @option{-W} option to
2059 truncate lines in that case.
2060
2061 The following changes were made in version 1.22i and apply to later
2062 versions of @command{pr}:
2063 @c FIXME: this whole section here sounds very awkward to me. I
2064 @c made a few small changes, but really it all needs to be redone. - Brian
2065 @c OK, I fixed another sentence or two, but some of it I just don't understand.
2066 @ - Brian
2067 @itemize @bullet
2068
2069 @item
2070 Some small @var{letter options} (@option{-s}, @option{-w}) have been
2071 redefined for better @acronym{POSIX} compliance.  The output of some further
2072 cases has been adapted to other Unix systems.  These changes are not
2073 compatible with earlier versions of the program.
2074
2075 @item
2076 Some @var{new capital letter} options (@option{-J}, @option{-S}, @option{-W})
2077 have been introduced to turn off unexpected interferences of small letter
2078 options.  The @option{-N} option and the second argument @var{last_page}
2079 of @samp{+FIRST_PAGE} offer more flexibility.  The detailed handling of
2080 form feeds set in the input files requires the @option{-T} option.
2081
2082 @item
2083 Capital letter options override small letter ones.
2084
2085 @item
2086 Some of the option-arguments (compare @option{-s}, @option{-e},
2087 @option{-i}, @option{-n}) cannot be specified as separate arguments from the
2088 preceding option letter (already stated in the @acronym{POSIX} specification).
2089 @end itemize
2090
2091 The program accepts the following options.  Also see @ref{Common options}.
2092
2093 @table @samp
2094
2095 @item +@var{first_page}[:@var{last_page}]
2096 @itemx --pages=@var{first_page}[:@var{last_page}]
2097 @c The two following @opindex lines evoke warnings because they contain `:'
2098 @c The `info' spec does not permit that.  If we use those lines, we end
2099 @c up with truncated index entries that don't work.
2100 @c @opindex +@var{first_page}[:@var{last_page}]
2101 @c @opindex --pages=@var{first_page}[:@var{last_page}]
2102 @opindex +@var{page_range}
2103 @opindex --pages=@var{page_range}
2104 Begin printing with page @var{first_page} and stop with @var{last_page}.
2105 Missing @samp{:@var{last_page}} implies end of file.  While estimating
2106 the number of skipped pages each form feed in the input file results
2107 in a new page.  Page counting with and without @samp{+@var{first_page}}
2108 is identical.  By default, counting starts with the first page of input
2109 file (not first page printed).  Line numbering may be altered by @option{-N}
2110 option.
2111
2112 @item -@var{column}
2113 @itemx --columns=@var{column}
2114 @opindex -@var{column}
2115 @opindex --columns
2116 @cindex down columns
2117 With each single @var{file}, produce @var{column} columns of output
2118 (default is 1) and print columns down, unless @option{-a} is used.  The
2119 column width is automatically decreased as @var{column} increases; unless
2120 you use the @option{-W/-w} option to increase @var{page_width} as well.
2121 This option might well cause some lines to be truncated.  The number of
2122 lines in the columns on each page are balanced.  The options @option{-e}
2123 and @option{-i} are on for multiple text-column output.  Together with
2124 @option{-J} option column alignment and line truncation is turned off.
2125 Lines of full length are joined in a free field format and @option{-S}
2126 option may set field separators.  @option{-@var{column}} may not be used
2127 with @option{-m} option.
2128
2129 @item -a
2130 @itemx --across
2131 @opindex -a
2132 @opindex --across
2133 @cindex across columns
2134 With each single @var{file}, print columns across rather than down.  The
2135 @option{-@var{column}} option must be given with @var{column} greater than one.
2136 If a line is too long to fit in a column, it is truncated.
2137
2138 @item -c
2139 @itemx --show-control-chars
2140 @opindex -c
2141 @opindex --show-control-chars
2142 Print control characters using hat notation (e.g., @samp{^G}); print
2143 other nonprinting characters in octal backslash notation.  By default,
2144 nonprinting characters are not changed.
2145
2146 @item -d
2147 @itemx --double-space
2148 @opindex -d
2149 @opindex --double-space
2150 @cindex double spacing
2151 Double space the output.
2152
2153 @item -D @var{format}
2154 @itemx --date-format=@var{format}
2155 @cindex time formats
2156 @cindex formatting times
2157 Format header dates using @var{format}, using the same conventions as
2158 for the command @samp{date +@var{format}}; @xref{date invocation}.
2159 Except for directives, which start with
2160 @samp{%}, characters in @var{format} are printed unchanged.  You can use
2161 this option to specify an arbitrary string in place of the header date,
2162 e.g., @option{--date-format="Monday morning"}.
2163
2164 @vindex POSIXLY_CORRECT
2165 @vindex LC_TIME
2166 Normally the date
2167 format defaults to @samp{%Y-%m-%d %H:%M} (for example, @samp{2001-12-04
2168 23:59}); but if the @env{POSIXLY_CORRECT} environment variable is set
2169 and the @env{LC_TIME} locale category specifies the @acronym{POSIX}
2170 locale, the default is @samp{%b %e %H:%M %Y} (for example,
2171 @samp{Dec@ @ 4 23:59 2001}.
2172
2173 @vindex TZ
2174 Time stamps are listed according to the time zone rules specified by
2175 the @env{TZ} environment variable, or by the system default rules if
2176 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
2177 with @env{TZ}, libc, The GNU C Library}.
2178
2179 @item -e[@var{in-tabchar}[@var{in-tabwidth}]]
2180 @itemx --expand-tabs[=@var{in-tabchar}[@var{in-tabwidth}]]
2181 @opindex -e
2182 @opindex --expand-tabs
2183 @cindex input tabs
2184 Expand @var{tab}s to spaces on input.  Optional argument @var{in-tabchar} is
2185 the input tab character (default is the TAB character).  Second optional
2186 argument @var{in-tabwidth} is the input tab character's width (default
2187 is 8).
2188
2189 @item -f
2190 @itemx -F
2191 @itemx --form-feed
2192 @opindex -F
2193 @opindex -f
2194 @opindex --form-feed
2195 Use a form feed instead of newlines to separate output pages.  The default
2196 page length of 66 lines is not altered.  But the number of lines of text
2197 per page changes from default 56 to 63 lines.
2198
2199 @item -h @var{HEADER}
2200 @itemx --header=@var{HEADER}
2201 @opindex -h
2202 @opindex --header
2203 Replace the file name in the header with the centered string @var{header}.
2204 When using the shell, @var{header} should be quoted and should be
2205 separated from @option{-h} by a space.
2206
2207 @item -i[@var{out-tabchar}[@var{out-tabwidth}]]
2208 @itemx --output-tabs[=@var{out-tabchar}[@var{out-tabwidth}]]
2209 @opindex -i
2210 @opindex --output-tabs
2211 @cindex output tabs
2212 Replace spaces with @var{tab}s on output.  Optional argument @var{out-tabchar}
2213 is the output tab character (default is the TAB character).  Second optional
2214 argument @var{out-tabwidth} is the output tab character's width (default
2215 is 8).
2216
2217 @item -J
2218 @itemx --join-lines
2219 @opindex -J
2220 @opindex --join-lines
2221 Merge lines of full length.  Used together with the column options
2222 @option{-@var{column}}, @option{-a -@var{column}} or @option{-m}.  Turns off
2223 @option{-W/-w} line truncation;
2224 no column alignment used; may be used with
2225 @option{--sep-string[=@var{string}]}.  @option{-J} has been introduced
2226 (together with @option{-W} and @option{--sep-string})
2227 to disentangle the old (@acronym{POSIX}-compliant) options @option{-w} and
2228 @option{-s} along with the three column options.
2229
2230
2231 @item -l @var{page_length}
2232 @itemx --length=@var{page_length}
2233 @opindex -l
2234 @opindex --length
2235 Set the page length to @var{page_length} (default 66) lines, including
2236 the lines of the header [and the footer].  If @var{page_length} is less
2237 than or equal to 10 (or <= 3 with @option{-F}), the header and footer are
2238 omitted, and all form feeds set in input files are eliminated, as if
2239 the @option{-T} option had been given.
2240
2241 @item -m
2242 @itemx --merge
2243 @opindex -m
2244 @opindex --merge
2245 Merge and print all @var{file}s in parallel, one in each column.  If a
2246 line is too long to fit in a column, it is truncated, unless the @option{-J}
2247 option is used.  @option{--sep-string[=@var{string}]} may be used.
2248 Empty pages in
2249 some @var{file}s (form feeds set) produce empty columns, still marked
2250 by @var{string}.  The result is a continuous line numbering and column
2251 marking throughout the whole merged file.  Completely empty merged pages
2252 show no separators or line numbers.  The default header becomes
2253 @samp{@var{date} @var{page}} with spaces inserted in the middle; this
2254 may be used with the @option{-h} or @option{--header} option to fill up
2255 the middle blank part.
2256
2257 @item -n[@var{number-separator}[@var{digits}]]
2258 @itemx --number-lines[=@var{number-separator}[@var{digits}]]
2259 @opindex -n
2260 @opindex --number-lines
2261 Provide @var{digits} digit line numbering (default for @var{digits} is
2262 5).  With multicolumn output the number occupies the first @var{digits}
2263 column positions of each text column or only each line of @option{-m}
2264 output.  With single column output the number precedes each line just as
2265 @option{-m} does.  Default counting of the line numbers starts with the
2266 first line of the input file (not the first line printed, compare the
2267 @option{--page} option and @option{-N} option).
2268 Optional argument @var{number-separator} is the character appended to
2269 the line number to separate it from the text followed.  The default
2270 separator is the TAB character.  In a strict sense a TAB is always
2271 printed with single column output only.  The @var{TAB}-width varies
2272 with the @var{TAB}-position, e.g., with the left @var{margin} specified
2273 by @option{-o} option.  With multicolumn output priority is given to
2274 @samp{equal width of output columns} (a @acronym{POSIX} specification).
2275 The @var{TAB}-width is fixed to the value of the first column and does
2276 not change with different values of left @var{margin}.  That means a
2277 fixed number of spaces is always printed in the place of the
2278 @var{number-separator tab}.  The tabification depends upon the output
2279 position.
2280
2281 @item -N @var{line_number}
2282 @itemx --first-line-number=@var{line_number}
2283 @opindex -N
2284 @opindex --first-line-number
2285 Start line counting with the number @var{line_number} at first line of
2286 first page printed (in most cases not the first line of the input file).
2287
2288 @item -o @var{margin}
2289 @itemx --indent=@var{margin}
2290 @opindex -o
2291 @opindex --indent
2292 @cindex indenting lines
2293 @cindex left margin
2294 Indent each line with a margin @var{margin} spaces wide (default is zero).
2295 The total page width is the size of the margin plus the @var{page_width}
2296 set with the @option{-W/-w} option.  A limited overflow may occur with
2297 numbered single column output (compare @option{-n} option).
2298
2299 @item -r
2300 @itemx --no-file-warnings
2301 @opindex -r
2302 @opindex --no-file-warnings
2303 Do not print a warning message when an argument @var{file} cannot be
2304 opened.  (The exit status will still be nonzero, however.)
2305
2306 @item -s[@var{char}]
2307 @itemx --separator[=@var{char}]
2308 @opindex -s
2309 @opindex --separator
2310 Separate columns by a single character @var{char}.  The default for
2311 @var{char} is the TAB character without @option{-w} and @samp{no
2312 character} with @option{-w}.  Without @option{-s} the default separator
2313 @samp{space} is set.  @option{-s[char]} turns off line truncation of all
2314 three column options (@option{-COLUMN}|@option{-a -COLUMN}|@option{-m}) unless
2315 @option{-w} is set.  This is a @acronym{POSIX}-compliant formulation.
2316
2317
2318 @item -S@var{string}
2319 @itemx --sep-string[=@var{string}]
2320 @opindex -S
2321 @opindex --sep-string
2322 Use @var{string} to separate output columns.  The @option{-S} option doesn't
2323 affect the @option{-W/-w} option, unlike the @option{-s} option which does.  It
2324 does not affect line truncation or column alignment.
2325 Without @option{-S}, and with @option{-J}, @command{pr} uses the default output
2326 separator, TAB@.
2327 Without @option{-S} or @option{-J}, @command{pr} uses a @samp{space}
2328 (same as @option{-S"@w{ }"}).  @option{--sep-string} with no
2329 @samp{=@var{string}} is equivalent to @option{--sep-string=""}.
2330
2331 @item -t
2332 @itemx --omit-header
2333 @opindex -t
2334 @opindex --omit-header
2335 Do not print the usual header [and footer] on each page, and do not fill
2336 out the bottom of pages (with blank lines or a form feed).  No page
2337 structure is produced, but form feeds set in the input files are retained.
2338 The predefined pagination is not changed.  @option{-t} or @option{-T} may be
2339 useful together with other options; e.g.: @option{-t -e4}, expand TAB characters
2340 in the input file to 4 spaces but don't make any other changes.  Use of
2341 @option{-t} overrides @option{-h}.
2342
2343 @item -T
2344 @itemx --omit-pagination
2345 @opindex -T
2346 @opindex --omit-pagination
2347 Do not print header [and footer].  In addition eliminate all form feeds
2348 set in the input files.
2349
2350 @item -v
2351 @itemx --show-nonprinting
2352 @opindex -v
2353 @opindex --show-nonprinting
2354 Print nonprinting characters in octal backslash notation.
2355
2356 @item -w @var{page_width}
2357 @itemx --width=@var{page_width}
2358 @opindex -w
2359 @opindex --width
2360 Set page width to @var{page_width} characters for multiple text-column
2361 output only (default for @var{page_width} is 72).  @option{-s[CHAR]} turns
2362 off the default page width and any line truncation and column alignment.
2363 Lines of full length are merged, regardless of the column options
2364 set.  No @var{page_width} setting is possible with single column output.
2365 A @acronym{POSIX}-compliant formulation.
2366
2367 @item -W @var{page_width}
2368 @itemx --page_width=@var{page_width}
2369 @opindex -W
2370 @opindex --page_width
2371 Set the page width to @var{page_width} characters.  That's valid with and
2372 without a column option.  Text lines are truncated, unless @option{-J}
2373 is used.  Together with one of the three column options
2374 (@option{-@var{column}}, @option{-a -@var{column}} or @option{-m}) column
2375 alignment is always used.  The separator options @option{-S} or @option{-s}
2376 don't affect the @option{-W} option.  Default is 72 characters.  Without
2377 @option{-W @var{page_width}} and without any of the column options NO line
2378 truncation is used (defined to keep downward compatibility and to meet
2379 most frequent tasks).  That's equivalent to @option{-W 72 -J}.  The header
2380 line is never truncated.
2381
2382 @end table
2383
2384 @exitstatus
2385
2386
2387 @node fold invocation
2388 @section @command{fold}: Wrap input lines to fit in specified width
2389
2390 @pindex fold
2391 @cindex wrapping long input lines
2392 @cindex folding long input lines
2393
2394 @command{fold} writes each @var{file} (@option{-} means standard input), or
2395 standard input if none are given, to standard output, breaking long
2396 lines.  Synopsis:
2397
2398 @example
2399 fold [@var{option}]@dots{} [@var{file}]@dots{}
2400 @end example
2401
2402 By default, @command{fold} breaks lines wider than 80 columns.  The output
2403 is split into as many lines as necessary.
2404
2405 @cindex screen columns
2406 @command{fold} counts screen columns by default; thus, a tab may count more
2407 than one column, backspace decreases the column count, and carriage
2408 return sets the column to zero.
2409
2410 The program accepts the following options.  Also see @ref{Common options}.
2411
2412 @table @samp
2413
2414 @item -b
2415 @itemx --bytes
2416 @opindex -b
2417 @opindex --bytes
2418 Count bytes rather than columns, so that tabs, backspaces, and carriage
2419 returns are each counted as taking up one column, just like other
2420 characters.
2421
2422 @item -s
2423 @itemx --spaces
2424 @opindex -s
2425 @opindex --spaces
2426 Break at word boundaries: the line is broken after the last blank before
2427 the maximum line length.  If the line contains no such blanks, the line
2428 is broken at the maximum line length as usual.
2429
2430 @item -w @var{width}
2431 @itemx --width=@var{width}
2432 @opindex -w
2433 @opindex --width
2434 Use a maximum line length of @var{width} columns instead of 80.
2435
2436 For compatibility @command{fold} supports an obsolete option syntax
2437 @option{-@var{width}}.  New scripts should use @option{-w @var{width}}
2438 instead.
2439
2440 @end table
2441
2442 @exitstatus
2443
2444
2445 @node Output of parts of files
2446 @chapter Output of parts of files
2447
2448 @cindex output of parts of files
2449 @cindex parts of files, output of
2450
2451 These commands output pieces of the input.
2452
2453 @menu
2454 * head invocation::             Output the first part of files.
2455 * tail invocation::             Output the last part of files.
2456 * split invocation::            Split a file into fixed-size pieces.
2457 * csplit invocation::           Split a file into context-determined pieces.
2458 @end menu
2459
2460 @node head invocation
2461 @section @command{head}: Output the first part of files
2462
2463 @pindex head
2464 @cindex initial part of files, outputting
2465 @cindex first part of files, outputting
2466
2467 @command{head} prints the first part (10 lines by default) of each
2468 @var{file}; it reads from standard input if no files are given or
2469 when given a @var{file} of @option{-}.  Synopsis:
2470
2471 @example
2472 head [@var{option}]@dots{} [@var{file}]@dots{}
2473 @end example
2474
2475 If more than one @var{file} is specified, @command{head} prints a
2476 one-line header consisting of:
2477
2478 @example
2479 ==> @var{file name} <==
2480 @end example
2481
2482 @noindent
2483 before the output for each @var{file}.
2484
2485 The program accepts the following options.  Also see @ref{Common options}.
2486
2487 @table @samp
2488
2489 @item -c @var{n}
2490 @itemx --bytes=@var{n}
2491 @opindex -c
2492 @opindex --bytes
2493 Print the first @var{n} bytes, instead of initial lines.  Appending
2494 @samp{b} multiplies @var{n} by 512, @samp{k} by 1024, and @samp{m}
2495 by 1048576.
2496 However, if @var{n} starts with a @samp{-},
2497 print all but the last @var{n} bytes of each file.
2498
2499 @itemx -n @var{n}
2500 @itemx --lines=@var{n}
2501 @opindex -n
2502 @opindex --lines
2503 Output the first @var{n} lines.
2504 However, if @var{n} starts with a @samp{-},
2505 print all but the last @var{n} lines of each file.
2506
2507 @item -q
2508 @itemx --quiet
2509 @itemx --silent
2510 @opindex -q
2511 @opindex --quiet
2512 @opindex --silent
2513 Never print file name headers.
2514
2515 @item -v
2516 @itemx --verbose
2517 @opindex -v
2518 @opindex --verbose
2519 Always print file name headers.
2520
2521 @end table
2522
2523 For compatibility @command{head} also supports an obsolete option syntax
2524 @option{-@var{count}@var{options}}, which is recognized only if it is
2525 specified first.  @var{count} is a decimal number optionally followed
2526 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
2527 @samp{l} to mean count by lines, or other option letters (@samp{cqv}).
2528 Scripts intended for standard hosts should use @option{-c @var{count}}
2529 or @option{-n @var{count}} instead.  If your script must also run on
2530 hosts that support only the obsolete syntax, it is usually simpler to
2531 avoid @command{head}, e.g., by using @samp{sed 5q} instead of
2532 @samp{head -5}.
2533
2534 @exitstatus
2535
2536
2537 @node tail invocation
2538 @section @command{tail}: Output the last part of files
2539
2540 @pindex tail
2541 @cindex last part of files, outputting
2542
2543 @command{tail} prints the last part (10 lines by default) of each
2544 @var{file}; it reads from standard input if no files are given or
2545 when given a @var{file} of @samp{-}.  Synopsis:
2546
2547 @example
2548 tail [@var{option}]@dots{} [@var{file}]@dots{}
2549 @end example
2550
2551 If more than one @var{file} is specified, @command{tail} prints a
2552 one-line header consisting of:
2553
2554 @example
2555 ==> @var{file name} <==
2556 @end example
2557
2558 @noindent
2559 before the output for each @var{file}.
2560
2561 @cindex BSD @command{tail}
2562 @sc{gnu} @command{tail} can output any amount of data (some other versions of
2563 @command{tail} cannot).  It also has no @option{-r} option (print in
2564 reverse), since reversing a file is really a different job from printing
2565 the end of a file; BSD @command{tail} (which is the one with @option{-r}) can
2566 only reverse files that are at most as large as its buffer, which is
2567 typically 32 KiB@.  A more reliable and versatile way to reverse files is
2568 the @sc{gnu} @command{tac} command.
2569
2570 If any option-argument is a number @var{n} starting with a @samp{+},
2571 @command{tail} begins printing with the @var{n}th item from the start of
2572 each file, instead of from the end.
2573
2574 The program accepts the following options.  Also see @ref{Common options}.
2575
2576 @table @samp
2577
2578 @item -c @var{bytes}
2579 @itemx --bytes=@var{bytes}
2580 @opindex -c
2581 @opindex --bytes
2582 Output the last @var{bytes} bytes, instead of final lines.  Appending
2583 @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and @samp{m}
2584 by 1048576.
2585
2586 @item -f
2587 @itemx --follow[=@var{how}]
2588 @opindex -f
2589 @opindex --follow
2590 @cindex growing files
2591 @vindex name @r{follow option}
2592 @vindex descriptor @r{follow option}
2593 Loop forever trying to read more characters at the end of the file,
2594 presumably because the file is growing.
2595 If more than one file is given, @command{tail} prints a header whenever it
2596 gets output from a different file, to indicate which file that output is
2597 from.
2598
2599 There are two ways to specify how you'd like to track files with this option,
2600 but that difference is noticeable only when a followed file is removed or
2601 renamed.
2602 If you'd like to continue to track the end of a growing file even after
2603 it has been unlinked, use @option{--follow=descriptor}.  This is the default
2604 behavior, but it is not useful if you're tracking a log file that may be
2605 rotated (removed or renamed, then reopened).  In that case, use
2606 @option{--follow=name} to track the named file by reopening it periodically
2607 to see if it has been removed and recreated by some other program.
2608
2609 No matter which method you use, if the tracked file is determined to have
2610 shrunk, @command{tail} prints a message saying the file has been truncated
2611 and resumes tracking the end of the file from the newly-determined endpoint.
2612
2613 When a file is removed, @command{tail}'s behavior depends on whether it is
2614 following the name or the descriptor.  When following by name, tail can
2615 detect that a file has been removed and gives a message to that effect,
2616 and if @option{--retry} has been specified it will continue checking
2617 periodically to see if the file reappears.
2618 When following a descriptor, tail does not detect that the file has
2619 been unlinked or renamed and issues no message;  even though the file
2620 may no longer be accessible via its original name, it may still be
2621 growing.
2622
2623 The option values @samp{descriptor} and @samp{name} may be specified only
2624 with the long form of the option, not with @option{-f}.
2625
2626 @vindex POSIXLY_CORRECT
2627 If @env{POSIXLY_CORRECT} is set, the @option{-f} option is ignored if
2628 no @var{file} operand is specified and standard input is a FIFO or a pipe.
2629
2630 @item -F
2631 @opindex -F
2632 This option is the same as @option{--follow=name --retry}.  That is, tail
2633 will attempt to reopen a file when it is removed.  Should this fail, tail
2634 will keep trying until it becomes accessible again.
2635
2636 @itemx --retry
2637 @opindex --retry
2638 This option is useful mainly when following by name (i.e., with
2639 @option{--follow=name}).
2640 Without this option, when tail encounters a file that doesn't
2641 exist or is otherwise inaccessible, it reports that fact and
2642 never checks it again.
2643
2644 @itemx --sleep-interval=@var{number}
2645 @opindex --sleep-interval
2646 Change the number of seconds to wait between iterations (the default is 1.0).
2647 During one iteration, every specified file is checked to see if it has
2648 changed size.
2649 Historical implementations of @command{tail} have required that
2650 @var{number} be an integer.  However, GNU @command{tail} accepts
2651 an arbitrary floating point number (using a period before any
2652 fractional digits).
2653
2654 @itemx --pid=@var{pid}
2655 @opindex --pid
2656 When following by name or by descriptor, you may specify the process ID,
2657 @var{pid}, of the sole writer of all @var{file} arguments.  Then, shortly
2658 after that process terminates, tail will also terminate.  This will
2659 work properly only if the writer and the tailing process are running on
2660 the same machine.  For example, to save the output of a build in a file
2661 and to watch the file grow, if you invoke @command{make} and @command{tail}
2662 like this then the tail process will stop when your build completes.
2663 Without this option, you would have had to kill the @code{tail -f}
2664 process yourself.
2665
2666 @example
2667 $ make >& makerr & tail --pid=$! -f makerr
2668 @end example
2669
2670 If you specify a @var{pid} that is not in use or that does not correspond
2671 to the process that is writing to the tailed files, then @command{tail}
2672 may terminate long before any @var{file}s stop growing or it may not
2673 terminate until long after the real writer has terminated.
2674 Note that @option{--pid} cannot be supported on some systems; @command{tail}
2675 will print a warning if this is the case.
2676
2677 @itemx --max-unchanged-stats=@var{n}
2678 @opindex --max-unchanged-stats
2679 When tailing a file by name, if there have been @var{n} (default
2680 n=@value{DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS}) consecutive
2681 iterations for which the file has not changed, then
2682 @code{open}/@code{fstat} the file to determine if that file name is
2683 still associated with the same device/inode-number pair as before.
2684 When following a log file that is rotated, this is approximately the
2685 number of seconds between when tail prints the last pre-rotation lines
2686 and when it prints the lines that have accumulated in the new log file.
2687 This option is meaningful only when following by name.
2688
2689 @itemx -n @var{n}
2690 @itemx --lines=@var{n}
2691 @opindex -n
2692 @opindex --lines
2693 Output the last @var{n} lines.
2694
2695 @item -q
2696 @itemx --quiet
2697 @itemx --silent
2698 @opindex -q
2699 @opindex --quiet
2700 @opindex --silent
2701 Never print file name headers.
2702
2703 @item -v
2704 @itemx --verbose
2705 @opindex -v
2706 @opindex --verbose
2707 Always print file name headers.
2708
2709 @end table
2710
2711 For compatibility @command{tail} also supports an obsolete usage
2712 @samp{tail -[@var{count}][bcl][f] [@var{file}]}, which is recognized
2713 only if it does not conflict with the usage described
2714 above.  This obsolete form uses exactly one option and at most one
2715 file.  In the option, @var{count} is an optional decimal number optionally
2716 followed by a size letter (@samp{b}, @samp{c}, @samp{l}) to mean count
2717 by 512-byte blocks, bytes, or lines, optionally followed by @samp{f}
2718 which has the same meaning as @option{-f}.
2719
2720 @vindex _POSIX2_VERSION
2721 On older systems, the leading @samp{-} can be replaced by @samp{+} in
2722 the obsolete option syntax with the same meaning as in counts, and
2723 obsolete usage overrides normal usage when the two conflict.
2724 This obsolete behavior can be enabled or disabled with the
2725 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
2726 conformance}).
2727
2728 Scripts intended for use on standard hosts should avoid obsolete
2729 syntax and should use @option{-c @var{count}[b]}, @option{-n
2730 @var{count}}, and/or @option{-f} instead.  If your script must also
2731 run on hosts that support only the obsolete syntax, you can often
2732 rewrite it to avoid problematic usages, e.g., by using @samp{sed -n
2733 '$p'} rather than @samp{tail -1}.  If that's not possible, the script
2734 can use a test like @samp{if tail -c +1 </dev/null >/dev/null 2>&1;
2735 then @dots{}} to decide which syntax to use.
2736
2737 Even if your script assumes the standard behavior, you should still
2738 beware usages whose behaviors differ depending on the @acronym{POSIX}
2739 version.  For example, avoid @samp{tail - main.c}, since it might be
2740 interpreted as either @samp{tail main.c} or as @samp{tail -- -
2741 main.c}; avoid @samp{tail -c 4}, since it might mean either @samp{tail
2742 -c4} or @samp{tail -c 10 4}; and avoid @samp{tail +4}, since it might
2743 mean either @samp{tail ./+4} or @samp{tail -n +4}.
2744
2745 @exitstatus
2746
2747
2748 @node split invocation
2749 @section @command{split}: Split a file into fixed-size pieces
2750
2751 @pindex split
2752 @cindex splitting a file into pieces
2753 @cindex pieces, splitting a file into
2754
2755 @command{split} creates output files containing consecutive sections of
2756 @var{input} (standard input if none is given or @var{input} is
2757 @samp{-}).  Synopsis:
2758
2759 @example
2760 split [@var{option}] [@var{input} [@var{prefix}]]
2761 @end example
2762
2763 By default, @command{split} puts 1000 lines of @var{input} (or whatever is
2764 left over for the last section), into each output file.
2765
2766 @cindex output file name prefix
2767 The output files' names consist of @var{prefix} (@samp{x} by default)
2768 followed by a group of characters (@samp{aa}, @samp{ab}, @dots{} by
2769 default), such that concatenating the output files in traditional
2770 sorted order by file name produces
2771 the original input file.  If the output file names are exhausted,
2772 @command{split} reports an error without deleting the output files
2773 that it did create.
2774
2775 The program accepts the following options.  Also see @ref{Common options}.
2776
2777 @table @samp
2778
2779 @item -a @var{length}
2780 @itemx --suffix-length=@var{length}
2781 @opindex -a
2782 @opindex --suffix-length
2783 Use suffixes of length @var{length}.  The default @var{length} is 2.
2784
2785 @item -l @var{lines}
2786 @itemx --lines=@var{lines}
2787 @opindex -l
2788 @opindex --lines
2789 Put @var{lines} lines of @var{input} into each output file.
2790
2791 For compatibility @command{split} also supports an obsolete
2792 option syntax @option{-@var{lines}}.  New scripts should use @option{-l
2793 @var{lines}} instead.
2794
2795 @item -b @var{bytes}
2796 @itemx --bytes=@var{bytes}
2797 @opindex -b
2798 @opindex --bytes
2799 Put the first @var{bytes} bytes of @var{input} into each output file.
2800 Appending @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and
2801 @samp{m} by 1048576.
2802
2803 @item -C @var{bytes}
2804 @itemx --line-bytes=@var{bytes}
2805 @opindex -C
2806 @opindex --line-bytes
2807 Put into each output file as many complete lines of @var{input} as
2808 possible without exceeding @var{bytes} bytes.  For lines longer than
2809 @var{bytes} bytes, put @var{bytes} bytes into each output file until
2810 less than @var{bytes} bytes of the line are left, then continue
2811 normally.  @var{bytes} has the same format as for the @option{--bytes}
2812 option.
2813
2814 @item -d
2815 @itemx --numeric-suffixes
2816 @opindex -d
2817 @opindex --numeric-suffixes
2818 Use digits in suffixes rather than lower-case letters.
2819
2820 @itemx --verbose
2821 @opindex --verbose
2822 Write a diagnostic to standard error just before each output file is opened.
2823
2824 @end table
2825
2826 @exitstatus
2827
2828
2829 @node csplit invocation
2830 @section @command{csplit}: Split a file into context-determined pieces
2831
2832 @pindex csplit
2833 @cindex context splitting
2834 @cindex splitting a file into pieces by context
2835
2836 @command{csplit} creates zero or more output files containing sections of
2837 @var{input} (standard input if @var{input} is @samp{-}).  Synopsis:
2838
2839 @example
2840 csplit [@var{option}]@dots{} @var{input} @var{pattern}@dots{}
2841 @end example
2842
2843 The contents of the output files are determined by the @var{pattern}
2844 arguments, as detailed below.  An error occurs if a @var{pattern}
2845 argument refers to a nonexistent line of the input file (e.g., if no
2846 remaining line matches a given regular expression).  After every
2847 @var{pattern} has been matched, any remaining input is copied into one
2848 last output file.
2849
2850 By default, @command{csplit} prints the number of bytes written to each
2851 output file after it has been created.
2852
2853 The types of pattern arguments are:
2854
2855 @table @samp
2856
2857 @item @var{n}
2858 Create an output file containing the input up to but not including line
2859 @var{n} (a positive integer).  If followed by a repeat count, also
2860 create an output file containing the next @var{n} lines of the input
2861 file once for each repeat.
2862
2863 @item /@var{regexp}/[@var{offset}]
2864 Create an output file containing the current line up to (but not
2865 including) the next line of the input file that contains a match for
2866 @var{regexp}.  The optional @var{offset} is an integer.
2867 If it is given, the input up to (but not including) the
2868 matching line plus or minus @var{offset} is put into the output file,
2869 and the line after that begins the next section of input.
2870
2871 @item %@var{regexp}%[@var{offset}]
2872 Like the previous type, except that it does not create an output
2873 file, so that section of the input file is effectively ignored.
2874
2875 @item @{@var{repeat-count}@}
2876 Repeat the previous pattern @var{repeat-count} additional
2877 times.  The @var{repeat-count} can either be a positive integer or an
2878 asterisk, meaning repeat as many times as necessary until the input is
2879 exhausted.
2880
2881 @end table
2882
2883 The output files' names consist of a prefix (@samp{xx} by default)
2884 followed by a suffix.  By default, the suffix is an ascending sequence
2885 of two-digit decimal numbers from @samp{00} to @samp{99}.  In any case,
2886 concatenating the output files in sorted order by file name produces the
2887 original input file.
2888
2889 By default, if @command{csplit} encounters an error or receives a hangup,
2890 interrupt, quit, or terminate signal, it removes any output files
2891 that it has created so far before it exits.
2892
2893 The program accepts the following options.  Also see @ref{Common options}.
2894
2895 @table @samp
2896
2897 @item -f @var{prefix}
2898 @itemx --prefix=@var{prefix}
2899 @opindex -f
2900 @opindex --prefix
2901 @cindex output file name prefix
2902 Use @var{prefix} as the output file name prefix.
2903
2904 @item -b @var{suffix}
2905 @itemx --suffix=@var{suffix}
2906 @opindex -b
2907 @opindex --suffix
2908 @cindex output file name suffix
2909 Use @var{suffix} as the output file name suffix.  When this option is
2910 specified, the suffix string must include exactly one
2911 @code{printf(3)}-style conversion specification, possibly including
2912 format specification flags, a field width, a precision specifications,
2913 or all of these kinds of modifiers.  The format letter must convert a
2914 binary integer argument to readable form; thus, only @samp{d}, @samp{i},
2915 @samp{u}, @samp{o}, @samp{x}, and @samp{X} conversions are allowed.  The
2916 entire @var{suffix} is given (with the current output file number) to
2917 @code{sprintf(3)} to form the file name suffixes for each of the
2918 individual output files in turn.  If this option is used, the
2919 @option{--digits} option is ignored.
2920
2921 @item -n @var{digits}
2922 @itemx --digits=@var{digits}
2923 @opindex -n
2924 @opindex --digits
2925 Use output file names containing numbers that are @var{digits} digits
2926 long instead of the default 2.
2927
2928 @item -k
2929 @itemx --keep-files
2930 @opindex -k
2931 @opindex --keep-files
2932 Do not remove output files when errors are encountered.
2933
2934 @item -z
2935 @itemx --elide-empty-files
2936 @opindex -z
2937 @opindex --elide-empty-files
2938 Suppress the generation of zero-length output files.  (In cases where
2939 the section delimiters of the input file are supposed to mark the first
2940 lines of each of the sections, the first output file will generally be a
2941 zero-length file unless you use this option.)  The output file sequence
2942 numbers always run consecutively starting from 0, even when this option
2943 is specified.
2944
2945 @item -s
2946 @itemx -q
2947 @itemx --silent
2948 @itemx --quiet
2949 @opindex -s
2950 @opindex -q
2951 @opindex --silent
2952 @opindex --quiet
2953 Do not print counts of output file sizes.
2954
2955 @end table
2956
2957 @exitstatus
2958
2959
2960 @node Summarizing files
2961 @chapter Summarizing files
2962
2963 @cindex summarizing files
2964
2965 These commands generate just a few numbers representing entire
2966 contents of files.
2967
2968 @menu
2969 * wc invocation::               Print newline, word, and byte counts.
2970 * sum invocation::              Print checksum and block counts.
2971 * cksum invocation::            Print CRC checksum and byte counts.
2972 * md5sum invocation::           Print or check MD5 digests.
2973 * sha1sum invocation::          Print or check SHA-1 digests.
2974 * sha2 utilities::              Print or check SHA-2 digests.
2975 @end menu
2976
2977
2978 @node wc invocation
2979 @section @command{wc}: Print newline, word, and byte counts
2980
2981 @pindex wc
2982 @cindex byte count
2983 @cindex character count
2984 @cindex word count
2985 @cindex line count
2986
2987 @command{wc} counts the number of bytes, characters, whitespace-separated
2988 words, and newlines in each given @var{file}, or standard input if none
2989 are given or for a @var{file} of @samp{-}.  Synopsis:
2990
2991 @example
2992 wc [@var{option}]@dots{} [@var{file}]@dots{}
2993 @end example
2994
2995 @cindex total counts
2996 @command{wc} prints one line of counts for each file, and if the file was
2997 given as an argument, it prints the file name following the counts.  If
2998 more than one @var{file} is given, @command{wc} prints a final line
2999 containing the cumulative counts, with the file name @file{total}.  The
3000 counts are printed in this order: newlines, words, characters, bytes,
3001 maximum line length.
3002 Each count is printed right-justified in a field with at least one
3003 space between fields so that the numbers and file names normally line
3004 up nicely in columns.  The width of the count fields varies depending
3005 on the inputs, so you should not depend on a particular field width.
3006 However, as a @acronym{GNU} extension, if only one count is printed,
3007 it is guaranteed to be printed without leading spaces.
3008
3009 By default, @command{wc} prints three counts: the newline, words, and byte
3010 counts.  Options can specify that only certain counts be printed.
3011 Options do not undo others previously given, so
3012
3013 @example
3014 wc --bytes --words
3015 @end example
3016
3017 @noindent
3018 prints both the byte counts and the word counts.
3019
3020 With the @option{--max-line-length} option, @command{wc} prints the length
3021 of the longest line per file, and if there is more than one file it
3022 prints the maximum (not the sum) of those lengths.
3023
3024 The program accepts the following options.  Also see @ref{Common options}.
3025
3026 @table @samp
3027
3028 @item -c
3029 @itemx --bytes
3030 @opindex -c
3031 @opindex --bytes
3032 Print only the byte counts.
3033
3034 @item -m
3035 @itemx --chars
3036 @opindex -m
3037 @opindex --chars
3038 Print only the character counts.
3039
3040 @item -w
3041 @itemx --words
3042 @opindex -w
3043 @opindex --words
3044 Print only the word counts.
3045
3046 @item -l
3047 @itemx --lines
3048 @opindex -l
3049 @opindex --lines
3050 Print only the newline counts.
3051
3052 @item -L
3053 @itemx --max-line-length
3054 @opindex -L
3055 @opindex --max-line-length
3056 Print only the maximum line lengths.
3057
3058 @itemx --files0-from=@var{FILE}
3059 @opindex --files0-from=@var{FILE}
3060 @cindex including files from @command{du}
3061 Rather than processing files named on the command line, process those
3062 named in file @var{FILE}; each name is terminated by a null byte.
3063 This is useful when
3064 the list of file names is so long that it may exceed a command line
3065 length limitation.
3066 In such cases, running @command{wc} via @command{xargs} is undesirable
3067 because it splits the list into pieces and makes @command{wc} print a
3068 total for each sublist rather than for the entire list.
3069 One way to produce a list of null-byte-terminated file names is with @sc{gnu}
3070 @command{find}, using its @option{-print0} predicate.  For example, to find
3071 the length of the longest line in any @file{.c} or @file{.h} file in the
3072 current hierarchy, do this:
3073
3074 @example
3075 find . -name '*.[ch]' -print0 | wc -L --files0-from=- | tail -n1
3076 @end example
3077
3078 Do not specify any @var{FILE} on the command line when using this option.
3079
3080 @end table
3081
3082 @exitstatus
3083
3084
3085 @node sum invocation
3086 @section @command{sum}: Print checksum and block counts
3087
3088 @pindex sum
3089 @cindex 16-bit checksum
3090 @cindex checksum, 16-bit
3091
3092 @command{sum} computes a 16-bit checksum for each given @var{file}, or
3093 standard input if none are given or for a @var{file} of @samp{-}.  Synopsis:
3094
3095 @example
3096 sum [@var{option}]@dots{} [@var{file}]@dots{}
3097 @end example
3098
3099 @command{sum} prints the checksum for each @var{file} followed by the
3100 number of blocks in the file (rounded up).  If more than one @var{file}
3101 is given, file names are also printed (by default).  (With the
3102 @option{--sysv} option, corresponding file names are printed when there is
3103 at least one file argument.)
3104
3105 By default, @sc{gnu} @command{sum} computes checksums using an algorithm
3106 compatible with BSD @command{sum} and prints file sizes in units of
3107 1024-byte blocks.
3108
3109 The program accepts the following options.  Also see @ref{Common options}.
3110
3111 @table @samp
3112
3113 @item -r
3114 @opindex -r
3115 @cindex BSD @command{sum}
3116 Use the default (BSD compatible) algorithm.  This option is included for
3117 compatibility with the System V @command{sum}.  Unless @option{-s} was also
3118 given, it has no effect.
3119
3120 @item -s
3121 @itemx --sysv
3122 @opindex -s
3123 @opindex --sysv
3124 @cindex System V @command{sum}
3125 Compute checksums using an algorithm compatible with System V
3126 @command{sum}'s default, and print file sizes in units of 512-byte blocks.
3127
3128 @end table
3129
3130 @command{sum} is provided for compatibility; the @command{cksum} program (see
3131 next section) is preferable in new applications.
3132
3133 @exitstatus
3134
3135
3136 @node cksum invocation
3137 @section @command{cksum}: Print CRC checksum and byte counts
3138
3139 @pindex cksum
3140 @cindex cyclic redundancy check
3141 @cindex CRC checksum
3142
3143 @command{cksum} computes a cyclic redundancy check (CRC) checksum for each
3144 given @var{file}, or standard input if none are given or for a
3145 @var{file} of @samp{-}.  Synopsis:
3146
3147 @example
3148 cksum [@var{option}]@dots{} [@var{file}]@dots{}
3149 @end example
3150
3151 @command{cksum} prints the CRC checksum for each file along with the number
3152 of bytes in the file, and the file name unless no arguments were given.
3153
3154 @command{cksum} is typically used to ensure that files
3155 transferred by unreliable means (e.g., netnews) have not been corrupted,
3156 by comparing the @command{cksum} output for the received files with the
3157 @command{cksum} output for the original files (typically given in the
3158 distribution).
3159
3160 The CRC algorithm is specified by the @acronym{POSIX} standard.  It is not
3161 compatible with the BSD or System V @command{sum} algorithms (see the
3162 previous section); it is more robust.
3163
3164 The only options are @option{--help} and @option{--version}.  @xref{Common
3165 options}.
3166
3167 @exitstatus
3168
3169
3170 @node md5sum invocation
3171 @section @command{md5sum}: Print or check MD5 digests
3172
3173 @pindex md5sum
3174 @cindex MD5
3175 @cindex 128-bit checksum
3176 @cindex checksum, 128-bit
3177 @cindex fingerprint, 128-bit
3178 @cindex message-digest, 128-bit
3179
3180 @command{md5sum} computes a 128-bit checksum (or @dfn{fingerprint} or
3181 @dfn{message-digest}) for each specified @var{file}.
3182
3183 Note: The MD5 digest is more reliable than a simple CRC (provided by
3184 the @command{cksum} command) for detecting accidental file corruption,
3185 as the chances of accidentally having two files with identical MD5
3186 are vanishingly small.  However, it should not be considered truly
3187 secure against malicious tampering: although finding a file with a
3188 given MD5 fingerprint, or modifying a file so as to retain its MD5 are
3189 considered infeasible at the moment, it is known how to produce
3190 different files with identical MD5 (a ``collision''), something which
3191 can be a security issue in certain contexts.  For more secure hashes,
3192 consider using SHA-1 or SHA-2.  @xref{sha1sum invocation}, and
3193 @ref{sha2 utilities}.
3194
3195 If a @var{file} is specified as @samp{-} or if no files are given
3196 @command{md5sum} computes the checksum for the standard input.
3197 @command{md5sum} can also determine whether a file and checksum are
3198 consistent.  Synopsis:
3199
3200 @example
3201 md5sum [@var{option}]@dots{} [@var{file}]@dots{}
3202 @end example
3203
3204 For each @var{file}, @samp{md5sum} outputs the MD5 checksum, a flag
3205 indicating a binary or text input file, and the file name.
3206 If @var{file} contains a backslash or newline, the
3207 line is started with a backslash, and each problematic character in
3208 the file name is escaped with a backslash, making the output
3209 unambiguous even in the presence of arbitrary file names.
3210 If @var{file} is omitted or specified as @samp{-}, standard input is read.
3211
3212 The program accepts the following options.  Also see @ref{Common options}.
3213
3214 @table @samp
3215
3216 @item -b
3217 @itemx --binary
3218 @opindex -b
3219 @opindex --binary
3220 @cindex binary input files
3221 Treat each input file as binary, by reading it in binary mode and
3222 outputting a @samp{*} flag.  This is the inverse of @option{--text}.
3223 On systems like @acronym{GNU} that do not distinguish between binary
3224 and text files, this option merely flags each input file as binary:
3225 the MD5 checksum is unaffected.  This option is the default on systems
3226 like MS-DOS that distinguish between binary and text files, except
3227 for reading standard input when standard input is a terminal.
3228
3229 @item -c
3230 @itemx --check
3231 Read file names and checksum information (not data) from each
3232 @var{file} (or from stdin if no @var{file} was specified) and report
3233 whether the checksums match the contents of the named files.
3234 The input to this mode of @command{md5sum} is usually the output of
3235 a prior, checksum-generating run of @samp{md5sum}.
3236 Each valid line of input consists of an MD5 checksum, a binary/text
3237 flag, and then a file name.
3238 Binary files are marked with @samp{*}, text with @samp{ }.
3239 For each such line, @command{md5sum} reads the named file and computes its
3240 MD5 checksum.  Then, if the computed message digest does not match the
3241 one on the line with the file name, the file is noted as having
3242 failed the test.  Otherwise, the file passes the test.
3243 By default, for each valid line, one line is written to standard
3244 output indicating whether the named file passed the test.
3245 After all checks have been performed, if there were any failures,
3246 a warning is issued to standard error.
3247 Use the @option{--status} option to inhibit that output.
3248 If any listed file cannot be opened or read, if any valid line has
3249 an MD5 checksum inconsistent with the associated file, or if no valid
3250 line is found, @command{md5sum} exits with nonzero status.  Otherwise,
3251 it exits successfully.
3252
3253 @itemx --status
3254 @opindex --status
3255 @cindex verifying MD5 checksums
3256 This option is useful only when verifying checksums.
3257 When verifying checksums, don't generate the default one-line-per-file
3258 diagnostic and don't output the warning summarizing any failures.
3259 Failures to open or read a file still evoke individual diagnostics to
3260 standard error.
3261 If all listed files are readable and are consistent with the associated
3262 MD5 checksums, exit successfully.  Otherwise exit with a status code
3263 indicating there was a failure.
3264
3265 @item -t
3266 @itemx --text
3267 @opindex -t
3268 @opindex --text
3269 @cindex text input files
3270 Treat each input file as text, by reading it in text mode and
3271 outputting a @samp{ } flag.  This is the inverse of @option{--binary}.
3272 This option is the default on systems like @acronym{GNU} that do not
3273 distinguish between binary and text files.  On other systems, it is
3274 the default for reading standard input when standard input is a
3275 terminal.
3276
3277 @item -w
3278 @itemx --warn
3279 @opindex -w
3280 @opindex --warn
3281 @cindex verifying MD5 checksums
3282 When verifying checksums, warn about improperly formatted MD5 checksum lines.
3283 This option is useful only if all but a few lines in the checked input
3284 are valid.
3285
3286 @end table
3287
3288 @exitstatus
3289
3290
3291 @node sha1sum invocation
3292 @section @command{sha1sum}: Print or check SHA-1 digests
3293
3294 @pindex sha1sum
3295 @cindex SHA-1
3296 @cindex 160-bit checksum
3297 @cindex checksum, 160-bit
3298 @cindex fingerprint, 160-bit
3299 @cindex message-digest, 160-bit
3300
3301 @command{sha1sum} computes a 160-bit checksum for each specified
3302 @var{file}.  The usage and options of this command are precisely the
3303 same as for @command{md5sum}.  @xref{md5sum invocation}.
3304
3305 Note: The SHA-1 digest is more secure than MD5, and no collisions of
3306 it are known (different files having the same fingerprint).  However,
3307 it is known that they can be produced with considerable, but not
3308 unreasonable, resources.  For this reason, it is generally considered
3309 that SHA-1 should be gradually phased out in favor of the more secure
3310 SHA-2 hash algorithms.  @xref{sha2 utilities}.
3311
3312
3313 @node sha2 utilities
3314 @section sha2 utilities: Print or check SHA-2 digests
3315
3316 @pindex sha224sum
3317 @pindex sha256sum
3318 @pindex sha384sum
3319 @pindex sha512sum
3320 @cindex SHA-2
3321 @cindex 224-bit checksum
3322 @cindex 256-bit checksum
3323 @cindex 384-bit checksum
3324 @cindex 512-bit checksum
3325 @cindex checksum, 224-bit
3326 @cindex checksum, 256-bit
3327 @cindex checksum, 384-bit
3328 @cindex checksum, 512-bit
3329 @cindex fingerprint, 224-bit
3330 @cindex fingerprint, 256-bit
3331 @cindex fingerprint, 384-bit
3332 @cindex fingerprint, 512-bit
3333 @cindex message-digest, 224-bit
3334 @cindex message-digest, 256-bit
3335 @cindex message-digest, 384-bit
3336 @cindex message-digest, 512-bit
3337
3338 The commands @command{sha224sum}, @command{sha256sum},
3339 @command{sha384sum} and @command{sha512sum} compute checksums of
3340 various lengths (respectively 224, 256, 384 and 512 bits),
3341 collectively known as the SHA-2 hashes.  The usage and options of
3342 these commands are precisely the same as for @command{md5sum}.
3343 @xref{md5sum invocation}.
3344
3345 Note: The SHA384 and SHA512 digests are considerably slower to
3346 compute, especially on 32-bit computers, than SHA224 or SHA256.
3347
3348
3349 @node Operating on sorted files
3350 @chapter Operating on sorted files
3351
3352 @cindex operating on sorted files
3353 @cindex sorted files, operations on
3354
3355 These commands work with (or produce) sorted files.
3356
3357 @menu
3358 * sort invocation::             Sort text files.
3359 * shuf invocation::             Shuffle text files.
3360 * uniq invocation::             Uniquify files.
3361 * comm invocation::             Compare two sorted files line by line.
3362 * ptx invocation::              Produce a permuted index of file contents.
3363 * tsort invocation::            Topological sort.
3364 * tsort background::            Where tsort came from.
3365 @end menu
3366
3367
3368 @node sort invocation
3369 @section @command{sort}: Sort text files
3370
3371 @pindex sort
3372 @cindex sorting files
3373
3374 @command{sort} sorts, merges, or compares all the lines from the given
3375 files, or standard input if none are given or for a @var{file} of
3376 @samp{-}.  By default, @command{sort} writes the results to standard
3377 output.  Synopsis:
3378
3379 @example
3380 sort [@var{option}]@dots{} [@var{file}]@dots{}
3381 @end example
3382
3383 @command{sort} has three modes of operation: sort (the default), merge,
3384 and check for sortedness.  The following options change the operation
3385 mode:
3386
3387 @table @samp
3388
3389 @item -c
3390 @itemx --check
3391 @itemx --check=diagnose-first
3392 @opindex -c
3393 @opindex --check
3394 @cindex checking for sortedness
3395 Check whether the given file is already sorted: if it is not all
3396 sorted, print a diagnostic containing the first out-of-order line and
3397 exit with a status of 1.
3398 Otherwise, exit successfully.
3399 At most one input file can be given.
3400
3401 @item -C
3402 @itemx --check=quiet
3403 @itemx --check=silent
3404 @opindex -c
3405 @opindex --check
3406 @cindex checking for sortedness
3407 Exit successfully if the given file is already sorted, and
3408 exit with status 1 otherwise.
3409 At most one input file can be given.
3410 This is like @option{-c}, except it does not print a diagnostic.
3411
3412 @item -m
3413 @itemx --merge
3414 @opindex -m
3415 @opindex --merge
3416 @cindex merging sorted files
3417 Merge the given files by sorting them as a group.  Each input file must
3418 always be individually sorted.  It always works to sort instead of
3419 merge; merging is provided because it is faster, in the case where it
3420 works.
3421
3422 @end table
3423
3424 @cindex sort stability
3425 @cindex sort's last-resort comparison
3426 A pair of lines is compared as follows:
3427 @command{sort} compares each pair of fields, in the
3428 order specified on the command line, according to the associated
3429 ordering options, until a difference is found or no fields are left.
3430 If no key fields are specified, @command{sort} uses a default key of
3431 the entire line.  Finally, as a last resort when all keys compare
3432 equal, @command{sort} compares entire lines as if no ordering options
3433 other than @option{--reverse} (@option{-r}) were specified.  The
3434 @option{--stable} (@option{-s}) option disables this @dfn{last-resort
3435 comparison} so that lines in which all fields compare equal are left
3436 in their original relative order.  The @option{--unique}
3437 (@option{-u}) option also disables the last-resort comparison.
3438
3439 @vindex LC_ALL
3440 @vindex LC_COLLATE
3441 Unless otherwise specified, all comparisons use the character collating
3442 sequence specified by the @env{LC_COLLATE} locale.@footnote{If you
3443 use a non-@acronym{POSIX} locale (e.g., by setting @env{LC_ALL}
3444 to @samp{en_US}), then @command{sort} may produce output that is sorted
3445 differently than you're accustomed to.  In that case, set the @env{LC_ALL}
3446 environment variable to @samp{C}.  Note that setting only @env{LC_COLLATE}
3447 has two problems.  First, it is ineffective if @env{LC_ALL} is also set.
3448 Second, it has undefined behavior if @env{LC_CTYPE} (or @env{LANG}, if
3449 @env{LC_CTYPE} is unset) is set to an incompatible value.  For example,
3450 you get undefined behavior if @env{LC_CTYPE} is @code{ja_JP.PCK} but
3451 @env{LC_COLLATE} is @code{en_US.UTF-8}.}
3452
3453 @sc{gnu} @command{sort} (as specified for all @sc{gnu} utilities) has no
3454 limit on input line length or restrictions on bytes allowed within lines.
3455 In addition, if the final byte of an input file is not a newline, @sc{gnu}
3456 @command{sort} silently supplies one.  A line's trailing newline is not
3457 part of the line for comparison purposes.
3458
3459 @cindex exit status of @command{sort}
3460 Exit status:
3461
3462 @display
3463 0 if no error occurred
3464 1 if invoked with @option{-c} or @option{-C} and the input is not sorted
3465 2 if an error occurred
3466 @end display
3467
3468 @vindex TMPDIR
3469 If the environment variable @env{TMPDIR} is set, @command{sort} uses its
3470 value as the directory for temporary files instead of @file{/tmp}.  The
3471 @option{--temporary-directory} (@option{-T}) option in turn overrides
3472 the environment variable.
3473
3474 The following options affect the ordering of output lines.  They may be
3475 specified globally or as part of a specific key field.  If no key
3476 fields are specified, global options apply to comparison of entire
3477 lines; otherwise the global options are inherited by key fields that do
3478 not specify any special options of their own.  In pre-@acronym{POSIX}
3479 versions of @command{sort}, global options affect only later key fields,
3480 so portable shell scripts should specify global options first.
3481
3482 @table @samp
3483
3484 @item -b
3485 @itemx --ignore-leading-blanks
3486 @opindex -b
3487 @opindex --ignore-leading-blanks
3488 @cindex blanks, ignoring leading
3489 @vindex LC_CTYPE
3490 Ignore leading blanks when finding sort keys in each line.
3491 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3492 can change this.
3493
3494 @item -d
3495 @itemx --dictionary-order
3496 @opindex -d
3497 @opindex --dictionary-order
3498 @cindex dictionary order
3499 @cindex phone directory order
3500 @cindex telephone directory order
3501 @vindex LC_CTYPE
3502 Sort in @dfn{phone directory} order: ignore all characters except
3503 letters, digits and blanks when sorting.
3504 By default letters and digits are those of @acronym{ASCII} and a blank
3505 is a space or a tab, but the @env{LC_CTYPE} locale can change this.
3506
3507 @item -f
3508 @itemx --ignore-case
3509 @opindex -f
3510 @opindex --ignore-case
3511 @cindex ignoring case
3512 @cindex case folding
3513 @vindex LC_CTYPE
3514 Fold lowercase characters into the equivalent uppercase characters when
3515 comparing so that, for example, @samp{b} and @samp{B} sort as equal.
3516 The @env{LC_CTYPE} locale determines character types.
3517
3518 @item -g
3519 @itemx --general-numeric-sort
3520 @opindex -g
3521 @opindex --general-numeric-sort
3522 @cindex general numeric sort
3523 @vindex LC_NUMERIC
3524 Sort numerically, using the standard C function @code{strtod} to convert
3525 a prefix of each line to a double-precision floating point number.
3526 This allows floating point numbers to be specified in scientific notation,
3527 like @code{1.0e-34} and @code{10e100}.
3528 The @env{LC_NUMERIC} locale determines the decimal-point character.
3529 Do not report overflow, underflow, or conversion errors.
3530 Use the following collating sequence:
3531
3532 @itemize @bullet
3533 @item
3534 Lines that do not start with numbers (all considered to be equal).
3535 @item
3536 NaNs (``Not a Number'' values, in IEEE floating point arithmetic)
3537 in a consistent but machine-dependent order.
3538 @item
3539 Minus infinity.
3540 @item
3541 Finite numbers in ascending numeric order (with @math{-0} and @math{+0} equal).
3542 @item
3543 Plus infinity.
3544 @end itemize
3545
3546 Use this option only if there is no alternative; it is much slower than
3547 @option{--numeric-sort} (@option{-n}) and it can lose information when
3548 converting to floating point.
3549
3550 @item -i
3551 @itemx --ignore-nonprinting
3552 @opindex -i
3553 @opindex --ignore-nonprinting
3554 @cindex nonprinting characters, ignoring
3555 @cindex unprintable characters, ignoring
3556 @vindex LC_CTYPE
3557 Ignore nonprinting characters.
3558 The @env{LC_CTYPE} locale determines character types.
3559 This option has no effect if the stronger @option{--dictionary-order}
3560 (@option{-d}) option is also given.
3561
3562 @item -M
3563 @itemx --month-sort
3564 @opindex -M
3565 @opindex --month-sort
3566 @cindex months, sorting by
3567 @vindex LC_TIME
3568 An initial string, consisting of any amount of blanks, followed
3569 by a month name abbreviation, is folded to UPPER case and
3570 compared in the order @samp{JAN} < @samp{FEB} < @dots{} < @samp{DEC}.
3571 Invalid names compare low to valid names.  The @env{LC_TIME} locale
3572 category determines the month spellings.
3573 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3574 can change this.
3575
3576 @item -n
3577 @itemx --numeric-sort
3578 @opindex -n
3579 @opindex --numeric-sort
3580 @cindex numeric sort
3581 @vindex LC_NUMERIC
3582 Sort numerically.  The number begins each line and consists
3583 of optional blanks, an optional @samp{-} sign, and zero or more
3584 digits possibly separated by thousands separators, optionally followed
3585 by a decimal-point character and zero or more digits.  An empty
3586 number is treated as @samp{0}.  The @env{LC_NUMERIC}
3587 locale specifies the decimal-point character and thousands separator.
3588 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3589 can change this.
3590
3591 Comparison is exact; there is no rounding error.
3592
3593 Neither a leading @samp{+} nor exponential notation is recognized.
3594 To compare such strings numerically, use the
3595 @option{--general-numeric-sort} (@option{-g}) option.
3596
3597 @item -r
3598 @itemx --reverse
3599 @opindex -r
3600 @opindex --reverse
3601 @cindex reverse sorting
3602 Reverse the result of comparison, so that lines with greater key values
3603 appear earlier in the output instead of later.
3604
3605 @item -R
3606 @itemx --random-sort
3607 @opindex -R
3608 @opindex --random-sort
3609 @cindex random sort
3610 Sort by hashing the input keys and then sorting the hash values.
3611 Choose the hash function at random, ensuring that it is free of
3612 collisions so that differing keys have differing hash values.  This is
3613 like a random permutation of the inputs (@pxref{shuf invocation}),
3614 except that keys with the same value sort together.
3615
3616 If multiple random sort fields are specified, the same random hash
3617 function is used for all fields.  To use different random hash
3618 functions for different fields, you can invoke @command{sort} more
3619 than once.
3620
3621 The choice of hash function is affected by the
3622 @option{--random-source} option.
3623
3624 @end table
3625
3626 Other options are:
3627
3628 @table @samp
3629
3630 @item --compress-program=@var{prog}
3631 Compress any temporary files with the program @var{prog}.
3632
3633 With no arguments, @var{prog} must compress standard input to standard
3634 output, and when given the @option{-d} option it must decompress
3635 standard input to standard output.
3636
3637 Terminate with an error if @var{prog} exits with nonzero status.
3638
3639 Whitespace and the backslash character should not appear in
3640 @var{prog}; they are reserved for future use.
3641
3642 @item -k @var{pos1}[,@var{pos2}]
3643 @itemx --key=@var{pos1}[,@var{pos2}]
3644 @opindex -k
3645 @opindex --key
3646 @cindex sort field
3647 Specify a sort field that consists of the part of the line between
3648 @var{pos1} and @var{pos2} (or the end of the line, if @var{pos2} is
3649 omitted), @emph{inclusive}.
3650
3651 Each @var{pos} has the form @samp{@var{f}[.@var{c}][@var{opts}]},
3652 where @var{f} is the number of the field to use, and @var{c} is the number
3653 of the first character from the beginning of the field.  Fields and character
3654 positions are numbered starting with 1; a character position of zero in
3655 @var{pos2} indicates the field's last character.  If @samp{.@var{c}} is
3656 omitted from @var{pos1}, it defaults to 1 (the beginning of the field);
3657 if omitted from @var{pos2}, it defaults to 0 (the end of the field).
3658 @var{opts} are ordering options, allowing individual keys to be sorted
3659 according to different rules; see below for details.  Keys can span
3660 multiple fields.
3661
3662 Example:  To sort on the second field, use @option{--key=2,2}
3663 (@option{-k 2,2}).  See below for more examples.
3664
3665 @item -o @var{output-file}
3666 @itemx --output=@var{output-file}
3667 @opindex -o
3668 @opindex --output
3669 @cindex overwriting of input, allowed
3670 Write output to @var{output-file} instead of standard output.
3671 Normally, @command{sort} reads all input before opening
3672 @var{output-file}, so you can safely sort a file in place by using
3673 commands like @code{sort -o F F} and @code{cat F | sort -o F}.
3674 However, @command{sort} with @option{--merge} (@option{-m}) can open
3675 the output file before reading all input, so a command like @code{cat
3676 F | sort -m -o F - G} is not safe as @command{sort} might start
3677 writing @file{F} before @command{cat} is done reading it.
3678
3679 @vindex POSIXLY_CORRECT
3680 On newer systems, @option{-o} cannot appear after an input file if
3681 @env{POSIXLY_CORRECT} is set, e.g., @samp{sort F -o F}.  Portable
3682 scripts should specify @option{-o @var{output-file}} before any input
3683 files.
3684
3685 @item --random-source=@var{file}
3686 @opindex --random-source
3687 @cindex random source for sorting
3688 Use @var{file} as a source of random data used to determine which
3689 random hash function to use with the @option{-R} option.  @xref{Random
3690 sources}.
3691
3692 @item -s
3693 @itemx --stable
3694 @opindex -s
3695 @opindex --stable
3696 @cindex sort stability
3697 @cindex sort's last-resort comparison
3698
3699 Make @command{sort} stable by disabling its last-resort comparison.
3700 This option has no effect if no fields or global ordering options
3701 other than @option{--reverse} (@option{-r}) are specified.
3702
3703 @item -S @var{size}
3704 @itemx --buffer-size=@var{size}
3705 @opindex -S
3706 @opindex --buffer-size
3707 @cindex size for main memory sorting
3708 Use a main-memory sort buffer of the given @var{size}.  By default,
3709 @var{size} is in units of 1024 bytes.  Appending @samp{%} causes
3710 @var{size} to be interpreted as a percentage of physical memory.
3711 Appending @samp{K} multiplies @var{size} by 1024 (the default),
3712 @samp{M} by 1,048,576, @samp{G} by 1,073,741,824, and so on for
3713 @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}.  Appending
3714 @samp{b} causes @var{size} to be interpreted as a byte count, with no
3715 multiplication.
3716
3717 This option can improve the performance of @command{sort} by causing it
3718 to start with a larger or smaller sort buffer than the default.
3719 However, this option affects only the initial buffer size.  The buffer
3720 grows beyond @var{size} if @command{sort} encounters input lines larger
3721 than @var{size}.
3722
3723 @item -t @var{separator}
3724 @itemx --field-separator=@var{separator}
3725 @opindex -t
3726 @opindex --field-separator
3727 @cindex field separator character
3728 Use character @var{separator} as the field separator when finding the
3729 sort keys in each line.  By default, fields are separated by the empty
3730 string between a non-blank character and a blank character.
3731 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3732 can change this.
3733
3734 That is, given the input line @w{@samp{ foo bar}}, @command{sort} breaks it
3735 into fields @w{@samp{ foo}} and @w{@samp{ bar}}.  The field separator is
3736 not considered to be part of either the field preceding or the field
3737 following, so with @samp{sort @w{-t " "}} the same input line has
3738 three fields: an empty field, @samp{foo}, and @samp{bar}.
3739 However, fields that extend to the end of the line,
3740 as @option{-k 2}, or fields consisting of a range, as @option{-k 2,3},
3741 retain the field separators present between the endpoints of the range.
3742
3743 To specify a null character (@acronym{ASCII} @sc{nul}) as
3744 the field separator, use the two-character string @samp{\0}, e.g.,
3745 @samp{sort -t '\0'}.
3746
3747 @item -T @var{tempdir}
3748 @itemx --temporary-directory=@var{tempdir}
3749 @opindex -T
3750 @opindex --temporary-directory
3751 @cindex temporary directory
3752 @vindex TMPDIR
3753 Use directory @var{tempdir} to store temporary files, overriding the
3754 @env{TMPDIR} environment variable.  If this option is given more than
3755 once, temporary files are stored in all the directories given.  If you
3756 have a large sort or merge that is I/O-bound, you can often improve
3757 performance by using this option to specify directories on different
3758 disks and controllers.
3759
3760 @item -u
3761 @itemx --unique
3762 @opindex -u
3763 @opindex --unique
3764 @cindex uniquifying output
3765
3766 Normally, output only the first of a sequence of lines that compare
3767 equal.  For the @option{--check} (@option{-c} or @option{-C}) option,
3768 check that no pair of consecutive lines compares equal.
3769
3770 This option also disables the default last-resort comparison.
3771
3772 The commands @code{sort -u} and @code{sort | uniq} are equivalent, but
3773 this equivalence does not extend to arbitrary @command{sort} options.
3774 For example, @code{sort -n -u} inspects only the value of the initial
3775 numeric string when checking for uniqueness, whereas @code{sort -n |
3776 uniq} inspects the entire line.  @xref{uniq invocation}.
3777
3778 @item -z
3779 @itemx --zero-terminated
3780 @opindex -z
3781 @opindex --zero-terminated
3782 @cindex sort zero-terminated lines
3783 Treat the input as a set of lines, each terminated by a null character
3784 (@acronym{ASCII} @sc{nul}) instead of a line feed
3785 (@acronym{ASCII} @sc{lf}).
3786 This option can be useful in conjunction with @samp{perl -0} or
3787 @samp{find -print0} and @samp{xargs -0} which do the same in order to
3788 reliably handle arbitrary file names (even those containing blanks
3789 or other special characters).
3790
3791 @end table
3792
3793 Historical (BSD and System V) implementations of @command{sort} have
3794 differed in their interpretation of some options, particularly
3795 @option{-b}, @option{-f}, and @option{-n}.  @sc{gnu} sort follows the @acronym{POSIX}
3796 behavior, which is usually (but not always!) like the System V behavior.
3797 According to @acronym{POSIX}, @option{-n} no longer implies @option{-b}.  For
3798 consistency, @option{-M} has been changed in the same way.  This may
3799 affect the meaning of character positions in field specifications in
3800 obscure cases.  The only fix is to add an explicit @option{-b}.
3801
3802 A position in a sort field specified with @option{-k} may have any
3803 of the option letters @samp{Mbdfinr} appended to it, in which case the
3804 global ordering options are not used for that particular field.  The
3805 @option{-b} option may be independently attached to either or both of
3806 the start and end positions of a field specification, and if it is
3807 inherited from the global options it will be attached to both.
3808 If input lines can contain leading or adjacent blanks and @option{-t}
3809 is not used, then @option{-k} is typically combined with @option{-b},
3810 @option{-g}, @option{-M}, or @option{-n}; otherwise the varying
3811 numbers of leading blanks in fields can cause confusing results.
3812
3813 If the start position in a sort field specifier falls after the end of
3814 the line or after the end field, the field is empty.  If the @option{-b}
3815 option was specified, the @samp{.@var{c}} part of a field specification
3816 is counted from the first nonblank character of the field.
3817
3818 @vindex _POSIX2_VERSION
3819 @vindex POSIXLY_CORRECT
3820 On older systems, @command{sort} supports an obsolete origin-zero
3821 syntax @samp{+@var{pos1} [-@var{pos2}]} for specifying sort keys.
3822 This obsolete behavior can be enabled or disabled with the
3823 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
3824 conformance}); it can also be enabled when @env{POSIXLY_CORRECT} is
3825 not set by using the obsolete syntax with @samp{-@var{pos2}} present.
3826
3827 Scripts intended for use on standard hosts should avoid obsolete
3828 syntax and should use @option{-k} instead.  For example, avoid
3829 @samp{sort +2}, since it might be interpreted as either @samp{sort
3830 ./+2} or @samp{sort -k 3}.  If your script must also run on hosts that
3831 support only the obsolete syntax, it can use a test like @samp{if sort
3832 -k 1 </dev/null >/dev/null 2>&1; then @dots{}} to decide which syntax
3833 to use.
3834
3835 Here are some examples to illustrate various combinations of options.
3836
3837 @itemize @bullet
3838
3839 @item
3840 Sort in descending (reverse) numeric order.
3841
3842 @example
3843 sort -n -r
3844 @end example
3845
3846 @item
3847 Sort alphabetically, omitting the first and second fields
3848 and the blanks at the start of the third field.
3849 This uses a single key composed of the characters beginning
3850 at the start of the first nonblank character in field three
3851 and extending to the end of each line.
3852
3853 @example
3854 sort -k 3b
3855 @end example
3856
3857 @item
3858 Sort numerically on the second field and resolve ties by sorting
3859 alphabetically on the third and fourth characters of field five.
3860 Use @samp{:} as the field delimiter.
3861
3862 @example
3863 sort -t : -k 2,2n -k 5.3,5.4
3864 @end example
3865
3866 Note that if you had written @option{-k 2n} instead of @option{-k 2,2n}
3867 @command{sort} would have used all characters beginning in the second field
3868 and extending to the end of the line as the primary @emph{numeric}
3869 key.  For the large majority of applications, treating keys spanning
3870 more than one field as numeric will not do what you expect.
3871
3872 Also note that the @samp{n} modifier was applied to the field-end
3873 specifier for the first key.  It would have been equivalent to
3874 specify @option{-k 2n,2} or @option{-k 2n,2n}.  All modifiers except
3875 @samp{b} apply to the associated @emph{field}, regardless of whether
3876 the modifier character is attached to the field-start and/or the
3877 field-end part of the key specifier.
3878
3879 @item
3880 Sort the password file on the fifth field and ignore any
3881 leading blanks.  Sort lines with equal values in field five
3882 on the numeric user ID in field three.  Fields are separated
3883 by @samp{:}.
3884
3885 @example
3886 sort -t : -k 5b,5 -k 3,3n /etc/passwd
3887 sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
3888 sort -t : -b -k 5,5 -k 3,3n /etc/passwd
3889 @end example
3890
3891 These three commands have equivalent effect.  The first specifies that
3892 the first key's start position ignores leading blanks and the second
3893 key is sorted numerically.  The other two commands rely on global
3894 options being inherited by sort keys that lack modifiers.  The inheritance
3895 works in this case because @option{-k 5b,5b} and @option{-k 5b,5} are
3896 equivalent, as the location of a field-end lacking a @samp{.@var{c}}
3897 character position is not affected by whether initial blanks are
3898 skipped.
3899
3900 @item
3901 Sort a set of log files, primarily by IPv4 address and secondarily by
3902 time stamp.  If two lines' primary and secondary keys are identical,
3903 output the lines in the same order that they were input.  The log
3904 files contain lines that look like this:
3905
3906 @example
3907 4.150.156.3 - - [01/Apr/2004:06:31:51 +0000] message 1
3908 211.24.3.231 - - [24/Apr/2004:20:17:39 +0000] message 2
3909 @end example
3910
3911 Fields are separated by exactly one space.  Sort IPv4 addresses
3912 lexicographically, e.g., 212.61.52.2 sorts before 212.129.233.201
3913 because 61 is less than 129.
3914
3915 @example
3916 sort -s -t ' ' -k 4.9n -k 4.5M -k 4.2n -k 4.14,4.21 file*.log |
3917 sort -s -t '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n
3918 @end example
3919
3920 This example cannot be done with a single @command{sort} invocation,
3921 since IPv4 address components are separated by @samp{.} while dates
3922 come just after a space.  So it is broken down into two invocations of
3923 @command{sort}: the first sorts by time stamp and the second by IPv4
3924 address.  The time stamp is sorted by year, then month, then day, and
3925 finally by hour-minute-second field, using @option{-k} to isolate each
3926 field.  Except for hour-minute-second there's no need to specify the
3927 end of each key field, since the @samp{n} and @samp{M} modifiers sort
3928 based on leading prefixes that cannot cross field boundaries.  The
3929 IPv4 addresses are sorted lexicographically.  The second sort uses
3930 @samp{-s} so that ties in the primary key are broken by the secondary
3931 key; the first sort uses @samp{-s} so that the combination of the two
3932 sorts is stable.
3933
3934 @item
3935 Generate a tags file in case-insensitive sorted order.
3936
3937 @smallexample
3938 find src -type f -print0 | sort -z -f | xargs -0 etags --append
3939 @end smallexample
3940
3941 The use of @option{-print0}, @option{-z}, and @option{-0} in this case means
3942 that file names that contain blanks or other special characters are
3943 not broken up
3944 by the sort operation.
3945
3946 @c This example is a bit contrived and needs more explanation.
3947 @c @item
3948 @c Sort records separated by an arbitrary string by using a pipe to convert
3949 @c each record delimiter string to @samp{\0}, then using sort's -z option,
3950 @c and converting each @samp{\0} back to the original record delimiter.
3951 @c
3952 @c @example
3953 @c printf 'c\n\nb\n\na\n'|perl -0pe 's/\n\n/\n\0/g'|sort -z|perl -0pe 's/\0/\n/g'
3954 @c @end example
3955
3956 @item
3957 Shuffle a list of directories, but preserve the order of files within
3958 each directory.  For instance, one could use this to generate a music
3959 playlist in which albums are shuffled but the songs of each album are
3960 played in order.
3961
3962 @example
3963 ls */* | sort -t / -k 1,1R -k 2,2
3964 @end example
3965
3966 @end itemize
3967
3968
3969 @node shuf invocation
3970 @section @command{shuf}: Shuffling text
3971
3972 @pindex shuf
3973 @cindex shuffling files
3974
3975 @command{shuf} shuffles its input by outputting a random permutation
3976 of its input lines.  Each output permutation is equally likely.
3977 Synopses:
3978
3979 @example
3980 shuf [@var{option}]@dots{} [@var{file}]
3981 shuf -e [@var{option}]@dots{} [@var{arg}]@dots{}
3982 shuf -i @var{lo}-@var{hi} [@var{option}]@dots{}
3983 @end example
3984
3985 @command{shuf} has three modes of operation that affect where it
3986 obtains its input lines.  By default, it reads lines from standard
3987 input.  The following options change the operation mode:
3988
3989 @table @samp
3990
3991 @item -e
3992 @itemx --echo
3993 @opindex -c
3994 @opindex --echo
3995 @cindex command-line operands to shuffle
3996 Treat each command-line operand as an input line.
3997
3998 @item -i @var{lo}-@var{hi}
3999 @itemx --input-range=@var{lo}-@var{hi}
4000 @opindex -i
4001 @opindex --input-range
4002 @cindex input range to shuffle
4003 Act as if input came from a file containing the range of unsigned
4004 decimal integers @var{lo}@dots{}@var{hi}, one per line.
4005
4006 @end table
4007
4008 @command{shuf}'s other options can affect its behavior in all
4009 operation modes:
4010
4011 @table @samp
4012
4013 @item -n @var{lines}
4014 @itemx --head-lines=@var{lines}
4015 @opindex -n
4016 @opindex --head-lines
4017 @cindex head of output
4018 Output at most @var{lines} lines.  By default, all input lines are
4019 output.
4020
4021 @item -o @var{output-file}
4022 @itemx --output=@var{output-file}
4023 @opindex -o
4024 @opindex --output
4025 @cindex overwriting of input, allowed
4026 Write output to @var{output-file} instead of standard output.
4027 @command{shuf} reads all input before opening
4028 @var{output-file}, so you can safely shuffle a file in place by using
4029 commands like @code{shuf -o F <F} and @code{cat F | shuf -o F}.
4030
4031 @item --random-source=@var{file}
4032 @opindex --random-source
4033 @cindex random source for shuffling
4034 Use @var{file} as a source of random data used to determine which
4035 permutation to generate.  @xref{Random sources}.
4036
4037 @item -z
4038 @itemx --zero-terminated
4039 @opindex -z
4040 @opindex --zero-terminated
4041 @cindex sort zero-terminated lines
4042 Treat the input and output as a set of lines, each terminated by a zero byte
4043 (@acronym{ASCII} @sc{nul} (Null) character) instead of an
4044 @acronym{ASCII} @sc{lf} (Line Feed).
4045 This option can be useful in conjunction with @samp{perl -0} or
4046 @samp{find -print0} and @samp{xargs -0} which do the same in order to
4047 reliably handle arbitrary file names (even those containing blanks
4048 or other special characters).
4049
4050 @end table
4051
4052 For example:
4053
4054 @example
4055 shuf <<EOF
4056 A man,
4057 a plan,
4058 a canal:
4059 Panama!
4060 EOF
4061 @end example
4062
4063 @noindent
4064 might produce the output
4065
4066 @example
4067 Panama!
4068 A man,
4069 a canal:
4070 a plan,
4071 @end example
4072
4073 @noindent
4074 Similarly, the command:
4075
4076 @example
4077 shuf -e clubs hearts diamonds spades
4078 @end example
4079
4080 @noindent
4081 might output:
4082
4083 @example
4084 clubs
4085 diamonds
4086 spades
4087 hearts
4088 @end example
4089
4090 @noindent
4091 and the command @samp{shuf -i 1-4} might output:
4092
4093 @example
4094 4
4095 2
4096 1
4097 3
4098 @end example
4099
4100 @noindent
4101 These examples all have four input lines, so @command{shuf} might
4102 produce any of the twenty-four possible permutations of the input.  In
4103 general, if there are @var{N} input lines, there are @var{N}! (i.e.,
4104 @var{N} factorial, or @var{N} * (@var{N} - 1) * @dots{} * 1) possible
4105 output permutations.
4106
4107 @exitstatus
4108
4109
4110 @node uniq invocation
4111 @section @command{uniq}: Uniquify files
4112
4113 @pindex uniq
4114 @cindex uniquify files
4115
4116 @command{uniq} writes the unique lines in the given @file{input}, or
4117 standard input if nothing is given or for an @var{input} name of
4118 @samp{-}.  Synopsis:
4119
4120 @example
4121 uniq [@var{option}]@dots{} [@var{input} [@var{output}]]
4122 @end example
4123
4124 By default, @command{uniq} prints its input lines, except that
4125 it discards all but the first of adjacent repeated lines, so that
4126 no output lines are repeated.  Optionally, it can instead discard
4127 lines that are not repeated, or all repeated lines.
4128
4129 The input need not be sorted, but repeated input lines are detected
4130 only if they are adjacent.  If you want to discard non-adjacent
4131 duplicate lines, perhaps you want to use @code{sort -u}.
4132 @xref{sort invocation}.
4133
4134 @vindex LC_COLLATE
4135 Comparisons use the character collating sequence specified by the
4136 @env{LC_COLLATE} locale category.
4137
4138 If no @var{output} file is specified, @command{uniq} writes to standard
4139 output.
4140
4141 The program accepts the following options.  Also see @ref{Common options}.
4142
4143 @table @samp
4144
4145 @item -f @var{n}
4146 @itemx --skip-fields=@var{n}
4147 @opindex -f
4148 @opindex --skip-fields
4149 Skip @var{n} fields on each line before checking for uniqueness.  Use
4150 a null string for comparison if a line has fewer than @var{n} fields.  Fields
4151 are sequences of non-space non-tab characters that are separated from
4152 each other by at least one space or tab.
4153
4154 For compatibility @command{uniq} supports an obsolete option syntax
4155 @option{-@var{n}}.  New scripts should use @option{-f @var{n}} instead.
4156
4157 @item -s @var{n}
4158 @itemx --skip-chars=@var{n}
4159 @opindex -s
4160 @opindex --skip-chars
4161 Skip @var{n} characters before checking for uniqueness.  Use a null string
4162 for comparison if a line has fewer than @var{n} characters.  If you use both
4163 the field and character skipping options, fields are skipped over first.
4164
4165 @vindex _POSIX2_VERSION
4166 On older systems, @command{uniq} supports an obsolete option syntax
4167 @option{+@var{n}}.
4168 This obsolete behavior can be enabled or disabled with the
4169 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
4170 conformance}), but portable scripts should avoid commands whose
4171 behavior depends on this variable.
4172 For example, use @samp{uniq ./+10} or @samp{uniq -s 10} rather than
4173 the ambiguous @samp{uniq +10}.
4174
4175 @item -c
4176 @itemx --count
4177 @opindex -c
4178 @opindex --count
4179 Print the number of times each line occurred along with the line.
4180
4181 @item -i
4182 @itemx --ignore-case
4183 @opindex -i
4184 @opindex --ignore-case
4185 Ignore differences in case when comparing lines.
4186
4187 @item -d
4188 @itemx --repeated
4189 @opindex -d
4190 @opindex --repeated
4191 @cindex repeated lines, outputting
4192 Discard lines that are not repeated.  When used by itself, this option
4193 causes @command{uniq} to print the first copy of each repeated line,
4194 and nothing else.
4195
4196 @item -D
4197 @itemx --all-repeated[=@var{delimit-method}]
4198 @opindex -D
4199 @opindex --all-repeated
4200 @cindex all repeated lines, outputting
4201 Do not discard the second and subsequent repeated input lines,
4202 but discard lines that are not repeated.
4203 This option is useful mainly in conjunction with other options e.g.,
4204 to ignore case or to compare only selected fields.
4205 The optional @var{delimit-method} tells how to delimit
4206 groups of repeated lines, and must be one of the following:
4207
4208 @table @samp
4209
4210 @item none
4211 Do not delimit groups of repeated lines.
4212 This is equivalent to @option{--all-repeated} (@option{-D}).
4213
4214 @item prepend
4215 Output a newline before each group of repeated lines.
4216
4217 @item separate
4218 Separate groups of repeated lines with a single newline.
4219 This is the same as using @samp{prepend}, except that
4220 there is no newline before the first group, and hence
4221 may be better suited for output direct to users.
4222 @end table
4223
4224 Note that when groups are delimited and the input stream contains
4225 two or more consecutive blank lines, then the output is ambiguous.
4226 To avoid that, filter the input through @samp{tr -s '\n'} to replace
4227 each sequence of consecutive newlines with a single newline.
4228
4229 This is a @sc{gnu} extension.
4230 @c FIXME: give an example showing *how* it's useful
4231
4232 @item -u
4233 @itemx --unique
4234 @opindex -u
4235 @opindex --unique
4236 @cindex unique lines, outputting
4237 Discard the first repeated line.  When used by itself, this option
4238 causes @command{uniq} to print unique lines, and nothing else.
4239
4240 @item -w @var{n}
4241 @itemx --check-chars=@var{n}
4242 @opindex -w
4243 @opindex --check-chars
4244 Compare at most @var{n} characters on each line (after skipping any specified
4245 fields and characters).  By default the entire rest of the lines are
4246 compared.
4247
4248 @end table
4249
4250 @exitstatus
4251
4252
4253 @node comm invocation
4254 @section @command{comm}: Compare two sorted files line by line
4255
4256 @pindex comm
4257 @cindex line-by-line comparison
4258 @cindex comparing sorted files
4259
4260 @command{comm} writes to standard output lines that are common, and lines
4261 that are unique, to two input files; a file name of @samp{-} means
4262 standard input.  Synopsis:
4263
4264 @example
4265 comm [@var{option}]@dots{} @var{file1} @var{file2}
4266 @end example
4267
4268 @vindex LC_COLLATE
4269 Before @command{comm} can be used, the input files must be sorted using the
4270 collating sequence specified by the @env{LC_COLLATE} locale.
4271 If an input file ends in a non-newline
4272 character, a newline is silently appended.  The @command{sort} command with
4273 no options always outputs a file that is suitable input to @command{comm}.
4274
4275 @cindex differing lines
4276 @cindex common lines
4277 With no options, @command{comm} produces three-column output.  Column one
4278 contains lines unique to @var{file1}, column two contains lines unique
4279 to @var{file2}, and column three contains lines common to both files.
4280 Columns are separated by a single TAB character.
4281 @c FIXME: when there's an option to supply an alternative separator
4282 @c string, append `by default' to the above sentence.
4283
4284 @opindex -1
4285 @opindex -2
4286 @opindex -3
4287 The options @option{-1}, @option{-2}, and @option{-3} suppress printing of
4288 the corresponding columns.  Also see @ref{Common options}.
4289
4290 Unlike some other comparison utilities, @command{comm} has an exit
4291 status that does not depend on the result of the comparison.
4292 Upon normal completion @command{comm} produces an exit code of zero.
4293 If there is an error it exits with nonzero status.
4294
4295
4296 @node tsort invocation
4297 @section @command{tsort}: Topological sort
4298
4299 @pindex tsort
4300 @cindex topological sort
4301
4302 @command{tsort} performs a topological sort on the given @var{file}, or
4303 standard input if no input file is given or for a @var{file} of
4304 @samp{-}.  For more details and some history, see @ref{tsort background}.
4305 Synopsis:
4306
4307 @example
4308 tsort [@var{option}] [@var{file}]
4309 @end example
4310
4311 @command{tsort} reads its input as pairs of strings, separated by blanks,
4312 indicating a partial ordering.  The output is a total ordering that
4313 corresponds to the given partial ordering.
4314
4315 For example
4316
4317 @example
4318 tsort <<EOF
4319 a b c
4320 d
4321 e f
4322 b c d e
4323 EOF
4324 @end example
4325
4326 @noindent
4327 will produce the output
4328
4329 @example
4330 a
4331 b
4332 c
4333 d
4334 e
4335 f
4336 @end example
4337
4338 Consider a more realistic example.
4339 You have a large set of functions all in one file, and they may all be
4340 declared static except one.  Currently that one (say @code{main}) is the
4341 first function defined in the file, and the ones it calls directly follow
4342 it, followed by those they call, etc.  Let's say that you are determined
4343 to take advantage of prototypes, so you have to choose between declaring
4344 all of those functions (which means duplicating a lot of information from
4345 the definitions) and rearranging the functions so that as many as possible
4346 are defined before they are used.  One way to automate the latter process
4347 is to get a list for each function of the functions it calls directly.
4348 Many programs can generate such lists.  They describe a call graph.
4349 Consider the following list, in which a given line indicates that the
4350 function on the left calls the one on the right directly.
4351
4352 @example
4353 main parse_options
4354 main tail_file
4355 main tail_forever
4356 tail_file pretty_name
4357 tail_file write_header
4358 tail_file tail
4359 tail_forever recheck
4360 tail_forever pretty_name
4361 tail_forever write_header
4362 tail_forever dump_remainder
4363 tail tail_lines
4364 tail tail_bytes
4365 tail_lines start_lines
4366 tail_lines dump_remainder
4367 tail_lines file_lines
4368 tail_lines pipe_lines
4369 tail_bytes xlseek
4370 tail_bytes start_bytes
4371 tail_bytes dump_remainder
4372 tail_bytes pipe_bytes
4373 file_lines dump_remainder
4374 recheck pretty_name
4375 @end example
4376
4377 then you can use @command{tsort} to produce an ordering of those
4378 functions that satisfies your requirement.
4379
4380 @example
4381 example$ tsort call-graph | tac
4382 dump_remainder
4383 start_lines
4384 file_lines
4385 pipe_lines
4386 xlseek
4387 start_bytes
4388 pipe_bytes
4389 tail_lines
4390 tail_bytes
4391 pretty_name
4392 write_header
4393 tail
4394 recheck
4395 parse_options
4396 tail_file
4397 tail_forever
4398 main
4399 @end example
4400
4401 @command{tsort} detects any cycles in the input and writes the first cycle
4402 encountered to standard error.
4403
4404 Note that for a given partial ordering, generally there is no unique
4405 total ordering.  In the context of the call graph above, the function
4406 @code{parse_options} may be placed anywhere in the list as long as it
4407 precedes @code{main}.
4408
4409 The only options are @option{--help} and @option{--version}.  @xref{Common
4410 options}.
4411
4412 @node tsort background
4413 @section @command{tsort}: Background
4414
4415 @command{tsort} exists because very early versions of the Unix linker processed
4416 an archive file exactly once, and in order.  As @command{ld} read each object
4417 in the archive, it decided whether it was needed in the program based on
4418 whether it defined any symbols which were undefined at that point in
4419 the link.
4420
4421 This meant that dependencies within the archive had to be handled
4422 specially.  For example, @code{scanf} probably calls @code{read}.  That means
4423 that in a single pass through an archive, it was important for @code{scanf.o}
4424 to appear before read.o, because otherwise a program which calls
4425 @code{scanf} but not @code{read} might end up with an unexpected unresolved
4426 reference to @code{read}.
4427
4428 The way to address this problem was to first generate a set of
4429 dependencies of one object file on another.  This was done by a shell
4430 script called @command{lorder}.  The GNU tools don't provide a version of
4431 lorder, as far as I know, but you can still find it in BSD
4432 distributions.
4433
4434 Then you ran @command{tsort} over the @command{lorder} output, and you used the
4435 resulting sort to define the order in which you added objects to the archive.
4436
4437 This whole procedure has been obsolete since about 1980, because
4438 Unix archives now contain a symbol table (traditionally built by
4439 @command{ranlib}, now generally built by @command{ar} itself), and the Unix
4440 linker uses the symbol table to effectively make multiple passes over
4441 an archive file.
4442
4443 Anyhow, that's where tsort came from.  To solve an old problem with
4444 the way the linker handled archive files, which has since been solved
4445 in different ways.
4446
4447 @exitstatus
4448
4449
4450 @node ptx invocation
4451 @section @command{ptx}: Produce permuted indexes
4452
4453 @pindex ptx
4454
4455 @command{ptx} reads a text file and essentially produces a permuted index, with
4456 each keyword in its context.  The calling sketch is either one of:
4457
4458 @example
4459 ptx [@var{option} @dots{}] [@var{file} @dots{}]
4460 ptx -G [@var{option} @dots{}] [@var{input} [@var{output}]]
4461 @end example
4462
4463 The @option{-G} (or its equivalent: @option{--traditional}) option disables
4464 all @sc{gnu} extensions and reverts to traditional mode, thus introducing some
4465 limitations and changing several of the program's default option values.
4466 When @option{-G} is not specified, @sc{gnu} extensions are always enabled.
4467 @sc{gnu} extensions to @command{ptx} are documented wherever appropriate in this
4468 document.  For the full list, see @xref{Compatibility in ptx}.
4469
4470 Individual options are explained in the following sections.
4471
4472 When @sc{gnu} extensions are enabled, there may be zero, one or several
4473 @var{file}s after the options.  If there is no @var{file}, the program
4474 reads the standard input.  If there is one or several @var{file}s, they
4475 give the name of input files which are all read in turn, as if all the
4476 input files were concatenated.  However, there is a full contextual
4477 break between each file and, when automatic referencing is requested,
4478 file names and line numbers refer to individual text input files.  In
4479 all cases, the program outputs the permuted index to the standard
4480 output.
4481
4482 When @sc{gnu} extensions are @emph{not} enabled, that is, when the program
4483 operates in traditional mode, there may be zero, one or two parameters
4484 besides the options.  If there are no parameters, the program reads the
4485 standard input and outputs the permuted index to the standard output.
4486 If there is only one parameter, it names the text @var{input} to be read
4487 instead of the standard input.  If two parameters are given, they give
4488 respectively the name of the @var{input} file to read and the name of
4489 the @var{output} file to produce.  @emph{Be very careful} to note that,
4490 in this case, the contents of file given by the second parameter is
4491 destroyed.  This behavior is dictated by System V @command{ptx}
4492 compatibility; @sc{gnu} Standards normally discourage output parameters not
4493 introduced by an option.
4494
4495 Note that for @emph{any} file named as the value of an option or as an
4496 input text file, a single dash @kbd{-} may be used, in which case
4497 standard input is assumed.  However, it would not make sense to use this
4498 convention more than once per program invocation.
4499
4500 @menu
4501 * General options in ptx::      Options which affect general program behavior.
4502 * Charset selection in ptx::    Underlying character set considerations.
4503 * Input processing in ptx::     Input fields, contexts, and keyword selection.
4504 * Output formatting in ptx::    Types of output format, and sizing the fields.
4505 * Compatibility in ptx::
4506 @end menu
4507
4508
4509 @node General options in ptx
4510 @subsection General options
4511
4512 @table @samp
4513
4514 @item -G
4515 @itemx --traditional
4516 As already explained, this option disables all @sc{gnu} extensions to
4517 @command{ptx} and switches to traditional mode.
4518
4519 @item --help
4520 Print a short help on standard output, then exit without further
4521 processing.
4522
4523 @item --version
4524 Print the program version on standard output, then exit without further
4525 processing.
4526
4527 @end table
4528
4529 @exitstatus
4530
4531
4532 @node Charset selection in ptx
4533 @subsection Charset selection
4534
4535 @c FIXME:  People don't necessarily know what an IBM-PC was these days.
4536 As it is set up now, the program assumes that the input file is coded
4537 using 8-bit @acronym{ISO} 8859-1 code, also known as Latin-1 character set,
4538 @emph{unless} it is compiled for MS-DOS, in which case it uses the
4539 character set of the IBM-PC@.  (@sc{gnu} @command{ptx} is not known to work on
4540 smaller MS-DOS machines anymore.)  Compared to 7-bit @acronym{ASCII}, the set
4541 of characters which are letters is different; this alters the behavior
4542 of regular expression matching.  Thus, the default regular expression
4543 for a keyword allows foreign or diacriticized letters.  Keyword sorting,
4544 however, is still crude; it obeys the underlying character set ordering
4545 quite blindly.
4546
4547 @table @samp
4548
4549 @item -f
4550 @itemx --ignore-case
4551 Fold lower case letters to upper case for sorting.
4552
4553 @end table
4554
4555
4556 @node Input processing in ptx
4557 @subsection Word selection and input processing
4558
4559 @table @samp
4560
4561 @item -b @var{file}
4562 @itemx --break-file=@var{file}
4563
4564 This option provides an alternative (to @option{-W}) method of describing
4565 which characters make up words.  It introduces the name of a
4566 file which contains a list of characters which can@emph{not} be part of
4567 one word; this file is called the @dfn{Break file}.  Any character which
4568 is not part of the Break file is a word constituent.  If both options
4569 @option{-b} and @option{-W} are specified, then @option{-W} has precedence and
4570 @option{-b} is ignored.
4571
4572 When @sc{gnu} extensions are enabled, the only way to avoid newline as a
4573 break character is to write all the break characters in the file with no
4574 newline at all, not even at the end of the file.  When @sc{gnu} extensions
4575 are disabled, spaces, tabs and newlines are always considered as break
4576 characters even if not included in the Break file.
4577
4578 @item -i @var{file}
4579 @itemx --ignore-file=@var{file}
4580
4581 The file associated with this option contains a list of words which will
4582 never be taken as keywords in concordance output.  It is called the
4583 @dfn{Ignore file}.  The file contains exactly one word in each line; the
4584 end of line separation of words is not subject to the value of the
4585 @option{-S} option.
4586
4587 @item -o @var{file}
4588 @itemx --only-file=@var{file}
4589
4590 The file associated with this option contains a list of words which will
4591 be retained in concordance output; any word not mentioned in this file
4592 is ignored.  The file is called the @dfn{Only file}.  The file contains
4593 exactly one word in each line; the end of line separation of words is
4594 not subject to the value of the @option{-S} option.
4595
4596 There is no default for the Only file.  When both an Only file and an
4597 Ignore file are specified, a word is considered a keyword only
4598 if it is listed in the Only file and not in the Ignore file.
4599
4600 @item -r
4601 @itemx --references
4602
4603 On each input line, the leading sequence of non-white space characters will be
4604 taken to be a reference that has the purpose of identifying this input
4605 line in the resulting permuted index.  For more information about reference
4606 production, see @xref{Output formatting in ptx}.
4607 Using this option changes the default value for option @option{-S}.
4608
4609 Using this option, the program does not try very hard to remove
4610 references from contexts in output, but it succeeds in doing so
4611 @emph{when} the context ends exactly at the newline.  If option
4612 @option{-r} is used with @option{-S} default value, or when @sc{gnu} extensions
4613 are disabled, this condition is always met and references are completely
4614 excluded from the output contexts.
4615
4616 @item -S @var{regexp}
4617 @itemx --sentence-regexp=@var{regexp}
4618
4619 This option selects which regular expression will describe the end of a
4620 line or the end of a sentence.  In fact, this regular expression is not
4621 the only distinction between end of lines or end of sentences, and input
4622 line boundaries have no special significance outside this option.  By
4623 default, when @sc{gnu} extensions are enabled and if @option{-r} option is not
4624 used, end of sentences are used.  In this case, this @var{regex} is
4625 imported from @sc{gnu} Emacs:
4626
4627 @example
4628 [.?!][]\"')@}]*\\($\\|\t\\|  \\)[ \t\n]*
4629 @end example
4630
4631 Whenever @sc{gnu} extensions are disabled or if @option{-r} option is used, end
4632 of lines are used; in this case, the default @var{regexp} is just:
4633
4634 @example
4635 \n
4636 @end example
4637
4638 Using an empty @var{regexp} is equivalent to completely disabling end of
4639 line or end of sentence recognition.  In this case, the whole file is
4640 considered to be a single big line or sentence.  The user might want to
4641 disallow all truncation flag generation as well, through option @option{-F
4642 ""}.  @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4643 Manual}.
4644
4645 When the keywords happen to be near the beginning of the input line or
4646 sentence, this often creates an unused area at the beginning of the
4647 output context line; when the keywords happen to be near the end of the
4648 input line or sentence, this often creates an unused area at the end of
4649 the output context line.  The program tries to fill those unused areas
4650 by wrapping around context in them; the tail of the input line or
4651 sentence is used to fill the unused area on the left of the output line;
4652 the head of the input line or sentence is used to fill the unused area
4653 on the right of the output line.
4654
4655 As a matter of convenience to the user, many usual backslashed escape
4656 sequences from the C language are recognized and converted to the
4657 corresponding characters by @command{ptx} itself.
4658
4659 @item -W @var{regexp}
4660 @itemx --word-regexp=@var{regexp}
4661
4662 This option selects which regular expression will describe each keyword.
4663 By default, if @sc{gnu} extensions are enabled, a word is a sequence of
4664 letters; the @var{regexp} used is @samp{\w+}.  When @sc{gnu} extensions are
4665 disabled, a word is by default anything which ends with a space, a tab
4666 or a newline; the @var{regexp} used is @samp{[^ \t\n]+}.
4667
4668 An empty @var{regexp} is equivalent to not using this option.
4669 @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4670 Manual}.
4671
4672 As a matter of convenience to the user, many usual backslashed escape
4673 sequences, as found in the C language, are recognized and converted to
4674 the corresponding characters by @command{ptx} itself.
4675
4676 @end table
4677
4678
4679 @node Output formatting in ptx
4680 @subsection Output formatting
4681
4682 Output format is mainly controlled by the @option{-O} and @option{-T} options
4683 described in the table below.  When neither @option{-O} nor @option{-T} are
4684 selected, and if @sc{gnu} extensions are enabled, the program chooses an
4685 output format suitable for a dumb terminal.  Each keyword occurrence is
4686 output to the center of one line, surrounded by its left and right
4687 contexts.  Each field is properly justified, so the concordance output
4688 can be readily observed.  As a special feature, if automatic
4689 references are selected by option @option{-A} and are output before the
4690 left context, that is, if option @option{-R} is @emph{not} selected, then
4691 a colon is added after the reference; this nicely interfaces with @sc{gnu}
4692 Emacs @code{next-error} processing.  In this default output format, each
4693 white space character, like newline and tab, is merely changed to
4694 exactly one space, with no special attempt to compress consecutive
4695 spaces.  This might change in the future.  Except for those white space
4696 characters, every other character of the underlying set of 256
4697 characters is transmitted verbatim.
4698
4699 Output format is further controlled by the following options.
4700
4701 @table @samp
4702
4703 @item -g @var{number}
4704 @itemx --gap-size=@var{number}
4705
4706 Select the size of the minimum white space gap between the fields on the
4707 output line.
4708
4709 @item -w @var{number}
4710 @itemx --width=@var{number}
4711
4712 Select the maximum output width of each final line.  If references are
4713 used, they are included or excluded from the maximum output width
4714 depending on the value of option @option{-R}.  If this option is not
4715 selected, that is, when references are output before the left context,
4716 the maximum output width takes into account the maximum length of all
4717 references.  If this option is selected, that is, when references are
4718 output after the right context, the maximum output width does not take
4719 into account the space taken by references, nor the gap that precedes
4720 them.
4721
4722 @item -A
4723 @itemx --auto-reference
4724
4725 Select automatic references.  Each input line will have an automatic
4726 reference made up of the file name and the line ordinal, with a single
4727 colon between them.  However, the file name will be empty when standard
4728 input is being read.  If both @option{-A} and @option{-r} are selected, then
4729 the input reference is still read and skipped, but the automatic
4730 reference is used at output time, overriding the input reference.
4731
4732 @item -R
4733 @itemx --right-side-refs
4734
4735 In the default output format, when option @option{-R} is not used, any
4736 references produced by the effect of options @option{-r} or @option{-A} are
4737 placed to the far right of output lines, after the right context.  With
4738 default output format, when the @option{-R} option is specified, references
4739 are rather placed at the beginning of each output line, before the left
4740 context.  For any other output format, option @option{-R} is
4741 ignored, with one exception:  with @option{-R} the width of references
4742 is @emph{not} taken into account in total output width given by @option{-w}.
4743
4744 This option is automatically selected whenever @sc{gnu} extensions are
4745 disabled.
4746
4747 @item -F @var{string}
4748 @itemx --flac-truncation=@var{string}
4749
4750 This option will request that any truncation in the output be reported
4751 using the string @var{string}.  Most output fields theoretically extend
4752 towards the beginning or the end of the current line, or current
4753 sentence, as selected with option @option{-S}.  But there is a maximum
4754 allowed output line width, changeable through option @option{-w}, which is
4755 further divided into space for various output fields.  When a field has
4756 to be truncated because it cannot extend beyond the beginning or the end of
4757 the current line to fit in, then a truncation occurs.  By default,
4758 the string used is a single slash, as in @option{-F /}.
4759
4760 @var{string} may have more than one character, as in @option{-F ...}.
4761 Also, in the particular case when @var{string} is empty (@option{-F ""}),
4762 truncation flagging is disabled, and no truncation marks are appended in
4763 this case.
4764
4765 As a matter of convenience to the user, many usual backslashed escape
4766 sequences, as found in the C language, are recognized and converted to
4767 the corresponding characters by @command{ptx} itself.
4768
4769 @item -M @var{string}
4770 @itemx --macro-name=@var{string}
4771
4772 Select another @var{string} to be used instead of @samp{xx}, while
4773 generating output suitable for @command{nroff}, @command{troff} or @TeX{}.
4774
4775 @item -O
4776 @itemx --format=roff
4777
4778 Choose an output format suitable for @command{nroff} or @command{troff}
4779 processing.  Each output line will look like:
4780
4781 @smallexample
4782 .xx "@var{tail}" "@var{before}" "@var{keyword_and_after}" "@var{head}" "@var{ref}"
4783 @end smallexample
4784
4785 so it will be possible to write a @samp{.xx} roff macro to take care of
4786 the output typesetting.  This is the default output format when @sc{gnu}
4787 extensions are disabled.  Option @option{-M} can be used to change
4788 @samp{xx} to another macro name.
4789
4790 In this output format, each non-graphical character, like newline and
4791 tab, is merely changed to exactly one space, with no special attempt to
4792 compress consecutive spaces.  Each quote character: @kbd{"} is doubled
4793 so it will be correctly processed by @command{nroff} or @command{troff}.
4794
4795 @item -T
4796 @itemx --format=tex
4797
4798 Choose an output format suitable for @TeX{} processing.  Each output
4799 line will look like:
4800
4801 @smallexample
4802 \xx @{@var{tail}@}@{@var{before}@}@{@var{keyword}@}@{@var{after}@}@{@var{head}@}@{@var{ref}@}
4803 @end smallexample
4804
4805 @noindent
4806 so it will be possible to write a @code{\xx} definition to take care of
4807 the output typesetting.  Note that when references are not being
4808 produced, that is, neither option @option{-A} nor option @option{-r} is
4809 selected, the last parameter of each @code{\xx} call is inhibited.
4810 Option @option{-M} can be used to change @samp{xx} to another macro
4811 name.
4812
4813 In this output format, some special characters, like @kbd{$}, @kbd{%},
4814 @kbd{&}, @kbd{#} and @kbd{_} are automatically protected with a
4815 backslash.  Curly brackets @kbd{@{}, @kbd{@}} are protected with a
4816 backslash and a pair of dollar signs (to force mathematical mode).  The
4817 backslash itself produces the sequence @code{\backslash@{@}}.
4818 Circumflex and tilde diacritical marks produce the sequence @code{^\@{ @}} and
4819 @code{~\@{ @}} respectively.  Other diacriticized characters of the
4820 underlying character set produce an appropriate @TeX{} sequence as far
4821 as possible.  The other non-graphical characters, like newline and tab,
4822 and all other characters which are not part of @acronym{ASCII}, are merely
4823 changed to exactly one space, with no special attempt to compress
4824 consecutive spaces.  Let me know how to improve this special character
4825 processing for @TeX{}.
4826
4827 @end table
4828
4829
4830 @node Compatibility in ptx
4831 @subsection The @sc{gnu} extensions to @command{ptx}
4832
4833 This version of @command{ptx} contains a few features which do not exist in
4834 System V @command{ptx}.  These extra features are suppressed by using the
4835 @option{-G} command line option, unless overridden by other command line
4836 options.  Some @sc{gnu} extensions cannot be recovered by overriding, so the
4837 simple rule is to avoid @option{-G} if you care about @sc{gnu} extensions.
4838 Here are the differences between this program and System V @command{ptx}.
4839
4840 @itemize @bullet
4841
4842 @item
4843 This program can read many input files at once, it always writes the
4844 resulting concordance on standard output.  On the other hand, System V
4845 @command{ptx} reads only one file and sends the result to standard output
4846 or, if a second @var{file} parameter is given on the command, to that
4847 @var{file}.
4848
4849 Having output parameters not introduced by options is a dangerous
4850 practice which @sc{gnu} avoids as far as possible.  So, for using @command{ptx}
4851 portably between @sc{gnu} and System V, you should always use it with a
4852 single input file, and always expect the result on standard output.  You
4853 might also want to automatically configure in a @option{-G} option to
4854 @command{ptx} calls in products using @command{ptx}, if the configurator finds
4855 that the installed @command{ptx} accepts @option{-G}.
4856
4857 @item
4858 The only options available in System V @command{ptx} are options @option{-b},
4859 @option{-f}, @option{-g}, @option{-i}, @option{-o}, @option{-r}, @option{-t} and
4860 @option{-w}.  All other options are @sc{gnu} extensions and are not repeated in
4861 this enumeration.  Moreover, some options have a slightly different
4862 meaning when @sc{gnu} extensions are enabled, as explained below.
4863
4864 @item
4865 By default, concordance output is not formatted for @command{troff} or
4866 @command{nroff}.  It is rather formatted for a dumb terminal.  @command{troff}
4867 or @command{nroff} output may still be selected through option @option{-O}.
4868
4869 @item
4870 Unless @option{-R} option is used, the maximum reference width is
4871 subtracted from the total output line width.  With @sc{gnu} extensions
4872 disabled, width of references is not taken into account in the output
4873 line width computations.
4874
4875 @item
4876 All 256 bytes, even null bytes, are always read and processed from
4877 input file with no adverse effect, even if @sc{gnu} extensions are disabled.
4878 However, System V @command{ptx} does not accept 8-bit characters, a few
4879 control characters are rejected, and the tilde @kbd{~} is also rejected.
4880
4881 @item
4882 Input line length is only limited by available memory, even if @sc{gnu}
4883 extensions are disabled.  However, System V @command{ptx} processes only
4884 the first 200 characters in each line.
4885
4886 @item
4887 The break (non-word) characters default to be every character except all
4888 letters of the underlying character set, diacriticized or not.  When @sc{gnu}
4889 extensions are disabled, the break characters default to space, tab and
4890 newline only.
4891
4892 @item
4893 The program makes better use of output line width.  If @sc{gnu} extensions
4894 are disabled, the program rather tries to imitate System V @command{ptx},
4895 but still, there are some slight disposition glitches this program does
4896 not completely reproduce.
4897
4898 @item
4899 The user can specify both an Ignore file and an Only file.  This is not
4900 allowed with System V @command{ptx}.
4901
4902 @end itemize
4903
4904
4905 @node Operating on fields within a line
4906 @chapter Operating on fields within a line
4907
4908 @menu
4909 * cut invocation::              Print selected parts of lines.
4910 * paste invocation::            Merge lines of files.
4911 * join invocation::             Join lines on a common field.
4912 @end menu
4913
4914
4915 @node cut invocation
4916 @section @command{cut}: Print selected parts of lines
4917
4918 @pindex cut
4919 @command{cut} writes to standard output selected parts of each line of each
4920 input file, or standard input if no files are given or for a file name of
4921 @samp{-}.  Synopsis:
4922
4923 @example
4924 cut [@var{option}]@dots{} [@var{file}]@dots{}
4925 @end example
4926
4927 In the table which follows, the @var{byte-list}, @var{character-list},
4928 and @var{field-list} are one or more numbers or ranges (two numbers
4929 separated by a dash) separated by commas.  Bytes, characters, and
4930 fields are numbered starting at 1.  Incomplete ranges may be
4931 given: @option{-@var{m}} means @samp{1-@var{m}}; @samp{@var{n}-} means
4932 @samp{@var{n}} through end of line or last field.  The list elements
4933 can be repeated, can overlap, and can be specified in any order; but
4934 the selected input is written in the same order that it is read, and
4935 is written exactly once.
4936
4937 The program accepts the following options.  Also see @ref{Common
4938 options}.
4939
4940 @table @samp
4941
4942 @item -b @var{byte-list}
4943 @itemx --bytes=@var{byte-list}
4944 @opindex -b
4945 @opindex --bytes
4946 Select for printing only the bytes in positions listed in
4947 @var{byte-list}.  Tabs and backspaces are treated like any other
4948 character; they take up 1 byte.  If an output delimiter is specified,
4949 (see the description of @option{--output-delimiter}), then output that
4950 string between ranges of selected bytes.
4951
4952 @item -c @var{character-list}
4953 @itemx --characters=@var{character-list}
4954 @opindex -c
4955 @opindex --characters
4956 Select for printing only the characters in positions listed in
4957 @var{character-list}.  The same as @option{-b} for now, but
4958 internationalization will change that.  Tabs and backspaces are
4959 treated like any other character; they take up 1 character.  If an
4960 output delimiter is specified, (see the description of
4961 @option{--output-delimiter}), then output that string between ranges
4962 of selected bytes.
4963
4964 @item -f @var{field-list}
4965 @itemx --fields=@var{field-list}
4966 @opindex -f
4967 @opindex --fields
4968 Select for printing only the fields listed in @var{field-list}.
4969 Fields are separated by a TAB character by default.  Also print any
4970 line that contains no delimiter character, unless the
4971 @option{--only-delimited} (@option{-s}) option is specified
4972
4973 @item -d @var{input_delim_byte}
4974 @itemx --delimiter=@var{input_delim_byte}
4975 @opindex -d
4976 @opindex --delimiter
4977 With @option{-f}, use the first byte of @var{input_delim_byte} as
4978 the input fields separator (default is TAB).
4979
4980 @item -n
4981 @opindex -n
4982 Do not split multi-byte characters (no-op for now).
4983
4984 @item -s
4985 @itemx --only-delimited
4986 @opindex -s
4987 @opindex --only-delimited
4988 For @option{-f}, do not print lines that do not contain the field separator
4989 character.  Normally, any line without a field separator is printed verbatim.
4990
4991 @item --output-delimiter=@var{output_delim_string}
4992 @opindex --output-delimiter
4993 With @option{-f}, output fields are separated by @var{output_delim_string}.
4994 The default with @option{-f} is to use the input delimiter.
4995 When using @option{-b} or @option{-c} to select ranges of byte or
4996 character offsets (as opposed to ranges of fields),
4997 output @var{output_delim_string} between non-overlapping
4998 ranges of selected bytes.
4999
5000 @item --complement
5001 @opindex --complement
5002 This option is a @acronym{GNU} extension.
5003 Select for printing the complement of the bytes, characters or fields
5004 selected with the @option{-b}, @option{-c} or @option{-f} options.
5005 In other words, do @emph{not} print the bytes, characters or fields
5006 specified via those options.  This option is useful when you have
5007 many fields and want to print all but a few of them.
5008
5009 @end table
5010
5011 @exitstatus
5012
5013
5014 @node paste invocation
5015 @section @command{paste}: Merge lines of files
5016
5017 @pindex paste
5018 @cindex merging files
5019
5020 @command{paste} writes to standard output lines consisting of sequentially
5021 corresponding lines of each given file, separated by a TAB character.
5022 Standard input is used for a file name of @samp{-} or if no input files
5023 are given.
5024
5025 For example:
5026
5027 @example
5028 $ cat num2
5029 1
5030 2
5031 $ cat let3
5032 a
5033 b
5034 c
5035 $ paste num2 let3
5036 1       a
5037 2       b
5038        @ c
5039 @end example
5040
5041 Synopsis:
5042
5043 @example
5044 paste [@var{option}]@dots{} [@var{file}]@dots{}
5045 @end example
5046
5047 The program accepts the following options.  Also see @ref{Common options}.
5048
5049 @table @samp
5050
5051 @item -s
5052 @itemx --serial
5053 @opindex -s
5054 @opindex --serial
5055 Paste the lines of one file at a time rather than one line from each
5056 file.  Using the above example data:
5057
5058 @example
5059 $ paste -s num2 let3
5060 1       2
5061 a       b       c
5062 @end example
5063
5064 @item -d @var{delim-list}
5065 @itemx --delimiters=@var{delim-list}
5066 @opindex -d
5067 @opindex --delimiters
5068 Consecutively use the characters in @var{delim-list} instead of
5069 TAB to separate merged lines.  When @var{delim-list} is
5070 exhausted, start again at its beginning.  Using the above example data:
5071
5072 @example
5073 $ paste -d '%_' num2 let3 num2
5074 1%a_1
5075 2%b_2
5076 %c_
5077 @end example
5078
5079 @end table
5080
5081 @exitstatus
5082
5083
5084 @node join invocation
5085 @section @command{join}: Join lines on a common field
5086
5087 @pindex join
5088 @cindex common field, joining on
5089
5090 @command{join} writes to standard output a line for each pair of input
5091 lines that have identical join fields.  Synopsis:
5092
5093 @example
5094 join [@var{option}]@dots{} @var{file1} @var{file2}
5095 @end example
5096
5097 Either @var{file1} or @var{file2} (but not both) can be @samp{-},
5098 meaning standard input.  @var{file1} and @var{file2} should be
5099 sorted on the join fields.
5100
5101 @vindex LC_COLLATE
5102 Normally, the sort order is that of the
5103 collating sequence specified by the @env{LC_COLLATE} locale.  Unless
5104 the @option{-t} option is given, the sort comparison ignores blanks at
5105 the start of the join field, as in @code{sort -b}.  If the
5106 @option{--ignore-case} option is given, the sort comparison ignores
5107 the case of characters in the join field, as in @code{sort -f}.
5108
5109 The @command{sort} and @command{join} commands should use consistent
5110 locales and options if the output of @command{sort} is fed to
5111 @command{join}.  You can use a command like @samp{sort -k 1b,1} to
5112 sort a file on its default join field, but if you select a non-default
5113 locale, join field, separator, or comparison options, then you should
5114 do so consistently between @command{join} and @command{sort}.
5115
5116 As a @acronym{GNU} extension, if the input has no unpairable lines the
5117 sort order can be any order that considers two fields to be equal if and
5118 only if the sort comparison described above considers them to be equal.
5119 For example:
5120
5121 @example
5122 $ cat file1
5123 a a1
5124 c c1
5125 b b1
5126 $ cat file2
5127 a a2
5128 c c2
5129 b b2
5130 $ join file1 file2
5131 a a1 a2
5132 c c1 c2
5133 b b1 b2
5134 @end example
5135
5136 The defaults are:
5137 @itemize
5138 @item the join field is the first field in each line;
5139 @item fields in the input are separated by one or more blanks, with leading
5140       blanks on the line ignored;
5141 @item fields in the output are separated by a space;
5142 @item each output line consists of the join field, the remaining
5143 fields from @var{file1}, then the remaining fields from @var{file2}.
5144 @end itemize
5145
5146 The program accepts the following options.  Also see @ref{Common options}.
5147
5148 @table @samp
5149
5150 @item -a @var{file-number}
5151 @opindex -a
5152 Print a line for each unpairable line in file @var{file-number} (either
5153 @samp{1} or @samp{2}), in addition to the normal output.
5154
5155 @item -e @var{string}
5156 @opindex -e
5157 Replace those output fields that are missing in the input with
5158 @var{string}.
5159
5160 @item -i
5161 @itemx --ignore-case
5162 @opindex -i
5163 @opindex --ignore-case
5164 Ignore differences in case when comparing keys.
5165 With this option, the lines of the input files must be ordered in the same way.
5166 Use @samp{sort -f} to produce this ordering.
5167
5168 @item -1 @var{field}
5169 @opindex -1
5170 Join on field @var{field} (a positive integer) of file 1.
5171
5172 @item -2 @var{field}
5173 @opindex -2
5174 Join on field @var{field} (a positive integer) of file 2.
5175
5176 @item -j @var{field}
5177 Equivalent to @option{-1 @var{field} -2 @var{field}}.
5178
5179 @item -o @var{field-list}
5180 Construct each output line according to the format in @var{field-list}.
5181 Each element in @var{field-list} is either the single character @samp{0} or
5182 has the form @var{m.n} where the file number, @var{m}, is @samp{1} or
5183 @samp{2} and @var{n} is a positive field number.
5184
5185 A field specification of @samp{0} denotes the join field.
5186 In most cases, the functionality of the @samp{0} field spec
5187 may be reproduced using the explicit @var{m.n} that corresponds
5188 to the join field.  However, when printing unpairable lines
5189 (using either of the @option{-a} or @option{-v} options), there is no way
5190 to specify the join field using @var{m.n} in @var{field-list}
5191 if there are unpairable lines in both files.
5192 To give @command{join} that functionality, @acronym{POSIX} invented the @samp{0}
5193 field specification notation.
5194
5195 The elements in @var{field-list}
5196 are separated by commas or blanks.
5197 Blank separators typically need to be quoted for the shell.  For
5198 example, the commands @samp{join -o 1.2,2.2} and @samp{join -o '1.2
5199 2.2'} are equivalent.
5200
5201 All output lines---including those printed because of any -a or -v
5202 option---are subject to the specified @var{field-list}.
5203
5204 @item -t @var{char}
5205 Use character @var{char} as the input and output field separator.
5206 Treat as significant each occurrence of @var{char} in the input file.
5207 Use @samp{sort -t @var{char}}, without the @option{-b} option of
5208 @samp{sort}, to produce this ordering.
5209
5210 @item -v @var{file-number}
5211 Print a line for each unpairable line in file @var{file-number}
5212 (either @samp{1} or @samp{2}), instead of the normal output.
5213
5214 @end table
5215
5216 @exitstatus
5217
5218
5219 @node Operating on characters
5220 @chapter Operating on characters
5221
5222 @cindex operating on characters
5223
5224 This commands operate on individual characters.
5225
5226 @menu
5227 * tr invocation::               Translate, squeeze, and/or delete characters.
5228 * expand invocation::           Convert tabs to spaces.
5229 * unexpand invocation::         Convert spaces to tabs.
5230 @end menu
5231
5232
5233 @node tr invocation
5234 @section @command{tr}: Translate, squeeze, and/or delete characters
5235
5236 @pindex tr
5237
5238 Synopsis:
5239
5240 @example
5241 tr [@var{option}]@dots{} @var{set1} [@var{set2}]
5242 @end example
5243
5244 @command{tr} copies standard input to standard output, performing
5245 one of the following operations:
5246
5247 @itemize @bullet
5248 @item
5249 translate, and optionally squeeze repeated characters in the result,
5250 @item
5251 squeeze repeated characters,
5252 @item
5253 delete characters,
5254 @item
5255 delete characters, then squeeze repeated characters from the result.
5256 @end itemize
5257
5258 The @var{set1} and (if given) @var{set2} arguments define ordered
5259 sets of characters, referred to below as @var{set1} and @var{set2}.  These
5260 sets are the characters of the input that @command{tr} operates on.
5261 The @option{--complement} (@option{-c}, @option{-C}) option replaces
5262 @var{set1} with its
5263 complement (all of the characters that are not in @var{set1}).
5264
5265 Currently @command{tr} fully supports only single-byte characters.
5266 Eventually it will support multibyte characters; when it does, the
5267 @option{-C} option will cause it to complement the set of characters,
5268 whereas @option{-c} will cause it to complement the set of values.
5269 This distinction will matter only when some values are not characters,
5270 and this is possible only in locales using multibyte encodings when
5271 the input contains encoding errors.
5272
5273 The program accepts the @option{--help} and @option{--version}
5274 options.  @xref{Common options}.  Options must precede operands.
5275
5276 @exitstatus
5277
5278 @menu
5279 * Character sets::              Specifying sets of characters.
5280 * Translating::                 Changing one set of characters to another.
5281 * Squeezing::                   Squeezing repeats and deleting.
5282 @end menu
5283
5284
5285 @node Character sets
5286 @subsection Specifying sets of characters
5287
5288 @cindex specifying sets of characters
5289
5290 The format of the @var{set1} and @var{set2} arguments resembles
5291 the format of regular expressions; however, they are not regular
5292 expressions, only lists of characters.  Most characters simply
5293 represent themselves in these strings, but the strings can contain
5294 the shorthands listed below, for convenience.  Some of them can be
5295 used only in @var{set1} or @var{set2}, as noted below.
5296
5297 @table @asis
5298
5299 @item Backslash escapes
5300 @cindex backslash escapes
5301
5302 The following backslash escape sequences are recognized:
5303
5304 @table @samp
5305 @item \a
5306 Control-G.
5307 @item \b
5308 Control-H.
5309 @item \f
5310 Control-L.
5311 @item \n
5312 Control-J.
5313 @item \r
5314 Control-M.
5315 @item \t
5316 Control-I.
5317 @item \v
5318 Control-K.
5319 @item \@var{ooo}
5320 The character with the value given by @var{ooo}, which is 1 to 3
5321 octal digits,
5322 @item \\
5323 A backslash.
5324 @end table
5325
5326 While a backslash followed by a character not listed above is
5327 interpreted as that character, the backslash also effectively
5328 removes any special significance, so it is useful to escape
5329 @samp{[}, @samp{]}, @samp{*}, and @samp{-}.
5330
5331 @item Ranges
5332 @cindex ranges
5333
5334 The notation @samp{@var{m}-@var{n}} expands to all of the characters
5335 from @var{m} through @var{n}, in ascending order.  @var{m} should
5336 collate before @var{n}; if it doesn't, an error results.  As an example,
5337 @samp{0-9} is the same as @samp{0123456789}.
5338
5339 @sc{gnu} @command{tr} does not support the System V syntax that uses square
5340 brackets to enclose ranges.  Translations specified in that format
5341 sometimes work as expected, since the brackets are often transliterated
5342 to themselves.  However, they should be avoided because they sometimes
5343 behave unexpectedly.  For example, @samp{tr -d '[0-9]'} deletes brackets
5344 as well as digits.
5345
5346 Many historically common and even accepted uses of ranges are not
5347 portable.  For example, on @acronym{EBCDIC} hosts using the @samp{A-Z}
5348 range will not do what most would expect because @samp{A} through @samp{Z}
5349 are not contiguous as they are in @acronym{ASCII}.
5350 If you can rely on a @acronym{POSIX} compliant version of @command{tr}, then
5351 the best way to work around this is to use character classes (see below).
5352 Otherwise, it is most portable (and most ugly) to enumerate the members
5353 of the ranges.
5354
5355 @item Repeated characters
5356 @cindex repeated characters
5357
5358 The notation @samp{[@var{c}*@var{n}]} in @var{set2} expands to @var{n}
5359 copies of character @var{c}.  Thus, @samp{[y*6]} is the same as
5360 @samp{yyyyyy}.  The notation @samp{[@var{c}*]} in @var{string2} expands
5361 to as many copies of @var{c} as are needed to make @var{set2} as long as
5362 @var{set1}.  If @var{n} begins with @samp{0}, it is interpreted in
5363 octal, otherwise in decimal.
5364
5365 @item Character classes
5366 @cindex character classes
5367
5368 The notation @samp{[:@var{class}:]} expands to all of the characters in
5369 the (predefined) class @var{class}.  The characters expand in no
5370 particular order, except for the @code{upper} and @code{lower} classes,
5371 which expand in ascending order.  When the @option{--delete} (@option{-d})
5372 and @option{--squeeze-repeats} (@option{-s}) options are both given, any
5373 character class can be used in @var{set2}.  Otherwise, only the
5374 character classes @code{lower} and @code{upper} are accepted in
5375 @var{set2}, and then only if the corresponding character class
5376 (@code{upper} and @code{lower}, respectively) is specified in the same
5377 relative position in @var{set1}.  Doing this specifies case conversion.
5378 The class names are given below; an error results when an invalid class
5379 name is given.
5380
5381 @table @code
5382 @item alnum
5383 @opindex alnum
5384 Letters and digits.
5385 @item alpha
5386 @opindex alpha
5387 Letters.
5388 @item blank
5389 @opindex blank
5390 Horizontal whitespace.
5391 @item cntrl
5392 @opindex cntrl
5393 Control characters.
5394 @item digit
5395 @opindex digit
5396 Digits.
5397 @item graph
5398 @opindex graph
5399 Printable characters, not including space.
5400 @item lower
5401 @opindex lower
5402 Lowercase letters.
5403 @item print
5404 @opindex print
5405 Printable characters, including space.
5406 @item punct
5407 @opindex punct
5408 Punctuation characters.
5409 @item space
5410 @opindex space
5411 Horizontal or vertical whitespace.
5412 @item upper
5413 @opindex upper
5414 Uppercase letters.
5415 @item xdigit
5416 @opindex xdigit
5417 Hexadecimal digits.
5418 @end table
5419
5420 @item Equivalence classes
5421 @cindex equivalence classes
5422
5423 The syntax @samp{[=@var{c}=]} expands to all of the characters that are
5424 equivalent to @var{c}, in no particular order.  Equivalence classes are
5425 a relatively recent invention intended to support non-English alphabets.
5426 But there seems to be no standard way to define them or determine their
5427 contents.  Therefore, they are not fully implemented in @sc{gnu} @command{tr};
5428 each character's equivalence class consists only of that character,
5429 which is of no particular use.
5430
5431 @end table
5432
5433
5434 @node Translating
5435 @subsection Translating
5436
5437 @cindex translating characters
5438
5439 @command{tr} performs translation when @var{set1} and @var{set2} are
5440 both given and the @option{--delete} (@option{-d}) option is not given.
5441 @command{tr} translates each character of its input that is in @var{set1}
5442 to the corresponding character in @var{set2}.  Characters not in
5443 @var{set1} are passed through unchanged.  When a character appears more
5444 than once in @var{set1} and the corresponding characters in @var{set2}
5445 are not all the same, only the final one is used.  For example, these
5446 two commands are equivalent:
5447
5448 @example
5449 tr aaa xyz
5450 tr a z
5451 @end example
5452
5453 A common use of @command{tr} is to convert lowercase characters to
5454 uppercase.  This can be done in many ways.  Here are three of them:
5455
5456 @example
5457 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
5458 tr a-z A-Z
5459 tr '[:lower:]' '[:upper:]'
5460 @end example
5461
5462 @noindent
5463 But note that using ranges like @code{a-z} above is not portable.
5464
5465 When @command{tr} is performing translation, @var{set1} and @var{set2}
5466 typically have the same length.  If @var{set1} is shorter than
5467 @var{set2}, the extra characters at the end of @var{set2} are ignored.
5468
5469 On the other hand, making @var{set1} longer than @var{set2} is not
5470 portable; @acronym{POSIX} says that the result is undefined.  In this situation,
5471 BSD @command{tr} pads @var{set2} to the length of @var{set1} by repeating
5472 the last character of @var{set2} as many times as necessary.  System V
5473 @command{tr} truncates @var{set1} to the length of @var{set2}.
5474
5475 By default, @sc{gnu} @command{tr} handles this case like BSD @command{tr}.
5476 When the @option{--truncate-set1} (@option{-t}) option is given,
5477 @sc{gnu} @command{tr} handles this case like the System V @command{tr}
5478 instead.  This option is ignored for operations other than translation.
5479
5480 Acting like System V @command{tr} in this case breaks the relatively common
5481 BSD idiom:
5482
5483 @example
5484 tr -cs A-Za-z0-9 '\012'
5485 @end example
5486
5487 @noindent
5488 because it converts only zero bytes (the first element in the
5489 complement of @var{set1}), rather than all non-alphanumerics, to
5490 newlines.
5491
5492 @noindent
5493 By the way, the above idiom is not portable because it uses ranges, and
5494 it assumes that the octal code for newline is 012.
5495 Assuming a @acronym{POSIX} compliant @command{tr}, here is a better way to write it:
5496
5497 @example
5498 tr -cs '[:alnum:]' '[\n*]'
5499 @end example
5500
5501
5502 @node Squeezing
5503 @subsection Squeezing repeats and deleting
5504
5505 @cindex squeezing repeat characters
5506 @cindex deleting characters
5507
5508 When given just the @option{--delete} (@option{-d}) option, @command{tr}
5509 removes any input characters that are in @var{set1}.
5510
5511 When given just the @option{--squeeze-repeats} (@option{-s}) option,
5512 @command{tr} replaces each input sequence of a repeated character that
5513 is in @var{set1} with a single occurrence of that character.
5514
5515 When given both @option{--delete} and @option{--squeeze-repeats}, @command{tr}
5516 first performs any deletions using @var{set1}, then squeezes repeats
5517 from any remaining characters using @var{set2}.
5518
5519 The @option{--squeeze-repeats} option may also be used when translating,
5520 in which case @command{tr} first performs translation, then squeezes
5521 repeats from any remaining characters using @var{set2}.
5522
5523 Here are some examples to illustrate various combinations of options:
5524
5525 @itemize @bullet
5526
5527 @item
5528 Remove all zero bytes:
5529
5530 @example
5531 tr -d '\0'
5532 @end example
5533
5534 @item
5535 Put all words on lines by themselves.  This converts all
5536 non-alphanumeric characters to newlines, then squeezes each string
5537 of repeated newlines into a single newline:
5538
5539 @example
5540 tr -cs '[:alnum:]' '[\n*]'
5541 @end example
5542
5543 @item
5544 Convert each sequence of repeated newlines to a single newline:
5545
5546 @example
5547 tr -s '\n'
5548 @end example
5549
5550 @item
5551 Find doubled occurrences of words in a document.
5552 @c Separate the following two "the"s, so typo checkers don't complain.
5553 For example, people often write ``the @w{}the'' with the repeated words
5554 separated by a newline.  The Bourne shell script below works first
5555 by converting each sequence of punctuation and blank characters to a
5556 single newline.  That puts each ``word'' on a line by itself.
5557 Next it maps all uppercase characters to lower case, and finally it
5558 runs @command{uniq} with the @option{-d} option to print out only the words
5559 that were repeated.
5560
5561 @example
5562 #!/bin/sh
5563 cat -- "$@@" \
5564   | tr -s '[:punct:][:blank:]' '[\n*]' \
5565   | tr '[:upper:]' '[:lower:]' \
5566   | uniq -d
5567 @end example
5568
5569 @item
5570 Deleting a small set of characters is usually straightforward.  For example,
5571 to remove all @samp{a}s, @samp{x}s, and @samp{M}s you would do this:
5572
5573 @example
5574 tr -d axM
5575 @end example
5576
5577 However, when @samp{-} is one of those characters, it can be tricky because
5578 @samp{-} has special meanings.  Performing the same task as above but also
5579 removing all @samp{-} characters, we might try @code{tr -d -axM}, but
5580 that would fail because @command{tr} would try to interpret @option{-a} as
5581 a command-line option.  Alternatively, we could try putting the hyphen
5582 inside the string, @code{tr -d a-xM}, but that wouldn't work either because
5583 it would make @command{tr} interpret @code{a-x} as the range of characters
5584 @samp{a}@dots{}@samp{x} rather than the three.
5585 One way to solve the problem is to put the hyphen at the end of the list
5586 of characters:
5587
5588 @example
5589 tr -d axM-
5590 @end example
5591
5592 Or you can use @samp{--} to terminate option processing:
5593
5594 @example
5595 tr -d -- -axM
5596 @end example
5597
5598 More generally, use the character class notation @code{[=c=]}
5599 with @samp{-} (or any other character) in place of the @samp{c}:
5600
5601 @example
5602 tr -d '[=-=]axM'
5603 @end example
5604
5605 Note how single quotes are used in the above example to protect the
5606 square brackets from interpretation by a shell.
5607
5608 @end itemize
5609
5610
5611 @node expand invocation
5612 @section @command{expand}: Convert tabs to spaces
5613
5614 @pindex expand
5615 @cindex tabs to spaces, converting
5616 @cindex converting tabs to spaces
5617
5618 @command{expand} writes the contents of each given @var{file}, or standard
5619 input if none are given or for a @var{file} of @samp{-}, to standard
5620 output, with tab characters converted to the appropriate number of
5621 spaces.  Synopsis:
5622
5623 @example
5624 expand [@var{option}]@dots{} [@var{file}]@dots{}
5625 @end example
5626
5627 By default, @command{expand} converts all tabs to spaces.  It preserves
5628 backspace characters in the output; they decrement the column count for
5629 tab calculations.  The default action is equivalent to @option{-t 8} (set
5630 tabs every 8 columns).
5631
5632 The program accepts the following options.  Also see @ref{Common options}.
5633
5634 @table @samp
5635
5636 @item -t @var{tab1}[,@var{tab2}]@dots{}
5637 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5638 @opindex -t
5639 @opindex --tabs
5640 @cindex tab stops, setting
5641 If only one tab stop is given, set the tabs @var{tab1} spaces apart
5642 (default is 8).  Otherwise, set the tabs at columns @var{tab1},
5643 @var{tab2}, @dots{} (numbered from 0), and replace any tabs beyond the
5644 last tab stop given with single spaces.  Tab stops can be separated by
5645 blanks as well as by commas.
5646
5647 For compatibility, GNU @command{expand} also accepts the obsolete
5648 option syntax, @option{-@var{t1}[,@var{t2}]@dots{}}.  New scripts
5649 should use @option{-t @var{t1}[,@var{t2}]@dots{}} instead.
5650
5651 @item -i
5652 @itemx --initial
5653 @opindex -i
5654 @opindex --initial
5655 @cindex initial tabs, converting
5656 Only convert initial tabs (those that precede all non-space or non-tab
5657 characters) on each line to spaces.
5658
5659 @end table
5660
5661 @exitstatus
5662
5663
5664 @node unexpand invocation
5665 @section @command{unexpand}: Convert spaces to tabs
5666
5667 @pindex unexpand
5668
5669 @command{unexpand} writes the contents of each given @var{file}, or
5670 standard input if none are given or for a @var{file} of @samp{-}, to
5671 standard output, converting blanks at the beginning of each line into
5672 as many tab characters as needed.  In the default @acronym{POSIX}
5673 locale, a @dfn{blank} is a space or a tab; other locales may specify
5674 additional blank characters.  Synopsis:
5675
5676 @example
5677 unexpand [@var{option}]@dots{} [@var{file}]@dots{}
5678 @end example
5679
5680 By default, @command{unexpand} converts only initial blanks (those
5681 that precede all non-blank characters) on each line.  It
5682 preserves backspace characters in the output; they decrement the column
5683 count for tab calculations.  By default, tabs are set at every 8th
5684 column.
5685
5686 The program accepts the following options.  Also see @ref{Common options}.
5687
5688 @table @samp
5689
5690 @item -t @var{tab1}[,@var{tab2}]@dots{}
5691 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5692 @opindex -t
5693 @opindex --tabs
5694 If only one tab stop is given, set the tabs @var{tab1} columns apart
5695 instead of the default 8.  Otherwise, set the tabs at columns
5696 @var{tab1}, @var{tab2}, @dots{} (numbered from 0), and leave blanks
5697 beyond the tab stops given unchanged.  Tab stops can be separated by
5698 blanks as well as by commas.  This option implies the @option{-a} option.
5699
5700 For compatibility, GNU @command{unexpand} supports the obsolete option syntax,
5701 @option{-@var{tab1}[,@var{tab2}]@dots{}}, where tab stops must be
5702 separated by commas.  (Unlike @option{-t}, this obsolete option does
5703 not imply @option{-a}.)  New scripts should use @option{--first-only -t
5704 @var{tab1}[,@var{tab2}]@dots{}} instead.
5705
5706 @item -a
5707 @itemx --all
5708 @opindex -a
5709 @opindex --all
5710 Also convert all sequences of two or more blanks just before a tab stop,
5711 even if they occur after non-blank characters in a line.
5712
5713 @end table
5714
5715 @exitstatus
5716
5717
5718 @node Directory listing
5719 @chapter Directory listing
5720
5721 This chapter describes the @command{ls} command and its variants @command{dir}
5722 and @command{vdir}, which list information about files.
5723
5724 @menu
5725 * ls invocation::               List directory contents.
5726 * dir invocation::              Briefly ls.
5727 * vdir invocation::             Verbosely ls.
5728 * dircolors invocation::        Color setup for ls, etc.
5729 @end menu
5730
5731
5732 @node ls invocation
5733 @section @command{ls}: List directory contents
5734
5735 @pindex ls
5736 @cindex directory listing
5737
5738 The @command{ls} program lists information about files (of any type,
5739 including directories).  Options and file arguments can be intermixed
5740 arbitrarily, as usual.
5741
5742 For non-option command-line arguments that are directories, by default
5743 @command{ls} lists the contents of directories, not recursively, and
5744 omitting files with names beginning with @samp{.}.  For other non-option
5745 arguments, by default @command{ls} lists just the file name.  If no
5746 non-option argument is specified, @command{ls} operates on the current
5747 directory, acting as if it had been invoked with a single argument of @samp{.}.
5748
5749 @vindex LC_ALL
5750 By default, the output is sorted alphabetically, according to the locale
5751 settings in effect.@footnote{If you use a non-@acronym{POSIX}
5752 locale (e.g., by setting @env{LC_ALL} to @samp{en_US}), then @command{ls} may
5753 produce output that is sorted differently than you're accustomed to.
5754 In that case, set the @env{LC_ALL} environment variable to @samp{C}.}
5755 If standard output is
5756 a terminal, the output is in columns (sorted vertically) and control
5757 characters are output as question marks; otherwise, the output is listed
5758 one per line and control characters are output as-is.
5759
5760 Because @command{ls} is such a fundamental program, it has accumulated many
5761 options over the years.  They are described in the subsections below;
5762 within each section, options are listed alphabetically (ignoring case).
5763 The division of options into the subsections is not absolute, since some
5764 options affect more than one aspect of @command{ls}'s operation.
5765
5766 @cindex exit status of @command{ls}
5767 Exit status:
5768
5769 @display
5770 0 success
5771 1 minor problems (e.g., a subdirectory was not found)
5772 2 serious trouble (e.g., memory exhausted)
5773 @end display
5774
5775 Also see @ref{Common options}.
5776
5777 @menu
5778 * Which files are listed::
5779 * What information is listed::
5780 * Sorting the output::
5781 * More details about version sort::
5782 * General output formatting::
5783 * Formatting file timestamps::
5784 * Formatting the file names::
5785 @end menu
5786
5787
5788 @node Which files are listed
5789 @subsection Which files are listed
5790
5791 These options determine which files @command{ls} lists information for.
5792 By default, @command{ls} lists files and the contents of any
5793 directories on the command line, except that in directories it ignores
5794 files whose names start with @samp{.}.
5795
5796 @table @samp
5797
5798 @item -a
5799 @itemx --all
5800 @opindex -a
5801 @opindex --all
5802 In directories, do not ignore file names that start with @samp{.}.
5803
5804 @item -A
5805 @itemx --almost-all
5806 @opindex -A
5807 @opindex --almost-all
5808 In directories, do not ignore all file names that start with @samp{.};
5809 ignore only @file{.} and @file{..}.  The @option{--all} (@option{-a})
5810 option overrides this option.
5811
5812 @item -B
5813 @itemx --ignore-backups
5814 @opindex -B
5815 @opindex --ignore-backups
5816 @cindex backup files, ignoring
5817 In directories, ignore files that end with @samp{~}.  This option is
5818 equivalent to @samp{--ignore='*~' --ignore='.*~'}.
5819
5820 @item -d
5821 @itemx --directory
5822 @opindex -d
5823 @opindex --directory
5824 List just the names of directories, as with other types of files, rather
5825 than listing their contents.
5826 @c The following sentence is the same as the one for -F.
5827 Do not follow symbolic links listed on the
5828 command line unless the @option{--dereference-command-line} (@option{-H}),
5829 @option{--dereference} (@option{-L}), or
5830 @option{--dereference-command-line-symlink-to-dir} options are specified.
5831
5832 @item -H
5833 @itemx --dereference-command-line
5834 @opindex -H
5835 @opindex --dereference-command-line
5836 @cindex symbolic links, dereferencing
5837 If a command line argument specifies a symbolic link, show information
5838 for the file the link references rather than for the link itself.
5839
5840 @itemx --dereference-command-line-symlink-to-dir
5841 @opindex --dereference-command-line-symlink-to-dir
5842 @cindex symbolic links, dereferencing
5843 Do not dereference symbolic links, with one exception:
5844 if a command line argument specifies a symbolic link that refers to
5845 a directory, show information for that directory rather than for the
5846 link itself.
5847 This is the default behavior when no other dereferencing-related
5848 option has been specified (@option{--classify} (@option{-F}),
5849 @option{--directory} (@option{-d}),
5850 (@option{-l}),
5851 @option{--dereference} (@option{-L}), or
5852 @option{--dereference-command-line} (@option{-H})).
5853
5854 @item --group-directories-first
5855 @opindex --group-directories-first
5856 Group all the directories before the files and then sort the
5857 directories and the files separately using the selected sort key
5858 (see --sort option).
5859 That is, this option specifies a primary sort key,
5860 and the --sort option specifies a secondary key.
5861
5862 @item --hide=PATTERN
5863 @opindex --hide=@var{pattern}
5864 In directories, ignore files whose names match the shell pattern
5865 @var{pattern}, unless the @option{--all} (@option{-a}) or
5866 @option{--almost-all} (@option{-A}) is also given.  This
5867 option acts like @option{--ignore=@var{pattern}} except that it has no
5868 effect if @option{--all} (@option{-a}) or @option{--almost-all}
5869 (@option{-A}) is also given.
5870
5871 This option can be useful in shell aliases.  For example, if
5872 @command{lx} is an alias for @samp{ls --hide='*~'} and @command{ly} is
5873 an alias for @samp{ls --ignore='*~'}, then the command @samp{lx -A}
5874 lists the file @file{README~} even though @samp{ly -A} would not.
5875
5876 @item -I @var{pattern}
5877 @itemx --ignore=@var{pattern}
5878 @opindex -I
5879 @opindex --ignore=@var{pattern}
5880 In directories, ignore files whose names match the shell pattern
5881 (not regular expression) @var{pattern}.  As
5882 in the shell, an initial @samp{.} in a file name does not match a
5883 wildcard at the start of @var{pattern}.  Sometimes it is useful
5884 to give this option several times.  For example,
5885
5886 @smallexample
5887 $ ls --ignore='.??*' --ignore='.[^.]' --ignore='#*'
5888 @end smallexample
5889
5890 The first option ignores names of length 3 or more that start with @samp{.},
5891 the second ignores all two-character names that start with @samp{.}
5892 except @samp{..}, and the third ignores names that start with @samp{#}.
5893
5894 @item -L
5895 @itemx --dereference
5896 @opindex -L
5897 @opindex --dereference
5898 @cindex symbolic links, dereferencing
5899 When showing file information for a symbolic link, show information
5900 for the file the link references rather than the link itself.
5901 However, even with this option, @command{ls} still prints the name
5902 of the link itself, not the name of the file that the link points to.
5903
5904 @item -R
5905 @itemx --recursive
5906 @opindex -R
5907 @opindex --recursive
5908 @cindex recursive directory listing
5909 @cindex directory listing, recursive
5910 List the contents of all directories recursively.
5911
5912 @end table
5913
5914
5915 @node What information is listed
5916 @subsection What information is listed
5917
5918 These options affect the information that @command{ls} displays.  By
5919 default, only file names are shown.
5920
5921 @table @samp
5922
5923 @item --author
5924 @opindex --author
5925 @cindex hurd, author, printing
5926 List each file's author when producing long format directory listings.
5927 In GNU/Hurd, file authors can differ from their owners, but in other
5928 operating systems the two are the same.
5929
5930 @item -D
5931 @itemx --dired
5932 @opindex -D
5933 @opindex --dired
5934 @cindex dired Emacs mode support
5935 With the long listing (@option{-l}) format, print an additional line after
5936 the main output:
5937
5938 @example
5939 //DIRED// @var{beg1} @var{end1} @var{beg2} @var{end2} @dots{}
5940 @end example
5941
5942 @noindent
5943 The @var{begN} and @var{endN} are unsigned integers that record the
5944 byte position of the beginning and end of each file name in the output.
5945 This makes it easy for Emacs to find the names, even when they contain
5946 unusual characters such as space or newline, without fancy searching.
5947
5948 If directories are being listed recursively (@option{-R}), output a similar
5949 line with offsets for each subdirectory name:
5950
5951 @example
5952 //SUBDIRED// @var{beg1} @var{end1} @dots{}
5953 @end example
5954
5955 Finally, output a line of the form:
5956
5957 @example
5958 //DIRED-OPTIONS// --quoting-style=@var{word}
5959 @end example
5960
5961 @noindent
5962 where @var{word} is the quoting style (@pxref{Formatting the file names}).
5963
5964 Here is an actual example:
5965
5966 @example
5967 $ mkdir -p a/sub/deeper a/sub2
5968 $ touch a/f1 a/f2
5969 $ touch a/sub/deeper/file
5970 $ ls -gloRF --dired a
5971   a:
5972   total 8
5973   -rw-r--r-- 1    0 Jun 10 12:27 f1
5974   -rw-r--r-- 1    0 Jun 10 12:27 f2
5975   drwxr-xr-x 3 4096 Jun 10 12:27 sub/
5976   drwxr-xr-x 2 4096 Jun 10 12:27 sub2/
5977
5978   a/sub:
5979   total 4
5980   drwxr-xr-x 2 4096 Jun 10 12:27 deeper/
5981
5982   a/sub/deeper:
5983   total 0
5984   -rw-r--r-- 1 0 Jun 10 12:27 file
5985
5986   a/sub2:
5987   total 0
5988 //DIRED// 48 50 84 86 120 123 158 162 217 223 282 286
5989 //SUBDIRED// 2 3 167 172 228 240 290 296
5990 //DIRED-OPTIONS// --quoting-style=literal
5991 @end example
5992
5993 Note that the pairs of offsets on the @samp{//DIRED//} line above delimit
5994 these names: @file{f1}, @file{f2}, @file{sub}, @file{sub2}, @file{deeper},
5995 @file{file}.
5996 The offsets on the @samp{//SUBDIRED//} line delimit the following
5997 directory names: @file{a}, @file{a/sub}, @file{a/sub/deeper}, @file{a/sub2}.
5998
5999 Here is an example of how to extract the fifth entry name, @samp{deeper},
6000 corresponding to the pair of offsets, 222 and 228:
6001
6002 @example
6003 $ ls -gloRF --dired a > out
6004 $ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
6005 deeper
6006 @end example
6007
6008 Note that although the listing above includes a trailing slash
6009 for the @samp{deeper} entry, the offsets select the name without
6010 the trailing slash.  However, if you invoke @command{ls} with @option{--dired}
6011 along with an option like @option{--escape} (aka @option{-b}) and operate
6012 on a file whose name contains special characters, notice that the backslash
6013 @emph{is} included:
6014
6015 @example
6016 $ touch 'a b'
6017 $ ls -blog --dired 'a b'
6018   -rw-r--r-- 1 0 Jun 10 12:28 a\ b
6019 //DIRED// 30 34
6020 //DIRED-OPTIONS// --quoting-style=escape
6021 @end example
6022
6023 If you use a quoting style that adds quote marks
6024 (e.g., @option{--quoting-style=c}), then the offsets include the quote marks.
6025 So beware that the user may select the quoting style via the environment
6026 variable @env{QUOTING_STYLE}.  Hence, applications using @option{--dired}
6027 should either specify an explicit @option{--quoting-style=literal} option
6028 (aka @option{-N} or @option{--literal}) on the command line, or else be
6029 prepared to parse the escaped names.
6030
6031 @item --full-time
6032 @opindex --full-time
6033 Produce long format directory listings, and list times in full.  It is
6034 equivalent to using @option{--format=long} with
6035 @option{--time-style=full-iso} (@pxref{Formatting file timestamps}).
6036
6037 @item -g
6038 @opindex -g
6039 Produce long format directory listings, but don't display owner information.
6040
6041 @item -G
6042 @itemx --no-group
6043 @opindex -G
6044 @opindex --no-group
6045 Inhibit display of group information in a long format directory listing.
6046 (This is the default in some non-@sc{gnu} versions of @command{ls}, so we
6047 provide this option for compatibility.)
6048
6049 @optHumanReadable
6050
6051 @item -i
6052 @itemx --inode
6053 @opindex -i
6054 @opindex --inode
6055 @cindex inode number, printing
6056 Print the inode number (also called the file serial number and index
6057 number) of each file to the left of the file name.  (This number
6058 uniquely identifies each file within a particular file system.)
6059
6060 @item -l
6061 @itemx --format=long
6062 @itemx --format=verbose
6063 @opindex -l
6064 @opindex --format
6065 @opindex long ls @r{format}
6066 @opindex verbose ls @r{format}
6067 In addition to the name of each file, print the file type, file mode bits,
6068 number of hard links, owner name, group name, size, and
6069 timestamp (@pxref{Formatting file timestamps}), normally
6070 the modification time.  Print question marks for information that
6071 cannot be determined.
6072
6073 Normally the size is printed as a byte count without punctuation, but
6074 this can be overridden (@pxref{Block size}).  For example, @option{-h}
6075 prints an abbreviated, human-readable count, and
6076 @samp{--block-size="'1"} prints a byte count with the thousands
6077 separator of the current locale.
6078
6079 For each directory that is listed, preface the files with a line
6080 @samp{total @var{blocks}}, where @var{blocks} is the total disk allocation
6081 for all files in that directory.  The block size currently defaults to 1024
6082 bytes, but this can be overridden (@pxref{Block size}).
6083 The @var{blocks} computed counts each hard link separately;
6084 this is arguably a deficiency.
6085
6086 The file type is one of the following characters:
6087
6088 @c The commented-out entries are ones we're not sure about.
6089
6090 @table @samp
6091 @item -
6092 regular file
6093 @item b
6094 block special file
6095 @item c
6096 character special file
6097 @item C
6098 high performance (``contiguous data'') file
6099 @item d
6100 directory
6101 @item D
6102 door (Solaris 2.5 and up)
6103 @c @item F
6104 @c semaphore, if this is a distinct file type
6105 @item l
6106 symbolic link
6107 @c @item m
6108 @c multiplexed file (7th edition Unix; obsolete)
6109 @item M
6110 off-line (``migrated'') file (Cray DMF)
6111 @item n
6112 network special file (HP-UX)
6113 @item p
6114 FIFO (named pipe)
6115 @item P
6116 port (Solaris 10 and up)
6117 @c @item Q
6118 @c message queue, if this is a distinct file type
6119 @item s
6120 socket
6121 @c @item S
6122 @c shared memory object, if this is a distinct file type
6123 @c @item T
6124 @c typed memory object, if this is a distinct file type
6125 @c @item w
6126 @c whiteout (4.4BSD; not implemented)
6127 @item ?
6128 some other file type
6129 @end table
6130
6131 @cindex permissions, output by @command{ls}
6132 The file mode bits listed are similar to symbolic mode specifications
6133 (@pxref{Symbolic Modes}).  But @command{ls} combines multiple bits into the
6134 third character of each set of permissions as follows:
6135
6136 @table @samp
6137 @item s
6138 If the set-user-ID or set-group-ID bit and the corresponding executable bit
6139 are both set.
6140
6141 @item S
6142 If the set-user-ID or set-group-ID bit is set but the corresponding
6143 executable bit is not set.
6144
6145 @item t
6146 If the restricted deletion flag or sticky bit, and the
6147 other-executable bit, are both set.  The restricted deletion flag is
6148 another name for the sticky bit.  @xref{Mode Structure}.
6149
6150 @item T
6151 If the restricted deletion flag or sticky bit is set but the
6152 other-executable bit is not set.
6153
6154 @item x
6155 If the executable bit is set and none of the above apply.
6156
6157 @item -
6158 Otherwise.
6159 @end table
6160
6161 Following the file mode bits is a single character that specifies
6162 whether an alternate access method such as an access control list
6163 applies to the file.  When the character following the file mode bits is a
6164 space, there is no alternate access method.  When it is a printing
6165 character, then there is such a method.
6166
6167 For a file with an extended access control list, a @samp{+} character is
6168 listed.  Basic access control lists are equivalent to the permissions
6169 listed, and are not considered an alternate access method.
6170
6171 @item -n
6172 @itemx --numeric-uid-gid
6173 @opindex -n
6174 @opindex --numeric-uid-gid
6175 @cindex numeric uid and gid
6176 @cindex numeric user and group IDs
6177 Produce long format directory listings, but
6178 display numeric user and group IDs instead of the owner and group names.
6179
6180 @item -o
6181 @opindex -o
6182 Produce long format directory listings, but don't display group information.
6183 It is equivalent to using @option{--format=long} with @option{--no-group} .
6184
6185 @item -s
6186 @itemx --size
6187 @opindex -s
6188 @opindex --size
6189 @cindex disk allocation
6190 @cindex size of files, reporting
6191 Print the disk allocation of each file to the left of the file name.
6192 This is the amount of disk space used by the file, which is usually a
6193 bit more than the file's size, but it can be less if the file has holes.
6194
6195 Normally the disk allocation is printed in units of
6196 1024 bytes, but this can be overridden (@pxref{Block size}).
6197
6198 @cindex NFS mounts from BSD to HP-UX
6199 For files that are NFS-mounted from an HP-UX system to a BSD system,
6200 this option reports sizes that are half the correct values.  On HP-UX
6201 systems, it reports sizes that are twice the correct values for files
6202 that are NFS-mounted from BSD systems.  This is due to a flaw in HP-UX;
6203 it also affects the HP-UX @command{ls} program.
6204
6205 @optSi
6206
6207 @end table
6208
6209
6210 @node Sorting the output
6211 @subsection Sorting the output
6212
6213 @cindex sorting @command{ls} output
6214 These options change the order in which @command{ls} sorts the information
6215 it outputs.  By default, sorting is done by character code
6216 (e.g., @acronym{ASCII} order).
6217
6218 @table @samp
6219
6220 @item -c
6221 @itemx --time=ctime
6222 @itemx --time=status
6223 @opindex -c
6224 @opindex --time
6225 @opindex ctime@r{, printing or sorting by}
6226 @opindex status time@r{, printing or sorting by}
6227 @opindex use time@r{, printing or sorting files by}
6228 If the long listing format (e.g., @option{-l}, @option{-o}) is being used,
6229 print the status change time (the @samp{ctime} in the inode) instead of
6230 the modification time.
6231 When explicitly sorting by time (@option{--sort=time} or @option{-t})
6232 or when not using a long listing format,
6233 sort according to the status change time.
6234
6235 @item -f
6236 @opindex -f
6237 @cindex unsorted directory listing
6238 @cindex directory order, listing by
6239 Primarily, like @option{-U}---do not sort; list the files in whatever
6240 order they are stored in the directory.  But also enable @option{-a} (list
6241 all files) and disable @option{-l}, @option{--color}, and @option{-s} (if they
6242 were specified before the @option{-f}).
6243
6244 @item -r
6245 @itemx --reverse
6246 @opindex -r
6247 @opindex --reverse
6248 @cindex reverse sorting
6249 Reverse whatever the sorting method is---e.g., list files in reverse
6250 alphabetical order, youngest first, smallest first, or whatever.
6251
6252 @item -S
6253 @itemx --sort=size
6254 @opindex -S
6255 @opindex --sort
6256 @opindex size of files@r{, sorting files by}
6257 Sort by file size, largest first.
6258
6259 @item -t
6260 @itemx --sort=time
6261 @opindex -t
6262 @opindex --sort
6263 @opindex modification time@r{, sorting files by}
6264 Sort by modification time (the @samp{mtime} in the inode), newest first.
6265
6266 @item -u
6267 @itemx --time=atime
6268 @itemx --time=access
6269 @itemx --time=use
6270 @opindex -u
6271 @opindex --time
6272 @opindex use time@r{, printing or sorting files by}
6273 @opindex atime@r{, printing or sorting files by}
6274 @opindex access time@r{, printing or sorting files by}
6275 If the long listing format (e.g., @option{--format=long}) is being used,
6276 print the last access time (the @samp{atime} in the inode).
6277 When explicitly sorting by time (@option{--sort=time} or @option{-t})
6278 or when not using a long listing format, sort according to the access time.
6279
6280 @item -U
6281 @itemx --sort=none
6282 @opindex -U
6283 @opindex --sort
6284 @opindex none@r{, sorting option for @command{ls}}
6285 Do not sort; list the files in whatever order they are
6286 stored in the directory.  (Do not do any of the other unrelated things
6287 that @option{-f} does.)  This is especially useful when listing very large
6288 directories, since not doing any sorting can be noticeably faster.
6289
6290 @item -v
6291 @itemx --sort=version
6292 @opindex -v
6293 @opindex --sort
6294 @opindex version@r{, sorting option for @command{ls}}
6295 Sort by version name and number, lowest first.  It behaves like a default
6296 sort, except that each sequence of decimal digits is treated numerically
6297 as an index/version number.  (@xref{More details about version sort}.)
6298
6299 @item -X
6300 @itemx --sort=extension
6301 @opindex -X
6302 @opindex --sort
6303 @opindex extension@r{, sorting files by}
6304 Sort directory contents alphabetically by file extension (characters
6305 after the last @samp{.}); files with no extension are sorted first.
6306
6307 @end table
6308
6309
6310 @node More details about version sort
6311 @subsection More details about version sort
6312
6313 The version sort takes into account the fact that file names frequently include
6314 indices or version numbers.  Standard sorting functions usually do not produce
6315 the ordering that people expect because comparisons are made on a
6316 character-by-character basis.  The version
6317 sort addresses this problem, and is especially useful when browsing
6318 directories that contain many files with indices/version numbers in their
6319 names:
6320
6321 @example
6322 $ ls -1            $ ls -1v
6323 foo.zml-1.gz       foo.zml-1.gz
6324 foo.zml-100.gz     foo.zml-2.gz
6325 foo.zml-12.gz      foo.zml-6.gz
6326 foo.zml-13.gz      foo.zml-12.gz
6327 foo.zml-2.gz       foo.zml-13.gz
6328 foo.zml-25.gz      foo.zml-25.gz
6329 foo.zml-6.gz       foo.zml-100.gz
6330 @end example
6331
6332 Note also that numeric parts with leading zeros are considered as
6333 fractional one:
6334
6335 @example
6336 $ ls -1            $ ls -1v
6337 abc-1.007.tgz      abc-1.007.tgz
6338 abc-1.012b.tgz     abc-1.01a.tgz
6339 abc-1.01a.tgz      abc-1.012b.tgz
6340 @end example
6341
6342 This functionality is implemented using the @code{strverscmp} function.
6343 @xref{String/Array Comparison, , , libc, The GNU C Library Reference Manual}.
6344 One result of that implementation decision is that @code{ls -v} does not
6345 use the locale category, @env{LC_COLLATE}.  As a result, non-numeric prefixes
6346 are sorted as if @env{LC_COLLATE} were set to @code{C}.
6347
6348 @node General output formatting
6349 @subsection General output formatting
6350
6351 These options affect the appearance of the overall output.
6352
6353 @table @samp
6354
6355 @item -1
6356 @itemx --format=single-column
6357 @opindex -1
6358 @opindex --format
6359 @opindex single-column @r{output of files}
6360 List one file per line.  This is the default for @command{ls} when standard
6361 output is not a terminal.
6362
6363 @item -C
6364 @itemx --format=vertical
6365 @opindex -C
6366 @opindex --format
6367 @opindex vertical @r{sorted files in columns}
6368 List files in columns, sorted vertically.  This is the default for
6369 @command{ls} if standard output is a terminal.  It is always the default
6370 for the @command{dir} program.
6371 @sc{gnu} @command{ls} uses variable width columns to display as many files as
6372 possible in the fewest lines.
6373
6374 @item --color [=@var{when}]
6375 @opindex --color
6376 @cindex color, distinguishing file types with
6377 Specify whether to use color for distinguishing file types.  @var{when}
6378 may be omitted, or one of:
6379 @itemize @bullet
6380 @item none
6381 @vindex none @r{color option}
6382 - Do not use color at all.  This is the default.
6383 @item auto
6384 @vindex auto @r{color option}
6385 @cindex terminal, using color iff
6386 - Only use color if standard output is a terminal.
6387 @item always
6388 @vindex always @r{color option}
6389 - Always use color.
6390 @end itemize
6391 Specifying @option{--color} and no @var{when} is equivalent to
6392 @option{--color=always}.
6393 Piping a colorized listing through a pager like @command{more} or
6394 @command{less} usually produces unreadable results.  However, using
6395 @code{more -f} does seem to work.
6396
6397 @item -F
6398 @itemx --classify
6399 @itemx --indicator-style=classify
6400 @opindex -F
6401 @opindex --classify
6402 @opindex --indicator-style
6403 @cindex file type and executables, marking
6404 @cindex executables and file type, marking
6405 Append a character to each file name indicating the file type.  Also,
6406 for regular files that are executable, append @samp{*}.  The file type
6407 indicators are @samp{/} for directories, @samp{@@} for symbolic links,
6408 @samp{|} for FIFOs, @samp{=} for sockets, @samp{>} for doors,
6409 and nothing for regular files.
6410 @c The following sentence is the same as the one for -d.
6411 Do not follow symbolic links listed on the
6412 command line unless the @option{--dereference-command-line} (@option{-H}),
6413 @option{--dereference} (@option{-L}), or
6414 @option{--dereference-command-line-symlink-to-dir} options are specified.
6415
6416 @item --file-type
6417 @itemx --indicator-style=file-type
6418 @opindex --file-type
6419 @opindex --indicator-style
6420 @cindex file type, marking
6421 Append a character to each file name indicating the file type.  This is
6422 like @option{-F}, except that executables are not marked.
6423
6424 @item --indicator-style=@var{word}
6425 @opindex --indicator-style
6426 Append a character indicator with style @var{word} to entry names,
6427 as follows:
6428
6429 @table @samp
6430 @item none
6431 Do not append any character indicator; this is the default.
6432 @item slash
6433 Append @samp{/} for directories.  This is the same as the @option{-p}
6434 option.
6435 @item file-type
6436 Append @samp{/} for directories, @samp{@@} for symbolic links, @samp{|}
6437 for FIFOs, @samp{=} for sockets, and nothing for regular files.  This is
6438 the same as the @option{--file-type} option.
6439 @item classify
6440 Append @samp{*} for executable regular files, otherwise behave as for
6441 @samp{file-type}.  This is the same as the @option{-F} or
6442 @option{--classify} option.
6443 @end table
6444
6445 @item -k
6446 @opindex -k
6447 Print file sizes in 1024-byte blocks, overriding the default block
6448 size (@pxref{Block size}).
6449 This option is equivalent to @option{--block-size=1K}.
6450
6451 @item -m
6452 @itemx --format=commas
6453 @opindex -m
6454 @opindex --format
6455 @opindex commas@r{, outputting between files}
6456 List files horizontally, with as many as will fit on each line,
6457 separated by @samp{, } (a comma and a space).
6458
6459 @item -p
6460 @itemx --indicator-style=slash
6461 @opindex -p
6462 @opindex --indicator-style
6463 @cindex file type, marking
6464 Append a @samp{/} to directory names.
6465
6466 @item -x
6467 @itemx --format=across
6468 @itemx --format=horizontal
6469 @opindex -x
6470 @opindex --format
6471 @opindex across@r{, listing files}
6472 @opindex horizontal@r{, listing files}
6473 List the files in columns, sorted horizontally.
6474
6475 @item -T @var{cols}
6476 @itemx --tabsize=@var{cols}
6477 @opindex -T
6478 @opindex --tabsize
6479 Assume that each tab stop is @var{cols} columns wide.  The default is 8.
6480 @command{ls} uses tabs where possible in the output, for efficiency.  If
6481 @var{cols} is zero, do not use tabs at all.
6482
6483 @c FIXME: remove in 2009, if Apple Terminal has been fixed for long enough.
6484 Some terminal emulators (at least Apple Terminal 1.5 (133) from Mac OS X 10.4.8)
6485 do not properly align columns to the right of a TAB following a
6486 non-@acronym{ASCII} byte.  If you use such a terminal emulator, use the
6487 @option{-T0} option or put @code{TABSIZE=0} in your environment to tell
6488 @command{ls} to align using spaces, not tabs.
6489
6490 @item -w
6491 @itemx --width=@var{cols}
6492 @opindex -w
6493 @opindex --width
6494 @vindex COLUMNS
6495 Assume the screen is @var{cols} columns wide.  The default is taken
6496 from the terminal settings if possible; otherwise the environment
6497 variable @env{COLUMNS} is used if it is set; otherwise the default
6498 is 80.
6499
6500 @end table
6501
6502
6503 @node Formatting file timestamps
6504 @subsection Formatting file timestamps
6505
6506 By default, file timestamps are listed in abbreviated form.  Most
6507 locales use a timestamp like @samp{2002-03-30 23:45}.  However, the
6508 default @acronym{POSIX} locale uses a date like @samp{Mar 30@ @ 2002}
6509 for non-recent timestamps, and a date-without-year and time like
6510 @samp{Mar 30 23:45} for recent timestamps.
6511
6512 A timestamp is considered to be @dfn{recent} if it is less than six
6513 months old, and is not dated in the future.  If a timestamp dated
6514 today is not listed in recent form, the timestamp is in the future,
6515 which means you probably have clock skew problems which may break
6516 programs like @command{make} that rely on file timestamps.
6517
6518 @vindex TZ
6519 Time stamps are listed according to the time zone rules specified by
6520 the @env{TZ} environment variable, or by the system default rules if
6521 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
6522 with @env{TZ}, libc, The GNU C Library}.
6523
6524 The following option changes how file timestamps are printed.
6525
6526 @table @samp
6527 @item --time-style=@var{style}
6528 @opindex --time-style
6529 @cindex time style
6530 List timestamps in style @var{style}.  The @var{style} should
6531 be one of the following:
6532
6533 @table @samp
6534 @item +@var{format}
6535 @vindex LC_TIME
6536 List timestamps using @var{format}, where @var{format} is interpreted
6537 like the format argument of @command{date} (@pxref{date invocation}).
6538 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
6539 @command{ls} to list timestamps like @samp{2002-03-30 23:45:56}.  As
6540 with @command{date}, @var{format}'s interpretation is affected by the
6541 @env{LC_TIME} locale category.
6542
6543 If @var{format} contains two format strings separated by a newline,
6544 the former is used for non-recent files and the latter for recent
6545 files; if you want output columns to line up, you may need to insert
6546 spaces in one of the two formats.
6547
6548 @item full-iso
6549 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
6550 format with nanosecond precision, e.g., @samp{2002-03-30
6551 23:45:56.477817180 -0700}.  This style is equivalent to
6552 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
6553
6554 This is useful because the time output includes all the information that
6555 is available from the operating system.  For example, this can help
6556 explain @command{make}'s behavior, since @acronym{GNU} @command{make}
6557 uses the full timestamp to determine whether a file is out of date.
6558
6559 @item long-iso
6560 List @acronym{ISO} 8601 date and time in minutes, e.g.,
6561 @samp{2002-03-30 23:45}.  These timestamps are shorter than
6562 @samp{full-iso} timestamps, and are usually good enough for everyday
6563 work.  This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
6564
6565 @item iso
6566 List @acronym{ISO} 8601 dates for non-recent timestamps (e.g.,
6567 @samp{2002-03-30@ }), and @acronym{ISO} 8601 month, day, hour, and
6568 minute for recent timestamps (e.g., @samp{03-30 23:45}).  These
6569 timestamps are uglier than @samp{long-iso} timestamps, but they carry
6570 nearly the same information in a smaller space and their brevity helps
6571 @command{ls} output fit within traditional 80-column output lines.
6572 The following two @command{ls} invocations are equivalent:
6573
6574 @example
6575 newline='
6576 '
6577 ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
6578 ls -l --time-style="iso"
6579 @end example
6580
6581 @item locale
6582 @vindex LC_TIME
6583 List timestamps in a locale-dependent form.  For example, a Finnish
6584 locale might list non-recent timestamps like @samp{maalis 30@ @ 2002}
6585 and recent timestamps like @samp{maalis 30 23:45}.  Locale-dependent
6586 timestamps typically consume more space than @samp{iso} timestamps and
6587 are harder for programs to parse because locale conventions vary so
6588 widely, but they are easier for many people to read.
6589
6590 The @env{LC_TIME} locale category specifies the timestamp format.  The
6591 default @acronym{POSIX} locale uses timestamps like @samp{Mar 30@
6592 @ 2002} and @samp{Mar 30 23:45}; in this locale, the following two
6593 @command{ls} invocations are equivalent:
6594
6595 @example
6596 newline='
6597 '
6598 ls -l --time-style="+%b %e  %Y$newline%b %e %H:%M"
6599 ls -l --time-style="locale"
6600 @end example
6601
6602 Other locales behave differently.  For example, in a German locale,
6603 @option{--time-style="locale"} might be equivalent to
6604 @option{--time-style="+%e. %b %Y $newline%e. %b %H:%M"}
6605 and might generate timestamps like @samp{30. M@"ar 2002@ } and
6606 @samp{30. M@"ar 23:45}.
6607
6608 @item posix-@var{style}
6609 @vindex LC_TIME
6610 List @acronym{POSIX}-locale timestamps if the @env{LC_TIME} locale
6611 category is @acronym{POSIX}, @var{style} timestamps otherwise.  For
6612 example, the @samp{posix-long-iso} style lists
6613 timestamps like @samp{Mar 30@ @ 2002} and @samp{Mar 30 23:45} when in
6614 the @acronym{POSIX} locale, and like @samp{2002-03-30 23:45} otherwise.
6615 @end table
6616 @end table
6617
6618 @vindex TIME_STYLE
6619 You can specify the default value of the @option{--time-style} option
6620 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
6621 the default style is @samp{locale}.  @acronym{GNU} Emacs 21.3 and
6622 later use the @option{--dired} option and therefore can parse any date
6623 format, but if you are using Emacs 21.1 or 21.2 and specify a
6624 non-@acronym{POSIX} locale you may need to set
6625 @samp{TIME_STYLE="posix-long-iso"}.
6626
6627 To avoid certain denial-of-service attacks, timestamps that would be
6628 longer than 1000 bytes may be treated as errors.
6629
6630
6631 @node Formatting the file names
6632 @subsection Formatting the file names
6633
6634 These options change how file names themselves are printed.
6635
6636 @table @samp
6637
6638 @item -b
6639 @itemx --escape
6640 @itemx --quoting-style=escape
6641 @opindex -b
6642 @opindex --escape
6643 @opindex --quoting-style
6644 @cindex backslash sequences for file names
6645 Quote nongraphic characters in file names using alphabetic and octal
6646 backslash sequences like those used in C.
6647
6648 @item -N
6649 @itemx --literal
6650 @itemx --quoting-style=literal
6651 @opindex -N
6652 @opindex --literal
6653 @opindex --quoting-style
6654 Do not quote file names.  However, with @command{ls} nongraphic
6655 characters are still printed as question marks if the output is a
6656 terminal and you do not specify the @option{--show-control-chars}
6657 option.
6658
6659 @item -q
6660 @itemx --hide-control-chars
6661 @opindex -q
6662 @opindex --hide-control-chars
6663 Print question marks instead of nongraphic characters in file names.
6664 This is the default if the output is a terminal and the program is
6665 @command{ls}.
6666
6667 @item -Q
6668 @itemx --quote-name
6669 @itemx --quoting-style=c
6670 @opindex -Q
6671 @opindex --quote-name
6672 @opindex --quoting-style
6673 Enclose file names in double quotes and quote nongraphic characters as
6674 in C.
6675
6676 @item --quoting-style=@var{word}
6677 @opindex --quoting-style
6678 @cindex quoting style
6679 Use style @var{word} to quote file names and other strings that may
6680 contain arbitrary characters.  The @var{word} should
6681 be one of the following:
6682
6683 @table @samp
6684 @item literal
6685 Output strings as-is; this is the same as the @option{-N} or
6686 @option{--literal} option.
6687 @item shell
6688 Quote strings for the shell if they contain shell metacharacters or would
6689 cause ambiguous output.
6690 The quoting is suitable for @acronym{POSIX}-compatible shells like
6691 @command{bash}, but it does not always work for incompatible shells
6692 like @command{csh}.
6693 @item shell-always
6694 Quote strings for the shell, even if they would normally not require quoting.
6695 @item c
6696 Quote strings as for C character string literals, including the
6697 surrounding double-quote characters; this is the same as the
6698 @option{-Q} or @option{--quote-name} option.
6699 @item escape
6700 Quote strings as for C character string literals, except omit the
6701 surrounding double-quote
6702 characters; this is the same as the @option{-b} or @option{--escape} option.
6703 @item clocale
6704 Quote strings as for C character string literals, except use
6705 surrounding quotation marks appropriate for the
6706 locale.
6707 @item locale
6708 @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
6709 Quote strings as for C character string literals, except use
6710 surrounding quotation marks appropriate for the locale, and quote
6711 @t{`like this'} instead of @t{"like
6712 this"} in the default C locale.  This looks nicer on many displays.
6713 @end table
6714
6715 You can specify the default value of the @option{--quoting-style} option
6716 with the environment variable @env{QUOTING_STYLE}.  If that environment
6717 variable is not set, the default value is @samp{literal}, but this
6718 default may change to @samp{shell} in a future version of this package.
6719
6720 @item --show-control-chars
6721 @opindex --show-control-chars
6722 Print nongraphic characters as-is in file names.
6723 This is the default unless the output is a terminal and the program is
6724 @command{ls}.
6725
6726 @end table
6727
6728
6729 @node dir invocation
6730 @section @command{dir}: Briefly list directory contents
6731
6732 @pindex dir
6733 @cindex directory listing, brief
6734
6735 @command{dir} is equivalent to @code{ls -C
6736 -b}; that is, by default files are listed in columns, sorted vertically,
6737 and special characters are represented by backslash escape sequences.
6738
6739 @xref{ls invocation, @command{ls}}.
6740
6741
6742 @node vdir invocation
6743 @section @command{vdir}: Verbosely list directory contents
6744
6745 @pindex vdir
6746 @cindex directory listing, verbose
6747
6748 @command{vdir} is equivalent to @code{ls -l
6749 -b}; that is, by default files are listed in long format and special
6750 characters are represented by backslash escape sequences.
6751
6752 @node dircolors invocation
6753 @section @command{dircolors}: Color setup for @command{ls}
6754
6755 @pindex dircolors
6756 @cindex color setup
6757 @cindex setup for color
6758
6759 @command{dircolors} outputs a sequence of shell commands to set up the
6760 terminal for color output from @command{ls} (and @command{dir}, etc.).
6761 Typical usage:
6762
6763 @example
6764 eval "`dircolors [@var{option}]@dots{} [@var{file}]`"
6765 @end example
6766
6767 If @var{file} is specified, @command{dircolors} reads it to determine which
6768 colors to use for which file types and extensions.  Otherwise, a
6769 precompiled database is used.  For details on the format of these files,
6770 run @samp{dircolors --print-database}.
6771
6772 @vindex LS_COLORS
6773 @vindex SHELL @r{environment variable, and color}
6774 The output is a shell command to set the @env{LS_COLORS} environment
6775 variable.  You can specify the shell syntax to use on the command line,
6776 or @command{dircolors} will guess it from the value of the @env{SHELL}
6777 environment variable.
6778
6779 The program accepts the following options.  Also see @ref{Common options}.
6780
6781 @table @samp
6782 @item -b
6783 @itemx --sh
6784 @itemx --bourne-shell
6785 @opindex -b
6786 @opindex --sh
6787 @opindex --bourne-shell
6788 @cindex Bourne shell syntax for color setup
6789 @cindex @command{sh} syntax for color setup
6790 Output Bourne shell commands.  This is the default if the @env{SHELL}
6791 environment variable is set and does not end with @samp{csh} or
6792 @samp{tcsh}.
6793
6794 @item -c
6795 @itemx --csh
6796 @itemx --c-shell
6797 @opindex -c
6798 @opindex --csh
6799 @opindex --c-shell
6800 @cindex C shell syntax for color setup
6801 @cindex @command{csh} syntax for color setup
6802 Output C shell commands.  This is the default if @code{SHELL} ends with
6803 @command{csh} or @command{tcsh}.
6804
6805 @item -p
6806 @itemx --print-database
6807 @opindex -p
6808 @opindex --print-database
6809 @cindex color database, printing
6810 @cindex database for color setup, printing
6811 @cindex printing color database
6812 Print the (compiled-in) default color configuration database.  This
6813 output is itself a valid configuration file, and is fairly descriptive
6814 of the possibilities.
6815
6816 @end table
6817
6818 @exitstatus
6819
6820
6821 @node Basic operations
6822 @chapter Basic operations
6823
6824 @cindex manipulating files
6825
6826 This chapter describes the commands for basic file manipulation:
6827 copying, moving (renaming), and deleting (removing).
6828
6829 @menu
6830 * cp invocation::               Copy files.
6831 * dd invocation::               Convert and copy a file.
6832 * install invocation::          Copy files and set attributes.
6833 * mv invocation::               Move (rename) files.
6834 * rm invocation::               Remove files or directories.
6835 * shred invocation::            Remove files more securely.
6836 @end menu
6837
6838
6839 @node cp invocation
6840 @section @command{cp}: Copy files and directories
6841
6842 @pindex cp
6843 @cindex copying files and directories
6844 @cindex files, copying
6845 @cindex directories, copying
6846
6847 @command{cp} copies files (or, optionally, directories).  The copy is
6848 completely independent of the original.  You can either copy one file to
6849 another, or copy arbitrarily many files to a destination directory.
6850 Synopses:
6851
6852 @example
6853 cp [@var{option}]@dots{} [-T] @var{source} @var{dest}
6854 cp [@var{option}]@dots{} @var{source}@dots{} @var{directory}
6855 cp [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
6856 @end example
6857
6858 @itemize @bullet
6859 @item
6860 If two file names are given, @command{cp} copies the first file to the
6861 second.
6862
6863 @item
6864 If the @option{--target-directory} (@option{-t}) option is given, or
6865 failing that if the last file is a directory and the
6866 @option{--no-target-directory} (@option{-T}) option is not given,
6867 @command{cp} copies each @var{source} file to the specified directory,
6868 using the @var{source}s' names.
6869 @end itemize
6870
6871 Generally, files are written just as they are read.  For exceptions,
6872 see the @option{--sparse} option below.
6873
6874 By default, @command{cp} does not copy directories.  However, the
6875 @option{-R}, @option{-a}, and @option{-r} options cause @command{cp} to
6876 copy recursively by descending into source directories and copying files
6877 to corresponding destination directories.
6878
6879 By default, @command{cp} follows symbolic links only when not copying
6880 recursively.  This default can be overridden with the
6881 @option{--archive} (@option{-a}), @option{-d}, @option{--dereference}
6882 (@option{-L}), @option{--no-dereference} (@option{-P}), and
6883 @option{-H} options.  If more than one of these options is specified,
6884 the last one silently overrides the others.
6885
6886 By default, @command{cp} copies the contents of special files only
6887 when not copying recursively.  This default can be overridden with the
6888 @option{--copy-contents} option.
6889
6890 @cindex self-backups
6891 @cindex backups, making only
6892 @command{cp} generally refuses to copy a file onto itself, with the
6893 following exception: if @option{--force --backup} is specified with
6894 @var{source} and @var{dest} identical, and referring to a regular file,
6895 @command{cp} will make a backup file, either regular or numbered, as
6896 specified in the usual ways (@pxref{Backup options}).  This is useful when
6897 you simply want to make a backup of an existing file before changing it.
6898
6899 The program accepts the following options.  Also see @ref{Common options}.
6900
6901 @table @samp
6902 @item -a
6903 @itemx --archive
6904 @opindex -a
6905 @opindex --archive
6906 Preserve as much as possible of the structure and attributes of the
6907 original files in the copy (but do not attempt to preserve internal
6908 directory structure; i.e., @samp{ls -U} may list the entries in a copied
6909 directory in a different order).
6910 Equivalent to @option{-dpPR}.
6911
6912 @item -b
6913 @itemx @w{@kbd{--backup}[=@var{method}]}
6914 @opindex -b
6915 @opindex --backup
6916 @vindex VERSION_CONTROL
6917 @cindex backups, making
6918 @xref{Backup options}.
6919 Make a backup of each file that would otherwise be overwritten or removed.
6920 As a special case, @command{cp} makes a backup of @var{source} when the force
6921 and backup options are given and @var{source} and @var{dest} are the same
6922 name for an existing, regular file.  One useful application of this
6923 combination of options is this tiny Bourne shell script:
6924
6925 @example
6926 #!/bin/sh
6927 # Usage: backup FILE...
6928 # Create a @sc{gnu}-style backup of each listed FILE.
6929 for i; do
6930   cp --backup --force -- "$i" "$i"
6931 done
6932 @end example
6933
6934 @item --copy-contents
6935 @cindex directories, copying recursively
6936 @cindex copying directories recursively
6937 @cindex recursively copying directories
6938 @cindex non-directories, copying as special files
6939 If copying recursively, copy the contents of any special files (e.g.,
6940 FIFOs and device files) as if they were regular files.  This means
6941 trying to read the data in each source file and writing it to the
6942 destination.  It is usually a mistake to use this option, as it
6943 normally has undesirable effects on special files like FIFOs and the
6944 ones typically found in the @file{/dev} directory.  In most cases,
6945 @code{cp -R --copy-contents} will hang indefinitely trying to read
6946 from FIFOs and special files like @file{/dev/console}, and it will
6947 fill up your destination disk if you use it to copy @file{/dev/zero}.
6948 This option has no effect unless copying recursively, and it does not
6949 affect the copying of symbolic links.
6950
6951 @item -d
6952 @opindex -d
6953 @cindex symbolic links, copying
6954 @cindex hard links, preserving
6955 Copy symbolic links as symbolic links rather than copying the files that
6956 they point to, and preserve hard links between source files in the copies.
6957 Equivalent to @option{--no-dereference --preserve=links}.
6958
6959 @item -f
6960 @itemx --force
6961 @opindex -f
6962 @opindex --force
6963 When copying without this option and an existing destination file cannot
6964 be opened for writing, the copy fails.  However, with @option{--force}),
6965 when a destination file cannot be opened, @command{cp} then unlinks it and
6966 tries to open it again.  Contrast this behavior with that enabled by
6967 @option{--link} and @option{--symbolic-link}, whereby the destination file
6968 is never opened but rather is unlinked unconditionally.  Also see the
6969 description of @option{--remove-destination}.
6970
6971 This option is independent of the @option{--interactive} or
6972 @option{-i} option: neither cancels the effect of the other.
6973
6974 @item -H
6975 @opindex -H
6976 If a command line argument specifies a symbolic link, then copy the
6977 file it points to rather than the symbolic link itself.  However,
6978 copy (preserving its nature) any symbolic link that is encountered
6979 via recursive traversal.
6980
6981 @item -i
6982 @itemx --interactive
6983 @opindex -i
6984 @opindex --interactive
6985 When copying a file other than a directory, prompt whether to
6986 overwrite an existing destination file.
6987
6988 @item -l
6989 @itemx --link
6990 @opindex -l
6991 @opindex --link
6992 Make hard links instead of copies of non-directories.
6993
6994 @item -L
6995 @itemx --dereference
6996 @opindex -L
6997 @opindex --dereference
6998 Always follow symbolic links.
6999
7000 @item -P
7001 @itemx --no-dereference
7002 @opindex -P
7003 @opindex --no-dereference
7004 @cindex symbolic links, copying
7005 Copy symbolic links as symbolic links rather than copying the files that
7006 they point to.
7007
7008 @item -p
7009 @itemx @w{@kbd{--preserve}[=@var{attribute_list}]}
7010 @opindex -p
7011 @opindex --preserve
7012 @cindex file information, preserving
7013 Preserve the specified attributes of the original files.
7014 If specified, the @var{attribute_list} must be a comma-separated list
7015 of one or more of the following strings:
7016
7017 @table @samp
7018 @itemx mode
7019 Preserve the file mode bits and access control lists.
7020 @itemx ownership
7021 Preserve the owner and group.  On most modern systems,
7022 only users with appropriate privileges may change the owner of a file,
7023 and ordinary users
7024 may preserve the group ownership of a file only if they happen to be
7025 a member of the desired group.
7026 @itemx timestamps
7027 Preserve the times of last access and last modification, when possible.
7028 In general, it is not possible to preserve these attributes
7029 when the affected file is a symbolic link.
7030 However, FreeBSD now provides the @code{lutimes} function, which makes
7031 it possibile even for symbolic links.  However, this implementation does
7032 not yet take advantage of that.
7033 @c FIXME: once we provide lutimes support, update the above.
7034 @itemx links
7035 Preserve in the destination files
7036 any links between corresponding source files.
7037 @c Give examples illustrating how hard links are preserved.
7038 @c Also, show how soft links map to hard links with -L and -H.
7039 @itemx all
7040 Preserve all file attributes.
7041 Equivalent to specifying all of the above.
7042 @end table
7043
7044 Using @option{--preserve} with no @var{attribute_list} is equivalent
7045 to @option{--preserve=mode,ownership,timestamps}.
7046
7047 In the absence of this option, each destination file is created with the
7048 mode bits of the corresponding source file, minus the bits set in the
7049 umask and minus the set-user-ID and set-group-ID bits.
7050 @xref{File permissions}.
7051
7052 @itemx @w{@kbd{--no-preserve}=@var{attribute_list}}
7053 @cindex file information, preserving
7054 Do not preserve the specified attributes.  The @var{attribute_list}
7055 has the same form as for @option{--preserve}.
7056
7057 @itemx --parents
7058 @opindex --parents
7059 @cindex parent directories and @command{cp}
7060 Form the name of each destination file by appending to the target
7061 directory a slash and the specified name of the source file.  The last
7062 argument given to @command{cp} must be the name of an existing directory.
7063 For example, the command:
7064
7065 @example
7066 cp --parents a/b/c existing_dir
7067 @end example
7068
7069 @noindent
7070 copies the file @file{a/b/c} to @file{existing_dir/a/b/c}, creating
7071 any missing intermediate directories.
7072
7073 @itemx @w{@kbd{--reply}=@var{how}}
7074 @opindex --reply
7075 @cindex interactivity
7076 @c FIXME: remove in 2008
7077 @strong{Deprecated: to be removed in 2008.}@*
7078 Using @option{--reply=yes} makes @command{cp} act as if @samp{yes} were
7079 given as a response to every prompt about a destination file.  That effectively
7080 cancels any preceding @option{--interactive} or @option{-i} option.
7081 Specify @option{--reply=no} to make @command{cp} act as if @samp{no} were
7082 given as a response to every prompt about a destination file.
7083 Specify @option{--reply=query} to make @command{cp} prompt the user
7084 about each existing destination file.
7085
7086 @item -R
7087 @itemx -r
7088 @itemx --recursive
7089 @opindex -R
7090 @opindex -r
7091 @opindex --recursive
7092 @cindex directories, copying recursively
7093 @cindex copying directories recursively
7094 @cindex recursively copying directories
7095 @cindex non-directories, copying as special files
7096 Copy directories recursively.  Symbolic links are not followed by
7097 default; see the @option{--archive} (@option{-a}), @option{-d},
7098 @option{--dereference} (@option{-L}), @option{--no-dereference}
7099 (@option{-P}), and @option{-H} options.  Special files are copied by
7100 creating a destination file of the same type as the source; see the
7101 @option{--copy-contents} option.  It is not portable to use
7102 @option{-r} to copy symbolic links or special files.  On some
7103 non-@sc{gnu} systems, @option{-r} implies the equivalent of
7104 @option{-L} and @option{--copy-contents} for historical reasons.
7105 Also, it is not portable to use @option{-R} to copy symbolic links
7106 unless you also specify @option{-P}, as @acronym{POSIX} allows
7107 implementations that dereference symbolic links by default.
7108
7109 @item --remove-destination
7110 @opindex --remove-destination
7111 Remove each existing destination file before attempting to open it
7112 (contrast with @option{-f} above).
7113
7114 @item --sparse=@var{when}
7115 @opindex --sparse=@var{when}
7116 @cindex sparse files, copying
7117 @cindex holes, copying files with
7118 @findex read @r{system call, and holes}
7119 A @dfn{sparse file} contains @dfn{holes}---a sequence of zero bytes that
7120 does not occupy any physical disk blocks; the @samp{read} system call
7121 reads these as zeros.  This can both save considerable disk space and
7122 increase speed, since many binary files contain lots of consecutive zero
7123 bytes.  By default, @command{cp} detects holes in input source files via a crude
7124 heuristic and makes the corresponding output file sparse as well.
7125 Only regular files may be sparse.
7126
7127 The @var{when} value can be one of the following:
7128
7129 @table @samp
7130 @item auto
7131 The default behavior: if the input file is sparse, attempt to make
7132 the output file sparse, too.  However, if an output file exists but
7133 refers to a non-regular file, then do not attempt to make it sparse.
7134
7135 @item always
7136 For each sufficiently long sequence of zero bytes in the input file,
7137 attempt to create a corresponding hole in the output file, even if the
7138 input file does not appear to be sparse.
7139 This is useful when the input file resides on a file system
7140 that does not support sparse files
7141 (for example,  @samp{efs} file systems in SGI IRIX 5.3 and earlier),
7142 but the output file is on a type of file system that does support them.
7143 Holes may be created only in regular files, so if the destination file
7144 is of some other type, @command{cp} does not even try to make it sparse.
7145
7146 @item never
7147 Never make the output file sparse.
7148 This is useful in creating a file for use with the @command{mkswap} command,
7149 since such a file must not have any holes.
7150 @end table
7151
7152 @optStripTrailingSlashes
7153
7154 @item -s
7155 @itemx --symbolic-link
7156 @opindex -s
7157 @opindex --symbolic-link
7158 @cindex symbolic links, copying with
7159 Make symbolic links instead of copies of non-directories.  All source
7160 file names must be absolute (starting with @samp{/}) unless the
7161 destination files are in the current directory.  This option merely
7162 results in an error message on systems that do not support symbolic links.
7163
7164 @optBackupSuffix
7165
7166 @optTargetDirectory
7167
7168 @optNoTargetDirectory
7169
7170 @item -u
7171 @itemx --update
7172 @opindex -u
7173 @opindex --update
7174 @cindex newer files, copying only
7175 Do not copy a non-directory that has an existing destination with the
7176 same or newer modification time.  If time stamps are being preserved,
7177 the comparison is to the source time stamp truncated to the
7178 resolutions of the destination file system and of the system calls
7179 used to update time stamps; this avoids duplicate work if several
7180 @samp{cp -pu} commands are executed with the same source and
7181 destination.
7182
7183 @item -v
7184 @itemx --verbose
7185 @opindex -v
7186 @opindex --verbose
7187 Print the name of each file before copying it.
7188
7189 @item -x
7190 @itemx --one-file-system
7191 @opindex -x
7192 @opindex --one-file-system
7193 @cindex file systems, omitting copying to different
7194 Skip subdirectories that are on different file systems from the one that
7195 the copy started on.
7196 However, mount point directories @emph{are} copied.
7197
7198 @end table
7199
7200 @exitstatus
7201
7202
7203 @node dd invocation
7204 @section @command{dd}: Convert and copy a file
7205
7206 @pindex dd
7207 @cindex converting while copying a file
7208
7209 @command{dd} copies a file (from standard input to standard output, by
7210 default) with a changeable I/O block size, while optionally performing
7211 conversions on it.  Synopses:
7212
7213 @example
7214 dd [@var{operand}]@dots{}
7215 dd @var{option}
7216 @end example
7217
7218 The only options are @option{--help} and @option{--version}.
7219 @xref{Common options}.  @command{dd} accepts the following operands.
7220
7221 @table @samp
7222
7223 @item if=@var{file}
7224 @opindex if
7225 Read from @var{file} instead of standard input.
7226
7227 @item of=@var{file}
7228 @opindex of
7229 Write to @var{file} instead of standard output.  Unless
7230 @samp{conv=notrunc} is given, @command{dd} truncates @var{file} to zero
7231 bytes (or the size specified with @samp{seek=}).
7232
7233 @item ibs=@var{bytes}
7234 @opindex ibs
7235 @cindex block size of input
7236 @cindex input block size
7237 Set the input block size to @var{bytes}.
7238 This makes @command{dd} read @var{bytes} per block.
7239
7240 @item obs=@var{bytes}
7241 @opindex obs
7242 @cindex block size of output
7243 @cindex output block size
7244 Set the output block size to @var{bytes}.
7245 This makes @command{dd} write @var{bytes} per block.
7246
7247 @item bs=@var{bytes}
7248 @opindex bs
7249 @cindex block size
7250 Set both input and output block sizes to @var{bytes}.
7251 This makes @command{dd} read and write @var{bytes} per block,
7252 overriding any @samp{ibs} and @samp{obs} settings.
7253
7254 @item cbs=@var{bytes}
7255 @opindex cbs
7256 @cindex block size of conversion
7257 @cindex conversion block size
7258 @cindex fixed-length records, converting to variable-length
7259 @cindex variable-length records, converting to fixed-length
7260 Set the conversion block size to @var{bytes}.
7261 When converting variable-length records to fixed-length ones
7262 (@option{conv=block}) or the reverse (@option{conv=unblock}),
7263 use @var{bytes} as the fixed record length.
7264
7265 @item skip=@var{blocks}
7266 @opindex skip
7267 Skip @var{blocks} @samp{ibs}-byte blocks in the input file before copying.
7268
7269 @item seek=@var{blocks}
7270 @opindex seek
7271 Skip @var{blocks} @samp{obs}-byte blocks in the output file before copying.
7272
7273 @item count=@var{blocks}
7274 @opindex count
7275 Copy @var{blocks} @samp{ibs}-byte blocks from the input file, instead
7276 of everything until the end of the file.
7277
7278 @item conv=@var{conversion}[,@var{conversion}]@dots{}
7279 @opindex conv
7280 Convert the file as specified by the @var{conversion} argument(s).
7281 (No spaces around any comma(s).)
7282
7283 Conversions:
7284
7285 @table @samp
7286
7287 @item ascii
7288 @opindex ascii@r{, converting to}
7289 Convert @acronym{EBCDIC} to @acronym{ASCII},
7290 using the conversion table specified by @acronym{POSIX}.
7291 This provides a 1:1 translation for all 256 bytes.
7292
7293 @item ebcdic
7294 @opindex ebcdic@r{, converting to}
7295 Convert @acronym{ASCII} to @acronym{EBCDIC}.
7296 This is the inverse of the @samp{ascii} conversion.
7297
7298 @item ibm
7299 @opindex alternate ebcdic@r{, converting to}
7300 Convert @acronym{ASCII} to alternate @acronym{EBCDIC},
7301 using the alternate conversion table specified by @acronym{POSIX}.
7302 This is not a 1:1 translation, but reflects common historical practice
7303 for @samp{~}, @samp{[}, and @samp{]}.
7304
7305 The @samp{ascii}, @samp{ebcdic}, and @samp{ibm} conversions are
7306 mutually exclusive.
7307
7308 @item block
7309 @opindex block @r{(space-padding)}
7310 For each line in the input, output @samp{cbs} bytes, replacing the
7311 input newline with a space and padding with spaces as necessary.
7312
7313 @item unblock
7314 @opindex unblock
7315 Replace trailing spaces in each @samp{cbs}-sized input block with a
7316 newline.
7317
7318 The @samp{block} and @samp{unblock} conversions are mutually exclusive.
7319
7320 @item lcase
7321 @opindex lcase@r{, converting to}
7322 Change uppercase letters to lowercase.
7323
7324 @item ucase
7325 @opindex ucase@r{, converting to}
7326 Change lowercase letters to uppercase.
7327
7328 The @samp{lcase} and @samp{ucase} conversions are mutually exclusive.
7329
7330 @item swab
7331 @opindex swab @r{(byte-swapping)}
7332 @cindex byte-swapping
7333 Swap every pair of input bytes.  @sc{gnu} @command{dd}, unlike others, works
7334 when an odd number of bytes are read---the last byte is simply copied
7335 (since there is nothing to swap it with).
7336
7337 @item noerror
7338 @opindex noerror
7339 @cindex read errors, ignoring
7340 Continue after read errors.
7341
7342 @item nocreat
7343 @opindex nocreat
7344 @cindex creating output file, avoiding
7345 Do not create the output file; the output file must already exist.
7346
7347 @item excl
7348 @opindex excl
7349 @cindex creating output file, requiring
7350 Fail if the output file already exists; @command{dd} must create the
7351 output file itself.
7352
7353 The @samp{excl} and @samp{nocreat} conversions are mutually exclusive.
7354
7355 @item notrunc
7356 @opindex notrunc
7357 @cindex truncating output file, avoiding
7358 Do not truncate the output file.
7359
7360 @item sync
7361 @opindex sync @r{(padding with nulls)}
7362 Pad every input block to size of @samp{ibs} with trailing zero bytes.
7363 When used with @samp{block} or @samp{unblock}, pad with spaces instead of
7364 zero bytes.
7365
7366 @item fdatasync
7367 @opindex fdatasync
7368 @cindex synchronized data writes, before finishing
7369 Synchronize output data just before finishing.  This forces a physical
7370 write of output data.
7371
7372 @item fsync
7373 @opindex fsync
7374 @cindex synchronized data and metadata writes, before finishing
7375 Synchronize output data and metadata just before finishing.  This
7376 forces a physical write of output data and metadata.
7377
7378 @end table
7379
7380 @item iflag=@var{flag}[,@var{flag}]@dots{}
7381 @opindex iflag
7382 Access the input file using the flags specified by the @var{flag}
7383 argument(s).  (No spaces around any comma(s).)
7384
7385 @item oflag=@var{flag}[,@var{flag}]@dots{}
7386 @opindex oflag
7387 Access the output file using the flags specified by the @var{flag}
7388 argument(s).  (No spaces around any comma(s).)
7389
7390 Here are the flags.  Not every flag is supported on every operating
7391 system.
7392
7393 @table @samp
7394
7395 @item append
7396 @opindex append
7397 @cindex appending to the output file
7398 Write in append mode, so that even if some other process is writing to
7399 this file, every @command{dd} write will append to the current
7400 contents of the file.  This flag makes sense only for output.
7401 If you combine this flag with the @samp{of=@var{file}} operand,
7402 you should also specify @samp{conv=notrunc} unless you want the
7403 output file to be truncated before being appended to.
7404
7405 @item direct
7406 @opindex direct
7407 @cindex direct I/O
7408 Use direct I/O for data, avoiding the buffer cache.
7409
7410 @item directory
7411 @opindex directory
7412 @cindex directory I/O
7413
7414 Fail unless the file is a directory.  Most operating systems do not
7415 allow I/O to a directory, so this flag has limited utility.
7416
7417 @item dsync
7418 @opindex dsync
7419 @cindex synchronized data reads
7420 Use synchronized I/O for data.  For the output file, this forces a
7421 physical write of output data on each write.  For the input file,
7422 this flag can matter when reading from a remote file that has been
7423 written to synchronously by some other process.  Metadata (e.g.,
7424 last-access and last-modified time) is not necessarily synchronized.
7425
7426 @item sync
7427 @opindex sync
7428 @cindex synchronized data and metadata I/O
7429 Use synchronized I/O for both data and metadata.
7430
7431 @item nonblock
7432 @opindex nonblock
7433 @cindex nonblocking I/O
7434 Use non-blocking I/O.
7435
7436 @item noatime
7437 @opindex noatime
7438 @cindex access time
7439 Do not update the file's access time.
7440 Some older file systems silently ignore this flag, so it is a good
7441 idea to test it on your files before relying on it.
7442
7443 @item noctty
7444 @opindex noctty
7445 @cindex controlling terminal
7446 Do not assign the file to be a controlling terminal for @command{dd}.
7447 This has no effect when the file is not a terminal.
7448 On many hosts (e.g., @acronym{GNU}/Linux hosts), this option has no effect
7449 at all.
7450
7451 @item nofollow
7452 @opindex nofollow
7453 @cindex symbolic links, following
7454 Do not follow symbolic links.
7455
7456 @item nolinks
7457 @opindex nolinks
7458 @cindex hard links
7459 Fail if the file has multiple hard links.
7460
7461 @item binary
7462 @opindex binary
7463 @cindex binary I/O
7464 Use binary I/O.  This option has an effect only on nonstandard
7465 platforms that distinguish binary from text I/O.
7466
7467 @item text
7468 @opindex text
7469 @cindex text I/O
7470 Use text I/O.  Like @samp{binary}, this option has no effect on
7471 standard platforms.
7472
7473 @end table
7474
7475 These flags are not supported on all systems, and @samp{dd} rejects
7476 attempts to use them when they are not supported.  When reading from
7477 standard input or writing to standard output, the @samp{nofollow} and
7478 @samp{noctty} flags should not be specified, and the other flags
7479 (e.g., @samp{nonblock}) can affect how other processes behave with the
7480 affected file descriptors, even after @command{dd} exits.
7481
7482 @end table
7483
7484 @cindex multipliers after numbers
7485 The numeric-valued strings above (@var{bytes} and @var{blocks}) can be
7486 followed by a multiplier: @samp{b}=512, @samp{c}=1,
7487 @samp{w}=2, @samp{x@var{m}}=@var{m}, or any of the
7488 standard block size suffixes like @samp{k}=1024 (@pxref{Block size}).
7489
7490 Use different @command{dd} invocations to use different block sizes for
7491 skipping and I/O@.  For example, the following shell commands copy data
7492 in 512 KiB blocks between a disk and a tape, but do not save or restore a
7493 4 KiB label at the start of the disk:
7494
7495 @example
7496 disk=/dev/rdsk/c0t1d0s2
7497 tape=/dev/rmt/0
7498
7499 # Copy all but the label from disk to tape.
7500 (dd bs=4k skip=1 count=0 && dd bs=512k) <$disk >$tape
7501
7502 # Copy from tape back to disk, but leave the disk label alone.
7503 (dd bs=4k seek=1 count=0 && dd bs=512k) <$tape >$disk
7504 @end example
7505
7506 Sending an @samp{INFO} signal to a running @command{dd}
7507 process makes it print I/O statistics to standard error
7508 and then resume copying.  In the example below,
7509 @command{dd} is run in the background to copy 10 million blocks.
7510 The @command{kill} command makes it output intermediate I/O statistics,
7511 and when @command{dd} completes, it outputs the final statistics.
7512
7513 @example
7514 $ dd if=/dev/zero of=/dev/null count=10MB & pid=$!
7515 $ kill -s INFO $pid; wait $pid
7516 3385223+0 records in
7517 3385223+0 records out
7518 1733234176 bytes (1.7 GB) copied, 6.42173 seconds, 270 MB/s
7519 10000000+0 records in
7520 10000000+0 records out
7521 5120000000 bytes (5.1 GB) copied, 18.913 seconds, 271 MB/s
7522 @end example
7523
7524 @vindex POSIXLY_CORRECT
7525 On systems lacking the @samp{INFO} signal @command{dd} responds to the
7526 @samp{USR1} signal instead, unless the @env{POSIXLY_CORRECT}
7527 environment variable is set.
7528
7529 @exitstatus
7530
7531
7532 @node install invocation
7533 @section @command{install}: Copy files and set attributes
7534
7535 @pindex install
7536 @cindex copying files and setting attributes
7537
7538 @command{install} copies files while setting their file mode bits and, if
7539 possible, their owner and group.  Synopses:
7540
7541 @example
7542 install [@var{option}]@dots{} [-T] @var{source} @var{dest}
7543 install [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7544 install [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7545 install [@var{option}]@dots{} -d @var{directory}@dots{}
7546 @end example
7547
7548 @itemize @bullet
7549 @item
7550 If two file names are given, @command{install} copies the first file to the
7551 second.
7552
7553 @item
7554 If the @option{--target-directory} (@option{-t}) option is given, or
7555 failing that if the last file is a directory and the
7556 @option{--no-target-directory} (@option{-T}) option is not given,
7557 @command{install} copies each @var{source} file to the specified
7558 directory, using the @var{source}s' names.
7559
7560 @item
7561 If the @option{--directory} (@option{-d}) option is given,
7562 @command{install} creates each @var{directory} and any missing parent
7563 directories.  Parent directories are created with mode
7564 @samp{u=rwx,go=rx} (755), regardless of the @option{-m} option or the
7565 current umask.  @xref{Directory Setuid and Setgid}, for how the
7566 set-user-ID and set-group-ID bits of parent directories are inherited.
7567 @end itemize
7568
7569 @cindex Makefiles, installing programs in
7570 @command{install} is similar to @command{cp}, but allows you to control the
7571 attributes of destination files.  It is typically used in Makefiles to
7572 copy programs into their destination directories.  It refuses to copy
7573 files onto themselves.
7574
7575 The program accepts the following options.  Also see @ref{Common options}.
7576
7577 @table @samp
7578
7579 @optBackup
7580
7581 @item -c
7582 @opindex -c
7583 Ignored; for compatibility with old Unix versions of @command{install}.
7584
7585 @item -d
7586 @itemx --directory
7587 @opindex -d
7588 @opindex --directory
7589 @cindex directories, creating with given attributes
7590 @cindex parent directories, creating missing
7591 @cindex leading directories, creating missing
7592 Create any missing parent directories, giving them the default
7593 attributes.  Then create each given directory, setting their owner,
7594 group and mode as given on the command line or to the defaults.
7595
7596 @item -g @var{group}
7597 @itemx --group=@var{group}
7598 @opindex -g
7599 @opindex --group
7600 @cindex group ownership of installed files, setting
7601 Set the group ownership of installed files or directories to
7602 @var{group}.  The default is the process's current group.  @var{group}
7603 may be either a group name or a numeric group ID.
7604
7605 @item -m @var{mode}
7606 @itemx --mode=@var{mode}
7607 @opindex -m
7608 @opindex --mode
7609 @cindex permissions of installed files, setting
7610 Set the file mode bits for the installed file or directory to @var{mode},
7611 which can be either an octal number, or a symbolic mode as in
7612 @command{chmod}, with @samp{a=} (no access allowed to anyone) as the
7613 point of departure (@pxref{File permissions}).
7614 The default mode is @samp{u=rwx,go=rx,a-s}---read, write, and
7615 execute for the owner, read and execute for group and other, and with
7616 set-user-ID and set-group-ID disabled.
7617 This default is not quite the same as @samp{755}, since it disables
7618 instead of preserving set-user-ID and set-group-ID on directories.
7619 @xref{Directory Setuid and Setgid}.
7620
7621 @item -o @var{owner}
7622 @itemx --owner=@var{owner}
7623 @opindex -o
7624 @opindex --owner
7625 @cindex ownership of installed files, setting
7626 @cindex appropriate privileges
7627 @vindex root @r{as default owner}
7628 If @command{install} has appropriate privileges (is run as root), set the
7629 ownership of installed files or directories to @var{owner}.  The default
7630 is @code{root}.  @var{owner} may be either a user name or a numeric user
7631 ID.
7632
7633 @item -p
7634 @itemx --preserve-timestamps
7635 @opindex -p
7636 @opindex --preserve-timestamps
7637 @cindex timestamps of installed files, preserving
7638 Set the time of last access and the time of last modification of each
7639 installed file to match those of each corresponding original file.
7640 When a file is installed without this option, its last access and
7641 last modification times are both set to the time of installation.
7642 This option is useful if you want to use the last modification times
7643 of installed files to keep track of when they were last built as opposed
7644 to when they were last installed.
7645
7646 @item -s
7647 @itemx --strip
7648 @opindex -s
7649 @opindex --strip
7650 @cindex symbol table information, stripping
7651 @cindex stripping symbol table information
7652 Strip the symbol tables from installed binary executables.
7653
7654 @optBackupSuffix
7655
7656 @optTargetDirectory
7657
7658 @optNoTargetDirectory
7659
7660 @item -v
7661 @itemx --verbose
7662 @opindex -v
7663 @opindex --verbose
7664 Print the name of each file before copying it.
7665
7666 @end table
7667
7668 @exitstatus
7669
7670
7671 @node mv invocation
7672 @section @command{mv}: Move (rename) files
7673
7674 @pindex mv
7675
7676 @command{mv} moves or renames files (or directories).  Synopses:
7677
7678 @example
7679 mv [@var{option}]@dots{} [-T] @var{source} @var{dest}
7680 mv [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7681 mv [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7682 @end example
7683
7684 @itemize @bullet
7685 @item
7686 If two file names are given, @command{mv} moves the first file to the
7687 second.
7688
7689 @item
7690 If the @option{--target-directory} (@option{-t}) option is given, or
7691 failing that if the last file is a directory and the
7692 @option{--no-target-directory} (@option{-T}) option is not given,
7693 @command{mv} moves each @var{source} file to the specified
7694 directory, using the @var{source}s' names.
7695 @end itemize
7696
7697 @command{mv} can move any type of file from one file system to another.
7698 Prior to version @code{4.0} of the fileutils,
7699 @command{mv} could move only regular files between file systems.
7700 For example, now @command{mv} can move an entire directory hierarchy
7701 including special device files from one partition to another.  It first
7702 uses some of the same code that's used by @code{cp -a} to copy the
7703 requested directories and files, then (assuming the copy succeeded)
7704 it removes the originals.  If the copy fails, then the part that was
7705 copied to the destination partition is removed.  If you were to copy
7706 three directories from one partition to another and the copy of the first
7707 directory succeeded, but the second didn't, the first would be left on
7708 the destination partition and the second and third would be left on the
7709 original partition.
7710
7711 @cindex prompting, and @command{mv}
7712 If a destination file exists but is normally unwritable, standard input
7713 is a terminal, and the @option{-f} or @option{--force} option is not given,
7714 @command{mv} prompts the user for whether to replace the file.  (You might
7715 own the file, or have write permission on its directory.)  If the
7716 response is not affirmative, the file is skipped.
7717
7718 @emph{Warning}: If you try to move a symlink that points to a directory,
7719 and you specify the symlink with a trailing slash, then @command{mv}
7720 doesn't move the symlink but instead moves the directory referenced
7721 by the symlink.  @xref{Trailing slashes}.
7722
7723 The program accepts the following options.  Also see @ref{Common options}.
7724
7725 @table @samp
7726
7727 @optBackup
7728
7729 @item -f
7730 @itemx --force
7731 @opindex -f
7732 @opindex --force
7733 @cindex prompts, omitting
7734 Do not prompt the user before removing a destination file.
7735
7736 @item -i
7737 @itemx --interactive
7738 @opindex -i
7739 @opindex --interactive
7740 @cindex prompts, forcing
7741 Prompt whether to overwrite each existing destination file, regardless
7742 of its permissions.
7743 If the response is not affirmative, the file is skipped.
7744
7745 @itemx @w{@kbd{--reply}=@var{how}}
7746 @opindex --reply
7747 @cindex interactivity
7748 @c FIXME: remove in 2008
7749 @strong{Deprecated: to be removed in 2008.}@*
7750 Specifying @option{--reply=yes} is equivalent to using @option{--force}.
7751 Specify @option{--reply=no} to make @command{mv} act as if @samp{no} were
7752 given as a response to every prompt about a destination file.
7753 Specify @option{--reply=query} to make @command{mv} prompt the user
7754 about each existing destination file.
7755 Note that @option{--reply=no} has an effect only when @command{mv} would prompt
7756 without @option{-i} or equivalent, i.e., when a destination file exists and is
7757 not writable, standard input is a terminal, and no @option{-f} (or equivalent)
7758 option is specified.
7759
7760 @item -u
7761 @itemx --update
7762 @opindex -u
7763 @opindex --update
7764 @cindex newer files, moving only
7765 Do not move a non-directory that has an existing destination with the
7766 same or newer modification time.
7767 If the move is across file system boundaries, the comparison is to the
7768 source time stamp truncated to the resolutions of the destination file
7769 system and of the system calls used to update time stamps; this avoids
7770 duplicate work if several @samp{mv -u} commands are executed with the
7771 same source and destination.
7772
7773 @item -v
7774 @itemx --verbose
7775 @opindex -v
7776 @opindex --verbose
7777 Print the name of each file before moving it.
7778
7779 @optStripTrailingSlashes
7780
7781 @optBackupSuffix
7782
7783 @optTargetDirectory
7784
7785 @optNoTargetDirectory
7786
7787 @end table
7788
7789 @exitstatus
7790
7791
7792 @node rm invocation
7793 @section @command{rm}: Remove files or directories
7794
7795 @pindex rm
7796 @cindex removing files or directories
7797
7798 @command{rm} removes each given @var{file}.  By default, it does not remove
7799 directories.  Synopsis:
7800
7801 @example
7802 rm [@var{option}]@dots{} [@var{file}]@dots{}
7803 @end example
7804
7805 @cindex prompting, and @command{rm}
7806 If the @option{-I} or @option{--interactive=once} option is given,
7807 and there are more than three files or the @option{-r}, @option{-R},
7808 or @option{--recursive} are given, then @command{rm} prompts the user
7809 for whether to proceed with the entire operation.  If the response is
7810 not affirmative, the entire command is aborted.
7811
7812 Otherwise, if a file is unwritable, standard input is a terminal, and
7813 the @option{-f} or @option{--force} option is not given, or the
7814 @option{-i} or @option{--interactive=always} option @emph{is} given,
7815 @command{rm} prompts the user for whether to remove the file.
7816 If the response is not affirmative, the file is skipped.
7817
7818 Any attempt to remove a file whose last file name component is
7819 @file{.} or @file{..} is rejected without any prompting.
7820
7821 @emph{Warning}: If you use @command{rm} to remove a file, it is usually
7822 possible to recover the contents of that file.  If you want more assurance
7823 that the contents are truly unrecoverable, consider using @command{shred}.
7824
7825 The program accepts the following options.  Also see @ref{Common options}.
7826
7827 @table @samp
7828
7829 @item -f
7830 @itemx --force
7831 @opindex -f
7832 @opindex --force
7833 Ignore nonexistent files and never prompt the user.
7834 Ignore any previous @option{--interactive} (@option{-i}) option.
7835
7836 @item -i
7837 @opindex -i
7838 Prompt whether to remove each file.
7839 If the response is not affirmative, the file is skipped.
7840 Ignore any previous @option{--force} (@option{-f}) option.
7841 Equivalent to @option{--interactive=always}.
7842
7843 @item -I
7844 @opindex -I
7845 Prompt once whether to proceed with the command, if more than three
7846 files are named or if a recursive removal is requested.  Ignore any
7847 previous @option{--force} (@option{-f}) option.  Equivalent to
7848 @option{--interactive=once}.
7849
7850 @itemx --interactive [=@var{when}]
7851 @opindex --interactive
7852 Specify when to issue an interactive prompt.  @var{when} may be
7853 omitted, or one of:
7854 @itemize @bullet
7855 @item never
7856 @vindex never @r{interactive option}
7857 - Do not prompt at all.
7858 @item once
7859 @vindex once @r{interactive option}
7860 - Prompt once if more than three files are named or if a recursive
7861 removal is requested.  Equivalent to @option{-I}.
7862 @item always
7863 @vindex always @r{interactive option}
7864 - Prompt for every file being removed.  Equivalent to @option{-i}.
7865 @end itemize
7866 Specifying @option{--interactive} and no @var{when} is equivalent to
7867 @option{--interactive=always}.
7868
7869 @itemx --one-file-system
7870 @opindex --one-file-system
7871 @cindex one file system, restricting @command{rm} to
7872 When removing a hierarchy recursively, skip any directory that is on a
7873 file system different from that of the corresponding command line argument.
7874
7875 This option is useful when removing a build ``chroot'' hierarchy,
7876 which normally contains no valuable data.  However, it is not uncommon
7877 to bind-mount @file{/home} into such a hierarchy, to make it easier to
7878 use one's start-up file.  The catch is that it's easy to forget to
7879 unmount @file{/home}.  Then, when you use @command{rm -rf} to remove
7880 your normally throw-away chroot, that command will remove everything
7881 under @file{/home}, too.
7882 Use the @option{--one-file-system} option, and it will
7883 warn about and skip directories on other file systems.
7884 Of course, this will not save your @file{/home} if it and your
7885 chroot happen to be on the same file system.
7886
7887 @itemx --preserve-root
7888 @opindex --preserve-root
7889 @cindex root directory, disallow recursive destruction
7890 Fail upon any attempt to remove the root directory, @file{/},
7891 when used with the @option{--recursive} option.
7892 This is the default behavior.
7893 @xref{Treating / specially}.
7894
7895 @itemx --no-preserve-root
7896 @opindex --no-preserve-root
7897 @cindex root directory, allow recursive destruction
7898 Do not treat @file{/} specially when removing recursively.
7899 This option is not recommended unless you really want to
7900 remove all the files on your computer.
7901 @xref{Treating / specially}.
7902
7903 @item -r
7904 @itemx -R
7905 @itemx --recursive
7906 @opindex -r
7907 @opindex -R
7908 @opindex --recursive
7909 @cindex directories, removing (recursively)
7910 Remove the listed directories and their contents recursively.
7911
7912 @item -v
7913 @itemx --verbose
7914 @opindex -v
7915 @opindex --verbose
7916 Print the name of each file before removing it.
7917
7918 @end table
7919
7920 @cindex files beginning with @samp{-}, removing
7921 @cindex @samp{-}, removing files beginning with
7922 One common question is how to remove files whose names begin with a
7923 @samp{-}.  @sc{gnu} @command{rm}, like every program that uses the @code{getopt}
7924 function to parse its arguments, lets you use the @samp{--} option to
7925 indicate that all following arguments are non-options.  To remove a file
7926 called @file{-f} in the current directory, you could type either:
7927
7928 @example
7929 rm -- -f
7930 @end example
7931
7932 @noindent
7933 or:
7934
7935 @example
7936 rm ./-f
7937 @end example
7938
7939 @opindex - @r{and Unix @command{rm}}
7940 The Unix @command{rm} program's use of a single @samp{-} for this purpose
7941 predates the development of the getopt standard syntax.
7942
7943 @exitstatus
7944
7945
7946 @node shred invocation
7947 @section @command{shred}: Remove files more securely
7948
7949 @pindex shred
7950 @cindex data, erasing
7951 @cindex erasing data
7952
7953 @command{shred} overwrites devices or files, to help prevent even
7954 very expensive hardware from recovering the data.
7955
7956 Ordinarily when you remove a file (@pxref{rm invocation}), the data is
7957 not actually destroyed.  Only the index listing where the file is
7958 stored is destroyed, and the storage is made available for reuse.
7959 There are undelete utilities that will attempt to reconstruct the index
7960 and can bring the file back if the parts were not reused.
7961
7962 On a busy system with a nearly-full drive, space can get reused in a few
7963 seconds.  But there is no way to know for sure.  If you have sensitive
7964 data, you may want to be sure that recovery is not possible by actually
7965 overwriting the file with non-sensitive data.
7966
7967 However, even after doing that, it is possible to take the disk back
7968 to a laboratory and use a lot of sensitive (and expensive) equipment
7969 to look for the faint ``echoes'' of the original data underneath the
7970 overwritten data.  If the data has only been overwritten once, it's not
7971 even that hard.
7972
7973 The best way to remove something irretrievably is to destroy the media
7974 it's on with acid, melt it down, or the like.  For cheap removable media
7975 like floppy disks, this is the preferred method.  However, hard drives
7976 are expensive and hard to melt, so the @command{shred} utility tries
7977 to achieve a similar effect non-destructively.
7978
7979 This uses many overwrite passes, with the data patterns chosen to
7980 maximize the damage they do to the old data.  While this will work on
7981 floppies, the patterns are designed for best effect on hard drives.
7982 For more details, see the source code and Peter Gutmann's paper
7983 @uref{http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html,
7984 @cite{Secure Deletion of Data from Magnetic and Solid-State Memory}},
7985 from the proceedings of the Sixth @acronym{USENIX} Security Symposium (San Jose,
7986 California, July 22--25, 1996).
7987
7988 @strong{Please note} that @command{shred} relies on a very important assumption:
7989 that the file system overwrites data in place.  This is the traditional
7990 way to do things, but many modern file system designs do not satisfy this
7991 assumption.  Exceptions include:
7992
7993 @itemize @bullet
7994
7995 @item
7996 Log-structured or journaled file systems, such as those supplied with
7997 AIX and Solaris, and JFS, ReiserFS, XFS, Ext3 (in @code{data=journal} mode),
7998 BFS, NTFS, etc.@: when they are configured to journal @emph{data}.
7999
8000 @item
8001 File systems that write redundant data and carry on even if some writes
8002 fail, such as RAID-based file systems.
8003
8004 @item
8005 File systems that make snapshots, such as Network Appliance's NFS server.
8006
8007 @item
8008 File systems that cache in temporary locations, such as NFS version 3
8009 clients.
8010
8011 @item
8012 Compressed file systems.
8013 @end itemize
8014
8015 In the particular case of ext3 file systems, the above disclaimer applies (and
8016 @command{shred} is thus of limited effectiveness) only in @code{data=journal}
8017 mode, which journals file data in addition to just metadata. In both
8018 the @code{data=ordered} (default) and @code{data=writeback} modes,
8019 @command{shred} works as usual.  Ext3 journaling modes can be changed
8020 by adding the @code{data=something} option to the mount options for a
8021 particular file system in the @file{/etc/fstab} file, as documented in
8022 the mount man page (man mount).
8023
8024 If you are not sure how your file system operates, then you should assume
8025 that it does not overwrite data in place, which means that shred cannot
8026 reliably operate on regular files in your file system.
8027
8028 Generally speaking, it is more reliable to shred a device than a file,
8029 since this bypasses the problem of file system design mentioned above.
8030 However, even shredding devices is not always completely reliable.  For
8031 example, most disks map out bad sectors invisibly to the application; if
8032 the bad sectors contain sensitive data, @command{shred} won't be able to
8033 destroy it.
8034
8035 @command{shred} makes no attempt to detect or report this problem, just as
8036 it makes no attempt to do anything about backups.  However, since it is
8037 more reliable to shred devices than files, @command{shred} by default does
8038 not truncate or remove the output file.  This default is more suitable
8039 for devices, which typically cannot be truncated and should not be
8040 removed.
8041
8042 Finally, consider the risk of backups and mirrors.
8043 File system backups and remote mirrors may contain copies of the
8044 file that cannot be removed, and that will allow a shredded file
8045 to be recovered later.  So if you keep any data you may later want
8046 to destroy using @command{shred}, be sure that it is not backed up or mirrored.
8047
8048 @example
8049 shred [@var{option}]@dots{} @var{file}[@dots{}]
8050 @end example
8051
8052 The program accepts the following options.  Also see @ref{Common options}.
8053
8054 @table @samp
8055
8056 @item -f
8057 @itemx --force
8058 @opindex -f
8059 @opindex --force
8060 @cindex force deletion
8061 Override file permissions if necessary to allow overwriting.
8062
8063 @item -@var{NUMBER}
8064 @itemx -n @var{NUMBER}
8065 @itemx --iterations=@var{NUMBER}
8066 @opindex -n @var{NUMBER}
8067 @opindex --iterations=@var{NUMBER}
8068 @cindex iterations, selecting the number of
8069 By default, @command{shred} uses 25 passes of overwrite.  This is enough
8070 for all of the useful overwrite patterns to be used at least once.
8071 You can reduce this to save time, or increase it if you have a lot of
8072 time to waste.
8073
8074 @item --random-source=@var{file}
8075 @opindex --random-source
8076 @cindex random source for shredding
8077 Use @var{file} as a source of random data used to overwrite and to
8078 choose pass ordering.  @xref{Random sources}.
8079
8080 @item -s @var{BYTES}
8081 @itemx --size=@var{BYTES}
8082 @opindex -s @var{BYTES}
8083 @opindex --size=@var{BYTES}
8084 @cindex size of file to shred
8085 Shred the first @var{BYTES} bytes of the file.  The default is to shred
8086 the whole file.  @var{BYTES} can be followed by a size specification like
8087 @samp{K}, @samp{M}, or @samp{G} to specify a multiple.  @xref{Block size}.
8088
8089 @item -u
8090 @itemx --remove
8091 @opindex -u
8092 @opindex --remove
8093 @cindex removing files after shredding
8094 After shredding a file, truncate it (if possible) and then remove it.
8095 If a file has multiple links, only the named links will be removed.
8096
8097 @item -v
8098 @itemx --verbose
8099 @opindex -v
8100 @opindex --verbose
8101 Display status updates as sterilization proceeds.
8102
8103 @item -x
8104 @itemx --exact
8105 @opindex -x
8106 @opindex --exact
8107 By default, @command{shred} rounds the size of a regular file up to the next
8108 multiple of the file system block size to fully erase the last block of the file.
8109 Use @option{--exact} to suppress that behavior.
8110 Thus, by default if you shred a 10-byte regular file on a system with 512-byte
8111 blocks, the resulting file will be 512 bytes long.  With this option,
8112 shred does not increase the apparent size of the file.
8113
8114 @item -z
8115 @itemx --zero
8116 @opindex -z
8117 @opindex --zero
8118 Normally, the last pass that @command{shred} writes is made up of
8119 random data.  If this would be conspicuous on your hard drive (for
8120 example, because it looks like encrypted data), or you just think
8121 it's tidier, the @option{--zero} option adds an additional overwrite pass with
8122 all zero bits.  This is in addition to the number of passes specified
8123 by the @option{--iterations} option.
8124
8125 @end table
8126
8127 You might use the following command to erase all trace of the
8128 file system you'd created on the floppy disk in your first drive.
8129 That command takes about 20 minutes to erase a ``1.44MB'' (actually
8130 1440 KiB) floppy.
8131
8132 @example
8133 shred --verbose /dev/fd0
8134 @end example
8135
8136 Similarly, to erase all data on a selected partition of
8137 your hard disk, you could give a command like this:
8138
8139 @example
8140 shred --verbose /dev/sda5
8141 @end example
8142
8143 A @var{file} of @samp{-} denotes standard output.
8144 The intended use of this is to shred a removed temporary file.
8145 For example:
8146
8147 @example
8148 i=`tempfile -m 0600`
8149 exec 3<>"$i"
8150 rm -- "$i"
8151 echo "Hello, world" >&3
8152 shred - >&3
8153 exec 3>-
8154 @end example
8155
8156 However, the command @samp{shred - >file} does not shred the contents
8157 of @var{file}, since the shell truncates @var{file} before invoking
8158 @command{shred}.  Use the command @samp{shred file} or (if using a
8159 Bourne-compatible shell) the command @samp{shred - 1<>file} instead.
8160
8161 @exitstatus
8162
8163
8164 @node Special file types
8165 @chapter Special file types
8166
8167 @cindex special file types
8168 @cindex file types, special
8169
8170 This chapter describes commands which create special types of files (and
8171 @command{rmdir}, which removes directories, one special file type).
8172
8173 @cindex special file types
8174 @cindex file types
8175 Although Unix-like operating systems have markedly fewer special file
8176 types than others, not @emph{everything} can be treated only as the
8177 undifferentiated byte stream of @dfn{normal files}.  For example, when a
8178 file is created or removed, the system must record this information,
8179 which it does in a @dfn{directory}---a special type of file.  Although
8180 you can read directories as normal files, if you're curious, in order
8181 for the system to do its job it must impose a structure, a certain
8182 order, on the bytes of the file.  Thus it is a ``special'' type of file.
8183
8184 Besides directories, other special file types include named pipes
8185 (FIFOs), symbolic links, sockets, and so-called @dfn{special files}.
8186
8187 @menu
8188 * link invocation::             Make a hard link via the link syscall
8189 * ln invocation::               Make links between files.
8190 * mkdir invocation::            Make directories.
8191 * mkfifo invocation::           Make FIFOs (named pipes).
8192 * mknod invocation::            Make block or character special files.
8193 * readlink invocation::         Print the referent of a symbolic link.
8194 * rmdir invocation::            Remove empty directories.
8195 * unlink invocation::           Remove files via the unlink syscall
8196 @end menu
8197
8198
8199 @node link invocation
8200 @section @command{link}: Make a hard link via the link syscall
8201
8202 @pindex link
8203 @cindex links, creating
8204 @cindex hard links, creating
8205 @cindex creating links (hard only)
8206
8207 @command{link} creates a single hard link at a time.
8208 It is a minimalist interface to the system-provided
8209 @code{link} function.  @xref{Hard Links, , , libc,
8210 The GNU C Library Reference Manual}.
8211 It avoids the bells and whistles of the more commonly-used
8212 @command{ln} command (@pxref{ln invocation}).
8213 Synopsis:
8214
8215 @example
8216 link @var{filename} @var{linkname}
8217 @end example
8218
8219 @var{filename} must specify an existing file, and @var{linkname}
8220 must specify a nonexistent entry in an existing directory.
8221 @command{link} simply calls @code{link (@var{filename}, @var{linkname})}
8222 to create the link.
8223
8224 On a @acronym{GNU} system, this command acts like @samp{ln --directory
8225 --no-target-directory @var{filename} @var{linkname}}.  However, the
8226 @option{--directory} and @option{--no-target-directory} options are
8227 not specified by @acronym{POSIX}, and the @command{link} command is
8228 more portable in practice.
8229
8230 @exitstatus
8231
8232
8233 @node ln invocation
8234 @section @command{ln}: Make links between files
8235
8236 @pindex ln
8237 @cindex links, creating
8238 @cindex hard links, creating
8239 @cindex symbolic (soft) links, creating
8240 @cindex creating links (hard or soft)
8241
8242 @cindex file systems and hard links
8243 @command{ln} makes links between files.  By default, it makes hard links;
8244 with the @option{-s} option, it makes symbolic (or @dfn{soft}) links.
8245 Synopses:
8246
8247 @example
8248 ln [@var{option}]@dots{} [-T] @var{target} @var{linkname}
8249 ln [@var{option}]@dots{} @var{target}
8250 ln [@var{option}]@dots{} @var{target}@dots{} @var{directory}
8251 ln [@var{option}]@dots{} -t @var{directory} @var{target}@dots{}
8252 @end example
8253
8254 @itemize @bullet
8255
8256 @item
8257 If two file names are given, @command{ln} creates a link to the first
8258 file from the second.
8259
8260 @item
8261 If one @var{target} is given, @command{ln} creates a link to that file
8262 in the current directory.
8263
8264 @item
8265 If the @option{--target-directory} (@option{-t}) option is given, or
8266 failing that if the last file is a directory and the
8267 @option{--no-target-directory} (@option{-T}) option is not given,
8268 @command{ln} creates a link to each @var{target} file in the specified
8269 directory, using the @var{target}s' names.
8270
8271 @end itemize
8272
8273 Normally @command{ln} does not remove existing files.  Use the
8274 @option{--force} (@option{-f}) option to remove them unconditionally,
8275 the @option{--interactive} (@option{-i}) option to remove them
8276 conditionally, and the @option{--backup} (@option{-b}) option to
8277 rename them.
8278
8279 @cindex hard link, defined
8280 @cindex inode, and hard links
8281 A @dfn{hard link} is another name for an existing file; the link and the
8282 original are indistinguishable.  Technically speaking, they share the
8283 same inode, and the inode contains all the information about a
8284 file---indeed, it is not incorrect to say that the inode @emph{is} the
8285 file.  On all existing implementations, you cannot make a hard link to
8286 a directory, and hard links cannot cross file system boundaries.  (These
8287 restrictions are not mandated by @acronym{POSIX}, however.)
8288
8289 @cindex dereferencing symbolic links
8290 @cindex symbolic link, defined
8291 @dfn{Symbolic links} (@dfn{symlinks} for short), on the other hand, are
8292 a special file type (which not all kernels support: System V release 3
8293 (and older) systems lack symlinks) in which the link file actually
8294 refers to a different file, by name.  When most operations (opening,
8295 reading, writing, and so on) are passed the symbolic link file, the
8296 kernel automatically @dfn{dereferences} the link and operates on the
8297 target of the link.  But some operations (e.g., removing) work on the
8298 link file itself, rather than on its target.  @xref{Symbolic Links,,,
8299 libc, The GNU C Library Reference Manual}.
8300
8301 The program accepts the following options.  Also see @ref{Common options}.
8302
8303 @table @samp
8304
8305 @optBackup
8306
8307 @item -d
8308 @itemx -F
8309 @itemx --directory
8310 @opindex -d
8311 @opindex -F
8312 @opindex --directory
8313 @cindex hard links to directories
8314 Allow users with appropriate privileges to attempt to make hard links
8315 to directories.
8316 However, note that this will probably fail due to
8317 system restrictions, even for the super-user.
8318
8319 @item -f
8320 @itemx --force
8321 @opindex -f
8322 @opindex --force
8323 Remove existing destination files.
8324
8325 @item -i
8326 @itemx --interactive
8327 @opindex -i
8328 @opindex --interactive
8329 @cindex prompting, and @command{ln}
8330 Prompt whether to remove existing destination files.
8331
8332 @item -n
8333 @itemx --no-dereference
8334 @opindex -n
8335 @opindex --no-dereference
8336 Do not treat the last operand specially when it is a symbolic link to
8337 a directory.  Instead, treat it as if it were a normal file.
8338
8339 When the destination is an actual directory (not a symlink to one),
8340 there is no ambiguity.  The link is created in that directory.
8341 But when the specified destination is a symlink to a directory,
8342 there are two ways to treat the user's request.  @command{ln} can
8343 treat the destination just as it would a normal directory and create
8344 the link in it.  On the other hand, the destination can be viewed as a
8345 non-directory---as the symlink itself.  In that case, @command{ln}
8346 must delete or backup that symlink before creating the new link.
8347 The default is to treat a destination that is a symlink to a directory
8348 just like a directory.
8349
8350 This option is weaker than the @option{--no-target-directory}
8351 (@option{-T}) option, so it has no effect if both options are given.
8352
8353 @item -s
8354 @itemx --symbolic
8355 @opindex -s
8356 @opindex --symbolic
8357 Make symbolic links instead of hard links.  This option merely produces
8358 an error message on systems that do not support symbolic links.
8359
8360 @optBackupSuffix
8361
8362 @optTargetDirectory
8363
8364 @optNoTargetDirectory
8365
8366 @item -v
8367 @itemx --verbose
8368 @opindex -v
8369 @opindex --verbose
8370 Print the name of each file after linking it successfully.
8371
8372 @end table
8373
8374 @exitstatus
8375
8376 Examples:
8377
8378 @smallexample
8379 Bad Example:
8380
8381 # Create link ../a pointing to a in that directory.
8382 # Not really useful because it points to itself.
8383 ln -s a ..
8384
8385 Better Example:
8386
8387 # Change to the target before creating symlinks to avoid being confused.
8388 cd ..
8389 ln -s adir/a .
8390
8391 Bad Example:
8392
8393 # Hard coded file names don't move well.
8394 ln -s $(pwd)/a /some/dir/
8395
8396 Better Example:
8397
8398 # Relative file names survive directory moves and also
8399 # work across networked file systems.
8400 ln -s afile anotherfile
8401 ln -s ../adir/afile yetanotherfile
8402 @end smallexample
8403
8404
8405 @node mkdir invocation
8406 @section @command{mkdir}: Make directories
8407
8408 @pindex mkdir
8409 @cindex directories, creating
8410 @cindex creating directories
8411
8412 @command{mkdir} creates directories with the specified names.  Synopsis:
8413
8414 @example
8415 mkdir [@var{option}]@dots{} @var{name}@dots{}
8416 @end example
8417
8418 @command{mkdir} creates each directory @var{name} in the order given.
8419 It reports an error if @var{name} already exists, unless the
8420 @option{-p} option is given and @var{name} is a directory.
8421
8422 The program accepts the following options.  Also see @ref{Common options}.
8423
8424 @table @samp
8425
8426 @item -m @var{mode}
8427 @itemx --mode=@var{mode}
8428 @opindex -m
8429 @opindex --mode
8430 @cindex modes of created directories, setting
8431 Set the file permission bits of created directories to @var{mode},
8432 which uses the same syntax as
8433 in @command{chmod} and uses @samp{a=rwx} (read, write and execute allowed for
8434 everyone) for the point of the departure.  @xref{File permissions}.
8435
8436 Normally the directory has the desired file mode bits at the moment it
8437 is created.  As a @acronym{GNU} extension, @var{mode} may also mention
8438 special mode bits, but in this case there may be a temporary window
8439 during which the directory exists but its special mode bits are
8440 incorrect.  @xref{Directory Setuid and Setgid}, for how the
8441 set-user-ID and set-group-ID bits of directories are inherited unless
8442 overridden in this way.
8443
8444 @item -p
8445 @itemx --parents
8446 @opindex -p
8447 @opindex --parents
8448 @cindex parent directories, creating
8449 Make any missing parent directories for each argument, setting their
8450 file permission bits to the umask modified by @samp{u+wx}.  Ignore
8451 existing parent directories, and do not change their file permission
8452 bits.
8453
8454 To set the file permission bits of any newly-created parent
8455 directories to a value that includes @samp{u+wx}, you can set the
8456 umask before invoking @command{mkdir}.  For example, if the shell
8457 command @samp{(umask u=rwx,go=rx; mkdir -p P/Q)} creates the parent
8458 @file{P} it sets the parent's permission bits to @samp{u=rwx,go=rx}.
8459 To set a parent's special mode bits as well, you can invoke
8460 @command{chmod} after @command{mkdir}.  @xref{Directory Setuid and
8461 Setgid}, for how the set-user-ID and set-group-ID bits of
8462 newly-created parent directories are inherited.
8463
8464 @item -v
8465 @itemx --verbose
8466 @opindex -v
8467 @opindex --verbose
8468 Print a message for each created directory.  This is most useful with
8469 @option{--parents}.
8470 @end table
8471
8472 @exitstatus
8473
8474
8475 @node mkfifo invocation
8476 @section @command{mkfifo}: Make FIFOs (named pipes)
8477
8478 @pindex mkfifo
8479 @cindex FIFOs, creating
8480 @cindex named pipes, creating
8481 @cindex creating FIFOs (named pipes)
8482
8483 @command{mkfifo} creates FIFOs (also called @dfn{named pipes}) with the
8484 specified names.  Synopsis:
8485
8486 @example
8487 mkfifo [@var{option}] @var{name}@dots{}
8488 @end example
8489
8490 A @dfn{FIFO} is a special file type that permits independent processes
8491 to communicate.  One process opens the FIFO file for writing, and
8492 another for reading, after which data can flow as with the usual
8493 anonymous pipe in shells or elsewhere.
8494
8495 The program accepts the following option.  Also see @ref{Common options}.
8496
8497 @table @samp
8498
8499 @item -m @var{mode}
8500 @itemx --mode=@var{mode}
8501 @opindex -m
8502 @opindex --mode
8503 @cindex modes of created FIFOs, setting
8504 Set the mode of created FIFOs to @var{mode}, which is symbolic as in
8505 @command{chmod} and uses @samp{a=rw} (read and write allowed for everyone)
8506 for the point of departure.  @var{mode} should specify only file
8507 permission bits.  @xref{File permissions}.
8508
8509 @end table
8510
8511 @exitstatus
8512
8513
8514 @node mknod invocation
8515 @section @command{mknod}: Make block or character special files
8516
8517 @pindex mknod
8518 @cindex block special files, creating
8519 @cindex character special files, creating
8520
8521 @command{mknod} creates a FIFO, character special file, or block special
8522 file with the specified name.  Synopsis:
8523
8524 @example
8525 mknod [@var{option}]@dots{} @var{name} @var{type} [@var{major} @var{minor}]
8526 @end example
8527
8528 @cindex special files
8529 @cindex block special files
8530 @cindex character special files
8531 Unlike the phrase ``special file type'' above, the term @dfn{special
8532 file} has a technical meaning on Unix: something that can generate or
8533 receive data.  Usually this corresponds to a physical piece of hardware,
8534 e.g., a printer or a disk.  (These files are typically created at
8535 system-configuration time.)  The @command{mknod} command is what creates
8536 files of this type.  Such devices can be read either a character at a
8537 time or a ``block'' (many characters) at a time, hence we say there are
8538 @dfn{block special} files and @dfn{character special} files.
8539
8540 The arguments after @var{name} specify the type of file to make:
8541
8542 @table @samp
8543
8544 @item p
8545 @opindex p @r{for FIFO file}
8546 for a FIFO
8547
8548 @item b
8549 @opindex b @r{for block special file}
8550 for a block special file
8551
8552 @item c
8553 @c Don't document the `u' option -- it's just a synonym for `c'.
8554 @c Do *any* versions of mknod still use it?
8555 @c @itemx u
8556 @opindex c @r{for character special file}
8557 @c @opindex u @r{for character special file}
8558 for a character special file
8559
8560 @end table
8561
8562 When making a block or character special file, the major and minor
8563 device numbers must be given after the file type.
8564 If a major or minor device number begins with @samp{0x} or @samp{0X},
8565 it is interpreted as hexadecimal; otherwise, if it begins with @samp{0},
8566 as octal; otherwise, as decimal.
8567
8568 The program accepts the following option.  Also see @ref{Common options}.
8569
8570 @table @samp
8571
8572 @item -m @var{mode}
8573 @itemx --mode=@var{mode}
8574 @opindex -m
8575 @opindex --mode
8576 Set the mode of created files to @var{mode}, which is symbolic as in
8577 @command{chmod} and uses @samp{a=rw} as the point of departure.
8578 @var{mode} should specify only file permission bits.
8579 @xref{File permissions}.
8580
8581 @end table
8582
8583 @exitstatus
8584
8585
8586 @node readlink invocation
8587 @section @command{readlink}: Print the referent of a symbolic link
8588
8589 @pindex readlink
8590 @cindex displaying value of a symbolic link
8591
8592 @command{readlink} may work in one of two supported modes:
8593
8594 @table @samp
8595
8596 @item Readlink mode
8597
8598 @command{readlink} outputs the value of the given symbolic link.
8599 If @command{readlink} is invoked with an argument other than the name
8600 of a symbolic link, it produces no output and exits with a nonzero exit code.
8601
8602 @item Canonicalize mode
8603
8604 @command{readlink} outputs the absolute name of the given file which contains
8605 no @file{.}, @file{..} components nor any repeated separators
8606 (@file{/}) or symbolic links.
8607
8608 @end table
8609
8610 @example
8611 readlink [@var{option}] @var{file}
8612 @end example
8613
8614 By default, @command{readlink} operates in readlink mode.
8615
8616 The program accepts the following options.  Also see @ref{Common options}.
8617
8618 @table @samp
8619
8620 @item -f
8621 @itemx --canonicalize
8622 @opindex -f
8623 @opindex --canonicalize
8624 Activate canonicalize mode.
8625 If any component of the file name except the last one is missing or unavailable,
8626 @command{readlink} produces no output and exits with a nonzero exit code.
8627
8628 @item -e
8629 @itemx --canonicalize-existing
8630 @opindex -e
8631 @opindex --canonicalize-existing
8632 Activate canonicalize mode.
8633 If any component is missing or unavailable, @command{readlink} produces
8634 no output and exits with a nonzero exit code.
8635
8636 @item -m
8637 @itemx --canonicalize-missing
8638 @opindex -m
8639 @opindex --canonicalize-missing
8640 Activate canonicalize mode.
8641 If any component is missing or unavailable, @command{readlink} treats it
8642 as a directory.
8643
8644 @item -n
8645 @itemx --no-newline
8646 @opindex -n
8647 @opindex --no-newline
8648 Do not output the trailing newline.
8649
8650 @item -s
8651 @itemx -q
8652 @itemx --silent
8653 @itemx --quiet
8654 @opindex -s
8655 @opindex -q
8656 @opindex --silent
8657 @opindex --quiet
8658 Suppress most error messages.
8659
8660 @item -v
8661 @itemx --verbose
8662 @opindex -v
8663 @opindex --verbose
8664 Report error messages.
8665
8666 @end table
8667
8668 The @command{readlink} utility first appeared in OpenBSD 2.1.
8669
8670 @exitstatus
8671
8672
8673 @node rmdir invocation
8674 @section @command{rmdir}: Remove empty directories
8675
8676 @pindex rmdir
8677 @cindex removing empty directories
8678 @cindex directories, removing empty
8679
8680 @command{rmdir} removes empty directories.  Synopsis:
8681
8682 @example
8683 rmdir [@var{option}]@dots{} @var{directory}@dots{}
8684 @end example
8685
8686 If any @var{directory} argument does not refer to an existing empty
8687 directory, it is an error.
8688
8689 The program accepts the following option.  Also see @ref{Common options}.
8690
8691 @table @samp
8692
8693 @item --ignore-fail-on-non-empty
8694 @opindex --ignore-fail-on-non-empty
8695 @cindex directory deletion, ignoring failures
8696 Ignore each failure to remove a directory that is solely because
8697 the directory is non-empty.
8698
8699 @item -p
8700 @itemx --parents
8701 @opindex -p
8702 @opindex --parents
8703 @cindex parent directories, removing
8704 Remove @var{directory}, then try to remove each component of @var{directory}.
8705 So, for example, @samp{rmdir -p a/b/c} is similar to @samp{rmdir a/b/c a/b a}.
8706 As such, it fails if any of those directories turns out not to be empty.
8707 Use the @option{--ignore-fail-on-non-empty} option to make it so such
8708 a failure does not evoke a diagnostic and does not cause @command{rmdir} to
8709 exit unsuccessfully.
8710
8711 @item -v
8712 @itemx --verbose
8713 @opindex -v
8714 @opindex --verbose
8715 @cindex directory deletion, reporting
8716 Give a diagnostic for each successful removal.
8717 @var{directory} is removed.
8718
8719 @end table
8720
8721 @xref{rm invocation}, for how to remove non-empty directories (recursively).
8722
8723 @exitstatus
8724
8725
8726 @node unlink invocation
8727 @section @command{unlink}: Remove files via the unlink syscall
8728
8729 @pindex unlink
8730 @cindex removing files or directories (via the unlink syscall)
8731
8732 @command{unlink} deletes a single specified file name.
8733 It is a minimalist interface to the system-provided
8734 @code{unlink} function.  @xref{Deleting Files, , , libc,
8735 The GNU C Library Reference Manual}.  Synopsis:
8736 It avoids the bells and whistles of the more commonly-used
8737 @command{rm} command (@pxref{rm invocation}).
8738
8739 @example
8740 unlink @var{filename}
8741 @end example
8742
8743 On some systems @code{unlink} can be used to delete the name of a
8744 directory.  On others, it can be used that way only by a privileged user.
8745 In the GNU system @code{unlink} can never delete the name of a directory.
8746
8747 The @command{unlink} command honors the @option{--help} and
8748 @option{--version} options.  To remove a file whose name begins with
8749 @samp{-}, prefix the name with @samp{./}, e.g., @samp{unlink ./--help}.
8750
8751 @exitstatus
8752
8753
8754 @node Changing file attributes
8755 @chapter Changing file attributes
8756
8757 @cindex changing file attributes
8758 @cindex file attributes, changing
8759 @cindex attributes, file
8760
8761 A file is not merely its contents, a name, and a file type
8762 (@pxref{Special file types}).  A file also has an owner (a user ID), a
8763 group (a group ID), permissions (what the owner can do with the file,
8764 what people in the group can do, and what everyone else can do), various
8765 timestamps, and other information.  Collectively, we call these a file's
8766 @dfn{attributes}.
8767
8768 These commands change file attributes.
8769
8770 @menu
8771 * chgrp invocation::            Change file groups.
8772 * chmod invocation::            Change access permissions.
8773 * chown invocation::            Change file owners and groups.
8774 * touch invocation::            Change file timestamps.
8775 @end menu
8776
8777
8778 @node chown invocation
8779 @section @command{chown}: Change file owner and group
8780
8781 @pindex chown
8782 @cindex file ownership, changing
8783 @cindex group ownership, changing
8784 @cindex changing file ownership
8785 @cindex changing group ownership
8786
8787 @command{chown} changes the user and/or group ownership of each given @var{file}
8788 to @var{new-owner} or to the user and group of an existing reference file.
8789 Synopsis:
8790
8791 @example
8792 chown [@var{option}]@dots{} @{@var{new-owner} | --reference=@var{ref_file}@} @var{file}@dots{}
8793 @end example
8794
8795 If used, @var{new-owner} specifies the new owner and/or group as follows
8796 (with no embedded white space):
8797
8798 @example
8799 [@var{owner}] [ : [@var{group}] ]
8800 @end example
8801
8802 Specifically:
8803
8804 @table @var
8805 @item owner
8806 If only an @var{owner} (a user name or numeric user ID) is given, that
8807 user is made the owner of each given file, and the files' group is not
8808 changed.
8809
8810 @item owner@samp{:}group
8811 If the @var{owner} is followed by a colon and a @var{group} (a
8812 group name or numeric group ID), with no spaces between them, the group
8813 ownership of the files is changed as well (to @var{group}).
8814
8815 @item owner@samp{:}
8816 If a colon but no group name follows @var{owner}, that user is
8817 made the owner of the files and the group of the files is changed to
8818 @var{owner}'s login group.
8819
8820 @item @samp{:}group
8821 If the colon and following @var{group} are given, but the owner
8822 is omitted, only the group of the files is changed; in this case,
8823 @command{chown} performs the same function as @command{chgrp}.
8824
8825 @item @samp{:}
8826 If only a colon is given, or if @var{new-owner} is empty, neither the
8827 owner nor the group is changed.
8828
8829 @end table
8830
8831 If @var{owner} or @var{group} is intended to represent a numeric user
8832 or group ID, then you may specify it with a leading @samp{+}.
8833 @xref{Disambiguating names and IDs}.
8834
8835 Some older scripts may still use @samp{.} in place of the @samp{:} separator.
8836 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not
8837 require support for that, but for backward compatibility @acronym{GNU}
8838 @command{chown} supports @samp{.} so long as no ambiguity results.
8839 New scripts should avoid the use of @samp{.} because it is not
8840 portable, and because it has undesirable results if the entire
8841 @var{owner@samp{.}group} happens to identify a user whose name
8842 contains @samp{.}.
8843
8844 The @command{chown} command sometimes clears the set-user-ID or
8845 set-group-ID permission bits.  This behavior depends on the policy and
8846 functionality of the underlying @code{chown} system call, which may
8847 make system-dependent file mode modifications outside the control of
8848 the @command{chown} command.  For example, the @command{chown} command
8849 might not affect those bits when invoked by a user with appropriate
8850 privileges, or when the
8851 bits signify some function other than executable permission (e.g.,
8852 mandatory locking).
8853 When in doubt, check the underlying system behavior.
8854
8855 The program accepts the following options.  Also see @ref{Common options}.
8856
8857 @table @samp
8858
8859 @item -c
8860 @itemx --changes
8861 @opindex -c
8862 @opindex --changes
8863 @cindex changed owners, verbosely describing
8864 Verbosely describe the action for each @var{file} whose ownership
8865 actually changes.
8866
8867 @item -f
8868 @itemx --silent
8869 @itemx --quiet
8870 @opindex -f
8871 @opindex --silent
8872 @opindex --quiet
8873 @cindex error messages, omitting
8874 Do not print error messages about files whose ownership cannot be
8875 changed.
8876
8877 @itemx @w{@kbd{--from}=@var{old-owner}}
8878 @opindex --from
8879 @cindex symbolic links, changing owner
8880 Change a @var{file}'s ownership only if it has current attributes specified
8881 by @var{old-owner}.  @var{old-owner} has the same form as @var{new-owner}
8882 described above.
8883 This option is useful primarily from a security standpoint in that
8884 it narrows considerably the window of potential abuse.
8885 For example, to reflect a user ID numbering change for one user's files
8886 without an option like this, @code{root} might run
8887
8888 @smallexample
8889 find / -owner OLDUSER -print0 | xargs -0 chown -h NEWUSER
8890 @end smallexample
8891
8892 But that is dangerous because the interval between when the @command{find}
8893 tests the existing file's owner and when the @command{chown} is actually run
8894 may be quite large.
8895 One way to narrow the gap would be to invoke chown for each file
8896 as it is found:
8897
8898 @example
8899 find / -owner OLDUSER -exec chown -h NEWUSER @{@} \;
8900 @end example
8901
8902 But that is very slow if there are many affected files.
8903 With this option, it is safer (the gap is narrower still)
8904 though still not perfect:
8905
8906 @example
8907 chown -h -R --from=OLDUSER NEWUSER /
8908 @end example
8909
8910 @item --dereference
8911 @opindex --dereference
8912 @cindex symbolic links, changing owner
8913 @findex lchown
8914 Do not act on symbolic links themselves but rather on what they point to.
8915 This is the default.
8916
8917 @item -h
8918 @itemx --no-dereference
8919 @opindex -h
8920 @opindex --no-dereference
8921 @cindex symbolic links, changing owner
8922 @findex lchown
8923 Act on symbolic links themselves instead of what they point to.
8924 This mode relies on the @code{lchown} system call.
8925 On systems that do not provide the @code{lchown} system call,
8926 @command{chown} fails when a file specified on the command line
8927 is a symbolic link.
8928 By default, no diagnostic is issued for symbolic links encountered
8929 during a recursive traversal, but see @option{--verbose}.
8930
8931 @itemx --preserve-root
8932 @opindex --preserve-root
8933 @cindex root directory, disallow recursive modification
8934 Fail upon any attempt to recursively change the root directory, @file{/}.
8935 Without @option{--recursive}, this option has no effect.
8936 @xref{Treating / specially}.
8937
8938 @itemx --no-preserve-root
8939 @opindex --no-preserve-root
8940 @cindex root directory, allow recursive modification
8941 Cancel the effect of any preceding @option{--preserve-root} option.
8942 @xref{Treating / specially}.
8943
8944 @item --reference=@var{ref_file}
8945 @opindex --reference
8946 Change the user and group of each @var{file} to be the same as those of
8947 @var{ref_file}.  If @var{ref_file} is a symbolic link, do not use the
8948 user and group of the symbolic link, but rather those of the file it
8949 refers to.
8950
8951 @item -v
8952 @itemx --verbose
8953 @opindex -v
8954 @opindex --verbose
8955 Output a diagnostic for every file processed.
8956 If a symbolic link is encountered during a recursive traversal
8957 on a system without the @code{lchown} system call, and @option{--no-dereference}
8958 is in effect, then issue a diagnostic saying neither the symbolic link nor
8959 its referent is being changed.
8960
8961 @item -R
8962 @itemx --recursive
8963 @opindex -R
8964 @opindex --recursive
8965 @cindex recursively changing file ownership
8966 Recursively change ownership of directories and their contents.
8967
8968 @choptH
8969 @xref{Traversing symlinks}.
8970
8971 @choptL
8972 @xref{Traversing symlinks}.
8973
8974 @choptP
8975 @xref{Traversing symlinks}.
8976
8977 @end table
8978
8979 @exitstatus
8980
8981 Examples:
8982
8983 @smallexample
8984 # Change the owner of /u to "root".
8985 chown root /u
8986
8987 # Likewise, but also change its group to "staff".
8988 chown root:staff /u
8989
8990 # Change the owner of /u and subfiles to "root".
8991 chown -hR root /u
8992 @end smallexample
8993
8994
8995 @node chgrp invocation
8996 @section @command{chgrp}: Change group ownership
8997
8998 @pindex chgrp
8999 @cindex group ownership, changing
9000 @cindex changing group ownership
9001
9002 @command{chgrp} changes the group ownership of each given @var{file}
9003 to @var{group} (which can be either a group name or a numeric group ID)
9004 or to the group of an existing reference file.  Synopsis:
9005
9006 @example
9007 chgrp [@var{option}]@dots{} @{@var{group} | --reference=@var{ref_file}@} @var{file}@dots{}
9008 @end example
9009
9010 If @var{group} is intended to represent a
9011 numeric group ID, then you may specify it with a leading @samp{+}.
9012 @xref{Disambiguating names and IDs}.
9013
9014 The program accepts the following options.  Also see @ref{Common options}.
9015
9016 @table @samp
9017
9018 @item -c
9019 @itemx --changes
9020 @opindex -c
9021 @opindex --changes
9022 @cindex changed files, verbosely describing
9023 Verbosely describe the action for each @var{file} whose group actually
9024 changes.
9025
9026 @item -f
9027 @itemx --silent
9028 @itemx --quiet
9029 @opindex -f
9030 @opindex --silent
9031 @opindex --quiet
9032 @cindex error messages, omitting
9033 Do not print error messages about files whose group cannot be
9034 changed.
9035
9036 @item --dereference
9037 @opindex --dereference
9038 @cindex symbolic links, changing owner
9039 @findex lchown
9040 Do not act on symbolic links themselves but rather on what they point to.
9041 This is the default.
9042
9043 @item -h
9044 @itemx --no-dereference
9045 @opindex -h
9046 @opindex --no-dereference
9047 @cindex symbolic links, changing group
9048 @findex lchown
9049 Act on symbolic links themselves instead of what they point to.
9050 This mode relies on the @code{lchown} system call.
9051 On systems that do not provide the @code{lchown} system call,
9052 @command{chgrp} fails when a file specified on the command line
9053 is a symbolic link.
9054 By default, no diagnostic is issued for symbolic links encountered
9055 during a recursive traversal, but see @option{--verbose}.
9056
9057 @itemx --preserve-root
9058 @opindex --preserve-root
9059 @cindex root directory, disallow recursive modification
9060 Fail upon any attempt to recursively change the root directory, @file{/}.
9061 Without @option{--recursive}, this option has no effect.
9062 @xref{Treating / specially}.
9063
9064 @itemx --no-preserve-root
9065 @opindex --no-preserve-root
9066 @cindex root directory, allow recursive modification
9067 Cancel the effect of any preceding @option{--preserve-root} option.
9068 @xref{Treating / specially}.
9069
9070 @item --reference=@var{ref_file}
9071 @opindex --reference
9072 Change the group of each @var{file} to be the same as that of
9073 @var{ref_file}.  If @var{ref_file} is a symbolic link, do not use the
9074 group of the symbolic link, but rather that of the file it refers to.
9075
9076 @item -v
9077 @itemx --verbose
9078 @opindex -v
9079 @opindex --verbose
9080 Output a diagnostic for every file processed.
9081 If a symbolic link is encountered during a recursive traversal
9082 on a system without the @code{lchown} system call, and @option{--no-dereference}
9083 is in effect, then issue a diagnostic saying neither the symbolic link nor
9084 its referent is being changed.
9085
9086 @item -R
9087 @itemx --recursive
9088 @opindex -R
9089 @opindex --recursive
9090 @cindex recursively changing group ownership
9091 Recursively change the group ownership of directories and their contents.
9092
9093 @choptH
9094 @xref{Traversing symlinks}.
9095
9096 @choptL
9097 @xref{Traversing symlinks}.
9098
9099 @choptP
9100 @xref{Traversing symlinks}.
9101
9102 @end table
9103
9104 @exitstatus
9105
9106 Examples:
9107
9108 @smallexample
9109 # Change the group of /u to "staff".
9110 chgrp staff /u
9111
9112 # Change the group of /u and subfiles to "staff".
9113 chgrp -hR staff /u
9114 @end smallexample
9115
9116
9117 @node chmod invocation
9118 @section @command{chmod}: Change access permissions
9119
9120 @pindex chmod
9121 @cindex changing access permissions
9122 @cindex access permissions, changing
9123 @cindex permissions, changing access
9124
9125 @command{chmod} changes the access permissions of the named files.  Synopsis:
9126
9127 @example
9128 chmod [@var{option}]@dots{} @{@var{mode} | --reference=@var{ref_file}@} @var{file}@dots{}
9129 @end example
9130
9131 @cindex symbolic links, permissions of
9132 @command{chmod} never changes the permissions of symbolic links, since
9133 the @command{chmod} system call cannot change their permissions.
9134 This is not a problem since the permissions of symbolic links are
9135 never used.  However, for each symbolic link listed on the command
9136 line, @command{chmod} changes the permissions of the pointed-to file.
9137 In contrast, @command{chmod} ignores symbolic links encountered during
9138 recursive directory traversals.
9139
9140 A successful use of @command{chmod} clears the set-group-ID bit of a
9141 regular file if the file's group ID does not match the user's
9142 effective group ID or one of the user's supplementary group IDs,
9143 unless the user has appropriate privileges.  Additional restrictions
9144 may cause the set-user-ID and set-group-ID bits of @var{mode} or
9145 @var{ref_file} to be ignored.  This behavior depends on the policy and
9146 functionality of the underlying @code{chmod} system call.  When in
9147 doubt, check the underlying system behavior.
9148
9149 If used, @var{mode} specifies the new file mode bits.
9150 For details, see the section on @ref{File permissions}.
9151 If you really want @var{mode} to have a leading @samp{-}, you should
9152 use @option{--} first, e.g., @samp{chmod -- -w file}.  Typically,
9153 though, @samp{chmod a-w file} is preferable, and @command{chmod -w
9154 file} (without the @option{--}) complains if it behaves differently
9155 from what @samp{chmod a-w file} would do.
9156
9157 The program accepts the following options.  Also see @ref{Common options}.
9158
9159 @table @samp
9160
9161 @item -c
9162 @itemx --changes
9163 @opindex -c
9164 @opindex --changes
9165 Verbosely describe the action for each @var{file} whose permissions
9166 actually changes.
9167
9168 @item -f
9169 @itemx --silent
9170 @itemx --quiet
9171 @opindex -f
9172 @opindex --silent
9173 @opindex --quiet
9174 @cindex error messages, omitting
9175 Do not print error messages about files whose permissions cannot be
9176 changed.
9177
9178 @itemx --preserve-root
9179 @opindex --preserve-root
9180 @cindex root directory, disallow recursive modification
9181 Fail upon any attempt to recursively change the root directory, @file{/}.
9182 Without @option{--recursive}, this option has no effect.
9183 @xref{Treating / specially}.
9184
9185 @itemx --no-preserve-root
9186 @opindex --no-preserve-root
9187 @cindex root directory, allow recursive modification
9188 Cancel the effect of any preceding @option{--preserve-root} option.
9189 @xref{Treating / specially}.
9190
9191 @item -v
9192 @itemx --verbose
9193 @opindex -v
9194 @opindex --verbose
9195 Verbosely describe the action or non-action taken for every @var{file}.
9196
9197 @item --reference=@var{ref_file}
9198 @opindex --reference
9199 Change the mode of each @var{file} to be the same as that of @var{ref_file}.
9200 @xref{File permissions}.
9201 If @var{ref_file} is a symbolic link, do not use the mode
9202 of the symbolic link, but rather that of the file it refers to.
9203
9204 @item -R
9205 @itemx --recursive
9206 @opindex -R
9207 @opindex --recursive
9208 @cindex recursively changing access permissions
9209 Recursively change permissions of directories and their contents.
9210
9211 @end table
9212
9213 @exitstatus
9214
9215
9216 @node touch invocation
9217 @section @command{touch}: Change file timestamps
9218
9219 @pindex touch
9220 @cindex changing file timestamps
9221 @cindex file timestamps, changing
9222 @cindex timestamps, changing file
9223
9224 @command{touch} changes the access and/or modification times of the
9225 specified files.  Synopsis:
9226
9227 @example
9228 touch [@var{option}]@dots{} @var{file}@dots{}
9229 @end example
9230
9231 @cindex empty files, creating
9232 Any @var{file} that does not exist is created empty.
9233
9234 A @var{file} of @samp{-} causes @command{touch} to change the
9235 times of the file associated with standard output.
9236
9237 @cindex permissions, for changing file timestamps
9238 If changing both the access and modification times to the current
9239 time, @command{touch} can change the timestamps for files that the user
9240 running it does not own but has write permission for.  Otherwise, the
9241 user must own the files.
9242
9243 Although @command{touch} provides options for changing two of the times---the
9244 times of last access and modification---of a file, there is actually
9245 a third one as well: the inode change time.  This is often referred to
9246 as a file's @code{ctime}.
9247 The inode change time represents the time when the file's meta-information
9248 last changed.  One common example of this is when the permissions of a
9249 file change.  Changing the permissions doesn't access the file, so
9250 the atime doesn't change, nor does it modify the file, so the mtime
9251 doesn't change.  Yet, something about the file itself has changed,
9252 and this must be noted somewhere.  This is the job of the ctime field.
9253 This is necessary, so that, for example, a backup program can make a
9254 fresh copy of the file, including the new permissions value.
9255 Another operation that modifies a file's ctime without affecting
9256 the others is renaming.  In any case, it is not possible, in normal
9257 operations, for a user to change the ctime field to a user-specified value.
9258
9259 @vindex TZ
9260 Time stamps assume the time zone rules specified by the @env{TZ}
9261 environment variable, or by the system default rules if @env{TZ} is
9262 not set.  @xref{TZ Variable,, Specifying the Time Zone with @env{TZ},
9263 libc, The GNU C Library}.  You can avoid ambiguities during
9264 daylight saving transitions by using @sc{utc} time stamps.
9265
9266 The program accepts the following options.  Also see @ref{Common options}.
9267
9268 @table @samp
9269
9270 @item -a
9271 @itemx --time=atime
9272 @itemx --time=access
9273 @itemx --time=use
9274 @opindex -a
9275 @opindex --time
9276 @opindex atime@r{, changing}
9277 @opindex access @r{time, changing}
9278 @opindex use @r{time, changing}
9279 Change the access time only.
9280
9281 @item -c
9282 @itemx --no-create
9283 @opindex -c
9284 @opindex --no-create
9285 Do not create files that do not exist.
9286
9287 @item -d
9288 @itemx --date=@var{time}
9289 @opindex -d
9290 @opindex --date
9291 @opindex time
9292 Use @var{time} instead of the current time.  It can contain month names,
9293 time zones, @samp{am} and @samp{pm}, @samp{yesterday}, etc.  For
9294 example, @option{--date="2004-02-27 14:19:13.489392193 +0530"}
9295 specifies the instant of time that is 489,392,193 nanoseconds after
9296 February 27, 2004 at 2:19:13 PM in a time zone that is 5 hours and 30
9297 minutes east of @acronym{UTC}.  @xref{Date input formats}.
9298 File systems that do not support high-resolution time stamps
9299 silently ignore any excess precision here.
9300
9301 @item -f
9302 @opindex -f
9303 @cindex BSD @command{touch} compatibility
9304 Ignored; for compatibility with BSD versions of @command{touch}.
9305
9306 @item -m
9307 @itemx --time=mtime
9308 @itemx --time=modify
9309 @opindex -m
9310 @opindex --time
9311 @opindex mtime@r{, changing}
9312 @opindex modify @r{time, changing}
9313 Change the modification time only.
9314
9315 @item -r @var{file}
9316 @itemx --reference=@var{file}
9317 @opindex -r
9318 @opindex --reference
9319 Use the times of the reference @var{file} instead of the current time.
9320 If this option is combined with the @option{--date=@var{time}}
9321 (@option{-d @var{time}}) option, the reference @var{file}'s time is
9322 the origin for any relative @var{time}s given, but is otherwise ignored.
9323 For example, @samp{-r foo -d '-5 seconds'} specifies a time stamp
9324 equal to five seconds before the corresponding time stamp for @file{foo}.
9325
9326 @item -t [[@var{CC}]@var{YY}]@var{MMDDhhmm}[.@var{ss}]
9327 Use the argument (optional four-digit or two-digit years, months,
9328 days, hours, minutes, optional seconds) instead of the current time.
9329 If the year is specified with only two digits, then @var{CC}
9330 is 20 for years in the range 0 @dots{} 68, and 19 for years in
9331 69 @dots{} 99.  If no digits of the year are specified,
9332 the argument is interpreted as a date in the current year.
9333
9334 @end table
9335
9336 @vindex _POSIX2_VERSION
9337 On older systems, @command{touch} supports an obsolete syntax, as follows.
9338 If no timestamp is given with any of the @option{-d}, @option{-r}, or
9339 @option{-t} options, and if there are two or more @var{file}s and the
9340 first @var{file} is of the form @samp{@var{MMDDhhmm}[@var{YY}]} and this
9341 would be a valid argument to the @option{-t} option (if the @var{YY}, if
9342 any, were moved to the front), and if the represented year
9343 is in the range 1969--1999, that argument is interpreted as the time
9344 for the other files instead of as a file name.
9345 This obsolete behavior can be enabled or disabled with the
9346 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
9347 conformance}), but portable scripts should avoid commands whose
9348 behavior depends on this variable.
9349 For example, use @samp{touch ./12312359 main.c} or @samp{touch -t
9350 12312359 main.c} rather than the ambiguous @samp{touch 12312359 main.c}.
9351
9352 @exitstatus
9353
9354
9355 @node Disk usage
9356 @chapter Disk usage
9357
9358 @cindex disk usage
9359
9360 No disk can hold an infinite amount of data.  These commands report
9361 how much disk storage is in use or available, report other file and
9362 file status information, and write buffers to disk.
9363
9364 @menu
9365 * df invocation::               Report file system disk space usage.
9366 * du invocation::               Estimate file space usage.
9367 * stat invocation::             Report file or file system status.
9368 * sync invocation::             Synchronize memory and disk.
9369 @end menu
9370
9371
9372 @node df invocation
9373 @section @command{df}: Report file system disk space usage
9374
9375 @pindex df
9376 @cindex file system disk usage
9377 @cindex disk usage by file system
9378
9379 @command{df} reports the amount of disk space used and available on
9380 file systems.  Synopsis:
9381
9382 @example
9383 df [@var{option}]@dots{} [@var{file}]@dots{}
9384 @end example
9385
9386 With no arguments, @command{df} reports the space used and available on all
9387 currently mounted file systems (of all types).  Otherwise, @command{df}
9388 reports on the file system containing each argument @var{file}.
9389
9390 Normally the disk space is printed in units of
9391 1024 bytes, but this can be overridden (@pxref{Block size}).
9392 Non-integer quantities are rounded up to the next higher unit.
9393
9394 @cindex disk device file
9395 @cindex device file, disk
9396 If an argument @var{file} is a disk device file containing a mounted
9397 file system, @command{df} shows the space available on that file system
9398 rather than on the file system containing the device node (i.e., the root
9399 file system).  @sc{gnu} @command{df} does not attempt to determine the disk usage
9400 on unmounted file systems, because on most kinds of systems doing so
9401 requires extremely nonportable intimate knowledge of file system
9402 structures.
9403
9404 The program accepts the following options.  Also see @ref{Common options}.
9405
9406 @table @samp
9407
9408 @item -a
9409 @itemx --all
9410 @opindex -a
9411 @opindex --all
9412 @cindex automounter file systems
9413 @cindex ignore file systems
9414 Include in the listing dummy file systems, which
9415 are omitted by default.  Such file systems are typically special-purpose
9416 pseudo-file-systems, such as automounter entries.
9417
9418 @item -B @var{size}
9419 @itemx --block-size=@var{size}
9420 @opindex -B
9421 @opindex --block-size
9422 @cindex file system sizes
9423 Scale sizes by @var{size} before printing them (@pxref{Block size}).
9424 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
9425
9426 @optHumanReadable
9427
9428 @item -H
9429 @opindex -H
9430 Equivalent to @option{--si}.
9431
9432 @item -i
9433 @itemx --inodes
9434 @opindex -i
9435 @opindex --inodes
9436 @cindex inode usage
9437 List inode usage information instead of block usage.  An inode (short
9438 for index node) contains information about a file such as its owner,
9439 permissions, timestamps, and location on the disk.
9440
9441 @item -k
9442 @opindex -k
9443 @cindex kibibytes for file system sizes
9444 Print sizes in 1024-byte blocks, overriding the default block size
9445 (@pxref{Block size}).
9446 This option is equivalent to @option{--block-size=1K}.
9447
9448 @item -l
9449 @itemx --local
9450 @opindex -l
9451 @opindex --local
9452 @cindex file system types, limiting output to certain
9453 Limit the listing to local file systems.  By default, remote file systems
9454 are also listed.
9455
9456 @item --no-sync
9457 @opindex --no-sync
9458 @cindex file system space, retrieving old data more quickly
9459 Do not invoke the @code{sync} system call before getting any usage data.
9460 This may make @command{df} run significantly faster on systems with many
9461 disks, but on some systems (notably SunOS) the results may be slightly
9462 out of date.  This is the default.
9463
9464 @item -P
9465 @itemx --portability
9466 @opindex -P
9467 @opindex --portability
9468 @cindex one-line output format
9469 @cindex @acronym{POSIX} output format
9470 @cindex portable output format
9471 @cindex output format, portable
9472 Use the @acronym{POSIX} output format.  This is like the default format except
9473 for the following:
9474
9475 @enumerate
9476 @item
9477 The information about each file system is always printed on exactly
9478 one line; a mount device is never put on a line by itself.  This means
9479 that if the mount device name is more than 20 characters long (e.g., for
9480 some network mounts), the columns are misaligned.
9481
9482 @item
9483 The labels in the header output line are changed to conform to @acronym{POSIX}.
9484
9485 @item
9486 The default block size and output format are unaffected by the
9487 @env{DF_BLOCK_SIZE}, @env{BLOCK_SIZE} and @env{BLOCKSIZE} environment
9488 variables.  However, the default block size is still affected by
9489 @env{POSIXLY_CORRECT}: it is 512 if @env{POSIXLY_CORRECT} is set, 1024
9490 otherwise.  @xref{Block size}.
9491 @end enumerate
9492
9493 @optSi
9494
9495 @item --sync
9496 @opindex --sync
9497 @cindex file system space, retrieving current data more slowly
9498 Invoke the @code{sync} system call before getting any usage data.  On
9499 some systems (notably SunOS), doing this yields more up to date results,
9500 but in general this option makes @command{df} much slower, especially when
9501 there are many or very busy file systems.
9502
9503 @item -t @var{fstype}
9504 @itemx --type=@var{fstype}
9505 @opindex -t
9506 @opindex --type
9507 @cindex file system types, limiting output to certain
9508 Limit the listing to file systems of type @var{fstype}.  Multiple
9509 file system types can be specified by giving multiple @option{-t} options.
9510 By default, nothing is omitted.
9511
9512 @item -T
9513 @itemx --print-type
9514 @opindex -T
9515 @opindex --print-type
9516 @cindex file system types, printing
9517 Print each file system's type.  The types printed here are the same ones
9518 you can include or exclude with @option{-t} and @option{-x}.  The particular
9519 types printed are whatever is supported by the system.  Here are some of
9520 the common names (this list is certainly not exhaustive):
9521
9522 @table @samp
9523
9524 @item nfs
9525 @cindex @acronym{NFS} file system type
9526 An @acronym{NFS} file system, i.e., one mounted over a network from another
9527 machine.  This is the one type name which seems to be used uniformly by
9528 all systems.
9529
9530 @item 4.2@r{, }ufs@r{, }efs@dots{}
9531 @cindex Linux file system types
9532 @cindex local file system types
9533 @opindex 4.2 @r{file system type}
9534 @opindex ufs @r{file system type}
9535 @opindex efs @r{file system type}
9536 A file system on a locally-mounted hard disk.  (The system might even
9537 support more than one type here; Linux does.)
9538
9539 @item hsfs@r{, }cdfs
9540 @cindex CD-ROM file system type
9541 @cindex High Sierra file system
9542 @opindex hsfs @r{file system type}
9543 @opindex cdfs @r{file system type}
9544 A file system on a CD-ROM drive.  HP-UX uses @samp{cdfs}, most other
9545 systems use @samp{hsfs} (@samp{hs} for ``High Sierra'').
9546
9547 @item pcfs
9548 @cindex PC file system
9549 @cindex DOS file system
9550 @cindex MS-DOS file system
9551 @cindex diskette file system
9552 @opindex pcfs
9553 An MS-DOS file system, usually on a diskette.
9554
9555 @end table
9556
9557 @item -x @var{fstype}
9558 @itemx --exclude-type=@var{fstype}
9559 @opindex -x
9560 @opindex --exclude-type
9561 Limit the listing to file systems not of type @var{fstype}.
9562 Multiple file system types can be eliminated by giving multiple
9563 @option{-x} options.  By default, no file system types are omitted.
9564
9565 @item -v
9566 Ignored; for compatibility with System V versions of @command{df}.
9567
9568 @end table
9569
9570 @exitstatus
9571 Failure includes the case where no output is generated, so you can
9572 inspect the exit status of a command like @samp{df -t ext3 -t reiserfs
9573 @var{dir}} to test whether @var{dir} is on a file system of type
9574 @samp{ext3} or @samp{reiserfs}.
9575
9576
9577 @node du invocation
9578 @section @command{du}: Estimate file space usage
9579
9580 @pindex du
9581 @cindex file space usage
9582 @cindex disk usage for files
9583
9584 @command{du} reports the amount of disk space used by the specified files
9585 and for each subdirectory (of directory arguments).  Synopsis:
9586
9587 @example
9588 du [@var{option}]@dots{} [@var{file}]@dots{}
9589 @end example
9590
9591 With no arguments, @command{du} reports the disk space for the current
9592 directory.  Normally the disk space is printed in units of
9593 1024 bytes, but this can be overridden (@pxref{Block size}).
9594 Non-integer quantities are rounded up to the next higher unit.
9595
9596 The program accepts the following options.  Also see @ref{Common options}.
9597
9598 @table @samp
9599
9600 @item -a
9601 @itemx --all
9602 @opindex -a
9603 @opindex --all
9604 Show counts for all files, not just directories.
9605
9606 @itemx --apparent-size
9607 @opindex --apparent-size
9608 Print apparent sizes, rather than disk usage.  The apparent size of a
9609 file is the number of bytes reported by @code{wc -c} on regular files,
9610 or more generally, @code{ls -l --block-size=1} or @code{stat --format=%s}.
9611 For example, a file containing the word @samp{zoo} with no newline would,
9612 of course, have an apparent size of 3.  Such a small file may require
9613 anywhere from 0 to 16 KiB or more of disk space, depending on
9614 the type and configuration of the file system on which the file resides.
9615 However, a sparse file created with this command:
9616
9617 @example
9618 dd bs=1 seek=2GiB if=/dev/null of=big
9619 @end example
9620
9621 @noindent
9622 has an apparent size of 2 GiB, yet on most modern
9623 systems, it actually uses almost no disk space.
9624
9625 @item -b
9626 @itemx --bytes
9627 @opindex -b
9628 @opindex --bytes
9629 Equivalent to @code{--apparent-size --block-size=1}.
9630
9631 @item -B @var{size}
9632 @itemx --block-size=@var{size}
9633 @opindex -B
9634 @opindex --block-size
9635 @cindex file sizes
9636 Scale sizes by @var{size} before printing them (@pxref{Block size}).
9637 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
9638
9639 @item -c
9640 @itemx --total
9641 @opindex -c
9642 @opindex --total
9643 @cindex grand total of disk space
9644 Print a grand total of all arguments after all arguments have
9645 been processed.  This can be used to find out the total disk usage of
9646 a given set of files or directories.
9647
9648 @item -D
9649 @itemx --dereference-args
9650 @opindex -D
9651 @opindex --dereference-args
9652 Dereference symbolic links that are command line arguments.
9653 Does not affect other symbolic links.  This is helpful for finding
9654 out the disk usage of directories, such as @file{/usr/tmp}, which
9655 are often symbolic links.
9656
9657 @itemx --files0-from=@var{FILE}
9658 @opindex --files0-from=@var{FILE}
9659 @cindex including files from @command{du}
9660 Rather than processing files named on the command line, process those
9661 named in file @var{FILE}; each name is terminated by a null byte.
9662 This is useful with the @option{--total} (@option{-c}) option when
9663 the list of file names is so long that it may exceed a command line
9664 length limitation.
9665 In such cases, running @command{du} via @command{xargs} is undesirable
9666 because it splits the list into pieces and makes @command{du} print a
9667 total for each sublist rather than for the entire list.
9668 One way to produce a list of null-byte-terminated file names is with @sc{gnu}
9669 @command{find}, using its @option{-print0} predicate.
9670 Do not specify any @var{FILE} on the command line when using this option.
9671
9672 @optHumanReadable
9673
9674 @item -H
9675 @opindex -H
9676 Currently, @option{-H} is the same as @option{--si},
9677 except that @option{-H} evokes a warning.
9678 This option will be changed to be equivalent to
9679 @option{--dereference-args} (@option{-D}).
9680
9681 @item -k
9682 @opindex -k
9683 @cindex kibibytes for file sizes
9684 Print sizes in 1024-byte blocks, overriding the default block size
9685 (@pxref{Block size}).
9686 This option is equivalent to @option{--block-size=1K}.
9687
9688 @item -l
9689 @itemx --count-links
9690 @opindex -l
9691 @opindex --count-links
9692 @cindex hard links, counting in @command{du}
9693 Count the size of all files, even if they have appeared already (as a
9694 hard link).
9695
9696 @item -L
9697 @itemx --dereference
9698 @opindex -L
9699 @opindex --dereference
9700 @cindex symbolic links, dereferencing in @command{du}
9701 Dereference symbolic links (show the disk space used by the file
9702 or directory that the link points to instead of the space used by
9703 the link).
9704
9705 @item -m
9706 @opindex -m
9707 @cindex mebibytes for file sizes
9708 Print sizes in 1,048,576-byte blocks, overriding the default block size
9709 (@pxref{Block size}).
9710 This option is equivalent to @option{--block-size=1M}.
9711
9712 @item -P
9713 @itemx --no-dereference
9714 @opindex -P
9715 @opindex --no-dereference
9716 @cindex symbolic links, dereferencing in @command{du}
9717 For each symbolic links encountered by @command{du},
9718 consider the disk space used by the symbolic link.
9719
9720 @item --max-depth=@var{DEPTH}
9721 @opindex --max-depth=@var{DEPTH}
9722 @cindex limiting output of @command{du}
9723 Show the total for each directory (and file if --all) that is at
9724 most MAX_DEPTH levels down from the root of the hierarchy.  The root
9725 is at level 0, so @code{du --max-depth=0} is equivalent to @code{du -s}.
9726
9727 @item -0
9728 @opindex -0
9729 @itemx --null
9730 @opindex --null
9731 @cindex output null-byte-terminated lines
9732 Output a null byte at the end of each line, rather than a newline.
9733 This option enables other programs to parse the output of @command{du}
9734 even when that output would contain file names with embedded newlines.
9735
9736 @itemx --si
9737 @opindex --si
9738 @cindex SI output
9739 Append an SI-style abbreviation to each size, such as @samp{MB} for
9740 megabytes.  Powers of 1000 are used, not 1024; @samp{MB} stands for
9741 1,000,000 bytes.  Use the @option{-h} or @option{--human-readable} option if
9742 you prefer powers of 1024.
9743
9744 @item -s
9745 @itemx --summarize
9746 @opindex -s
9747 @opindex --summarize
9748 Display only a total for each argument.
9749
9750 @item -S
9751 @itemx --separate-dirs
9752 @opindex -S
9753 @opindex --separate-dirs
9754 Report the size of each directory separately, not including the sizes
9755 of subdirectories.
9756
9757 @itemx --time
9758 @opindex --time
9759 @cindex last modified dates, displaying in @command{du}
9760 Show time of the most recent modification of any file in the directory,
9761 or any of its subdirectories.
9762
9763 @itemx --time=ctime
9764 @itemx --time=status
9765 @itemx --time=use
9766 @opindex --time
9767 @opindex ctime@r{, show the most recent}
9768 @opindex status time@r{, show the most recent}
9769 @opindex use time@r{, show the most recent}
9770 Show the most recent status change time (the @samp{ctime} in the inode) of
9771 any file in the directory, instead of the modification time.
9772
9773 @itemx --time=atime
9774 @itemx --time=access
9775 @opindex --time
9776 @opindex atime@r{, show the most recent}
9777 @opindex access time@r{, show the most recent}
9778 Show the most recent access time (the @samp{atime} in the inode) of
9779 any file in the directory, instead of the modification time.
9780
9781 @item --time-style=@var{style}
9782 @opindex --time-style
9783 @cindex time style
9784 List timestamps in style @var{style}.  This option has an effect only if
9785 the @option{--time} option is also specified.  The @var{style} should
9786 be one of the following:
9787
9788 @table @samp
9789 @item +@var{format}
9790 @vindex LC_TIME
9791 List timestamps using @var{format}, where @var{format} is interpreted
9792 like the format argument of @command{date} (@pxref{date invocation}).
9793 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
9794 @command{du} to list timestamps like @samp{2002-03-30 23:45:56}.  As
9795 with @command{date}, @var{format}'s interpretation is affected by the
9796 @env{LC_TIME} locale category.
9797
9798 @item full-iso
9799 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
9800 format with nanosecond precision, e.g., @samp{2002-03-30
9801 23:45:56.477817180 -0700}.  This style is equivalent to
9802 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
9803
9804 @item long-iso
9805 List @acronym{ISO} 8601 date and time in minutes, e.g.,
9806 @samp{2002-03-30 23:45}.  These timestamps are shorter than
9807 @samp{full-iso} timestamps, and are usually good enough for everyday
9808 work.  This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
9809
9810 @item iso
9811 List @acronym{ISO} 8601 dates for timestamps, e.g., @samp{2002-03-30}.
9812 This style is equivalent to @samp{+%Y-%m-%d}.
9813 @end table
9814
9815 @vindex TIME_STYLE
9816 You can specify the default value of the @option{--time-style} option
9817 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
9818 the default style is @samp{long-iso}.  For compatibility with @command{ls},
9819 if @env{TIME_STYLE} begins with @samp{+} and contains a newline,
9820 the newline and any later characters are ignored; if @env{TIME_STYLE}
9821 begins with @samp{posix-} the @samp{posix-} is ignored; and if
9822 @env{TIME_STYLE} is @samp{locale} it is ignored.
9823
9824 @item -x
9825 @itemx --one-file-system
9826 @opindex -x
9827 @opindex --one-file-system
9828 @cindex one file system, restricting @command{du} to
9829 Skip directories that are on different file systems from the one that
9830 the argument being processed is on.
9831
9832 @item --exclude=@var{PATTERN}
9833 @opindex --exclude=@var{PATTERN}
9834 @cindex excluding files from @command{du}
9835 When recursing, skip subdirectories or files matching @var{PATTERN}.
9836 For example, @code{du --exclude='*.o'} excludes files whose names
9837 end in @samp{.o}.
9838
9839 @item -X @var{FILE}
9840 @itemx --exclude-from=@var{FILE}
9841 @opindex -X @var{FILE}
9842 @opindex --exclude-from=@var{FILE}
9843 @cindex excluding files from @command{du}
9844 Like @option{--exclude}, except take the patterns to exclude from @var{FILE},
9845 one per line.  If @var{FILE} is @samp{-}, take the patterns from standard
9846 input.
9847
9848 @end table
9849
9850 @cindex NFS mounts from BSD to HP-UX
9851 On BSD systems, @command{du} reports sizes that are half the correct
9852 values for files that are NFS-mounted from HP-UX systems.  On HP-UX
9853 systems, it reports sizes that are twice the correct values for
9854 files that are NFS-mounted from BSD systems.  This is due to a flaw
9855 in HP-UX; it also affects the HP-UX @command{du} program.
9856
9857 @exitstatus
9858
9859
9860 @node stat invocation
9861 @section @command{stat}: Report file or file system status
9862
9863 @pindex stat
9864 @cindex file status
9865 @cindex file system status
9866
9867 @command{stat} displays information about the specified file(s).  Synopsis:
9868
9869 @example
9870 stat [@var{option}]@dots{} [@var{file}]@dots{}
9871 @end example
9872
9873 With no option, @command{stat} reports all information about the given files.
9874 But it also can be used to report the information of the file systems the
9875 given files are located on.  If the files are links, @command{stat} can
9876 also give information about the files the links point to.
9877
9878
9879 @table @samp
9880
9881 @item -L
9882 @itemx --dereference
9883 @opindex -L
9884 @opindex --dereference
9885 @cindex symbolic links, dereferencing in @command{stat}
9886 Change how @command{stat} treats symbolic links.
9887 With this option, @command{stat} acts on the file referenced
9888 by each symbolic link argument.
9889 Without it, @command{stat} acts on any symbolic link argument directly.
9890
9891 @item -f
9892 @itemx --file-system
9893 @opindex -f
9894 @opindex --file-system
9895 @cindex file systems
9896 Report information about the file systems where the given files are located
9897 instead of information about the files themselves.
9898
9899 @item -c
9900 @itemx --format=@var{format}
9901 @opindex -c
9902 @opindex --format=@var{format}
9903 @cindex output format
9904 Use @var{format} rather than the default format.
9905 @var{format} is automatically newline-terminated, so
9906 running a command like the following with two or more @var{file}
9907 operands produces a line of output for each operand:
9908 @example
9909 $ stat --format=%d:%i / /usr
9910 2050:2
9911 2057:2
9912 @end example
9913
9914 @itemx --printf=@var{format}
9915 @opindex --printf=@var{format}
9916 @cindex output format
9917 Use @var{format} rather than the default format.
9918 Like @option{--format}, but interpret backslash escapes,
9919 and do not output a mandatory trailing newline.
9920 If you want a newline, include @samp{\n} in the @var{format}.
9921 Here's how you would use @option{--printf} to print the device
9922 and inode numbers of @file{/} and @file{/usr}:
9923 @example
9924 $ stat --printf='%d:%i\n' / /usr
9925 2050:2
9926 2057:2
9927 @end example
9928
9929 @item -t
9930 @itemx --terse
9931 @opindex -t
9932 @opindex --terse
9933 @cindex terse output
9934 Print the information in terse form, suitable for parsing by other programs.
9935
9936 The valid format sequences for files are:
9937
9938 @itemize @bullet
9939 @item %a - Access rights in octal
9940 @item %A - Access rights in human readable form
9941 @item %b - Number of blocks allocated (see @samp{%B})
9942 @item %B - The size in bytes of each block reported by @samp{%b}
9943 @item %d - Device number in decimal
9944 @item %D - Device number in hex
9945 @item %f - Raw mode in hex
9946 @item %F - File type
9947 @item %g - Group ID of owner
9948 @item %G - Group name of owner
9949 @item %h - Number of hard links
9950 @item %i - Inode number
9951 @item %n - File name
9952 @item %N - Quoted file name with dereference if symbolic link
9953 @item %o - I/O block size
9954 @item %s - Total size, in bytes
9955 @item %t - Major device type in hex
9956 @item %T - Minor device type in hex
9957 @item %u - User ID of owner
9958 @item %U - User name of owner
9959 @item %x - Time of last access
9960 @item %X - Time of last access as seconds since Epoch
9961 @item %y - Time of last modification
9962 @item %Y - Time of last modification as seconds since Epoch
9963 @item %z - Time of last change
9964 @item %Z - Time of last change as seconds since Epoch
9965 @end itemize
9966
9967 The valid format sequences for file systems are:
9968
9969 @itemize @bullet
9970 @item %a - Free blocks available to non-super-user
9971 @item %b - Total data blocks in file system
9972 @item %c - Total file nodes in file system
9973 @item %d - Free file nodes in file system
9974 @item %f - Free blocks in file system
9975 @item %i - File System ID in hex
9976 @item %l - Maximum length of file names
9977 @item %n - File name
9978 @item %s - Block size (for faster transfers)
9979 @item %S - Fundamental block size (for block counts)
9980 @item %t - Type in hex
9981 @item %T - Type in human readable form
9982 @end itemize
9983
9984 @vindex TZ
9985 Time stamps are listed according to the time zone rules specified by
9986 the @env{TZ} environment variable, or by the system default rules if
9987 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
9988 with @env{TZ}, libc, The GNU C Library}.
9989 @end table
9990
9991 @exitstatus
9992
9993
9994 @node sync invocation
9995 @section @command{sync}: Synchronize data on disk with memory
9996
9997 @pindex sync
9998 @cindex synchronize disk and memory
9999
10000 @cindex superblock, writing
10001 @cindex inodes, written buffered
10002 @command{sync} writes any data buffered in memory out to disk.  This can
10003 include (but is not limited to) modified superblocks, modified inodes,
10004 and delayed reads and writes.  This must be implemented by the kernel;
10005 The @command{sync} program does nothing but exercise the @code{sync} system
10006 call.
10007
10008 @cindex crashes and corruption
10009 The kernel keeps data in memory to avoid doing (relatively slow) disk
10010 reads and writes.  This improves performance, but if the computer
10011 crashes, data may be lost or the file system corrupted as a
10012 result.  The @command{sync} command ensures everything in memory
10013 is written to disk.
10014
10015 Any arguments are ignored, except for a lone @option{--help} or
10016 @option{--version} (@pxref{Common options}).
10017
10018 @exitstatus
10019
10020
10021 @node Printing text
10022 @chapter Printing text
10023
10024 @cindex printing text, commands for
10025 @cindex commands for printing text
10026
10027 This section describes commands that display text strings.
10028
10029 @menu
10030 * echo invocation::             Print a line of text.
10031 * printf invocation::           Format and print data.
10032 * yes invocation::              Print a string until interrupted.
10033 @end menu
10034
10035
10036 @node echo invocation
10037 @section @command{echo}: Print a line of text
10038
10039 @pindex echo
10040 @cindex displaying text
10041 @cindex printing text
10042 @cindex text, displaying
10043 @cindex arbitrary text, displaying
10044
10045 @command{echo} writes each given @var{string} to standard output, with a
10046 space between each and a newline after the last one.  Synopsis:
10047
10048 @example
10049 echo [@var{option}]@dots{} [@var{string}]@dots{}
10050 @end example
10051
10052 The program accepts the following options.  Also see @ref{Common options}.
10053 Options must precede operands, and the normally-special argument
10054 @samp{--} has no special meaning and is treated like any other
10055 @var{string}.
10056
10057 @table @samp
10058 @item -n
10059 @opindex -n
10060 Do not output the trailing newline.
10061
10062 @item -e
10063 @opindex -e
10064 @cindex backslash escapes
10065 Enable interpretation of the following backslash-escaped characters in
10066 each @var{string}:
10067
10068 @table @samp
10069 @item \a
10070 alert (bell)
10071 @item \b
10072 backspace
10073 @item \c
10074 suppress trailing newline
10075 @item \f
10076 form feed
10077 @item \n
10078 new line
10079 @item \r
10080 carriage return
10081 @item \t
10082 horizontal tab
10083 @item \v
10084 vertical tab
10085 @item \\
10086 backslash
10087 @item \0@var{nnn}
10088 the eight-bit value that is the octal number @var{nnn}
10089 (zero to three octal digits)
10090 @item \@var{nnn}
10091 the eight-bit value that is the octal number @var{nnn}
10092 (one to three octal digits)
10093 @item \x@var{hh}
10094 the eight-bit value that is the hexadecimal number @var{hh}
10095 (one or two hexadecimal digits)
10096 @end table
10097
10098 @item -E
10099 @opindex -E
10100 @cindex backslash escapes
10101 Disable interpretation of backslash escapes in each @var{string}.
10102 This is the default.  If @option{-e} and @option{-E} are both
10103 specified, the last one given takes effect.
10104
10105 @end table
10106
10107 @vindex POSIXLY_CORRECT
10108 If the @env{POSIXLY_CORRECT} environment variable is set, then when
10109 @command{echo}'s first argument is not @option{-n} it outputs
10110 option-like arguments instead of treating them as options.  For
10111 example, @code{echo -ne hello} outputs @samp{-ne hello} instead of
10112 plain @samp{hello}.
10113
10114 @acronym{POSIX} does not require support for any options, and says
10115 that the behavior of @command{echo} is implementation-defined if any
10116 @var{string} contains a backslash or if the first argument is
10117 @option{-n}.  Portable programs can use the @command{printf} command
10118 if they need to omit trailing newlines or output control characters or
10119 backslashes.  @xref{printf invocation}.
10120
10121 @exitstatus
10122
10123
10124 @node printf invocation
10125 @section @command{printf}: Format and print data
10126
10127 @pindex printf
10128 @command{printf} does formatted printing of text.  Synopsis:
10129
10130 @example
10131 printf @var{format} [@var{argument}]@dots{}
10132 @end example
10133
10134 @command{printf} prints the @var{format} string, interpreting @samp{%}
10135 directives and @samp{\} escapes to format numeric and string arguments
10136 in a way that is mostly similar to the C @samp{printf} function.  The
10137 differences are as follows:
10138
10139 @itemize @bullet
10140
10141 @item
10142 The @var{format} argument is reused as necessary to convert all the
10143 given @var{argument}s.  For example, the command @samp{printf %s a b}
10144 outputs @samp{ab}.
10145
10146 @item
10147 Missing @var{argument}s are treated as null strings or as zeros,
10148 depending on whether the context expects a string or a number.  For
10149 example, the command @samp{printf %sx%d} prints @samp{x0}.
10150
10151 @item
10152 @kindex \c
10153 An additional escape, @samp{\c}, causes @command{printf} to produce no
10154 further output.  For example, the command @samp{printf 'A%sC\cD%sF' B
10155 E} prints @samp{ABC}.
10156
10157 @item
10158 The hexadecimal escape sequence @samp{\x@var{hh}} has at most two
10159 digits, as opposed to C where it can have an unlimited number of
10160 digits.  For example, the command @samp{printf '\x07e'} prints two
10161 bytes, whereas the C statement @samp{printf ("\x07e")} prints just
10162 one.
10163
10164 @item
10165 @kindex %b
10166 @command{printf} has an additional directive, @samp{%b}, which prints its
10167 argument string with @samp{\} escapes interpreted in the same way as in
10168 the @var{format} string, except that octal escapes are of the form
10169 @samp{\0@var{ooo}} where @var{ooo} is 0 to 3 octal digits.
10170 If a precision is also given, it limits the number of bytes printed
10171 from the converted string.
10172
10173 @item
10174 Numeric arguments must be single C constants, possibly with leading
10175 @samp{+} or @samp{-}.  For example, @samp{printf %.4d -3} outputs
10176 @samp{-0003}.
10177
10178 @item
10179 @vindex POSIXLY_CORRECT
10180 If the leading character of a numeric argument is @samp{"} or @samp{'}
10181 then its value is the numeric value of the immediately following
10182 character.  Any remaining characters are silently ignored if the
10183 @env{POSIXLY_CORRECT} environment variable is set; otherwise, a
10184 warning is printed.  For example, @samp{printf "%d" "'a"} outputs
10185 @samp{97} on hosts that use the @acronym{ASCII} character set, since
10186 @samp{a} has the numeric value 97 in @acronym{ASCII}.
10187
10188 @end itemize
10189
10190 @vindex LC_NUMERIC
10191 A floating-point argument must use a period before any fractional
10192 digits, but is printed according to the @env{LC_NUMERIC} category of the
10193 current locale.  For example, in a locale whose radix character is a
10194 comma, the command @samp{printf %g 3.14} outputs @samp{3,14} whereas
10195 the command @samp{printf %g 3,14} is an error.
10196
10197 @kindex \@var{ooo}
10198 @kindex \x@var{hh}
10199 @command{printf} interprets @samp{\@var{ooo}} in @var{format} as an octal number
10200 (if @var{ooo} is 1 to 3 octal digits) specifying a character to print,
10201 and @samp{\x@var{hh}} as a hexadecimal number (if @var{hh} is 1 to 2 hex
10202 digits) specifying a character to print.
10203
10204 @kindex \uhhhh
10205 @kindex \Uhhhhhhhh
10206 @cindex Unicode
10207 @cindex ISO/IEC 10646
10208 @vindex LC_CTYPE
10209 @command{printf} interprets two character syntaxes introduced in
10210 @acronym{ISO} C 99:
10211 @samp{\u} for 16-bit Unicode (@acronym{ISO}/@acronym{IEC} 10646)
10212 characters, specified as
10213 four hexadecimal digits @var{hhhh}, and @samp{\U} for 32-bit Unicode
10214 characters, specified as eight hexadecimal digits @var{hhhhhhhh}.
10215 @command{printf} outputs the Unicode characters
10216 according to the @env{LC_CTYPE} locale.
10217
10218 The processing of @samp{\u} and @samp{\U} requires a full-featured
10219 @code{iconv} facility.  It is activated on systems with glibc 2.2 (or newer),
10220 or when @code{libiconv} is installed prior to this package.  Otherwise
10221 @samp{\u} and @samp{\U} will print as-is.
10222
10223 The only options are a lone @option{--help} or
10224 @option{--version}.  @xref{Common options}.
10225 Options must precede operands.
10226
10227 The Unicode character syntaxes are useful for writing strings in a locale
10228 independent way.  For example, a string containing the Euro currency symbol
10229
10230 @example
10231 $ /usr/local/bin/printf '\u20AC 14.95'
10232 @end example
10233
10234 @noindent
10235 will be output correctly in all locales supporting the Euro symbol
10236 (@acronym{ISO}-8859-15, UTF-8, and others).  Similarly, a Chinese string
10237
10238 @example
10239 $ /usr/local/bin/printf '\u4e2d\u6587'
10240 @end example
10241
10242 @noindent
10243 will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).
10244
10245 Note that in these examples, the full name of @command{printf} has been
10246 given, to distinguish it from the GNU @code{bash} built-in function
10247 @command{printf}.
10248
10249 For larger strings, you don't need to look up the hexadecimal code
10250 values of each character one by one.  @acronym{ASCII} characters mixed with \u
10251 escape sequences is also known as the JAVA source file encoding.  You can
10252 use GNU recode 3.5c (or newer) to convert strings to this encoding.  Here
10253 is how to convert a piece of text into a shell script which will output
10254 this text in a locale-independent way:
10255
10256 @smallexample
10257 $ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
10258     '\u4e2d\u6587\n' > sample.txt
10259 $ recode BIG5..JAVA < sample.txt \
10260     | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
10261     > sample.sh
10262 @end smallexample
10263
10264 @exitstatus
10265
10266
10267 @node yes invocation
10268 @section @command{yes}: Print a string until interrupted
10269
10270 @pindex yes
10271 @cindex repeated output of a string
10272
10273 @command{yes} prints the command line arguments, separated by spaces and
10274 followed by a newline, forever until it is killed.  If no arguments are
10275 given, it prints @samp{y} followed by a newline forever until killed.
10276
10277 Upon a write error, @command{yes} exits with status @samp{1}.
10278
10279 The only options are a lone @option{--help} or @option{--version}.
10280 To output an argument that begins with
10281 @samp{-}, precede it with @option{--}, e.g., @samp{yes -- --help}.
10282 @xref{Common options}.
10283
10284
10285 @node Conditions
10286 @chapter Conditions
10287
10288 @cindex conditions
10289 @cindex commands for exit status
10290 @cindex exit status commands
10291
10292 This section describes commands that are primarily useful for their exit
10293 status, rather than their output.  Thus, they are often used as the
10294 condition of shell @code{if} statements, or as the last command in a
10295 pipeline.
10296
10297 @menu
10298 * false invocation::            Do nothing, unsuccessfully.
10299 * true invocation::             Do nothing, successfully.
10300 * test invocation::             Check file types and compare values.
10301 * expr invocation::             Evaluate expressions.
10302 @end menu
10303
10304
10305 @node false invocation
10306 @section @command{false}: Do nothing, unsuccessfully
10307
10308 @pindex false
10309 @cindex do nothing, unsuccessfully
10310 @cindex failure exit status
10311 @cindex exit status of @command{false}
10312
10313 @command{false} does nothing except return an exit status of 1, meaning
10314 @dfn{failure}.  It can be used as a place holder in shell scripts
10315 where an unsuccessful command is needed.
10316 In most modern shells, @command{false} is a built-in command, so when
10317 you use @samp{false} in a script, you're probably using the built-in
10318 command, not the one documented here.
10319
10320 @command{false} honors the @option{--help} and @option{--version} options.
10321
10322 This version of @command{false} is implemented as a C program, and is thus
10323 more secure and faster than a shell script implementation, and may safely
10324 be used as a dummy shell for the purpose of disabling accounts.
10325
10326 Note that @command{false} (unlike all other programs documented herein)
10327 exits unsuccessfully, even when invoked with
10328 @option{--help} or @option{--version}.
10329
10330 Portable programs should not assume that the exit status of
10331 @command{false} is 1, as it is greater than 1 on some
10332 non-@acronym{GNU} hosts.
10333
10334
10335 @node true invocation
10336 @section @command{true}: Do nothing, successfully
10337
10338 @pindex true
10339 @cindex do nothing, successfully
10340 @cindex no-op
10341 @cindex successful exit
10342 @cindex exit status of @command{true}
10343
10344 @command{true} does nothing except return an exit status of 0, meaning
10345 @dfn{success}.  It can be used as a place holder in shell scripts
10346 where a successful command is needed, although the shell built-in
10347 command @code{:} (colon) may do the same thing faster.
10348 In most modern shells, @command{true} is a built-in command, so when
10349 you use @samp{true} in a script, you're probably using the built-in
10350 command, not the one documented here.
10351
10352 @command{true} honors the @option{--help} and @option{--version} options.
10353
10354 Note, however, that it is possible to cause @command{true}
10355 to exit with nonzero status: with the @option{--help} or @option{--version}
10356 option, and with standard
10357 output already closed or redirected to a file that evokes an I/O error.
10358 For example, using a Bourne-compatible shell:
10359
10360 @example
10361 $ ./true --version >&-
10362 ./true: write error: Bad file number
10363 $ ./true --version > /dev/full
10364 ./true: write error: No space left on device
10365 @end example
10366
10367 This version of @command{true} is implemented as a C program, and is thus
10368 more secure and faster than a shell script implementation, and may safely
10369 be used as a dummy shell for the purpose of disabling accounts.
10370
10371 @node test invocation
10372 @section @command{test}: Check file types and compare values
10373
10374 @pindex test
10375 @cindex check file types
10376 @cindex compare values
10377 @cindex expression evaluation
10378
10379 @command{test} returns a status of 0 (true) or 1 (false) depending on the
10380 evaluation of the conditional expression @var{expr}.  Each part of the
10381 expression must be a separate argument.
10382
10383 @command{test} has file status checks, string operators, and numeric
10384 comparison operators.
10385
10386 @command{test} has an alternate form that uses opening and closing
10387 square brackets instead a leading @samp{test}.  For example, instead
10388 of @samp{test -d /}, you can write @samp{[ -d / ]}.  The square
10389 brackets must be separate arguments; for example, @samp{[-d /]} does
10390 not have the desired effect.  Since @samp{test @var{expr}} and @samp{[
10391 @var{expr} ]} have the same meaning, only the former form is discussed
10392 below.
10393
10394 Synopses:
10395
10396 @example
10397 test @var{expression}
10398 test
10399 [ @var{expression} ]
10400 [ ]
10401 [ @var{option}
10402 @end example
10403
10404 @cindex conflicts with shell built-ins
10405 @cindex built-in shell commands, conflicts with
10406 Because most shells have a built-in @command{test} command, using an
10407 unadorned @command{test} in a script or interactively may get you
10408 different functionality than that described here.
10409
10410 If @var{expression} is omitted, @command{test} returns false.
10411 If @var{expression} is a single argument,
10412 @command{test} returns false if the argument is null and true otherwise.  The argument
10413 can be any string, including strings like @samp{-d}, @samp{-1},
10414 @samp{--}, @samp{--help}, and @samp{--version} that most other
10415 programs would treat as options.  To get help and version information,
10416 invoke the commands @samp{[ --help} and @samp{[ --version}, without
10417 the usual closing brackets.  @xref{Common options}.
10418
10419 @cindex exit status of @command{test}
10420 Exit status:
10421
10422 @display
10423 0 if the expression is true,
10424 1 if the expression is false,
10425 2 if an error occurred.
10426 @end display
10427
10428 @menu
10429 * File type tests::             -[bcdfhLpSt]
10430 * Access permission tests::     -[gkruwxOG]
10431 * File characteristic tests::   -e -s -nt -ot -ef
10432 * String tests::                -z -n = !=
10433 * Numeric tests::               -eq -ne -lt -le -gt -ge
10434 * Connectives for test::        ! -a -o
10435 @end menu
10436
10437
10438 @node File type tests
10439 @subsection File type tests
10440
10441 @cindex file type tests
10442
10443 These options test for particular types of files.  (Everything's a file,
10444 but not all files are the same!)
10445
10446 @table @samp
10447
10448 @item -b @var{file}
10449 @opindex -b
10450 @cindex block special check
10451 True if @var{file} exists and is a block special device.
10452
10453 @item -c @var{file}
10454 @opindex -c
10455 @cindex character special check
10456 True if @var{file} exists and is a character special device.
10457
10458 @item -d @var{file}
10459 @opindex -d
10460 @cindex directory check
10461 True if @var{file} exists and is a directory.
10462
10463 @item -f @var{file}
10464 @opindex -f
10465 @cindex regular file check
10466 True if @var{file} exists and is a regular file.
10467
10468 @item -h @var{file}
10469 @itemx -L @var{file}
10470 @opindex -L
10471 @opindex -h
10472 @cindex symbolic link check
10473 True if @var{file} exists and is a symbolic link.
10474 Unlike all other file-related tests, this test does not dereference
10475 @var{file} if it is a symbolic link.
10476
10477 @item -p @var{file}
10478 @opindex -p
10479 @cindex named pipe check
10480 True if @var{file} exists and is a named pipe.
10481
10482 @item -S @var{file}
10483 @opindex -S
10484 @cindex socket check
10485 True if @var{file} exists and is a socket.
10486
10487 @item -t @var{fd}
10488 @opindex -t
10489 @cindex terminal check
10490 True if @var{fd} is a file descriptor that is associated with a
10491 terminal.
10492
10493 @end table
10494
10495
10496 @node Access permission tests
10497 @subsection Access permission tests
10498
10499 @cindex access permission tests
10500 @cindex permission tests
10501
10502 These options test for particular access permissions.
10503
10504 @table @samp
10505
10506 @item -g @var{file}
10507 @opindex -g
10508 @cindex set-group-ID check
10509 True if @var{file} exists and has its set-group-ID bit set.
10510
10511 @item -k @var{file}
10512 @opindex -k
10513 @cindex sticky bit check
10514 True if @var{file} exists and has its @dfn{sticky} bit set.
10515
10516 @item -r @var{file}
10517 @opindex -r
10518 @cindex readable file check
10519 True if @var{file} exists and read permission is granted.
10520
10521 @item -u @var{file}
10522 @opindex -u
10523 @cindex set-user-ID check
10524 True if @var{file} exists and has its set-user-ID bit set.
10525
10526 @item -w @var{file}
10527 @opindex -w
10528 @cindex writable file check
10529 True if @var{file} exists and write permission is granted.
10530
10531 @item -x @var{file}
10532 @opindex -x
10533 @cindex executable file check
10534 True if @var{file} exists and execute permission is granted
10535 (or search permission, if it is a directory).
10536
10537 @item -O @var{file}
10538 @opindex -O
10539 @cindex owned by effective user ID check
10540 True if @var{file} exists and is owned by the current effective user ID.
10541
10542 @item -G @var{file}
10543 @opindex -G
10544 @cindex owned by effective group ID check
10545 True if @var{file} exists and is owned by the current effective group ID.
10546
10547 @end table
10548
10549 @node File characteristic tests
10550 @subsection File characteristic tests
10551
10552 @cindex file characteristic tests
10553
10554 These options test other file characteristics.
10555
10556 @table @samp
10557
10558 @item -e @var{file}
10559 @opindex -e
10560 @cindex existence-of-file check
10561 True if @var{file} exists.
10562
10563 @item -s @var{file}
10564 @opindex -s
10565 @cindex nonempty file check
10566 True if @var{file} exists and has a size greater than zero.
10567
10568 @item @var{file1} -nt @var{file2}
10569 @opindex -nt
10570 @cindex newer-than file check
10571 True if @var{file1} is newer (according to modification date) than
10572 @var{file2}, or if @var{file1} exists and @var{file2} does not.
10573
10574 @item @var{file1} -ot @var{file2}
10575 @opindex -ot
10576 @cindex older-than file check
10577 True if @var{file1} is older (according to modification date) than
10578 @var{file2}, or if @var{file2} exists and @var{file1} does not.
10579
10580 @item @var{file1} -ef @var{file2}
10581 @opindex -ef
10582 @cindex same file check
10583 @cindex hard link check
10584 True if @var{file1} and @var{file2} have the same device and inode
10585 numbers, i.e., if they are hard links to each other.
10586
10587 @end table
10588
10589
10590 @node String tests
10591 @subsection String tests
10592
10593 @cindex string tests
10594
10595 These options test string characteristics.  You may need to quote
10596 @var{string} arguments for the shell.  For example:
10597
10598 @example
10599 test -n "$V"
10600 @end example
10601
10602 The quotes here prevent the wrong arguments from being passed to
10603 @command{test} if @samp{$V} is empty or contains special characters.
10604
10605 @table @samp
10606
10607 @item -z @var{string}
10608 @opindex -z
10609 @cindex zero-length string check
10610 True if the length of @var{string} is zero.
10611
10612 @item -n @var{string}
10613 @itemx @var{string}
10614 @opindex -n
10615 @cindex nonzero-length string check
10616 True if the length of @var{string} is nonzero.
10617
10618 @item @var{string1} = @var{string2}
10619 @opindex =
10620 @cindex equal string check
10621 True if the strings are equal.
10622
10623 @item @var{string1} != @var{string2}
10624 @opindex !=
10625 @cindex not-equal string check
10626 True if the strings are not equal.
10627
10628 @end table
10629
10630
10631 @node Numeric tests
10632 @subsection Numeric tests
10633
10634 @cindex numeric tests
10635 @cindex arithmetic tests
10636
10637 Numeric relationals.  The arguments must be entirely numeric (possibly
10638 negative), or the special expression @w{@code{-l @var{string}}}, which
10639 evaluates to the length of @var{string}.
10640
10641 @table @samp
10642
10643 @item @var{arg1} -eq @var{arg2}
10644 @itemx @var{arg1} -ne @var{arg2}
10645 @itemx @var{arg1} -lt @var{arg2}
10646 @itemx @var{arg1} -le @var{arg2}
10647 @itemx @var{arg1} -gt @var{arg2}
10648 @itemx @var{arg1} -ge @var{arg2}
10649 @opindex -eq
10650 @opindex -ne
10651 @opindex -lt
10652 @opindex -le
10653 @opindex -gt
10654 @opindex -ge
10655 These arithmetic binary operators return true if @var{arg1} is equal,
10656 not-equal, less-than, less-than-or-equal, greater-than, or
10657 greater-than-or-equal than @var{arg2}, respectively.
10658
10659 @end table
10660
10661 For example:
10662
10663 @example
10664 test -1 -gt -2 && echo yes
10665 @result{} yes
10666 test -l abc -gt 1 && echo yes
10667 @result{} yes
10668 test 0x100 -eq 1
10669 @error{} test: integer expression expected before -eq
10670 @end example
10671
10672
10673 @node Connectives for test
10674 @subsection Connectives for @command{test}
10675
10676 @cindex logical connectives
10677 @cindex connectives, logical
10678
10679 The usual logical connectives.
10680
10681 @table @samp
10682
10683 @item ! @var{expr}
10684 @opindex !
10685 True if @var{expr} is false.
10686
10687 @item @var{expr1} -a @var{expr2}
10688 @opindex -a
10689 @cindex logical and operator
10690 @cindex and operator
10691 True if both @var{expr1} and @var{expr2} are true.
10692
10693 @item @var{expr1} -o @var{expr2}
10694 @opindex -o
10695 @cindex logical or operator
10696 @cindex or operator
10697 True if either @var{expr1} or @var{expr2} is true.
10698
10699 @end table
10700
10701
10702 @node expr invocation
10703 @section @command{expr}: Evaluate expressions
10704
10705 @pindex expr
10706 @cindex expression evaluation
10707 @cindex evaluation of expressions
10708
10709 @command{expr} evaluates an expression and writes the result on standard
10710 output.  Each token of the expression must be a separate argument.
10711
10712 Operands are either integers or strings.  Integers consist of one or
10713 more decimal digits, with an optional leading @samp{-}.
10714 @command{expr} converts
10715 anything appearing in an operand position to an integer or a string
10716 depending on the operation being applied to it.
10717
10718 Strings are not quoted for @command{expr} itself, though you may need to
10719 quote them to protect characters with special meaning to the shell,
10720 e.g., spaces.  However, regardless of whether it is quoted, a string
10721 operand should not be a parenthesis or any of @command{expr}'s
10722 operators like @code{+}, so you cannot safely pass an arbitrary string
10723 @code{$str} to expr merely by quoting it to the shell.  One way to
10724 work around this is to use the @sc{gnu} extension @code{+},
10725 (e.g., @code{+ "$str" = foo}); a more portable way is to use
10726 @code{@w{" $str"}} and to adjust the rest of the expression to take
10727 the leading space into account (e.g., @code{@w{" $str" = " foo"}}).
10728
10729 You should not pass a negative integer or a string with leading
10730 @samp{-} as @command{expr}'s first argument, as it might be
10731 misinterpreted as an option; this can be avoided by parenthesization.
10732 Also, portable scripts should not use a string operand that happens to
10733 take the form of an integer; this can be worked around by inserting
10734 leading spaces as mentioned above.
10735
10736 @cindex parentheses for grouping
10737 Operators may be given as infix symbols or prefix keywords.  Parentheses
10738 may be used for grouping in the usual manner.  You must quote
10739 parentheses and many operators to avoid the shell evaluating them,
10740 however.
10741
10742 The only options are @option{--help} and @option{--version}.  @xref{Common
10743 options}.  Options must precede operands.
10744
10745 @cindex exit status of @command{expr}
10746 Exit status:
10747
10748 @display
10749 0 if the expression is neither null nor 0,
10750 1 if the expression is null or 0,
10751 2 if the expression is invalid,
10752 3 if an internal error occurred (e.g., arithmetic overflow).
10753 @end display
10754
10755 @menu
10756 * String expressions::          + : match substr index length
10757 * Numeric expressions::         + - * / %
10758 * Relations for expr::          | & < <= = == != >= >
10759 * Examples of expr::            Examples.
10760 @end menu
10761
10762
10763 @node String expressions
10764 @subsection String expressions
10765
10766 @cindex string expressions
10767 @cindex expressions, string
10768
10769 @command{expr} supports pattern matching and other string operators.  These
10770 have lower precedence than both the numeric and relational operators (in
10771 the next sections).
10772
10773 @table @samp
10774
10775 @item @var{string} : @var{regex}
10776 @cindex pattern matching
10777 @cindex regular expression matching
10778 @cindex matching patterns
10779 Perform pattern matching.  The arguments are converted to strings and the
10780 second is considered to be a (basic, a la GNU @code{grep}) regular
10781 expression, with a @code{^} implicitly prepended.  The first argument is
10782 then matched against this regular expression.
10783
10784 If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
10785 @code{:} expression returns the part of @var{string} that matched the
10786 subexpression; otherwise, it returns the number of characters matched.
10787
10788 If the match fails, the @code{:} operator returns the null string if
10789 @samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
10790
10791 @kindex \( @r{regexp operator}
10792 Only the first @samp{\( @dots{} \)} pair is relevant to the return
10793 value; additional pairs are meaningful only for grouping the regular
10794 expression operators.
10795
10796 @kindex \+ @r{regexp operator}
10797 @kindex \? @r{regexp operator}
10798 @kindex \| @r{regexp operator}
10799 In the regular expression, @code{\+}, @code{\?}, and @code{\|} are
10800 operators which respectively match one or more, zero or one, or separate
10801 alternatives.  SunOS and other @command{expr}'s treat these as regular
10802 characters.  (@acronym{POSIX} allows either behavior.)
10803 @xref{Top, , Regular Expression Library, regex, Regex}, for details of
10804 regular expression syntax.  Some examples are in @ref{Examples of expr}.
10805
10806 @item match @var{string} @var{regex}
10807 @findex match
10808 An alternative way to do pattern matching.  This is the same as
10809 @w{@samp{@var{string} : @var{regex}}}.
10810
10811 @item substr @var{string} @var{position} @var{length}
10812 @findex substr
10813 Returns the substring of @var{string} beginning at @var{position}
10814 with length at most @var{length}.  If either @var{position} or
10815 @var{length} is negative, zero, or non-numeric, returns the null string.
10816
10817 @item index @var{string} @var{charset}
10818 @findex index
10819 Returns the first position in @var{string} where the first character in
10820 @var{charset} was found.  If no character in @var{charset} is found in
10821 @var{string}, return 0.
10822
10823 @item length @var{string}
10824 @findex length
10825 Returns the length of @var{string}.
10826
10827 @item + @var{token}
10828 @kindex +
10829 Interpret @var{token} as a string, even if it is a keyword like @var{match}
10830 or an operator like @code{/}.
10831 This makes it possible to test @code{expr length + "$x"} or
10832 @code{expr + "$x" : '.*/\(.\)'} and have it do the right thing even if
10833 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
10834 This operator is a @acronym{GNU} extension.  Portable shell scripts should use
10835 @code{@w{" $token"} : @w{' \(.*\)'}} instead of @code{+ "$token"}.
10836
10837 @end table
10838
10839 To make @command{expr} interpret keywords as strings, you must use the
10840 @code{quote} operator.
10841
10842
10843 @node Numeric expressions
10844 @subsection Numeric expressions
10845
10846 @cindex numeric expressions
10847 @cindex expressions, numeric
10848
10849 @command{expr} supports the usual numeric operators, in order of increasing
10850 precedence.  The string operators (previous section) have lower precedence,
10851 the connectives (next section) have higher.
10852
10853 @table @samp
10854
10855 @item + -
10856 @kindex +
10857 @kindex -
10858 @cindex addition
10859 @cindex subtraction
10860 Addition and subtraction.  Both arguments are converted to integers;
10861 an error occurs if this cannot be done.
10862
10863 @item * / %
10864 @kindex *
10865 @kindex /
10866 @kindex %
10867 @cindex multiplication
10868 @cindex division
10869 @cindex remainder
10870 Multiplication, division, remainder.  Both arguments are converted to
10871 integers; an error occurs if this cannot be done.
10872
10873 @end table
10874
10875
10876 @node Relations for expr
10877 @subsection Relations for @command{expr}
10878
10879 @cindex connectives, logical
10880 @cindex logical connectives
10881 @cindex relations, numeric or string
10882
10883 @command{expr} supports the usual logical connectives and relations.  These
10884 are higher precedence than either the string or numeric operators
10885 (previous sections).  Here is the list, lowest-precedence operator first.
10886
10887 @table @samp
10888
10889 @item |
10890 @kindex |
10891 @cindex logical or operator
10892 @cindex or operator
10893 Returns its first argument if that is neither null nor zero, otherwise
10894 its second argument if it is neither null nor zero, otherwise 0.  It
10895 does not evaluate its second argument if its first argument is neither
10896 null nor zero.
10897
10898 @item &
10899 @kindex &
10900 @cindex logical and operator
10901 @cindex and operator
10902 Return its first argument if neither argument is null or zero, otherwise
10903 0.  It does not evaluate its second argument if its first argument is
10904 null or zero.
10905
10906 @item < <= = == != >= >
10907 @kindex <
10908 @kindex <=
10909 @kindex =
10910 @kindex ==
10911 @kindex >
10912 @kindex >=
10913 @cindex comparison operators
10914 @vindex LC_COLLATE
10915 Compare the arguments and return 1 if the relation is true, 0 otherwise.
10916 @code{==} is a synonym for @code{=}.  @command{expr} first tries to convert
10917 both arguments to integers and do a numeric comparison; if either
10918 conversion fails, it does a lexicographic comparison using the character
10919 collating sequence specified by the @env{LC_COLLATE} locale.
10920
10921 @end table
10922
10923
10924 @node Examples of expr
10925 @subsection Examples of using @command{expr}
10926
10927 @cindex examples of @command{expr}
10928 Here are a few examples, including quoting for shell metacharacters.
10929
10930 To add 1 to the shell variable @code{foo}, in Bourne-compatible shells:
10931
10932 @example
10933 foo=`expr $foo + 1`
10934 @end example
10935
10936 To print the non-directory part of the file name stored in
10937 @code{$fname}, which need not contain a @code{/}:
10938
10939 @example
10940 expr $fname : '.*/\(.*\)' '|' $fname
10941 @end example
10942
10943 An example showing that @code{\+} is an operator:
10944
10945 @example
10946 expr aaa : 'a\+'
10947 @result{} 3
10948 @end example
10949
10950 @example
10951 expr abc : 'a\(.\)c'
10952 @result{} b
10953 expr index abcdef cz
10954 @result{} 3
10955 expr index index a
10956 @error{} expr: syntax error
10957 expr index quote index a
10958 @result{} 0
10959 @end example
10960
10961
10962 @node Redirection
10963 @chapter Redirection
10964
10965 @cindex redirection
10966 @cindex commands for redirection
10967
10968 Unix shells commonly provide several forms of @dfn{redirection}---ways
10969 to change the input source or output destination of a command.  But one
10970 useful redirection is performed by a separate command, not by the shell;
10971 it's described here.
10972
10973 @menu
10974 * tee invocation::              Redirect output to multiple files.
10975 @end menu
10976
10977
10978 @node tee invocation
10979 @section @command{tee}: Redirect output to multiple files
10980
10981 @pindex tee
10982 @cindex pipe fitting
10983 @cindex destinations, multiple output
10984 @cindex read from stdin and write to stdout and files
10985
10986 The @command{tee} command copies standard input to standard output and also
10987 to any files given as arguments.  This is useful when you want not only
10988 to send some data down a pipe, but also to save a copy.  Synopsis:
10989
10990 @example
10991 tee [@var{option}]@dots{} [@var{file}]@dots{}
10992 @end example
10993
10994 If a file being written to does not already exist, it is created.  If a
10995 file being written to already exists, the data it previously contained
10996 is overwritten unless the @option{-a} option is used.
10997
10998 A @var{file} of @samp{-} causes @command{tee} to send another copy of
10999 input to standard output, but this is typically not that useful as the
11000 copies are interleaved.
11001
11002 The program accepts the following options.  Also see @ref{Common options}.
11003
11004 @table @samp
11005 @item -a
11006 @itemx --append
11007 @opindex -a
11008 @opindex --append
11009 Append standard input to the given files rather than overwriting
11010 them.
11011
11012 @item -i
11013 @itemx --ignore-interrupts
11014 @opindex -i
11015 @opindex --ignore-interrupts
11016 Ignore interrupt signals.
11017
11018 @end table
11019
11020 @exitstatus
11021
11022
11023 @node File name manipulation
11024 @chapter File name manipulation
11025
11026 @cindex file name manipulation
11027 @cindex manipulation of file names
11028 @cindex commands for file name manipulation
11029
11030 This section describes commands that manipulate file names.
11031
11032 @menu
11033 * basename invocation::         Strip directory and suffix from a file name.
11034 * dirname invocation::          Strip non-directory suffix from a file name.
11035 * pathchk invocation::          Check file name portability.
11036 @end menu
11037
11038
11039 @node basename invocation
11040 @section @command{basename}: Strip directory and suffix from a file name
11041
11042 @pindex basename
11043 @cindex strip directory and suffix from file names
11044 @cindex directory, stripping from file names
11045 @cindex suffix, stripping from file names
11046 @cindex file names, stripping directory and suffix
11047 @cindex leading directory components, stripping
11048
11049 @command{basename} removes any leading directory components from
11050 @var{name}.  Synopsis:
11051
11052 @example
11053 basename @var{name} [@var{suffix}]
11054 @end example
11055
11056 If @var{suffix} is specified and is identical to the end of @var{name},
11057 it is removed from @var{name} as well.  Note that since trailing slashes
11058 are removed prior to suffix matching, @var{suffix} will do nothing if it
11059 contains slashes.  @command{basename} prints the result on standard
11060 output.
11061
11062 @c This test is used both here and in the section on dirname.
11063 @macro basenameAndDirname
11064 Together, @command{basename} and @command{dirname} are designed such
11065 that if @samp{ls "$name"} succeeds, then the command sequence @samp{cd
11066 "$(dirname "$name")"; ls "$(basename "$name")"} will, too.  This works
11067 for everything except file names containing a trailing newline.
11068 @end macro
11069 @basenameAndDirname
11070
11071 @acronym{POSIX} allows the implementation to define the results if
11072 @var{name} is empty or @samp{//}.  In the former case, @acronym{GNU}
11073 @command{basename} returns the empty string.  In the latter case, the
11074 result is @samp{//} on platforms where @var{//} is distinct from
11075 @var{/}, and @samp{/} on platforms where there is no difference.
11076
11077 The only options are @option{--help} and @option{--version}.  @xref{Common
11078 options}.  Options must precede operands.
11079
11080 @exitstatus
11081
11082 Examples:
11083
11084 @smallexample
11085 # Output "sort".
11086 basename /usr/bin/sort
11087
11088 # Output "stdio".
11089 basename include/stdio.h .h
11090 @end smallexample
11091
11092
11093 @node dirname invocation
11094 @section @command{dirname}: Strip non-directory suffix from a file name
11095
11096 @pindex dirname
11097 @cindex directory components, printing
11098 @cindex stripping non-directory suffix
11099 @cindex non-directory suffix, stripping
11100
11101 @command{dirname} prints all but the final slash-delimited component of
11102 a string (presumably a file name).  Synopsis:
11103
11104 @example
11105 dirname @var{name}
11106 @end example
11107
11108 If @var{name} is a single component, @command{dirname} prints @samp{.}
11109 (meaning the current directory).
11110
11111 @basenameAndDirname
11112
11113 @acronym{POSIX} allows the implementation to define the results if
11114 @var{name} is @samp{//}.  With @acronym{GNU} @command{dirname}, the
11115 result is @samp{//} on platforms where @var{//} is distinct from
11116 @var{/}, and @samp{/} on platforms where there is no difference.
11117
11118 The only options are @option{--help} and @option{--version}.  @xref{Common
11119 options}.
11120
11121 @exitstatus
11122
11123 Examples:
11124
11125 @smallexample
11126 # Output "/usr/bin".
11127 dirname /usr/bin/sort
11128
11129 # Output ".".
11130 dirname stdio.h
11131 @end smallexample
11132
11133
11134 @node pathchk invocation
11135 @section @command{pathchk}: Check file name portability
11136
11137 @pindex pathchk
11138 @cindex file names, checking validity and portability
11139 @cindex valid file names, checking for
11140 @cindex portable file names, checking for
11141
11142 @command{pathchk} checks portability of file names.  Synopsis:
11143
11144 @example
11145 pathchk [@var{option}]@dots{} @var{name}@dots{}
11146 @end example
11147
11148 For each @var{name}, @command{pathchk} prints a message if any of
11149 these conditions is true:
11150
11151 @enumerate
11152 @item
11153 One of the existing directories in @var{name} does not have search
11154 (execute) permission,
11155 @item
11156 The length of @var{name} is larger than the maximum supported by the
11157 operating system.
11158 @item
11159 The length of one component of @var{name} is longer than
11160 its file system's maximum.
11161 @end enumerate
11162
11163 A nonexistent @var{name} is not an error, so long a file with that
11164 name could be created under the above conditions.
11165
11166 The program accepts the following options.  Also see @ref{Common options}.
11167 Options must precede operands.
11168
11169 @table @samp
11170
11171 @item -p
11172 @opindex -p
11173 Instead of performing checks based on the underlying file system,
11174 print a message if any of these conditions is true:
11175
11176 @enumerate
11177 @item
11178 A file name is empty.
11179
11180 @item
11181 The length of a file name or one of its components exceeds the
11182 @acronym{POSIX} minimum limits for portability.
11183
11184 @item
11185 A file name contains a character outside the portable file name
11186 character set, namely, the ASCII letters and digits, @samp{-},
11187 @samp{.}, @samp{/}, and @samp{_}.
11188 @end enumerate
11189
11190 @item -P
11191 @opindex -P
11192 Print a message if a file name is empty, or if it contains a component
11193 that begins with @samp{-}.
11194
11195 @item --portability
11196 @opindex --portability
11197 Print a message if a file name is not portable to all @acronym{POSIX}
11198 hosts.  This option is equivalent to @samp{-p -P}.
11199
11200 @end table
11201
11202 @cindex exit status of @command{pathchk}
11203 Exit status:
11204
11205 @display
11206 0 if all specified file names passed all checks,
11207 1 otherwise.
11208 @end display
11209
11210
11211 @node Working context
11212 @chapter Working context
11213
11214 @cindex working context
11215 @cindex commands for printing the working context
11216
11217 This section describes commands that display or alter the context in
11218 which you are working: the current directory, the terminal settings, and
11219 so forth.  See also the user-related commands in the next section.
11220
11221 @menu
11222 * pwd invocation::              Print working directory.
11223 * stty invocation::             Print or change terminal characteristics.
11224 * printenv invocation::         Print environment variables.
11225 * tty invocation::              Print file name of terminal on standard input.
11226 @end menu
11227
11228
11229 @node pwd invocation
11230 @section @command{pwd}: Print working directory
11231
11232 @pindex pwd
11233 @cindex print name of current directory
11234 @cindex current working directory, printing
11235 @cindex working directory, printing
11236
11237 @cindex symbolic links and @command{pwd}
11238 @command{pwd} prints the fully resolved name of the current directory.
11239 That is, all components of the printed name will be actual directory
11240 names---none will be symbolic links.
11241
11242 @cindex conflicts with shell built-ins
11243 @cindex built-in shell commands, conflicts with
11244 Because most shells have a built-in @command{pwd} command, using an
11245 unadorned @command{pwd} in a script or interactively may get you
11246 different functionality than that described here.
11247
11248 The only options are a lone @option{--help} or
11249 @option{--version}.  @xref{Common options}.
11250
11251 @exitstatus
11252
11253
11254 @node stty invocation
11255 @section @command{stty}: Print or change terminal characteristics
11256
11257 @pindex stty
11258 @cindex change or print terminal settings
11259 @cindex terminal settings
11260 @cindex line settings of terminal
11261
11262 @command{stty} prints or changes terminal characteristics, such as baud rate.
11263 Synopses:
11264
11265 @example
11266 stty [@var{option}] [@var{setting}]@dots{}
11267 stty [@var{option}]
11268 @end example
11269
11270 If given no line settings, @command{stty} prints the baud rate, line
11271 discipline number (on systems that support it), and line settings
11272 that have been changed from the values set by @samp{stty sane}.
11273 By default, mode reading and setting are performed on the tty line
11274 connected to standard input, although this can be modified by the
11275 @option{--file} option.
11276
11277 @command{stty} accepts many non-option arguments that change aspects of
11278 the terminal line operation, as described below.
11279
11280 The program accepts the following options.  Also see @ref{Common options}.
11281
11282 @table @samp
11283 @item -a
11284 @itemx --all
11285 @opindex -a
11286 @opindex --all
11287 Print all current settings in human-readable form.  This option may not
11288 be used in combination with any line settings.
11289
11290 @item -F @var{device}
11291 @itemx --file=@var{device}
11292 @opindex -F
11293 @opindex --file
11294 Set the line opened by the file name specified in @var{device} instead of
11295 the tty line connected to standard input.  This option is necessary
11296 because opening a @acronym{POSIX} tty requires use of the @code{O_NONDELAY} flag to
11297 prevent a @acronym{POSIX} tty from blocking until the carrier detect line is high if
11298 the @code{clocal} flag is not set.  Hence, it is not always possible
11299 to allow the shell to open the device in the traditional manner.
11300
11301 @item -g
11302 @itemx --save
11303 @opindex -g
11304 @opindex --save
11305 @cindex machine-readable @command{stty} output
11306 Print all current settings in a form that can be used as an argument to
11307 another @command{stty} command to restore the current settings.  This option
11308 may not be used in combination with any line settings.
11309
11310 @end table
11311
11312 Many settings can be turned off by preceding them with a @samp{-}.
11313 Such arguments are marked below with ``May be negated'' in their
11314 description.  The descriptions themselves refer to the positive
11315 case, that is, when @emph{not} negated (unless stated otherwise,
11316 of course).
11317
11318 Some settings are not available on all @acronym{POSIX} systems, since they use
11319 extensions.  Such arguments are marked below with ``Non-@acronym{POSIX}'' in their
11320 description.  On non-@acronym{POSIX} systems, those or other settings also may not
11321 be available, but it's not feasible to document all the variations: just
11322 try it and see.
11323
11324 @exitstatus
11325
11326 @menu
11327 * Control::                     Control settings
11328 * Input::                       Input settings
11329 * Output::                      Output settings
11330 * Local::                       Local settings
11331 * Combination::                 Combination settings
11332 * Characters::                  Special characters
11333 * Special::                     Special settings
11334 @end menu
11335
11336
11337 @node Control
11338 @subsection Control settings
11339
11340 @cindex control settings
11341 Control settings:
11342
11343 @table @samp
11344 @item parenb
11345 @opindex parenb
11346 @cindex two-way parity
11347 Generate parity bit in output and expect parity bit in input.
11348 May be negated.
11349
11350 @item parodd
11351 @opindex parodd
11352 @cindex odd parity
11353 @cindex even parity
11354 Set odd parity (even if negated).  May be negated.
11355
11356 @item cs5
11357 @itemx cs6
11358 @itemx cs7
11359 @itemx cs8
11360 @opindex cs@var{n}
11361 @cindex character size
11362 @cindex eight-bit characters
11363 Set character size to 5, 6, 7, or 8 bits.
11364
11365 @item hup
11366 @itemx hupcl
11367 @opindex hup[cl]
11368 Send a hangup signal when the last process closes the tty.  May be
11369 negated.
11370
11371 @item cstopb
11372 @opindex cstopb
11373 @cindex stop bits
11374 Use two stop bits per character (one if negated).  May be negated.
11375
11376 @item cread
11377 @opindex cread
11378 Allow input to be received.  May be negated.
11379
11380 @item clocal
11381 @opindex clocal
11382 @cindex modem control
11383 Disable modem control signals.  May be negated.
11384
11385 @item crtscts
11386 @opindex crtscts
11387 @cindex hardware flow control
11388 @cindex flow control, hardware
11389 @cindex RTS/CTS flow control
11390 Enable RTS/CTS flow control.  Non-@acronym{POSIX}.  May be negated.
11391 @end table
11392
11393
11394 @node Input
11395 @subsection Input settings
11396
11397 @cindex input settings
11398
11399 @table @samp
11400 @item ignbrk
11401 @opindex ignbrk
11402 @cindex breaks, ignoring
11403 Ignore break characters.  May be negated.
11404
11405 @item brkint
11406 @opindex brkint
11407 @cindex breaks, cause interrupts
11408 Make breaks cause an interrupt signal.  May be negated.
11409
11410 @item ignpar
11411 @opindex ignpar
11412 @cindex parity, ignoring
11413 Ignore characters with parity errors.  May be negated.
11414
11415 @item parmrk
11416 @opindex parmrk
11417 @cindex parity errors, marking
11418 Mark parity errors (with a 255-0-character sequence).  May be negated.
11419
11420 @item inpck
11421 @opindex inpck
11422 Enable input parity checking.  May be negated.
11423
11424 @item istrip
11425 @opindex istrip
11426 @cindex eight-bit input
11427 Clear high (8th) bit of input characters.  May be negated.
11428
11429 @item inlcr
11430 @opindex inlcr
11431 @cindex newline, translating to return
11432 Translate newline to carriage return.  May be negated.
11433
11434 @item igncr
11435 @opindex igncr
11436 @cindex return, ignoring
11437 Ignore carriage return.  May be negated.
11438
11439 @item icrnl
11440 @opindex icrnl
11441 @cindex return, translating to newline
11442 Translate carriage return to newline.  May be negated.
11443
11444 @item iutf8
11445 @opindex iutf8
11446 @cindex input encoding, UTF-8
11447 Assume input characters are UTF-8 encoded.  May be negated.
11448
11449 @item ixon
11450 @opindex ixon
11451 @kindex C-s/C-q flow control
11452 @cindex XON/XOFF flow control
11453 Enable XON/XOFF flow control (that is, @kbd{CTRL-S}/@kbd{CTRL-Q}).  May
11454 be negated.
11455
11456 @item ixoff
11457 @itemx tandem
11458 @opindex ixoff
11459 @opindex tandem
11460 @cindex software flow control
11461 @cindex flow control, software
11462 Enable sending of @code{stop} character when the system input buffer
11463 is almost full, and @code{start} character when it becomes almost
11464 empty again.  May be negated.
11465
11466 @item iuclc
11467 @opindex iuclc
11468 @cindex uppercase, translating to lowercase
11469 Translate uppercase characters to lowercase.  Non-@acronym{POSIX}.  May be
11470 negated.
11471
11472 @item ixany
11473 @opindex ixany
11474 Allow any character to restart output (only the start character
11475 if negated).  Non-@acronym{POSIX}.  May be negated.
11476
11477 @item imaxbel
11478 @opindex imaxbel
11479 @cindex beeping at input buffer full
11480 Enable beeping and not flushing input buffer if a character arrives
11481 when the input buffer is full.  Non-@acronym{POSIX}.  May be negated.
11482 @end table
11483
11484
11485 @node Output
11486 @subsection Output settings
11487
11488 @cindex output settings
11489 These arguments specify output-related operations.
11490
11491 @table @samp
11492 @item opost
11493 @opindex opost
11494 Postprocess output.  May be negated.
11495
11496 @item olcuc
11497 @opindex olcuc
11498 @cindex lowercase, translating to output
11499 Translate lowercase characters to uppercase.  Non-@acronym{POSIX}.  May be
11500 negated.
11501
11502 @item ocrnl
11503 @opindex ocrnl
11504 @cindex return, translating to newline
11505 Translate carriage return to newline.  Non-@acronym{POSIX}.  May be negated.
11506
11507 @item onlcr
11508 @opindex onlcr
11509 @cindex newline, translating to crlf
11510 Translate newline to carriage return-newline.  Non-@acronym{POSIX}.  May be
11511 negated.
11512
11513 @item onocr
11514 @opindex onocr
11515 Do not print carriage returns in the first column.  Non-@acronym{POSIX}.
11516 May be negated.
11517
11518 @item onlret
11519 @opindex onlret
11520 Newline performs a carriage return.  Non-@acronym{POSIX}.  May be negated.
11521
11522 @item ofill
11523 @opindex ofill
11524 @cindex pad instead of timing for delaying
11525 Use fill (padding) characters instead of timing for delays.  Non-@acronym{POSIX}.
11526 May be negated.
11527
11528 @item ofdel
11529 @opindex ofdel
11530 @cindex pad character
11531 Use delete characters for fill instead of null characters.  Non-@acronym{POSIX}.
11532 May be negated.
11533
11534 @item nl1
11535 @itemx nl0
11536 @opindex nl@var{n}
11537 Newline delay style.  Non-@acronym{POSIX}.
11538
11539 @item cr3
11540 @itemx cr2
11541 @itemx cr1
11542 @itemx cr0
11543 @opindex cr@var{n}
11544 Carriage return delay style.  Non-@acronym{POSIX}.
11545
11546 @item tab3
11547 @itemx tab2
11548 @itemx tab1
11549 @itemx tab0
11550 @opindex tab@var{n}
11551 Horizontal tab delay style.  Non-@acronym{POSIX}.
11552
11553 @item bs1
11554 @itemx bs0
11555 @opindex bs@var{n}
11556 Backspace delay style.  Non-@acronym{POSIX}.
11557
11558 @item vt1
11559 @itemx vt0
11560 @opindex vt@var{n}
11561 Vertical tab delay style.  Non-@acronym{POSIX}.
11562
11563 @item ff1
11564 @itemx ff0
11565 @opindex ff@var{n}
11566 Form feed delay style.  Non-@acronym{POSIX}.
11567 @end table
11568
11569
11570 @node Local
11571 @subsection Local settings
11572
11573 @cindex local settings
11574
11575 @table @samp
11576 @item isig
11577 @opindex isig
11578 Enable @code{interrupt}, @code{quit}, and @code{suspend} special
11579 characters.  May be negated.
11580
11581 @item icanon
11582 @opindex icanon
11583 Enable @code{erase}, @code{kill}, @code{werase}, and @code{rprnt}
11584 special characters.  May be negated.
11585
11586 @item iexten
11587 @opindex iexten
11588 Enable non-@acronym{POSIX} special characters.  May be negated.
11589
11590 @item echo
11591 @opindex echo
11592 Echo input characters.  May be negated.
11593
11594 @item echoe
11595 @itemx crterase
11596 @opindex echoe
11597 @opindex crterase
11598 Echo @code{erase} characters as backspace-space-backspace.  May be
11599 negated.
11600
11601 @item echok
11602 @opindex echok
11603 @cindex newline echoing after @code{kill}
11604 Echo a newline after a @code{kill} character.  May be negated.
11605
11606 @item echonl
11607 @opindex echonl
11608 @cindex newline, echoing
11609 Echo newline even if not echoing other characters.  May be negated.
11610
11611 @item noflsh
11612 @opindex noflsh
11613 @cindex flushing, disabling
11614 Disable flushing after @code{interrupt} and @code{quit} special
11615 characters.  May be negated.
11616
11617 @item xcase
11618 @opindex xcase
11619 @cindex case translation
11620 Enable input and output of uppercase characters by preceding their
11621 lowercase equivalents with @samp{\}, when @code{icanon} is set.
11622 Non-@acronym{POSIX}.  May be negated.
11623
11624 @item tostop
11625 @opindex tostop
11626 @cindex background jobs, stopping at terminal write
11627 Stop background jobs that try to write to the terminal.  Non-@acronym{POSIX}.
11628 May be negated.
11629
11630 @item echoprt
11631 @itemx prterase
11632 @opindex echoprt
11633 @opindex prterase
11634 Echo erased characters backward, between @samp{\} and @samp{/}.
11635 Non-@acronym{POSIX}.  May be negated.
11636
11637 @item echoctl
11638 @itemx ctlecho
11639 @opindex echoctl
11640 @opindex ctlecho
11641 @cindex control characters, using @samp{^@var{c}}
11642 @cindex hat notation for control characters
11643 Echo control characters in hat notation (@samp{^@var{c}}) instead
11644 of literally.  Non-@acronym{POSIX}.  May be negated.
11645
11646 @item echoke
11647 @itemx crtkill
11648 @opindex echoke
11649 @opindex crtkill
11650 Echo the @code{kill} special character by erasing each character on
11651 the line as indicated by the @code{echoprt} and @code{echoe} settings,
11652 instead of by the @code{echoctl} and @code{echok} settings.  Non-@acronym{POSIX}.
11653 May be negated.
11654 @end table
11655
11656
11657 @node Combination
11658 @subsection Combination settings
11659
11660 @cindex combination settings
11661 Combination settings:
11662
11663 @table @samp
11664 @item evenp
11665 @opindex evenp
11666 @itemx parity
11667 @opindex parity
11668 Same as @code{parenb -parodd cs7}.  May be negated.  If negated, same
11669 as @code{-parenb cs8}.
11670
11671 @item oddp
11672 @opindex oddp
11673 Same as @code{parenb parodd cs7}.  May be negated.  If negated, same
11674 as @code{-parenb cs8}.
11675
11676 @item nl
11677 @opindex nl
11678 Same as @code{-icrnl -onlcr}.  May be negated.  If negated, same as
11679 @code{icrnl -inlcr -igncr onlcr -ocrnl -onlret}.
11680
11681 @item ek
11682 @opindex ek
11683 Reset the @code{erase} and @code{kill} special characters to their default
11684 values.
11685
11686 @item sane
11687 @opindex sane
11688 Same as:
11689
11690 @c This is too long to write inline.
11691 @example
11692 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff
11693 -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr
11694 -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
11695 ff0 isig icanon iexten echo echoe echok -echonl
11696 -noflsh -xcase -tostop -echoprt echoctl echoke
11697 @end example
11698
11699 @noindent
11700 and also sets all special characters to their default values.
11701
11702 @item cooked
11703 @opindex cooked
11704 Same as @code{brkint ignpar istrip icrnl ixon opost isig icanon}, plus
11705 sets the @code{eof} and @code{eol} characters to their default values
11706 if they are the same as the @code{min} and @code{time} characters.
11707 May be negated.  If negated, same as @code{raw}.
11708
11709 @item raw
11710 @opindex raw
11711 Same as:
11712
11713 @example
11714 -ignbrk -brkint -ignpar -parmrk -inpck -istrip
11715 -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
11716 -imaxbel -opost -isig -icanon -xcase min 1 time 0
11717 @end example
11718
11719 @noindent
11720 May be negated.  If negated, same as @code{cooked}.
11721
11722 @item cbreak
11723 @opindex cbreak
11724 Same as @option{-icanon}.  May be negated.  If negated, same as
11725 @code{icanon}.
11726
11727 @item pass8
11728 @opindex pass8
11729 @cindex eight-bit characters
11730 Same as @code{-parenb -istrip cs8}.  May be negated.  If negated,
11731 same as @code{parenb istrip cs7}.
11732
11733 @item litout
11734 @opindex litout
11735 Same as @option{-parenb -istrip -opost cs8}.  May be negated.
11736 If negated, same as @code{parenb istrip opost cs7}.
11737
11738 @item decctlq
11739 @opindex decctlq
11740 Same as @option{-ixany}.  Non-@acronym{POSIX}.  May be negated.
11741
11742 @item tabs
11743 @opindex tabs
11744 Same as @code{tab0}.  Non-@acronym{POSIX}.  May be negated.  If negated, same
11745 as @code{tab3}.
11746
11747 @item lcase
11748 @itemx LCASE
11749 @opindex lcase
11750 @opindex LCASE
11751 Same as @code{xcase iuclc olcuc}.  Non-@acronym{POSIX}.  May be negated.
11752
11753 @item crt
11754 @opindex crt
11755 Same as @code{echoe echoctl echoke}.
11756
11757 @item dec
11758 @opindex dec
11759 Same as @code{echoe echoctl echoke -ixany intr ^C erase ^? kill C-u}.
11760 @end table
11761
11762
11763 @node Characters
11764 @subsection Special characters
11765
11766 @cindex special characters
11767 @cindex characters, special
11768
11769 The special characters' default values vary from system to system.
11770 They are set with the syntax @samp{name value}, where the names are
11771 listed below and the value can be given either literally, in hat
11772 notation (@samp{^@var{c}}), or as an integer which may start with
11773 @samp{0x} to indicate hexadecimal, @samp{0} to indicate octal, or
11774 any other digit to indicate decimal.
11775
11776 @cindex disabling special characters
11777 @kindex u@r{, and disabling special characters}
11778 For GNU stty, giving a value of @code{^-} or @code{undef} disables that
11779 special character.  (This is incompatible with Ultrix @command{stty},
11780 which uses  a value of @samp{u} to disable a special character.  GNU
11781 @command{stty} treats a value @samp{u} like any other, namely to set that
11782 special character to @key{U}.)
11783
11784 @table @samp
11785
11786 @item intr
11787 @opindex intr
11788 Send an interrupt signal.
11789
11790 @item quit
11791 @opindex quit
11792 Send a quit signal.
11793
11794 @item erase
11795 @opindex erase
11796 Erase the last character typed.
11797
11798 @item kill
11799 @opindex kill
11800 Erase the current line.
11801
11802 @item eof
11803 @opindex eof
11804 Send an end of file (terminate the input).
11805
11806 @item eol
11807 @opindex eol
11808 End the line.
11809
11810 @item eol2
11811 @opindex eol2
11812 Alternate character to end the line.  Non-@acronym{POSIX}.
11813
11814 @item swtch
11815 @opindex swtch
11816 Switch to a different shell layer.  Non-@acronym{POSIX}.
11817
11818 @item start
11819 @opindex start
11820 Restart the output after stopping it.
11821
11822 @item stop
11823 @opindex stop
11824 Stop the output.
11825
11826 @item susp
11827 @opindex susp
11828 Send a terminal stop signal.
11829
11830 @item dsusp
11831 @opindex dsusp
11832 Send a terminal stop signal after flushing the input.  Non-@acronym{POSIX}.
11833
11834 @item rprnt
11835 @opindex rprnt
11836 Redraw the current line.  Non-@acronym{POSIX}.
11837
11838 @item werase
11839 @opindex werase
11840 Erase the last word typed.  Non-@acronym{POSIX}.
11841
11842 @item lnext
11843 @opindex lnext
11844 Enter the next character typed literally, even if it is a special
11845 character.  Non-@acronym{POSIX}.
11846 @end table
11847
11848
11849 @node Special
11850 @subsection Special settings
11851
11852 @cindex special settings
11853
11854 @table @samp
11855 @item min @var{n}
11856 @opindex min
11857 Set the minimum number of characters that will satisfy a read until
11858 the time value has expired, when @option{-icanon} is set.
11859
11860 @item time @var{n}
11861 @opindex time
11862 Set the number of tenths of a second before reads time out if the minimum
11863 number of characters have not been read, when @option{-icanon} is set.
11864
11865 @item ispeed @var{n}
11866 @opindex ispeed
11867 Set the input speed to @var{n}.
11868
11869 @item ospeed @var{n}
11870 @opindex ospeed
11871 Set the output speed to @var{n}.
11872
11873 @item rows @var{n}
11874 @opindex rows
11875 Tell the tty kernel driver that the terminal has @var{n} rows.  Non-@acronym{POSIX}.
11876
11877 @item cols @var{n}
11878 @itemx columns @var{n}
11879 @opindex cols
11880 @opindex columns
11881 Tell the kernel that the terminal has @var{n} columns.  Non-@acronym{POSIX}.
11882
11883 @item size
11884 @opindex size
11885 @vindex LINES
11886 @vindex COLUMNS
11887 Print the number of rows and columns that the kernel thinks the
11888 terminal has.  (Systems that don't support rows and columns in the kernel
11889 typically use the environment variables @env{LINES} and @env{COLUMNS}
11890 instead; however, GNU @command{stty} does not know anything about them.)
11891 Non-@acronym{POSIX}.
11892
11893 @item line @var{n}
11894 @opindex line
11895 Use line discipline @var{n}.  Non-@acronym{POSIX}.
11896
11897 @item speed
11898 @opindex speed
11899 Print the terminal speed.
11900
11901 @item @var{n}
11902 @cindex baud rate, setting
11903 @c FIXME: Is this still true that the baud rate can't be set
11904 @c higher than 38400?
11905 Set the input and output speeds to @var{n}.  @var{n} can be one
11906 of: 0 50 75 110 134 134.5 150 200 300 600 1200 1800 2400 4800 9600
11907 19200 38400 @code{exta} @code{extb}.  @code{exta} is the same as
11908 19200; @code{extb} is the same as 38400.  0 hangs up the line if
11909 @option{-clocal} is set.
11910 @end table
11911
11912
11913 @node printenv invocation
11914 @section @command{printenv}: Print all or some environment variables
11915
11916 @pindex printenv
11917 @cindex printing all or some environment variables
11918 @cindex environment variables, printing
11919
11920 @command{printenv} prints environment variable values.  Synopsis:
11921
11922 @example
11923 printenv [@var{option}] [@var{variable}]@dots{}
11924 @end example
11925
11926 If no @var{variable}s are specified, @command{printenv} prints the value of
11927 every environment variable.  Otherwise, it prints the value of each
11928 @var{variable} that is set, and nothing for those that are not set.
11929
11930 The only options are a lone @option{--help} or @option{--version}.
11931 @xref{Common options}.
11932
11933 @cindex exit status of @command{printenv}
11934 Exit status:
11935
11936 @display
11937 0 if all variables specified were found
11938 1 if at least one specified variable was not found
11939 2 if a write error occurred
11940 @end display
11941
11942
11943 @node tty invocation
11944 @section @command{tty}: Print file name of terminal on standard input
11945
11946 @pindex tty
11947 @cindex print terminal file name
11948 @cindex terminal file name, printing
11949
11950 @command{tty} prints the file name of the terminal connected to its standard
11951 input.  It prints @samp{not a tty} if standard input is not a terminal.
11952 Synopsis:
11953
11954 @example
11955 tty [@var{option}]@dots{}
11956 @end example
11957
11958 The program accepts the following option.  Also see @ref{Common options}.
11959
11960 @table @samp
11961
11962 @item -s
11963 @itemx --silent
11964 @itemx --quiet
11965 @opindex -s
11966 @opindex --silent
11967 @opindex --quiet
11968 Print nothing; only return an exit status.
11969
11970 @end table
11971
11972 @cindex exit status of @command{tty}
11973 Exit status:
11974
11975 @display
11976 0 if standard input is a terminal
11977 1 if standard input is not a terminal
11978 2 if given incorrect arguments
11979 3 if a write error occurs
11980 @end display
11981
11982
11983 @node User information
11984 @chapter User information
11985
11986 @cindex user information, commands for
11987 @cindex commands for printing user information
11988
11989 This section describes commands that print user-related information:
11990 logins, groups, and so forth.
11991
11992 @menu
11993 * id invocation::               Print user identity.
11994 * logname invocation::          Print current login name.
11995 * whoami invocation::           Print effective user ID.
11996 * groups invocation::           Print group names a user is in.
11997 * users invocation::            Print login names of users currently logged in.
11998 * who invocation::              Print who is currently logged in.
11999 @end menu
12000
12001
12002 @node id invocation
12003 @section @command{id}: Print user identity
12004
12005 @pindex id
12006 @cindex real user and group IDs, printing
12007 @cindex effective user and group IDs, printing
12008 @cindex printing real and effective user and group IDs
12009
12010 @command{id} prints information about the given user, or the process
12011 running it if no user is specified.  Synopsis:
12012
12013 @example
12014 id [@var{option}]@dots{} [@var{username}]
12015 @end example
12016
12017 By default, it prints the real user ID, real group ID, effective user ID
12018 if different from the real user ID, effective group ID if different from
12019 the real group ID, and supplemental group IDs.
12020
12021 Each of these numeric values is preceded by an identifying string and
12022 followed by the corresponding user or group name in parentheses.
12023
12024 The options cause @command{id} to print only part of the above information.
12025 Also see @ref{Common options}.
12026
12027 @table @samp
12028 @item -g
12029 @itemx --group
12030 @opindex -g
12031 @opindex --group
12032 Print only the group ID.
12033
12034 @item -G
12035 @itemx --groups
12036 @opindex -G
12037 @opindex --groups
12038 Print only the group ID and the supplementary groups.
12039
12040 @item -n
12041 @itemx --name
12042 @opindex -n
12043 @opindex --name
12044 Print the user or group name instead of the ID number.  Requires
12045 @option{-u}, @option{-g}, or @option{-G}.
12046
12047 @item -r
12048 @itemx --real
12049 @opindex -r
12050 @opindex --real
12051 Print the real, instead of effective, user or group ID.  Requires
12052 @option{-u}, @option{-g}, or @option{-G}.
12053
12054 @item -u
12055 @itemx --user
12056 @opindex -u
12057 @opindex --user
12058 Print only the user ID.
12059
12060 @end table
12061
12062 @exitstatus
12063
12064
12065 @node logname invocation
12066 @section @command{logname}: Print current login name
12067
12068 @pindex logname
12069 @cindex printing user's login name
12070 @cindex login name, printing
12071 @cindex user name, printing
12072
12073 @flindex utmp
12074 @command{logname} prints the calling user's name, as found in a
12075 system-maintained file (often @file{/var/run/utmp} or
12076 @file{/etc/utmp}), and exits with a status of 0.  If there is no entry
12077 for the calling process, @command{logname} prints
12078 an error message and exits with a status of 1.
12079
12080 The only options are @option{--help} and @option{--version}.  @xref{Common
12081 options}.
12082
12083 @exitstatus
12084
12085
12086 @node whoami invocation
12087 @section @command{whoami}: Print effective user ID
12088
12089 @pindex whoami
12090 @cindex effective user ID, printing
12091 @cindex printing the effective user ID
12092
12093 @command{whoami} prints the user name associated with the current
12094 effective user ID.  It is equivalent to the command @samp{id -un}.
12095
12096 The only options are @option{--help} and @option{--version}.  @xref{Common
12097 options}.
12098
12099 @exitstatus
12100
12101
12102 @node groups invocation
12103 @section @command{groups}: Print group names a user is in
12104
12105 @pindex groups
12106 @cindex printing groups a user is in
12107 @cindex supplementary groups, printing
12108
12109 @command{groups} prints the names of the primary and any supplementary
12110 groups for each given @var{username}, or the current process if no names
12111 are given.  If more than one name is given, the name of each user is
12112 printed before
12113 the list of that user's groups.  Synopsis:
12114
12115 @example
12116 groups [@var{username}]@dots{}
12117 @end example
12118
12119 The group lists are equivalent to the output of the command @samp{id -Gn}.
12120
12121 The only options are @option{--help} and @option{--version}.  @xref{Common
12122 options}.
12123
12124 @exitstatus
12125
12126
12127 @node users invocation
12128 @section @command{users}: Print login names of users currently logged in
12129
12130 @pindex users
12131 @cindex printing current usernames
12132 @cindex usernames, printing current
12133
12134 @cindex login sessions, printing users with
12135 @command{users} prints on a single line a blank-separated list of user
12136 names of users currently logged in to the current host.  Each user name
12137 corresponds to a login session, so if a user has more than one login
12138 session, that user's name will appear the same number of times in the
12139 output.  Synopsis:
12140
12141 @example
12142 users [@var{file}]
12143 @end example
12144
12145 @flindex utmp
12146 @flindex wtmp
12147 With no @var{file} argument, @command{users} extracts its information from
12148 a system-maintained file (often @file{/var/run/utmp} or
12149 @file{/etc/utmp}).  If a file argument is given, @command{users} uses
12150 that file instead.  A common choice is @file{/var/log/wtmp}.
12151
12152 The only options are @option{--help} and @option{--version}.  @xref{Common
12153 options}.
12154
12155 @exitstatus
12156
12157
12158 @node who invocation
12159 @section @command{who}: Print who is currently logged in
12160
12161 @pindex who
12162 @cindex printing current user information
12163 @cindex information, about current users
12164
12165 @command{who} prints information about users who are currently logged on.
12166 Synopsis:
12167
12168 @example
12169 @command{who} [@var{option}] [@var{file}] [am i]
12170 @end example
12171
12172 @cindex terminal lines, currently used
12173 @cindex login time
12174 @cindex remote hostname
12175 If given no non-option arguments, @command{who} prints the following
12176 information for each user currently logged on: login name, terminal
12177 line, login time, and remote hostname or X display.
12178
12179 @flindex utmp
12180 @flindex wtmp
12181 If given one non-option argument, @command{who} uses that instead of
12182 a default system-maintained file (often @file{/var/run/utmp} or
12183 @file{/etc/utmp}) as the name of the file containing the record of
12184 users logged on.  @file{/var/log/wtmp} is commonly given as an argument
12185 to @command{who} to look at who has previously logged on.
12186
12187 @opindex am i
12188 @opindex who am i
12189 If given two non-option arguments, @command{who} prints only the entry
12190 for the user running it (determined from its standard input), preceded
12191 by the hostname.  Traditionally, the two arguments given are @samp{am
12192 i}, as in @samp{who am i}.
12193
12194 @vindex TZ
12195 Time stamps are listed according to the time zone rules specified by
12196 the @env{TZ} environment variable, or by the system default rules if
12197 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
12198 with @env{TZ}, libc, The GNU C Library}.
12199
12200 The program accepts the following options.  Also see @ref{Common options}.
12201
12202 @table @samp
12203
12204 @item -a
12205 @itemx --all
12206 @opindex -a
12207 @opindex --all
12208 Same as @samp{-b -d --login -p -r -t -T -u}.
12209
12210 @item -b
12211 @itemx --boot
12212 @opindex -b
12213 @opindex --boot
12214 Print the date and time of last system boot.
12215
12216 @item -d
12217 @itemx --dead
12218 @opindex -d
12219 @opindex --dead
12220 Print information corresponding to dead processes.
12221
12222 @item -H
12223 @itemx --heading
12224 @opindex -H
12225 @opindex --heading
12226 Print column headings.
12227
12228 @item -m
12229 @opindex -m
12230 Same as @samp{who am i}.
12231
12232 @item -q
12233 @itemx --count
12234 @opindex -q
12235 @opindex --count
12236 Print only the login names and the number of users logged on.
12237 Overrides all other options.
12238
12239 @item -s
12240 @opindex -s
12241 Ignored; for compatibility with other versions of @command{who}.
12242
12243 @itemx -u
12244 @opindex -u
12245 @cindex idle time
12246 After the login time, print the number of hours and minutes that the
12247 user has been idle.  @samp{.} means the user was active in the last minute.
12248 @samp{old} means the user has been idle for more than 24 hours.
12249
12250 @item -l
12251 @itemx --login
12252 @opindex -l
12253 @opindex --login
12254 List only the entries that correspond to processes via which the
12255 system is waiting for a user to login.  The user name is always @samp{LOGIN}.
12256
12257 @itemx --lookup
12258 @opindex --lookup
12259 Attempt to canonicalize hostnames found in utmp through a DNS lookup.  This
12260 is not the default because it can cause significant delays on systems with
12261 automatic dial-up internet access.
12262
12263 @item -H
12264 @itemx --heading
12265 @opindex -H
12266 @opindex --heading
12267 Print a line of column headings.
12268
12269 @item -w
12270 @itemx -T
12271 @itemx --mesg
12272 @itemx --message
12273 @itemx --writable
12274 @opindex -w
12275 @opindex -T
12276 @opindex --mesg
12277 @opindex --message
12278 @opindex --writable
12279 @cindex message status
12280 @pindex write@r{, allowed}
12281 After each login name print a character indicating the user's message status:
12282
12283 @display
12284 @samp{+} allowing @code{write} messages
12285 @samp{-} disallowing @code{write} messages
12286 @samp{?} cannot find terminal device
12287 @end display
12288
12289 @end table
12290
12291 @exitstatus
12292
12293
12294 @node System context
12295 @chapter System context
12296
12297 @cindex system context
12298 @cindex context, system
12299 @cindex commands for system context
12300
12301 This section describes commands that print or change system-wide
12302 information.
12303
12304 @menu
12305 * date invocation::             Print or set system date and time.
12306 * uname invocation::            Print system information.
12307 * hostname invocation::         Print or set system name.
12308 * hostid invocation::           Print numeric host identifier.
12309 @end menu
12310
12311
12312 @node date invocation
12313 @section @command{date}: Print or set system date and time
12314
12315 @pindex date
12316 @cindex time, printing or setting
12317 @cindex printing the current time
12318
12319 Synopses:
12320
12321 @example
12322 date [@var{option}]@dots{} [+@var{format}]
12323 date [-u|--utc|--universal] @c this avoids a newline in the output
12324 [ MMDDhhmm[[CC]YY][.ss] ]
12325 @end example
12326
12327 @vindex LC_TIME
12328 Invoking @command{date} with no @var{format} argument is equivalent to invoking
12329 it with a default format that depends on the @env{LC_TIME} locale category.
12330 In the default C locale, this format is @samp{'+%a %b %e %H:%M:%S %Z %Y'},
12331 so the output looks like @samp{Thu Mar @ 3 13:47:51 PST 2005}.
12332
12333 @vindex TZ
12334 Normally, @command{date} uses the time zone rules indicated by the
12335 @env{TZ} environment variable, or the system default rules if @env{TZ}
12336 is not set.  @xref{TZ Variable,, Specifying the Time Zone with
12337 @env{TZ}, libc, The GNU C Library}.
12338
12339 @findex strftime @r{and @command{date}}
12340 @cindex time formats
12341 @cindex formatting times
12342 If given an argument that starts with a @samp{+}, @command{date} prints the
12343 current date and time (or the date and time specified by the
12344 @option{--date} option, see below) in the format defined by that argument,
12345 which is similar to that of the @code{strftime} function.  Except for
12346 conversion specifiers, which start with @samp{%}, characters in the
12347 format string are printed unchanged.  The conversion specifiers are
12348 described below.
12349
12350 @exitstatus
12351
12352 @menu
12353 * Time conversion specifiers::     %[HIklMNpPrRsSTXzZ]
12354 * Date conversion specifiers::     %[aAbBcCdDeFgGhjmuUVwWxyY]
12355 * Literal conversion specifiers::  %[%nt]
12356 * Padding and other flags::        Pad with zeros, spaces, etc.
12357 * Setting the time::               Changing the system clock.
12358 * Options for date::               Instead of the current time.
12359 * Examples of date::               Examples.
12360 @end menu
12361
12362 @node Time conversion specifiers
12363 @subsection Time conversion specifiers
12364
12365 @cindex time conversion specifiers
12366 @cindex conversion specifiers, time
12367
12368 @command{date} conversion specifiers related to times.
12369
12370 @table @samp
12371 @item %H
12372 hour (@samp{00}@dots{}@samp{23})
12373 @item %I
12374 hour (@samp{01}@dots{}@samp{12})
12375 @item %k
12376 hour (@samp{ 0}@dots{}@samp{23}).
12377 This is a @acronym{GNU} extension.
12378 @item %l
12379 hour (@samp{ 1}@dots{}@samp{12}).
12380 This is a @acronym{GNU} extension.
12381 @item %M
12382 minute (@samp{00}@dots{}@samp{59})
12383 @item %N
12384 nanoseconds (@samp{000000000}@dots{}@samp{999999999}).
12385 This is a @acronym{GNU} extension.
12386 @item %p
12387 locale's equivalent of either @samp{AM} or @samp{PM};
12388 blank in many locales.
12389 Noon is treated as @samp{PM} and midnight as @samp{AM}.
12390 @item %P
12391 like @samp{%p}, except lower case.
12392 This is a @acronym{GNU} extension.
12393 @item %r
12394 locale's 12-hour clock time (e.g., @samp{11:11:04 PM})
12395 @item %R
12396 24-hour hour and minute.  Same as @samp{%H:%M}.
12397 This is a @acronym{GNU} extension.
12398 @item %s
12399 @cindex epoch, seconds since
12400 @cindex seconds since the epoch
12401 @cindex beginning of time
12402 seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
12403 Leap seconds are not counted unless leap second support is available.
12404 @xref{%s-examples}, for examples.
12405 This is a @acronym{GNU} extension.
12406 @item %S
12407 second (@samp{00}@dots{}@samp{60}).
12408 This may be @samp{60} if leap seconds are supported.
12409 @item %T
12410 24-hour hour, minute, and second.  Same as @samp{%H:%M:%S}.
12411 @item %X
12412 locale's time representation (e.g., @samp{23:13:48})
12413 @item %z
12414 @w{@acronym{RFC} 2822/@acronym{ISO} 8601} style numeric time zone
12415 (e.g., @samp{-0600} or @samp{+0530}), or nothing if no
12416 time zone is determinable.  This value reflects the numeric time zone
12417 appropriate for the current time, using the time zone rules specified
12418 by the @env{TZ} environment variable.
12419 The time (and optionally, the time zone rules) can be overridden
12420 by the @option{--date} option.
12421 This is a @acronym{GNU} extension.
12422 @item %:z
12423 @w{@acronym{RFC} 3339/@acronym{ISO} 8601} style numeric time zone with
12424 @samp{:} (e.g., @samp{-06:00} or @samp{+05:30}), or nothing if no time
12425 zone is determinable.
12426 This is a @acronym{GNU} extension.
12427 @item %::z
12428 Numeric time zone to the nearest second with @samp{:} (e.g.,
12429 @samp{-06:00:00} or @samp{+05:30:00}), or nothing if no time zone is
12430 determinable.
12431 This is a @acronym{GNU} extension.
12432 @item %:::z
12433 Numeric time zone with @samp{:} using the minimum necessary precision
12434 (e.g., @samp{-06}, @samp{+05:30}, or @samp{-04:56:02}), or nothing if
12435 no time zone is determinable.
12436 This is a @acronym{GNU} extension.
12437 @item %Z
12438 alphabetic time zone abbreviation (e.g., @samp{EDT}), or nothing if no
12439 time zone is determinable.  See @samp{%z} for how it is determined.
12440 @end table
12441
12442
12443 @node Date conversion specifiers
12444 @subsection Date conversion specifiers
12445
12446 @cindex date conversion specifiers
12447 @cindex conversion specifiers, date
12448
12449 @command{date} conversion specifiers related to dates.
12450
12451 @table @samp
12452 @item %a
12453 locale's abbreviated weekday name (e.g., @samp{Sun})
12454 @item %A
12455 locale's full weekday name, variable length (e.g., @samp{Sunday})
12456 @item %b
12457 locale's abbreviated month name (e.g., @samp{Jan})
12458 @item %B
12459 locale's full month name, variable length (e.g., @samp{January})
12460 @item %c
12461 locale's date and time (e.g., @samp{Thu Mar @ 3 23:05:25 2005})
12462 @item %C
12463 century.  This is like @samp{%Y}, except the last two digits are omitted.
12464 For example, it is @samp{20} if @samp{%Y} is @samp{2000},
12465 and is @samp{-0} if @samp{%Y} is @samp{-001}.
12466 It is normally at least two characters, but it may be more.
12467 @item %d
12468 day of month (e.g., @samp{01})
12469 @item %D
12470 date; same as @samp{%m/%d/%y}
12471 @item %e
12472 day of month, space padded; same as @samp{%_d}
12473 @item %F
12474 full date in @acronym{ISO} 8601 format; same as @samp{%Y-%m-%d}.
12475 This is a good choice for a date format, as it is standard and
12476 is easy to sort in the usual case where years are in the range
12477 0000@dots{}9999.
12478 This is a @acronym{GNU} extension.
12479 @item %g
12480 year corresponding to the @acronym{ISO} week number, but without the century
12481 (range @samp{00} through @samp{99}).  This has the same format and value
12482 as @samp{%y}, except that if the @acronym{ISO} week number (see
12483 @samp{%V}) belongs
12484 to the previous or next year, that year is used instead.
12485 This is a @acronym{GNU} extension.
12486 @item %G
12487 year corresponding to the @acronym{ISO} week number.  This has the
12488 same format and value as @samp{%Y}, except that if the @acronym{ISO}
12489 week number (see
12490 @samp{%V}) belongs to the previous or next year, that year is used
12491 instead.
12492 It is normally useful only if @samp{%V} is also used;
12493 for example, the format @samp{%G-%m-%d} is probably a mistake,
12494 since it combines the ISO week number year with the conventional month and day.
12495 This is a @acronym{GNU} extension.
12496 @item %h
12497 same as @samp{%b}
12498 @item %j
12499 day of year (@samp{001}@dots{}@samp{366})
12500 @item %m
12501 month (@samp{01}@dots{}@samp{12})
12502 @item %u
12503 day of week (@samp{1}@dots{}@samp{7}) with @samp{1} corresponding to Monday
12504 @item %U
12505 week number of year, with Sunday as the first day of the week
12506 (@samp{00}@dots{}@samp{53}).
12507 Days in a new year preceding the first Sunday are in week zero.
12508 @item %V
12509 @acronym{ISO} week number, that is, the
12510 week number of year, with Monday as the first day of the week
12511 (@samp{01}@dots{}@samp{53}).
12512 If the week containing January 1 has four or more days in
12513 the new year, then it is considered week 1; otherwise, it is week 53 of
12514 the previous year, and the next week is week 1.  (See the @acronym{ISO} 8601
12515 standard.)
12516 @item %w
12517 day of week (@samp{0}@dots{}@samp{6}) with 0 corresponding to Sunday
12518 @item %W
12519 week number of year, with Monday as first day of week
12520 (@samp{00}@dots{}@samp{53}).
12521 Days in a new year preceding the first Monday are in week zero.
12522 @item %x
12523 locale's date representation (e.g., @samp{12/31/99})
12524 @item %y
12525 last two digits of year (@samp{00}@dots{}@samp{99})
12526 @item %Y
12527 year.  This is normally at least four characters, but it may be more.
12528 Year @samp{0000} precedes year @samp{0001}, and year @samp{-001}
12529 precedes year @samp{0000}.
12530 @end table
12531
12532
12533 @node Literal conversion specifiers
12534 @subsection Literal conversion specifiers
12535
12536 @cindex literal conversion specifiers
12537 @cindex conversion specifiers, literal
12538
12539 @command{date} conversion specifiers that produce literal strings.
12540
12541 @table @samp
12542 @item %%
12543 a literal %
12544 @item %n
12545 a newline
12546 @item %t
12547 a horizontal tab
12548 @end table
12549
12550
12551 @node Padding and other flags
12552 @subsection Padding and other flags
12553
12554 @cindex numeric field padding
12555 @cindex padding of numeric fields
12556 @cindex fields, padding numeric
12557
12558 Unless otherwise specified, @command{date} normally pads numeric fields
12559 with zeros, so that, for
12560 example, numeric months are always output as two digits.
12561 Seconds since the epoch are not padded, though,
12562 since there is no natural width for them.
12563
12564 As a @acronym{GNU} extension, @command{date} recognizes any of the
12565 following optional flags after the @samp{%}:
12566
12567 @table @samp
12568 @item -
12569 (hyphen) Do not pad the field; useful if the output is intended for
12570 human consumption.
12571 @item _
12572 (underscore) Pad with spaces; useful if you need a fixed
12573 number of characters in the output, but zeros are too distracting.
12574 @item 0
12575 (zero) Pad with zeros even if the conversion specifier
12576 would normally pad with spaces.
12577 @item ^
12578 Use upper case characters if possible.
12579 @item #
12580 Use opposite case characters if possible.
12581 A field that is normally upper case becomes lower case, and vice versa.
12582 @end table
12583
12584 @noindent
12585 Here are some examples of padding:
12586
12587 @example
12588 date +%d/%m -d "Feb 1"
12589 @result{} 01/02
12590 date +%-d/%-m -d "Feb 1"
12591 @result{} 1/2
12592 date +%_d/%_m -d "Feb 1"
12593 @result{}  1/ 2
12594 @end example
12595
12596 As a @acronym{GNU} extension, you can specify the field width
12597 (after any flag, if present) as a decimal number.  If the natural size of the
12598 output is of the field has less than the specified number of characters,
12599 the result is written right adjusted and padded to the given
12600 size.  For example, @samp{%9B} prints the right adjusted month name in
12601 a field of width 9.
12602
12603 An optional modifier can follow the optional flag and width
12604 specification.  The modifiers are:
12605
12606 @table @samp
12607 @item E
12608 Use the locale's alternate representation for date and time.  This
12609 modifier applies to the @samp{%c}, @samp{%C}, @samp{%x}, @samp{%X},
12610 @samp{%y} and @samp{%Y} conversion specifiers.  In a Japanese locale, for
12611 example, @samp{%Ex} might yield a date format based on the Japanese
12612 Emperors' reigns.
12613
12614 @item O
12615 Use the locale's alternate numeric symbols for numbers.  This modifier
12616 applies only to numeric conversion specifiers.
12617 @end table
12618
12619 If the format supports the modifier but no alternate representation
12620 is available, it is ignored.
12621
12622
12623 @node Setting the time
12624 @subsection Setting the time
12625
12626 @cindex setting the time
12627 @cindex time setting
12628 @cindex appropriate privileges
12629
12630 If given an argument that does not start with @samp{+}, @command{date} sets
12631 the system clock to the date and time specified by that argument (as
12632 described below).  You must have appropriate privileges to set the
12633 system clock.  The @option{--date} and @option{--set} options may not be
12634 used with such an argument.  The @option{--universal} option may be used
12635 with such an argument to indicate that the specified date and time are
12636 relative to Coordinated Universal Time rather than to the local time
12637 zone.
12638
12639 The argument must consist entirely of digits, which have the following
12640 meaning:
12641
12642 @table @samp
12643 @item MM
12644 month
12645 @item DD
12646 day within month
12647 @item hh
12648 hour
12649 @item mm
12650 minute
12651 @item CC
12652 first two digits of year (optional)
12653 @item YY
12654 last two digits of year (optional)
12655 @item ss
12656 second (optional)
12657 @end table
12658
12659 The @option{--set} option also sets the system clock; see the next section.
12660
12661
12662 @node Options for date
12663 @subsection Options for @command{date}
12664
12665 @cindex @command{date} options
12666 @cindex options for @command{date}
12667
12668 The program accepts the following options.  Also see @ref{Common options}.
12669
12670 @table @samp
12671
12672 @item -d @var{datestr}
12673 @itemx --date=@var{datestr}
12674 @opindex -d
12675 @opindex --date
12676 @cindex parsing date strings
12677 @cindex date strings, parsing
12678 @cindex arbitrary date strings, parsing
12679 @opindex yesterday
12680 @opindex tomorrow
12681 @opindex next @var{day}
12682 @opindex last @var{day}
12683 Display the date and time specified in @var{datestr} instead of the
12684 current date and time.  @var{datestr} can be in almost any common
12685 format.  It can contain month names, time zones, @samp{am} and @samp{pm},
12686 @samp{yesterday}, etc.  For example, @option{--date="2004-02-27
12687 14:19:13.489392193 +0530"} specifies the instant of time that is
12688 489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
12689 time zone that is 5 hours and 30 minutes east of @acronym{UTC}.
12690 @xref{Date input formats}.
12691
12692 @item -f @var{datefile}
12693 @itemx --file=@var{datefile}
12694 @opindex -f
12695 @opindex --file
12696 Parse each line in @var{datefile} as with @option{-d} and display the
12697 resulting date and time.  If @var{datefile} is @samp{-}, use standard
12698 input.  This is useful when you have many dates to process, because the
12699 system overhead of starting up the @command{date} executable many times can
12700 be considerable.
12701
12702 @item -r @var{file}
12703 @itemx --reference=@var{file}
12704 @opindex -r
12705 @opindex --reference
12706 Display the date and time of the last modification of @var{file},
12707 instead of the current date and time.
12708
12709 @item -R
12710 @itemx --rfc-822
12711 @itemx --rfc-2822
12712 @opindex -R
12713 @opindex --rfc-822
12714 @opindex --rfc-2822
12715 Display the date and time using the format @samp{%a, %d %b %Y %H:%M:%S
12716 %z}, evaluated in the C locale so abbreviations are always in English.
12717 For example:
12718
12719 @example
12720 Fri, 09 Sep 2005 13:51:39 -0700
12721 @end example
12722
12723 This format conforms to
12724 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc2822.txt, Internet
12725 @acronym{RFCs} 2822} and
12726 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc822.txt, 822}, the
12727 current and previous standards for Internet email.
12728
12729 @item --rfc-3339=@var{timespec}
12730 @opindex --rfc-3339=@var{timespec}
12731 Display the date using a format specified by
12732 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3339.txt, Internet
12733 @acronym{RFC} 3339}.  This is a subset of the @acronym{ISO} 8601
12734 format, except that it also permits applications to use a space rather
12735 than a @samp{T} to separate dates from times.  Unlike the other
12736 standard formats, @acronym{RFC} 3339 format is always suitable as
12737 input for the @option{--date} (@option{-d}) and @option{--file}
12738 (@option{-f}) options, regardless of the current locale.
12739
12740 The argument @var{timespec} specifies how much of the time to include.
12741 It can be one of the following:
12742
12743 @table @samp
12744 @item date
12745 Print just the full-date, e.g., @samp{2005-09-14}.
12746 This is equivalent to the format @samp{%Y-%m-%d}.
12747
12748 @item seconds
12749 Print the full-date and full-time separated by a space, e.g.,
12750 @samp{2005-09-14 00:56:06+05:30}.  The output ends with a numeric
12751 time-offset; here the @samp{+05:30} means that local time is five
12752 hours and thirty minutes east of @acronym{UTC}.  This is equivalent to
12753 the format @samp{%Y-%m-%d %H:%M:%S%:z}.
12754
12755 @item ns
12756 Like @samp{seconds}, but also print nanoseconds, e.g.,
12757 @samp{2005-09-14 00:56:06.998458565+05:30}.
12758 This is equivalent to the format @samp{%Y-%m-%d %H:%M:%S.%N%:z}.
12759
12760 @end table
12761
12762 @item -s @var{datestr}
12763 @itemx --set=@var{datestr}
12764 @opindex -s
12765 @opindex --set
12766 Set the date and time to @var{datestr}.  See @option{-d} above.
12767
12768 @item -u
12769 @itemx --utc
12770 @itemx --universal
12771 @opindex -u
12772 @opindex --utc
12773 @opindex --universal
12774 @cindex Coordinated Universal Time
12775 @cindex UTC
12776 @cindex Greenwich Mean Time
12777 @cindex GMT
12778 @vindex TZ
12779 Use Coordinated Universal Time (@acronym{UTC}) by operating as if the
12780 @env{TZ} environment variable were set to the string @samp{UTC0}.
12781 Coordinated
12782 Universal Time is often called ``Greenwich Mean Time'' (@sc{gmt}) for
12783 historical reasons.
12784 @end table
12785
12786
12787 @node Examples of date
12788 @subsection Examples of @command{date}
12789
12790 @cindex examples of @command{date}
12791
12792 Here are a few examples.  Also see the documentation for the @option{-d}
12793 option in the previous section.
12794
12795 @itemize @bullet
12796
12797 @item
12798 To print the date of the day before yesterday:
12799
12800 @example
12801 date --date='2 days ago'
12802 @end example
12803
12804 @item
12805 To print the date of the day three months and one day hence:
12806
12807 @example
12808 date --date='3 months 1 day'
12809 @end example
12810
12811 @item
12812 To print the day of year of Christmas in the current year:
12813
12814 @example
12815 date --date='25 Dec' +%j
12816 @end example
12817
12818 @item
12819 To print the current full month name and the day of the month:
12820
12821 @example
12822 date '+%B %d'
12823 @end example
12824
12825 But this may not be what you want because for the first nine days of
12826 the month, the @samp{%d} expands to a zero-padded two-digit field,
12827 for example @samp{date -d 1may '+%B %d'} will print @samp{May 01}.
12828
12829 @item
12830 To print a date without the leading zero for one-digit days
12831 of the month, you can use the (@acronym{GNU} extension)
12832 @samp{-} flag to suppress
12833 the padding altogether:
12834
12835 @example
12836 date -d 1may '+%B %-d
12837 @end example
12838
12839 @item
12840 To print the current date and time in the format required by many
12841 non-@acronym{GNU} versions of @command{date} when setting the system clock:
12842
12843 @example
12844 date +%m%d%H%M%Y.%S
12845 @end example
12846
12847 @item
12848 To set the system clock forward by two minutes:
12849
12850 @example
12851 date --set='+2 minutes'
12852 @end example
12853
12854 @item
12855 To print the date in @acronym{RFC} 2822 format,
12856 use @samp{date --rfc-2822}.  Here is some example output:
12857
12858 @example
12859 Fri, 09 Sep 2005 13:51:39 -0700
12860 @end example
12861
12862 @anchor{%s-examples}
12863 @item
12864 To convert a date string to the number of seconds since the epoch
12865 (which is 1970-01-01 00:00:00 UTC), use the @option{--date} option with
12866 the @samp{%s} format.  That can be useful in sorting and/or graphing
12867 and/or comparing data by date.  The following command outputs the
12868 number of the seconds since the epoch for the time two minutes after the
12869 epoch:
12870
12871 @example
12872 date --date='1970-01-01 00:02:00 +0000' +%s
12873 120
12874 @end example
12875
12876 If you do not specify time zone information in the date string,
12877 @command{date} uses your computer's idea of the time zone when
12878 interpreting the string.  For example, if your computer's time zone is
12879 that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
12880 seconds) behind UTC:
12881
12882 @example
12883 # local time zone used
12884 date --date='1970-01-01 00:02:00' +%s
12885 18120
12886 @end example
12887
12888 @item
12889 If you're sorting or graphing dated data, your raw date values may be
12890 represented as seconds since the epoch.  But few people can look at
12891 the date @samp{946684800} and casually note ``Oh, that's the first second
12892 of the year 2000 in Greenwich, England.''
12893
12894 @example
12895 date --date='2000-01-01 UTC' +%s
12896 946684800
12897 @end example
12898
12899 An alternative is to use the @option{--utc} (@option{-u}) option.
12900 Then you may omit @samp{UTC} from the date string.  Although this
12901 produces the same result for @samp{%s} and many other format sequences,
12902 with a time zone offset different from zero, it would give a different
12903 result for zone-dependent formats like @samp{%z}.
12904
12905 @example
12906 date -u --date=2000-01-01 +%s
12907 946684800
12908 @end example
12909
12910 To convert such an unwieldy number of seconds back to
12911 a more readable form, use a command like this:
12912
12913 @smallexample
12914 # local time zone used
12915 date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
12916 1999-12-31 19:00:00 -0500
12917 @end smallexample
12918
12919 Or if you do not mind depending on the @samp{@@} feature present since
12920 coreutils 5.3.0, you could shorten this to:
12921
12922 @smallexample
12923 date -d @@946684800 +"%F %T %z"
12924 1999-12-31 19:00:00 -0500
12925 @end smallexample
12926
12927 Often it is better to output UTC-relative date and time:
12928
12929 @smallexample
12930 date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
12931 2000-01-01 00:00:00 +0000
12932 @end smallexample
12933
12934 @end itemize
12935
12936
12937 @node uname invocation
12938 @section @command{uname}: Print system information
12939
12940 @pindex uname
12941 @cindex print system information
12942 @cindex system information, printing
12943
12944 @command{uname} prints information about the machine and operating system
12945 it is run on.  If no options are given, @command{uname} acts as if the
12946 @option{-s} option were given.  Synopsis:
12947
12948 @example
12949 uname [@var{option}]@dots{}
12950 @end example
12951
12952 If multiple options or @option{-a} are given, the selected information is
12953 printed in this order:
12954
12955 @example
12956 @var{kernel-name} @var{nodename} @var{kernel-release} @var{kernel-version}
12957 @var{machine} @var{processor} @var{hardware-platform} @var{operating-system}
12958 @end example
12959
12960 The information may contain internal spaces, so such output cannot be
12961 parsed reliably.  In the following example, @var{release} is
12962 @samp{2.2.18ss.e820-bda652a #4 SMP Tue Jun 5 11:24:08 PDT 2001}:
12963
12964 @smallexample
12965 uname -a
12966 @result{} Linux dum 2.2.18 #4 SMP Tue Jun 5 11:24:08 PDT 2001 i686 unknown unknown GNU/Linux
12967 @end smallexample
12968
12969
12970 The program accepts the following options.  Also see @ref{Common options}.
12971
12972 @table @samp
12973
12974 @item -a
12975 @itemx --all
12976 @opindex -a
12977 @opindex --all
12978 Print all of the below information, except omit the processor type
12979 and the hardware platform name if they are unknown.
12980
12981 @item -i
12982 @itemx --hardware-platform
12983 @opindex -i
12984 @opindex --hardware-platform
12985 @cindex implementation, hardware
12986 @cindex hardware platform
12987 @cindex platform, hardware
12988 Print the hardware platform name
12989 (sometimes called the hardware implementation).
12990 Print @samp{unknown} if the kernel does not make this information
12991 easily available, as is the case with Linux kernels.
12992
12993 @item -m
12994 @itemx --machine
12995 @opindex -m
12996 @opindex --machine
12997 @cindex machine type
12998 @cindex hardware class
12999 @cindex hardware type
13000 Print the machine hardware name (sometimes called the hardware class
13001 or hardware type).
13002
13003 @item -n
13004 @itemx --nodename
13005 @opindex -n
13006 @opindex --nodename
13007 @cindex hostname
13008 @cindex node name
13009 @cindex network node name
13010 Print the network node hostname.
13011
13012 @item -p
13013 @itemx --processor
13014 @opindex -p
13015 @opindex --processor
13016 @cindex host processor type
13017 Print the processor type (sometimes called the instruction set
13018 architecture or ISA).
13019 Print @samp{unknown} if the kernel does not make this information
13020 easily available, as is the case with Linux kernels.
13021
13022 @item -o
13023 @itemx --operating-system
13024 @opindex -o
13025 @opindex --operating-system
13026 @cindex operating system name
13027 Print the name of the operating system.
13028
13029 @item -r
13030 @itemx --kernel-release
13031 @opindex -r
13032 @opindex --kernel-release
13033 @cindex kernel release
13034 @cindex release of kernel
13035 Print the kernel release.
13036
13037 @item -s
13038 @itemx --kernel-name
13039 @opindex -s
13040 @opindex --kernel-name
13041 @cindex kernel name
13042 @cindex name of kernel
13043 Print the kernel name.
13044 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) calls this
13045 ``the implementation of the operating system'', because the
13046 @acronym{POSIX} specification itself has no notion of ``kernel''.
13047 The kernel name might be the same as the operating system name printed
13048 by the @option{-o} or @option{--operating-system} option, but it might
13049 differ.  Some operating systems (e.g., FreeBSD, HP-UX) have the same
13050 name as their underlying kernels; others (e.g., GNU/Linux, Solaris)
13051 do not.
13052
13053 @item -v
13054 @itemx --kernel-version
13055 @opindex -v
13056 @opindex --kernel-version
13057 @cindex kernel version
13058 @cindex version of kernel
13059 Print the kernel version.
13060
13061 @end table
13062
13063 @exitstatus
13064
13065
13066 @node hostname invocation
13067 @section @command{hostname}: Print or set system name
13068
13069 @pindex hostname
13070 @cindex setting the hostname
13071 @cindex printing the hostname
13072 @cindex system name, printing
13073 @cindex appropriate privileges
13074
13075 With no arguments, @command{hostname} prints the name of the current host
13076 system.  With one argument, it sets the current host name to the
13077 specified string.  You must have appropriate privileges to set the host
13078 name.  Synopsis:
13079
13080 @example
13081 hostname [@var{name}]
13082 @end example
13083
13084 The only options are @option{--help} and @option{--version}.  @xref{Common
13085 options}.
13086
13087 @exitstatus
13088
13089
13090 @node hostid invocation
13091 @section @command{hostid}: Print numeric host identifier.
13092
13093 @pindex hostid
13094 @cindex printing the host identifier
13095
13096 @command{hostid} prints the numeric identifier of the current host
13097 in hexadecimal.  This command accepts no arguments.
13098 The only options are @option{--help} and @option{--version}.
13099 @xref{Common options}.
13100
13101 For example, here's what it prints on one system I use:
13102
13103 @example
13104 $ hostid
13105 1bac013d
13106 @end example
13107
13108 On that system, the 32-bit quantity happens to be closely
13109 related to the system's Internet address, but that isn't always
13110 the case.
13111
13112 @exitstatus
13113
13114
13115 @node Modified command invocation
13116 @chapter Modified command invocation
13117
13118 @cindex modified command invocation
13119 @cindex invocation of commands, modified
13120 @cindex commands for invoking other commands
13121
13122 This section describes commands that run other commands in some context
13123 different than the current one: a modified environment, as a different
13124 user, etc.
13125
13126 @menu
13127 * chroot invocation::           Modify the root directory.
13128 * env invocation::              Modify environment variables.
13129 * nice invocation::             Modify niceness.
13130 * nohup invocation::            Immunize to hangups.
13131 * su invocation::               Modify user and group ID.
13132 @end menu
13133
13134
13135 @node chroot invocation
13136 @section @command{chroot}: Run a command with a different root directory
13137
13138 @pindex chroot
13139 @cindex running a program in a specified root directory
13140 @cindex root directory, running a program in a specified
13141
13142 @command{chroot} runs a command with a specified root directory.
13143 On many systems, only the super-user can do this.
13144 Synopses:
13145
13146 @example
13147 chroot @var{newroot} [@var{command} [@var{args}]@dots{}]
13148 chroot @var{option}
13149 @end example
13150
13151 Ordinarily, file names are looked up starting at the root of the
13152 directory structure, i.e., @file{/}.  @command{chroot} changes the root to
13153 the directory @var{newroot} (which must exist) and then runs
13154 @var{command} with optional @var{args}.  If @var{command} is not
13155 specified, the default is the value of the @env{SHELL} environment
13156 variable or @command{/bin/sh} if not set, invoked with the @option{-i} option.
13157 @var{command} must not be a special built-in utility
13158 (@pxref{Special built-in utilities}).
13159
13160 The only options are @option{--help} and @option{--version}.  @xref{Common
13161 options}.  Options must precede operands.
13162
13163 Here are a few tips to help avoid common problems in using chroot.
13164 To start with a simple example, make @var{command} refer to a statically
13165 linked binary.  If you were to use a dynamically linked executable, then
13166 you'd have to arrange to have the shared libraries in the right place under
13167 your new root directory.
13168
13169 For example, if you create a statically linked @command{ls} executable,
13170 and put it in @file{/tmp/empty}, you can run this command as root:
13171
13172 @example
13173 $ chroot /tmp/empty /ls -Rl /
13174 @end example
13175
13176 Then you'll see output like this:
13177
13178 @example
13179 /:
13180 total 1023
13181 -rwxr-xr-x 1 0 0 1041745 Aug 16 11:17 ls
13182 @end example
13183
13184 If you want to use a dynamically linked executable, say @command{bash},
13185 then first run @samp{ldd bash} to see what shared objects it needs.
13186 Then, in addition to copying the actual binary, also copy the listed
13187 files to the required positions under your intended new root directory.
13188 Finally, if the executable requires any other files (e.g., data, state,
13189 device files), copy them into place, too.
13190
13191 @cindex exit status of @command{chroot}
13192 Exit status:
13193
13194 @display
13195 1   if @command{chroot} itself fails
13196 126 if @var{command} is found but cannot be invoked
13197 127 if @var{command} cannot be found
13198 the exit status of @var{command} otherwise
13199 @end display
13200
13201
13202 @node env invocation
13203 @section @command{env}: Run a command in a modified environment
13204
13205 @pindex env
13206 @cindex environment, running a program in a modified
13207 @cindex modified environment, running a program in a
13208 @cindex running a program in a modified environment
13209
13210 @command{env} runs a command with a modified environment.  Synopses:
13211
13212 @example
13213 env [@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c
13214 [@var{command} [@var{args}]@dots{}]
13215 env
13216 @end example
13217
13218 Operands of the form @samp{@var{variable}=@var{value}} set
13219 the environment variable @var{variable} to value @var{value}.
13220 @var{value} may be empty (@samp{@var{variable}=}).  Setting a variable
13221 to an empty value is different from unsetting it.
13222 These operands are evaluated left-to-right, so if two operands
13223 mention the same variable the earlier is ignored.
13224
13225 Environment variable names can be empty, and can contain any
13226 characters other than @samp{=} and the null character (@acronym{ASCII}
13227 @sc{nul}).  However, it is wise to limit yourself to names that
13228 consist solely of underscores, digits, and @acronym{ASCII} letters,
13229 and that begin with a non-digit, as applications like the shell do not
13230 work well with other names.
13231
13232 @vindex PATH
13233 The first operand that does not contain the character @samp{=}
13234 specifies the program to invoke; it is
13235 searched for according to the @env{PATH} environment variable.  Any
13236 remaining arguments are passed as arguments to that program.
13237 The program should not be a special built-in utility
13238 (@pxref{Special built-in utilities}).
13239
13240 @cindex environment, printing
13241
13242 If no command name is specified following the environment
13243 specifications, the resulting environment is printed.  This is like
13244 specifying the @command{printenv} program.
13245
13246 The program accepts the following options.  Also see @ref{Common options}.
13247 Options must precede operands.
13248
13249 @table @samp
13250
13251 @item -u @var{name}
13252 @itemx --unset=@var{name}
13253 @opindex -u
13254 @opindex --unset
13255 Remove variable @var{name} from the environment, if it was in the
13256 environment.
13257
13258 @item -
13259 @itemx -i
13260 @itemx --ignore-environment
13261 @opindex -
13262 @opindex -i
13263 @opindex --ignore-environment
13264 Start with an empty environment, ignoring the inherited environment.
13265
13266 @end table
13267
13268 @cindex exit status of @command{env}
13269 Exit status:
13270
13271 @display
13272 0   if no @var{command} is specified and the environment is output
13273 1   if @command{env} itself fails
13274 126 if @var{command} is found but cannot be invoked
13275 127 if @var{command} cannot be found
13276 the exit status of @var{command} otherwise
13277 @end display
13278
13279
13280 @node nice invocation
13281 @section @command{nice}: Run a command with modified niceness
13282
13283 @pindex nice
13284 @cindex niceness
13285 @cindex scheduling, affecting
13286 @cindex appropriate privileges
13287
13288 @command{nice} prints or modifies a process's @dfn{niceness},
13289 a parameter that affects whether the process is scheduled favorably.
13290 Synopsis:
13291
13292 @example
13293 nice [@var{option}]@dots{} [@var{command} [@var{arg}]@dots{}]
13294 @end example
13295
13296 If no arguments are given, @command{nice} prints the current niceness.
13297 Otherwise, @command{nice} runs the given @var{command} with its
13298 niceness adjusted.  By default, its niceness is incremented by 10.
13299
13300 Nicenesses range at least from @minus{}20 (resulting in the most
13301 favorable scheduling) through 19 (the least favorable).  Some systems
13302 may have a wider range of nicenesses; conversely, other systems may
13303 enforce more restrictive limits.  An attempt to set the niceness
13304 outside the supported range is treated as an attempt to use the
13305 minimum or maximum supported value.
13306
13307 A niceness should not be confused with a scheduling priority, which
13308 lets applications determine the order in which threads are scheduled
13309 to run.  Unlike a priority, a niceness is merely advice to the
13310 scheduler, which the scheduler is free to ignore.  Also, as a point of
13311 terminology, @acronym{POSIX} defines the behavior of @command{nice} in
13312 terms of a @dfn{nice value}, which is the nonnegative difference
13313 between a niceness and the minimum niceness.  Though @command{nice}
13314 conforms to @acronym{POSIX}, its documentation and diagnostics use the
13315 term ``niceness'' for compatibility with historical practice.
13316
13317 @var{command} must not be a special built-in utility (@pxref{Special
13318 built-in utilities}).
13319
13320 @cindex conflicts with shell built-ins
13321 @cindex built-in shell commands, conflicts with
13322 Because many shells have a built-in @command{nice} command, using an
13323 unadorned @command{nice} in a script or interactively may get you
13324 different functionality than that described here.
13325
13326 The program accepts the following option.  Also see @ref{Common options}.
13327 Options must precede operands.
13328
13329 @table @samp
13330 @item -n @var{adjustment}
13331 @itemx --adjustment=@var{adjustment}
13332 @opindex -n
13333 @opindex --adjustment
13334 Add @var{adjustment} instead of 10 to the command's niceness.  If
13335 @var{adjustment} is negative and you lack appropriate privileges,
13336 @command{nice} issues a warning but otherwise acts as if you specified
13337 a zero adjustment.
13338
13339 For compatibility @command{nice} also supports an obsolete
13340 option syntax @option{-@var{adjustment}}.  New scripts should use
13341 @option{-n @var{adjustment}} instead.
13342
13343 @end table
13344
13345 @cindex exit status of @command{nice}
13346 Exit status:
13347
13348 @display
13349 0   if no @var{command} is specified and the niceness is output
13350 1   if @command{nice} itself fails
13351 126 if @var{command} is found but cannot be invoked
13352 127 if @var{command} cannot be found
13353 the exit status of @var{command} otherwise
13354 @end display
13355
13356 It is sometimes useful to run a non-interactive program with reduced niceness.
13357
13358 @example
13359 $ nice factor 4611686018427387903
13360 @end example
13361
13362 Since @command{nice} prints the current niceness,
13363 you can invoke it through itself to demonstrate how it works.
13364
13365 The default behavior is to increase the niceness by @samp{10}:
13366
13367 @example
13368 $ nice
13369 0
13370 $ nice nice
13371 10
13372 $ nice -n 10 nice
13373 10
13374 @end example
13375
13376 The @var{adjustment} is relative to the current niceness.  In the
13377 next example, the first @command{nice} invocation runs the second one
13378 with niceness 10, and it in turn runs the final one with a niceness
13379 that is 3 more:
13380
13381 @example
13382 $ nice nice -n 3 nice
13383 13
13384 @end example
13385
13386 Specifying a niceness larger than the supported range
13387 is the same as specifying the maximum supported value:
13388
13389 @example
13390 $ nice -n 10000000000 nice
13391 19
13392 @end example
13393
13394 Only a privileged user may run a process with lower niceness:
13395
13396 @example
13397 $ nice -n -1 nice
13398 nice: cannot set niceness: Permission denied
13399 0
13400 $ sudo nice -n -1 nice
13401 -1
13402 @end example
13403
13404
13405 @node nohup invocation
13406 @section @command{nohup}: Run a command immune to hangups
13407
13408 @pindex nohup
13409 @cindex hangups, immunity to
13410 @cindex immunity to hangups
13411 @cindex logging out and continuing to run
13412
13413 @flindex nohup.out
13414 @command{nohup} runs the given @var{command} with hangup signals ignored,
13415 so that the command can continue running in the background after you log
13416 out.  Synopsis:
13417
13418 @example
13419 nohup @var{command} [@var{arg}]@dots{}
13420 @end example
13421
13422 If standard input is a terminal, it is redirected from
13423 @file{/dev/null} so that terminal sessions do not mistakenly consider
13424 the terminal to be used by the command.  This is a @acronym{GNU}
13425 extension; programs intended to be portable to non-@acronym{GNU} hosts
13426 should use @samp{nohup @var{command} [@var{arg}]@dots{} </dev/null}
13427 instead.
13428
13429 @flindex nohup.out
13430 If standard output is a terminal, the command's standard output is appended
13431 to the file @file{nohup.out}; if that cannot be written to, it is appended
13432 to the file @file{$HOME/nohup.out}; and if that cannot be written to, the
13433 command is not run.
13434 Any @file{nohup.out} or @file{$HOME/nohup.out} file created by
13435 @command{nohup} is made readable and writable only to the user,
13436 regardless of the current umask settings.
13437
13438 If standard error is a terminal, it is normally redirected to the same file
13439 descriptor as the (possibly-redirected) standard output.
13440 However, if standard output is closed, standard error terminal output
13441 is instead appended to the file @file{nohup.out} or
13442 @file{$HOME/nohup.out} as above.
13443
13444 @command{nohup} does not automatically put the command it runs in the
13445 background; you must do that explicitly, by ending the command line
13446 with an @samp{&}.  Also, @command{nohup} does not alter the
13447 niceness of @var{command}; use @command{nice} for that,
13448 e.g., @samp{nohup nice @var{command}}.
13449
13450 @var{command} must not be a special built-in utility (@pxref{Special
13451 built-in utilities}).
13452
13453 The only options are @option{--help} and @option{--version}.  @xref{Common
13454 options}.  Options must precede operands.
13455
13456 @cindex exit status of @command{nohup}
13457 Exit status:
13458
13459 @display
13460 126 if @var{command} is found but cannot be invoked
13461 127 if @command{nohup} itself fails or if @var{command} cannot be found
13462 the exit status of @var{command} otherwise
13463 @end display
13464
13465
13466 @node su invocation
13467 @section @command{su}: Run a command with substitute user and group ID
13468
13469 @pindex su
13470 @cindex substitute user and group IDs
13471 @cindex user ID, switching
13472 @cindex super-user, becoming
13473 @cindex root, becoming
13474
13475 @command{su} allows one user to temporarily become another user.  It runs a
13476 command (often an interactive shell) with the real and effective user
13477 ID, group ID, and supplemental groups of a given @var{user}.  Synopsis:
13478
13479 @example
13480 su [@var{option}]@dots{} [@var{user} [@var{arg}]@dots{}]
13481 @end example
13482
13483 @cindex passwd entry, and @command{su} shell
13484 @flindex /bin/sh
13485 @flindex /etc/passwd
13486 If no @var{user} is given, the default is @code{root}, the super-user.
13487 The shell to use is taken from @var{user}'s @code{passwd} entry, or
13488 @file{/bin/sh} if none is specified there.  If @var{user} has a
13489 password, @command{su} prompts for the password unless run by a user with
13490 effective user ID of zero (the super-user).
13491
13492 @vindex HOME
13493 @vindex SHELL
13494 @vindex USER
13495 @vindex LOGNAME
13496 @cindex login shell
13497 By default, @command{su} does not change the current directory.
13498 It sets the environment variables @env{HOME} and @env{SHELL}
13499 from the password entry for @var{user}, and if @var{user} is not
13500 the super-user, sets @env{USER} and @env{LOGNAME} to @var{user}.
13501 By default, the shell is not a login shell.
13502
13503 Any additional @var{arg}s are passed as additional arguments to the
13504 shell.
13505
13506 @cindex @option{-su}
13507 GNU @command{su} does not treat @file{/bin/sh} or any other shells specially
13508 (e.g., by setting @code{argv[0]} to @option{-su}, passing @option{-c} only
13509 to certain shells, etc.).
13510
13511 @findex syslog
13512 @command{su} can optionally be compiled to use @code{syslog} to report
13513 failed, and optionally successful, @command{su} attempts.  (If the system
13514 supports @code{syslog}.)  However, GNU @command{su} does not check if the
13515 user is a member of the @code{wheel} group; see below.
13516
13517 The program accepts the following options.  Also see @ref{Common options}.
13518
13519 @table @samp
13520 @item -c @var{command}
13521 @itemx --command=@var{command}
13522 @opindex -c
13523 @opindex --command
13524 Pass @var{command}, a single command line to run, to the shell with
13525 a @option{-c} option instead of starting an interactive shell.
13526
13527 @item -f
13528 @itemx --fast
13529 @opindex -f
13530 @opindex --fast
13531 @flindex .cshrc
13532 @cindex file name pattern expansion, disabled
13533 @cindex globbing, disabled
13534 Pass the @option{-f} option to the shell.  This probably only makes sense
13535 if the shell run is @command{csh} or @command{tcsh}, for which the @option{-f}
13536 option prevents reading the startup file (@file{.cshrc}).  With
13537 Bourne-like shells, the @option{-f} option disables file name pattern
13538 expansion (globbing), which is not likely to be useful.
13539
13540 @item -
13541 @itemx -l
13542 @itemx --login
13543 @opindex -
13544 @opindex -l
13545 @opindex --login
13546 @c other variables already indexed above
13547 @vindex TERM
13548 @vindex PATH
13549 @cindex login shell, creating
13550 Make the shell a login shell.  This means the following.  Unset all
13551 environment variables except @env{TERM}, @env{HOME}, and @env{SHELL}
13552 (which are set as described above), and @env{USER} and @env{LOGNAME}
13553 (which are set, even for the super-user, as described above), and set
13554 @env{PATH} to a compiled-in default value.  Change to @var{user}'s home
13555 directory.  Prepend @samp{-} to the shell's name, intended to make it
13556 read its login startup file(s).
13557
13558 @item -m
13559 @itemx -p
13560 @itemx --preserve-environment
13561 @opindex -m
13562 @opindex -p
13563 @opindex --preserve-environment
13564 @cindex environment, preserving
13565 @flindex /etc/shells
13566 @cindex restricted shell
13567 Do not change the environment variables @env{HOME}, @env{USER},
13568 @env{LOGNAME}, or @env{SHELL}.  Run the shell given in the environment
13569 variable @env{SHELL} instead of the shell from @var{user}'s passwd
13570 entry, unless the user running @command{su} is not the super-user and
13571 @var{user}'s shell is restricted.  A @dfn{restricted shell} is one that
13572 is not listed in the file @file{/etc/shells}, or in a compiled-in list
13573 if that file does not exist.  Parts of what this option does can be
13574 overridden by @option{--login} and @option{--shell}.
13575
13576 @item -s @var{shell}
13577 @itemx --shell=@var{shell}
13578 @opindex -s
13579 @opindex --shell
13580 Run @var{shell} instead of the shell from @var{user}'s passwd entry,
13581 unless the user running @command{su} is not the super-user and @var{user}'s
13582 shell is restricted (see @option{-m} just above).
13583
13584 @end table
13585
13586 @cindex exit status of @command{su}
13587 Exit status:
13588
13589 @display
13590 1   if @command{su} itself fails
13591 126 if subshell is found but cannot be invoked
13592 127 if subshell cannot be found
13593 the exit status of the subshell otherwise
13594 @end display
13595
13596 @cindex wheel group, not supported
13597 @cindex group wheel, not supported
13598 @cindex fascism
13599 @subsection Why GNU @command{su} does not support the @samp{wheel} group
13600
13601 (This section is by Richard Stallman.)
13602
13603 @cindex Twenex
13604 @cindex MIT AI lab
13605 Sometimes a few of the users try to hold total power over all the
13606 rest.  For example, in 1984, a few users at the MIT AI lab decided to
13607 seize power by changing the operator password on the Twenex system and
13608 keeping it secret from everyone else.  (I was able to thwart this coup
13609 and give power back to the users by patching the kernel, but I
13610 wouldn't know how to do that in Unix.)
13611
13612 However, occasionally the rulers do tell someone.  Under the usual
13613 @command{su} mechanism, once someone learns the root password who
13614 sympathizes with the ordinary users, he or she can tell the rest.  The
13615 ``wheel group'' feature would make this impossible, and thus cement the
13616 power of the rulers.
13617
13618 I'm on the side of the masses, not that of the rulers.  If you are
13619 used to supporting the bosses and sysadmins in whatever they do, you
13620 might find this idea strange at first.
13621
13622
13623 @node Process control
13624 @chapter Process control
13625
13626 @cindex processes, commands for controlling
13627 @cindex commands for controlling processes
13628
13629 @menu
13630 * kill invocation::             Sending a signal to processes.
13631 @end menu
13632
13633
13634 @node kill invocation
13635 @section @command{kill}: Send a signal to processes
13636
13637 @pindex kill
13638 @cindex send a signal to processes
13639
13640 The @command{kill} command sends a signal to processes, causing them
13641 to terminate or otherwise act upon receiving the signal in some way.
13642 Alternatively, it lists information about signals.  Synopses:
13643
13644 @example
13645 kill [-s @var{signal} | --signal @var{signal} | -@var{signal}] @var{pid}@dots{}
13646 kill [-l | --list | -t | --table] [@var{signal}]@dots{}
13647 @end example
13648
13649 The first form of the @command{kill} command sends a signal to all
13650 @var{pid} arguments.  The default signal to send if none is specified
13651 is @samp{TERM}.  The special signal number @samp{0} does not denote a
13652 valid signal, but can be used to test whether the @var{pid} arguments
13653 specify processes to which a signal could be sent.
13654
13655 If @var{pid} is positive, the signal is sent to the process with the
13656 process ID @var{pid}.  If @var{pid} is zero, the signal is sent to all
13657 processes in the process group of the current process.  If @var{pid}
13658 is @minus{}1, the signal is sent to all processes for which the user has
13659 permission to send a signal.  If @var{pid} is less than @minus{}1, the signal
13660 is sent to all processes in the process group that equals the absolute
13661 value of @var{pid}.
13662
13663 If @var{pid} is not positive, a system-dependent set of system
13664 processes is excluded from the list of processes to which the signal
13665 is sent.
13666
13667 If a negative @var{PID} argument is desired as the first one, it
13668 should be preceded by @option{--}.  However, as a common extension to
13669 @acronym{POSIX}, @option{--} is not required with @samp{kill
13670 -@var{signal} -@var{pid}}.  The following commands are equivalent:
13671
13672 @example
13673 kill -15 -1
13674 kill -TERM -1
13675 kill -s TERM -- -1
13676 kill -- -1
13677 @end example
13678
13679 The first form of the @command{kill} command succeeds if every @var{pid}
13680 argument specifies at least one process that the signal was sent to.
13681
13682 The second form of the @command{kill} command lists signal information.
13683 Either the @option{-l} or @option{--list} option, or the @option{-t}
13684 or @option{--table} option must be specified.  Without any
13685 @var{signal} argument, all supported signals are listed.  The output
13686 of @option{-l} or @option{--list} is a list of the signal names, one
13687 per line; if @var{signal} is already a name, the signal number is
13688 printed instead.  The output of @option{-t} or @option{--table} is a
13689 table of signal numbers, names, and descriptions.  This form of the
13690 @command{kill} command succeeds if all @var{signal} arguments are valid
13691 and if there is no output error.
13692
13693 The @command{kill} command also supports the @option{--help} and
13694 @option{--version} options.  @xref{Common options}.
13695
13696 A @var{signal} may be a signal name like @samp{HUP}, or a signal
13697 number like @samp{1}, or an exit status of a process terminated by the
13698 signal.  A signal name can be given in canonical form or prefixed by
13699 @samp{SIG}.  The case of the letters is ignored, except for the
13700 @option{-@var{signal}} option which must use upper case to avoid
13701 ambiguity with lower case option letters.  The following signal names
13702 and numbers are supported on all @acronym{POSIX} compliant systems:
13703
13704 @table @samp
13705 @item HUP
13706 1.  Hangup.
13707 @item INT
13708 2.  Terminal interrupt.
13709 @item QUIT
13710 3.  Terminal quit.
13711 @item ABRT
13712 6.  Process abort.
13713 @item KILL
13714 9.  Kill (cannot be caught or ignored).
13715 @item ALRM
13716 14.  Alarm Clock.
13717 @item TERM
13718 15.  Termination.
13719 @end table
13720
13721 @noindent
13722 Other supported signal names have system-dependent corresponding
13723 numbers.  All systems conforming to @acronym{POSIX} 1003.1-2001 also
13724 support the following signals:
13725
13726 @table @samp
13727 @item BUS
13728 Access to an undefined portion of a memory object.
13729 @item CHLD
13730 Child process terminated, stopped, or continued.
13731 @item CONT
13732 Continue executing, if stopped.
13733 @item FPE
13734 Erroneous arithmetic operation.
13735 @item ILL
13736 Illegal Instruction.
13737 @item PIPE
13738 Write on a pipe with no one to read it.
13739 @item SEGV
13740 Invalid memory reference.
13741 @item STOP
13742 Stop executing (cannot be caught or ignored).
13743 @item TSTP
13744 Terminal stop.
13745 @item TTIN
13746 Background process attempting read.
13747 @item TTOU
13748 Background process attempting write.
13749 @item URG
13750 High bandwidth data is available at a socket.
13751 @item USR1
13752 User-defined signal 1.
13753 @item USR2
13754 User-defined signal 2.
13755 @end table
13756
13757 @noindent
13758 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XSI} extension
13759 also support the following signals:
13760
13761 @table @samp
13762 @item POLL
13763 Pollable event.
13764 @item PROF
13765 Profiling timer expired.
13766 @item SYS
13767 Bad system call.
13768 @item TRAP
13769 Trace/breakpoint trap.
13770 @item VTALRM
13771 Virtual timer expired.
13772 @item XCPU
13773 CPU time limit exceeded.
13774 @item XFSZ
13775 File size limit exceeded.
13776 @end table
13777
13778 @noindent
13779 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XRT} extension
13780 also support at least eight real-time signals called @samp{RTMIN},
13781 @samp{RTMIN+1}, @dots{}, @samp{RTMAX-1}, @samp{RTMAX}.
13782
13783
13784 @node Delaying
13785 @chapter Delaying
13786
13787 @cindex delaying commands
13788 @cindex commands for delaying
13789
13790 @c Perhaps @command{wait} or other commands should be described here also?
13791
13792 @menu
13793 * sleep invocation::            Delay for a specified time.
13794 @end menu
13795
13796
13797 @node sleep invocation
13798 @section @command{sleep}: Delay for a specified time
13799
13800 @pindex sleep
13801 @cindex delay for a specified time
13802
13803 @command{sleep} pauses for an amount of time specified by the sum of
13804 the values of the command line arguments.
13805 Synopsis:
13806
13807 @example
13808 sleep @var{number}[smhd]@dots{}
13809 @end example
13810
13811 @cindex time units
13812 Each argument is a number followed by an optional unit; the default
13813 is seconds.  The units are:
13814
13815 @table @samp
13816 @item s
13817 seconds
13818 @item m
13819 minutes
13820 @item h
13821 hours
13822 @item d
13823 days
13824 @end table
13825
13826 Historical implementations of @command{sleep} have required that
13827 @var{number} be an integer, and only accepted a single argument
13828 without a suffix.  However, GNU @command{sleep} accepts
13829 arbitrary floating point numbers (using a period before any fractional
13830 digits).
13831
13832 The only options are @option{--help} and @option{--version}.  @xref{Common
13833 options}.
13834
13835 @exitstatus
13836
13837
13838 @node Numeric operations
13839 @chapter Numeric operations
13840
13841 @cindex numeric operations
13842 These programs do numerically-related operations.
13843
13844 @menu
13845 * factor invocation::              Show factors of numbers.
13846 * seq invocation::                 Print sequences of numbers.
13847 @end menu
13848
13849
13850 @node factor invocation
13851 @section @command{factor}: Print prime factors
13852
13853 @pindex factor
13854 @cindex prime factors
13855
13856 @command{factor} prints prime factors.  Synopses:
13857
13858 @example
13859 factor [@var{number}]@dots{}
13860 factor @var{option}
13861 @end example
13862
13863 If no @var{number} is specified on the command line, @command{factor} reads
13864 numbers from standard input, delimited by newlines, tabs, or spaces.
13865
13866 The only options are @option{--help} and @option{--version}.  @xref{Common
13867 options}.
13868
13869 The algorithm it uses is not very sophisticated, so for some inputs
13870 @command{factor} runs for a long time.  The hardest numbers to factor are
13871 the products of large primes.  Factoring the product of the two largest 32-bit
13872 prime numbers takes about 80 seconds of CPU time on a 1.6 GHz Athlon.
13873
13874 @example
13875 $ p=`echo '4294967279 * 4294967291'|bc`
13876 $ factor $p
13877 18446743979220271189: 4294967279 4294967291
13878 @end example
13879
13880 Similarly, it takes about 80 seconds for GNU factor (from coreutils-5.1.2)
13881 to ``factor'' the largest 64-bit prime:
13882
13883 @example
13884 $ factor 18446744073709551557
13885   18446744073709551557: 18446744073709551557
13886 @end example
13887
13888 In contrast, @command{factor} factors the largest 64-bit number in just
13889 over a tenth of a second:
13890
13891 @example
13892 $ factor `echo '2^64-1'|bc`
13893 18446744073709551615: 3 5 17 257 641 65537 6700417
13894 @end example
13895
13896 @exitstatus
13897
13898
13899 @node seq invocation
13900 @section @command{seq}: Print numeric sequences
13901
13902 @pindex seq
13903 @cindex numeric sequences
13904 @cindex sequence of numbers
13905
13906 @command{seq} prints a sequence of numbers to standard output.  Synopses:
13907
13908 @example
13909 seq [@var{option}]@dots{} @var{last}
13910 seq [@var{option}]@dots{} @var{first} @var{last}
13911 seq [@var{option}]@dots{} @var{first} @var{increment} @var{last}
13912 @end example
13913
13914 @command{seq} prints the numbers from @var{first} to @var{last} by
13915 @var{increment}.  By default, each number is printed on a separate line.
13916 When @var{increment} is not specified, it defaults to @samp{1},
13917 even when @var{first} is larger than @var{last}.
13918 @var{first} also defaults to @samp{1}.  So @code{seq 1} prints
13919 @samp{1}, but @code{seq 0} and @code{seq 10 5} produce no output.
13920 Floating-point numbers
13921 may be specified (using a period before any fractional digits).
13922
13923 The program accepts the following options.  Also see @ref{Common options}.
13924 Options must precede operands.
13925
13926 @table @samp
13927 @item -f @var{format}
13928 @itemx --format=@var{format}
13929 @opindex -f @var{format}
13930 @opindex --format=@var{format}
13931 @cindex formatting of numbers in @command{seq}
13932 Print all numbers using @var{format}.
13933 @var{format} must contain exactly one of the @samp{printf}-style
13934 floating point conversion specifications @samp{%a}, @samp{%e},
13935 @samp{%f}, @samp{%g}, @samp{%A}, @samp{%E}, @samp{%F}, @samp{%G}.
13936 The @samp{%} may be followed by zero or more flags taken from the set
13937 @samp{-+#0 '}, then an optional width containing one or more digits,
13938 then an optional precision consisting of a @samp{.} followed by zero
13939 or more digits.  @var{format} may also contain any number of @samp{%%}
13940 conversion specifications.  All conversion specifications have the
13941 same meaning as with @samp{printf}.
13942
13943 The default format is derived from @var{first}, @var{step}, and
13944 @var{last}.  If these all use a fixed point decimal representation,
13945 the default format is @samp{%.@var{p}f}, where @var{p} is the minimum
13946 precision that can represent the output numbers exactly.  Otherwise,
13947 the default format is @samp{%g}.
13948
13949 @item -s @var{string}
13950 @itemx --separator=@var{string}
13951 @cindex separator for numbers in @command{seq}
13952 Separate numbers with @var{string}; default is a newline.
13953 The output always terminates with a newline.
13954
13955 @item -w
13956 @itemx --equal-width
13957 Print all numbers with the same width, by padding with leading zeros.
13958 @var{first}, @var{step}, and @var{last} should all use a fixed point
13959 decimal representation.
13960 (To have other kinds of padding, use @option{--format}).
13961
13962 @end table
13963
13964 You can get finer-grained control over output with @option{-f}:
13965
13966 @example
13967 $ seq -f '(%9.2E)' -9e5 1.1e6 1.3e6
13968 (-9.00E+05)
13969 ( 2.00E+05)
13970 ( 1.30E+06)
13971 @end example
13972
13973 If you want hexadecimal integer output, you can use @command{printf}
13974 to perform the conversion:
13975
13976 @example
13977 $ printf '%x\n' `seq 1048575 1024 1050623`
13978 fffff
13979 1003ff
13980 1007ff
13981 @end example
13982
13983 For very long lists of numbers, use xargs to avoid
13984 system limitations on the length of an argument list:
13985
13986 @example
13987 $ seq 1000000 | xargs printf '%x\n' | tail -n 3
13988 f423e
13989 f423f
13990 f4240
13991 @end example
13992
13993 To generate octal output, use the printf @code{%o} format instead
13994 of @code{%x}.
13995
13996 On most systems, seq can produce whole-number output for values up to
13997 at least @code{2^53}.  Larger integers are approximated.  The details
13998 differ depending on your floating-point implementation, but a common
13999 case is that @command{seq} works with integers through @code{2^64},
14000 and larger integers may not be numerically correct:
14001
14002 @example
14003 $ seq 18446744073709551616 1 18446744073709551618
14004 18446744073709551616
14005 18446744073709551616
14006 18446744073709551618
14007 @end example
14008
14009 Be careful when using @command{seq} with a fractional @var{increment};
14010 otherwise you may see surprising results.  Most people would expect to
14011 see @code{0.000003} printed as the last number in this example:
14012
14013 @example
14014 $ seq -s ' ' 0 0.000001 0.000003
14015 0.000000 0.000001 0.000002
14016 @end example
14017
14018 But that doesn't happen on many systems because @command{seq} is
14019 implemented using binary floating point arithmetic (via the C
14020 @code{long double} type)---which means decimal fractions like @code{0.000001}
14021 cannot be represented exactly.  That in turn means some nonintuitive
14022 conditions like @w{@code{0.000001 * 3 > 0.000003}} will end up being true.
14023
14024 To work around that in the above example, use a slightly larger number as
14025 the @var{last} value:
14026
14027 @example
14028 $ seq -s ' ' 0 0.000001 0.0000031
14029 0.000000 0.000001 0.000002 0.000003
14030 @end example
14031
14032 In general, when using an @var{increment} with a fractional part, where
14033 (@var{last} - @var{first}) / @var{increment} is (mathematically) a whole
14034 number, specify a slightly larger (or smaller, if @var{increment} is negative)
14035 value for @var{last} to ensure that @var{last} is the final value printed
14036 by seq.
14037
14038 @exitstatus
14039
14040
14041 @node File permissions
14042 @chapter File permissions
14043 @include perm.texi
14044
14045 @include getdate.texi
14046
14047 @c              What's GNU?
14048 @c              Arnold Robbins
14049 @node Opening the software toolbox
14050 @chapter Opening the Software Toolbox
14051
14052 An earlier version of this chapter appeared in
14053 @uref{http://www.linuxjournal.com/article.php?sid=2762, the
14054 @cite{What's GNU?} column of @cite{Linux Journal}, 2 (June, 1994)}.
14055 It was written by Arnold Robbins.
14056
14057 @menu
14058 * Toolbox introduction::        Toolbox introduction
14059 * I/O redirection::             I/O redirection
14060 * The who command::             The @command{who} command
14061 * The cut command::             The @command{cut} command
14062 * The sort command::            The @command{sort} command
14063 * The uniq command::            The @command{uniq} command
14064 * Putting the tools together::  Putting the tools together
14065 @end menu
14066
14067
14068 @node Toolbox introduction
14069 @unnumberedsec Toolbox Introduction
14070
14071 This month's column is only peripherally related to the GNU Project, in
14072 that it describes a number of the GNU tools on your GNU/Linux system and how they
14073 might be used.  What it's really about is the ``Software Tools'' philosophy
14074 of program development and usage.
14075
14076 The software tools philosophy was an important and integral concept
14077 in the initial design and development of Unix (of which Linux and GNU are
14078 essentially clones).  Unfortunately, in the modern day press of
14079 Internetworking and flashy GUIs, it seems to have fallen by the
14080 wayside.  This is a shame, since it provides a powerful mental model
14081 for solving many kinds of problems.
14082
14083 Many people carry a Swiss Army knife around in their pants pockets (or
14084 purse).  A Swiss Army knife is a handy tool to have: it has several knife
14085 blades, a screwdriver, tweezers, toothpick, nail file, corkscrew, and perhaps
14086 a number of other things on it.  For the everyday, small miscellaneous jobs
14087 where you need a simple, general purpose tool, it's just the thing.
14088
14089 On the other hand, an experienced carpenter doesn't build a house using
14090 a Swiss Army knife.  Instead, he has a toolbox chock full of specialized
14091 tools---a saw, a hammer, a screwdriver, a plane, and so on.  And he knows
14092 exactly when and where to use each tool; you won't catch him hammering nails
14093 with the handle of his screwdriver.
14094
14095 The Unix developers at Bell Labs were all professional programmers and trained
14096 computer scientists.  They had found that while a one-size-fits-all program
14097 might appeal to a user because there's only one program to use, in practice
14098 such programs are
14099
14100 @enumerate a
14101 @item
14102 difficult to write,
14103
14104 @item
14105 difficult to maintain and
14106 debug, and
14107
14108 @item
14109 difficult to extend to meet new situations.
14110 @end enumerate
14111
14112 Instead, they felt that programs should be specialized tools.  In short, each
14113 program ``should do one thing well.''  No more and no less.  Such programs are
14114 simpler to design, write, and get right---they only do one thing.
14115
14116 Furthermore, they found that with the right machinery for hooking programs
14117 together, that the whole was greater than the sum of the parts.  By combining
14118 several special purpose programs, you could accomplish a specific task
14119 that none of the programs was designed for, and accomplish it much more
14120 quickly and easily than if you had to write a special purpose program.
14121 We will see some (classic) examples of this further on in the column.
14122 (An important additional point was that, if necessary, take a detour
14123 and build any software tools you may need first, if you don't already
14124 have something appropriate in the toolbox.)
14125
14126 @node I/O redirection
14127 @unnumberedsec I/O Redirection
14128
14129 Hopefully, you are familiar with the basics of I/O redirection in the
14130 shell, in particular the concepts of ``standard input,'' ``standard output,''
14131 and ``standard error''.  Briefly, ``standard input'' is a data source, where
14132 data comes from.  A program should not need to either know or care if the
14133 data source is a disk file, a keyboard, a magnetic tape, or even a punched
14134 card reader.  Similarly, ``standard output'' is a data sink, where data goes
14135 to.  The program should neither know nor care where this might be.
14136 Programs that only read their standard input, do something to the data,
14137 and then send it on, are called @dfn{filters}, by analogy to filters in a
14138 water pipeline.
14139
14140 With the Unix shell, it's very easy to set up data pipelines:
14141
14142 @smallexample
14143 program_to_create_data | filter1 | ... | filterN > final.pretty.data
14144 @end smallexample
14145
14146 We start out by creating the raw data; each filter applies some successive
14147 transformation to the data, until by the time it comes out of the pipeline,
14148 it is in the desired form.
14149
14150 This is fine and good for standard input and standard output.  Where does the
14151 standard error come in to play?  Well, think about @command{filter1} in
14152 the pipeline above.  What happens if it encounters an error in the data it
14153 sees?  If it writes an error message to standard output, it will just
14154 disappear down the pipeline into @command{filter2}'s input, and the
14155 user will probably never see it.  So programs need a place where they can send
14156 error messages so that the user will notice them.  This is standard error,
14157 and it is usually connected to your console or window, even if you have
14158 redirected standard output of your program away from your screen.
14159
14160 For filter programs to work together, the format of the data has to be
14161 agreed upon.  The most straightforward and easiest format to use is simply
14162 lines of text.  Unix data files are generally just streams of bytes, with
14163 lines delimited by the @acronym{ASCII} @sc{lf} (Line Feed) character,
14164 conventionally called a ``newline'' in the Unix literature.  (This is
14165 @code{'\n'} if you're a C programmer.)  This is the format used by all
14166 the traditional filtering programs.  (Many earlier operating systems
14167 had elaborate facilities and special purpose programs for managing
14168 binary data.  Unix has always shied away from such things, under the
14169 philosophy that it's easiest to simply be able to view and edit your
14170 data with a text editor.)
14171
14172 OK, enough introduction.  Let's take a look at some of the tools, and then
14173 we'll see how to hook them together in interesting ways.   In the following
14174 discussion, we will only present those command line options that interest
14175 us.  As you should always do, double check your system documentation
14176 for the full story.
14177
14178 @node The who command
14179 @unnumberedsec The @command{who} Command
14180
14181 The first program is the @command{who} command.  By itself, it generates a
14182 list of the users who are currently logged in.  Although I'm writing
14183 this on a single-user system, we'll pretend that several people are
14184 logged in:
14185
14186 @example
14187 $ who
14188 @print{} arnold   console Jan 22 19:57
14189 @print{} miriam   ttyp0   Jan 23 14:19(:0.0)
14190 @print{} bill     ttyp1   Jan 21 09:32(:0.0)
14191 @print{} arnold   ttyp2   Jan 23 20:48(:0.0)
14192 @end example
14193
14194 Here, the @samp{$} is the usual shell prompt, at which I typed @samp{who}.
14195 There are three people logged in, and I am logged in twice.  On traditional
14196 Unix systems, user names are never more than eight characters long.  This
14197 little bit of trivia will be useful later.  The output of @command{who} is nice,
14198 but the data is not all that exciting.
14199
14200 @node The cut command
14201 @unnumberedsec The @command{cut} Command
14202
14203 The next program we'll look at is the @command{cut} command.  This program
14204 cuts out columns or fields of input data.  For example, we can tell it
14205 to print just the login name and full name from the @file{/etc/passwd}
14206 file.  The @file{/etc/passwd} file has seven fields, separated by
14207 colons:
14208
14209 @example
14210 arnold:xyzzy:2076:10:Arnold D. Robbins:/home/arnold:/bin/bash
14211 @end example
14212
14213 To get the first and fifth fields, we would use @command{cut} like this:
14214
14215 @example
14216 $ cut -d: -f1,5 /etc/passwd
14217 @print{} root:Operator
14218 @dots{}
14219 @print{} arnold:Arnold D. Robbins
14220 @print{} miriam:Miriam A. Robbins
14221 @dots{}
14222 @end example
14223
14224 With the @option{-c} option, @command{cut} will cut out specific characters
14225 (i.e., columns) in the input lines.  This is useful for input data
14226 that has fixed width fields, and does not have a field separator.  For
14227 example, list the Monday dates for the current month:
14228
14229 @c Is using cal ok?  Looked at gcal, but I don't like it.
14230 @example
14231 $ cal | cut -c 3-5
14232 @print{}Mo
14233 @print{}
14234 @print{}  6
14235 @print{} 13
14236 @print{} 20
14237 @print{} 27
14238 @end example
14239
14240 @node The sort command
14241 @unnumberedsec The @command{sort} Command
14242
14243 Next we'll look at the @command{sort} command.  This is one of the most
14244 powerful commands on a Unix-style system; one that you will often find
14245 yourself using when setting up fancy data plumbing.
14246
14247 The @command{sort}
14248 command reads and sorts each file named on the command line.  It then
14249 merges the sorted data and writes it to standard output.  It will read
14250 standard input if no files are given on the command line (thus
14251 making it into a filter).  The sort is based on the character collating
14252 sequence or based on user-supplied ordering criteria.
14253
14254
14255 @node The uniq command
14256 @unnumberedsec The @command{uniq} Command
14257
14258 Finally (at least for now), we'll look at the @command{uniq} program.  When
14259 sorting data, you will often end up with duplicate lines, lines that
14260 are identical.  Usually, all you need is one instance of each line.
14261 This is where @command{uniq} comes in.  The @command{uniq} program reads its
14262 standard input.  It prints only one
14263 copy of each repeated line.  It does have several options.  Later on,
14264 we'll use the @option{-c} option, which prints each unique line, preceded
14265 by a count of the number of times that line occurred in the input.
14266
14267
14268 @node Putting the tools together
14269 @unnumberedsec Putting the Tools Together
14270
14271 Now, let's suppose this is a large ISP server system with dozens of users
14272 logged in.  The management wants the system administrator to write a program that will
14273 generate a sorted list of logged in users.  Furthermore, even if a user
14274 is logged in multiple times, his or her name should only show up in the
14275 output once.
14276
14277 The administrator could sit down with the system documentation and write a C
14278 program that did this.  It would take perhaps a couple of hundred lines
14279 of code and about two hours to write it, test it, and debug it.
14280 However, knowing the software toolbox, the administrator can instead start out
14281 by generating just a list of logged on users:
14282
14283 @example
14284 $ who | cut -c1-8
14285 @print{} arnold
14286 @print{} miriam
14287 @print{} bill
14288 @print{} arnold
14289 @end example
14290
14291 Next, sort the list:
14292
14293 @example
14294 $ who | cut -c1-8 | sort
14295 @print{} arnold
14296 @print{} arnold
14297 @print{} bill
14298 @print{} miriam
14299 @end example
14300
14301 Finally, run the sorted list through @command{uniq}, to weed out duplicates:
14302
14303 @example
14304 $ who | cut -c1-8 | sort | uniq
14305 @print{} arnold
14306 @print{} bill
14307 @print{} miriam
14308 @end example
14309
14310 The @command{sort} command actually has a @option{-u} option that does what
14311 @command{uniq} does.  However, @command{uniq} has other uses for which one
14312 cannot substitute @samp{sort -u}.
14313
14314 The administrator puts this pipeline into a shell script, and makes it available for
14315 all the users on the system (@samp{#} is the system administrator,
14316 or @code{root}, prompt):
14317
14318 @example
14319 # cat > /usr/local/bin/listusers
14320 who | cut -c1-8 | sort | uniq
14321 ^D
14322 # chmod +x /usr/local/bin/listusers
14323 @end example
14324
14325 There are four major points to note here.  First, with just four
14326 programs, on one command line, the administrator was able to save about two
14327 hours worth of work.  Furthermore, the shell pipeline is just about as
14328 efficient as the C program would be, and it is much more efficient in
14329 terms of programmer time.  People time is much more expensive than
14330 computer time, and in our modern ``there's never enough time to do
14331 everything'' society, saving two hours of programmer time is no mean
14332 feat.
14333
14334 Second, it is also important to emphasize that with the
14335 @emph{combination} of the tools, it is possible to do a special
14336 purpose job never imagined by the authors of the individual programs.
14337
14338 Third, it is also valuable to build up your pipeline in stages, as we did here.
14339 This allows you to view the data at each stage in the pipeline, which helps
14340 you acquire the confidence that you are indeed using these tools correctly.
14341
14342 Finally, by bundling the pipeline in a shell script, other users can use
14343 your command, without having to remember the fancy plumbing you set up for
14344 them.  In terms of how you run them, shell scripts and compiled programs are
14345 indistinguishable.
14346
14347 After the previous warm-up exercise, we'll look at two additional, more
14348 complicated pipelines.  For them, we need to introduce two more tools.
14349
14350 The first is the @command{tr} command, which stands for ``transliterate.''
14351 The @command{tr} command works on a character-by-character basis, changing
14352 characters.  Normally it is used for things like mapping upper case to
14353 lower case:
14354
14355 @example
14356 $ echo ThIs ExAmPlE HaS MIXED case! | tr '[:upper:]' '[:lower:]'
14357 @print{} this example has mixed case!
14358 @end example
14359
14360 There are several options of interest:
14361
14362 @table @code
14363 @item -c
14364 work on the complement of the listed characters, i.e.,
14365 operations apply to characters not in the given set
14366
14367 @item -d
14368 delete characters in the first set from the output
14369
14370 @item -s
14371 squeeze repeated characters in the output into just one character.
14372 @end table
14373
14374 We will be using all three options in a moment.
14375
14376 The other command we'll look at is @command{comm}.  The @command{comm}
14377 command takes two sorted input files as input data, and prints out the
14378 files' lines in three columns.  The output columns are the data lines
14379 unique to the first file, the data lines unique to the second file, and
14380 the data lines that are common to both.  The @option{-1}, @option{-2}, and
14381 @option{-3} command line options @emph{omit} the respective columns.  (This is
14382 non-intuitive and takes a little getting used to.)  For example:
14383
14384 @example
14385 $ cat f1
14386 @print{} 11111
14387 @print{} 22222
14388 @print{} 33333
14389 @print{} 44444
14390 $ cat f2
14391 @print{} 00000
14392 @print{} 22222
14393 @print{} 33333
14394 @print{} 55555
14395 $ comm f1 f2
14396 @print{}         00000
14397 @print{} 11111
14398 @print{}                 22222
14399 @print{}                 33333
14400 @print{} 44444
14401 @print{}         55555
14402 @end example
14403
14404 The file name @file{-} tells @command{comm} to read standard input
14405 instead of a regular file.
14406
14407 Now we're ready to build a fancy pipeline.  The first application is a word
14408 frequency counter.  This helps an author determine if he or she is over-using
14409 certain words.
14410
14411 The first step is to change the case of all the letters in our input file
14412 to one case.  ``The'' and ``the'' are the same word when doing counting.
14413
14414 @example
14415 $ tr '[:upper:]' '[:lower:]' < whats.gnu | ...
14416 @end example
14417
14418 The next step is to get rid of punctuation.  Quoted words and unquoted words
14419 should be treated identically; it's easiest to just get the punctuation out of
14420 the way.
14421
14422 @smallexample
14423 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' | ...
14424 @end smallexample
14425
14426 The second @command{tr} command operates on the complement of the listed
14427 characters, which are all the letters, the digits, the underscore, and
14428 the blank.  The @samp{\n} represents the newline character; it has to
14429 be left alone.  (The @acronym{ASCII} tab character should also be included for
14430 good measure in a production script.)
14431
14432 At this point, we have data consisting of words separated by blank space.
14433 The words only contain alphanumeric characters (and the underscore).  The
14434 next step is break the data apart so that we have one word per line.  This
14435 makes the counting operation much easier, as we will see shortly.
14436
14437 @smallexample
14438 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14439 > tr -s ' ' '\n' | ...
14440 @end smallexample
14441
14442 This command turns blanks into newlines.  The @option{-s} option squeezes
14443 multiple newline characters in the output into just one.  This helps us
14444 avoid blank lines.  (The @samp{>} is the shell's ``secondary prompt.''
14445 This is what the shell prints when it notices you haven't finished
14446 typing in all of a command.)
14447
14448 We now have data consisting of one word per line, no punctuation, all one
14449 case.  We're ready to count each word:
14450
14451 @smallexample
14452 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14453 > tr -s ' ' '\n' | sort | uniq -c | ...
14454 @end smallexample
14455
14456 At this point, the data might look something like this:
14457
14458 @example
14459      60 a
14460       2 able
14461       6 about
14462       1 above
14463       2 accomplish
14464       1 acquire
14465       1 actually
14466       2 additional
14467 @end example
14468
14469 The output is sorted by word, not by count!  What we want is the most
14470 frequently used words first.  Fortunately, this is easy to accomplish,
14471 with the help of two more @command{sort} options:
14472
14473 @table @code
14474 @item -n
14475 do a numeric sort, not a textual one
14476
14477 @item -r
14478 reverse the order of the sort
14479 @end table
14480
14481 The final pipeline looks like this:
14482
14483 @smallexample
14484 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14485 > tr -s ' ' '\n' | sort | uniq -c | sort -n -r
14486 @print{}    156 the
14487 @print{}     60 a
14488 @print{}     58 to
14489 @print{}     51 of
14490 @print{}     51 and
14491 @dots{}
14492 @end smallexample
14493
14494 Whew!  That's a lot to digest.  Yet, the same principles apply.  With six
14495 commands, on two lines (really one long one split for convenience), we've
14496 created a program that does something interesting and useful, in much
14497 less time than we could have written a C program to do the same thing.
14498
14499 A minor modification to the above pipeline can give us a simple spelling
14500 checker!  To determine if you've spelled a word correctly, all you have to
14501 do is look it up in a dictionary.  If it is not there, then chances are
14502 that your spelling is incorrect.  So, we need a dictionary.
14503 The conventional location for a dictionary is @file{/usr/dict/words}.
14504 On my GNU/Linux system,@footnote{Redhat Linux 6.1, for the November 2000
14505 revision of this article.}
14506 this is a is a sorted, 45,402 word dictionary.
14507
14508 Now, how to compare our file with the dictionary?  As before, we generate
14509 a sorted list of words, one per line:
14510
14511 @smallexample
14512 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14513 > tr -s ' ' '\n' | sort -u | ...
14514 @end smallexample
14515
14516 Now, all we need is a list of words that are @emph{not} in the
14517 dictionary.  Here is where the @command{comm} command comes in.
14518
14519 @smallexample
14520 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14521 > tr -s ' ' '\n' | sort -u |
14522 > comm -23 - /usr/dict/words
14523 @end smallexample
14524
14525 The @option{-2} and @option{-3} options eliminate lines that are only in the
14526 dictionary (the second file), and lines that are in both files.  Lines
14527 only in the first file (standard input, our stream of words), are
14528 words that are not in the dictionary.  These are likely candidates for
14529 spelling errors.  This pipeline was the first cut at a production
14530 spelling checker on Unix.
14531
14532 There are some other tools that deserve brief mention.
14533
14534 @table @command
14535 @item grep
14536 search files for text that matches a regular expression
14537
14538 @item wc
14539 count lines, words, characters
14540
14541 @item tee
14542 a T-fitting for data pipes, copies data to files and to standard output
14543
14544 @item sed
14545 the stream editor, an advanced tool
14546
14547 @item awk
14548 a data manipulation language, another advanced tool
14549 @end table
14550
14551 The software tools philosophy also espoused the following bit of
14552 advice: ``Let someone else do the hard part.''  This means, take
14553 something that gives you most of what you need, and then massage it the
14554 rest of the way until it's in the form that you want.
14555
14556 To summarize:
14557
14558 @enumerate 1
14559 @item
14560 Each program should do one thing well.  No more, no less.
14561
14562 @item
14563 Combining programs with appropriate plumbing leads to results where
14564 the whole is greater than the sum of the parts.  It also leads to novel
14565 uses of programs that the authors might never have imagined.
14566
14567 @item
14568 Programs should never print extraneous header or trailer data, since these
14569 could get sent on down a pipeline.  (A point we didn't mention earlier.)
14570
14571 @item
14572 Let someone else do the hard part.
14573
14574 @item
14575 Know your toolbox!  Use each program appropriately.  If you don't have an
14576 appropriate tool, build one.
14577 @end enumerate
14578
14579 As of this writing, all the programs we've discussed are available via
14580 anonymous @command{ftp} from: @*
14581 @uref{ftp://gnudist.gnu.org/textutils/textutils-1.22.tar.gz}.  (There may
14582 be more recent versions available now.)
14583
14584 None of what I have presented in this column is new.  The Software Tools
14585 philosophy was first introduced in the book @cite{Software Tools}, by
14586 Brian Kernighan and P.J. Plauger (Addison-Wesley, ISBN 0-201-03669-X).
14587 This book showed how to write and use software tools.  It was written in
14588 1976, using a preprocessor for FORTRAN named @command{ratfor} (RATional
14589 FORtran).  At the time, C was not as ubiquitous as it is now; FORTRAN
14590 was.  The last chapter presented a @command{ratfor} to FORTRAN
14591 processor, written in @command{ratfor}.  @command{ratfor} looks an awful
14592 lot like C; if you know C, you won't have any problem following the
14593 code.
14594
14595 In 1981, the book was updated and made available as @cite{Software Tools
14596 in Pascal} (Addison-Wesley, ISBN 0-201-10342-7).  Both books are
14597 still in print and are well worth
14598 reading if you're a programmer.  They certainly made a major change in
14599 how I view programming.
14600
14601 The programs in both books are available from
14602 @uref{http://cm.bell-labs.com/who/bwk, Brian Kernighan's home page}.
14603 For a number of years, there was an active
14604 Software Tools Users Group, whose members had ported the original
14605 @command{ratfor} programs to essentially every computer system with a
14606 FORTRAN compiler.  The popularity of the group waned in the middle 1980s
14607 as Unix began to spread beyond universities.
14608
14609 With the current proliferation of GNU code and other clones of Unix programs,
14610 these programs now receive little attention; modern C versions are
14611 much more efficient and do more than these programs do.  Nevertheless, as
14612 exposition of good programming style, and evangelism for a still-valuable
14613 philosophy, these books are unparalleled, and I recommend them highly.
14614
14615 Acknowledgment: I would like to express my gratitude to Brian Kernighan
14616 of Bell Labs, the original Software Toolsmith, for reviewing this column.
14617
14618 @node Copying This Manual
14619 @appendix Copying This Manual
14620
14621 @menu
14622 * GNU Free Documentation License::  License for copying this manual.
14623 @end menu
14624
14625 @include fdl.texi
14626
14627 @node Index
14628 @unnumbered Index
14629
14630 @printindex cp
14631
14632 @shortcontents
14633 @contents
14634 @bye
14635
14636 @c Local variables:
14637 @c texinfo-column-for-description: 32
14638 @c End: