maint: avoid trivial syntax-check failure
[platform/upstream/coreutils.git] / doc / coreutils.texi
1 \input texinfo
2 @c %**start of header
3 @setfilename coreutils.info
4 @settitle @sc{gnu} Coreutils
5
6 @c %**end of header
7
8 @include version.texi
9 @include constants.texi
10
11 @c Define new indices.
12 @defcodeindex op
13 @defcodeindex fl
14
15 @c Put everything in one index (arbitrarily chosen to be the concept index).
16 @syncodeindex fl cp
17 @syncodeindex fn cp
18 @syncodeindex ky cp
19 @syncodeindex op cp
20 @syncodeindex pg cp
21 @syncodeindex vr cp
22
23 @dircategory Basics
24 @direntry
25 * Coreutils: (coreutils).       Core GNU (file, text, shell) utilities.
26 * Common options: (coreutils)Common options.      Common options.
27 * File permissions: (coreutils)File permissions.  Access modes.
28 * Date input formats: (coreutils)Date input formats.
29 @end direntry
30
31 @c FIXME: the following need documentation
32 @c * [: (coreutils)[ invocation.                   File/string tests.
33 @c * pinky: (coreutils)pinky invocation.           FIXME.
34
35 @dircategory Individual utilities
36 @direntry
37 * arch: (coreutils)arch invocation.             Print machine hardware name.
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 * chcon: (coreutils)chcon invocation.           Change SELinux CTX of files.
42 * chgrp: (coreutils)chgrp invocation.           Change file groups.
43 * chmod: (coreutils)chmod invocation.           Change file permissions.
44 * chown: (coreutils)chown invocation.           Change file owners/groups.
45 * chroot: (coreutils)chroot invocation.         Specify the root directory.
46 * cksum: (coreutils)cksum invocation.           Print POSIX CRC checksum.
47 * comm: (coreutils)comm invocation.             Compare sorted files by line.
48 * cp: (coreutils)cp invocation.                 Copy files.
49 * csplit: (coreutils)csplit invocation.         Split by context.
50 * cut: (coreutils)cut invocation.               Print selected parts of lines.
51 * date: (coreutils)date invocation.             Print/set system date and time.
52 * dd: (coreutils)dd invocation.                 Copy and convert a file.
53 * df: (coreutils)df invocation.                 Report file system disk usage.
54 * dir: (coreutils)dir invocation.               List directories briefly.
55 * dircolors: (coreutils)dircolors invocation.   Color setup for ls.
56 * dirname: (coreutils)dirname invocation.       Strip last file name component.
57 * du: (coreutils)du invocation.                 Report on disk usage.
58 * echo: (coreutils)echo invocation.             Print a line of text.
59 * env: (coreutils)env invocation.               Modify the environment.
60 * expand: (coreutils)expand invocation.         Convert tabs to spaces.
61 * expr: (coreutils)expr invocation.             Evaluate expressions.
62 * factor: (coreutils)factor invocation.         Print prime factors
63 * false: (coreutils)false invocation.           Do nothing, unsuccessfully.
64 * fmt: (coreutils)fmt invocation.               Reformat paragraph text.
65 * fold: (coreutils)fold invocation.             Wrap long input lines.
66 * groups: (coreutils)groups invocation.         Print group names a user is in.
67 * head: (coreutils)head invocation.             Output the first part of files.
68 * hostid: (coreutils)hostid invocation.         Print numeric host identifier.
69 * hostname: (coreutils)hostname invocation.     Print or set system name.
70 * id: (coreutils)id invocation.                 Print user identity.
71 * install: (coreutils)install invocation.       Copy and change attributes.
72 * join: (coreutils)join invocation.             Join lines on a common field.
73 * kill: (coreutils)kill invocation.             Send a signal to processes.
74 * link: (coreutils)link invocation.             Make hard links between files.
75 * ln: (coreutils)ln invocation.                 Make links between files.
76 * logname: (coreutils)logname invocation.       Print current login name.
77 * ls: (coreutils)ls invocation.                 List directory contents.
78 * md5sum: (coreutils)md5sum invocation.         Print or check MD5 digests.
79 * mkdir: (coreutils)mkdir invocation.           Create directories.
80 * mkfifo: (coreutils)mkfifo invocation.         Create FIFOs (named pipes).
81 * mknod: (coreutils)mknod invocation.           Create special files.
82 * mktemp: (coreutils)mktemp invocation.         Create temporary files.
83 * mv: (coreutils)mv invocation.                 Rename files.
84 * nice: (coreutils)nice invocation.             Modify niceness.
85 * nl: (coreutils)nl invocation.                 Number lines and write files.
86 * nohup: (coreutils)nohup invocation.           Immunize to hangups.
87 * nproc: (coreutils)nproc invocation.           Print the number of processors.
88 * od: (coreutils)od invocation.                 Dump files in octal, etc.
89 * paste: (coreutils)paste invocation.           Merge lines of files.
90 * pathchk: (coreutils)pathchk invocation.       Check file name portability.
91 * pr: (coreutils)pr invocation.                 Paginate or columnate files.
92 * printenv: (coreutils)printenv invocation.     Print environment variables.
93 * printf: (coreutils)printf invocation.         Format and print data.
94 * ptx: (coreutils)ptx invocation.               Produce permuted indexes.
95 * pwd: (coreutils)pwd invocation.               Print working directory.
96 * readlink: (coreutils)readlink invocation.     Print referent of a symlink.
97 * rm: (coreutils)rm invocation.                 Remove files.
98 * rmdir: (coreutils)rmdir invocation.           Remove empty directories.
99 * runcon: (coreutils)runcon invocation.         Run in specified SELinux CTX.
100 * seq: (coreutils)seq invocation.               Print numeric sequences
101 * sha1sum: (coreutils)sha1sum invocation.       Print or check SHA-1 digests.
102 * sha2: (coreutils)sha2 utilities.              Print or check SHA-2 digests.
103 * shred: (coreutils)shred invocation.           Remove files more securely.
104 * shuf: (coreutils)shuf invocation.             Shuffling text files.
105 * sleep: (coreutils)sleep invocation.           Delay for a specified time.
106 * sort: (coreutils)sort invocation.             Sort text files.
107 * split: (coreutils)split invocation.           Split into pieces.
108 * stat: (coreutils)stat invocation.             Report file(system) status.
109 * stdbuf: (coreutils)stdbuf invocation.         Modify stdio buffering.
110 * stty: (coreutils)stty invocation.             Print/change terminal settings.
111 * su: (coreutils)su invocation.                 Modify user and group ID.
112 * sum: (coreutils)sum invocation.               Print traditional checksum.
113 * sync: (coreutils)sync invocation.             Synchronize memory and disk.
114 * tac: (coreutils)tac invocation.               Reverse files.
115 * tail: (coreutils)tail invocation.             Output the last part of files.
116 * tee: (coreutils)tee invocation.               Redirect to multiple files.
117 * test: (coreutils)test invocation.             File/string tests.
118 * timeout: (coreutils)timeout invocation.       Run with time limit.
119 * touch: (coreutils)touch invocation.           Change file timestamps.
120 * tr: (coreutils)tr invocation.                 Translate characters.
121 * true: (coreutils)true invocation.             Do nothing, successfully.
122 * truncate: (coreutils)truncate invocation.     Shrink/extend size of a file.
123 * tsort: (coreutils)tsort invocation.           Topological sort.
124 * tty: (coreutils)tty invocation.               Print terminal name.
125 * uname: (coreutils)uname invocation.           Print system information.
126 * unexpand: (coreutils)unexpand invocation.     Convert spaces to tabs.
127 * uniq: (coreutils)uniq invocation.             Uniquify files.
128 * unlink: (coreutils)unlink invocation.         Removal via unlink(2).
129 * uptime: (coreutils)uptime invocation.         Print uptime and load.
130 * users: (coreutils)users invocation.           Print current user names.
131 * vdir: (coreutils)vdir invocation.             List directories verbosely.
132 * wc: (coreutils)wc invocation.                 Line, word, and byte counts.
133 * who: (coreutils)who invocation.               Print who is logged in.
134 * whoami: (coreutils)whoami invocation.         Print effective user ID.
135 * yes: (coreutils)yes invocation.               Print a string indefinitely.
136 @end direntry
137
138 @copying
139 This manual documents version @value{VERSION} of the @sc{gnu} core
140 utilities, including the standard programs for text and file manipulation.
141
142 Copyright @copyright{} 1994-1996, 2000-2011 Free Software Foundation, Inc.
143
144 @quotation
145 Permission is granted to copy, distribute and/or modify this document
146 under the terms of the GNU Free Documentation License, Version 1.3 or
147 any later version published by the Free Software Foundation; with no
148 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
149 Texts.  A copy of the license is included in the section entitled ``GNU
150 Free Documentation License''.
151 @end quotation
152 @end copying
153
154 @titlepage
155 @title @sc{gnu} @code{Coreutils}
156 @subtitle Core GNU utilities
157 @subtitle for version @value{VERSION}, @value{UPDATED}
158 @author David MacKenzie et al.
159
160 @page
161 @vskip 0pt plus 1filll
162 @insertcopying
163 @end titlepage
164 @shortcontents
165 @contents
166
167 @ifnottex
168 @node Top
169 @top GNU Coreutils
170
171 @insertcopying
172 @end ifnottex
173
174 @cindex core utilities
175 @cindex text utilities
176 @cindex shell utilities
177 @cindex file utilities
178
179 @menu
180 * Introduction::                 Caveats, overview, and authors
181 * Common options::               Common options
182 * Output of entire files::       cat tac nl od base64
183 * Formatting file contents::     fmt pr fold
184 * Output of parts of files::     head tail split csplit
185 * Summarizing files::            wc sum cksum md5sum sha1sum sha2
186 * Operating on sorted files::    sort shuf uniq comm ptx tsort
187 * Operating on fields::          cut paste join
188 * Operating on characters::      tr expand unexpand
189 * Directory listing::            ls dir vdir dircolors
190 * Basic operations::             cp dd install mv rm shred
191 * Special file types::         mkdir rmdir unlink mkfifo mknod ln link readlink
192 * Changing file attributes::     chgrp chmod chown touch
193 * Disk usage::                   df du stat sync truncate
194 * Printing text::                echo printf yes
195 * Conditions::                   false true test expr
196 * Redirection::                  tee
197 * File name manipulation::       dirname basename pathchk mktemp
198 * Working context::              pwd stty printenv tty
199 * User information::             id logname whoami groups users who
200 * System context::               date arch nproc uname hostname hostid uptime
201 * SELinux context::              chcon runcon
202 * Modified command invocation::  chroot env nice nohup stdbuf su timeout
203 * Process control::              kill
204 * Delaying::                     sleep
205 * Numeric operations::           factor seq
206 * File permissions::             Access modes
207 * Date input formats::           Specifying date strings
208 * Opening the software toolbox:: The software tools philosophy
209 * GNU Free Documentation License:: Copying and sharing this manual
210 * Concept index::                General index
211
212 @detailmenu
213  --- The Detailed Node Listing ---
214
215 Common Options
216
217 * Exit status::                  Indicating program success or failure
218 * Backup options::               Backup options
219 * Block size::                   Block size
220 * Floating point::               Floating point number representation
221 * Signal specifications::        Specifying signals
222 * Disambiguating names and IDs:: chgrp and chown owner and group syntax
223 * Random sources::               Sources of random data
224 * Target directory::             Target directory
225 * Trailing slashes::             Trailing slashes
226 * Traversing symlinks::          Traversing symlinks to directories
227 * Treating / specially::         Treating / specially
228 * Standards conformance::        Standards conformance
229
230 Output of entire files
231
232 * cat invocation::               Concatenate and write files
233 * tac invocation::               Concatenate and write files in reverse
234 * nl invocation::                Number lines and write files
235 * od invocation::                Write files in octal or other formats
236 * base64 invocation::            Transform data into printable data
237
238 Formatting file contents
239
240 * fmt invocation::               Reformat paragraph text
241 * pr invocation::                Paginate or columnate files for printing
242 * fold invocation::              Wrap input lines to fit in specified width
243
244 Output of parts of files
245
246 * head invocation::              Output the first part of files
247 * tail invocation::              Output the last part of files
248 * split invocation::             Split a file into fixed-size pieces
249 * csplit invocation::            Split a file into context-determined pieces
250
251 Summarizing files
252
253 * wc invocation::                Print newline, word, and byte counts
254 * sum invocation::               Print checksum and block counts
255 * cksum invocation::             Print CRC checksum and byte counts
256 * md5sum invocation::            Print or check MD5 digests
257 * sha1sum invocation::           Print or check SHA-1 digests
258 * sha2 utilities::               Print or check SHA-2 digests
259
260 Operating on sorted files
261
262 * sort invocation::              Sort text files
263 * shuf invocation::              Shuffle text files
264 * uniq invocation::              Uniquify files
265 * comm invocation::              Compare two sorted files line by line
266 * ptx invocation::               Produce a permuted index of file contents
267 * tsort invocation::             Topological sort
268
269 @command{ptx}: Produce permuted indexes
270
271 * General options in ptx::       Options which affect general program behavior
272 * Charset selection in ptx::     Underlying character set considerations
273 * Input processing in ptx::      Input fields, contexts, and keyword selection
274 * Output formatting in ptx::     Types of output format, and sizing the fields
275 * Compatibility in ptx::         The @acronym{GNU} extensions to @command{ptx}
276
277 Operating on fields
278
279 * cut invocation::               Print selected parts of lines
280 * paste invocation::             Merge lines of files
281 * join invocation::              Join lines on a common field
282
283 Operating on characters
284
285 * tr invocation::                Translate, squeeze, and/or delete characters
286 * expand invocation::            Convert tabs to spaces
287 * unexpand invocation::          Convert spaces to tabs
288
289 @command{tr}: Translate, squeeze, and/or delete characters
290
291 * Character sets::               Specifying sets of characters
292 * Translating::                  Changing one set of characters to another
293 * Squeezing::                    Squeezing repeats and deleting
294
295 Directory listing
296
297 * ls invocation::                List directory contents
298 * dir invocation::               Briefly list directory contents
299 * vdir invocation::              Verbosely list directory contents
300 * dircolors invocation::         Color setup for @command{ls}
301
302 @command{ls}:  List directory contents
303
304 * Which files are listed::       Which files are listed
305 * What information is listed::   What information is listed
306 * Sorting the output::           Sorting the output
307 * Details about version sort::   More details about version sort
308 * General output formatting::    General output formatting
309 * Formatting the file names::    Formatting the file names
310
311 Basic operations
312
313 * cp invocation::                Copy files and directories
314 * dd invocation::                Convert and copy a file
315 * install invocation::           Copy files and set attributes
316 * mv invocation::                Move (rename) files
317 * rm invocation::                Remove files or directories
318 * shred invocation::             Remove files more securely
319
320 Special file types
321
322 * link invocation::              Make a hard link via the link syscall
323 * ln invocation::                Make links between files
324 * mkdir invocation::             Make directories
325 * mkfifo invocation::            Make FIFOs (named pipes)
326 * mknod invocation::             Make block or character special files
327 * readlink invocation::          Print value of a symlink or canonical file name
328 * rmdir invocation::             Remove empty directories
329 * unlink invocation::            Remove files via unlink syscall
330
331 Changing file attributes
332
333 * chown invocation::             Change file owner and group
334 * chgrp invocation::             Change group ownership
335 * chmod invocation::             Change access permissions
336 * touch invocation::             Change file timestamps
337
338 Disk usage
339
340 * df invocation::                Report file system disk space usage
341 * du invocation::                Estimate file space usage
342 * stat invocation::              Report file or file system status
343 * sync invocation::              Synchronize data on disk with memory
344 * truncate invocation::          Shrink or extend the size of a file
345
346 Printing text
347
348 * echo invocation::              Print a line of text
349 * printf invocation::            Format and print data
350 * yes invocation::               Print a string until interrupted
351
352 Conditions
353
354 * false invocation::             Do nothing, unsuccessfully
355 * true invocation::              Do nothing, successfully
356 * test invocation::              Check file types and compare values
357 * expr invocation::              Evaluate expressions
358
359 @command{test}: Check file types and compare values
360
361 * File type tests::              File type tests
362 * Access permission tests::      Access permission tests
363 * File characteristic tests::    File characteristic tests
364 * String tests::                 String tests
365 * Numeric tests::                Numeric tests
366
367 @command{expr}: Evaluate expression
368
369 * String expressions::           + : match substr index length
370 * Numeric expressions::          + - * / %
371 * Relations for expr::           | & < <= = == != >= >
372 * Examples of expr::             Examples of using @command{expr}
373
374 Redirection
375
376 * tee invocation::               Redirect output to multiple files or processes
377
378 File name manipulation
379
380 * basename invocation::          Strip directory and suffix from a file name
381 * dirname invocation::           Strip last file name component
382 * pathchk invocation::           Check file name validity and portability
383 * mktemp invocation::            Create temporary file or directory
384
385 Working context
386
387 * pwd invocation::               Print working directory
388 * stty invocation::              Print or change terminal characteristics
389 * printenv invocation::          Print all or some environment variables
390 * tty invocation::               Print file name of terminal on standard input
391
392 @command{stty}: Print or change terminal characteristics
393
394 * Control::                      Control settings
395 * Input::                        Input settings
396 * Output::                       Output settings
397 * Local::                        Local settings
398 * Combination::                  Combination settings
399 * Characters::                   Special characters
400 * Special::                      Special settings
401
402 User information
403
404 * id invocation::                Print user identity
405 * logname invocation::           Print current login name
406 * whoami invocation::            Print effective user ID
407 * groups invocation::            Print group names a user is in
408 * users invocation::             Print login names of users currently logged in
409 * who invocation::               Print who is currently logged in
410
411 System context
412
413 * arch invocation::              Print machine hardware name
414 * date invocation::              Print or set system date and time
415 * nproc invocation::             Print the number of processors
416 * uname invocation::             Print system information
417 * hostname invocation::          Print or set system name
418 * hostid invocation::            Print numeric host identifier
419 * uptime invocation::            Print system uptime and load
420
421 @command{date}: Print or set system date and time
422
423 * Time conversion specifiers::   %[HIklMNpPrRsSTXzZ]
424 * Date conversion specifiers::   %[aAbBcCdDeFgGhjmuUVwWxyY]
425 * Literal conversion specifiers:: %[%nt]
426 * Padding and other flags::      Pad with zeros, spaces, etc.
427 * Setting the time::             Changing the system clock
428 * Options for date::             Instead of the current time
429 * Date input formats::           Specifying date strings
430 * Examples of date::             Examples
431
432 SELinux context
433
434 * chcon invocation::             Change SELinux context of file
435 * runcon invocation::            Run a command in specified SELinux context
436
437 Modified command invocation
438
439 * chroot invocation::            Run a command with a different root directory
440 * env invocation::               Run a command in a modified environment
441 * nice invocation::              Run a command with modified niceness
442 * nohup invocation::             Run a command immune to hangups
443 * stdbuf invocation::            Run a command with modified I/O buffering
444 * su invocation::                Run a command with substitute user and group ID
445 * timeout invocation::           Run a command with a time limit
446
447 Process control
448
449 * kill invocation::              Sending a signal to processes.
450
451 Delaying
452
453 * sleep invocation::             Delay for a specified time
454
455 Numeric operations
456
457 * factor invocation::            Print prime factors
458 * seq invocation::               Print numeric sequences
459
460 File permissions
461
462 * Mode Structure::               Structure of file mode bits
463 * Symbolic Modes::               Mnemonic representation of file mode bits
464 * Numeric Modes::                File mode bits as octal numbers
465 * Directory Setuid and Setgid::  Set-user-ID and set-group-ID on directories
466
467 Date input formats
468
469 * General date syntax::          Common rules
470 * Calendar date items::          19 Dec 1994
471 * Time of day items::            9:20pm
472 * Time zone items::              @sc{est}, @sc{pdt}, @sc{gmt}
473 * Day of week items::            Monday and others
474 * Relative items in date strings:: next tuesday, 2 years ago
475 * Pure numbers in date strings:: 19931219, 1440
476 * Seconds since the Epoch::      @@1078100502
477 * Specifying time zone rules::   TZ="America/New_York", TZ="UTC0"
478 * Authors of parse_datetime::    Bellovin, Eggert, Salz, Berets, et al
479
480 Opening the software toolbox
481
482 * Toolbox introduction::         Toolbox introduction
483 * I/O redirection::              I/O redirection
484 * The who command::              The @command{who} command
485 * The cut command::              The @command{cut} command
486 * The sort command::             The @command{sort} command
487 * The uniq command::             The @command{uniq} command
488 * Putting the tools together::   Putting the tools together
489
490 Copying This Manual
491
492 * GNU Free Documentation License::     Copying and sharing this manual
493
494 @end detailmenu
495 @end menu
496
497
498 @node Introduction
499 @chapter Introduction
500
501 This manual is a work in progress: many sections make no attempt to explain
502 basic concepts in a way suitable for novices.  Thus, if you are interested,
503 please get involved in improving this manual.  The entire @sc{gnu} community
504 will benefit.
505
506 @cindex @acronym{POSIX}
507 The @sc{gnu} utilities documented here are mostly compatible with the
508 @acronym{POSIX} standard.
509 @cindex bugs, reporting
510 Please report bugs to @email{bug-coreutils@@gnu.org}.  Remember
511 to include the version number, machine architecture, input files, and
512 any other information needed to reproduce the bug: your input, what you
513 expected, what you got, and why it is wrong.  Diffs are welcome, but
514 please include a description of the problem as well, since this is
515 sometimes difficult to infer.  @xref{Bugs, , , gcc, Using and Porting GNU CC}.
516
517 @cindex Berry, K.
518 @cindex Paterson, R.
519 @cindex Stallman, R.
520 @cindex Pinard, F.
521 @cindex MacKenzie, D.
522 @cindex Meyering, J.
523 @cindex Youmans, B.
524 This manual was originally derived from the Unix man pages in the
525 distributions, which were written by David MacKenzie and updated by Jim
526 Meyering.  What you are reading now is the authoritative documentation
527 for these utilities; the man pages are no longer being maintained.  The
528 original @command{fmt} man page was written by Ross Paterson.  Fran@,{c}ois
529 Pinard did the initial conversion to Texinfo format.  Karl Berry did the
530 indexing, some reorganization, and editing of the results.  Brian
531 Youmans of the Free Software Foundation office staff combined the
532 manuals for textutils, fileutils, and sh-utils to produce the present
533 omnibus manual.  Richard Stallman contributed his usual invaluable
534 insights to the overall process.
535
536 @node Common options
537 @chapter Common options
538
539 @macro optBackup
540 @item -b
541 @itemx @w{@kbd{--backup}[=@var{method}]}
542 @opindex -b
543 @opindex --backup
544 @vindex VERSION_CONTROL
545 @cindex backups, making
546 @xref{Backup options}.
547 Make a backup of each file that would otherwise be overwritten or removed.
548 @end macro
549
550 @macro optBackupSuffix
551 @item -S @var{suffix}
552 @itemx --suffix=@var{suffix}
553 @opindex -S
554 @opindex --suffix
555 Append @var{suffix} to each backup file made with @option{-b}.
556 @xref{Backup options}.
557 @end macro
558
559 @macro optTargetDirectory
560 @item -t @var{directory}
561 @itemx @w{@kbd{--target-directory}=@var{directory}}
562 @opindex -t
563 @opindex --target-directory
564 @cindex target directory
565 @cindex destination directory
566 Specify the destination @var{directory}.
567 @xref{Target directory}.
568 @end macro
569
570 @macro optNoTargetDirectory
571 @item -T
572 @itemx --no-target-directory
573 @opindex -T
574 @opindex --no-target-directory
575 @cindex target directory
576 @cindex destination directory
577 Do not treat the last operand specially when it is a directory or a
578 symbolic link to a directory.  @xref{Target directory}.
579 @end macro
580
581 @macro optNull{cmd}
582 @item -0
583 @opindex -0
584 @itemx --null
585 @opindex --null
586 @cindex output @sc{nul}-byte-terminated lines
587 Output a zero byte (@acronym{ASCII} @sc{nul}) at the end of each line,
588 rather than a newline. This option enables other programs to parse the
589 output of @command{\cmd\} even when that output would contain data
590 with embedded newlines.
591 @end macro
592
593 @macro optSi
594 @itemx --si
595 @opindex --si
596 @cindex SI output
597 Append an SI-style abbreviation to each size, such as @samp{M} for
598 megabytes.  Powers of 1000 are used, not 1024; @samp{M} stands for
599 1,000,000 bytes.  This option is equivalent to
600 @option{--block-size=si}.  Use the @option{-h} or
601 @option{--human-readable} option if
602 you prefer powers of 1024.
603 @end macro
604
605 @macro optHumanReadable
606 @item -h
607 @itemx --human-readable
608 @opindex -h
609 @opindex --human-readable
610 @cindex human-readable output
611 Append a size letter to each size, such as @samp{M} for mebibytes.
612 Powers of 1024 are used, not 1000; @samp{M} stands for 1,048,576 bytes.
613 This option is equivalent to @option{--block-size=human-readable}.
614 Use the @option{--si} option if you prefer powers of 1000.
615 @end macro
616
617 @macro optStripTrailingSlashes
618 @itemx @w{@kbd{--strip-trailing-slashes}}
619 @opindex --strip-trailing-slashes
620 @cindex stripping trailing slashes
621 Remove any trailing slashes from each @var{source} argument.
622 @xref{Trailing slashes}.
623 @end macro
624
625 @macro mayConflictWithShellBuiltIn{cmd}
626 @cindex conflicts with shell built-ins
627 @cindex built-in shell commands, conflicts with
628 Due to shell aliases and built-in @command{\cmd\} functions, using an
629 unadorned @command{\cmd\} interactively or in a script may get you
630 different functionality than that described here.  Invoke it via
631 @command{env} (i.e., @code{env \cmd\ @dots{}}) to avoid interference
632 from the shell.
633
634 @end macro
635
636 @macro multiplierSuffixes{varName}
637 @var{\varName\} may be, or may be an integer optionally followed by,
638 one of the following multiplicative suffixes:
639 @example
640 @samp{b}  =>            512 ("blocks")
641 @samp{KB} =>           1000 (KiloBytes)
642 @samp{K}  =>           1024 (KibiBytes)
643 @samp{MB} =>      1000*1000 (MegaBytes)
644 @samp{M}  =>      1024*1024 (MebiBytes)
645 @samp{GB} => 1000*1000*1000 (GigaBytes)
646 @samp{G}  => 1024*1024*1024 (GibiBytes)
647 @end example
648 and so on for @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}.
649 @end macro
650
651 @c FIXME: same as above, but no ``blocks'' line.
652 @macro multiplierSuffixesNoBlocks{varName}
653 @var{\varName\} may be, or may be an integer optionally followed by,
654 one of the following multiplicative suffixes:
655 @example
656 @samp{KB} =>           1000 (KiloBytes)
657 @samp{K}  =>           1024 (KibiBytes)
658 @samp{MB} =>      1000*1000 (MegaBytes)
659 @samp{M}  =>      1024*1024 (MebiBytes)
660 @samp{GB} => 1000*1000*1000 (GigaBytes)
661 @samp{G}  => 1024*1024*1024 (GibiBytes)
662 @end example
663 and so on for @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}.
664 @end macro
665
666 @cindex common options
667
668 Certain options are available in all of these programs.  Rather than
669 writing identical descriptions for each of the programs, they are
670 described here.  (In fact, every @sc{gnu} program accepts (or should accept)
671 these options.)
672
673 @vindex POSIXLY_CORRECT
674 Normally options and operands can appear in any order, and programs act
675 as if all the options appear before any operands.  For example,
676 @samp{sort -r passwd -t :} acts like @samp{sort -r -t : passwd}, since
677 @samp{:} is an option-argument of @option{-t}.  However, if the
678 @env{POSIXLY_CORRECT} environment variable is set, options must appear
679 before operands, unless otherwise specified for a particular command.
680
681 A few programs can usefully have trailing operands with leading
682 @samp{-}.  With such a program, options must precede operands even if
683 @env{POSIXLY_CORRECT} is not set, and this fact is noted in the
684 program description.  For example, the @command{env} command's options
685 must appear before its operands, since in some cases the operands
686 specify a command that itself contains options.
687
688 Most programs that accept long options recognize unambiguous
689 abbreviations of those options.  For example, @samp{rmdir
690 --ignore-fail-on-non-empty} can be invoked as @samp{rmdir
691 --ignore-fail} or even @samp{rmdir --i}.  Ambiguous options, such as
692 @samp{ls --h}, are identified as such.
693
694 Some of these programs recognize the @option{--help} and @option{--version}
695 options only when one of them is the sole command line argument.  For
696 these programs, abbreviations of the long options are not always recognized.
697
698 @table @samp
699
700 @item --help
701 @opindex --help
702 @cindex help, online
703 Print a usage message listing all available options, then exit successfully.
704
705 @item --version
706 @opindex --version
707 @cindex version number, finding
708 Print the version number, then exit successfully.
709
710 @item --
711 @opindex --
712 @cindex option delimiter
713 Delimit the option list.  Later arguments, if any, are treated as
714 operands even if they begin with @samp{-}.  For example, @samp{sort --
715 -r} reads from the file named @file{-r}.
716
717 @end table
718
719 @cindex standard input
720 @cindex standard output
721 A single @samp{-} operand is not really an option, though it looks like one.  It
722 stands for standard input, or for standard output if that is clear from
723 the context.  For example, @samp{sort -} reads from standard input,
724 and is equivalent to plain @samp{sort}, and @samp{tee -} writes an
725 extra copy of its input to standard output.  Unless otherwise
726 specified, @samp{-} can appear as any operand that requires a file
727 name.
728
729 @menu
730 * Exit status::                 Indicating program success or failure.
731 * Backup options::              -b -S, in some programs.
732 * Block size::                  BLOCK_SIZE and --block-size, in some programs.
733 * Floating point::              Floating point number representation.
734 * Signal specifications::       Specifying signals using the --signal option.
735 * Disambiguating names and IDs:: chgrp and chown owner and group syntax
736 * Random sources::              --random-source, in some programs.
737 * Target directory::            Specifying a target directory, in some programs.
738 * Trailing slashes::            --strip-trailing-slashes, in some programs.
739 * Traversing symlinks::         -H, -L, or -P, in some programs.
740 * Treating / specially::        --preserve-root and --no-preserve-root.
741 * Special built-in utilities::  @command{break}, @command{:}, @dots{}
742 * Standards conformance::       Conformance to the @acronym{POSIX} standard.
743 @end menu
744
745
746 @node Exit status
747 @section Exit status
748
749 @macro exitstatus
750 An exit status of zero indicates success,
751 and a nonzero value indicates failure.
752 @end macro
753
754 Nearly every command invocation yields an integral @dfn{exit status}
755 that can be used to change how other commands work.
756 For the vast majority of commands, an exit status of zero indicates
757 success.  Failure is indicated by a nonzero value---typically
758 @samp{1}, though it may differ on unusual platforms as @acronym{POSIX}
759 requires only that it be nonzero.
760
761 However, some of the programs documented here do produce
762 other exit status values and a few associate different
763 meanings with the values @samp{0} and @samp{1}.
764 Here are some of the exceptions:
765 @command{chroot}, @command{env}, @command{expr}, @command{nice},
766 @command{nohup}, @command{printenv}, @command{sort}, @command{stdbuf},
767 @command{su}, @command{test}, @command{timeout}, @command{tty}.
768
769
770 @node Backup options
771 @section Backup options
772
773 @cindex backup options
774
775 Some @sc{gnu} programs (at least @command{cp}, @command{install},
776 @command{ln}, and @command{mv}) optionally make backups of files
777 before writing new versions.
778 These options control the details of these backups.  The options are also
779 briefly mentioned in the descriptions of the particular programs.
780
781 @table @samp
782
783 @item -b
784 @itemx @w{@kbd{--backup}[=@var{method}]}
785 @opindex -b
786 @opindex --backup
787 @vindex VERSION_CONTROL
788 @cindex backups, making
789 Make a backup of each file that would otherwise be overwritten or removed.
790 Without this option, the original versions are destroyed.
791 Use @var{method} to determine the type of backups to make.
792 When this option is used but @var{method} is not specified,
793 then the value of the @env{VERSION_CONTROL}
794 environment variable is used.  And if @env{VERSION_CONTROL} is not set,
795 the default backup type is @samp{existing}.
796
797 Note that the short form of this option, @option{-b} does not accept any
798 argument.  Using @option{-b} is equivalent to using @option{--backup=existing}.
799
800 @vindex version-control @r{Emacs variable}
801 This option corresponds to the Emacs variable @samp{version-control};
802 the values for @var{method} are the same as those used in Emacs.
803 This option also accepts more descriptive names.
804 The valid @var{method}s are (unique abbreviations are accepted):
805
806 @table @samp
807 @item none
808 @itemx off
809 @opindex none @r{backup method}
810 Never make backups.
811
812 @item numbered
813 @itemx t
814 @opindex numbered @r{backup method}
815 Always make numbered backups.
816
817 @item existing
818 @itemx nil
819 @opindex existing @r{backup method}
820 Make numbered backups of files that already have them, simple backups
821 of the others.
822
823 @item simple
824 @itemx never
825 @opindex simple @r{backup method}
826 Always make simple backups.  Please note @samp{never} is not to be
827 confused with @samp{none}.
828
829 @end table
830
831 @item -S @var{suffix}
832 @itemx --suffix=@var{suffix}
833 @opindex -S
834 @opindex --suffix
835 @cindex backup suffix
836 @vindex SIMPLE_BACKUP_SUFFIX
837 Append @var{suffix} to each backup file made with @option{-b}.  If this
838 option is not specified, the value of the @env{SIMPLE_BACKUP_SUFFIX}
839 environment variable is used.  And if @env{SIMPLE_BACKUP_SUFFIX} is not
840 set, the default is @samp{~}, just as in Emacs.
841
842 @end table
843
844 @node Block size
845 @section Block size
846
847 @cindex block size
848
849 Some @sc{gnu} programs (at least @command{df}, @command{du}, and
850 @command{ls}) display sizes in ``blocks''.  You can adjust the block size
851 and method of display to make sizes easier to read.  The block size
852 used for display is independent of any file system block size.
853 Fractional block counts are rounded up to the nearest integer.
854
855 @opindex --block-size=@var{size}
856 @vindex BLOCKSIZE
857 @vindex BLOCK_SIZE
858 @vindex DF_BLOCK_SIZE
859 @vindex DU_BLOCK_SIZE
860 @vindex LS_BLOCK_SIZE
861 @vindex POSIXLY_CORRECT@r{, and block size}
862
863 The default block size is chosen by examining the following environment
864 variables in turn; the first one that is set determines the block size.
865
866 @table @code
867
868 @item DF_BLOCK_SIZE
869 This specifies the default block size for the @command{df} command.
870 Similarly, @env{DU_BLOCK_SIZE} specifies the default for @command{du} and
871 @env{LS_BLOCK_SIZE} for @command{ls}.
872
873 @item BLOCK_SIZE
874 This specifies the default block size for all three commands, if the
875 above command-specific environment variables are not set.
876
877 @item BLOCKSIZE
878 This specifies the default block size for all values that are normally
879 printed as blocks, if neither @env{BLOCK_SIZE} nor the above
880 command-specific environment variables are set.  Unlike the other
881 environment variables, @env{BLOCKSIZE} does not affect values that are
882 normally printed as byte counts, e.g., the file sizes contained in
883 @code{ls -l} output.
884
885 @item POSIXLY_CORRECT
886 If neither @env{@var{command}_BLOCK_SIZE}, nor @env{BLOCK_SIZE}, nor
887 @env{BLOCKSIZE} is set, but this variable is set, the block size
888 defaults to 512.
889
890 @end table
891
892 If none of the above environment variables are set, the block size
893 currently defaults to 1024 bytes in most contexts, but this number may
894 change in the future.  For @command{ls} file sizes, the block size
895 defaults to 1 byte.
896
897 @cindex human-readable output
898 @cindex SI output
899
900 A block size specification can be a positive integer specifying the number
901 of bytes per block, or it can be @code{human-readable} or @code{si} to
902 select a human-readable format.  Integers may be followed by suffixes
903 that are upward compatible with the
904 @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI prefixes}
905 for decimal multiples and with the
906 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
907 prefixes for binary multiples}.
908
909 With human-readable formats, output sizes are followed by a size letter
910 such as @samp{M} for megabytes.  @code{BLOCK_SIZE=human-readable} uses
911 powers of 1024; @samp{M} stands for 1,048,576 bytes.
912 @code{BLOCK_SIZE=si} is similar, but uses powers of 1000 and appends
913 @samp{B}; @samp{MB} stands for 1,000,000 bytes.
914
915 @vindex LC_NUMERIC
916 A block size specification preceded by @samp{'} causes output sizes to
917 be displayed with thousands separators.  The @env{LC_NUMERIC} locale
918 specifies the thousands separator and grouping.  For example, in an
919 American English locale, @samp{--block-size="'1kB"} would cause a size
920 of 1234000 bytes to be displayed as @samp{1,234}.  In the default C
921 locale, there is no thousands separator so a leading @samp{'} has no
922 effect.
923
924 An integer block size can be followed by a suffix to specify a
925 multiple of that size.  A bare size letter,
926 or one followed by @samp{iB}, specifies
927 a multiple using powers of 1024.  A size letter followed by @samp{B}
928 specifies powers of 1000 instead.  For example, @samp{1M} and
929 @samp{1MiB} are equivalent to @samp{1048576}, whereas @samp{1MB} is
930 equivalent to @samp{1000000}.
931
932 A plain suffix without a preceding integer acts as if @samp{1} were
933 prepended, except that it causes a size indication to be appended to
934 the output.  For example, @samp{--block-size="kB"} displays 3000 as
935 @samp{3kB}.
936
937 The following suffixes are defined.  Large sizes like @code{1Y}
938 may be rejected by your computer due to limitations of its arithmetic.
939
940 @table @samp
941 @item kB
942 @cindex kilobyte, definition of
943 kilobyte: @math{10^3 = 1000}.
944 @item k
945 @itemx K
946 @itemx KiB
947 @cindex kibibyte, definition of
948 kibibyte: @math{2^{10} = 1024}.  @samp{K} is special: the SI prefix is
949 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
950 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
951 @item MB
952 @cindex megabyte, definition of
953 megabyte: @math{10^6 = 1,000,000}.
954 @item M
955 @itemx MiB
956 @cindex mebibyte, definition of
957 mebibyte: @math{2^{20} = 1,048,576}.
958 @item GB
959 @cindex gigabyte, definition of
960 gigabyte: @math{10^9 = 1,000,000,000}.
961 @item G
962 @itemx GiB
963 @cindex gibibyte, definition of
964 gibibyte: @math{2^{30} = 1,073,741,824}.
965 @item TB
966 @cindex terabyte, definition of
967 terabyte:  @math{10^{12} = 1,000,000,000,000}.
968 @item T
969 @itemx TiB
970 @cindex tebibyte, definition of
971 tebibyte: @math{2^{40} = 1,099,511,627,776}.
972 @item PB
973 @cindex petabyte, definition of
974 petabyte: @math{10^{15} = 1,000,000,000,000,000}.
975 @item P
976 @itemx PiB
977 @cindex pebibyte, definition of
978 pebibyte: @math{2^{50} = 1,125,899,906,842,624}.
979 @item EB
980 @cindex exabyte, definition of
981 exabyte: @math{10^{18} = 1,000,000,000,000,000,000}.
982 @item E
983 @itemx EiB
984 @cindex exbibyte, definition of
985 exbibyte: @math{2^{60} = 1,152,921,504,606,846,976}.
986 @item ZB
987 @cindex zettabyte, definition of
988 zettabyte: @math{10^{21} = 1,000,000,000,000,000,000,000}
989 @item Z
990 @itemx ZiB
991 @math{2^{70} = 1,180,591,620,717,411,303,424}.
992 (@samp{Zi} is a @acronym{GNU} extension to IEC 60027-2.)
993 @item YB
994 @cindex yottabyte, definition of
995 yottabyte: @math{10^{24} = 1,000,000,000,000,000,000,000,000}.
996 @item Y
997 @itemx YiB
998 @math{2^{80} = 1,208,925,819,614,629,174,706,176}.
999 (@samp{Yi} is a @acronym{GNU} extension to IEC 60027-2.)
1000 @end table
1001
1002 @opindex -k
1003 @opindex -h
1004 @opindex --block-size
1005 @opindex --human-readable
1006 @opindex --si
1007
1008 Block size defaults can be overridden by an explicit
1009 @option{--block-size=@var{size}} option.  The @option{-k}
1010 option is equivalent to @option{--block-size=1K}, which
1011 is the default unless the @env{POSIXLY_CORRECT} environment variable is
1012 set.  The @option{-h} or @option{--human-readable} option is equivalent to
1013 @option{--block-size=human-readable}.  The @option{--si} option is
1014 equivalent to @option{--block-size=si}.
1015
1016 @node Floating point
1017 @section Floating point numbers
1018 @cindex floating point
1019 @cindex IEEE floating point
1020
1021 Commands that accept or produce floating point numbers employ the
1022 floating point representation of the underlying system, and suffer
1023 from rounding error, overflow, and similar floating-point issues.
1024 Almost all modern systems use IEEE-754 floating point, and it is
1025 typically portable to assume IEEE-754 behavior these days.  IEEE-754
1026 has positive and negative infinity, distinguishes positive from
1027 negative zero, and uses special values called NaNs to represent
1028 invalid computations such as dividing zero by itself.  For more
1029 information, please see David Goldberg's paper
1030 @uref{http://@/www.validlab.com/@/goldberg/@/paper.pdf, What Every
1031 Computer Scientist Should Know About Floating-Point Arithmetic}.
1032
1033 @vindex LC_NUMERIC
1034 Commands that accept floating point numbers as options, operands or
1035 input use the standard C functions @code{strtod} and @code{strtold} to
1036 convert from text to floating point numbers.  These floating point
1037 numbers therefore can use scientific notation like @code{1.0e-34} and
1038 @code{-10e100}.  Modern C implementations also accept hexadecimal
1039 floating point numbers such as @code{-0x.ep-3}, which stands for
1040 @minus{}14/16 times @math{2^-3}, which equals @minus{}0.109375.  The
1041 @env{LC_NUMERIC} locale determines the decimal-point character.
1042 @xref{Parsing of Floats,,, libc, The GNU C Library Reference Manual}.
1043
1044 @node Signal specifications
1045 @section Signal specifications
1046 @cindex signals, specifying
1047
1048 A @var{signal} may be a signal name like @samp{HUP}, or a signal
1049 number like @samp{1}, or an exit status of a process terminated by the
1050 signal.  A signal name can be given in canonical form or prefixed by
1051 @samp{SIG}.  The case of the letters is ignored. The following signal names
1052 and numbers are supported on all @acronym{POSIX} compliant systems:
1053
1054 @table @samp
1055 @item HUP
1056 1.  Hangup.
1057 @item INT
1058 2.  Terminal interrupt.
1059 @item QUIT
1060 3.  Terminal quit.
1061 @item ABRT
1062 6.  Process abort.
1063 @item KILL
1064 9.  Kill (cannot be caught or ignored).
1065 @item ALRM
1066 14.  Alarm Clock.
1067 @item TERM
1068 15.  Termination.
1069 @end table
1070
1071 @noindent
1072 Other supported signal names have system-dependent corresponding
1073 numbers.  All systems conforming to @acronym{POSIX} 1003.1-2001 also
1074 support the following signals:
1075
1076 @table @samp
1077 @item BUS
1078 Access to an undefined portion of a memory object.
1079 @item CHLD
1080 Child process terminated, stopped, or continued.
1081 @item CONT
1082 Continue executing, if stopped.
1083 @item FPE
1084 Erroneous arithmetic operation.
1085 @item ILL
1086 Illegal Instruction.
1087 @item PIPE
1088 Write on a pipe with no one to read it.
1089 @item SEGV
1090 Invalid memory reference.
1091 @item STOP
1092 Stop executing (cannot be caught or ignored).
1093 @item TSTP
1094 Terminal stop.
1095 @item TTIN
1096 Background process attempting read.
1097 @item TTOU
1098 Background process attempting write.
1099 @item URG
1100 High bandwidth data is available at a socket.
1101 @item USR1
1102 User-defined signal 1.
1103 @item USR2
1104 User-defined signal 2.
1105 @end table
1106
1107 @noindent
1108 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XSI} extension
1109 also support the following signals:
1110
1111 @table @samp
1112 @item POLL
1113 Pollable event.
1114 @item PROF
1115 Profiling timer expired.
1116 @item SYS
1117 Bad system call.
1118 @item TRAP
1119 Trace/breakpoint trap.
1120 @item VTALRM
1121 Virtual timer expired.
1122 @item XCPU
1123 CPU time limit exceeded.
1124 @item XFSZ
1125 File size limit exceeded.
1126 @end table
1127
1128 @noindent
1129 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XRT} extension
1130 also support at least eight real-time signals called @samp{RTMIN},
1131 @samp{RTMIN+1}, @dots{}, @samp{RTMAX-1}, @samp{RTMAX}.
1132
1133 @node Disambiguating names and IDs
1134 @section chown and chgrp: Disambiguating user names and IDs
1135 @cindex user names, disambiguating
1136 @cindex user IDs, disambiguating
1137 @cindex group names, disambiguating
1138 @cindex group IDs, disambiguating
1139 @cindex disambiguating group names and IDs
1140
1141 Since the @var{owner} and @var{group} arguments to @command{chown} and
1142 @command{chgrp} may be specified as names or numeric IDs, there is an
1143 apparent ambiguity.
1144 What if a user or group @emph{name} is a string of digits?
1145 @footnote{Using a number as a user name is common in some environments.}
1146 Should the command interpret it as a user name or as an ID?
1147 @acronym{POSIX} requires that @command{chown} and @command{chgrp}
1148 first attempt to resolve the specified string as a name, and
1149 only once that fails, then try to interpret it as an ID.
1150 This is troublesome when you want to specify a numeric ID, say 42,
1151 and it must work even in a pathological situation where
1152 @samp{42} is a user name that maps to some other user ID, say 1000.
1153 Simply invoking @code{chown 42 F}, will set @file{F}s owner ID to
1154 1000---not what you intended.
1155
1156 GNU @command{chown} and @command{chgrp} provide a way to work around this,
1157 that at the same time may result in a significant performance improvement
1158 by eliminating a database look-up.
1159 Simply precede each numeric user ID and/or group ID with a @samp{+},
1160 in order to force its interpretation as an integer:
1161
1162 @example
1163 chown +42 F
1164 chgrp +$numeric_group_id another-file
1165 chown +0:+0 /
1166 @end example
1167
1168 GNU @command{chown} and @command{chgrp}
1169 skip the name look-up process for each @samp{+}-prefixed string,
1170 because a string containing @samp{+} is never a valid user or group name.
1171 This syntax is accepted on most common Unix systems, but not on Solaris 10.
1172
1173 @node Random sources
1174 @section Sources of random data
1175
1176 @cindex random sources
1177
1178 The @command{shuf}, @command{shred}, and @command{sort} commands
1179 sometimes need random data to do their work.  For example, @samp{sort
1180 -R} must choose a hash function at random, and it needs random data to
1181 make this selection.
1182
1183 By default these commands use an internal pseudorandom generator
1184 initialized by a small amount of entropy, but can be directed to use
1185 an external source with the @option{--random-source=@var{file}} option.
1186 An error is reported if @var{file} does not contain enough bytes.
1187
1188 For example, the device file @file{/dev/urandom} could be used as the
1189 source of random data.  Typically, this device gathers environmental
1190 noise from device drivers and other sources into an entropy pool, and
1191 uses the pool to generate random bits.  If the pool is short of data,
1192 the device reuses the internal pool to produce more bits, using a
1193 cryptographically secure pseudorandom number generator.  But be aware
1194 that this device is not designed for bulk random data generation
1195 and is relatively slow.
1196
1197 @file{/dev/urandom} suffices for most practical uses, but applications
1198 requiring high-value or long-term protection of private data may
1199 require an alternate data source like @file{/dev/random} or
1200 @file{/dev/arandom}.  The set of available sources depends on your
1201 operating system.
1202
1203 To reproduce the results of an earlier invocation of a command, you
1204 can save some random data into a file and then use that file as the
1205 random source in earlier and later invocations of the command.
1206
1207 @node Target directory
1208 @section Target directory
1209
1210 @cindex target directory
1211
1212 The @command{cp}, @command{install}, @command{ln}, and @command{mv}
1213 commands normally treat the last operand specially when it is a
1214 directory or a symbolic link to a directory.  For example, @samp{cp
1215 source dest} is equivalent to @samp{cp source dest/source} if
1216 @file{dest} is a directory.  Sometimes this behavior is not exactly
1217 what is wanted, so these commands support the following options to
1218 allow more fine-grained control:
1219
1220 @table @samp
1221
1222 @item -T
1223 @itemx --no-target-directory
1224 @opindex --no-target-directory
1225 @cindex target directory
1226 @cindex destination directory
1227 Do not treat the last operand specially when it is a directory or a
1228 symbolic link to a directory.  This can help avoid race conditions in
1229 programs that operate in a shared area.  For example, when the command
1230 @samp{mv /tmp/source /tmp/dest} succeeds, there is no guarantee that
1231 @file{/tmp/source} was renamed to @file{/tmp/dest}: it could have been
1232 renamed to @file{/tmp/dest/source} instead, if some other process
1233 created @file{/tmp/dest} as a directory.  However, if @file{mv
1234 -T /tmp/source /tmp/dest} succeeds, there is no
1235 question that @file{/tmp/source} was renamed to @file{/tmp/dest}.
1236
1237 In the opposite situation, where you want the last operand to be
1238 treated as a directory and want a diagnostic otherwise, you can use
1239 the @option{--target-directory} (@option{-t}) option.
1240
1241 @item -t @var{directory}
1242 @itemx @w{@kbd{--target-directory}=@var{directory}}
1243 @opindex --target-directory
1244 @cindex target directory
1245 @cindex destination directory
1246 Use @var{directory} as the directory component of each destination
1247 file name.
1248
1249 The interface for most programs is that after processing options and a
1250 finite (possibly zero) number of fixed-position arguments, the remaining
1251 argument list is either expected to be empty, or is a list of items
1252 (usually files) that will all be handled identically.  The @command{xargs}
1253 program is designed to work well with this convention.
1254
1255 The commands in the @command{mv}-family are unusual in that they take
1256 a variable number of arguments with a special case at the @emph{end}
1257 (namely, the target directory).  This makes it nontrivial to perform some
1258 operations, e.g., ``move all files from here to ../d/'', because
1259 @code{mv * ../d/} might exhaust the argument space, and @code{ls | xargs ...}
1260 doesn't have a clean way to specify an extra final argument for each
1261 invocation of the subject command.  (It can be done by going through a
1262 shell command, but that requires more human labor and brain power than
1263 it should.)
1264
1265 The @w{@kbd{--target-directory}} (@option{-t}) option allows the @command{cp},
1266 @command{install}, @command{ln}, and @command{mv} programs to be used
1267 conveniently with @command{xargs}.  For example, you can move the files
1268 from the current directory to a sibling directory, @code{d} like this:
1269
1270 @smallexample
1271 ls | xargs mv -t ../d --
1272 @end smallexample
1273
1274 However, this doesn't move files whose names begin with @samp{.}.
1275 If you use the @sc{gnu} @command{find} program, you can move those
1276 files too, with this command:
1277
1278 @example
1279 find . -mindepth 1 -maxdepth 1 \
1280   | xargs mv -t ../d
1281 @end example
1282
1283 But both of the above approaches fail if there are no files in the
1284 current directory, or if any file has a name containing a blank or
1285 some other special characters.
1286 The following example removes those limitations and requires both
1287 @sc{gnu} @command{find} and @sc{gnu} @command{xargs}:
1288
1289 @example
1290 find . -mindepth 1 -maxdepth 1 -print0 \
1291   | xargs --null --no-run-if-empty \
1292       mv -t ../d
1293 @end example
1294
1295 @end table
1296
1297 @noindent
1298 The @option{--target-directory} (@option{-t}) and
1299 @option{--no-target-directory} (@option{-T})
1300 options cannot be combined.
1301
1302 @node Trailing slashes
1303 @section Trailing slashes
1304
1305 @cindex trailing slashes
1306
1307 Some @sc{gnu} programs (at least @command{cp} and @command{mv}) allow you to
1308 remove any trailing slashes from each @var{source} argument before
1309 operating on it.  The @w{@kbd{--strip-trailing-slashes}} option enables
1310 this behavior.
1311
1312 This is useful when a @var{source} argument may have a trailing slash and
1313 @c FIXME: mv's behavior in this case is system-dependent
1314 specify a symbolic link to a directory.  This scenario is in fact rather
1315 common because some shells can automatically append a trailing slash when
1316 performing file name completion on such symbolic links.  Without this
1317 option, @command{mv}, for example, (via the system's rename function) must
1318 interpret a trailing slash as a request to dereference the symbolic link
1319 and so must rename the indirectly referenced @emph{directory} and not
1320 the symbolic link.  Although it may seem surprising that such behavior
1321 be the default, it is required by @acronym{POSIX} and is consistent with
1322 other parts of that standard.
1323
1324 @node Traversing symlinks
1325 @section Traversing symlinks
1326
1327 @cindex symbolic link to directory, controlling traversal of
1328
1329 The following options modify how @command{chown} and @command{chgrp}
1330 @c FIXME: note that `du' has these options, too, but they have slightly
1331 @c different meaning.
1332 traverse a hierarchy when the @option{--recursive} (@option{-R})
1333 option is also specified.
1334 If more than one of the following options is specified, only the final
1335 one takes effect.
1336 These options specify whether processing a symbolic link to a directory
1337 entails operating on just the symbolic link or on all files in the
1338 hierarchy rooted at that directory.
1339
1340 These options are independent of @option{--dereference} and
1341 @option{--no-dereference} (@option{-h}), which control whether to modify
1342 a symlink or its referent.
1343
1344 @table @samp
1345
1346 @macro choptH
1347 @item -H
1348 @opindex -H
1349 @cindex symbolic link to directory, traverse if on the command line
1350 If @option{--recursive} (@option{-R}) is specified and
1351 a command line argument is a symbolic link to a directory, traverse it.
1352 @end macro
1353 @choptH
1354
1355 @macro choptL
1356 @item -L
1357 @opindex -L
1358 @cindex symbolic link to directory, traverse each that is encountered
1359 In a recursive traversal, traverse every symbolic link to a directory
1360 that is encountered.
1361 @end macro
1362 @choptL
1363
1364 @macro choptP
1365 @item -P
1366 @opindex -P
1367 @cindex symbolic link to directory, never traverse
1368 Do not traverse any symbolic links.
1369 This is the default if none of @option{-H}, @option{-L},
1370 or @option{-P} is specified.
1371 @end macro
1372 @choptP
1373
1374 @end table
1375
1376
1377 @node Treating / specially
1378 @section Treating @file{/} specially
1379
1380 Certain commands can operate destructively on entire hierarchies.
1381 For example, if a user with appropriate privileges mistakenly runs
1382 @samp{rm -rf / tmp/junk}, that may remove
1383 all files on the entire system.  Since there are so few
1384 legitimate uses for such a command,
1385 @sc{gnu} @command{rm} normally declines to operate on any directory
1386 that resolves to @file{/}.  If you really want to try to remove all
1387 the files on your system, you can use the @option{--no-preserve-root}
1388 option, but the default behavior, specified by the
1389 @option{--preserve-option}, is safer for most purposes.
1390
1391 The commands @command{chgrp}, @command{chmod} and @command{chown}
1392 can also operate destructively on entire hierarchies, so they too
1393 support these options.  Although, unlike @command{rm}, they don't
1394 actually unlink files, these commands are arguably more dangerous
1395 when operating recursively on @file{/}, since they often work much
1396 more quickly, and hence damage more files before an alert user can
1397 interrupt them.  Tradition and @acronym{POSIX} require these commands
1398 to operate recursively on @file{/}, so they default to
1399 @option{--no-preserve-root}, but using the @option{--preserve-root}
1400 option makes them safer for most purposes.  For convenience you can
1401 specify @option{--preserve-root} in an alias or in a shell function.
1402
1403 Note that the @option{--preserve-root} option also ensures
1404 that @command{chgrp} and @command{chown} do not modify @file{/}
1405 even when dereferencing a symlink pointing to @file{/}.
1406
1407 @node Special built-in utilities
1408 @section Special built-in utilities
1409
1410 Some programs like @command{nice} can invoke other programs; for
1411 example, the command @samp{nice cat file} invokes the program
1412 @command{cat} by executing the command @samp{cat file}.  However,
1413 @dfn{special built-in utilities} like @command{exit} cannot be invoked
1414 this way.  For example, the command @samp{nice exit} does not have a
1415 well-defined behavior: it may generate an error message instead of
1416 exiting.
1417
1418 Here is a list of the special built-in utilities that are standardized
1419 by @acronym{POSIX} 1003.1-2004.
1420
1421 @quotation
1422 @t{.@: : break continue eval exec exit export readonly
1423 return set shift times trap unset}
1424 @end quotation
1425
1426 For example, because @samp{.}, @samp{:}, and @samp{exec} are special,
1427 the commands @samp{nice . foo.sh}, @samp{nice :}, and @samp{nice exec
1428 pwd} do not work as you might expect.
1429
1430 Many shells extend this list.  For example, Bash has several extra
1431 special built-in utilities like @command{history}, and
1432 @command{suspend}, and with Bash the command @samp{nice suspend}
1433 generates an error message instead of suspending.
1434
1435 @node Standards conformance
1436 @section Standards conformance
1437
1438 @vindex POSIXLY_CORRECT
1439 In a few cases, the @sc{gnu} utilities' default behavior is
1440 incompatible with the @acronym{POSIX} standard.  To suppress these
1441 incompatibilities, define the @env{POSIXLY_CORRECT} environment
1442 variable.  Unless you are checking for @acronym{POSIX} conformance, you
1443 probably do not need to define @env{POSIXLY_CORRECT}.
1444
1445 Newer versions of @acronym{POSIX} are occasionally incompatible with older
1446 versions.  For example, older versions of @acronym{POSIX} required the
1447 command @samp{sort +1} to sort based on the second and succeeding
1448 fields in each input line, but starting with @acronym{POSIX} 1003.1-2001
1449 the same command is required to sort the file named @file{+1}, and you
1450 must instead use the command @samp{sort -k 2} to get the field-based
1451 sort.
1452
1453 @vindex _POSIX2_VERSION
1454 The @sc{gnu} utilities normally conform to the version of @acronym{POSIX}
1455 that is standard for your system.  To cause them to conform to a
1456 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
1457 environment variable to a value of the form @var{yyyymm} specifying
1458 the year and month the standard was adopted.  Three values are currently
1459 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
1460 @acronym{POSIX} 1003.2-1992, @samp{200112} stands for @acronym{POSIX}
1461 1003.1-2001, and @samp{200809} stands for @acronym{POSIX} 1003.1-2008.
1462 For example, if you have a newer system but are running software
1463 that assumes an older version of @acronym{POSIX} and uses @samp{sort +1}
1464 or @samp{tail +10}, you can work around any compatibility problems by setting
1465 @samp{_POSIX2_VERSION=199209} in your environment.
1466
1467 @node Output of entire files
1468 @chapter Output of entire files
1469
1470 @cindex output of entire files
1471 @cindex entire files, output of
1472
1473 These commands read and write entire files, possibly transforming them
1474 in some way.
1475
1476 @menu
1477 * cat invocation::              Concatenate and write files.
1478 * tac invocation::              Concatenate and write files in reverse.
1479 * nl invocation::               Number lines and write files.
1480 * od invocation::               Write files in octal or other formats.
1481 * base64 invocation::           Transform data into printable data.
1482 @end menu
1483
1484 @node cat invocation
1485 @section @command{cat}: Concatenate and write files
1486
1487 @pindex cat
1488 @cindex concatenate and write files
1489 @cindex copying files
1490
1491 @command{cat} copies each @var{file} (@samp{-} means standard input), or
1492 standard input if none are given, to standard output.  Synopsis:
1493
1494 @example
1495 cat [@var{option}] [@var{file}]@dots{}
1496 @end example
1497
1498 The program accepts the following options.  Also see @ref{Common options}.
1499
1500 @table @samp
1501
1502 @item -A
1503 @itemx --show-all
1504 @opindex -A
1505 @opindex --show-all
1506 Equivalent to @option{-vET}.
1507
1508 @item -b
1509 @itemx --number-nonblank
1510 @opindex -b
1511 @opindex --number-nonblank
1512 Number all nonempty output lines, starting with 1.
1513
1514 @item -e
1515 @opindex -e
1516 Equivalent to @option{-vE}.
1517
1518 @item -E
1519 @itemx --show-ends
1520 @opindex -E
1521 @opindex --show-ends
1522 Display a @samp{$} after the end of each line.
1523
1524 @item -n
1525 @itemx --number
1526 @opindex -n
1527 @opindex --number
1528 Number all output lines, starting with 1.  This option is ignored
1529 if @option{-b} is in effect.
1530
1531 @item -s
1532 @itemx --squeeze-blank
1533 @opindex -s
1534 @opindex --squeeze-blank
1535 @cindex squeezing empty lines
1536 Suppress repeated adjacent empty lines; output just one empty line
1537 instead of several.
1538
1539 @item -t
1540 @opindex -t
1541 Equivalent to @option{-vT}.
1542
1543 @item -T
1544 @itemx --show-tabs
1545 @opindex -T
1546 @opindex --show-tabs
1547 Display TAB characters as @samp{^I}.
1548
1549 @item -u
1550 @opindex -u
1551 Ignored; for @acronym{POSIX} compatibility.
1552
1553 @item -v
1554 @itemx --show-nonprinting
1555 @opindex -v
1556 @opindex --show-nonprinting
1557 Display control characters except for LFD and TAB using
1558 @samp{^} notation and precede characters that have the high bit set with
1559 @samp{M-}.
1560
1561 @end table
1562
1563 On systems like MS-DOS that distinguish between text and binary files,
1564 @command{cat} normally reads and writes in binary mode.  However,
1565 @command{cat} reads in text mode if one of the options
1566 @option{-bensAE} is used or if @command{cat} is reading from standard
1567 input and standard input is a terminal.  Similarly, @command{cat}
1568 writes in text mode if one of the options @option{-bensAE} is used or
1569 if standard output is a terminal.
1570
1571 @exitstatus
1572
1573 Examples:
1574
1575 @smallexample
1576 # Output f's contents, then standard input, then g's contents.
1577 cat f - g
1578
1579 # Copy standard input to standard output.
1580 cat
1581 @end smallexample
1582
1583
1584 @node tac invocation
1585 @section @command{tac}: Concatenate and write files in reverse
1586
1587 @pindex tac
1588 @cindex reversing files
1589
1590 @command{tac} copies each @var{file} (@samp{-} means standard input), or
1591 standard input if none are given, to standard output, reversing the
1592 records (lines by default) in each separately.  Synopsis:
1593
1594 @example
1595 tac [@var{option}]@dots{} [@var{file}]@dots{}
1596 @end example
1597
1598 @dfn{Records} are separated by instances of a string (newline by
1599 default).  By default, this separator string is attached to the end of
1600 the record that it follows in the file.
1601
1602 The program accepts the following options.  Also see @ref{Common options}.
1603
1604 @table @samp
1605
1606 @item -b
1607 @itemx --before
1608 @opindex -b
1609 @opindex --before
1610 The separator is attached to the beginning of the record that it
1611 precedes in the file.
1612
1613 @item -r
1614 @itemx --regex
1615 @opindex -r
1616 @opindex --regex
1617 Treat the separator string as a regular expression.  Users of @command{tac}
1618 on MS-DOS/MS-Windows should note that, since @command{tac} reads files in
1619 binary mode, each line of a text file might end with a CR/LF pair
1620 instead of the Unix-style LF.
1621
1622 @item -s @var{separator}
1623 @itemx --separator=@var{separator}
1624 @opindex -s
1625 @opindex --separator
1626 Use @var{separator} as the record separator, instead of newline.
1627
1628 @end table
1629
1630 @exitstatus
1631
1632
1633 @node nl invocation
1634 @section @command{nl}: Number lines and write files
1635
1636 @pindex nl
1637 @cindex numbering lines
1638 @cindex line numbering
1639
1640 @command{nl} writes each @var{file} (@samp{-} means standard input), or
1641 standard input if none are given, to standard output, with line numbers
1642 added to some or all of the lines.  Synopsis:
1643
1644 @example
1645 nl [@var{option}]@dots{} [@var{file}]@dots{}
1646 @end example
1647
1648 @cindex logical pages, numbering on
1649 @command{nl} decomposes its input into (logical) pages; by default, the
1650 line number is reset to 1 at the top of each logical page.  @command{nl}
1651 treats all of the input files as a single document; it does not reset
1652 line numbers or logical pages between files.
1653
1654 @cindex headers, numbering
1655 @cindex body, numbering
1656 @cindex footers, numbering
1657 A logical page consists of three sections: header, body, and footer.
1658 Any of the sections can be empty.  Each can be numbered in a different
1659 style from the others.
1660
1661 The beginnings of the sections of logical pages are indicated in the
1662 input file by a line containing exactly one of these delimiter strings:
1663
1664 @table @samp
1665 @item \:\:\:
1666 start of header;
1667 @item \:\:
1668 start of body;
1669 @item \:
1670 start of footer.
1671 @end table
1672
1673 The two characters from which these strings are made can be changed from
1674 @samp{\} and @samp{:} via options (see below), but the pattern and
1675 length of each string cannot be changed.
1676
1677 A section delimiter is replaced by an empty line on output.  Any text
1678 that comes before the first section delimiter string in the input file
1679 is considered to be part of a body section, so @command{nl} treats a
1680 file that contains no section delimiters as a single body section.
1681
1682 The program accepts the following options.  Also see @ref{Common options}.
1683
1684 @table @samp
1685
1686 @item -b @var{style}
1687 @itemx --body-numbering=@var{style}
1688 @opindex -b
1689 @opindex --body-numbering
1690 Select the numbering style for lines in the body section of each
1691 logical page.  When a line is not numbered, the current line number
1692 is not incremented, but the line number separator character is still
1693 prepended to the line.  The styles are:
1694
1695 @table @samp
1696 @item a
1697 number all lines,
1698 @item t
1699 number only nonempty lines (default for body),
1700 @item n
1701 do not number lines (default for header and footer),
1702 @item p@var{bre}
1703 number only lines that contain a match for the basic regular
1704 expression @var{bre}.
1705 @xref{Regular Expressions, , Regular Expressions, grep, The GNU Grep Manual}.
1706 @end table
1707
1708 @item -d @var{cd}
1709 @itemx --section-delimiter=@var{cd}
1710 @opindex -d
1711 @opindex --section-delimiter
1712 @cindex section delimiters of pages
1713 Set the section delimiter characters to @var{cd}; default is
1714 @samp{\:}.  If only @var{c} is given, the second remains @samp{:}.
1715 (Remember to protect @samp{\} or other metacharacters from shell
1716 expansion with quotes or extra backslashes.)
1717
1718 @item -f @var{style}
1719 @itemx --footer-numbering=@var{style}
1720 @opindex -f
1721 @opindex --footer-numbering
1722 Analogous to @option{--body-numbering}.
1723
1724 @item -h @var{style}
1725 @itemx --header-numbering=@var{style}
1726 @opindex -h
1727 @opindex --header-numbering
1728 Analogous to @option{--body-numbering}.
1729
1730 @item -i @var{number}
1731 @itemx --line-increment=@var{number}
1732 @opindex -i
1733 @opindex --line-increment
1734 Increment line numbers by @var{number} (default 1).
1735
1736 @item -l @var{number}
1737 @itemx --join-blank-lines=@var{number}
1738 @opindex -l
1739 @opindex --join-blank-lines
1740 @cindex empty lines, numbering
1741 @cindex blank lines, numbering
1742 Consider @var{number} (default 1) consecutive empty lines to be one
1743 logical line for numbering, and only number the last one.  Where fewer
1744 than @var{number} consecutive empty lines occur, do not number them.
1745 An empty line is one that contains no characters, not even spaces
1746 or tabs.
1747
1748 @item -n @var{format}
1749 @itemx --number-format=@var{format}
1750 @opindex -n
1751 @opindex --number-format
1752 Select the line numbering format (default is @code{rn}):
1753
1754 @table @samp
1755 @item ln
1756 @opindex ln @r{format for @command{nl}}
1757 left justified, no leading zeros;
1758 @item rn
1759 @opindex rn @r{format for @command{nl}}
1760 right justified, no leading zeros;
1761 @item rz
1762 @opindex rz @r{format for @command{nl}}
1763 right justified, leading zeros.
1764 @end table
1765
1766 @item -p
1767 @itemx --no-renumber
1768 @opindex -p
1769 @opindex --no-renumber
1770 Do not reset the line number at the start of a logical page.
1771
1772 @item -s @var{string}
1773 @itemx --number-separator=@var{string}
1774 @opindex -s
1775 @opindex --number-separator
1776 Separate the line number from the text line in the output with
1777 @var{string} (default is the TAB character).
1778
1779 @item -v @var{number}
1780 @itemx --starting-line-number=@var{number}
1781 @opindex -v
1782 @opindex --starting-line-number
1783 Set the initial line number on each logical page to @var{number} (default 1).
1784
1785 @item -w @var{number}
1786 @itemx --number-width=@var{number}
1787 @opindex -w
1788 @opindex --number-width
1789 Use @var{number} characters for line numbers (default 6).
1790
1791 @end table
1792
1793 @exitstatus
1794
1795
1796 @node od invocation
1797 @section @command{od}: Write files in octal or other formats
1798
1799 @pindex od
1800 @cindex octal dump of files
1801 @cindex hex dump of files
1802 @cindex ASCII dump of files
1803 @cindex file contents, dumping unambiguously
1804
1805 @command{od} writes an unambiguous representation of each @var{file}
1806 (@samp{-} means standard input), or standard input if none are given.
1807 Synopses:
1808
1809 @smallexample
1810 od [@var{option}]@dots{} [@var{file}]@dots{}
1811 od [-abcdfilosx]@dots{} [@var{file}] [[+]@var{offset}[.][b]]
1812 od [@var{option}]@dots{} --traditional [@var{file}]@c
1813  [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1814 @end smallexample
1815
1816 Each line of output consists of the offset in the input, followed by
1817 groups of data from the file.  By default, @command{od} prints the offset in
1818 octal, and each group of file data is a C @code{short int}'s worth of input
1819 printed as a single octal number.
1820
1821 If @var{offset} is given, it specifies how many input bytes to skip
1822 before formatting and writing.  By default, it is interpreted as an
1823 octal number, but the optional trailing decimal point causes it to be
1824 interpreted as decimal.  If no decimal is specified and the offset
1825 begins with @samp{0x} or @samp{0X} it is interpreted as a hexadecimal
1826 number.  If there is a trailing @samp{b}, the number of bytes skipped
1827 will be @var{offset} multiplied by 512.
1828
1829 If a command is of both the first and second forms, the second form is
1830 assumed if the last operand begins with @samp{+} or (if there are two
1831 operands) a digit.  For example, in @samp{od foo 10} and @samp{od +10}
1832 the @samp{10} is an offset, whereas in @samp{od 10} the @samp{10} is a
1833 file name.
1834
1835 The program accepts the following options.  Also see @ref{Common options}.
1836
1837 @table @samp
1838
1839 @item -A @var{radix}
1840 @itemx --address-radix=@var{radix}
1841 @opindex -A
1842 @opindex --address-radix
1843 @cindex radix for file offsets
1844 @cindex file offset radix
1845 Select the base in which file offsets are printed.  @var{radix} can
1846 be one of the following:
1847
1848 @table @samp
1849 @item d
1850 decimal;
1851 @item o
1852 octal;
1853 @item x
1854 hexadecimal;
1855 @item n
1856 none (do not print offsets).
1857 @end table
1858
1859 The default is octal.
1860
1861 @item -j @var{bytes}
1862 @itemx --skip-bytes=@var{bytes}
1863 @opindex -j
1864 @opindex --skip-bytes
1865 Skip @var{bytes} input bytes before formatting and writing.  If
1866 @var{bytes} begins with @samp{0x} or @samp{0X}, it is interpreted in
1867 hexadecimal; otherwise, if it begins with @samp{0}, in octal; otherwise,
1868 in decimal.
1869 @multiplierSuffixes{bytes}
1870
1871 @item -N @var{bytes}
1872 @itemx --read-bytes=@var{bytes}
1873 @opindex -N
1874 @opindex --read-bytes
1875 Output at most @var{bytes} bytes of the input.  Prefixes and suffixes on
1876 @code{bytes} are interpreted as for the @option{-j} option.
1877
1878 @item -S @var{bytes}
1879 @itemx --strings[=@var{bytes}]
1880 @opindex -S
1881 @opindex --strings
1882 @cindex string constants, outputting
1883 Instead of the normal output, output only @dfn{string constants}: at
1884 least @var{bytes} consecutive @acronym{ASCII} graphic characters,
1885 followed by a zero byte (@acronym{ASCII} @sc{nul}).
1886 Prefixes and suffixes on @var{bytes} are interpreted as for the
1887 @option{-j} option.
1888
1889 If @var{n} is omitted with @option{--strings}, the default is 3.
1890
1891 @item -t @var{type}
1892 @itemx --format=@var{type}
1893 @opindex -t
1894 @opindex --format
1895 Select the format in which to output the file data.  @var{type} is a
1896 string of one or more of the below type indicator characters.  If you
1897 include more than one type indicator character in a single @var{type}
1898 string, or use this option more than once, @command{od} writes one copy
1899 of each output line using each of the data types that you specified,
1900 in the order that you specified.
1901
1902 Adding a trailing ``z'' to any type specification appends a display
1903 of the @acronym{ASCII} character representation of the printable characters
1904 to the output line generated by the type specification.
1905
1906 @table @samp
1907 @item a
1908 named character, ignoring high-order bit
1909 @item c
1910 @acronym{ASCII} character or backslash escape,
1911 @item d
1912 signed decimal
1913 @item f
1914 floating point (@pxref{Floating point})
1915 @item o
1916 octal
1917 @item u
1918 unsigned decimal
1919 @item x
1920 hexadecimal
1921 @end table
1922
1923 The type @code{a} outputs things like @samp{sp} for space, @samp{nl} for
1924 newline, and @samp{nul} for a zero byte.  Only the least significant
1925 seven bits of each byte is used; the high-order bit is ignored.
1926 Type @code{c} outputs
1927 @samp{ }, @samp{\n}, and @code{\0}, respectively.
1928
1929 @cindex type size
1930 Except for types @samp{a} and @samp{c}, you can specify the number
1931 of bytes to use in interpreting each number in the given data type
1932 by following the type indicator character with a decimal integer.
1933 Alternately, you can specify the size of one of the C compiler's
1934 built-in data types by following the type indicator character with
1935 one of the following characters.  For integers (@samp{d}, @samp{o},
1936 @samp{u}, @samp{x}):
1937
1938 @table @samp
1939 @item C
1940 char
1941 @item S
1942 short
1943 @item I
1944 int
1945 @item L
1946 long
1947 @end table
1948
1949 For floating point (@code{f}):
1950
1951 @table @asis
1952 @item F
1953 float
1954 @item D
1955 double
1956 @item L
1957 long double
1958 @end table
1959
1960 @item -v
1961 @itemx --output-duplicates
1962 @opindex -v
1963 @opindex --output-duplicates
1964 Output consecutive lines that are identical.  By default, when two or
1965 more consecutive output lines would be identical, @command{od} outputs only
1966 the first line, and puts just an asterisk on the following line to
1967 indicate the elision.
1968
1969 @item -w[@var{n}]
1970 @itemx --width[=@var{n}]
1971 @opindex -w
1972 @opindex --width
1973 Dump @code{n} input bytes per output line.  This must be a multiple of
1974 the least common multiple of the sizes associated with the specified
1975 output types.
1976
1977 If this option is not given at all, the default is 16.  If @var{n} is
1978 omitted, the default is 32.
1979
1980 @end table
1981
1982 The next several options are shorthands for format specifications.
1983 @sc{gnu} @command{od} accepts any combination of shorthands and format
1984 specification options.  These options accumulate.
1985
1986 @table @samp
1987
1988 @item -a
1989 @opindex -a
1990 Output as named characters.  Equivalent to @samp{-t a}.
1991
1992 @item -b
1993 @opindex -b
1994 Output as octal bytes.  Equivalent to @samp{-t o1}.
1995
1996 @item -c
1997 @opindex -c
1998 Output as @acronym{ASCII} characters or backslash escapes.  Equivalent to
1999 @samp{-t c}.
2000
2001 @item -d
2002 @opindex -d
2003 Output as unsigned decimal two-byte units.  Equivalent to @samp{-t u2}.
2004
2005 @item -f
2006 @opindex -f
2007 Output as floats.  Equivalent to @samp{-t fF}.
2008
2009 @item -i
2010 @opindex -i
2011 Output as decimal ints.  Equivalent to @samp{-t dI}.
2012
2013 @item -l
2014 @opindex -l
2015 Output as decimal long ints.  Equivalent to @samp{-t dL}.
2016
2017 @item -o
2018 @opindex -o
2019 Output as octal two-byte units.  Equivalent to @option{-t o2}.
2020
2021 @item -s
2022 @opindex -s
2023 Output as decimal two-byte units.  Equivalent to @option{-t d2}.
2024
2025 @item -x
2026 @opindex -x
2027 Output as hexadecimal two-byte units.  Equivalent to @samp{-t x2}.
2028
2029 @item --traditional
2030 @opindex --traditional
2031 Recognize the non-option label argument that traditional @command{od}
2032 accepted.  The following syntax:
2033
2034 @smallexample
2035 od --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
2036 @end smallexample
2037
2038 @noindent
2039 can be used to specify at most one file and optional arguments
2040 specifying an offset and a pseudo-start address, @var{label}.
2041 The @var{label} argument is interpreted
2042 just like @var{offset}, but it specifies an initial pseudo-address.  The
2043 pseudo-addresses are displayed in parentheses following any normal
2044 address.
2045
2046 @end table
2047
2048 @exitstatus
2049
2050 @node base64 invocation
2051 @section @command{base64}: Transform data into printable data
2052
2053 @pindex base64
2054 @cindex base64 encoding
2055
2056 @command{base64} transforms data read from a file, or standard input,
2057 into (or from) base64 encoded form.  The base64 encoded form uses
2058 printable @acronym{ASCII} characters to represent binary data.
2059 Synopses:
2060
2061 @smallexample
2062 base64 [@var{option}]@dots{} [@var{file}]
2063 base64 --decode [@var{option}]@dots{} [@var{file}]
2064 @end smallexample
2065
2066 The base64 encoding expands data to roughly 133% of the original.
2067 The format conforms to
2068 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc4648.txt, RFC 4648}.
2069
2070 The program accepts the following options.  Also see @ref{Common options}.
2071
2072 @table @samp
2073
2074 @item -w @var{cols}
2075 @itemx --wrap=@var{cols}
2076 @opindex -w
2077 @opindex --wrap
2078 @cindex wrap data
2079 @cindex column to wrap data after
2080 During encoding, wrap lines after @var{cols} characters.  This must be
2081 a positive number.
2082
2083 The default is to wrap after 76 characters.  Use the value 0 to
2084 disable line wrapping altogether.
2085
2086 @item -d
2087 @itemx --decode
2088 @opindex -d
2089 @opindex --decode
2090 @cindex Decode base64 data
2091 @cindex Base64 decoding
2092 Change the mode of operation, from the default of encoding data, to
2093 decoding data.  Input is expected to be base64 encoded data, and the
2094 output will be the original data.
2095
2096 @item -i
2097 @itemx --ignore-garbage
2098 @opindex -i
2099 @opindex --ignore-garbage
2100 @cindex Ignore garbage in base64 stream
2101 When decoding, newlines are always accepted.
2102 During decoding, ignore unrecognized bytes,
2103 to permit distorted data to be decoded.
2104
2105 @end table
2106
2107 @exitstatus
2108
2109
2110 @node Formatting file contents
2111 @chapter Formatting file contents
2112
2113 @cindex formatting file contents
2114
2115 These commands reformat the contents of files.
2116
2117 @menu
2118 * fmt invocation::              Reformat paragraph text.
2119 * pr invocation::               Paginate or columnate files for printing.
2120 * fold invocation::             Wrap input lines to fit in specified width.
2121 @end menu
2122
2123
2124 @node fmt invocation
2125 @section @command{fmt}: Reformat paragraph text
2126
2127 @pindex fmt
2128 @cindex reformatting paragraph text
2129 @cindex paragraphs, reformatting
2130 @cindex text, reformatting
2131
2132 @command{fmt} fills and joins lines to produce output lines of (at most)
2133 a given number of characters (75 by default).  Synopsis:
2134
2135 @example
2136 fmt [@var{option}]@dots{} [@var{file}]@dots{}
2137 @end example
2138
2139 @command{fmt} reads from the specified @var{file} arguments (or standard
2140 input if none are given), and writes to standard output.
2141
2142 By default, blank lines, spaces between words, and indentation are
2143 preserved in the output; successive input lines with different
2144 indentation are not joined; tabs are expanded on input and introduced on
2145 output.
2146
2147 @cindex line-breaking
2148 @cindex sentences and line-breaking
2149 @cindex Knuth, Donald E.
2150 @cindex Plass, Michael F.
2151 @command{fmt} prefers breaking lines at the end of a sentence, and tries to
2152 avoid line breaks after the first word of a sentence or before the last
2153 word of a sentence.  A @dfn{sentence break} is defined as either the end
2154 of a paragraph or a word ending in any of @samp{.?!}, followed by two
2155 spaces or end of line, ignoring any intervening parentheses or quotes.
2156 Like @TeX{}, @command{fmt} reads entire ``paragraphs'' before choosing line
2157 breaks; the algorithm is a variant of that given by Donald E. Knuth
2158 and Michael F. Plass in ``Breaking Paragraphs Into Lines'',
2159 @cite{Software---Practice & Experience} @b{11}, 11 (November 1981),
2160 1119--1184.
2161
2162 The program accepts the following options.  Also see @ref{Common options}.
2163
2164 @table @samp
2165
2166 @item -c
2167 @itemx --crown-margin
2168 @opindex -c
2169 @opindex --crown-margin
2170 @cindex crown margin
2171 @dfn{Crown margin} mode: preserve the indentation of the first two
2172 lines within a paragraph, and align the left margin of each subsequent
2173 line with that of the second line.
2174
2175 @item -t
2176 @itemx --tagged-paragraph
2177 @opindex -t
2178 @opindex --tagged-paragraph
2179 @cindex tagged paragraphs
2180 @dfn{Tagged paragraph} mode: like crown margin mode, except that if
2181 indentation of the first line of a paragraph is the same as the
2182 indentation of the second, the first line is treated as a one-line
2183 paragraph.
2184
2185 @item -s
2186 @itemx --split-only
2187 @opindex -s
2188 @opindex --split-only
2189 Split lines only.  Do not join short lines to form longer ones.  This
2190 prevents sample lines of code, and other such ``formatted'' text from
2191 being unduly combined.
2192
2193 @item -u
2194 @itemx --uniform-spacing
2195 @opindex -u
2196 @opindex --uniform-spacing
2197 Uniform spacing.  Reduce spacing between words to one space, and spacing
2198 between sentences to two spaces.
2199
2200 @item -@var{width}
2201 @itemx -w @var{width}
2202 @itemx --width=@var{width}
2203 @opindex -@var{width}
2204 @opindex -w
2205 @opindex --width
2206 Fill output lines up to @var{width} characters (default 75).  @command{fmt}
2207 initially tries to make lines about 7% shorter than this, to give it
2208 room to balance line lengths.
2209
2210 @item -p @var{prefix}
2211 @itemx --prefix=@var{prefix}
2212 Only lines beginning with @var{prefix} (possibly preceded by whitespace)
2213 are subject to formatting.  The prefix and any preceding whitespace are
2214 stripped for the formatting and then re-attached to each formatted output
2215 line.  One use is to format certain kinds of program comments, while
2216 leaving the code unchanged.
2217
2218 @end table
2219
2220 @exitstatus
2221
2222
2223 @node pr invocation
2224 @section @command{pr}: Paginate or columnate files for printing
2225
2226 @pindex pr
2227 @cindex printing, preparing files for
2228 @cindex multicolumn output, generating
2229 @cindex merging files in parallel
2230
2231 @command{pr} writes each @var{file} (@samp{-} means standard input), or
2232 standard input if none are given, to standard output, paginating and
2233 optionally outputting in multicolumn format; optionally merges all
2234 @var{file}s, printing all in parallel, one per column.  Synopsis:
2235
2236 @example
2237 pr [@var{option}]@dots{} [@var{file}]@dots{}
2238 @end example
2239
2240 @vindex LC_MESSAGES
2241 By default, a 5-line header is printed at each page: two blank lines;
2242 a line with the date, the file name, and the page count; and two more
2243 blank lines.  A footer of five blank lines is also printed.
2244 The default @var{page_length} is 66
2245 lines.  The default number of text lines is therefore 56.
2246 The text line of the header takes the form
2247 @samp{@var{date} @var{string} @var{page}}, with spaces inserted around
2248 @var{string} so that the line takes up the full @var{page_width}.  Here,
2249 @var{date} is the date (see the @option{-D} or @option{--date-format}
2250 option for details), @var{string} is the centered header string, and
2251 @var{page} identifies the page number.  The @env{LC_MESSAGES} locale
2252 category affects the spelling of @var{page}; in the default C locale, it
2253 is @samp{Page @var{number}} where @var{number} is the decimal page
2254 number.
2255
2256 Form feeds in the input cause page breaks in the output.  Multiple form
2257 feeds produce empty pages.
2258
2259 Columns are of equal width, separated by an optional string (default
2260 is @samp{space}).  For multicolumn output, lines will always be truncated to
2261 @var{page_width} (default 72), unless you use the @option{-J} option.
2262 For single
2263 column output no line truncation occurs by default.  Use @option{-W} option to
2264 truncate lines in that case.
2265
2266 The following changes were made in version 1.22i and apply to later
2267 versions of @command{pr}:
2268 @c FIXME: this whole section here sounds very awkward to me. I
2269 @c made a few small changes, but really it all needs to be redone. - Brian
2270 @c OK, I fixed another sentence or two, but some of it I just don't understand.
2271 @ - Brian
2272 @itemize @bullet
2273
2274 @item
2275 Some small @var{letter options} (@option{-s}, @option{-w}) have been
2276 redefined for better @acronym{POSIX} compliance.  The output of some further
2277 cases has been adapted to other Unix systems.  These changes are not
2278 compatible with earlier versions of the program.
2279
2280 @item
2281 Some @var{new capital letter} options (@option{-J}, @option{-S}, @option{-W})
2282 have been introduced to turn off unexpected interferences of small letter
2283 options.  The @option{-N} option and the second argument @var{last_page}
2284 of @samp{+FIRST_PAGE} offer more flexibility.  The detailed handling of
2285 form feeds set in the input files requires the @option{-T} option.
2286
2287 @item
2288 Capital letter options override small letter ones.
2289
2290 @item
2291 Some of the option-arguments (compare @option{-s}, @option{-e},
2292 @option{-i}, @option{-n}) cannot be specified as separate arguments from the
2293 preceding option letter (already stated in the @acronym{POSIX} specification).
2294 @end itemize
2295
2296 The program accepts the following options.  Also see @ref{Common options}.
2297
2298 @table @samp
2299
2300 @item +@var{first_page}[:@var{last_page}]
2301 @itemx --pages=@var{first_page}[:@var{last_page}]
2302 @c The two following @opindex lines evoke warnings because they contain `:'
2303 @c The `info' spec does not permit that.  If we use those lines, we end
2304 @c up with truncated index entries that don't work.
2305 @c @opindex +@var{first_page}[:@var{last_page}]
2306 @c @opindex --pages=@var{first_page}[:@var{last_page}]
2307 @opindex +@var{page_range}
2308 @opindex --pages=@var{page_range}
2309 Begin printing with page @var{first_page} and stop with @var{last_page}.
2310 Missing @samp{:@var{last_page}} implies end of file.  While estimating
2311 the number of skipped pages each form feed in the input file results
2312 in a new page.  Page counting with and without @samp{+@var{first_page}}
2313 is identical.  By default, counting starts with the first page of input
2314 file (not first page printed).  Line numbering may be altered by @option{-N}
2315 option.
2316
2317 @item -@var{column}
2318 @itemx --columns=@var{column}
2319 @opindex -@var{column}
2320 @opindex --columns
2321 @cindex down columns
2322 With each single @var{file}, produce @var{column} columns of output
2323 (default is 1) and print columns down, unless @option{-a} is used.  The
2324 column width is automatically decreased as @var{column} increases; unless
2325 you use the @option{-W/-w} option to increase @var{page_width} as well.
2326 This option might well cause some lines to be truncated.  The number of
2327 lines in the columns on each page are balanced.  The options @option{-e}
2328 and @option{-i} are on for multiple text-column output.  Together with
2329 @option{-J} option column alignment and line truncation is turned off.
2330 Lines of full length are joined in a free field format and @option{-S}
2331 option may set field separators.  @option{-@var{column}} may not be used
2332 with @option{-m} option.
2333
2334 @item -a
2335 @itemx --across
2336 @opindex -a
2337 @opindex --across
2338 @cindex across columns
2339 With each single @var{file}, print columns across rather than down.  The
2340 @option{-@var{column}} option must be given with @var{column} greater than one.
2341 If a line is too long to fit in a column, it is truncated.
2342
2343 @item -c
2344 @itemx --show-control-chars
2345 @opindex -c
2346 @opindex --show-control-chars
2347 Print control characters using hat notation (e.g., @samp{^G}); print
2348 other nonprinting characters in octal backslash notation.  By default,
2349 nonprinting characters are not changed.
2350
2351 @item -d
2352 @itemx --double-space
2353 @opindex -d
2354 @opindex --double-space
2355 @cindex double spacing
2356 Double space the output.
2357
2358 @item -D @var{format}
2359 @itemx --date-format=@var{format}
2360 @cindex time formats
2361 @cindex formatting times
2362 Format header dates using @var{format}, using the same conventions as
2363 for the command @samp{date +@var{format}}; @xref{date invocation}.
2364 Except for directives, which start with
2365 @samp{%}, characters in @var{format} are printed unchanged.  You can use
2366 this option to specify an arbitrary string in place of the header date,
2367 e.g., @option{--date-format="Monday morning"}.
2368
2369 @vindex POSIXLY_CORRECT
2370 @vindex LC_TIME
2371 The default date format is @samp{%Y-%m-%d %H:%M} (for example,
2372 @samp{2001-12-04 23:59});
2373 but if the @env{POSIXLY_CORRECT} environment variable is set
2374 and the @env{LC_TIME} locale category specifies the @acronym{POSIX}
2375 locale, the default is @samp{%b %e %H:%M %Y} (for example,
2376 @samp{Dec@ @ 4 23:59 2001}.
2377
2378 @vindex TZ
2379 Time stamps are listed according to the time zone rules specified by
2380 the @env{TZ} environment variable, or by the system default rules if
2381 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
2382 with @env{TZ}, libc, The GNU C Library Reference Manual}.
2383
2384 @item -e[@var{in-tabchar}[@var{in-tabwidth}]]
2385 @itemx --expand-tabs[=@var{in-tabchar}[@var{in-tabwidth}]]
2386 @opindex -e
2387 @opindex --expand-tabs
2388 @cindex input tabs
2389 Expand @var{tab}s to spaces on input.  Optional argument @var{in-tabchar} is
2390 the input tab character (default is the TAB character).  Second optional
2391 argument @var{in-tabwidth} is the input tab character's width (default
2392 is 8).
2393
2394 @item -f
2395 @itemx -F
2396 @itemx --form-feed
2397 @opindex -F
2398 @opindex -f
2399 @opindex --form-feed
2400 Use a form feed instead of newlines to separate output pages.  This does
2401 not alter the default page length of 66 lines.
2402
2403 @item -h @var{header}
2404 @itemx --header=@var{header}
2405 @opindex -h
2406 @opindex --header
2407 Replace the file name in the header with the centered string @var{header}.
2408 When using the shell, @var{header} should be quoted and should be
2409 separated from @option{-h} by a space.
2410
2411 @item -i[@var{out-tabchar}[@var{out-tabwidth}]]
2412 @itemx --output-tabs[=@var{out-tabchar}[@var{out-tabwidth}]]
2413 @opindex -i
2414 @opindex --output-tabs
2415 @cindex output tabs
2416 Replace spaces with @var{tab}s on output.  Optional argument @var{out-tabchar}
2417 is the output tab character (default is the TAB character).  Second optional
2418 argument @var{out-tabwidth} is the output tab character's width (default
2419 is 8).
2420
2421 @item -J
2422 @itemx --join-lines
2423 @opindex -J
2424 @opindex --join-lines
2425 Merge lines of full length.  Used together with the column options
2426 @option{-@var{column}}, @option{-a -@var{column}} or @option{-m}.  Turns off
2427 @option{-W/-w} line truncation;
2428 no column alignment used; may be used with
2429 @option{--sep-string[=@var{string}]}.  @option{-J} has been introduced
2430 (together with @option{-W} and @option{--sep-string})
2431 to disentangle the old (@acronym{POSIX}-compliant) options @option{-w} and
2432 @option{-s} along with the three column options.
2433
2434
2435 @item -l @var{page_length}
2436 @itemx --length=@var{page_length}
2437 @opindex -l
2438 @opindex --length
2439 Set the page length to @var{page_length} (default 66) lines, including
2440 the lines of the header [and the footer].  If @var{page_length} is less
2441 than or equal to 10, the header and footer are omitted, as if the
2442 @option{-t} option had been given.
2443
2444 @item -m
2445 @itemx --merge
2446 @opindex -m
2447 @opindex --merge
2448 Merge and print all @var{file}s in parallel, one in each column.  If a
2449 line is too long to fit in a column, it is truncated, unless the @option{-J}
2450 option is used.  @option{--sep-string[=@var{string}]} may be used.
2451 Empty pages in
2452 some @var{file}s (form feeds set) produce empty columns, still marked
2453 by @var{string}.  The result is a continuous line numbering and column
2454 marking throughout the whole merged file.  Completely empty merged pages
2455 show no separators or line numbers.  The default header becomes
2456 @samp{@var{date} @var{page}} with spaces inserted in the middle; this
2457 may be used with the @option{-h} or @option{--header} option to fill up
2458 the middle blank part.
2459
2460 @item -n[@var{number-separator}[@var{digits}]]
2461 @itemx --number-lines[=@var{number-separator}[@var{digits}]]
2462 @opindex -n
2463 @opindex --number-lines
2464 Provide @var{digits} digit line numbering (default for @var{digits} is
2465 5).  With multicolumn output the number occupies the first @var{digits}
2466 column positions of each text column or only each line of @option{-m}
2467 output.  With single column output the number precedes each line just as
2468 @option{-m} does.  Default counting of the line numbers starts with the
2469 first line of the input file (not the first line printed, compare the
2470 @option{--page} option and @option{-N} option).
2471 Optional argument @var{number-separator} is the character appended to
2472 the line number to separate it from the text followed.  The default
2473 separator is the TAB character.  In a strict sense a TAB is always
2474 printed with single column output only.  The TAB width varies
2475 with the TAB position, e.g., with the left @var{margin} specified
2476 by @option{-o} option.  With multicolumn output priority is given to
2477 @samp{equal width of output columns} (a @acronym{POSIX} specification).
2478 The TAB width is fixed to the value of the first column and does
2479 not change with different values of left @var{margin}.  That means a
2480 fixed number of spaces is always printed in the place of the
2481 @var{number-separator} TAB.  The tabification depends upon the output
2482 position.
2483
2484 @item -N @var{line_number}
2485 @itemx --first-line-number=@var{line_number}
2486 @opindex -N
2487 @opindex --first-line-number
2488 Start line counting with the number @var{line_number} at first line of
2489 first page printed (in most cases not the first line of the input file).
2490
2491 @item -o @var{margin}
2492 @itemx --indent=@var{margin}
2493 @opindex -o
2494 @opindex --indent
2495 @cindex indenting lines
2496 @cindex left margin
2497 Indent each line with a margin @var{margin} spaces wide (default is zero).
2498 The total page width is the size of the margin plus the @var{page_width}
2499 set with the @option{-W/-w} option.  A limited overflow may occur with
2500 numbered single column output (compare @option{-n} option).
2501
2502 @item -r
2503 @itemx --no-file-warnings
2504 @opindex -r
2505 @opindex --no-file-warnings
2506 Do not print a warning message when an argument @var{file} cannot be
2507 opened.  (The exit status will still be nonzero, however.)
2508
2509 @item -s[@var{char}]
2510 @itemx --separator[=@var{char}]
2511 @opindex -s
2512 @opindex --separator
2513 Separate columns by a single character @var{char}.  The default for
2514 @var{char} is the TAB character without @option{-w} and @samp{no
2515 character} with @option{-w}.  Without @option{-s} the default separator
2516 @samp{space} is set.  @option{-s[char]} turns off line truncation of all
2517 three column options (@option{-COLUMN}|@option{-a -COLUMN}|@option{-m}) unless
2518 @option{-w} is set.  This is a @acronym{POSIX}-compliant formulation.
2519
2520
2521 @item -S@var{string}
2522 @itemx --sep-string[=@var{string}]
2523 @opindex -S
2524 @opindex --sep-string
2525 Use @var{string} to separate output columns.  The @option{-S} option doesn't
2526 affect the @option{-W/-w} option, unlike the @option{-s} option which does.  It
2527 does not affect line truncation or column alignment.
2528 Without @option{-S}, and with @option{-J}, @command{pr} uses the default output
2529 separator, TAB@.
2530 Without @option{-S} or @option{-J}, @command{pr} uses a @samp{space}
2531 (same as @option{-S"@w{ }"}).  @option{--sep-string} with no
2532 @samp{=@var{string}} is equivalent to @option{--sep-string=""}.
2533
2534 @item -t
2535 @itemx --omit-header
2536 @opindex -t
2537 @opindex --omit-header
2538 Do not print the usual header [and footer] on each page, and do not fill
2539 out the bottom of pages (with blank lines or a form feed).  No page
2540 structure is produced, but form feeds set in the input files are retained.
2541 The predefined pagination is not changed.  @option{-t} or @option{-T} may be
2542 useful together with other options; e.g.: @option{-t -e4}, expand TAB characters
2543 in the input file to 4 spaces but don't make any other changes.  Use of
2544 @option{-t} overrides @option{-h}.
2545
2546 @item -T
2547 @itemx --omit-pagination
2548 @opindex -T
2549 @opindex --omit-pagination
2550 Do not print header [and footer].  In addition eliminate all form feeds
2551 set in the input files.
2552
2553 @item -v
2554 @itemx --show-nonprinting
2555 @opindex -v
2556 @opindex --show-nonprinting
2557 Print nonprinting characters in octal backslash notation.
2558
2559 @item -w @var{page_width}
2560 @itemx --width=@var{page_width}
2561 @opindex -w
2562 @opindex --width
2563 Set page width to @var{page_width} characters for multiple text-column
2564 output only (default for @var{page_width} is 72).  @option{-s[CHAR]} turns
2565 off the default page width and any line truncation and column alignment.
2566 Lines of full length are merged, regardless of the column options
2567 set.  No @var{page_width} setting is possible with single column output.
2568 A @acronym{POSIX}-compliant formulation.
2569
2570 @item -W @var{page_width}
2571 @itemx --page_width=@var{page_width}
2572 @opindex -W
2573 @opindex --page_width
2574 Set the page width to @var{page_width} characters.  That's valid with and
2575 without a column option.  Text lines are truncated, unless @option{-J}
2576 is used.  Together with one of the three column options
2577 (@option{-@var{column}}, @option{-a -@var{column}} or @option{-m}) column
2578 alignment is always used.  The separator options @option{-S} or @option{-s}
2579 don't affect the @option{-W} option.  Default is 72 characters.  Without
2580 @option{-W @var{page_width}} and without any of the column options NO line
2581 truncation is used (defined to keep downward compatibility and to meet
2582 most frequent tasks).  That's equivalent to @option{-W 72 -J}.  The header
2583 line is never truncated.
2584
2585 @end table
2586
2587 @exitstatus
2588
2589
2590 @node fold invocation
2591 @section @command{fold}: Wrap input lines to fit in specified width
2592
2593 @pindex fold
2594 @cindex wrapping long input lines
2595 @cindex folding long input lines
2596
2597 @command{fold} writes each @var{file} (@option{-} means standard input), or
2598 standard input if none are given, to standard output, breaking long
2599 lines.  Synopsis:
2600
2601 @example
2602 fold [@var{option}]@dots{} [@var{file}]@dots{}
2603 @end example
2604
2605 By default, @command{fold} breaks lines wider than 80 columns.  The output
2606 is split into as many lines as necessary.
2607
2608 @cindex screen columns
2609 @command{fold} counts screen columns by default; thus, a tab may count more
2610 than one column, backspace decreases the column count, and carriage
2611 return sets the column to zero.
2612
2613 The program accepts the following options.  Also see @ref{Common options}.
2614
2615 @table @samp
2616
2617 @item -b
2618 @itemx --bytes
2619 @opindex -b
2620 @opindex --bytes
2621 Count bytes rather than columns, so that tabs, backspaces, and carriage
2622 returns are each counted as taking up one column, just like other
2623 characters.
2624
2625 @item -s
2626 @itemx --spaces
2627 @opindex -s
2628 @opindex --spaces
2629 Break at word boundaries: the line is broken after the last blank before
2630 the maximum line length.  If the line contains no such blanks, the line
2631 is broken at the maximum line length as usual.
2632
2633 @item -w @var{width}
2634 @itemx --width=@var{width}
2635 @opindex -w
2636 @opindex --width
2637 Use a maximum line length of @var{width} columns instead of 80.
2638
2639 For compatibility @command{fold} supports an obsolete option syntax
2640 @option{-@var{width}}.  New scripts should use @option{-w @var{width}}
2641 instead.
2642
2643 @end table
2644
2645 @exitstatus
2646
2647
2648 @node Output of parts of files
2649 @chapter Output of parts of files
2650
2651 @cindex output of parts of files
2652 @cindex parts of files, output of
2653
2654 These commands output pieces of the input.
2655
2656 @menu
2657 * head invocation::             Output the first part of files.
2658 * tail invocation::             Output the last part of files.
2659 * split invocation::            Split a file into pieces.
2660 * csplit invocation::           Split a file into context-determined pieces.
2661 @end menu
2662
2663 @node head invocation
2664 @section @command{head}: Output the first part of files
2665
2666 @pindex head
2667 @cindex initial part of files, outputting
2668 @cindex first part of files, outputting
2669
2670 @command{head} prints the first part (10 lines by default) of each
2671 @var{file}; it reads from standard input if no files are given or
2672 when given a @var{file} of @option{-}.  Synopsis:
2673
2674 @example
2675 head [@var{option}]@dots{} [@var{file}]@dots{}
2676 @end example
2677
2678 If more than one @var{file} is specified, @command{head} prints a
2679 one-line header consisting of:
2680
2681 @example
2682 ==> @var{file name} <==
2683 @end example
2684
2685 @noindent
2686 before the output for each @var{file}.
2687
2688 The program accepts the following options.  Also see @ref{Common options}.
2689
2690 @table @samp
2691
2692 @item -c @var{k}
2693 @itemx --bytes=@var{k}
2694 @opindex -c
2695 @opindex --bytes
2696 Print the first @var{k} bytes, instead of initial lines.
2697 However, if @var{k} starts with a @samp{-},
2698 print all but the last @var{k} bytes of each file.
2699 @multiplierSuffixes{k}
2700
2701 @itemx -n @var{k}
2702 @itemx --lines=@var{k}
2703 @opindex -n
2704 @opindex --lines
2705 Output the first @var{k} lines.
2706 However, if @var{k} starts with a @samp{-},
2707 print all but the last @var{k} lines of each file.
2708 Size multiplier suffixes are the same as with the @option{-c} option.
2709
2710 @item -q
2711 @itemx --quiet
2712 @itemx --silent
2713 @opindex -q
2714 @opindex --quiet
2715 @opindex --silent
2716 Never print file name headers.
2717
2718 @item -v
2719 @itemx --verbose
2720 @opindex -v
2721 @opindex --verbose
2722 Always print file name headers.
2723
2724 @end table
2725
2726 For compatibility @command{head} also supports an obsolete option syntax
2727 @option{-@var{count}@var{options}}, which is recognized only if it is
2728 specified first.  @var{count} is a decimal number optionally followed
2729 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
2730 @samp{l} to mean count by lines, or other option letters (@samp{cqv}).
2731 Scripts intended for standard hosts should use @option{-c @var{count}}
2732 or @option{-n @var{count}} instead.  If your script must also run on
2733 hosts that support only the obsolete syntax, it is usually simpler to
2734 avoid @command{head}, e.g., by using @samp{sed 5q} instead of
2735 @samp{head -5}.
2736
2737 @exitstatus
2738
2739
2740 @node tail invocation
2741 @section @command{tail}: Output the last part of files
2742
2743 @pindex tail
2744 @cindex last part of files, outputting
2745
2746 @command{tail} prints the last part (10 lines by default) of each
2747 @var{file}; it reads from standard input if no files are given or
2748 when given a @var{file} of @samp{-}.  Synopsis:
2749
2750 @example
2751 tail [@var{option}]@dots{} [@var{file}]@dots{}
2752 @end example
2753
2754 If more than one @var{file} is specified, @command{tail} prints a
2755 one-line header consisting of:
2756
2757 @example
2758 ==> @var{file name} <==
2759 @end example
2760
2761 @noindent
2762 before the output for each @var{file}.
2763
2764 @cindex BSD @command{tail}
2765 @sc{gnu} @command{tail} can output any amount of data (some other versions of
2766 @command{tail} cannot).  It also has no @option{-r} option (print in
2767 reverse), since reversing a file is really a different job from printing
2768 the end of a file; BSD @command{tail} (which is the one with @option{-r}) can
2769 only reverse files that are at most as large as its buffer, which is
2770 typically 32 KiB@.  A more reliable and versatile way to reverse files is
2771 the @sc{gnu} @command{tac} command.
2772
2773 The program accepts the following options.  Also see @ref{Common options}.
2774
2775 @table @samp
2776
2777 @item -c @var{k}
2778 @itemx --bytes=@var{k}
2779 @opindex -c
2780 @opindex --bytes
2781 Output the last @var{k} bytes, instead of final lines.
2782 However, if @var{k} starts with a @samp{+}, start printing with the
2783 @var{k}th byte from the start of each file, instead of from the end.
2784 @multiplierSuffixes{k}
2785
2786 @item -f
2787 @itemx --follow[=@var{how}]
2788 @opindex -f
2789 @opindex --follow
2790 @cindex growing files
2791 @vindex name @r{follow option}
2792 @vindex descriptor @r{follow option}
2793 Loop forever trying to read more characters at the end of the file,
2794 presumably because the file is growing.
2795 If more than one file is given, @command{tail} prints a header whenever it
2796 gets output from a different file, to indicate which file that output is
2797 from.
2798
2799 There are two ways to specify how you'd like to track files with this option,
2800 but that difference is noticeable only when a followed file is removed or
2801 renamed.
2802 If you'd like to continue to track the end of a growing file even after
2803 it has been unlinked, use @option{--follow=descriptor}.  This is the default
2804 behavior, but it is not useful if you're tracking a log file that may be
2805 rotated (removed or renamed, then reopened).  In that case, use
2806 @option{--follow=name} to track the named file, perhaps by reopening it
2807 periodically to see if it has been removed and recreated by some other program.
2808 Note that the inotify-based implementation handles this case without
2809 the need for any periodic reopening.
2810
2811 No matter which method you use, if the tracked file is determined to have
2812 shrunk, @command{tail} prints a message saying the file has been truncated
2813 and resumes tracking the end of the file from the newly-determined endpoint.
2814
2815 When a file is removed, @command{tail}'s behavior depends on whether it is
2816 following the name or the descriptor.  When following by name, tail can
2817 detect that a file has been removed and gives a message to that effect,
2818 and if @option{--retry} has been specified it will continue checking
2819 periodically to see if the file reappears.
2820 When following a descriptor, tail does not detect that the file has
2821 been unlinked or renamed and issues no message;  even though the file
2822 may no longer be accessible via its original name, it may still be
2823 growing.
2824
2825 The option values @samp{descriptor} and @samp{name} may be specified only
2826 with the long form of the option, not with @option{-f}.
2827
2828 The @option{-f} option is ignored if
2829 no @var{file} operand is specified and standard input is a FIFO or a pipe.
2830 Likewise, the @option{-f} option has no effect for any
2831 operand specified as @samp{-}, when standard input is a FIFO or a pipe.
2832
2833 With kernel inotify support, output is asynchronous and generally very prompt.
2834 Otherwise, @command{tail} sleeps for one second between checks---
2835 use @option{--sleep-interval=@var{n}} to change that default---which can
2836 make the output appear slightly less responsive or bursty.
2837
2838 @item -F
2839 @opindex -F
2840 This option is the same as @option{--follow=name --retry}.  That is, tail
2841 will attempt to reopen a file when it is removed.  Should this fail, tail
2842 will keep trying until it becomes accessible again.
2843
2844 @itemx --retry
2845 @opindex --retry
2846 This option is useful mainly when following by name (i.e., with
2847 @option{--follow=name}).
2848 Without this option, when tail encounters a file that doesn't
2849 exist or is otherwise inaccessible, it reports that fact and
2850 never checks it again.
2851
2852 @itemx --sleep-interval=@var{number}
2853 @opindex --sleep-interval
2854 Change the number of seconds to wait between iterations (the default is 1.0).
2855 During one iteration, every specified file is checked to see if it has
2856 changed size.
2857 Historical implementations of @command{tail} have required that
2858 @var{number} be an integer.  However, GNU @command{tail} accepts
2859 an arbitrary floating point number.  @xref{Floating point}.
2860 When @command{tail} uses inotify, this polling-related option
2861 is usually ignored.  However, if you also specify @option{--pid=@var{p}},
2862 @command{tail} checks whether process @var{p} is alive at least
2863 every @var{number} seconds.
2864
2865 @itemx --pid=@var{pid}
2866 @opindex --pid
2867 When following by name or by descriptor, you may specify the process ID,
2868 @var{pid}, of the sole writer of all @var{file} arguments.  Then, shortly
2869 after that process terminates, tail will also terminate.  This will
2870 work properly only if the writer and the tailing process are running on
2871 the same machine.  For example, to save the output of a build in a file
2872 and to watch the file grow, if you invoke @command{make} and @command{tail}
2873 like this then the tail process will stop when your build completes.
2874 Without this option, you would have had to kill the @code{tail -f}
2875 process yourself.
2876
2877 @example
2878 $ make >& makerr & tail --pid=$! -f makerr
2879 @end example
2880
2881 If you specify a @var{pid} that is not in use or that does not correspond
2882 to the process that is writing to the tailed files, then @command{tail}
2883 may terminate long before any @var{file}s stop growing or it may not
2884 terminate until long after the real writer has terminated.
2885 Note that @option{--pid} cannot be supported on some systems; @command{tail}
2886 will print a warning if this is the case.
2887
2888 @itemx --max-unchanged-stats=@var{n}
2889 @opindex --max-unchanged-stats
2890 When tailing a file by name, if there have been @var{n} (default
2891 n=@value{DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS}) consecutive
2892 iterations for which the file has not changed, then
2893 @code{open}/@code{fstat} the file to determine if that file name is
2894 still associated with the same device/inode-number pair as before.
2895 When following a log file that is rotated, this is approximately the
2896 number of seconds between when tail prints the last pre-rotation lines
2897 and when it prints the lines that have accumulated in the new log file.
2898 This option is meaningful only when polling (i.e., without inotify)
2899 and when following by name.
2900
2901 @itemx -n @var{k}
2902 @itemx --lines=@var{k}
2903 @opindex -n
2904 @opindex --lines
2905 Output the last @var{k} lines.
2906 However, if @var{k} starts with a @samp{+}, start printing with the
2907 @var{k}th line from the start of each file, instead of from the end.
2908 Size multiplier suffixes are the same as with the @option{-c} option.
2909
2910 @item -q
2911 @itemx --quiet
2912 @itemx --silent
2913 @opindex -q
2914 @opindex --quiet
2915 @opindex --silent
2916 Never print file name headers.
2917
2918 @item -v
2919 @itemx --verbose
2920 @opindex -v
2921 @opindex --verbose
2922 Always print file name headers.
2923
2924 @end table
2925
2926 For compatibility @command{tail} also supports an obsolete usage
2927 @samp{tail -[@var{count}][bcl][f] [@var{file}]}, which is recognized
2928 only if it does not conflict with the usage described
2929 above.  This obsolete form uses exactly one option and at most one
2930 file.  In the option, @var{count} is an optional decimal number optionally
2931 followed by a size letter (@samp{b}, @samp{c}, @samp{l}) to mean count
2932 by 512-byte blocks, bytes, or lines, optionally followed by @samp{f}
2933 which has the same meaning as @option{-f}.
2934
2935 @vindex _POSIX2_VERSION
2936 On older systems, the leading @samp{-} can be replaced by @samp{+} in
2937 the obsolete option syntax with the same meaning as in counts, and
2938 obsolete usage overrides normal usage when the two conflict.
2939 This obsolete behavior can be enabled or disabled with the
2940 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
2941 conformance}).
2942
2943 Scripts intended for use on standard hosts should avoid obsolete
2944 syntax and should use @option{-c @var{count}[b]}, @option{-n
2945 @var{count}}, and/or @option{-f} instead.  If your script must also
2946 run on hosts that support only the obsolete syntax, you can often
2947 rewrite it to avoid problematic usages, e.g., by using @samp{sed -n
2948 '$p'} rather than @samp{tail -1}.  If that's not possible, the script
2949 can use a test like @samp{if tail -c +1 </dev/null >/dev/null 2>&1;
2950 then @dots{}} to decide which syntax to use.
2951
2952 Even if your script assumes the standard behavior, you should still
2953 beware usages whose behaviors differ depending on the @acronym{POSIX}
2954 version.  For example, avoid @samp{tail - main.c}, since it might be
2955 interpreted as either @samp{tail main.c} or as @samp{tail -- -
2956 main.c}; avoid @samp{tail -c 4}, since it might mean either @samp{tail
2957 -c4} or @samp{tail -c 10 4}; and avoid @samp{tail +4}, since it might
2958 mean either @samp{tail ./+4} or @samp{tail -n +4}.
2959
2960 @exitstatus
2961
2962
2963 @node split invocation
2964 @section @command{split}: Split a file into pieces.
2965
2966 @pindex split
2967 @cindex splitting a file into pieces
2968 @cindex pieces, splitting a file into
2969
2970 @command{split} creates output files containing consecutive or interleaved
2971 sections of @var{input}  (standard input if none is given or @var{input}
2972 is @samp{-}).  Synopsis:
2973
2974 @example
2975 split [@var{option}] [@var{input} [@var{prefix}]]
2976 @end example
2977
2978 By default, @command{split} puts 1000 lines of @var{input} (or whatever is
2979 left over for the last section), into each output file.
2980
2981 @cindex output file name prefix
2982 The output files' names consist of @var{prefix} (@samp{x} by default)
2983 followed by a group of characters (@samp{aa}, @samp{ab}, @dots{} by
2984 default), such that concatenating the output files in traditional
2985 sorted order by file name produces the original input file (except
2986 @option{-r}).  If the output file names are exhausted, @command{split}
2987 reports an error without deleting the output files that it did create.
2988
2989 The program accepts the following options.  Also see @ref{Common options}.
2990
2991 @table @samp
2992
2993 @item -l @var{lines}
2994 @itemx --lines=@var{lines}
2995 @opindex -l
2996 @opindex --lines
2997 Put @var{lines} lines of @var{input} into each output file.
2998
2999 For compatibility @command{split} also supports an obsolete
3000 option syntax @option{-@var{lines}}.  New scripts should use
3001 @option{-l @var{lines}} instead.
3002
3003 @item -b @var{size}
3004 @itemx --bytes=@var{size}
3005 @opindex -b
3006 @opindex --bytes
3007 Put @var{size} bytes of @var{input} into each output file.
3008 @multiplierSuffixes{size}
3009
3010 @item -C @var{size}
3011 @itemx --line-bytes=@var{size}
3012 @opindex -C
3013 @opindex --line-bytes
3014 Put into each output file as many complete lines of @var{input} as
3015 possible without exceeding @var{size} bytes.  Individual lines longer than
3016 @var{size} bytes are broken into multiple files.
3017 @var{size} has the same format as for the @option{--bytes} option.
3018
3019 @itemx --filter=@var{command}
3020 @opindex --filter
3021 With this option, rather than simply writing to each output file,
3022 write through a pipe to the specified shell @var{command} for each output file.
3023 @var{command} should use the $FILE environment variable, which is set
3024 to a different output file name for each invocation of the command.
3025 For example, imagine that you have a 1TiB compressed file
3026 that, if uncompressed, would be too large to reside on disk,
3027 yet you must split it into individually-compressed pieces
3028 of a more manageable size.
3029 To do that, you might run this command:
3030
3031 @example
3032 xz -dc BIG.xz | split -b200G --filter='xz > $FILE.xz' - big-
3033 @end example
3034
3035 Assuming a 10:1 compression ratio, that would create about fifty 20GiB files
3036 with names @file{big-xaa.xz}, @file{big-xab.xz}, @file{big-xac.xz}, etc.
3037
3038 @item -n @var{chunks}
3039 @itemx --number=@var{chunks}
3040 @opindex -n
3041 @opindex --number
3042
3043 Split @var{input} to @var{chunks} output files where @var{chunks} may be:
3044
3045 @example
3046 @var{n}      generate @var{n} files based on current size of @var{input}
3047 @var{k}/@var{n}    only output @var{k}th of @var{n} to stdout
3048 l/@var{n}    generate @var{n} files without splitting lines
3049 l/@var{k}/@var{n}  likewise but only output @var{k}th of @var{n} to stdout
3050 r/@var{n}    like @samp{l} but use round robin distribution
3051 r/@var{k}/@var{n}  likewise but only output @var{k}th of @var{n} to stdout
3052 @end example
3053
3054 Any excess bytes remaining after dividing the @var{input}
3055 into @var{n} chunks, are assigned to the last chunk.
3056 Any excess bytes appearing after the initial calculation are discarded
3057 (except when using @samp{r} mode).
3058
3059 All @var{n} files are created even if there are fewer than @var{n} lines,
3060 or the @var{input} is truncated.
3061
3062 For @samp{l} mode, chunks are approximately @var{input} size / @var{n}.
3063 The @var{input} is partitioned into @var{n} equal sized portions, with
3064 the last assigned any excess.  If a line @emph{starts} within a partition
3065 it is written completely to the corresponding file.  Since lines
3066 are not split even if they overlap a partition, the files written
3067 can be larger or smaller than the partition size, and even empty
3068 if a line is so long as to completely overlap the partition.
3069
3070 For @samp{r} mode, the size of @var{input} is irrelevant,
3071 and so can be a pipe for example.
3072
3073 @item -a @var{length}
3074 @itemx --suffix-length=@var{length}
3075 @opindex -a
3076 @opindex --suffix-length
3077 Use suffixes of length @var{length}.  The default @var{length} is 2.
3078
3079 @item -d
3080 @itemx --numeric-suffixes
3081 @opindex -d
3082 @opindex --numeric-suffixes
3083 Use digits in suffixes rather than lower-case letters.
3084
3085 @item -e
3086 @itemx --elide-empty-files
3087 @opindex -e
3088 @opindex --elide-empty-files
3089 Suppress the generation of zero-length output files.  This can happen
3090 with the @option{--number} option if a file is (truncated to be) shorter
3091 than the number requested, or if a line is so long as to completely
3092 span a chunk.  The output file sequence numbers, always run consecutively
3093 even when this option is specified.
3094
3095 @item -u
3096 @itemx --unbuffered
3097 @opindex -u
3098 @opindex --unbuffered
3099 Immediately copy input to output in @option{--number r/...} mode,
3100 which is a much slower mode of operation.
3101
3102 @itemx --verbose
3103 @opindex --verbose
3104 Write a diagnostic just before each output file is opened.
3105
3106 @end table
3107
3108 @exitstatus
3109
3110 Here are a few examples to illustrate how the
3111 @option{--number} (@option{-n}) option works:
3112
3113 Notice how, by default, one line may be split onto two or more:
3114
3115 @example
3116 $ seq -w 6 10 > k; split -n3 k; head xa?
3117 ==> xaa <==
3118 06
3119 07
3120 ==> xab <==
3121
3122 08
3123 0
3124 ==> xac <==
3125 9
3126 10
3127 @end example
3128
3129 Use the "l/" modifier to suppress that:
3130
3131 @example
3132 $ seq -w 6 10 > k; split -nl/3 k; head xa?
3133 ==> xaa <==
3134 06
3135 07
3136
3137 ==> xab <==
3138 08
3139 09
3140
3141 ==> xac <==
3142 10
3143 @end example
3144
3145 Use the "r/" modifier to distribute lines in a round-robin fashion:
3146
3147 @example
3148 $ seq -w 6 10 > k; split -nr/3 k; head xa?
3149 ==> xaa <==
3150 06
3151 09
3152
3153 ==> xab <==
3154 07
3155 10
3156
3157 ==> xac <==
3158 08
3159 @end example
3160
3161 You can also extract just the Kth chunk.
3162 This extracts and prints just the 7th "chunk" of 33:
3163
3164 @example
3165 $ seq 100 > k; split -nl/7/33 k
3166 20
3167 21
3168 22
3169 @end example
3170
3171
3172 @node csplit invocation
3173 @section @command{csplit}: Split a file into context-determined pieces
3174
3175 @pindex csplit
3176 @cindex context splitting
3177 @cindex splitting a file into pieces by context
3178
3179 @command{csplit} creates zero or more output files containing sections of
3180 @var{input} (standard input if @var{input} is @samp{-}).  Synopsis:
3181
3182 @example
3183 csplit [@var{option}]@dots{} @var{input} @var{pattern}@dots{}
3184 @end example
3185
3186 The contents of the output files are determined by the @var{pattern}
3187 arguments, as detailed below.  An error occurs if a @var{pattern}
3188 argument refers to a nonexistent line of the input file (e.g., if no
3189 remaining line matches a given regular expression).  After every
3190 @var{pattern} has been matched, any remaining input is copied into one
3191 last output file.
3192
3193 By default, @command{csplit} prints the number of bytes written to each
3194 output file after it has been created.
3195
3196 The types of pattern arguments are:
3197
3198 @table @samp
3199
3200 @item @var{n}
3201 Create an output file containing the input up to but not including line
3202 @var{n} (a positive integer).  If followed by a repeat count, also
3203 create an output file containing the next @var{n} lines of the input
3204 file once for each repeat.
3205
3206 @item /@var{regexp}/[@var{offset}]
3207 Create an output file containing the current line up to (but not
3208 including) the next line of the input file that contains a match for
3209 @var{regexp}.  The optional @var{offset} is an integer.
3210 If it is given, the input up to (but not including) the
3211 matching line plus or minus @var{offset} is put into the output file,
3212 and the line after that begins the next section of input.
3213
3214 @item %@var{regexp}%[@var{offset}]
3215 Like the previous type, except that it does not create an output
3216 file, so that section of the input file is effectively ignored.
3217
3218 @item @{@var{repeat-count}@}
3219 Repeat the previous pattern @var{repeat-count} additional
3220 times.  The @var{repeat-count} can either be a positive integer or an
3221 asterisk, meaning repeat as many times as necessary until the input is
3222 exhausted.
3223
3224 @end table
3225
3226 The output files' names consist of a prefix (@samp{xx} by default)
3227 followed by a suffix.  By default, the suffix is an ascending sequence
3228 of two-digit decimal numbers from @samp{00} to @samp{99}.  In any case,
3229 concatenating the output files in sorted order by file name produces the
3230 original input file.
3231
3232 By default, if @command{csplit} encounters an error or receives a hangup,
3233 interrupt, quit, or terminate signal, it removes any output files
3234 that it has created so far before it exits.
3235
3236 The program accepts the following options.  Also see @ref{Common options}.
3237
3238 @table @samp
3239
3240 @item -f @var{prefix}
3241 @itemx --prefix=@var{prefix}
3242 @opindex -f
3243 @opindex --prefix
3244 @cindex output file name prefix
3245 Use @var{prefix} as the output file name prefix.
3246
3247 @item -b @var{suffix}
3248 @itemx --suffix=@var{suffix}
3249 @opindex -b
3250 @opindex --suffix
3251 @cindex output file name suffix
3252 Use @var{suffix} as the output file name suffix.  When this option is
3253 specified, the suffix string must include exactly one
3254 @code{printf(3)}-style conversion specification, possibly including
3255 format specification flags, a field width, a precision specifications,
3256 or all of these kinds of modifiers.  The format letter must convert a
3257 binary unsigned integer argument to readable form.  The format letters
3258 @samp{d} and @samp{i} are aliases for @samp{u}, and the
3259 @samp{u}, @samp{o}, @samp{x}, and @samp{X} conversions are allowed.  The
3260 entire @var{suffix} is given (with the current output file number) to
3261 @code{sprintf(3)} to form the file name suffixes for each of the
3262 individual output files in turn.  If this option is used, the
3263 @option{--digits} option is ignored.
3264
3265 @item -n @var{digits}
3266 @itemx --digits=@var{digits}
3267 @opindex -n
3268 @opindex --digits
3269 Use output file names containing numbers that are @var{digits} digits
3270 long instead of the default 2.
3271
3272 @item -k
3273 @itemx --keep-files
3274 @opindex -k
3275 @opindex --keep-files
3276 Do not remove output files when errors are encountered.
3277
3278 @item -z
3279 @itemx --elide-empty-files
3280 @opindex -z
3281 @opindex --elide-empty-files
3282 Suppress the generation of zero-length output files.  (In cases where
3283 the section delimiters of the input file are supposed to mark the first
3284 lines of each of the sections, the first output file will generally be a
3285 zero-length file unless you use this option.)  The output file sequence
3286 numbers always run consecutively starting from 0, even when this option
3287 is specified.
3288
3289 @item -s
3290 @itemx -q
3291 @itemx --silent
3292 @itemx --quiet
3293 @opindex -s
3294 @opindex -q
3295 @opindex --silent
3296 @opindex --quiet
3297 Do not print counts of output file sizes.
3298
3299 @end table
3300
3301 @exitstatus
3302
3303 Here is an example of its usage.
3304 First, create an empty directory for the exercise,
3305 and cd into it:
3306
3307 @example
3308 $ mkdir d && cd d
3309 @end example
3310
3311 Now, split the sequence of 1..14 on lines that end with 0 or 5:
3312
3313 @example
3314 $ seq 14 | csplit - '/[05]$/' '@{*@}'
3315 8
3316 10
3317 15
3318 @end example
3319
3320 Each number printed above is the size of an output
3321 file that csplit has just created.
3322 List the names of those output files:
3323
3324 @example
3325 $ ls
3326 xx00  xx01  xx02
3327 @end example
3328
3329 Use @command{head} to show their contents:
3330
3331 @example
3332 $ head xx*
3333 ==> xx00 <==
3334 1
3335 2
3336 3
3337 4
3338
3339 ==> xx01 <==
3340 5
3341 6
3342 7
3343 8
3344 9
3345
3346 ==> xx02 <==
3347 10
3348 11
3349 12
3350 13
3351 14
3352 @end example
3353
3354 @node Summarizing files
3355 @chapter Summarizing files
3356
3357 @cindex summarizing files
3358
3359 These commands generate just a few numbers representing entire
3360 contents of files.
3361
3362 @menu
3363 * wc invocation::               Print newline, word, and byte counts.
3364 * sum invocation::              Print checksum and block counts.
3365 * cksum invocation::            Print CRC checksum and byte counts.
3366 * md5sum invocation::           Print or check MD5 digests.
3367 * sha1sum invocation::          Print or check SHA-1 digests.
3368 * sha2 utilities::              Print or check SHA-2 digests.
3369 @end menu
3370
3371
3372 @node wc invocation
3373 @section @command{wc}: Print newline, word, and byte counts
3374
3375 @pindex wc
3376 @cindex byte count
3377 @cindex character count
3378 @cindex word count
3379 @cindex line count
3380
3381 @command{wc} counts the number of bytes, characters, whitespace-separated
3382 words, and newlines in each given @var{file}, or standard input if none
3383 are given or for a @var{file} of @samp{-}.  Synopsis:
3384
3385 @example
3386 wc [@var{option}]@dots{} [@var{file}]@dots{}
3387 @end example
3388
3389 @cindex total counts
3390 @command{wc} prints one line of counts for each file, and if the file was
3391 given as an argument, it prints the file name following the counts.  If
3392 more than one @var{file} is given, @command{wc} prints a final line
3393 containing the cumulative counts, with the file name @file{total}.  The
3394 counts are printed in this order: newlines, words, characters, bytes,
3395 maximum line length.
3396 Each count is printed right-justified in a field with at least one
3397 space between fields so that the numbers and file names normally line
3398 up nicely in columns.  The width of the count fields varies depending
3399 on the inputs, so you should not depend on a particular field width.
3400 However, as a @acronym{GNU} extension, if only one count is printed,
3401 it is guaranteed to be printed without leading spaces.
3402
3403 By default, @command{wc} prints three counts: the newline, words, and byte
3404 counts.  Options can specify that only certain counts be printed.
3405 Options do not undo others previously given, so
3406
3407 @example
3408 wc --bytes --words
3409 @end example
3410
3411 @noindent
3412 prints both the byte counts and the word counts.
3413
3414 With the @option{--max-line-length} option, @command{wc} prints the length
3415 of the longest line per file, and if there is more than one file it
3416 prints the maximum (not the sum) of those lengths.  The line lengths here
3417 are measured in screen columns, according to the current locale and
3418 assuming tab positions in every 8th column.
3419
3420 The program accepts the following options.  Also see @ref{Common options}.
3421
3422 @table @samp
3423
3424 @item -c
3425 @itemx --bytes
3426 @opindex -c
3427 @opindex --bytes
3428 Print only the byte counts.
3429
3430 @item -m
3431 @itemx --chars
3432 @opindex -m
3433 @opindex --chars
3434 Print only the character counts.
3435
3436 @item -w
3437 @itemx --words
3438 @opindex -w
3439 @opindex --words
3440 Print only the word counts.
3441
3442 @item -l
3443 @itemx --lines
3444 @opindex -l
3445 @opindex --lines
3446 Print only the newline counts.
3447
3448 @item -L
3449 @itemx --max-line-length
3450 @opindex -L
3451 @opindex --max-line-length
3452 Print only the maximum line lengths.
3453
3454 @macro filesZeroFromOption{cmd,withTotalOption,subListOutput}
3455 @itemx --files0-from=@var{file}
3456 @opindex --files0-from=@var{file}
3457 @c This is commented out to avoid a texi2dvi failure.
3458 @c texi2dvi (GNU Texinfo 4.11) 1.104
3459 @c @cindex including files from @command{\cmd\}
3460 Disallow processing files named on the command line, and instead process
3461 those named in file @var{file}; each name being terminated by a zero byte
3462 (@acronym{ASCII} @sc{nul}).
3463 This is useful \withTotalOption\
3464 when the list of file names is so long that it may exceed a command line
3465 length limitation.
3466 In such cases, running @command{\cmd\} via @command{xargs} is undesirable
3467 because it splits the list into pieces and makes @command{\cmd\} print
3468 \subListOutput\ for each sublist rather than for the entire list.
3469 One way to produce a list of @acronym{ASCII} @sc{nul} terminated file
3470 names is with @sc{gnu}
3471 @command{find}, using its @option{-print0} predicate.
3472 If @var{file} is @samp{-} then the @acronym{ASCII} @sc{nul} terminated
3473 file names are read from standard input.
3474 @end macro
3475 @filesZeroFromOption{wc,,a total}
3476
3477 For example, to find the length of the longest line in any @file{.c} or
3478 @file{.h} file in the current hierarchy, do this:
3479
3480 @example
3481 find . -name '*.[ch]' -print0 |
3482   wc -L --files0-from=- | tail -n1
3483 @end example
3484
3485 @end table
3486
3487 @exitstatus
3488
3489
3490 @node sum invocation
3491 @section @command{sum}: Print checksum and block counts
3492
3493 @pindex sum
3494 @cindex 16-bit checksum
3495 @cindex checksum, 16-bit
3496
3497 @command{sum} computes a 16-bit checksum for each given @var{file}, or
3498 standard input if none are given or for a @var{file} of @samp{-}.  Synopsis:
3499
3500 @example
3501 sum [@var{option}]@dots{} [@var{file}]@dots{}
3502 @end example
3503
3504 @command{sum} prints the checksum for each @var{file} followed by the
3505 number of blocks in the file (rounded up).  If more than one @var{file}
3506 is given, file names are also printed (by default).  (With the
3507 @option{--sysv} option, corresponding file names are printed when there is
3508 at least one file argument.)
3509
3510 By default, @sc{gnu} @command{sum} computes checksums using an algorithm
3511 compatible with BSD @command{sum} and prints file sizes in units of
3512 1024-byte blocks.
3513
3514 The program accepts the following options.  Also see @ref{Common options}.
3515
3516 @table @samp
3517
3518 @item -r
3519 @opindex -r
3520 @cindex BSD @command{sum}
3521 Use the default (BSD compatible) algorithm.  This option is included for
3522 compatibility with the System V @command{sum}.  Unless @option{-s} was also
3523 given, it has no effect.
3524
3525 @item -s
3526 @itemx --sysv
3527 @opindex -s
3528 @opindex --sysv
3529 @cindex System V @command{sum}
3530 Compute checksums using an algorithm compatible with System V
3531 @command{sum}'s default, and print file sizes in units of 512-byte blocks.
3532
3533 @end table
3534
3535 @command{sum} is provided for compatibility; the @command{cksum} program (see
3536 next section) is preferable in new applications.
3537
3538 @exitstatus
3539
3540
3541 @node cksum invocation
3542 @section @command{cksum}: Print CRC checksum and byte counts
3543
3544 @pindex cksum
3545 @cindex cyclic redundancy check
3546 @cindex CRC checksum
3547
3548 @command{cksum} computes a cyclic redundancy check (CRC) checksum for each
3549 given @var{file}, or standard input if none are given or for a
3550 @var{file} of @samp{-}.  Synopsis:
3551
3552 @example
3553 cksum [@var{option}]@dots{} [@var{file}]@dots{}
3554 @end example
3555
3556 @command{cksum} prints the CRC checksum for each file along with the number
3557 of bytes in the file, and the file name unless no arguments were given.
3558
3559 @command{cksum} is typically used to ensure that files
3560 transferred by unreliable means (e.g., netnews) have not been corrupted,
3561 by comparing the @command{cksum} output for the received files with the
3562 @command{cksum} output for the original files (typically given in the
3563 distribution).
3564
3565 The CRC algorithm is specified by the @acronym{POSIX} standard.  It is not
3566 compatible with the BSD or System V @command{sum} algorithms (see the
3567 previous section); it is more robust.
3568
3569 The only options are @option{--help} and @option{--version}.  @xref{Common
3570 options}.
3571
3572 @exitstatus
3573
3574
3575 @node md5sum invocation
3576 @section @command{md5sum}: Print or check MD5 digests
3577
3578 @pindex md5sum
3579 @cindex MD5
3580 @cindex 128-bit checksum
3581 @cindex checksum, 128-bit
3582 @cindex fingerprint, 128-bit
3583 @cindex message-digest, 128-bit
3584
3585 @command{md5sum} computes a 128-bit checksum (or @dfn{fingerprint} or
3586 @dfn{message-digest}) for each specified @var{file}.
3587
3588 Note: The MD5 digest is more reliable than a simple CRC (provided by
3589 the @command{cksum} command) for detecting accidental file corruption,
3590 as the chances of accidentally having two files with identical MD5
3591 are vanishingly small.  However, it should not be considered secure
3592 against malicious tampering: although finding a file with a given MD5
3593 fingerprint is considered infeasible at the moment, it is known how
3594 to modify certain files, including digital certificates, so that they
3595 appear valid when signed with an MD5 digest.
3596 For more secure hashes, consider using SHA-2.  @xref{sha2 utilities}.
3597
3598 If a @var{file} is specified as @samp{-} or if no files are given
3599 @command{md5sum} computes the checksum for the standard input.
3600 @command{md5sum} can also determine whether a file and checksum are
3601 consistent.  Synopsis:
3602
3603 @example
3604 md5sum [@var{option}]@dots{} [@var{file}]@dots{}
3605 @end example
3606
3607 For each @var{file}, @samp{md5sum} outputs the MD5 checksum, a flag
3608 indicating a binary or text input file, and the file name.
3609 If @var{file} contains a backslash or newline, the
3610 line is started with a backslash, and each problematic character in
3611 the file name is escaped with a backslash, making the output
3612 unambiguous even in the presence of arbitrary file names.
3613 If @var{file} is omitted or specified as @samp{-}, standard input is read.
3614
3615 The program accepts the following options.  Also see @ref{Common options}.
3616
3617 @table @samp
3618
3619 @item -b
3620 @itemx --binary
3621 @opindex -b
3622 @opindex --binary
3623 @cindex binary input files
3624 Treat each input file as binary, by reading it in binary mode and
3625 outputting a @samp{*} flag.  This is the inverse of @option{--text}.
3626 On systems like @acronym{GNU} that do not distinguish between binary
3627 and text files, this option merely flags each input file as binary:
3628 the MD5 checksum is unaffected.  This option is the default on systems
3629 like MS-DOS that distinguish between binary and text files, except
3630 for reading standard input when standard input is a terminal.
3631
3632 @item -c
3633 @itemx --check
3634 Read file names and checksum information (not data) from each
3635 @var{file} (or from stdin if no @var{file} was specified) and report
3636 whether the checksums match the contents of the named files.
3637 The input to this mode of @command{md5sum} is usually the output of
3638 a prior, checksum-generating run of @samp{md5sum}.
3639 Each valid line of input consists of an MD5 checksum, a binary/text
3640 flag, and then a file name.
3641 Binary files are marked with @samp{*}, text with @samp{ }.
3642 For each such line, @command{md5sum} reads the named file and computes its
3643 MD5 checksum.  Then, if the computed message digest does not match the
3644 one on the line with the file name, the file is noted as having
3645 failed the test.  Otherwise, the file passes the test.
3646 By default, for each valid line, one line is written to standard
3647 output indicating whether the named file passed the test.
3648 After all checks have been performed, if there were any failures,
3649 a warning is issued to standard error.
3650 Use the @option{--status} option to inhibit that output.
3651 If any listed file cannot be opened or read, if any valid line has
3652 an MD5 checksum inconsistent with the associated file, or if no valid
3653 line is found, @command{md5sum} exits with nonzero status.  Otherwise,
3654 it exits successfully.
3655
3656 @itemx --quiet
3657 @opindex --quiet
3658 @cindex verifying MD5 checksums
3659 This option is useful only when verifying checksums.
3660 When verifying checksums, don't generate an 'OK' message per successfully
3661 checked file.  Files that fail the verification are reported in the
3662 default one-line-per-file format.  If there is any checksum mismatch,
3663 print a warning summarizing the failures to standard error.
3664
3665 @itemx --status
3666 @opindex --status
3667 @cindex verifying MD5 checksums
3668 This option is useful only when verifying checksums.
3669 When verifying checksums, don't generate the default one-line-per-file
3670 diagnostic and don't output the warning summarizing any failures.
3671 Failures to open or read a file still evoke individual diagnostics to
3672 standard error.
3673 If all listed files are readable and are consistent with the associated
3674 MD5 checksums, exit successfully.  Otherwise exit with a status code
3675 indicating there was a failure.
3676
3677 @item -t
3678 @itemx --text
3679 @opindex -t
3680 @opindex --text
3681 @cindex text input files
3682 Treat each input file as text, by reading it in text mode and
3683 outputting a @samp{ } flag.  This is the inverse of @option{--binary}.
3684 This option is the default on systems like @acronym{GNU} that do not
3685 distinguish between binary and text files.  On other systems, it is
3686 the default for reading standard input when standard input is a
3687 terminal.
3688
3689 @item -w
3690 @itemx --warn
3691 @opindex -w
3692 @opindex --warn
3693 @cindex verifying MD5 checksums
3694 When verifying checksums, warn about improperly formatted MD5 checksum lines.
3695 This option is useful only if all but a few lines in the checked input
3696 are valid.
3697
3698 @end table
3699
3700 @exitstatus
3701
3702
3703 @node sha1sum invocation
3704 @section @command{sha1sum}: Print or check SHA-1 digests
3705
3706 @pindex sha1sum
3707 @cindex SHA-1
3708 @cindex 160-bit checksum
3709 @cindex checksum, 160-bit
3710 @cindex fingerprint, 160-bit
3711 @cindex message-digest, 160-bit
3712
3713 @command{sha1sum} computes a 160-bit checksum for each specified
3714 @var{file}.  The usage and options of this command are precisely the
3715 same as for @command{md5sum}.  @xref{md5sum invocation}.
3716
3717 Note: The SHA-1 digest is more secure than MD5, and no collisions of
3718 it are known (different files having the same fingerprint).  However,
3719 it is known that they can be produced with considerable, but not
3720 unreasonable, resources.  For this reason, it is generally considered
3721 that SHA-1 should be gradually phased out in favor of the more secure
3722 SHA-2 hash algorithms.  @xref{sha2 utilities}.
3723
3724
3725 @node sha2 utilities
3726 @section sha2 utilities: Print or check SHA-2 digests
3727
3728 @pindex sha224sum
3729 @pindex sha256sum
3730 @pindex sha384sum
3731 @pindex sha512sum
3732 @cindex SHA-2
3733 @cindex 224-bit checksum
3734 @cindex 256-bit checksum
3735 @cindex 384-bit checksum
3736 @cindex 512-bit checksum
3737 @cindex checksum, 224-bit
3738 @cindex checksum, 256-bit
3739 @cindex checksum, 384-bit
3740 @cindex checksum, 512-bit
3741 @cindex fingerprint, 224-bit
3742 @cindex fingerprint, 256-bit
3743 @cindex fingerprint, 384-bit
3744 @cindex fingerprint, 512-bit
3745 @cindex message-digest, 224-bit
3746 @cindex message-digest, 256-bit
3747 @cindex message-digest, 384-bit
3748 @cindex message-digest, 512-bit
3749
3750 The commands @command{sha224sum}, @command{sha256sum},
3751 @command{sha384sum} and @command{sha512sum} compute checksums of
3752 various lengths (respectively 224, 256, 384 and 512 bits),
3753 collectively known as the SHA-2 hashes.  The usage and options of
3754 these commands are precisely the same as for @command{md5sum}.
3755 @xref{md5sum invocation}.
3756
3757 Note: The SHA384 and SHA512 digests are considerably slower to
3758 compute, especially on 32-bit computers, than SHA224 or SHA256.
3759
3760
3761 @node Operating on sorted files
3762 @chapter Operating on sorted files
3763
3764 @cindex operating on sorted files
3765 @cindex sorted files, operations on
3766
3767 These commands work with (or produce) sorted files.
3768
3769 @menu
3770 * sort invocation::             Sort text files.
3771 * shuf invocation::             Shuffle text files.
3772 * uniq invocation::             Uniquify files.
3773 * comm invocation::             Compare two sorted files line by line.
3774 * ptx invocation::              Produce a permuted index of file contents.
3775 * tsort invocation::            Topological sort.
3776 @end menu
3777
3778
3779 @node sort invocation
3780 @section @command{sort}: Sort text files
3781
3782 @pindex sort
3783 @cindex sorting files
3784
3785 @command{sort} sorts, merges, or compares all the lines from the given
3786 files, or standard input if none are given or for a @var{file} of
3787 @samp{-}.  By default, @command{sort} writes the results to standard
3788 output.  Synopsis:
3789
3790 @example
3791 sort [@var{option}]@dots{} [@var{file}]@dots{}
3792 @end example
3793
3794 @command{sort} has three modes of operation: sort (the default), merge,
3795 and check for sortedness.  The following options change the operation
3796 mode:
3797
3798 @table @samp
3799
3800 @item -c
3801 @itemx --check
3802 @itemx --check=diagnose-first
3803 @opindex -c
3804 @opindex --check
3805 @cindex checking for sortedness
3806 Check whether the given file is already sorted: if it is not all
3807 sorted, print a diagnostic containing the first out-of-order line and
3808 exit with a status of 1.
3809 Otherwise, exit successfully.
3810 At most one input file can be given.
3811
3812 @item -C
3813 @itemx --check=quiet
3814 @itemx --check=silent
3815 @opindex -c
3816 @opindex --check
3817 @cindex checking for sortedness
3818 Exit successfully if the given file is already sorted, and
3819 exit with status 1 otherwise.
3820 At most one input file can be given.
3821 This is like @option{-c}, except it does not print a diagnostic.
3822
3823 @item -m
3824 @itemx --merge
3825 @opindex -m
3826 @opindex --merge
3827 @cindex merging sorted files
3828 Merge the given files by sorting them as a group.  Each input file must
3829 always be individually sorted.  It always works to sort instead of
3830 merge; merging is provided because it is faster, in the case where it
3831 works.
3832
3833 @end table
3834
3835 @cindex sort stability
3836 @cindex sort's last-resort comparison
3837 A pair of lines is compared as follows:
3838 @command{sort} compares each pair of fields, in the
3839 order specified on the command line, according to the associated
3840 ordering options, until a difference is found or no fields are left.
3841 If no key fields are specified, @command{sort} uses a default key of
3842 the entire line.  Finally, as a last resort when all keys compare
3843 equal, @command{sort} compares entire lines as if no ordering options
3844 other than @option{--reverse} (@option{-r}) were specified.  The
3845 @option{--stable} (@option{-s}) option disables this @dfn{last-resort
3846 comparison} so that lines in which all fields compare equal are left
3847 in their original relative order.  The @option{--unique}
3848 (@option{-u}) option also disables the last-resort comparison.
3849
3850 @vindex LC_ALL
3851 @vindex LC_COLLATE
3852 Unless otherwise specified, all comparisons use the character collating
3853 sequence specified by the @env{LC_COLLATE} locale.@footnote{If you
3854 use a non-@acronym{POSIX} locale (e.g., by setting @env{LC_ALL}
3855 to @samp{en_US}), then @command{sort} may produce output that is sorted
3856 differently than you're accustomed to.  In that case, set the @env{LC_ALL}
3857 environment variable to @samp{C}.  Note that setting only @env{LC_COLLATE}
3858 has two problems.  First, it is ineffective if @env{LC_ALL} is also set.
3859 Second, it has undefined behavior if @env{LC_CTYPE} (or @env{LANG}, if
3860 @env{LC_CTYPE} is unset) is set to an incompatible value.  For example,
3861 you get undefined behavior if @env{LC_CTYPE} is @code{ja_JP.PCK} but
3862 @env{LC_COLLATE} is @code{en_US.UTF-8}.}
3863
3864 @sc{gnu} @command{sort} (as specified for all @sc{gnu} utilities) has no
3865 limit on input line length or restrictions on bytes allowed within lines.
3866 In addition, if the final byte of an input file is not a newline, @sc{gnu}
3867 @command{sort} silently supplies one.  A line's trailing newline is not
3868 part of the line for comparison purposes.
3869
3870 @cindex exit status of @command{sort}
3871 Exit status:
3872
3873 @display
3874 0 if no error occurred
3875 1 if invoked with @option{-c} or @option{-C} and the input is not sorted
3876 2 if an error occurred
3877 @end display
3878
3879 @vindex TMPDIR
3880 If the environment variable @env{TMPDIR} is set, @command{sort} uses its
3881 value as the directory for temporary files instead of @file{/tmp}.  The
3882 @option{--temporary-directory} (@option{-T}) option in turn overrides
3883 the environment variable.
3884
3885 The following options affect the ordering of output lines.  They may be
3886 specified globally or as part of a specific key field.  If no key
3887 fields are specified, global options apply to comparison of entire
3888 lines; otherwise the global options are inherited by key fields that do
3889 not specify any special options of their own.  In pre-@acronym{POSIX}
3890 versions of @command{sort}, global options affect only later key fields,
3891 so portable shell scripts should specify global options first.
3892
3893 @table @samp
3894
3895 @item -b
3896 @itemx --ignore-leading-blanks
3897 @opindex -b
3898 @opindex --ignore-leading-blanks
3899 @cindex blanks, ignoring leading
3900 @vindex LC_CTYPE
3901 Ignore leading blanks when finding sort keys in each line.
3902 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3903 can change this.  Note blanks may be ignored by your locale's collating
3904 rules, but without this option they will be significant for character
3905 positions specified in keys with the @option{-k} option.
3906
3907 @item -d
3908 @itemx --dictionary-order
3909 @opindex -d
3910 @opindex --dictionary-order
3911 @cindex dictionary order
3912 @cindex phone directory order
3913 @cindex telephone directory order
3914 @vindex LC_CTYPE
3915 Sort in @dfn{phone directory} order: ignore all characters except
3916 letters, digits and blanks when sorting.
3917 By default letters and digits are those of @acronym{ASCII} and a blank
3918 is a space or a tab, but the @env{LC_CTYPE} locale can change this.
3919
3920 @item -f
3921 @itemx --ignore-case
3922 @opindex -f
3923 @opindex --ignore-case
3924 @cindex ignoring case
3925 @cindex case folding
3926 @vindex LC_CTYPE
3927 Fold lowercase characters into the equivalent uppercase characters when
3928 comparing so that, for example, @samp{b} and @samp{B} sort as equal.
3929 The @env{LC_CTYPE} locale determines character types.
3930 When used with @option{--unique} those lower case equivalent lines are
3931 thrown away. (There is currently no way to throw away the upper case
3932 equivalent instead. (Any @option{--reverse} given would only affect
3933 the final result, after the throwing away.))
3934
3935 @item -g
3936 @itemx --general-numeric-sort
3937 @itemx --sort=general-numeric
3938 @opindex -g
3939 @opindex --general-numeric-sort
3940 @opindex --sort
3941 @cindex general numeric sort
3942 @vindex LC_NUMERIC
3943 Sort numerically, converting a prefix of each line to a long
3944 double-precision floating point number.  @xref{Floating point}.
3945 Do not report overflow, underflow, or conversion errors.
3946 Use the following collating sequence:
3947
3948 @itemize @bullet
3949 @item
3950 Lines that do not start with numbers (all considered to be equal).
3951 @item
3952 NaNs (``Not a Number'' values, in IEEE floating point arithmetic)
3953 in a consistent but machine-dependent order.
3954 @item
3955 Minus infinity.
3956 @item
3957 Finite numbers in ascending numeric order (with @math{-0} and @math{+0} equal).
3958 @item
3959 Plus infinity.
3960 @end itemize
3961
3962 Use this option only if there is no alternative; it is much slower than
3963 @option{--numeric-sort} (@option{-n}) and it can lose information when
3964 converting to floating point.
3965
3966 @item -h
3967 @itemx --human-numeric-sort
3968 @itemx --sort=human-numeric
3969 @opindex -h
3970 @opindex --human-numeric-sort
3971 @opindex --sort
3972 @cindex human numeric sort
3973 @vindex LC_NUMERIC
3974 Sort numerically, first by numeric sign (negative, zero, or positive);
3975 then by @acronym{SI} suffix (either empty, or @samp{k} or @samp{K}, or
3976 one of @samp{MGTPEZY}, in that order; @pxref{Block size}); and finally
3977 by numeric value.  For example, @samp{1023M} sorts before @samp{1G}
3978 because @samp{M} (mega) precedes @samp{G} (giga) as an @acronym{SI}
3979 suffix.  This option sorts values that are consistently scaled to the
3980 nearest suffix, regardless of whether suffixes denote powers of 1000
3981 or 1024, and it therefore sorts the output of any single invocation of
3982 the @command{df}, @command{du}, or @command{ls} commands that are
3983 invoked with their @option{--human-readable} or @option{--si} options.
3984 The syntax for numbers is the same as for the @option{--numeric-sort}
3985 option; the @acronym{SI} suffix must immediately follow the number.
3986
3987 @item -i
3988 @itemx --ignore-nonprinting
3989 @opindex -i
3990 @opindex --ignore-nonprinting
3991 @cindex nonprinting characters, ignoring
3992 @cindex unprintable characters, ignoring
3993 @vindex LC_CTYPE
3994 Ignore nonprinting characters.
3995 The @env{LC_CTYPE} locale determines character types.
3996 This option has no effect if the stronger @option{--dictionary-order}
3997 (@option{-d}) option is also given.
3998
3999 @item -M
4000 @itemx --month-sort
4001 @itemx --sort=month
4002 @opindex -M
4003 @opindex --month-sort
4004 @opindex --sort
4005 @cindex months, sorting by
4006 @vindex LC_TIME
4007 An initial string, consisting of any amount of blanks, followed
4008 by a month name abbreviation, is folded to UPPER case and
4009 compared in the order @samp{JAN} < @samp{FEB} < @dots{} < @samp{DEC}.
4010 Invalid names compare low to valid names.  The @env{LC_TIME} locale
4011 category determines the month spellings.
4012 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
4013 can change this.
4014
4015 @item -n
4016 @itemx --numeric-sort
4017 @itemx --sort=numeric
4018 @opindex -n
4019 @opindex --numeric-sort
4020 @opindex --sort
4021 @cindex numeric sort
4022 @vindex LC_NUMERIC
4023 Sort numerically.  The number begins each line and consists
4024 of optional blanks, an optional @samp{-} sign, and zero or more
4025 digits possibly separated by thousands separators, optionally followed
4026 by a decimal-point character and zero or more digits.  An empty
4027 number is treated as @samp{0}.  The @env{LC_NUMERIC}
4028 locale specifies the decimal-point character and thousands separator.
4029 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
4030 can change this.
4031
4032 Comparison is exact; there is no rounding error.
4033
4034 Neither a leading @samp{+} nor exponential notation is recognized.
4035 To compare such strings numerically, use the
4036 @option{--general-numeric-sort} (@option{-g}) option.
4037
4038 @item -V
4039 @itemx --version-sort
4040 @opindex -V
4041 @opindex --version-sort
4042 @cindex version number sort
4043 Sort by version name and number.  It behaves like a standard sort,
4044 except that each sequence of decimal digits is treated numerically
4045 as an index/version number.  (@xref{Details about version sort}.)
4046
4047 @item -r
4048 @itemx --reverse
4049 @opindex -r
4050 @opindex --reverse
4051 @cindex reverse sorting
4052 Reverse the result of comparison, so that lines with greater key values
4053 appear earlier in the output instead of later.
4054
4055 @item -R
4056 @itemx --random-sort
4057 @itemx --sort=random
4058 @opindex -R
4059 @opindex --random-sort
4060 @opindex --sort
4061 @cindex random sort
4062 Sort by hashing the input keys and then sorting the hash values.
4063 Choose the hash function at random, ensuring that it is free of
4064 collisions so that differing keys have differing hash values.  This is
4065 like a random permutation of the inputs (@pxref{shuf invocation}),
4066 except that keys with the same value sort together.
4067
4068 If multiple random sort fields are specified, the same random hash
4069 function is used for all fields.  To use different random hash
4070 functions for different fields, you can invoke @command{sort} more
4071 than once.
4072
4073 The choice of hash function is affected by the
4074 @option{--random-source} option.
4075
4076 @end table
4077
4078 Other options are:
4079
4080 @table @samp
4081
4082 @item --compress-program=@var{prog}
4083 Compress any temporary files with the program @var{prog}.
4084
4085 With no arguments, @var{prog} must compress standard input to standard
4086 output, and when given the @option{-d} option it must decompress
4087 standard input to standard output.
4088
4089 Terminate with an error if @var{prog} exits with nonzero status.
4090
4091 White space and the backslash character should not appear in
4092 @var{prog}; they are reserved for future use.
4093
4094 @filesZeroFromOption{sort,,sorted output}
4095
4096 @item -k @var{pos1}[,@var{pos2}]
4097 @itemx --key=@var{pos1}[,@var{pos2}]
4098 @opindex -k
4099 @opindex --key
4100 @cindex sort field
4101 Specify a sort field that consists of the part of the line between
4102 @var{pos1} and @var{pos2} (or the end of the line, if @var{pos2} is
4103 omitted), @emph{inclusive}.
4104
4105 Each @var{pos} has the form @samp{@var{f}[.@var{c}][@var{opts}]},
4106 where @var{f} is the number of the field to use, and @var{c} is the number
4107 of the first character from the beginning of the field.  Fields and character
4108 positions are numbered starting with 1; a character position of zero in
4109 @var{pos2} indicates the field's last character.  If @samp{.@var{c}} is
4110 omitted from @var{pos1}, it defaults to 1 (the beginning of the field);
4111 if omitted from @var{pos2}, it defaults to 0 (the end of the field).
4112 @var{opts} are ordering options, allowing individual keys to be sorted
4113 according to different rules; see below for details.  Keys can span
4114 multiple fields.
4115
4116 Example:  To sort on the second field, use @option{--key=2,2}
4117 (@option{-k 2,2}).  See below for more notes on keys and more examples.
4118 See also the @option{--debug} option to help determine the part
4119 of the line being used in the sort.
4120
4121 @item --debug
4122 Highlight the portion of each line used for sorting.
4123 Also issue warnings about questionable usage to stderr.
4124
4125 @item --batch-size=@var{nmerge}
4126 @opindex --batch-size
4127 @cindex number of inputs to merge, nmerge
4128 Merge at most @var{nmerge} inputs at once.
4129
4130 When @command{sort} has to merge more than @var{nmerge} inputs,
4131 it merges them in groups of @var{nmerge}, saving the result in
4132 a temporary file, which is then used as an input in a subsequent merge.
4133
4134 A large value of @var{nmerge} may improve merge performance and decrease
4135 temporary storage utilization at the expense of increased memory usage
4136 and I/0.  Conversely a small value of @var{nmerge} may reduce memory
4137 requirements and I/0 at the expense of temporary storage consumption and
4138 merge performance.
4139
4140 The value of @var{nmerge} must be at least 2.  The default value is
4141 currently 16, but this is implementation-dependent and may change in
4142 the future.
4143
4144 The value of @var{nmerge} may be bounded by a resource limit for open
4145 file descriptors.  The commands @samp{ulimit -n} or @samp{getconf
4146 OPEN_MAX} may display limits for your systems; these limits may be
4147 modified further if your program already has some files open, or if
4148 the operating system has other limits on the number of open files.  If
4149 the value of @var{nmerge} exceeds the resource limit, @command{sort}
4150 silently uses a smaller value.
4151
4152 @item -o @var{output-file}
4153 @itemx --output=@var{output-file}
4154 @opindex -o
4155 @opindex --output
4156 @cindex overwriting of input, allowed
4157 Write output to @var{output-file} instead of standard output.
4158 Normally, @command{sort} reads all input before opening
4159 @var{output-file}, so you can safely sort a file in place by using
4160 commands like @code{sort -o F F} and @code{cat F | sort -o F}.
4161 However, @command{sort} with @option{--merge} (@option{-m}) can open
4162 the output file before reading all input, so a command like @code{cat
4163 F | sort -m -o F - G} is not safe as @command{sort} might start
4164 writing @file{F} before @command{cat} is done reading it.
4165
4166 @vindex POSIXLY_CORRECT
4167 On newer systems, @option{-o} cannot appear after an input file if
4168 @env{POSIXLY_CORRECT} is set, e.g., @samp{sort F -o F}.  Portable
4169 scripts should specify @option{-o @var{output-file}} before any input
4170 files.
4171
4172 @item --random-source=@var{file}
4173 @opindex --random-source
4174 @cindex random source for sorting
4175 Use @var{file} as a source of random data used to determine which
4176 random hash function to use with the @option{-R} option.  @xref{Random
4177 sources}.
4178
4179 @item -s
4180 @itemx --stable
4181 @opindex -s
4182 @opindex --stable
4183 @cindex sort stability
4184 @cindex sort's last-resort comparison
4185
4186 Make @command{sort} stable by disabling its last-resort comparison.
4187 This option has no effect if no fields or global ordering options
4188 other than @option{--reverse} (@option{-r}) are specified.
4189
4190 @item -S @var{size}
4191 @itemx --buffer-size=@var{size}
4192 @opindex -S
4193 @opindex --buffer-size
4194 @cindex size for main memory sorting
4195 Use a main-memory sort buffer of the given @var{size}.  By default,
4196 @var{size} is in units of 1024 bytes.  Appending @samp{%} causes
4197 @var{size} to be interpreted as a percentage of physical memory.
4198 Appending @samp{K} multiplies @var{size} by 1024 (the default),
4199 @samp{M} by 1,048,576, @samp{G} by 1,073,741,824, and so on for
4200 @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}.  Appending
4201 @samp{b} causes @var{size} to be interpreted as a byte count, with no
4202 multiplication.
4203
4204 This option can improve the performance of @command{sort} by causing it
4205 to start with a larger or smaller sort buffer than the default.
4206 However, this option affects only the initial buffer size.  The buffer
4207 grows beyond @var{size} if @command{sort} encounters input lines larger
4208 than @var{size}.
4209
4210 @item -t @var{separator}
4211 @itemx --field-separator=@var{separator}
4212 @opindex -t
4213 @opindex --field-separator
4214 @cindex field separator character
4215 Use character @var{separator} as the field separator when finding the
4216 sort keys in each line.  By default, fields are separated by the empty
4217 string between a non-blank character and a blank character.
4218 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
4219 can change this.
4220
4221 That is, given the input line @w{@samp{ foo bar}}, @command{sort} breaks it
4222 into fields @w{@samp{ foo}} and @w{@samp{ bar}}.  The field separator is
4223 not considered to be part of either the field preceding or the field
4224 following, so with @samp{sort @w{-t " "}} the same input line has
4225 three fields: an empty field, @samp{foo}, and @samp{bar}.
4226 However, fields that extend to the end of the line,
4227 as @option{-k 2}, or fields consisting of a range, as @option{-k 2,3},
4228 retain the field separators present between the endpoints of the range.
4229
4230 To specify @acronym{ASCII} @sc{nul} as the field separator,
4231 use the two-character string @samp{\0}, e.g., @samp{sort -t '\0'}.
4232
4233 @item -T @var{tempdir}
4234 @itemx --temporary-directory=@var{tempdir}
4235 @opindex -T
4236 @opindex --temporary-directory
4237 @cindex temporary directory
4238 @vindex TMPDIR
4239 Use directory @var{tempdir} to store temporary files, overriding the
4240 @env{TMPDIR} environment variable.  If this option is given more than
4241 once, temporary files are stored in all the directories given.  If you
4242 have a large sort or merge that is I/O-bound, you can often improve
4243 performance by using this option to specify directories on different
4244 disks and controllers.
4245
4246 @item --parallel=@var{n}
4247 @opindex --parallel
4248 @cindex multithreaded sort
4249 Set the number of sorts run in parallel to @var{n}. By default,
4250 @var{n} is set to the number of available processors, but limited
4251 to 8, as there are diminishing performance gains after that.
4252 Note also that using @var{n} threads increases the memory usage by
4253 a factor of log @var{n}.  Also see @ref{nproc invocation}.
4254
4255 @item -u
4256 @itemx --unique
4257 @opindex -u
4258 @opindex --unique
4259 @cindex uniquifying output
4260
4261 Normally, output only the first of a sequence of lines that compare
4262 equal.  For the @option{--check} (@option{-c} or @option{-C}) option,
4263 check that no pair of consecutive lines compares equal.
4264
4265 This option also disables the default last-resort comparison.
4266
4267 The commands @code{sort -u} and @code{sort | uniq} are equivalent, but
4268 this equivalence does not extend to arbitrary @command{sort} options.
4269 For example, @code{sort -n -u} inspects only the value of the initial
4270 numeric string when checking for uniqueness, whereas @code{sort -n |
4271 uniq} inspects the entire line.  @xref{uniq invocation}.
4272
4273 @macro zeroTerminatedOption
4274 @item -z
4275 @itemx --zero-terminated
4276 @opindex -z
4277 @opindex --zero-terminated
4278 @cindex process zero-terminated items
4279 Delimit items with a zero byte rather than a newline (@acronym{ASCII} @sc{lf}).
4280 I.E. treat input as items separated by @acronym{ASCII} @sc{nul}
4281 and terminate output items with @acronym{ASCII} @sc{nul}.
4282 This option can be useful in conjunction with @samp{perl -0} or
4283 @samp{find -print0} and @samp{xargs -0} which do the same in order to
4284 reliably handle arbitrary file names (even those containing blanks
4285 or other special characters).
4286 @end macro
4287 @zeroTerminatedOption
4288
4289 @end table
4290
4291 Historical (BSD and System V) implementations of @command{sort} have
4292 differed in their interpretation of some options, particularly
4293 @option{-b}, @option{-f}, and @option{-n}.
4294 @sc{gnu} sort follows the @acronym{POSIX}
4295 behavior, which is usually (but not always!) like the System V behavior.
4296 According to @acronym{POSIX}, @option{-n} no longer implies @option{-b}.  For
4297 consistency, @option{-M} has been changed in the same way.  This may
4298 affect the meaning of character positions in field specifications in
4299 obscure cases.  The only fix is to add an explicit @option{-b}.
4300
4301 A position in a sort field specified with @option{-k} may have any
4302 of the option letters @samp{MbdfghinRrV} appended to it, in which case no
4303 global ordering options are inherited by that particular field.  The
4304 @option{-b} option may be independently attached to either or both of
4305 the start and end positions of a field specification, and if it is
4306 inherited from the global options it will be attached to both.
4307 If input lines can contain leading or adjacent blanks and @option{-t}
4308 is not used, then @option{-k} is typically combined with @option{-b} or
4309 an option that implicitly ignores leading blanks (@samp{Mghn}) as otherwise
4310 the varying numbers of leading blanks in fields can cause confusing results.
4311
4312 If the start position in a sort field specifier falls after the end of
4313 the line or after the end field, the field is empty.  If the @option{-b}
4314 option was specified, the @samp{.@var{c}} part of a field specification
4315 is counted from the first nonblank character of the field.
4316
4317 @vindex _POSIX2_VERSION
4318 @vindex POSIXLY_CORRECT
4319 On older systems, @command{sort} supports an obsolete origin-zero
4320 syntax @samp{+@var{pos1} [-@var{pos2}]} for specifying sort keys.
4321 The obsolete sequence @samp{sort +@var{a}.@var{x} -@var{b}.@var{y}}
4322 is equivalent to @samp{sort -k @var{a+1}.@var{x+1},@var{b}} if @var{y}
4323 is @samp{0} or absent, otherwise it is equivalent to @samp{sort -k
4324 @var{a+1}.@var{x+1},@var{b+1}.@var{y}}.
4325
4326 This obsolete behavior can be enabled or disabled with the
4327 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
4328 conformance}); it can also be enabled when @env{POSIXLY_CORRECT} is
4329 not set by using the obsolete syntax with @samp{-@var{pos2}} present.
4330
4331 Scripts intended for use on standard hosts should avoid obsolete
4332 syntax and should use @option{-k} instead.  For example, avoid
4333 @samp{sort +2}, since it might be interpreted as either @samp{sort
4334 ./+2} or @samp{sort -k 3}.  If your script must also run on hosts that
4335 support only the obsolete syntax, it can use a test like @samp{if sort
4336 -k 1 </dev/null >/dev/null 2>&1; then @dots{}} to decide which syntax
4337 to use.
4338
4339 Here are some examples to illustrate various combinations of options.
4340
4341 @itemize @bullet
4342
4343 @item
4344 Sort in descending (reverse) numeric order.
4345
4346 @example
4347 sort -n -r
4348 @end example
4349
4350 @item
4351 Run no more that 4 sorts concurrently, using a buffer size of 10M.
4352
4353 @example
4354 sort --parallel=4 -S 10M
4355 @end example
4356
4357 @item
4358 Sort alphabetically, omitting the first and second fields
4359 and the blanks at the start of the third field.
4360 This uses a single key composed of the characters beginning
4361 at the start of the first nonblank character in field three
4362 and extending to the end of each line.
4363
4364 @example
4365 sort -k 3b
4366 @end example
4367
4368 @item
4369 Sort numerically on the second field and resolve ties by sorting
4370 alphabetically on the third and fourth characters of field five.
4371 Use @samp{:} as the field delimiter.
4372
4373 @example
4374 sort -t : -k 2,2n -k 5.3,5.4
4375 @end example
4376
4377 Note that if you had written @option{-k 2n} instead of @option{-k 2,2n}
4378 @command{sort} would have used all characters beginning in the second field
4379 and extending to the end of the line as the primary @emph{numeric}
4380 key.  For the large majority of applications, treating keys spanning
4381 more than one field as numeric will not do what you expect.
4382
4383 Also note that the @samp{n} modifier was applied to the field-end
4384 specifier for the first key.  It would have been equivalent to
4385 specify @option{-k 2n,2} or @option{-k 2n,2n}.  All modifiers except
4386 @samp{b} apply to the associated @emph{field}, regardless of whether
4387 the modifier character is attached to the field-start and/or the
4388 field-end part of the key specifier.
4389
4390 @item
4391 Sort the password file on the fifth field and ignore any
4392 leading blanks.  Sort lines with equal values in field five
4393 on the numeric user ID in field three.  Fields are separated
4394 by @samp{:}.
4395
4396 @example
4397 sort -t : -k 5b,5 -k 3,3n /etc/passwd
4398 sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
4399 sort -t : -b -k 5,5 -k 3,3n /etc/passwd
4400 @end example
4401
4402 These three commands have equivalent effect.  The first specifies that
4403 the first key's start position ignores leading blanks and the second
4404 key is sorted numerically.  The other two commands rely on global
4405 options being inherited by sort keys that lack modifiers.  The inheritance
4406 works in this case because @option{-k 5b,5b} and @option{-k 5b,5} are
4407 equivalent, as the location of a field-end lacking a @samp{.@var{c}}
4408 character position is not affected by whether initial blanks are
4409 skipped.
4410
4411 @item
4412 Sort a set of log files, primarily by IPv4 address and secondarily by
4413 time stamp.  If two lines' primary and secondary keys are identical,
4414 output the lines in the same order that they were input.  The log
4415 files contain lines that look like this:
4416
4417 @example
4418 4.150.156.3 - - [01/Apr/2004:06:31:51 +0000] message 1
4419 211.24.3.231 - - [24/Apr/2004:20:17:39 +0000] message 2
4420 @end example
4421
4422 Fields are separated by exactly one space.  Sort IPv4 addresses
4423 lexicographically, e.g., 212.61.52.2 sorts before 212.129.233.201
4424 because 61 is less than 129.
4425
4426 @example
4427 sort -s -t ' ' -k 4.9n -k 4.5M -k 4.2n -k 4.14,4.21 file*.log |
4428 sort -s -t '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n
4429 @end example
4430
4431 This example cannot be done with a single @command{sort} invocation,
4432 since IPv4 address components are separated by @samp{.} while dates
4433 come just after a space.  So it is broken down into two invocations of
4434 @command{sort}: the first sorts by time stamp and the second by IPv4
4435 address.  The time stamp is sorted by year, then month, then day, and
4436 finally by hour-minute-second field, using @option{-k} to isolate each
4437 field.  Except for hour-minute-second there's no need to specify the
4438 end of each key field, since the @samp{n} and @samp{M} modifiers sort
4439 based on leading prefixes that cannot cross field boundaries.  The
4440 IPv4 addresses are sorted lexicographically.  The second sort uses
4441 @samp{-s} so that ties in the primary key are broken by the secondary
4442 key; the first sort uses @samp{-s} so that the combination of the two
4443 sorts is stable.
4444
4445 @item
4446 Generate a tags file in case-insensitive sorted order.
4447
4448 @smallexample
4449 find src -type f -print0 | sort -z -f | xargs -0 etags --append
4450 @end smallexample
4451
4452 The use of @option{-print0}, @option{-z}, and @option{-0} in this case means
4453 that file names that contain blanks or other special characters are
4454 not broken up
4455 by the sort operation.
4456
4457 @c This example is a bit contrived and needs more explanation.
4458 @c @item
4459 @c Sort records separated by an arbitrary string by using a pipe to convert
4460 @c each record delimiter string to @samp{\0}, then using sort's -z option,
4461 @c and converting each @samp{\0} back to the original record delimiter.
4462 @c
4463 @c @example
4464 @c printf 'c\n\nb\n\na\n' |
4465 @c perl -0pe 's/\n\n/\n\0/g' |
4466 @c sort -z |
4467 @c perl -0pe 's/\0/\n/g'
4468 @c @end example
4469
4470 @item
4471 Use the common @acronym{DSU, Decorate Sort Undecorate} idiom to
4472 sort lines according to their length.
4473
4474 @example
4475 awk '@{print length, $0@}' /etc/passwd | sort -n | cut -f2- -d' '
4476 @end example
4477
4478 In general this technique can be used to sort data that the @command{sort}
4479 command does not support, or is inefficient at, sorting directly.
4480
4481 @item
4482 Shuffle a list of directories, but preserve the order of files within
4483 each directory.  For instance, one could use this to generate a music
4484 playlist in which albums are shuffled but the songs of each album are
4485 played in order.
4486
4487 @example
4488 ls */* | sort -t / -k 1,1R -k 2,2
4489 @end example
4490
4491 @end itemize
4492
4493
4494 @node shuf invocation
4495 @section @command{shuf}: Shuffling text
4496
4497 @pindex shuf
4498 @cindex shuffling files
4499
4500 @command{shuf} shuffles its input by outputting a random permutation
4501 of its input lines.  Each output permutation is equally likely.
4502 Synopses:
4503
4504 @example
4505 shuf [@var{option}]@dots{} [@var{file}]
4506 shuf -e [@var{option}]@dots{} [@var{arg}]@dots{}
4507 shuf -i @var{lo}-@var{hi} [@var{option}]@dots{}
4508 @end example
4509
4510 @command{shuf} has three modes of operation that affect where it
4511 obtains its input lines.  By default, it reads lines from standard
4512 input.  The following options change the operation mode:
4513
4514 @table @samp
4515
4516 @item -e
4517 @itemx --echo
4518 @opindex -c
4519 @opindex --echo
4520 @cindex command-line operands to shuffle
4521 Treat each command-line operand as an input line.
4522
4523 @item -i @var{lo}-@var{hi}
4524 @itemx --input-range=@var{lo}-@var{hi}
4525 @opindex -i
4526 @opindex --input-range
4527 @cindex input range to shuffle
4528 Act as if input came from a file containing the range of unsigned
4529 decimal integers @var{lo}@dots{}@var{hi}, one per line.
4530
4531 @end table
4532
4533 @command{shuf}'s other options can affect its behavior in all
4534 operation modes:
4535
4536 @table @samp
4537
4538 @item -n @var{lines}
4539 @itemx --head-count=@var{count}
4540 @opindex -n
4541 @opindex --head-count
4542 @cindex head of output
4543 Output at most @var{count} lines.  By default, all input lines are
4544 output.
4545
4546 @item -o @var{output-file}
4547 @itemx --output=@var{output-file}
4548 @opindex -o
4549 @opindex --output
4550 @cindex overwriting of input, allowed
4551 Write output to @var{output-file} instead of standard output.
4552 @command{shuf} reads all input before opening
4553 @var{output-file}, so you can safely shuffle a file in place by using
4554 commands like @code{shuf -o F <F} and @code{cat F | shuf -o F}.
4555
4556 @item --random-source=@var{file}
4557 @opindex --random-source
4558 @cindex random source for shuffling
4559 Use @var{file} as a source of random data used to determine which
4560 permutation to generate.  @xref{Random sources}.
4561
4562 @zeroTerminatedOption
4563
4564 @end table
4565
4566 For example:
4567
4568 @example
4569 shuf <<EOF
4570 A man,
4571 a plan,
4572 a canal:
4573 Panama!
4574 EOF
4575 @end example
4576
4577 @noindent
4578 might produce the output
4579
4580 @example
4581 Panama!
4582 A man,
4583 a canal:
4584 a plan,
4585 @end example
4586
4587 @noindent
4588 Similarly, the command:
4589
4590 @example
4591 shuf -e clubs hearts diamonds spades
4592 @end example
4593
4594 @noindent
4595 might output:
4596
4597 @example
4598 clubs
4599 diamonds
4600 spades
4601 hearts
4602 @end example
4603
4604 @noindent
4605 and the command @samp{shuf -i 1-4} might output:
4606
4607 @example
4608 4
4609 2
4610 1
4611 3
4612 @end example
4613
4614 @noindent
4615 These examples all have four input lines, so @command{shuf} might
4616 produce any of the twenty-four possible permutations of the input.  In
4617 general, if there are @var{n} input lines, there are @var{n}! (i.e.,
4618 @var{n} factorial, or @var{n} * (@var{n} - 1) * @dots{} * 1) possible
4619 output permutations.
4620
4621 @exitstatus
4622
4623
4624 @node uniq invocation
4625 @section @command{uniq}: Uniquify files
4626
4627 @pindex uniq
4628 @cindex uniquify files
4629
4630 @command{uniq} writes the unique lines in the given @file{input}, or
4631 standard input if nothing is given or for an @var{input} name of
4632 @samp{-}.  Synopsis:
4633
4634 @example
4635 uniq [@var{option}]@dots{} [@var{input} [@var{output}]]
4636 @end example
4637
4638 By default, @command{uniq} prints its input lines, except that
4639 it discards all but the first of adjacent repeated lines, so that
4640 no output lines are repeated.  Optionally, it can instead discard
4641 lines that are not repeated, or all repeated lines.
4642
4643 The input need not be sorted, but repeated input lines are detected
4644 only if they are adjacent.  If you want to discard non-adjacent
4645 duplicate lines, perhaps you want to use @code{sort -u}.
4646 @xref{sort invocation}.
4647
4648 @vindex LC_COLLATE
4649 Comparisons honor the rules specified by the @env{LC_COLLATE}
4650 locale category.
4651
4652 If no @var{output} file is specified, @command{uniq} writes to standard
4653 output.
4654
4655 The program accepts the following options.  Also see @ref{Common options}.
4656
4657 @table @samp
4658
4659 @item -f @var{n}
4660 @itemx --skip-fields=@var{n}
4661 @opindex -f
4662 @opindex --skip-fields
4663 Skip @var{n} fields on each line before checking for uniqueness.  Use
4664 a null string for comparison if a line has fewer than @var{n} fields.  Fields
4665 are sequences of non-space non-tab characters that are separated from
4666 each other by at least one space or tab.
4667
4668 For compatibility @command{uniq} supports an obsolete option syntax
4669 @option{-@var{n}}.  New scripts should use @option{-f @var{n}} instead.
4670
4671 @item -s @var{n}
4672 @itemx --skip-chars=@var{n}
4673 @opindex -s
4674 @opindex --skip-chars
4675 Skip @var{n} characters before checking for uniqueness.  Use a null string
4676 for comparison if a line has fewer than @var{n} characters.  If you use both
4677 the field and character skipping options, fields are skipped over first.
4678
4679 @vindex _POSIX2_VERSION
4680 On older systems, @command{uniq} supports an obsolete option syntax
4681 @option{+@var{n}}.
4682 This obsolete behavior can be enabled or disabled with the
4683 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
4684 conformance}), but portable scripts should avoid commands whose
4685 behavior depends on this variable.
4686 For example, use @samp{uniq ./+10} or @samp{uniq -s 10} rather than
4687 the ambiguous @samp{uniq +10}.
4688
4689 @item -c
4690 @itemx --count
4691 @opindex -c
4692 @opindex --count
4693 Print the number of times each line occurred along with the line.
4694
4695 @item -i
4696 @itemx --ignore-case
4697 @opindex -i
4698 @opindex --ignore-case
4699 Ignore differences in case when comparing lines.
4700
4701 @item -d
4702 @itemx --repeated
4703 @opindex -d
4704 @opindex --repeated
4705 @cindex repeated lines, outputting
4706 Discard lines that are not repeated.  When used by itself, this option
4707 causes @command{uniq} to print the first copy of each repeated line,
4708 and nothing else.
4709
4710 @item -D
4711 @itemx --all-repeated[=@var{delimit-method}]
4712 @opindex -D
4713 @opindex --all-repeated
4714 @cindex all repeated lines, outputting
4715 Do not discard the second and subsequent repeated input lines,
4716 but discard lines that are not repeated.
4717 This option is useful mainly in conjunction with other options e.g.,
4718 to ignore case or to compare only selected fields.
4719 The optional @var{delimit-method} tells how to delimit
4720 groups of repeated lines, and must be one of the following:
4721
4722 @table @samp
4723
4724 @item none
4725 Do not delimit groups of repeated lines.
4726 This is equivalent to @option{--all-repeated} (@option{-D}).
4727
4728 @item prepend
4729 Output a newline before each group of repeated lines.
4730 With @option{--zero-terminated} (@option{-z}), use a zero
4731 byte (@acronym{ASCII} @sc{nul}) instead of a newline.
4732
4733 @item separate
4734 Separate groups of repeated lines with a single newline.
4735 With @option{--zero-terminated} (@option{-z}), use a zero
4736 byte (@acronym{ASCII} @sc{nul}) instead of a newline.
4737 This is the same as using @samp{prepend}, except that
4738 no delimiter is inserted before the first group, and hence
4739 may be better suited for output direct to users.
4740 @end table
4741
4742 Note that when groups are delimited and the input stream contains
4743 two or more consecutive blank lines, then the output is ambiguous.
4744 To avoid that, filter the input through @samp{tr -s '\n'} to replace
4745 each sequence of consecutive newlines with a single newline.
4746
4747 This is a @sc{gnu} extension.
4748 @c FIXME: give an example showing *how* it's useful
4749
4750 @item -u
4751 @itemx --unique
4752 @opindex -u
4753 @opindex --unique
4754 @cindex unique lines, outputting
4755 Discard the first repeated line.  When used by itself, this option
4756 causes @command{uniq} to print unique lines, and nothing else.
4757
4758 @item -w @var{n}
4759 @itemx --check-chars=@var{n}
4760 @opindex -w
4761 @opindex --check-chars
4762 Compare at most @var{n} characters on each line (after skipping any specified
4763 fields and characters).  By default the entire rest of the lines are
4764 compared.
4765
4766 @zeroTerminatedOption
4767
4768 @end table
4769
4770 @exitstatus
4771
4772
4773 @node comm invocation
4774 @section @command{comm}: Compare two sorted files line by line
4775
4776 @pindex comm
4777 @cindex line-by-line comparison
4778 @cindex comparing sorted files
4779
4780 @command{comm} writes to standard output lines that are common, and lines
4781 that are unique, to two input files; a file name of @samp{-} means
4782 standard input.  Synopsis:
4783
4784 @example
4785 comm [@var{option}]@dots{} @var{file1} @var{file2}
4786 @end example
4787
4788 @vindex LC_COLLATE
4789 Before @command{comm} can be used, the input files must be sorted using the
4790 collating sequence specified by the @env{LC_COLLATE} locale.
4791 If an input file ends in a non-newline
4792 character, a newline is silently appended.  The @command{sort} command with
4793 no options always outputs a file that is suitable input to @command{comm}.
4794
4795 @cindex differing lines
4796 @cindex common lines
4797 With no options, @command{comm} produces three-column output.  Column one
4798 contains lines unique to @var{file1}, column two contains lines unique
4799 to @var{file2}, and column three contains lines common to both files.
4800 Columns are separated by a single TAB character.
4801 @c FIXME: when there's an option to supply an alternative separator
4802 @c string, append `by default' to the above sentence.
4803
4804 @opindex -1
4805 @opindex -2
4806 @opindex -3
4807 The options @option{-1}, @option{-2}, and @option{-3} suppress printing of
4808 the corresponding columns (and separators).  Also see @ref{Common options}.
4809
4810 Unlike some other comparison utilities, @command{comm} has an exit
4811 status that does not depend on the result of the comparison.
4812 Upon normal completion @command{comm} produces an exit code of zero.
4813 If there is an error it exits with nonzero status.
4814
4815 @macro checkOrderOption{cmd}
4816 If the @option{--check-order} option is given, unsorted inputs will
4817 cause a fatal error message.  If the option @option{--nocheck-order}
4818 is given, unsorted inputs will never cause an error message.  If neither
4819 of these options is given, wrongly sorted inputs are diagnosed
4820 only if an input file is found to contain unpairable
4821 @ifset JOIN_COMMAND
4822 lines, and when both input files are non empty.
4823 @end ifset
4824 @ifclear JOIN_COMMAND
4825 lines.
4826 @end ifclear
4827 If an input file is diagnosed as being unsorted, the @command{\cmd\}
4828 command will exit with a nonzero status (and the output should not be used).
4829
4830 Forcing @command{\cmd\} to process wrongly sorted input files
4831 containing unpairable lines by specifying @option{--nocheck-order} is
4832 not guaranteed to produce any particular output.  The output will
4833 probably not correspond with whatever you hoped it would be.
4834 @end macro
4835 @checkOrderOption{comm}
4836
4837 @table @samp
4838
4839 @item --check-order
4840 Fail with an error message if either input file is wrongly ordered.
4841
4842 @item --nocheck-order
4843 Do not check that both input files are in sorted order.
4844
4845 Other options are:
4846
4847 @item --output-delimiter=@var{str}
4848 Print @var{str} between adjacent output columns,
4849 rather than the default of a single TAB character.
4850
4851 The delimiter @var{str} may not be empty.
4852
4853 @end table
4854
4855 @node ptx invocation
4856 @section @command{ptx}: Produce permuted indexes
4857
4858 @pindex ptx
4859
4860 @command{ptx} reads a text file and essentially produces a permuted index, with
4861 each keyword in its context.  The calling sketch is either one of:
4862
4863 @example
4864 ptx [@var{option} @dots{}] [@var{file} @dots{}]
4865 ptx -G [@var{option} @dots{}] [@var{input} [@var{output}]]
4866 @end example
4867
4868 The @option{-G} (or its equivalent: @option{--traditional}) option disables
4869 all @sc{gnu} extensions and reverts to traditional mode, thus introducing some
4870 limitations and changing several of the program's default option values.
4871 When @option{-G} is not specified, @sc{gnu} extensions are always enabled.
4872 @sc{gnu} extensions to @command{ptx} are documented wherever appropriate in this
4873 document.  For the full list, see @xref{Compatibility in ptx}.
4874
4875 Individual options are explained in the following sections.
4876
4877 When @sc{gnu} extensions are enabled, there may be zero, one or several
4878 @var{file}s after the options.  If there is no @var{file}, the program
4879 reads the standard input.  If there is one or several @var{file}s, they
4880 give the name of input files which are all read in turn, as if all the
4881 input files were concatenated.  However, there is a full contextual
4882 break between each file and, when automatic referencing is requested,
4883 file names and line numbers refer to individual text input files.  In
4884 all cases, the program outputs the permuted index to the standard
4885 output.
4886
4887 When @sc{gnu} extensions are @emph{not} enabled, that is, when the program
4888 operates in traditional mode, there may be zero, one or two parameters
4889 besides the options.  If there are no parameters, the program reads the
4890 standard input and outputs the permuted index to the standard output.
4891 If there is only one parameter, it names the text @var{input} to be read
4892 instead of the standard input.  If two parameters are given, they give
4893 respectively the name of the @var{input} file to read and the name of
4894 the @var{output} file to produce.  @emph{Be very careful} to note that,
4895 in this case, the contents of file given by the second parameter is
4896 destroyed.  This behavior is dictated by System V @command{ptx}
4897 compatibility; @sc{gnu} Standards normally discourage output parameters not
4898 introduced by an option.
4899
4900 Note that for @emph{any} file named as the value of an option or as an
4901 input text file, a single dash @kbd{-} may be used, in which case
4902 standard input is assumed.  However, it would not make sense to use this
4903 convention more than once per program invocation.
4904
4905 @menu
4906 * General options in ptx::      Options which affect general program behavior.
4907 * Charset selection in ptx::    Underlying character set considerations.
4908 * Input processing in ptx::     Input fields, contexts, and keyword selection.
4909 * Output formatting in ptx::    Types of output format, and sizing the fields.
4910 * Compatibility in ptx::
4911 @end menu
4912
4913
4914 @node General options in ptx
4915 @subsection General options
4916
4917 @table @samp
4918
4919 @item -G
4920 @itemx --traditional
4921 As already explained, this option disables all @sc{gnu} extensions to
4922 @command{ptx} and switches to traditional mode.
4923
4924 @item --help
4925 Print a short help on standard output, then exit without further
4926 processing.
4927
4928 @item --version
4929 Print the program version on standard output, then exit without further
4930 processing.
4931
4932 @end table
4933
4934 @exitstatus
4935
4936
4937 @node Charset selection in ptx
4938 @subsection Charset selection
4939
4940 @c FIXME:  People don't necessarily know what an IBM-PC was these days.
4941 As it is set up now, the program assumes that the input file is coded
4942 using 8-bit @acronym{ISO} 8859-1 code, also known as Latin-1 character set,
4943 @emph{unless} it is compiled for MS-DOS, in which case it uses the
4944 character set of the IBM-PC@.  (@sc{gnu} @command{ptx} is not known to work on
4945 smaller MS-DOS machines anymore.)  Compared to 7-bit @acronym{ASCII}, the set
4946 of characters which are letters is different; this alters the behavior
4947 of regular expression matching.  Thus, the default regular expression
4948 for a keyword allows foreign or diacriticized letters.  Keyword sorting,
4949 however, is still crude; it obeys the underlying character set ordering
4950 quite blindly.
4951
4952 @table @samp
4953
4954 @item -f
4955 @itemx --ignore-case
4956 Fold lower case letters to upper case for sorting.
4957
4958 @end table
4959
4960
4961 @node Input processing in ptx
4962 @subsection Word selection and input processing
4963
4964 @table @samp
4965
4966 @item -b @var{file}
4967 @itemx --break-file=@var{file}
4968
4969 This option provides an alternative (to @option{-W}) method of describing
4970 which characters make up words.  It introduces the name of a
4971 file which contains a list of characters which can@emph{not} be part of
4972 one word; this file is called the @dfn{Break file}.  Any character which
4973 is not part of the Break file is a word constituent.  If both options
4974 @option{-b} and @option{-W} are specified, then @option{-W} has precedence and
4975 @option{-b} is ignored.
4976
4977 When @sc{gnu} extensions are enabled, the only way to avoid newline as a
4978 break character is to write all the break characters in the file with no
4979 newline at all, not even at the end of the file.  When @sc{gnu} extensions
4980 are disabled, spaces, tabs and newlines are always considered as break
4981 characters even if not included in the Break file.
4982
4983 @item -i @var{file}
4984 @itemx --ignore-file=@var{file}
4985
4986 The file associated with this option contains a list of words which will
4987 never be taken as keywords in concordance output.  It is called the
4988 @dfn{Ignore file}.  The file contains exactly one word in each line; the
4989 end of line separation of words is not subject to the value of the
4990 @option{-S} option.
4991
4992 @item -o @var{file}
4993 @itemx --only-file=@var{file}
4994
4995 The file associated with this option contains a list of words which will
4996 be retained in concordance output; any word not mentioned in this file
4997 is ignored.  The file is called the @dfn{Only file}.  The file contains
4998 exactly one word in each line; the end of line separation of words is
4999 not subject to the value of the @option{-S} option.
5000
5001 There is no default for the Only file.  When both an Only file and an
5002 Ignore file are specified, a word is considered a keyword only
5003 if it is listed in the Only file and not in the Ignore file.
5004
5005 @item -r
5006 @itemx --references
5007
5008 On each input line, the leading sequence of non-white space characters will be
5009 taken to be a reference that has the purpose of identifying this input
5010 line in the resulting permuted index.  For more information about reference
5011 production, see @xref{Output formatting in ptx}.
5012 Using this option changes the default value for option @option{-S}.
5013
5014 Using this option, the program does not try very hard to remove
5015 references from contexts in output, but it succeeds in doing so
5016 @emph{when} the context ends exactly at the newline.  If option
5017 @option{-r} is used with @option{-S} default value, or when @sc{gnu} extensions
5018 are disabled, this condition is always met and references are completely
5019 excluded from the output contexts.
5020
5021 @item -S @var{regexp}
5022 @itemx --sentence-regexp=@var{regexp}
5023
5024 This option selects which regular expression will describe the end of a
5025 line or the end of a sentence.  In fact, this regular expression is not
5026 the only distinction between end of lines or end of sentences, and input
5027 line boundaries have no special significance outside this option.  By
5028 default, when @sc{gnu} extensions are enabled and if @option{-r} option is not
5029 used, end of sentences are used.  In this case, this @var{regex} is
5030 imported from @sc{gnu} Emacs:
5031
5032 @example
5033 [.?!][]\"')@}]*\\($\\|\t\\|  \\)[ \t\n]*
5034 @end example
5035
5036 Whenever @sc{gnu} extensions are disabled or if @option{-r} option is used, end
5037 of lines are used; in this case, the default @var{regexp} is just:
5038
5039 @example
5040 \n
5041 @end example
5042
5043 Using an empty @var{regexp} is equivalent to completely disabling end of
5044 line or end of sentence recognition.  In this case, the whole file is
5045 considered to be a single big line or sentence.  The user might want to
5046 disallow all truncation flag generation as well, through option @option{-F
5047 ""}.  @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
5048 Manual}.
5049
5050 When the keywords happen to be near the beginning of the input line or
5051 sentence, this often creates an unused area at the beginning of the
5052 output context line; when the keywords happen to be near the end of the
5053 input line or sentence, this often creates an unused area at the end of
5054 the output context line.  The program tries to fill those unused areas
5055 by wrapping around context in them; the tail of the input line or
5056 sentence is used to fill the unused area on the left of the output line;
5057 the head of the input line or sentence is used to fill the unused area
5058 on the right of the output line.
5059
5060 As a matter of convenience to the user, many usual backslashed escape
5061 sequences from the C language are recognized and converted to the
5062 corresponding characters by @command{ptx} itself.
5063
5064 @item -W @var{regexp}
5065 @itemx --word-regexp=@var{regexp}
5066
5067 This option selects which regular expression will describe each keyword.
5068 By default, if @sc{gnu} extensions are enabled, a word is a sequence of
5069 letters; the @var{regexp} used is @samp{\w+}.  When @sc{gnu} extensions are
5070 disabled, a word is by default anything which ends with a space, a tab
5071 or a newline; the @var{regexp} used is @samp{[^ \t\n]+}.
5072
5073 An empty @var{regexp} is equivalent to not using this option.
5074 @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
5075 Manual}.
5076
5077 As a matter of convenience to the user, many usual backslashed escape
5078 sequences, as found in the C language, are recognized and converted to
5079 the corresponding characters by @command{ptx} itself.
5080
5081 @end table
5082
5083
5084 @node Output formatting in ptx
5085 @subsection Output formatting
5086
5087 Output format is mainly controlled by the @option{-O} and @option{-T} options
5088 described in the table below.  When neither @option{-O} nor @option{-T} are
5089 selected, and if @sc{gnu} extensions are enabled, the program chooses an
5090 output format suitable for a dumb terminal.  Each keyword occurrence is
5091 output to the center of one line, surrounded by its left and right
5092 contexts.  Each field is properly justified, so the concordance output
5093 can be readily observed.  As a special feature, if automatic
5094 references are selected by option @option{-A} and are output before the
5095 left context, that is, if option @option{-R} is @emph{not} selected, then
5096 a colon is added after the reference; this nicely interfaces with @sc{gnu}
5097 Emacs @code{next-error} processing.  In this default output format, each
5098 white space character, like newline and tab, is merely changed to
5099 exactly one space, with no special attempt to compress consecutive
5100 spaces.  This might change in the future.  Except for those white space
5101 characters, every other character of the underlying set of 256
5102 characters is transmitted verbatim.
5103
5104 Output format is further controlled by the following options.
5105
5106 @table @samp
5107
5108 @item -g @var{number}
5109 @itemx --gap-size=@var{number}
5110
5111 Select the size of the minimum white space gap between the fields on the
5112 output line.
5113
5114 @item -w @var{number}
5115 @itemx --width=@var{number}
5116
5117 Select the maximum output width of each final line.  If references are
5118 used, they are included or excluded from the maximum output width
5119 depending on the value of option @option{-R}.  If this option is not
5120 selected, that is, when references are output before the left context,
5121 the maximum output width takes into account the maximum length of all
5122 references.  If this option is selected, that is, when references are
5123 output after the right context, the maximum output width does not take
5124 into account the space taken by references, nor the gap that precedes
5125 them.
5126
5127 @item -A
5128 @itemx --auto-reference
5129
5130 Select automatic references.  Each input line will have an automatic
5131 reference made up of the file name and the line ordinal, with a single
5132 colon between them.  However, the file name will be empty when standard
5133 input is being read.  If both @option{-A} and @option{-r} are selected, then
5134 the input reference is still read and skipped, but the automatic
5135 reference is used at output time, overriding the input reference.
5136
5137 @item -R
5138 @itemx --right-side-refs
5139
5140 In the default output format, when option @option{-R} is not used, any
5141 references produced by the effect of options @option{-r} or @option{-A} are
5142 placed to the far right of output lines, after the right context.  With
5143 default output format, when the @option{-R} option is specified, references
5144 are rather placed at the beginning of each output line, before the left
5145 context.  For any other output format, option @option{-R} is
5146 ignored, with one exception:  with @option{-R} the width of references
5147 is @emph{not} taken into account in total output width given by @option{-w}.
5148
5149 This option is automatically selected whenever @sc{gnu} extensions are
5150 disabled.
5151
5152 @item -F @var{string}
5153 @itemx --flac-truncation=@var{string}
5154
5155 This option will request that any truncation in the output be reported
5156 using the string @var{string}.  Most output fields theoretically extend
5157 towards the beginning or the end of the current line, or current
5158 sentence, as selected with option @option{-S}.  But there is a maximum
5159 allowed output line width, changeable through option @option{-w}, which is
5160 further divided into space for various output fields.  When a field has
5161 to be truncated because it cannot extend beyond the beginning or the end of
5162 the current line to fit in, then a truncation occurs.  By default,
5163 the string used is a single slash, as in @option{-F /}.
5164
5165 @var{string} may have more than one character, as in @option{-F ...}.
5166 Also, in the particular case when @var{string} is empty (@option{-F ""}),
5167 truncation flagging is disabled, and no truncation marks are appended in
5168 this case.
5169
5170 As a matter of convenience to the user, many usual backslashed escape
5171 sequences, as found in the C language, are recognized and converted to
5172 the corresponding characters by @command{ptx} itself.
5173
5174 @item -M @var{string}
5175 @itemx --macro-name=@var{string}
5176
5177 Select another @var{string} to be used instead of @samp{xx}, while
5178 generating output suitable for @command{nroff}, @command{troff} or @TeX{}.
5179
5180 @item -O
5181 @itemx --format=roff
5182
5183 Choose an output format suitable for @command{nroff} or @command{troff}
5184 processing.  Each output line will look like:
5185
5186 @smallexample
5187 .xx "@var{tail}" "@var{before}" "@var{keyword_and_after}"@c
5188  "@var{head}" "@var{ref}"
5189 @end smallexample
5190
5191 so it will be possible to write a @samp{.xx} roff macro to take care of
5192 the output typesetting.  This is the default output format when @sc{gnu}
5193 extensions are disabled.  Option @option{-M} can be used to change
5194 @samp{xx} to another macro name.
5195
5196 In this output format, each non-graphical character, like newline and
5197 tab, is merely changed to exactly one space, with no special attempt to
5198 compress consecutive spaces.  Each quote character: @kbd{"} is doubled
5199 so it will be correctly processed by @command{nroff} or @command{troff}.
5200
5201 @item -T
5202 @itemx --format=tex
5203
5204 Choose an output format suitable for @TeX{} processing.  Each output
5205 line will look like:
5206
5207 @smallexample
5208 \xx @{@var{tail}@}@{@var{before}@}@{@var{keyword}@}@c
5209 @{@var{after}@}@{@var{head}@}@{@var{ref}@}
5210 @end smallexample
5211
5212 @noindent
5213 so it will be possible to write a @code{\xx} definition to take care of
5214 the output typesetting.  Note that when references are not being
5215 produced, that is, neither option @option{-A} nor option @option{-r} is
5216 selected, the last parameter of each @code{\xx} call is inhibited.
5217 Option @option{-M} can be used to change @samp{xx} to another macro
5218 name.
5219
5220 In this output format, some special characters, like @kbd{$}, @kbd{%},
5221 @kbd{&}, @kbd{#} and @kbd{_} are automatically protected with a
5222 backslash.  Curly brackets @kbd{@{}, @kbd{@}} are protected with a
5223 backslash and a pair of dollar signs (to force mathematical mode).  The
5224 backslash itself produces the sequence @code{\backslash@{@}}.
5225 Circumflex and tilde diacritical marks produce the sequence @code{^\@{ @}} and
5226 @code{~\@{ @}} respectively.  Other diacriticized characters of the
5227 underlying character set produce an appropriate @TeX{} sequence as far
5228 as possible.  The other non-graphical characters, like newline and tab,
5229 and all other characters which are not part of @acronym{ASCII}, are merely
5230 changed to exactly one space, with no special attempt to compress
5231 consecutive spaces.  Let me know how to improve this special character
5232 processing for @TeX{}.
5233
5234 @end table
5235
5236
5237 @node Compatibility in ptx
5238 @subsection The @sc{gnu} extensions to @command{ptx}
5239
5240 This version of @command{ptx} contains a few features which do not exist in
5241 System V @command{ptx}.  These extra features are suppressed by using the
5242 @option{-G} command line option, unless overridden by other command line
5243 options.  Some @sc{gnu} extensions cannot be recovered by overriding, so the
5244 simple rule is to avoid @option{-G} if you care about @sc{gnu} extensions.
5245 Here are the differences between this program and System V @command{ptx}.
5246
5247 @itemize @bullet
5248
5249 @item
5250 This program can read many input files at once, it always writes the
5251 resulting concordance on standard output.  On the other hand, System V
5252 @command{ptx} reads only one file and sends the result to standard output
5253 or, if a second @var{file} parameter is given on the command, to that
5254 @var{file}.
5255
5256 Having output parameters not introduced by options is a dangerous
5257 practice which @sc{gnu} avoids as far as possible.  So, for using @command{ptx}
5258 portably between @sc{gnu} and System V, you should always use it with a
5259 single input file, and always expect the result on standard output.  You
5260 might also want to automatically configure in a @option{-G} option to
5261 @command{ptx} calls in products using @command{ptx}, if the configurator finds
5262 that the installed @command{ptx} accepts @option{-G}.
5263
5264 @item
5265 The only options available in System V @command{ptx} are options @option{-b},
5266 @option{-f}, @option{-g}, @option{-i}, @option{-o}, @option{-r}, @option{-t} and
5267 @option{-w}.  All other options are @sc{gnu} extensions and are not repeated in
5268 this enumeration.  Moreover, some options have a slightly different
5269 meaning when @sc{gnu} extensions are enabled, as explained below.
5270
5271 @item
5272 By default, concordance output is not formatted for @command{troff} or
5273 @command{nroff}.  It is rather formatted for a dumb terminal.  @command{troff}
5274 or @command{nroff} output may still be selected through option @option{-O}.
5275
5276 @item
5277 Unless @option{-R} option is used, the maximum reference width is
5278 subtracted from the total output line width.  With @sc{gnu} extensions
5279 disabled, width of references is not taken into account in the output
5280 line width computations.
5281
5282 @item
5283 All 256 bytes, even @acronym{ASCII} @sc{nul} bytes, are always read and
5284 processed from input file with no adverse effect, even if @sc{gnu} extensions
5285 are disabled. However, System V @command{ptx} does not accept 8-bit characters,
5286 a few control characters are rejected, and the tilde @kbd{~} is also rejected.
5287
5288 @item
5289 Input line length is only limited by available memory, even if @sc{gnu}
5290 extensions are disabled.  However, System V @command{ptx} processes only
5291 the first 200 characters in each line.
5292
5293 @item
5294 The break (non-word) characters default to be every character except all
5295 letters of the underlying character set, diacriticized or not.  When @sc{gnu}
5296 extensions are disabled, the break characters default to space, tab and
5297 newline only.
5298
5299 @item
5300 The program makes better use of output line width.  If @sc{gnu} extensions
5301 are disabled, the program rather tries to imitate System V @command{ptx},
5302 but still, there are some slight disposition glitches this program does
5303 not completely reproduce.
5304
5305 @item
5306 The user can specify both an Ignore file and an Only file.  This is not
5307 allowed with System V @command{ptx}.
5308
5309 @end itemize
5310
5311
5312 @node tsort invocation
5313 @section @command{tsort}: Topological sort
5314
5315 @pindex tsort
5316 @cindex topological sort
5317
5318 @command{tsort} performs a topological sort on the given @var{file}, or
5319 standard input if no input file is given or for a @var{file} of
5320 @samp{-}.  For more details and some history, see @ref{tsort background}.
5321 Synopsis:
5322
5323 @example
5324 tsort [@var{option}] [@var{file}]
5325 @end example
5326
5327 @command{tsort} reads its input as pairs of strings, separated by blanks,
5328 indicating a partial ordering.  The output is a total ordering that
5329 corresponds to the given partial ordering.
5330
5331 For example
5332
5333 @example
5334 tsort <<EOF
5335 a b c
5336 d
5337 e f
5338 b c d e
5339 EOF
5340 @end example
5341
5342 @noindent
5343 will produce the output
5344
5345 @example
5346 a
5347 b
5348 c
5349 d
5350 e
5351 f
5352 @end example
5353
5354 Consider a more realistic example.
5355 You have a large set of functions all in one file, and they may all be
5356 declared static except one.  Currently that one (say @code{main}) is the
5357 first function defined in the file, and the ones it calls directly follow
5358 it, followed by those they call, etc.  Let's say that you are determined
5359 to take advantage of prototypes, so you have to choose between declaring
5360 all of those functions (which means duplicating a lot of information from
5361 the definitions) and rearranging the functions so that as many as possible
5362 are defined before they are used.  One way to automate the latter process
5363 is to get a list for each function of the functions it calls directly.
5364 Many programs can generate such lists.  They describe a call graph.
5365 Consider the following list, in which a given line indicates that the
5366 function on the left calls the one on the right directly.
5367
5368 @example
5369 main parse_options
5370 main tail_file
5371 main tail_forever
5372 tail_file pretty_name
5373 tail_file write_header
5374 tail_file tail
5375 tail_forever recheck
5376 tail_forever pretty_name
5377 tail_forever write_header
5378 tail_forever dump_remainder
5379 tail tail_lines
5380 tail tail_bytes
5381 tail_lines start_lines
5382 tail_lines dump_remainder
5383 tail_lines file_lines
5384 tail_lines pipe_lines
5385 tail_bytes xlseek
5386 tail_bytes start_bytes
5387 tail_bytes dump_remainder
5388 tail_bytes pipe_bytes
5389 file_lines dump_remainder
5390 recheck pretty_name
5391 @end example
5392
5393 then you can use @command{tsort} to produce an ordering of those
5394 functions that satisfies your requirement.
5395
5396 @example
5397 example$ tsort call-graph | tac
5398 dump_remainder
5399 start_lines
5400 file_lines
5401 pipe_lines
5402 xlseek
5403 start_bytes
5404 pipe_bytes
5405 tail_lines
5406 tail_bytes
5407 pretty_name
5408 write_header
5409 tail
5410 recheck
5411 parse_options
5412 tail_file
5413 tail_forever
5414 main
5415 @end example
5416
5417 @command{tsort} detects any cycles in the input and writes the first cycle
5418 encountered to standard error.
5419
5420 Note that for a given partial ordering, generally there is no unique
5421 total ordering.  In the context of the call graph above, the function
5422 @code{parse_options} may be placed anywhere in the list as long as it
5423 precedes @code{main}.
5424
5425 The only options are @option{--help} and @option{--version}.  @xref{Common
5426 options}.
5427
5428 @exitstatus
5429
5430 @menu
5431 * tsort background::            Where tsort came from.
5432 @end menu
5433
5434 @node tsort background
5435 @subsection @command{tsort}: Background
5436
5437 @command{tsort} exists because very early versions of the Unix linker processed
5438 an archive file exactly once, and in order.  As @command{ld} read each object
5439 in the archive, it decided whether it was needed in the program based on
5440 whether it defined any symbols which were undefined at that point in
5441 the link.
5442
5443 This meant that dependencies within the archive had to be handled
5444 specially.  For example, @code{scanf} probably calls @code{read}.  That means
5445 that in a single pass through an archive, it was important for @code{scanf.o}
5446 to appear before read.o, because otherwise a program which calls
5447 @code{scanf} but not @code{read} might end up with an unexpected unresolved
5448 reference to @code{read}.
5449
5450 The way to address this problem was to first generate a set of
5451 dependencies of one object file on another.  This was done by a shell
5452 script called @command{lorder}.  The GNU tools don't provide a version of
5453 lorder, as far as I know, but you can still find it in BSD
5454 distributions.
5455
5456 Then you ran @command{tsort} over the @command{lorder} output, and you used the
5457 resulting sort to define the order in which you added objects to the archive.
5458
5459 This whole procedure has been obsolete since about 1980, because
5460 Unix archives now contain a symbol table (traditionally built by
5461 @command{ranlib}, now generally built by @command{ar} itself), and the Unix
5462 linker uses the symbol table to effectively make multiple passes over
5463 an archive file.
5464
5465 Anyhow, that's where tsort came from.  To solve an old problem with
5466 the way the linker handled archive files, which has since been solved
5467 in different ways.
5468
5469
5470 @node Operating on fields
5471 @chapter Operating on fields
5472
5473 @menu
5474 * cut invocation::              Print selected parts of lines.
5475 * paste invocation::            Merge lines of files.
5476 * join invocation::             Join lines on a common field.
5477 @end menu
5478
5479
5480 @node cut invocation
5481 @section @command{cut}: Print selected parts of lines
5482
5483 @pindex cut
5484 @command{cut} writes to standard output selected parts of each line of each
5485 input file, or standard input if no files are given or for a file name of
5486 @samp{-}.  Synopsis:
5487
5488 @example
5489 cut @var{option}@dots{} [@var{file}]@dots{}
5490 @end example
5491
5492 In the table which follows, the @var{byte-list}, @var{character-list},
5493 and @var{field-list} are one or more numbers or ranges (two numbers
5494 separated by a dash) separated by commas.  Bytes, characters, and
5495 fields are numbered starting at 1.  Incomplete ranges may be
5496 given: @option{-@var{m}} means @samp{1-@var{m}}; @samp{@var{n}-} means
5497 @samp{@var{n}} through end of line or last field.  The list elements
5498 can be repeated, can overlap, and can be specified in any order; but
5499 the selected input is written in the same order that it is read, and
5500 is written exactly once.
5501
5502 The program accepts the following options.  Also see @ref{Common
5503 options}.
5504
5505 @table @samp
5506
5507 @item -b @var{byte-list}
5508 @itemx --bytes=@var{byte-list}
5509 @opindex -b
5510 @opindex --bytes
5511 Select for printing only the bytes in positions listed in
5512 @var{byte-list}.  Tabs and backspaces are treated like any other
5513 character; they take up 1 byte.  If an output delimiter is specified,
5514 (see the description of @option{--output-delimiter}), then output that
5515 string between ranges of selected bytes.
5516
5517 @item -c @var{character-list}
5518 @itemx --characters=@var{character-list}
5519 @opindex -c
5520 @opindex --characters
5521 Select for printing only the characters in positions listed in
5522 @var{character-list}.  The same as @option{-b} for now, but
5523 internationalization will change that.  Tabs and backspaces are
5524 treated like any other character; they take up 1 character.  If an
5525 output delimiter is specified, (see the description of
5526 @option{--output-delimiter}), then output that string between ranges
5527 of selected bytes.
5528
5529 @item -f @var{field-list}
5530 @itemx --fields=@var{field-list}
5531 @opindex -f
5532 @opindex --fields
5533 Select for printing only the fields listed in @var{field-list}.
5534 Fields are separated by a TAB character by default.  Also print any
5535 line that contains no delimiter character, unless the
5536 @option{--only-delimited} (@option{-s}) option is specified.
5537
5538 Note @command{awk} supports more sophisticated field processing,
5539 and by default will use (and discard) runs of blank characters to
5540 separate fields, and ignore leading and trailing blanks.
5541 @example
5542 @verbatim
5543 awk '{print $2}'    # print the second field
5544 awk '{print $NF-1}' # print the penultimate field
5545 awk '{print $2,$1}' # reorder the first two fields
5546 @end verbatim
5547 @end example
5548
5549 In the unlikely event that @command{awk} is unavailable,
5550 one can use the @command{join} command, to process blank
5551 characters as @command{awk} does above.
5552 @example
5553 @verbatim
5554 join -a1 -o 1.2     - /dev/null # print the second field
5555 join -a1 -o 1.2,1.1 - /dev/null # reorder the first two fields
5556 @end verbatim
5557 @end example
5558
5559 @item -d @var{input_delim_byte}
5560 @itemx --delimiter=@var{input_delim_byte}
5561 @opindex -d
5562 @opindex --delimiter
5563 With @option{-f}, use the first byte of @var{input_delim_byte} as
5564 the input fields separator (default is TAB).
5565
5566 @item -n
5567 @opindex -n
5568 Do not split multi-byte characters (no-op for now).
5569
5570 @item -s
5571 @itemx --only-delimited
5572 @opindex -s
5573 @opindex --only-delimited
5574 For @option{-f}, do not print lines that do not contain the field separator
5575 character.  Normally, any line without a field separator is printed verbatim.
5576
5577 @item --output-delimiter=@var{output_delim_string}
5578 @opindex --output-delimiter
5579 With @option{-f}, output fields are separated by @var{output_delim_string}.
5580 The default with @option{-f} is to use the input delimiter.
5581 When using @option{-b} or @option{-c} to select ranges of byte or
5582 character offsets (as opposed to ranges of fields),
5583 output @var{output_delim_string} between non-overlapping
5584 ranges of selected bytes.
5585
5586 @item --complement
5587 @opindex --complement
5588 This option is a @acronym{GNU} extension.
5589 Select for printing the complement of the bytes, characters or fields
5590 selected with the @option{-b}, @option{-c} or @option{-f} options.
5591 In other words, do @emph{not} print the bytes, characters or fields
5592 specified via those options.  This option is useful when you have
5593 many fields and want to print all but a few of them.
5594
5595 @end table
5596
5597 @exitstatus
5598
5599
5600 @node paste invocation
5601 @section @command{paste}: Merge lines of files
5602
5603 @pindex paste
5604 @cindex merging files
5605
5606 @command{paste} writes to standard output lines consisting of sequentially
5607 corresponding lines of each given file, separated by a TAB character.
5608 Standard input is used for a file name of @samp{-} or if no input files
5609 are given.
5610
5611 For example:
5612
5613 @example
5614 $ cat num2
5615 1
5616 2
5617 $ cat let3
5618 a
5619 b
5620 c
5621 $ paste num2 let3
5622 1       a
5623 2       b
5624        @ c
5625 @end example
5626
5627 Synopsis:
5628
5629 @example
5630 paste [@var{option}]@dots{} [@var{file}]@dots{}
5631 @end example
5632
5633 The program accepts the following options.  Also see @ref{Common options}.
5634
5635 @table @samp
5636
5637 @item -s
5638 @itemx --serial
5639 @opindex -s
5640 @opindex --serial
5641 Paste the lines of one file at a time rather than one line from each
5642 file.  Using the above example data:
5643
5644 @example
5645 $ paste -s num2 let3
5646 1       2
5647 a       b       c
5648 @end example
5649
5650 @item -d @var{delim-list}
5651 @itemx --delimiters=@var{delim-list}
5652 @opindex -d
5653 @opindex --delimiters
5654 Consecutively use the characters in @var{delim-list} instead of
5655 TAB to separate merged lines.  When @var{delim-list} is
5656 exhausted, start again at its beginning.  Using the above example data:
5657
5658 @example
5659 $ paste -d '%_' num2 let3 num2
5660 1%a_1
5661 2%b_2
5662 %c_
5663 @end example
5664
5665 @end table
5666
5667 @exitstatus
5668
5669
5670 @node join invocation
5671 @section @command{join}: Join lines on a common field
5672
5673 @pindex join
5674 @cindex common field, joining on
5675
5676 @command{join} writes to standard output a line for each pair of input
5677 lines that have identical join fields.  Synopsis:
5678
5679 @example
5680 join [@var{option}]@dots{} @var{file1} @var{file2}
5681 @end example
5682
5683 Either @var{file1} or @var{file2} (but not both) can be @samp{-},
5684 meaning standard input.  @var{file1} and @var{file2} should be
5685 sorted on the join fields.
5686
5687 @vindex LC_COLLATE
5688 Normally, the sort order is that of the
5689 collating sequence specified by the @env{LC_COLLATE} locale.  Unless
5690 the @option{-t} option is given, the sort comparison ignores blanks at
5691 the start of the join field, as in @code{sort -b}.  If the
5692 @option{--ignore-case} option is given, the sort comparison ignores
5693 the case of characters in the join field, as in @code{sort -f}.
5694
5695 The @command{sort} and @command{join} commands should use consistent
5696 locales and options if the output of @command{sort} is fed to
5697 @command{join}.  You can use a command like @samp{sort -k 1b,1} to
5698 sort a file on its default join field, but if you select a non-default
5699 locale, join field, separator, or comparison options, then you should
5700 do so consistently between @command{join} and @command{sort}.
5701 If @samp{join -t ''} is specified then the whole line is considered which
5702 matches the default operation of sort.
5703
5704 If the input has no unpairable lines, a @acronym{GNU} extension is
5705 available; the sort order can be any order that considers two fields
5706 to be equal if and only if the sort comparison described above
5707 considers them to be equal.  For example:
5708
5709 @example
5710 $ cat file1
5711 a a1
5712 c c1
5713 b b1
5714 $ cat file2
5715 a a2
5716 c c2
5717 b b2
5718 $ join file1 file2
5719 a a1 a2
5720 c c1 c2
5721 b b1 b2
5722 @end example
5723
5724 @set JOIN_COMMAND
5725 @checkOrderOption{join}
5726 @clear JOIN_COMMAND
5727
5728 The defaults are:
5729 @itemize
5730 @item the join field is the first field in each line;
5731 @item fields in the input are separated by one or more blanks, with leading
5732 blanks on the line ignored;
5733 @item fields in the output are separated by a space;
5734 @item each output line consists of the join field, the remaining
5735 fields from @var{file1}, then the remaining fields from @var{file2}.
5736 @end itemize
5737
5738 The program accepts the following options.  Also see @ref{Common options}.
5739
5740 @table @samp
5741
5742 @item -a @var{file-number}
5743 @opindex -a
5744 Print a line for each unpairable line in file @var{file-number} (either
5745 @samp{1} or @samp{2}), in addition to the normal output.
5746
5747 @item --check-order
5748 Fail with an error message if either input file is wrongly ordered.
5749
5750 @item --nocheck-order
5751 Do not check that both input files are in sorted order.  This is the default.
5752
5753 @item -e @var{string}
5754 @opindex -e
5755 Replace those output fields that are missing in the input with @var{string}.
5756 I.E. missing fields specified with the @option{-12jo} options.
5757
5758 @item --header
5759 @opindex --header
5760 Treat the first line of each input file as a header line. The header lines will
5761 be joined and printed as the first output line.  If @option{-o} is used to
5762 specify output format, the header line will be printed according to the
5763 specified format.  The header lines will not be checked for ordering even if
5764 @option{--check-order} is specified.  Also if the header lines from each file
5765 do not match, the heading fields from the first file will be used.
5766
5767 @item -i
5768 @itemx --ignore-case
5769 @opindex -i
5770 @opindex --ignore-case
5771 Ignore differences in case when comparing keys.
5772 With this option, the lines of the input files must be ordered in the same way.
5773 Use @samp{sort -f} to produce this ordering.
5774
5775 @item -1 @var{field}
5776 @opindex -1
5777 Join on field @var{field} (a positive integer) of file 1.
5778
5779 @item -2 @var{field}
5780 @opindex -2
5781 Join on field @var{field} (a positive integer) of file 2.
5782
5783 @item -j @var{field}
5784 Equivalent to @option{-1 @var{field} -2 @var{field}}.
5785
5786 @item -o @var{field-list}
5787 @itemx -o auto
5788 If the keyword @samp{auto} is specified, infer the output format from
5789 the first line in each file.  This is the same as the default output format
5790 but also ensures the same number of fields are output for each line.
5791 Missing fields are replaced with the @option{-e} option and extra fields
5792 are discarded.
5793
5794 Otherwise, construct each output line according to the format in
5795 @var{field-list}.  Each element in @var{field-list} is either the single
5796 character @samp{0} or has the form @var{m.n} where the file number, @var{m},
5797 is @samp{1} or @samp{2} and @var{n} is a positive field number.
5798
5799 A field specification of @samp{0} denotes the join field.
5800 In most cases, the functionality of the @samp{0} field spec
5801 may be reproduced using the explicit @var{m.n} that corresponds
5802 to the join field.  However, when printing unpairable lines
5803 (using either of the @option{-a} or @option{-v} options), there is no way
5804 to specify the join field using @var{m.n} in @var{field-list}
5805 if there are unpairable lines in both files.
5806 To give @command{join} that functionality, @acronym{POSIX} invented the @samp{0}
5807 field specification notation.
5808
5809 The elements in @var{field-list}
5810 are separated by commas or blanks.
5811 Blank separators typically need to be quoted for the shell.  For
5812 example, the commands @samp{join -o 1.2,2.2} and @samp{join -o '1.2
5813 2.2'} are equivalent.
5814
5815 All output lines---including those printed because of any -a or -v
5816 option---are subject to the specified @var{field-list}.
5817
5818 @item -t @var{char}
5819 Use character @var{char} as the input and output field separator.
5820 Treat as significant each occurrence of @var{char} in the input file.
5821 Use @samp{sort -t @var{char}}, without the @option{-b} option of
5822 @samp{sort}, to produce this ordering.  If @samp{join -t ''} is specified,
5823 the whole line is considered, matching the default operation of sort.
5824 If @samp{-t '\0'} is specified then the @acronym{ASCII} @sc{nul}
5825 character is used to delimit the fields.
5826
5827 @item -v @var{file-number}
5828 Print a line for each unpairable line in file @var{file-number}
5829 (either @samp{1} or @samp{2}), instead of the normal output.
5830
5831 @end table
5832
5833 @exitstatus
5834
5835
5836 @node Operating on characters
5837 @chapter Operating on characters
5838
5839 @cindex operating on characters
5840
5841 This commands operate on individual characters.
5842
5843 @menu
5844 * tr invocation::               Translate, squeeze, and/or delete characters.
5845 * expand invocation::           Convert tabs to spaces.
5846 * unexpand invocation::         Convert spaces to tabs.
5847 @end menu
5848
5849
5850 @node tr invocation
5851 @section @command{tr}: Translate, squeeze, and/or delete characters
5852
5853 @pindex tr
5854
5855 Synopsis:
5856
5857 @example
5858 tr [@var{option}]@dots{} @var{set1} [@var{set2}]
5859 @end example
5860
5861 @command{tr} copies standard input to standard output, performing
5862 one of the following operations:
5863
5864 @itemize @bullet
5865 @item
5866 translate, and optionally squeeze repeated characters in the result,
5867 @item
5868 squeeze repeated characters,
5869 @item
5870 delete characters,
5871 @item
5872 delete characters, then squeeze repeated characters from the result.
5873 @end itemize
5874
5875 The @var{set1} and (if given) @var{set2} arguments define ordered
5876 sets of characters, referred to below as @var{set1} and @var{set2}.  These
5877 sets are the characters of the input that @command{tr} operates on.
5878 The @option{--complement} (@option{-c}, @option{-C}) option replaces
5879 @var{set1} with its
5880 complement (all of the characters that are not in @var{set1}).
5881
5882 Currently @command{tr} fully supports only single-byte characters.
5883 Eventually it will support multibyte characters; when it does, the
5884 @option{-C} option will cause it to complement the set of characters,
5885 whereas @option{-c} will cause it to complement the set of values.
5886 This distinction will matter only when some values are not characters,
5887 and this is possible only in locales using multibyte encodings when
5888 the input contains encoding errors.
5889
5890 The program accepts the @option{--help} and @option{--version}
5891 options.  @xref{Common options}.  Options must precede operands.
5892
5893 @exitstatus
5894
5895 @menu
5896 * Character sets::              Specifying sets of characters.
5897 * Translating::                 Changing one set of characters to another.
5898 * Squeezing::                   Squeezing repeats and deleting.
5899 @end menu
5900
5901
5902 @node Character sets
5903 @subsection Specifying sets of characters
5904
5905 @cindex specifying sets of characters
5906
5907 The format of the @var{set1} and @var{set2} arguments resembles
5908 the format of regular expressions; however, they are not regular
5909 expressions, only lists of characters.  Most characters simply
5910 represent themselves in these strings, but the strings can contain
5911 the shorthands listed below, for convenience.  Some of them can be
5912 used only in @var{set1} or @var{set2}, as noted below.
5913
5914 @table @asis
5915
5916 @item Backslash escapes
5917 @cindex backslash escapes
5918
5919 The following backslash escape sequences are recognized:
5920
5921 @table @samp
5922 @item \a
5923 Control-G.
5924 @item \b
5925 Control-H.
5926 @item \f
5927 Control-L.
5928 @item \n
5929 Control-J.
5930 @item \r
5931 Control-M.
5932 @item \t
5933 Control-I.
5934 @item \v
5935 Control-K.
5936 @item \@var{ooo}
5937 The 8-bit character with the value given by @var{ooo}, which is 1 to 3
5938 octal digits.  Note that @samp{\400} is interpreted as the two-byte
5939 sequence, @samp{\040} @samp{0}.
5940 @item \\
5941 A backslash.
5942 @end table
5943
5944 While a backslash followed by a character not listed above is
5945 interpreted as that character, the backslash also effectively
5946 removes any special significance, so it is useful to escape
5947 @samp{[}, @samp{]}, @samp{*}, and @samp{-}.
5948
5949 @item Ranges
5950 @cindex ranges
5951
5952 The notation @samp{@var{m}-@var{n}} expands to all of the characters
5953 from @var{m} through @var{n}, in ascending order.  @var{m} should
5954 collate before @var{n}; if it doesn't, an error results.  As an example,
5955 @samp{0-9} is the same as @samp{0123456789}.
5956
5957 @sc{gnu} @command{tr} does not support the System V syntax that uses square
5958 brackets to enclose ranges.  Translations specified in that format
5959 sometimes work as expected, since the brackets are often transliterated
5960 to themselves.  However, they should be avoided because they sometimes
5961 behave unexpectedly.  For example, @samp{tr -d '[0-9]'} deletes brackets
5962 as well as digits.
5963
5964 Many historically common and even accepted uses of ranges are not
5965 portable.  For example, on @acronym{EBCDIC} hosts using the @samp{A-Z}
5966 range will not do what most would expect because @samp{A} through @samp{Z}
5967 are not contiguous as they are in @acronym{ASCII}.
5968 If you can rely on a @acronym{POSIX} compliant version of @command{tr}, then
5969 the best way to work around this is to use character classes (see below).
5970 Otherwise, it is most portable (and most ugly) to enumerate the members
5971 of the ranges.
5972
5973 @item Repeated characters
5974 @cindex repeated characters
5975
5976 The notation @samp{[@var{c}*@var{n}]} in @var{set2} expands to @var{n}
5977 copies of character @var{c}.  Thus, @samp{[y*6]} is the same as
5978 @samp{yyyyyy}.  The notation @samp{[@var{c}*]} in @var{string2} expands
5979 to as many copies of @var{c} as are needed to make @var{set2} as long as
5980 @var{set1}.  If @var{n} begins with @samp{0}, it is interpreted in
5981 octal, otherwise in decimal.
5982
5983 @item Character classes
5984 @cindex character classes
5985
5986 The notation @samp{[:@var{class}:]} expands to all of the characters in
5987 the (predefined) class @var{class}.  The characters expand in no
5988 particular order, except for the @code{upper} and @code{lower} classes,
5989 which expand in ascending order.  When the @option{--delete} (@option{-d})
5990 and @option{--squeeze-repeats} (@option{-s}) options are both given, any
5991 character class can be used in @var{set2}.  Otherwise, only the
5992 character classes @code{lower} and @code{upper} are accepted in
5993 @var{set2}, and then only if the corresponding character class
5994 (@code{upper} and @code{lower}, respectively) is specified in the same
5995 relative position in @var{set1}.  Doing this specifies case conversion.
5996 The class names are given below; an error results when an invalid class
5997 name is given.
5998
5999 @table @code
6000 @item alnum
6001 @opindex alnum
6002 Letters and digits.
6003 @item alpha
6004 @opindex alpha
6005 Letters.
6006 @item blank
6007 @opindex blank
6008 Horizontal whitespace.
6009 @item cntrl
6010 @opindex cntrl
6011 Control characters.
6012 @item digit
6013 @opindex digit
6014 Digits.
6015 @item graph
6016 @opindex graph
6017 Printable characters, not including space.
6018 @item lower
6019 @opindex lower
6020 Lowercase letters.
6021 @item print
6022 @opindex print
6023 Printable characters, including space.
6024 @item punct
6025 @opindex punct
6026 Punctuation characters.
6027 @item space
6028 @opindex space
6029 Horizontal or vertical whitespace.
6030 @item upper
6031 @opindex upper
6032 Uppercase letters.
6033 @item xdigit
6034 @opindex xdigit
6035 Hexadecimal digits.
6036 @end table
6037
6038 @item Equivalence classes
6039 @cindex equivalence classes
6040
6041 The syntax @samp{[=@var{c}=]} expands to all of the characters that are
6042 equivalent to @var{c}, in no particular order.  Equivalence classes are
6043 a relatively recent invention intended to support non-English alphabets.
6044 But there seems to be no standard way to define them or determine their
6045 contents.  Therefore, they are not fully implemented in @sc{gnu} @command{tr};
6046 each character's equivalence class consists only of that character,
6047 which is of no particular use.
6048
6049 @end table
6050
6051
6052 @node Translating
6053 @subsection Translating
6054
6055 @cindex translating characters
6056
6057 @command{tr} performs translation when @var{set1} and @var{set2} are
6058 both given and the @option{--delete} (@option{-d}) option is not given.
6059 @command{tr} translates each character of its input that is in @var{set1}
6060 to the corresponding character in @var{set2}.  Characters not in
6061 @var{set1} are passed through unchanged.  When a character appears more
6062 than once in @var{set1} and the corresponding characters in @var{set2}
6063 are not all the same, only the final one is used.  For example, these
6064 two commands are equivalent:
6065
6066 @example
6067 tr aaa xyz
6068 tr a z
6069 @end example
6070
6071 A common use of @command{tr} is to convert lowercase characters to
6072 uppercase.  This can be done in many ways.  Here are three of them:
6073
6074 @example
6075 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
6076 tr a-z A-Z
6077 tr '[:lower:]' '[:upper:]'
6078 @end example
6079
6080 @noindent
6081 But note that using ranges like @code{a-z} above is not portable.
6082
6083 When @command{tr} is performing translation, @var{set1} and @var{set2}
6084 typically have the same length.  If @var{set1} is shorter than
6085 @var{set2}, the extra characters at the end of @var{set2} are ignored.
6086
6087 On the other hand, making @var{set1} longer than @var{set2} is not
6088 portable; @acronym{POSIX} says that the result is undefined.  In this situation,
6089 BSD @command{tr} pads @var{set2} to the length of @var{set1} by repeating
6090 the last character of @var{set2} as many times as necessary.  System V
6091 @command{tr} truncates @var{set1} to the length of @var{set2}.
6092
6093 By default, @sc{gnu} @command{tr} handles this case like BSD @command{tr}.
6094 When the @option{--truncate-set1} (@option{-t}) option is given,
6095 @sc{gnu} @command{tr} handles this case like the System V @command{tr}
6096 instead.  This option is ignored for operations other than translation.
6097
6098 Acting like System V @command{tr} in this case breaks the relatively common
6099 BSD idiom:
6100
6101 @example
6102 tr -cs A-Za-z0-9 '\012'
6103 @end example
6104
6105 @noindent
6106 because it converts only zero bytes (the first element in the
6107 complement of @var{set1}), rather than all non-alphanumerics, to
6108 newlines.
6109
6110 @noindent
6111 By the way, the above idiom is not portable because it uses ranges, and
6112 it assumes that the octal code for newline is 012.
6113 Assuming a @acronym{POSIX} compliant @command{tr}, here is a better
6114 way to write it:
6115
6116 @example
6117 tr -cs '[:alnum:]' '[\n*]'
6118 @end example
6119
6120
6121 @node Squeezing
6122 @subsection Squeezing repeats and deleting
6123
6124 @cindex squeezing repeat characters
6125 @cindex deleting characters
6126
6127 When given just the @option{--delete} (@option{-d}) option, @command{tr}
6128 removes any input characters that are in @var{set1}.
6129
6130 When given just the @option{--squeeze-repeats} (@option{-s}) option,
6131 @command{tr} replaces each input sequence of a repeated character that
6132 is in @var{set1} with a single occurrence of that character.
6133
6134 When given both @option{--delete} and @option{--squeeze-repeats}, @command{tr}
6135 first performs any deletions using @var{set1}, then squeezes repeats
6136 from any remaining characters using @var{set2}.
6137
6138 The @option{--squeeze-repeats} option may also be used when translating,
6139 in which case @command{tr} first performs translation, then squeezes
6140 repeats from any remaining characters using @var{set2}.
6141
6142 Here are some examples to illustrate various combinations of options:
6143
6144 @itemize @bullet
6145
6146 @item
6147 Remove all zero bytes:
6148
6149 @example
6150 tr -d '\0'
6151 @end example
6152
6153 @item
6154 Put all words on lines by themselves.  This converts all
6155 non-alphanumeric characters to newlines, then squeezes each string
6156 of repeated newlines into a single newline:
6157
6158 @example
6159 tr -cs '[:alnum:]' '[\n*]'
6160 @end example
6161
6162 @item
6163 Convert each sequence of repeated newlines to a single newline:
6164
6165 @example
6166 tr -s '\n'
6167 @end example
6168
6169 @item
6170 Find doubled occurrences of words in a document.
6171 @c Separate the following two "the"s, so typo checkers don't complain.
6172 For example, people often write ``the @w{}the'' with the repeated words
6173 separated by a newline.  The Bourne shell script below works first
6174 by converting each sequence of punctuation and blank characters to a
6175 single newline.  That puts each ``word'' on a line by itself.
6176 Next it maps all uppercase characters to lower case, and finally it
6177 runs @command{uniq} with the @option{-d} option to print out only the words
6178 that were repeated.
6179
6180 @example
6181 #!/bin/sh
6182 cat -- "$@@" \
6183   | tr -s '[:punct:][:blank:]' '[\n*]' \
6184   | tr '[:upper:]' '[:lower:]' \
6185   | uniq -d
6186 @end example
6187
6188 @item
6189 Deleting a small set of characters is usually straightforward.  For example,
6190 to remove all @samp{a}s, @samp{x}s, and @samp{M}s you would do this:
6191
6192 @example
6193 tr -d axM
6194 @end example
6195
6196 However, when @samp{-} is one of those characters, it can be tricky because
6197 @samp{-} has special meanings.  Performing the same task as above but also
6198 removing all @samp{-} characters, we might try @code{tr -d -axM}, but
6199 that would fail because @command{tr} would try to interpret @option{-a} as
6200 a command-line option.  Alternatively, we could try putting the hyphen
6201 inside the string, @code{tr -d a-xM}, but that wouldn't work either because
6202 it would make @command{tr} interpret @code{a-x} as the range of characters
6203 @samp{a}@dots{}@samp{x} rather than the three.
6204 One way to solve the problem is to put the hyphen at the end of the list
6205 of characters:
6206
6207 @example
6208 tr -d axM-
6209 @end example
6210
6211 Or you can use @samp{--} to terminate option processing:
6212
6213 @example
6214 tr -d -- -axM
6215 @end example
6216
6217 More generally, use the character class notation @code{[=c=]}
6218 with @samp{-} (or any other character) in place of the @samp{c}:
6219
6220 @example
6221 tr -d '[=-=]axM'
6222 @end example
6223
6224 Note how single quotes are used in the above example to protect the
6225 square brackets from interpretation by a shell.
6226
6227 @end itemize
6228
6229
6230 @node expand invocation
6231 @section @command{expand}: Convert tabs to spaces
6232
6233 @pindex expand
6234 @cindex tabs to spaces, converting
6235 @cindex converting tabs to spaces
6236
6237 @command{expand} writes the contents of each given @var{file}, or standard
6238 input if none are given or for a @var{file} of @samp{-}, to standard
6239 output, with tab characters converted to the appropriate number of
6240 spaces.  Synopsis:
6241
6242 @example
6243 expand [@var{option}]@dots{} [@var{file}]@dots{}
6244 @end example
6245
6246 By default, @command{expand} converts all tabs to spaces.  It preserves
6247 backspace characters in the output; they decrement the column count for
6248 tab calculations.  The default action is equivalent to @option{-t 8} (set
6249 tabs every 8 columns).
6250
6251 The program accepts the following options.  Also see @ref{Common options}.
6252
6253 @table @samp
6254
6255 @item -t @var{tab1}[,@var{tab2}]@dots{}
6256 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
6257 @opindex -t
6258 @opindex --tabs
6259 @cindex tab stops, setting
6260 If only one tab stop is given, set the tabs @var{tab1} spaces apart
6261 (default is 8).  Otherwise, set the tabs at columns @var{tab1},
6262 @var{tab2}, @dots{} (numbered from 0), and replace any tabs beyond the
6263 last tab stop given with single spaces.  Tab stops can be separated by
6264 blanks as well as by commas.
6265
6266 For compatibility, GNU @command{expand} also accepts the obsolete
6267 option syntax, @option{-@var{t1}[,@var{t2}]@dots{}}.  New scripts
6268 should use @option{-t @var{t1}[,@var{t2}]@dots{}} instead.
6269
6270 @item -i
6271 @itemx --initial
6272 @opindex -i
6273 @opindex --initial
6274 @cindex initial tabs, converting
6275 Only convert initial tabs (those that precede all non-space or non-tab
6276 characters) on each line to spaces.
6277
6278 @end table
6279
6280 @exitstatus
6281
6282
6283 @node unexpand invocation
6284 @section @command{unexpand}: Convert spaces to tabs
6285
6286 @pindex unexpand
6287
6288 @command{unexpand} writes the contents of each given @var{file}, or
6289 standard input if none are given or for a @var{file} of @samp{-}, to
6290 standard output, converting blanks at the beginning of each line into
6291 as many tab characters as needed.  In the default @acronym{POSIX}
6292 locale, a @dfn{blank} is a space or a tab; other locales may specify
6293 additional blank characters.  Synopsis:
6294
6295 @example
6296 unexpand [@var{option}]@dots{} [@var{file}]@dots{}
6297 @end example
6298
6299 By default, @command{unexpand} converts only initial blanks (those
6300 that precede all non-blank characters) on each line.  It
6301 preserves backspace characters in the output; they decrement the column
6302 count for tab calculations.  By default, tabs are set at every 8th
6303 column.
6304
6305 The program accepts the following options.  Also see @ref{Common options}.
6306
6307 @table @samp
6308
6309 @item -t @var{tab1}[,@var{tab2}]@dots{}
6310 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
6311 @opindex -t
6312 @opindex --tabs
6313 If only one tab stop is given, set the tabs @var{tab1} columns apart
6314 instead of the default 8.  Otherwise, set the tabs at columns
6315 @var{tab1}, @var{tab2}, @dots{} (numbered from 0), and leave blanks
6316 beyond the tab stops given unchanged.  Tab stops can be separated by
6317 blanks as well as by commas.  This option implies the @option{-a} option.
6318
6319 For compatibility, GNU @command{unexpand} supports the obsolete option syntax,
6320 @option{-@var{tab1}[,@var{tab2}]@dots{}}, where tab stops must be
6321 separated by commas.  (Unlike @option{-t}, this obsolete option does
6322 not imply @option{-a}.)  New scripts should use @option{--first-only -t
6323 @var{tab1}[,@var{tab2}]@dots{}} instead.
6324
6325 @item -a
6326 @itemx --all
6327 @opindex -a
6328 @opindex --all
6329 Also convert all sequences of two or more blanks just before a tab stop,
6330 even if they occur after non-blank characters in a line.
6331
6332 @end table
6333
6334 @exitstatus
6335
6336
6337 @node Directory listing
6338 @chapter Directory listing
6339
6340 This chapter describes the @command{ls} command and its variants @command{dir}
6341 and @command{vdir}, which list information about files.
6342
6343 @menu
6344 * ls invocation::               List directory contents.
6345 * dir invocation::              Briefly ls.
6346 * vdir invocation::             Verbosely ls.
6347 * dircolors invocation::        Color setup for ls, etc.
6348 @end menu
6349
6350
6351 @node ls invocation
6352 @section @command{ls}: List directory contents
6353
6354 @pindex ls
6355 @cindex directory listing
6356
6357 The @command{ls} program lists information about files (of any type,
6358 including directories).  Options and file arguments can be intermixed
6359 arbitrarily, as usual.
6360
6361 For non-option command-line arguments that are directories, by default
6362 @command{ls} lists the contents of directories, not recursively, and
6363 omitting files with names beginning with @samp{.}.  For other non-option
6364 arguments, by default @command{ls} lists just the file name.  If no
6365 non-option argument is specified, @command{ls} operates on the current
6366 directory, acting as if it had been invoked with a single argument of @samp{.}.
6367
6368 @vindex LC_ALL
6369 By default, the output is sorted alphabetically, according to the locale
6370 settings in effect.@footnote{If you use a non-@acronym{POSIX}
6371 locale (e.g., by setting @env{LC_ALL} to @samp{en_US}), then @command{ls} may
6372 produce output that is sorted differently than you're accustomed to.
6373 In that case, set the @env{LC_ALL} environment variable to @samp{C}.}
6374 If standard output is
6375 a terminal, the output is in columns (sorted vertically) and control
6376 characters are output as question marks; otherwise, the output is listed
6377 one per line and control characters are output as-is.
6378
6379 Because @command{ls} is such a fundamental program, it has accumulated many
6380 options over the years.  They are described in the subsections below;
6381 within each section, options are listed alphabetically (ignoring case).
6382 The division of options into the subsections is not absolute, since some
6383 options affect more than one aspect of @command{ls}'s operation.
6384
6385 @cindex exit status of @command{ls}
6386 Exit status:
6387
6388 @display
6389 0 success
6390 1 minor problems  (e.g., failure to access a file or directory not
6391   specified as a command line argument.  This happens when listing a
6392   directory in which entries are actively being removed or renamed.)
6393 2 serious trouble (e.g., memory exhausted, invalid option, failure
6394   to access a file or directory specified as a command line argument
6395   or a directory loop)
6396 @end display
6397
6398 Also see @ref{Common options}.
6399
6400 @menu
6401 * Which files are listed::
6402 * What information is listed::
6403 * Sorting the output::
6404 * Details about version sort::
6405 * General output formatting::
6406 * Formatting file timestamps::
6407 * Formatting the file names::
6408 @end menu
6409
6410
6411 @node Which files are listed
6412 @subsection Which files are listed
6413
6414 These options determine which files @command{ls} lists information for.
6415 By default, @command{ls} lists files and the contents of any
6416 directories on the command line, except that in directories it ignores
6417 files whose names start with @samp{.}.
6418
6419 @table @samp
6420
6421 @item -a
6422 @itemx --all
6423 @opindex -a
6424 @opindex --all
6425 In directories, do not ignore file names that start with @samp{.}.
6426
6427 @item -A
6428 @itemx --almost-all
6429 @opindex -A
6430 @opindex --almost-all
6431 In directories, do not ignore all file names that start with @samp{.};
6432 ignore only @file{.} and @file{..}.  The @option{--all} (@option{-a})
6433 option overrides this option.
6434
6435 @item -B
6436 @itemx --ignore-backups
6437 @opindex -B
6438 @opindex --ignore-backups
6439 @cindex backup files, ignoring
6440 In directories, ignore files that end with @samp{~}.  This option is
6441 equivalent to @samp{--ignore='*~' --ignore='.*~'}.
6442
6443 @item -d
6444 @itemx --directory
6445 @opindex -d
6446 @opindex --directory
6447 List just the names of directories, as with other types of files, rather
6448 than listing their contents.
6449 @c The following sentence is the same as the one for -F.
6450 Do not follow symbolic links listed on the
6451 command line unless the @option{--dereference-command-line} (@option{-H}),
6452 @option{--dereference} (@option{-L}), or
6453 @option{--dereference-command-line-symlink-to-dir} options are specified.
6454
6455 @item -H
6456 @itemx --dereference-command-line
6457 @opindex -H
6458 @opindex --dereference-command-line
6459 @cindex symbolic links, dereferencing
6460 If a command line argument specifies a symbolic link, show information
6461 for the file the link references rather than for the link itself.
6462
6463 @itemx --dereference-command-line-symlink-to-dir
6464 @opindex --dereference-command-line-symlink-to-dir
6465 @cindex symbolic links, dereferencing
6466 Do not dereference symbolic links, with one exception:
6467 if a command line argument specifies a symbolic link that refers to
6468 a directory, show information for that directory rather than for the
6469 link itself.
6470 This is the default behavior when no other dereferencing-related
6471 option has been specified (@option{--classify} (@option{-F}),
6472 @option{--directory} (@option{-d}),
6473 (@option{-l}),
6474 @option{--dereference} (@option{-L}), or
6475 @option{--dereference-command-line} (@option{-H})).
6476
6477 @item --group-directories-first
6478 @opindex --group-directories-first
6479 Group all the directories before the files and then sort the
6480 directories and the files separately using the selected sort key
6481 (see --sort option).
6482 That is, this option specifies a primary sort key,
6483 and the --sort option specifies a secondary key.
6484 However, any use of @option{--sort=none}
6485 (@option{-U}) disables this option altogether.
6486
6487 @item --hide=PATTERN
6488 @opindex --hide=@var{pattern}
6489 In directories, ignore files whose names match the shell pattern
6490 @var{pattern}, unless the @option{--all} (@option{-a}) or
6491 @option{--almost-all} (@option{-A}) is also given.  This
6492 option acts like @option{--ignore=@var{pattern}} except that it has no
6493 effect if @option{--all} (@option{-a}) or @option{--almost-all}
6494 (@option{-A}) is also given.
6495
6496 This option can be useful in shell aliases.  For example, if
6497 @command{lx} is an alias for @samp{ls --hide='*~'} and @command{ly} is
6498 an alias for @samp{ls --ignore='*~'}, then the command @samp{lx -A}
6499 lists the file @file{README~} even though @samp{ly -A} would not.
6500
6501 @item -I @var{pattern}
6502 @itemx --ignore=@var{pattern}
6503 @opindex -I
6504 @opindex --ignore=@var{pattern}
6505 In directories, ignore files whose names match the shell pattern
6506 (not regular expression) @var{pattern}.  As
6507 in the shell, an initial @samp{.} in a file name does not match a
6508 wildcard at the start of @var{pattern}.  Sometimes it is useful
6509 to give this option several times.  For example,
6510
6511 @smallexample
6512 $ ls --ignore='.??*' --ignore='.[^.]' --ignore='#*'
6513 @end smallexample
6514
6515 The first option ignores names of length 3 or more that start with @samp{.},
6516 the second ignores all two-character names that start with @samp{.}
6517 except @samp{..}, and the third ignores names that start with @samp{#}.
6518
6519 @item -L
6520 @itemx --dereference
6521 @opindex -L
6522 @opindex --dereference
6523 @cindex symbolic links, dereferencing
6524 When showing file information for a symbolic link, show information
6525 for the file the link references rather than the link itself.
6526 However, even with this option, @command{ls} still prints the name
6527 of the link itself, not the name of the file that the link points to.
6528
6529 @item -R
6530 @itemx --recursive
6531 @opindex -R
6532 @opindex --recursive
6533 @cindex recursive directory listing
6534 @cindex directory listing, recursive
6535 List the contents of all directories recursively.
6536
6537 @end table
6538
6539
6540 @node What information is listed
6541 @subsection What information is listed
6542
6543 These options affect the information that @command{ls} displays.  By
6544 default, only file names are shown.
6545
6546 @table @samp
6547
6548 @item --author
6549 @opindex --author
6550 @cindex hurd, author, printing
6551 List each file's author when producing long format directory listings.
6552 In GNU/Hurd, file authors can differ from their owners, but in other
6553 operating systems the two are the same.
6554
6555 @item -D
6556 @itemx --dired
6557 @opindex -D
6558 @opindex --dired
6559 @cindex dired Emacs mode support
6560 With the long listing (@option{-l}) format, print an additional line after
6561 the main output:
6562
6563 @example
6564 //DIRED// @var{beg1} @var{end1} @var{beg2} @var{end2} @dots{}
6565 @end example
6566
6567 @noindent
6568 The @var{begn} and @var{endn} are unsigned integers that record the
6569 byte position of the beginning and end of each file name in the output.
6570 This makes it easy for Emacs to find the names, even when they contain
6571 unusual characters such as space or newline, without fancy searching.
6572
6573 If directories are being listed recursively (@option{-R}), output a similar
6574 line with offsets for each subdirectory name:
6575
6576 @example
6577 //SUBDIRED// @var{beg1} @var{end1} @dots{}
6578 @end example
6579
6580 Finally, output a line of the form:
6581
6582 @example
6583 //DIRED-OPTIONS// --quoting-style=@var{word}
6584 @end example
6585
6586 @noindent
6587 where @var{word} is the quoting style (@pxref{Formatting the file names}).
6588
6589 Here is an actual example:
6590
6591 @example
6592 $ mkdir -p a/sub/deeper a/sub2
6593 $ touch a/f1 a/f2
6594 $ touch a/sub/deeper/file
6595 $ ls -gloRF --dired a
6596   a:
6597   total 8
6598   -rw-r--r-- 1    0 Jun 10 12:27 f1
6599   -rw-r--r-- 1    0 Jun 10 12:27 f2
6600   drwxr-xr-x 3 4096 Jun 10 12:27 sub/
6601   drwxr-xr-x 2 4096 Jun 10 12:27 sub2/
6602
6603   a/sub:
6604   total 4
6605   drwxr-xr-x 2 4096 Jun 10 12:27 deeper/
6606
6607   a/sub/deeper:
6608   total 0
6609   -rw-r--r-- 1 0 Jun 10 12:27 file
6610
6611   a/sub2:
6612   total 0
6613 //DIRED// 48 50 84 86 120 123 158 162 217 223 282 286
6614 //SUBDIRED// 2 3 167 172 228 240 290 296
6615 //DIRED-OPTIONS// --quoting-style=literal
6616 @end example
6617
6618 Note that the pairs of offsets on the @samp{//DIRED//} line above delimit
6619 these names: @file{f1}, @file{f2}, @file{sub}, @file{sub2}, @file{deeper},
6620 @file{file}.
6621 The offsets on the @samp{//SUBDIRED//} line delimit the following
6622 directory names: @file{a}, @file{a/sub}, @file{a/sub/deeper}, @file{a/sub2}.
6623
6624 Here is an example of how to extract the fifth entry name, @samp{deeper},
6625 corresponding to the pair of offsets, 222 and 228:
6626
6627 @example
6628 $ ls -gloRF --dired a > out
6629 $ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
6630 deeper
6631 @end example
6632
6633 Note that although the listing above includes a trailing slash
6634 for the @samp{deeper} entry, the offsets select the name without
6635 the trailing slash.  However, if you invoke @command{ls} with @option{--dired}
6636 along with an option like @option{--escape} (aka @option{-b}) and operate
6637 on a file whose name contains special characters, notice that the backslash
6638 @emph{is} included:
6639
6640 @example
6641 $ touch 'a b'
6642 $ ls -blog --dired 'a b'
6643   -rw-r--r-- 1 0 Jun 10 12:28 a\ b
6644 //DIRED// 30 34
6645 //DIRED-OPTIONS// --quoting-style=escape
6646 @end example
6647
6648 If you use a quoting style that adds quote marks
6649 (e.g., @option{--quoting-style=c}), then the offsets include the quote marks.
6650 So beware that the user may select the quoting style via the environment
6651 variable @env{QUOTING_STYLE}.  Hence, applications using @option{--dired}
6652 should either specify an explicit @option{--quoting-style=literal} option
6653 (aka @option{-N} or @option{--literal}) on the command line, or else be
6654 prepared to parse the escaped names.
6655
6656 @item --full-time
6657 @opindex --full-time
6658 Produce long format directory listings, and list times in full.  It is
6659 equivalent to using @option{--format=long} with
6660 @option{--time-style=full-iso} (@pxref{Formatting file timestamps}).
6661
6662 @item -g
6663 @opindex -g
6664 Produce long format directory listings, but don't display owner information.
6665
6666 @item -G
6667 @itemx --no-group
6668 @opindex -G
6669 @opindex --no-group
6670 Inhibit display of group information in a long format directory listing.
6671 (This is the default in some non-@sc{gnu} versions of @command{ls}, so we
6672 provide this option for compatibility.)
6673
6674 @optHumanReadable
6675
6676 @item -i
6677 @itemx --inode
6678 @opindex -i
6679 @opindex --inode
6680 @cindex inode number, printing
6681 Print the inode number (also called the file serial number and index
6682 number) of each file to the left of the file name.  (This number
6683 uniquely identifies each file within a particular file system.)
6684
6685 @item -l
6686 @itemx --format=long
6687 @itemx --format=verbose
6688 @opindex -l
6689 @opindex --format
6690 @opindex long ls @r{format}
6691 @opindex verbose ls @r{format}
6692 In addition to the name of each file, print the file type, file mode bits,
6693 number of hard links, owner name, group name, size, and
6694 timestamp (@pxref{Formatting file timestamps}), normally
6695 the modification time.  Print question marks for information that
6696 cannot be determined.
6697
6698 Normally the size is printed as a byte count without punctuation, but
6699 this can be overridden (@pxref{Block size}).  For example, @option{-h}
6700 prints an abbreviated, human-readable count, and
6701 @samp{--block-size="'1"} prints a byte count with the thousands
6702 separator of the current locale.
6703
6704 For each directory that is listed, preface the files with a line
6705 @samp{total @var{blocks}}, where @var{blocks} is the total disk allocation
6706 for all files in that directory.  The block size currently defaults to 1024
6707 bytes, but this can be overridden (@pxref{Block size}).
6708 The @var{blocks} computed counts each hard link separately;
6709 this is arguably a deficiency.
6710
6711 The file type is one of the following characters:
6712
6713 @c The commented-out entries are ones we're not sure about.
6714
6715 @table @samp
6716 @item -
6717 regular file
6718 @item b
6719 block special file
6720 @item c
6721 character special file
6722 @item C
6723 high performance (``contiguous data'') file
6724 @item d
6725 directory
6726 @item D
6727 door (Solaris 2.5 and up)
6728 @c @item F
6729 @c semaphore, if this is a distinct file type
6730 @item l
6731 symbolic link
6732 @c @item m
6733 @c multiplexed file (7th edition Unix; obsolete)
6734 @item M
6735 off-line (``migrated'') file (Cray DMF)
6736 @item n
6737 network special file (HP-UX)
6738 @item p
6739 FIFO (named pipe)
6740 @item P
6741 port (Solaris 10 and up)
6742 @c @item Q
6743 @c message queue, if this is a distinct file type
6744 @item s
6745 socket
6746 @c @item S
6747 @c shared memory object, if this is a distinct file type
6748 @c @item T
6749 @c typed memory object, if this is a distinct file type
6750 @c @item w
6751 @c whiteout (4.4BSD; not implemented)
6752 @item ?
6753 some other file type
6754 @end table
6755
6756 @cindex permissions, output by @command{ls}
6757 The file mode bits listed are similar to symbolic mode specifications
6758 (@pxref{Symbolic Modes}).  But @command{ls} combines multiple bits into the
6759 third character of each set of permissions as follows:
6760
6761 @table @samp
6762 @item s
6763 If the set-user-ID or set-group-ID bit and the corresponding executable bit
6764 are both set.
6765
6766 @item S
6767 If the set-user-ID or set-group-ID bit is set but the corresponding
6768 executable bit is not set.
6769
6770 @item t
6771 If the restricted deletion flag or sticky bit, and the
6772 other-executable bit, are both set.  The restricted deletion flag is
6773 another name for the sticky bit.  @xref{Mode Structure}.
6774
6775 @item T
6776 If the restricted deletion flag or sticky bit is set but the
6777 other-executable bit is not set.
6778
6779 @item x
6780 If the executable bit is set and none of the above apply.
6781
6782 @item -
6783 Otherwise.
6784 @end table
6785
6786 Following the file mode bits is a single character that specifies
6787 whether an alternate access method such as an access control list
6788 applies to the file.  When the character following the file mode bits is a
6789 space, there is no alternate access method.  When it is a printing
6790 character, then there is such a method.
6791
6792 GNU @command{ls} uses a @samp{.} character to indicate a file
6793 with an SELinux security context, but no other alternate access method.
6794
6795 A file with any other combination of alternate access methods
6796 is marked with a @samp{+} character.
6797
6798 @item -n
6799 @itemx --numeric-uid-gid
6800 @opindex -n
6801 @opindex --numeric-uid-gid
6802 @cindex numeric uid and gid
6803 @cindex numeric user and group IDs
6804 Produce long format directory listings, but
6805 display numeric user and group IDs instead of the owner and group names.
6806
6807 @item -o
6808 @opindex -o
6809 Produce long format directory listings, but don't display group information.
6810 It is equivalent to using @option{--format=long} with @option{--no-group} .
6811
6812 @item -s
6813 @itemx --size
6814 @opindex -s
6815 @opindex --size
6816 @cindex disk allocation
6817 @cindex size of files, reporting
6818 Print the disk allocation of each file to the left of the file name.
6819 This is the amount of disk space used by the file, which is usually a
6820 bit more than the file's size, but it can be less if the file has holes.
6821
6822 Normally the disk allocation is printed in units of
6823 1024 bytes, but this can be overridden (@pxref{Block size}).
6824
6825 @cindex NFS mounts from BSD to HP-UX
6826 For files that are NFS-mounted from an HP-UX system to a BSD system,
6827 this option reports sizes that are half the correct values.  On HP-UX
6828 systems, it reports sizes that are twice the correct values for files
6829 that are NFS-mounted from BSD systems.  This is due to a flaw in HP-UX;
6830 it also affects the HP-UX @command{ls} program.
6831
6832 @optSi
6833
6834 @item -Z
6835 @itemx --context
6836 @opindex -Z
6837 @opindex --context
6838 @cindex SELinux
6839 @cindex security context
6840 Display the SELinux security context or @samp{?} if none is found.
6841 When used with the @option{-l} option, print the security context
6842 to the left of the size column.
6843
6844 @end table
6845
6846
6847 @node Sorting the output
6848 @subsection Sorting the output
6849
6850 @cindex sorting @command{ls} output
6851 These options change the order in which @command{ls} sorts the information
6852 it outputs.  By default, sorting is done by character code
6853 (e.g., @acronym{ASCII} order).
6854
6855 @table @samp
6856
6857 @item -c
6858 @itemx --time=ctime
6859 @itemx --time=status
6860 @opindex -c
6861 @opindex --time
6862 @opindex ctime@r{, printing or sorting by}
6863 @opindex status time@r{, printing or sorting by}
6864 @opindex use time@r{, printing or sorting files by}
6865 If the long listing format (e.g., @option{-l}, @option{-o}) is being used,
6866 print the status change time (the @samp{ctime} in the inode) instead of
6867 the modification time.
6868 When explicitly sorting by time (@option{--sort=time} or @option{-t})
6869 or when not using a long listing format,
6870 sort according to the status change time.
6871
6872 @item -f
6873 @opindex -f
6874 @cindex unsorted directory listing
6875 @cindex directory order, listing by
6876 Primarily, like @option{-U}---do not sort; list the files in whatever
6877 order they are stored in the directory.  But also enable @option{-a} (list
6878 all files) and disable @option{-l}, @option{--color}, and @option{-s} (if they
6879 were specified before the @option{-f}).
6880
6881 @item -r
6882 @itemx --reverse
6883 @opindex -r
6884 @opindex --reverse
6885 @cindex reverse sorting
6886 Reverse whatever the sorting method is---e.g., list files in reverse
6887 alphabetical order, youngest first, smallest first, or whatever.
6888
6889 @item -S
6890 @itemx --sort=size
6891 @opindex -S
6892 @opindex --sort
6893 @opindex size of files@r{, sorting files by}
6894 Sort by file size, largest first.
6895
6896 @item -t
6897 @itemx --sort=time
6898 @opindex -t
6899 @opindex --sort
6900 @opindex modification time@r{, sorting files by}
6901 Sort by modification time (the @samp{mtime} in the inode), newest first.
6902
6903 @item -u
6904 @itemx --time=atime
6905 @itemx --time=access
6906 @itemx --time=use
6907 @opindex -u
6908 @opindex --time
6909 @opindex use time@r{, printing or sorting files by}
6910 @opindex atime@r{, printing or sorting files by}
6911 @opindex access time@r{, printing or sorting files by}
6912 If the long listing format (e.g., @option{--format=long}) is being used,
6913 print the last access time (the @samp{atime} in the inode).
6914 When explicitly sorting by time (@option{--sort=time} or @option{-t})
6915 or when not using a long listing format, sort according to the access time.
6916
6917 @item -U
6918 @itemx --sort=none
6919 @opindex -U
6920 @opindex --sort
6921 @opindex none@r{, sorting option for @command{ls}}
6922 Do not sort; list the files in whatever order they are
6923 stored in the directory.  (Do not do any of the other unrelated things
6924 that @option{-f} does.)  This is especially useful when listing very large
6925 directories, since not doing any sorting can be noticeably faster.
6926
6927 @item -v
6928 @itemx --sort=version
6929 @opindex -v
6930 @opindex --sort
6931 @opindex version@r{, sorting option for @command{ls}}
6932 Sort by version name and number, lowest first.  It behaves like a default
6933 sort, except that each sequence of decimal digits is treated numerically
6934 as an index/version number.  (@xref{Details about version sort}.)
6935
6936 @item -X
6937 @itemx --sort=extension
6938 @opindex -X
6939 @opindex --sort
6940 @opindex extension@r{, sorting files by}
6941 Sort directory contents alphabetically by file extension (characters
6942 after the last @samp{.}); files with no extension are sorted first.
6943
6944 @end table
6945
6946
6947 @node Details about version sort
6948 @subsection Details about version sort
6949
6950 Version sorting handles the fact that file names frequently include indices or
6951 version numbers.  Standard sorting usually does not produce the order that one
6952 expects because comparisons are made on a character-by-character basis.
6953 Version sorting is especially useful when browsing directories that contain
6954 many files with indices/version numbers in their names:
6955
6956 @example
6957 $ ls -1            $ ls -1v
6958 abc.zml-1.gz       abc.zml-1.gz
6959 abc.zml-12.gz      abc.zml-2.gz
6960 abc.zml-2.gz       abc.zml-12.gz
6961 @end example
6962
6963 Version-sorted strings are compared such that if @var{ver1} and @var{ver2}
6964 are version numbers and @var{prefix} and @var{suffix} (@var{suffix} matching
6965 the regular expression @samp{(\.[A-Za-z~][A-Za-z0-9~]*)*}) are strings then
6966 @var{ver1} < @var{ver2} implies that the name composed of
6967 ``@var{prefix} @var{ver1} @var{suffix}'' sorts before
6968 ``@var{prefix} @var{ver2} @var{suffix}''.
6969
6970 Note also that leading zeros of numeric parts are ignored:
6971
6972 @example
6973 $ ls -1            $ ls -1v
6974 abc-1.007.tgz      abc-1.01a.tgz
6975 abc-1.012b.tgz     abc-1.007.tgz
6976 abc-1.01a.tgz      abc-1.012b.tgz
6977 @end example
6978
6979 This functionality is implemented using gnulib's @code{filevercmp} function,
6980 which has some caveats worth noting.
6981
6982 @itemize @bullet
6983 @item @env{LC_COLLATE} is ignored, which means @samp{ls -v} and @samp{sort -V}
6984 will sort non-numeric prefixes as if the @env{LC_COLLATE} locale category
6985 was set to @samp{C}.
6986 @item Some suffixes will not be matched by the regular
6987 expression mentioned above.  Consequently these examples may
6988 not sort as you expect:
6989
6990 @example
6991 abc-1.2.3.4.7z
6992 abc-1.2.3.7z
6993 @end example
6994
6995 @example
6996 abc-1.2.3.4.x86_64.rpm
6997 abc-1.2.3.x86_64.rpm
6998 @end example
6999 @end itemize
7000
7001 @node General output formatting
7002 @subsection General output formatting
7003
7004 These options affect the appearance of the overall output.
7005
7006 @table @samp
7007
7008 @item -1
7009 @itemx --format=single-column
7010 @opindex -1
7011 @opindex --format
7012 @opindex single-column @r{output of files}
7013 List one file per line.  This is the default for @command{ls} when standard
7014 output is not a terminal.
7015
7016 @item -C
7017 @itemx --format=vertical
7018 @opindex -C
7019 @opindex --format
7020 @opindex vertical @r{sorted files in columns}
7021 List files in columns, sorted vertically.  This is the default for
7022 @command{ls} if standard output is a terminal.  It is always the default
7023 for the @command{dir} program.
7024 @sc{gnu} @command{ls} uses variable width columns to display as many files as
7025 possible in the fewest lines.
7026
7027 @item --color [=@var{when}]
7028 @opindex --color
7029 @cindex color, distinguishing file types with
7030 Specify whether to use color for distinguishing file types.  @var{when}
7031 may be omitted, or one of:
7032 @itemize @bullet
7033 @item none
7034 @vindex none @r{color option}
7035 - Do not use color at all.  This is the default.
7036 @item auto
7037 @vindex auto @r{color option}
7038 @cindex terminal, using color iff
7039 - Only use color if standard output is a terminal.
7040 @item always
7041 @vindex always @r{color option}
7042 - Always use color.
7043 @end itemize
7044 Specifying @option{--color} and no @var{when} is equivalent to
7045 @option{--color=always}.
7046 Piping a colorized listing through a pager like @command{more} or
7047 @command{less} usually produces unreadable results.  However, using
7048 @code{more -f} does seem to work.
7049
7050 @vindex LS_COLORS
7051 @vindex SHELL @r{environment variable, and color}
7052 Note that using the @option{--color} option may incur a noticeable
7053 performance penalty when run in a directory with very many entries,
7054 because the default settings require that @command{ls} @code{stat} every
7055 single file it lists.
7056 However, if you would like most of the file-type coloring
7057 but can live without the other coloring options (e.g.,
7058 executable, orphan, sticky, other-writable, capability), use
7059 @command{dircolors} to set the @env{LS_COLORS} environment variable like this,
7060 @example
7061 eval $(dircolors -p | perl -pe \
7062   's/^((CAP|S[ET]|O[TR]|M|E)\w+).*/$1 00/' | dircolors -)
7063 @end example
7064 and on a @code{dirent.d_type}-capable file system, @command{ls}
7065 will perform only one @code{stat} call per command line argument.
7066
7067 @item -F
7068 @itemx --classify
7069 @itemx --indicator-style=classify
7070 @opindex -F
7071 @opindex --classify
7072 @opindex --indicator-style
7073 @cindex file type and executables, marking
7074 @cindex executables and file type, marking
7075 Append a character to each file name indicating the file type.  Also,
7076 for regular files that are executable, append @samp{*}.  The file type
7077 indicators are @samp{/} for directories, @samp{@@} for symbolic links,
7078 @samp{|} for FIFOs, @samp{=} for sockets, @samp{>} for doors,
7079 and nothing for regular files.
7080 @c The following sentence is the same as the one for -d.
7081 Do not follow symbolic links listed on the
7082 command line unless the @option{--dereference-command-line} (@option{-H}),
7083 @option{--dereference} (@option{-L}), or
7084 @option{--dereference-command-line-symlink-to-dir} options are specified.
7085
7086 @item --file-type
7087 @itemx --indicator-style=file-type
7088 @opindex --file-type
7089 @opindex --indicator-style
7090 @cindex file type, marking
7091 Append a character to each file name indicating the file type.  This is
7092 like @option{-F}, except that executables are not marked.
7093
7094 @item --indicator-style=@var{word}
7095 @opindex --indicator-style
7096 Append a character indicator with style @var{word} to entry names,
7097 as follows:
7098
7099 @table @samp
7100 @item none
7101 Do not append any character indicator; this is the default.
7102 @item slash
7103 Append @samp{/} for directories.  This is the same as the @option{-p}
7104 option.
7105 @item file-type
7106 Append @samp{/} for directories, @samp{@@} for symbolic links, @samp{|}
7107 for FIFOs, @samp{=} for sockets, and nothing for regular files.  This is
7108 the same as the @option{--file-type} option.
7109 @item classify
7110 Append @samp{*} for executable regular files, otherwise behave as for
7111 @samp{file-type}.  This is the same as the @option{-F} or
7112 @option{--classify} option.
7113 @end table
7114
7115 @item -k
7116 @opindex -k
7117 Print file sizes in 1024-byte blocks, overriding the default block
7118 size (@pxref{Block size}).
7119 This option is equivalent to @option{--block-size=1K}.
7120
7121 @item -m
7122 @itemx --format=commas
7123 @opindex -m
7124 @opindex --format
7125 @opindex commas@r{, outputting between files}
7126 List files horizontally, with as many as will fit on each line,
7127 separated by @samp{, } (a comma and a space).
7128
7129 @item -p
7130 @itemx --indicator-style=slash
7131 @opindex -p
7132 @opindex --indicator-style
7133 @cindex file type, marking
7134 Append a @samp{/} to directory names.
7135
7136 @item -x
7137 @itemx --format=across
7138 @itemx --format=horizontal
7139 @opindex -x
7140 @opindex --format
7141 @opindex across@r{, listing files}
7142 @opindex horizontal@r{, listing files}
7143 List the files in columns, sorted horizontally.
7144
7145 @item -T @var{cols}
7146 @itemx --tabsize=@var{cols}
7147 @opindex -T
7148 @opindex --tabsize
7149 Assume that each tab stop is @var{cols} columns wide.  The default is 8.
7150 @command{ls} uses tabs where possible in the output, for efficiency.  If
7151 @var{cols} is zero, do not use tabs at all.
7152
7153 @c FIXME: remove in 2009, if Apple Terminal has been fixed for long enough.
7154 Some terminal emulators (at least Apple Terminal 1.5 (133) from Mac OS X 10.4.8)
7155 do not properly align columns to the right of a TAB following a
7156 non-@acronym{ASCII} byte.  If you use such a terminal emulator, use the
7157 @option{-T0} option or put @code{TABSIZE=0} in your environment to tell
7158 @command{ls} to align using spaces, not tabs.
7159
7160 @item -w
7161 @itemx --width=@var{cols}
7162 @opindex -w
7163 @opindex --width
7164 @vindex COLUMNS
7165 Assume the screen is @var{cols} columns wide.  The default is taken
7166 from the terminal settings if possible; otherwise the environment
7167 variable @env{COLUMNS} is used if it is set; otherwise the default
7168 is 80.
7169
7170 @end table
7171
7172
7173 @node Formatting file timestamps
7174 @subsection Formatting file timestamps
7175
7176 By default, file timestamps are listed in abbreviated form, using
7177 a date like @samp{Mar 30@ @ 2002} for non-recent timestamps, and a
7178 date-without-year and time like @samp{Mar 30 23:45} for recent timestamps.
7179 This format can change depending on the current locale as detailed below.
7180
7181 A timestamp is considered to be @dfn{recent} if it is less than six
7182 months old, and is not dated in the future.  If a timestamp dated
7183 today is not listed in recent form, the timestamp is in the future,
7184 which means you probably have clock skew problems which may break
7185 programs like @command{make} that rely on file timestamps.
7186
7187 @vindex TZ
7188 Time stamps are listed according to the time zone rules specified by
7189 the @env{TZ} environment variable, or by the system default rules if
7190 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
7191 with @env{TZ}, libc, The GNU C Library Reference Manual}.
7192
7193 The following option changes how file timestamps are printed.
7194
7195 @table @samp
7196 @item --time-style=@var{style}
7197 @opindex --time-style
7198 @cindex time style
7199 List timestamps in style @var{style}.  The @var{style} should
7200 be one of the following:
7201
7202 @table @samp
7203 @item +@var{format}
7204 @vindex LC_TIME
7205 List timestamps using @var{format}, where @var{format} is interpreted
7206 like the format argument of @command{date} (@pxref{date invocation}).
7207 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
7208 @command{ls} to list timestamps like @samp{2002-03-30 23:45:56}.  As
7209 with @command{date}, @var{format}'s interpretation is affected by the
7210 @env{LC_TIME} locale category.
7211
7212 If @var{format} contains two format strings separated by a newline,
7213 the former is used for non-recent files and the latter for recent
7214 files; if you want output columns to line up, you may need to insert
7215 spaces in one of the two formats.
7216
7217 @item full-iso
7218 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
7219 format with nanosecond precision, e.g., @samp{2002-03-30
7220 23:45:56.477817180 -0700}.  This style is equivalent to
7221 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
7222
7223 This is useful because the time output includes all the information that
7224 is available from the operating system.  For example, this can help
7225 explain @command{make}'s behavior, since @acronym{GNU} @command{make}
7226 uses the full timestamp to determine whether a file is out of date.
7227
7228 @item long-iso
7229 List @acronym{ISO} 8601 date and time in minutes, e.g.,
7230 @samp{2002-03-30 23:45}.  These timestamps are shorter than
7231 @samp{full-iso} timestamps, and are usually good enough for everyday
7232 work.  This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
7233
7234 @item iso
7235 List @acronym{ISO} 8601 dates for non-recent timestamps (e.g.,
7236 @samp{2002-03-30@ }), and @acronym{ISO} 8601 month, day, hour, and
7237 minute for recent timestamps (e.g., @samp{03-30 23:45}).  These
7238 timestamps are uglier than @samp{long-iso} timestamps, but they carry
7239 nearly the same information in a smaller space and their brevity helps
7240 @command{ls} output fit within traditional 80-column output lines.
7241 The following two @command{ls} invocations are equivalent:
7242
7243 @example
7244 newline='
7245 '
7246 ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
7247 ls -l --time-style="iso"
7248 @end example
7249
7250 @item locale
7251 @vindex LC_TIME
7252 List timestamps in a locale-dependent form.  For example, a Finnish
7253 locale might list non-recent timestamps like @samp{maalis 30@ @ 2002}
7254 and recent timestamps like @samp{maalis 30 23:45}.  Locale-dependent
7255 timestamps typically consume more space than @samp{iso} timestamps and
7256 are harder for programs to parse because locale conventions vary so
7257 widely, but they are easier for many people to read.
7258
7259 The @env{LC_TIME} locale category specifies the timestamp format.  The
7260 default @acronym{POSIX} locale uses timestamps like @samp{Mar 30@
7261 @ 2002} and @samp{Mar 30 23:45}; in this locale, the following two
7262 @command{ls} invocations are equivalent:
7263
7264 @example
7265 newline='
7266 '
7267 ls -l --time-style="+%b %e  %Y$newline%b %e %H:%M"
7268 ls -l --time-style="locale"
7269 @end example
7270
7271 Other locales behave differently.  For example, in a German locale,
7272 @option{--time-style="locale"} might be equivalent to
7273 @option{--time-style="+%e. %b %Y $newline%e. %b %H:%M"}
7274 and might generate timestamps like @samp{30. M@"ar 2002@ } and
7275 @samp{30. M@"ar 23:45}.
7276
7277 @item posix-@var{style}
7278 @vindex LC_TIME
7279 List @acronym{POSIX}-locale timestamps if the @env{LC_TIME} locale
7280 category is @acronym{POSIX}, @var{style} timestamps otherwise.  For
7281 example, the @samp{posix-long-iso} style lists
7282 timestamps like @samp{Mar 30@ @ 2002} and @samp{Mar 30 23:45} when in
7283 the @acronym{POSIX} locale, and like @samp{2002-03-30 23:45} otherwise.
7284 @end table
7285 @end table
7286
7287 @vindex TIME_STYLE
7288 You can specify the default value of the @option{--time-style} option
7289 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
7290 the default style is @samp{locale}.  @acronym{GNU} Emacs 21.3 and
7291 later use the @option{--dired} option and therefore can parse any date
7292 format, but if you are using Emacs 21.1 or 21.2 and specify a
7293 non-@acronym{POSIX} locale you may need to set
7294 @samp{TIME_STYLE="posix-long-iso"}.
7295
7296 To avoid certain denial-of-service attacks, timestamps that would be
7297 longer than 1000 bytes may be treated as errors.
7298
7299
7300 @node Formatting the file names
7301 @subsection Formatting the file names
7302
7303 These options change how file names themselves are printed.
7304
7305 @table @samp
7306
7307 @item -b
7308 @itemx --escape
7309 @itemx --quoting-style=escape
7310 @opindex -b
7311 @opindex --escape
7312 @opindex --quoting-style
7313 @cindex backslash sequences for file names
7314 Quote nongraphic characters in file names using alphabetic and octal
7315 backslash sequences like those used in C.
7316
7317 @item -N
7318 @itemx --literal
7319 @itemx --quoting-style=literal
7320 @opindex -N
7321 @opindex --literal
7322 @opindex --quoting-style
7323 Do not quote file names.  However, with @command{ls} nongraphic
7324 characters are still printed as question marks if the output is a
7325 terminal and you do not specify the @option{--show-control-chars}
7326 option.
7327
7328 @item -q
7329 @itemx --hide-control-chars
7330 @opindex -q
7331 @opindex --hide-control-chars
7332 Print question marks instead of nongraphic characters in file names.
7333 This is the default if the output is a terminal and the program is
7334 @command{ls}.
7335
7336 @item -Q
7337 @itemx --quote-name
7338 @itemx --quoting-style=c
7339 @opindex -Q
7340 @opindex --quote-name
7341 @opindex --quoting-style
7342 Enclose file names in double quotes and quote nongraphic characters as
7343 in C.
7344
7345 @item --quoting-style=@var{word}
7346 @opindex --quoting-style
7347 @cindex quoting style
7348 Use style @var{word} to quote file names and other strings that may
7349 contain arbitrary characters.  The @var{word} should
7350 be one of the following:
7351
7352 @table @samp
7353 @item literal
7354 Output strings as-is; this is the same as the @option{-N} or
7355 @option{--literal} option.
7356 @item shell
7357 Quote strings for the shell if they contain shell metacharacters or would
7358 cause ambiguous output.
7359 The quoting is suitable for @acronym{POSIX}-compatible shells like
7360 @command{bash}, but it does not always work for incompatible shells
7361 like @command{csh}.
7362 @item shell-always
7363 Quote strings for the shell, even if they would normally not require quoting.
7364 @item c
7365 Quote strings as for C character string literals, including the
7366 surrounding double-quote characters; this is the same as the
7367 @option{-Q} or @option{--quote-name} option.
7368 @item escape
7369 Quote strings as for C character string literals, except omit the
7370 surrounding double-quote
7371 characters; this is the same as the @option{-b} or @option{--escape} option.
7372 @item clocale
7373 Quote strings as for C character string literals, except use
7374 surrounding quotation marks appropriate for the
7375 locale.
7376 @item locale
7377 @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
7378 Quote strings as for C character string literals, except use
7379 surrounding quotation marks appropriate for the locale, and quote
7380 @t{`like this'} instead of @t{"like
7381 this"} in the default C locale.  This looks nicer on many displays.
7382 @end table
7383
7384 You can specify the default value of the @option{--quoting-style} option
7385 with the environment variable @env{QUOTING_STYLE}.  If that environment
7386 variable is not set, the default value is @samp{literal}, but this
7387 default may change to @samp{shell} in a future version of this package.
7388
7389 @item --show-control-chars
7390 @opindex --show-control-chars
7391 Print nongraphic characters as-is in file names.
7392 This is the default unless the output is a terminal and the program is
7393 @command{ls}.
7394
7395 @end table
7396
7397
7398 @node dir invocation
7399 @section @command{dir}: Briefly list directory contents
7400
7401 @pindex dir
7402 @cindex directory listing, brief
7403
7404 @command{dir} is equivalent to @code{ls -C
7405 -b}; that is, by default files are listed in columns, sorted vertically,
7406 and special characters are represented by backslash escape sequences.
7407
7408 @xref{ls invocation, @command{ls}}.
7409
7410
7411 @node vdir invocation
7412 @section @command{vdir}: Verbosely list directory contents
7413
7414 @pindex vdir
7415 @cindex directory listing, verbose
7416
7417 @command{vdir} is equivalent to @code{ls -l
7418 -b}; that is, by default files are listed in long format and special
7419 characters are represented by backslash escape sequences.
7420
7421 @node dircolors invocation
7422 @section @command{dircolors}: Color setup for @command{ls}
7423
7424 @pindex dircolors
7425 @cindex color setup
7426 @cindex setup for color
7427
7428 @command{dircolors} outputs a sequence of shell commands to set up the
7429 terminal for color output from @command{ls} (and @command{dir}, etc.).
7430 Typical usage:
7431
7432 @example
7433 eval "`dircolors [@var{option}]@dots{} [@var{file}]`"
7434 @end example
7435
7436 If @var{file} is specified, @command{dircolors} reads it to determine which
7437 colors to use for which file types and extensions.  Otherwise, a
7438 precompiled database is used.  For details on the format of these files,
7439 run @samp{dircolors --print-database}.
7440
7441 To make @command{dircolors} read a @file{~/.dircolors} file if it
7442 exists, you can put the following lines in your @file{~/.bashrc} (or
7443 adapt them to your favorite shell):
7444
7445 @example
7446 d=.dircolors
7447 test -r $d && eval "$(dircolors $d)"
7448 @end example
7449
7450 @vindex LS_COLORS
7451 @vindex SHELL @r{environment variable, and color}
7452 The output is a shell command to set the @env{LS_COLORS} environment
7453 variable.  You can specify the shell syntax to use on the command line,
7454 or @command{dircolors} will guess it from the value of the @env{SHELL}
7455 environment variable.
7456
7457 The program accepts the following options.  Also see @ref{Common options}.
7458
7459 @table @samp
7460 @item -b
7461 @itemx --sh
7462 @itemx --bourne-shell
7463 @opindex -b
7464 @opindex --sh
7465 @opindex --bourne-shell
7466 @cindex Bourne shell syntax for color setup
7467 @cindex @command{sh} syntax for color setup
7468 Output Bourne shell commands.  This is the default if the @env{SHELL}
7469 environment variable is set and does not end with @samp{csh} or
7470 @samp{tcsh}.
7471
7472 @item -c
7473 @itemx --csh
7474 @itemx --c-shell
7475 @opindex -c
7476 @opindex --csh
7477 @opindex --c-shell
7478 @cindex C shell syntax for color setup
7479 @cindex @command{csh} syntax for color setup
7480 Output C shell commands.  This is the default if @code{SHELL} ends with
7481 @command{csh} or @command{tcsh}.
7482
7483 @item -p
7484 @itemx --print-database
7485 @opindex -p
7486 @opindex --print-database
7487 @cindex color database, printing
7488 @cindex database for color setup, printing
7489 @cindex printing color database
7490 Print the (compiled-in) default color configuration database.  This
7491 output is itself a valid configuration file, and is fairly descriptive
7492 of the possibilities.
7493
7494 @end table
7495
7496 @exitstatus
7497
7498
7499 @node Basic operations
7500 @chapter Basic operations
7501
7502 @cindex manipulating files
7503
7504 This chapter describes the commands for basic file manipulation:
7505 copying, moving (renaming), and deleting (removing).
7506
7507 @menu
7508 * cp invocation::               Copy files.
7509 * dd invocation::               Convert and copy a file.
7510 * install invocation::          Copy files and set attributes.
7511 * mv invocation::               Move (rename) files.
7512 * rm invocation::               Remove files or directories.
7513 * shred invocation::            Remove files more securely.
7514 @end menu
7515
7516
7517 @node cp invocation
7518 @section @command{cp}: Copy files and directories
7519
7520 @pindex cp
7521 @cindex copying files and directories
7522 @cindex files, copying
7523 @cindex directories, copying
7524
7525 @command{cp} copies files (or, optionally, directories).  The copy is
7526 completely independent of the original.  You can either copy one file to
7527 another, or copy arbitrarily many files to a destination directory.
7528 Synopses:
7529
7530 @example
7531 cp [@var{option}]@dots{} [-T] @var{source} @var{dest}
7532 cp [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7533 cp [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7534 @end example
7535
7536 @itemize @bullet
7537 @item
7538 If two file names are given, @command{cp} copies the first file to the
7539 second.
7540
7541 @item
7542 If the @option{--target-directory} (@option{-t}) option is given, or
7543 failing that if the last file is a directory and the
7544 @option{--no-target-directory} (@option{-T}) option is not given,
7545 @command{cp} copies each @var{source} file to the specified directory,
7546 using the @var{source}s' names.
7547 @end itemize
7548
7549 Generally, files are written just as they are read.  For exceptions,
7550 see the @option{--sparse} option below.
7551
7552 By default, @command{cp} does not copy directories.  However, the
7553 @option{-R}, @option{-a}, and @option{-r} options cause @command{cp} to
7554 copy recursively by descending into source directories and copying files
7555 to corresponding destination directories.
7556
7557 When copying from a symbolic link, @command{cp} normally follows the
7558 link only when not copying
7559 recursively.  This default can be overridden with the
7560 @option{--archive} (@option{-a}), @option{-d}, @option{--dereference}
7561 (@option{-L}), @option{--no-dereference} (@option{-P}), and
7562 @option{-H} options.  If more than one of these options is specified,
7563 the last one silently overrides the others.
7564
7565 When copying to a symbolic link, @command{cp} follows the
7566 link only when it refers to an existing regular file.
7567 However, when copying to a dangling symbolic link, @command{cp}
7568 refuses by default, and fails with a diagnostic, since the operation
7569 is inherently dangerous.  This behavior is contrary to historical
7570 practice and to @acronym{POSIX}.
7571 Set @env{POSIXLY_CORRECT} to make @command{cp} attempt to create
7572 the target of a dangling destination symlink, in spite of the possible risk.
7573 Also, when an option like
7574 @option{--backup} or @option{--link} acts to rename or remove the
7575 destination before copying, @command{cp} renames or removes the
7576 symbolic link rather than the file it points to.
7577
7578 By default, @command{cp} copies the contents of special files only
7579 when not copying recursively.  This default can be overridden with the
7580 @option{--copy-contents} option.
7581
7582 @cindex self-backups
7583 @cindex backups, making only
7584 @command{cp} generally refuses to copy a file onto itself, with the
7585 following exception: if @option{--force --backup} is specified with
7586 @var{source} and @var{dest} identical, and referring to a regular file,
7587 @command{cp} will make a backup file, either regular or numbered, as
7588 specified in the usual ways (@pxref{Backup options}).  This is useful when
7589 you simply want to make a backup of an existing file before changing it.
7590
7591 The program accepts the following options.  Also see @ref{Common options}.
7592
7593 @table @samp
7594 @item -a
7595 @itemx --archive
7596 @opindex -a
7597 @opindex --archive
7598 Preserve as much as possible of the structure and attributes of the
7599 original files in the copy (but do not attempt to preserve internal
7600 directory structure; i.e., @samp{ls -U} may list the entries in a copied
7601 directory in a different order).
7602 Try to preserve SELinux security context and extended attributes (xattr),
7603 but ignore any failure to do that and print no corresponding diagnostic.
7604 Equivalent to @option{-dR --preserve=all} with the reduced diagnostics.
7605
7606 @itemx --attributes-only
7607 @opindex --attributes-only
7608 Preserve the specified attributes of the original files in the copy,
7609 but do not copy any data.  See the @option{--preserve} option for
7610 controlling which attributes to copy.
7611
7612 @item -b
7613 @itemx @w{@kbd{--backup}[=@var{method}]}
7614 @opindex -b
7615 @opindex --backup
7616 @vindex VERSION_CONTROL
7617 @cindex backups, making
7618 @xref{Backup options}.
7619 Make a backup of each file that would otherwise be overwritten or removed.
7620 As a special case, @command{cp} makes a backup of @var{source} when the force
7621 and backup options are given and @var{source} and @var{dest} are the same
7622 name for an existing, regular file.  One useful application of this
7623 combination of options is this tiny Bourne shell script:
7624
7625 @example
7626 #!/bin/sh
7627 # Usage: backup FILE...
7628 # Create a @sc{gnu}-style backup of each listed FILE.
7629 for i; do
7630   cp --backup --force -- "$i" "$i"
7631 done
7632 @end example
7633
7634 @item --copy-contents
7635 @cindex directories, copying recursively
7636 @cindex copying directories recursively
7637 @cindex recursively copying directories
7638 @cindex non-directories, copying as special files
7639 If copying recursively, copy the contents of any special files (e.g.,
7640 FIFOs and device files) as if they were regular files.  This means
7641 trying to read the data in each source file and writing it to the
7642 destination.  It is usually a mistake to use this option, as it
7643 normally has undesirable effects on special files like FIFOs and the
7644 ones typically found in the @file{/dev} directory.  In most cases,
7645 @code{cp -R --copy-contents} will hang indefinitely trying to read
7646 from FIFOs and special files like @file{/dev/console}, and it will
7647 fill up your destination disk if you use it to copy @file{/dev/zero}.
7648 This option has no effect unless copying recursively, and it does not
7649 affect the copying of symbolic links.
7650
7651 @item -d
7652 @opindex -d
7653 @cindex symbolic links, copying
7654 @cindex hard links, preserving
7655 Copy symbolic links as symbolic links rather than copying the files that
7656 they point to, and preserve hard links between source files in the copies.
7657 Equivalent to @option{--no-dereference --preserve=links}.
7658
7659 @item -f
7660 @itemx --force
7661 @opindex -f
7662 @opindex --force
7663 When copying without this option and an existing destination file cannot
7664 be opened for writing, the copy fails.  However, with @option{--force}),
7665 when a destination file cannot be opened, @command{cp} then removes it and
7666 tries to open it again.  Contrast this behavior with that enabled by
7667 @option{--link} and @option{--symbolic-link}, whereby the destination file
7668 is never opened but rather is removed unconditionally.  Also see the
7669 description of @option{--remove-destination}.
7670
7671 This option is independent of the @option{--interactive} or
7672 @option{-i} option: neither cancels the effect of the other.
7673
7674 This option is redundant if the @option{--no-clobber} or @option{-n} option is
7675 used.
7676
7677 @item -H
7678 @opindex -H
7679 If a command line argument specifies a symbolic link, then copy the
7680 file it points to rather than the symbolic link itself.  However,
7681 copy (preserving its nature) any symbolic link that is encountered
7682 via recursive traversal.
7683
7684 @item -i
7685 @itemx --interactive
7686 @opindex -i
7687 @opindex --interactive
7688 When copying a file other than a directory, prompt whether to
7689 overwrite an existing destination file. The @option{-i} option overrides
7690 a previous @option{-n} option.
7691
7692 @item -l
7693 @itemx --link
7694 @opindex -l
7695 @opindex --link
7696 Make hard links instead of copies of non-directories.
7697
7698 @item -L
7699 @itemx --dereference
7700 @opindex -L
7701 @opindex --dereference
7702 Follow symbolic links when copying from them.
7703 With this option, @command{cp} cannot create a symbolic link.
7704 For example, a symlink (to regular file) in the source tree will be copied to
7705 a regular file in the destination tree.
7706
7707 @item -n
7708 @itemx --no-clobber
7709 @opindex -n
7710 @opindex --no-clobber
7711 Do not overwrite an existing file. The @option{-n} option overrides a previous
7712 @option{-i} option. This option is mutually exclusive with @option{-b} or
7713 @option{--backup} option.
7714
7715 @item -P
7716 @itemx --no-dereference
7717 @opindex -P
7718 @opindex --no-dereference
7719 @cindex symbolic links, copying
7720 Copy symbolic links as symbolic links rather than copying the files that
7721 they point to.  This option affects only symbolic links in the source;
7722 symbolic links in the destination are always followed if possible.
7723
7724 @item -p
7725 @itemx @w{@kbd{--preserve}[=@var{attribute_list}]}
7726 @opindex -p
7727 @opindex --preserve
7728 @cindex file information, preserving, extended attributes, xattr
7729 Preserve the specified attributes of the original files.
7730 If specified, the @var{attribute_list} must be a comma-separated list
7731 of one or more of the following strings:
7732
7733 @table @samp
7734 @itemx mode
7735 Preserve the file mode bits and access control lists.
7736 @itemx ownership
7737 Preserve the owner and group.  On most modern systems,
7738 only users with appropriate privileges may change the owner of a file,
7739 and ordinary users
7740 may preserve the group ownership of a file only if they happen to be
7741 a member of the desired group.
7742 @itemx timestamps
7743 Preserve the times of last access and last modification, when possible.
7744 On older systems, it is not possible to preserve these attributes
7745 when the affected file is a symbolic link.
7746 However, many systems now provide the @code{utimensat} function,
7747 which makes it possible even for symbolic links.
7748 @itemx links
7749 Preserve in the destination files
7750 any links between corresponding source files.
7751 Note that with @option{-L} or @option{-H}, this option can convert
7752 symbolic links to hard links.  For example,
7753 @example
7754 $ mkdir c; : > a; ln -s a b; cp -aH a b c; ls -i1 c
7755 74161745 a
7756 74161745 b
7757 @end example
7758 @noindent
7759 Note the inputs: @file{b} is a symlink to regular file @file{a},
7760 yet the files in destination directory, @file{c/}, are hard-linked.
7761 Since @option{-a} implies @option{--preserve=links}, and since @option{-H}
7762 tells @command{cp} to dereference command line arguments, it sees two files
7763 with the same inode number, and preserves the perceived hard link.
7764
7765 Here is a similar example that exercises @command{cp}'s @option{-L} option:
7766 @smallexample
7767 $ mkdir b c; (cd b; : > a; ln -s a b); cp -aL b c; ls -i1 c/b
7768 74163295 a
7769 74163295 b
7770 @end smallexample
7771
7772 @itemx context
7773 Preserve SELinux security context of the file, or fail with full diagnostics.
7774 @itemx xattr
7775 Preserve extended attributes of the file, or fail with full diagnostics.
7776 If @command{cp} is built without xattr support, ignore this option.
7777 If SELinux context, ACLs or Capabilities are implemented using xattrs,
7778 they are preserved by this option as well.
7779 @itemx all
7780 Preserve all file attributes.
7781 Equivalent to specifying all of the above, but with the difference
7782 that failure to preserve SELinux security context or extended attributes
7783 does not change @command{cp}'s exit status.  In contrast to @option{-a},
7784 all but @samp{Operation not supported} warnings are output.
7785 @end table
7786
7787 Using @option{--preserve} with no @var{attribute_list} is equivalent
7788 to @option{--preserve=mode,ownership,timestamps}.
7789
7790 In the absence of this option, each destination file is created with the
7791 mode bits of the corresponding source file, minus the bits set in the
7792 umask and minus the set-user-ID and set-group-ID bits.
7793 @xref{File permissions}.
7794
7795 @itemx @w{@kbd{--no-preserve}=@var{attribute_list}}
7796 @cindex file information, preserving
7797 Do not preserve the specified attributes.  The @var{attribute_list}
7798 has the same form as for @option{--preserve}.
7799
7800 @itemx --parents
7801 @opindex --parents
7802 @cindex parent directories and @command{cp}
7803 Form the name of each destination file by appending to the target
7804 directory a slash and the specified name of the source file.  The last
7805 argument given to @command{cp} must be the name of an existing directory.
7806 For example, the command:
7807
7808 @example
7809 cp --parents a/b/c existing_dir
7810 @end example
7811
7812 @noindent
7813 copies the file @file{a/b/c} to @file{existing_dir/a/b/c}, creating
7814 any missing intermediate directories.
7815
7816 @item -R
7817 @itemx -r
7818 @itemx --recursive
7819 @opindex -R
7820 @opindex -r
7821 @opindex --recursive
7822 @cindex directories, copying recursively
7823 @cindex copying directories recursively
7824 @cindex recursively copying directories
7825 @cindex non-directories, copying as special files
7826 Copy directories recursively.  By default, do not follow symbolic
7827 links in the source; see the @option{--archive} (@option{-a}), @option{-d},
7828 @option{--dereference} (@option{-L}), @option{--no-dereference}
7829 (@option{-P}), and @option{-H} options.  Special files are copied by
7830 creating a destination file of the same type as the source; see the
7831 @option{--copy-contents} option.  It is not portable to use
7832 @option{-r} to copy symbolic links or special files.  On some
7833 non-@sc{gnu} systems, @option{-r} implies the equivalent of
7834 @option{-L} and @option{--copy-contents} for historical reasons.
7835 Also, it is not portable to use @option{-R} to copy symbolic links
7836 unless you also specify @option{-P}, as @acronym{POSIX} allows
7837 implementations that dereference symbolic links by default.
7838
7839 @item --reflink[=@var{when}]
7840 @opindex --reflink[=@var{when}]
7841 @cindex COW
7842 @cindex clone
7843 @cindex copy on write
7844 Perform a lightweight, copy-on-write (COW) copy, if supported by the
7845 file system. Once it has succeeded, beware that the source and destination
7846 files share the same disk data blocks as long as they remain unmodified.
7847 Thus, if a disk I/O error affects data blocks of one of the files,
7848 the other suffers the same fate.
7849
7850 The @var{when} value can be one of the following:
7851
7852 @table @samp
7853 @item always
7854 The default behavior: if the copy-on-write operation is not supported
7855 then report the failure for each file and exit with a failure status.
7856
7857 @item auto
7858 If the copy-on-write operation is not supported then fall back
7859 to the standard copy behaviour.
7860 @end table
7861
7862 This option is overridden by the @option{--link}, @option{--symbolic-link}
7863 and @option{--attributes-only} options, thus allowing it to be used
7864 to configure the default data copying behavior for @command{cp}.
7865 For example, with the following alias, @command{cp} will use the
7866 minimum amount of space supported by the file system.
7867
7868 @example
7869 alias cp='cp --reflink=auto --sparse=always'
7870 @end example
7871
7872 @item --remove-destination
7873 @opindex --remove-destination
7874 Remove each existing destination file before attempting to open it
7875 (contrast with @option{-f} above).
7876
7877 @item --sparse=@var{when}
7878 @opindex --sparse=@var{when}
7879 @cindex sparse files, copying
7880 @cindex holes, copying files with
7881 @findex read @r{system call, and holes}
7882 A @dfn{sparse file} contains @dfn{holes}---a sequence of zero bytes that
7883 does not occupy any physical disk blocks; the @samp{read} system call
7884 reads these as zeros.  This can both save considerable disk space and
7885 increase speed, since many binary files contain lots of consecutive zero
7886 bytes.  By default, @command{cp} detects holes in input source files via a crude
7887 heuristic and makes the corresponding output file sparse as well.
7888 Only regular files may be sparse.
7889
7890 The @var{when} value can be one of the following:
7891
7892 @table @samp
7893 @item auto
7894 The default behavior: if the input file is sparse, attempt to make
7895 the output file sparse, too.  However, if an output file exists but
7896 refers to a non-regular file, then do not attempt to make it sparse.
7897
7898 @item always
7899 For each sufficiently long sequence of zero bytes in the input file,
7900 attempt to create a corresponding hole in the output file, even if the
7901 input file does not appear to be sparse.
7902 This is useful when the input file resides on a file system
7903 that does not support sparse files
7904 (for example,  @samp{efs} file systems in SGI IRIX 5.3 and earlier),
7905 but the output file is on a type of file system that does support them.
7906 Holes may be created only in regular files, so if the destination file
7907 is of some other type, @command{cp} does not even try to make it sparse.
7908
7909 @item never
7910 Never make the output file sparse.
7911 This is useful in creating a file for use with the @command{mkswap} command,
7912 since such a file must not have any holes.
7913 @end table
7914
7915 @optStripTrailingSlashes
7916
7917 @item -s
7918 @itemx --symbolic-link
7919 @opindex -s
7920 @opindex --symbolic-link
7921 @cindex symbolic links, copying with
7922 Make symbolic links instead of copies of non-directories.  All source
7923 file names must be absolute (starting with @samp{/}) unless the
7924 destination files are in the current directory.  This option merely
7925 results in an error message on systems that do not support symbolic links.
7926
7927 @optBackupSuffix
7928
7929 @optTargetDirectory
7930
7931 @optNoTargetDirectory
7932
7933 @item -u
7934 @itemx --update
7935 @opindex -u
7936 @opindex --update
7937 @cindex newer files, copying only
7938 Do not copy a non-directory that has an existing destination with the
7939 same or newer modification time.  If time stamps are being preserved,
7940 the comparison is to the source time stamp truncated to the
7941 resolutions of the destination file system and of the system calls
7942 used to update time stamps; this avoids duplicate work if several
7943 @samp{cp -pu} commands are executed with the same source and
7944 destination.
7945
7946 @item -v
7947 @itemx --verbose
7948 @opindex -v
7949 @opindex --verbose
7950 Print the name of each file before copying it.
7951
7952 @item -x
7953 @itemx --one-file-system
7954 @opindex -x
7955 @opindex --one-file-system
7956 @cindex file systems, omitting copying to different
7957 Skip subdirectories that are on different file systems from the one that
7958 the copy started on.
7959 However, mount point directories @emph{are} copied.
7960
7961 @end table
7962
7963 @exitstatus
7964
7965
7966 @node dd invocation
7967 @section @command{dd}: Convert and copy a file
7968
7969 @pindex dd
7970 @cindex converting while copying a file
7971
7972 @command{dd} copies a file (from standard input to standard output, by
7973 default) with a changeable I/O block size, while optionally performing
7974 conversions on it.  Synopses:
7975
7976 @example
7977 dd [@var{operand}]@dots{}
7978 dd @var{option}
7979 @end example
7980
7981 The only options are @option{--help} and @option{--version}.
7982 @xref{Common options}.  @command{dd} accepts the following operands.
7983
7984 @table @samp
7985
7986 @item if=@var{file}
7987 @opindex if
7988 Read from @var{file} instead of standard input.
7989
7990 @item of=@var{file}
7991 @opindex of
7992 Write to @var{file} instead of standard output.  Unless
7993 @samp{conv=notrunc} is given, @command{dd} truncates @var{file} to zero
7994 bytes (or the size specified with @samp{seek=}).
7995
7996 @item ibs=@var{bytes}
7997 @opindex ibs
7998 @cindex block size of input
7999 @cindex input block size
8000 Set the input block size to @var{bytes}.
8001 This makes @command{dd} read @var{bytes} per block.
8002 The default is 512 bytes.
8003
8004 @item obs=@var{bytes}
8005 @opindex obs
8006 @cindex block size of output
8007 @cindex output block size
8008 Set the output block size to @var{bytes}.
8009 This makes @command{dd} write @var{bytes} per block.
8010 The default is 512 bytes.
8011
8012 @item bs=@var{bytes}
8013 @opindex bs
8014 @cindex block size
8015 Set both input and output block sizes to @var{bytes}.
8016 This makes @command{dd} read and write @var{bytes} per block,
8017 overriding any @samp{ibs} and @samp{obs} settings.
8018 In addition, if no data-transforming @option{conv} option is specified,
8019 input is copied to the output as soon as it's read,
8020 even if it is smaller than the block size.
8021
8022 @item cbs=@var{bytes}
8023 @opindex cbs
8024 @cindex block size of conversion
8025 @cindex conversion block size
8026 @cindex fixed-length records, converting to variable-length
8027 @cindex variable-length records, converting to fixed-length
8028 Set the conversion block size to @var{bytes}.
8029 When converting variable-length records to fixed-length ones
8030 (@option{conv=block}) or the reverse (@option{conv=unblock}),
8031 use @var{bytes} as the fixed record length.
8032
8033 @item skip=@var{blocks}
8034 @opindex skip
8035 Skip @var{blocks} @samp{ibs}-byte blocks in the input file before copying.
8036
8037 @item seek=@var{blocks}
8038 @opindex seek
8039 Skip @var{blocks} @samp{obs}-byte blocks in the output file before copying.
8040
8041 @item count=@var{blocks}
8042 @opindex count
8043 Copy @var{blocks} @samp{ibs}-byte blocks from the input file, instead
8044 of everything until the end of the file.
8045
8046 @item status=noxfer
8047 @opindex status
8048 Do not print the overall transfer rate and volume statistics
8049 that normally make up the third status line when @command{dd} exits.
8050
8051 @item conv=@var{conversion}[,@var{conversion}]@dots{}
8052 @opindex conv
8053 Convert the file as specified by the @var{conversion} argument(s).
8054 (No spaces around any comma(s).)
8055
8056 Conversions:
8057
8058 @table @samp
8059
8060 @item ascii
8061 @opindex ascii@r{, converting to}
8062 Convert @acronym{EBCDIC} to @acronym{ASCII},
8063 using the conversion table specified by @acronym{POSIX}.
8064 This provides a 1:1 translation for all 256 bytes.
8065
8066 @item ebcdic
8067 @opindex ebcdic@r{, converting to}
8068 Convert @acronym{ASCII} to @acronym{EBCDIC}.
8069 This is the inverse of the @samp{ascii} conversion.
8070
8071 @item ibm
8072 @opindex alternate ebcdic@r{, converting to}
8073 Convert @acronym{ASCII} to alternate @acronym{EBCDIC},
8074 using the alternate conversion table specified by @acronym{POSIX}.
8075 This is not a 1:1 translation, but reflects common historical practice
8076 for @samp{~}, @samp{[}, and @samp{]}.
8077
8078 The @samp{ascii}, @samp{ebcdic}, and @samp{ibm} conversions are
8079 mutually exclusive.
8080
8081 @item block
8082 @opindex block @r{(space-padding)}
8083 For each line in the input, output @samp{cbs} bytes, replacing the
8084 input newline with a space and padding with spaces as necessary.
8085
8086 @item unblock
8087 @opindex unblock
8088 Remove any trailing spaces in each @samp{cbs}-sized input block,
8089 and append a newline.
8090
8091 The @samp{block} and @samp{unblock} conversions are mutually exclusive.
8092
8093 @item lcase
8094 @opindex lcase@r{, converting to}
8095 Change uppercase letters to lowercase.
8096
8097 @item ucase
8098 @opindex ucase@r{, converting to}
8099 Change lowercase letters to uppercase.
8100
8101 The @samp{lcase} and @samp{ucase} conversions are mutually exclusive.
8102
8103 @item swab
8104 @opindex swab @r{(byte-swapping)}
8105 @cindex byte-swapping
8106 Swap every pair of input bytes.  @sc{gnu} @command{dd}, unlike others, works
8107 when an odd number of bytes are read---the last byte is simply copied
8108 (since there is nothing to swap it with).
8109
8110 @item sync
8111 @opindex sync @r{(padding with @acronym{ASCII} @sc{nul}s)}
8112 Pad every input block to size of @samp{ibs} with trailing zero bytes.
8113 When used with @samp{block} or @samp{unblock}, pad with spaces instead of
8114 zero bytes.
8115
8116 @end table
8117
8118 The following ``conversions'' are really file flags
8119 and don't affect internal processing:
8120
8121 @table @samp
8122 @item excl
8123 @opindex excl
8124 @cindex creating output file, requiring
8125 Fail if the output file already exists; @command{dd} must create the
8126 output file itself.
8127
8128 @item nocreat
8129 @opindex nocreat
8130 @cindex creating output file, avoiding
8131 Do not create the output file; the output file must already exist.
8132
8133 The @samp{excl} and @samp{nocreat} conversions are mutually exclusive.
8134
8135 @item notrunc
8136 @opindex notrunc
8137 @cindex truncating output file, avoiding
8138 Do not truncate the output file.
8139
8140 @item noerror
8141 @opindex noerror
8142 @cindex read errors, ignoring
8143 Continue after read errors.
8144
8145 @item fdatasync
8146 @opindex fdatasync
8147 @cindex synchronized data writes, before finishing
8148 Synchronize output data just before finishing.  This forces a physical
8149 write of output data.
8150
8151 @item fsync
8152 @opindex fsync
8153 @cindex synchronized data and metadata writes, before finishing
8154 Synchronize output data and metadata just before finishing.  This
8155 forces a physical write of output data and metadata.
8156
8157 @end table
8158
8159 @item iflag=@var{flag}[,@var{flag}]@dots{}
8160 @opindex iflag
8161 Access the input file using the flags specified by the @var{flag}
8162 argument(s).  (No spaces around any comma(s).)
8163
8164 @item oflag=@var{flag}[,@var{flag}]@dots{}
8165 @opindex oflag
8166 Access the output file using the flags specified by the @var{flag}
8167 argument(s).  (No spaces around any comma(s).)
8168
8169 Here are the flags.  Not every flag is supported on every operating
8170 system.
8171
8172 @table @samp
8173
8174 @item append
8175 @opindex append
8176 @cindex appending to the output file
8177 Write in append mode, so that even if some other process is writing to
8178 this file, every @command{dd} write will append to the current
8179 contents of the file.  This flag makes sense only for output.
8180 If you combine this flag with the @samp{of=@var{file}} operand,
8181 you should also specify @samp{conv=notrunc} unless you want the
8182 output file to be truncated before being appended to.
8183
8184 @item cio
8185 @opindex cio
8186 @cindex concurrent I/O
8187 Use concurrent I/O mode for data.  This mode performs direct I/O
8188 and drops the @acronym{POSIX} requirement to serialize all I/O to the same file.
8189 A file cannot be opened in CIO mode and with a standard open at the
8190 same time.
8191
8192 @item direct
8193 @opindex direct
8194 @cindex direct I/O
8195 Use direct I/O for data, avoiding the buffer cache.
8196 Note that the kernel may impose restrictions on read or write buffer sizes.
8197 For example, with an ext4 destination file system and a linux-based kernel,
8198 using @samp{oflag=direct} will cause writes to fail with @code{EINVAL} if the
8199 output buffer size is not a multiple of 512.
8200
8201 @item directory
8202 @opindex directory
8203 @cindex directory I/O
8204
8205 Fail unless the file is a directory.  Most operating systems do not
8206 allow I/O to a directory, so this flag has limited utility.
8207
8208 @item dsync
8209 @opindex dsync
8210 @cindex synchronized data reads
8211 Use synchronized I/O for data.  For the output file, this forces a
8212 physical write of output data on each write.  For the input file,
8213 this flag can matter when reading from a remote file that has been
8214 written to synchronously by some other process.  Metadata (e.g.,
8215 last-access and last-modified time) is not necessarily synchronized.
8216
8217 @item sync
8218 @opindex sync
8219 @cindex synchronized data and metadata I/O
8220 Use synchronized I/O for both data and metadata.
8221
8222 @item nocache
8223 @opindex nocache
8224 @cindex discarding file cache
8225 Discard the data cache for a file.
8226 When count=0 all cache is discarded,
8227 otherwise the cache is dropped for the processed
8228 portion of the file.  Also when count=0
8229 failure to discard the cache is diagnosed
8230 and reflected in the exit status.
8231 Here as some usage examples:
8232
8233 @example
8234 # Advise to drop cache for whole file
8235 dd if=ifile iflag=nocache count=0
8236
8237 # Ensure drop cache for the whole file
8238 dd of=ofile oflag=nocache conv=notrunc,fdatasync count=0
8239
8240 # Drop cache for part of file
8241 dd if=ifile iflag=nocache skip=10 count=10 of=/dev/null
8242
8243 # Stream data using just the read-ahead cache
8244 dd if=ifile of=ofile iflag=nocache oflag=nocache
8245 @end example
8246
8247 @item nonblock
8248 @opindex nonblock
8249 @cindex nonblocking I/O
8250 Use non-blocking I/O.
8251
8252 @item noatime
8253 @opindex noatime
8254 @cindex access time
8255 Do not update the file's access time.
8256 Some older file systems silently ignore this flag, so it is a good
8257 idea to test it on your files before relying on it.
8258
8259 @item noctty
8260 @opindex noctty
8261 @cindex controlling terminal
8262 Do not assign the file to be a controlling terminal for @command{dd}.
8263 This has no effect when the file is not a terminal.
8264 On many hosts (e.g., @acronym{GNU}/Linux hosts), this option has no effect
8265 at all.
8266
8267 @item nofollow
8268 @opindex nofollow
8269 @cindex symbolic links, following
8270 Do not follow symbolic links.
8271
8272 @item nolinks
8273 @opindex nolinks
8274 @cindex hard links
8275 Fail if the file has multiple hard links.
8276
8277 @item binary
8278 @opindex binary
8279 @cindex binary I/O
8280 Use binary I/O.  This option has an effect only on nonstandard
8281 platforms that distinguish binary from text I/O.
8282
8283 @item text
8284 @opindex text
8285 @cindex text I/O
8286 Use text I/O.  Like @samp{binary}, this option has no effect on
8287 standard platforms.
8288
8289 @item fullblock
8290 @opindex fullblock
8291 Accumulate full blocks from input.  The @code{read} system call
8292 may return early if a full block is not available.
8293 When that happens, continue calling @code{read} to fill the remainder
8294 of the block.
8295 This flag can be used only with @code{iflag}.
8296
8297 @end table
8298
8299 These flags are not supported on all systems, and @samp{dd} rejects
8300 attempts to use them when they are not supported.  When reading from
8301 standard input or writing to standard output, the @samp{nofollow} and
8302 @samp{noctty} flags should not be specified, and the other flags
8303 (e.g., @samp{nonblock}) can affect how other processes behave with the
8304 affected file descriptors, even after @command{dd} exits.
8305
8306 @end table
8307
8308 @cindex multipliers after numbers
8309 The numeric-valued strings above (@var{bytes} and @var{blocks}) can be
8310 followed by a multiplier: @samp{b}=512, @samp{c}=1,
8311 @samp{w}=2, @samp{x@var{m}}=@var{m}, or any of the
8312 standard block size suffixes like @samp{k}=1024 (@pxref{Block size}).
8313
8314 Any block size you specify via @samp{bs=}, @samp{ibs=}, @samp{obs=}, @samp{cbs=}
8315 should not be too large---values larger than a few megabytes
8316 are generally wasteful or (as in the gigabyte..exabyte case) downright
8317 counterproductive or error-inducing.
8318
8319 Use different @command{dd} invocations to use different block sizes for
8320 skipping and I/O@.  For example, the following shell commands copy data
8321 in 512 KiB blocks between a disk and a tape, but do not save or restore a
8322 4 KiB label at the start of the disk:
8323
8324 @example
8325 disk=/dev/rdsk/c0t1d0s2
8326 tape=/dev/rmt/0
8327
8328 # Copy all but the label from disk to tape.
8329 (dd bs=4k skip=1 count=0 && dd bs=512k) <$disk >$tape
8330
8331 # Copy from tape back to disk, but leave the disk label alone.
8332 (dd bs=4k seek=1 count=0 && dd bs=512k) <$tape >$disk
8333 @end example
8334
8335 Sending an @samp{INFO} signal to a running @command{dd}
8336 process makes it print I/O statistics to standard error
8337 and then resume copying.  In the example below,
8338 @command{dd} is run in the background to copy 10 million blocks.
8339 The @command{kill} command makes it output intermediate I/O statistics,
8340 and when @command{dd} completes normally or is killed by the
8341 @code{SIGINT} signal, it outputs the final statistics.
8342
8343 @example
8344 $ dd if=/dev/zero of=/dev/null count=10MB & pid=$!
8345 $ kill -s INFO $pid; wait $pid
8346 3385223+0 records in
8347 3385223+0 records out
8348 1733234176 bytes (1.7 GB) copied, 6.42173 seconds, 270 MB/s
8349 10000000+0 records in
8350 10000000+0 records out
8351 5120000000 bytes (5.1 GB) copied, 18.913 seconds, 271 MB/s
8352 @end example
8353
8354 @vindex POSIXLY_CORRECT
8355 On systems lacking the @samp{INFO} signal @command{dd} responds to the
8356 @samp{USR1} signal instead, unless the @env{POSIXLY_CORRECT}
8357 environment variable is set.
8358
8359 @exitstatus
8360
8361
8362 @node install invocation
8363 @section @command{install}: Copy files and set attributes
8364
8365 @pindex install
8366 @cindex copying files and setting attributes
8367
8368 @command{install} copies files while setting their file mode bits and, if
8369 possible, their owner and group.  Synopses:
8370
8371 @example
8372 install [@var{option}]@dots{} [-T] @var{source} @var{dest}
8373 install [@var{option}]@dots{} @var{source}@dots{} @var{directory}
8374 install [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
8375 install [@var{option}]@dots{} -d @var{directory}@dots{}
8376 @end example
8377
8378 @itemize @bullet
8379 @item
8380 If two file names are given, @command{install} copies the first file to the
8381 second.
8382
8383 @item
8384 If the @option{--target-directory} (@option{-t}) option is given, or
8385 failing that if the last file is a directory and the
8386 @option{--no-target-directory} (@option{-T}) option is not given,
8387 @command{install} copies each @var{source} file to the specified
8388 directory, using the @var{source}s' names.
8389
8390 @item
8391 If the @option{--directory} (@option{-d}) option is given,
8392 @command{install} creates each @var{directory} and any missing parent
8393 directories.  Parent directories are created with mode
8394 @samp{u=rwx,go=rx} (755), regardless of the @option{-m} option or the
8395 current umask.  @xref{Directory Setuid and Setgid}, for how the
8396 set-user-ID and set-group-ID bits of parent directories are inherited.
8397 @end itemize
8398
8399 @cindex Makefiles, installing programs in
8400 @command{install} is similar to @command{cp}, but allows you to control the
8401 attributes of destination files.  It is typically used in Makefiles to
8402 copy programs into their destination directories.  It refuses to copy
8403 files onto themselves.
8404
8405 @cindex extended attributes, xattr
8406 @command{install} never preserves extended attributes (xattr).
8407
8408 The program accepts the following options.  Also see @ref{Common options}.
8409
8410 @table @samp
8411
8412 @optBackup
8413
8414 @item -C
8415 @itemx --compare
8416 @opindex -C
8417 @opindex --compare
8418 Compare each pair of source and destination files, and if the destination has
8419 identical content and any specified owner, group, permissions, and possibly
8420 SELinux context, then do not modify the destination at all.
8421
8422 @item -c
8423 @opindex -c
8424 Ignored; for compatibility with old Unix versions of @command{install}.
8425
8426 @item -D
8427 @opindex -D
8428 Create any missing parent directories of @var{dest},
8429 then copy @var{source} to @var{dest}.
8430 This option is ignored if a destination directory is specified
8431 via @option{--target-directory=DIR}.
8432
8433 @item -d
8434 @itemx --directory
8435 @opindex -d
8436 @opindex --directory
8437 @cindex directories, creating with given attributes
8438 @cindex parent directories, creating missing
8439 @cindex leading directories, creating missing
8440 Create any missing parent directories, giving them the default
8441 attributes.  Then create each given directory, setting their owner,
8442 group and mode as given on the command line or to the defaults.
8443
8444 @item -g @var{group}
8445 @itemx --group=@var{group}
8446 @opindex -g
8447 @opindex --group
8448 @cindex group ownership of installed files, setting
8449 Set the group ownership of installed files or directories to
8450 @var{group}.  The default is the process's current group.  @var{group}
8451 may be either a group name or a numeric group ID.
8452
8453 @item -m @var{mode}
8454 @itemx --mode=@var{mode}
8455 @opindex -m
8456 @opindex --mode
8457 @cindex permissions of installed files, setting
8458 Set the file mode bits for the installed file or directory to @var{mode},
8459 which can be either an octal number, or a symbolic mode as in
8460 @command{chmod}, with @samp{a=} (no access allowed to anyone) as the
8461 point of departure (@pxref{File permissions}).
8462 The default mode is @samp{u=rwx,go=rx,a-s}---read, write, and
8463 execute for the owner, read and execute for group and other, and with
8464 set-user-ID and set-group-ID disabled.
8465 This default is not quite the same as @samp{755}, since it disables
8466 instead of preserving set-user-ID and set-group-ID on directories.
8467 @xref{Directory Setuid and Setgid}.
8468
8469 @item -o @var{owner}
8470 @itemx --owner=@var{owner}
8471 @opindex -o
8472 @opindex --owner
8473 @cindex ownership of installed files, setting
8474 @cindex appropriate privileges
8475 @vindex root @r{as default owner}
8476 If @command{install} has appropriate privileges (is run as root), set the
8477 ownership of installed files or directories to @var{owner}.  The default
8478 is @code{root}.  @var{owner} may be either a user name or a numeric user
8479 ID.
8480
8481 @item --preserve-context
8482 @opindex --preserve-context
8483 @cindex SELinux
8484 @cindex security context
8485 Preserve the SELinux security context of files and directories.
8486 Failure to preserve the context in all of the files or directories
8487 will result in an exit status of 1.  If SELinux is disabled then
8488 print a warning and ignore the option.
8489
8490 @item -p
8491 @itemx --preserve-timestamps
8492 @opindex -p
8493 @opindex --preserve-timestamps
8494 @cindex timestamps of installed files, preserving
8495 Set the time of last access and the time of last modification of each
8496 installed file to match those of each corresponding original file.
8497 When a file is installed without this option, its last access and
8498 last modification times are both set to the time of installation.
8499 This option is useful if you want to use the last modification times
8500 of installed files to keep track of when they were last built as opposed
8501 to when they were last installed.
8502
8503 @item -s
8504 @itemx --strip
8505 @opindex -s
8506 @opindex --strip
8507 @cindex symbol table information, stripping
8508 @cindex stripping symbol table information
8509 Strip the symbol tables from installed binary executables.
8510
8511 @itemx --strip-program=@var{program}
8512 @opindex --strip-program
8513 @cindex symbol table information, stripping, program
8514 Program used to strip binaries.
8515
8516 @optBackupSuffix
8517
8518 @optTargetDirectory
8519
8520 @optNoTargetDirectory
8521
8522 @item -v
8523 @itemx --verbose
8524 @opindex -v
8525 @opindex --verbose
8526 Print the name of each file before copying it.
8527
8528 @item -Z @var{context}
8529 @itemx --context=@var{context}
8530 @opindex -Z
8531 @opindex --context
8532 @cindex SELinux
8533 @cindex security context
8534 Set the default SELinux security context to be used for any
8535 created files and directories.  If SELinux is disabled then
8536 print a warning and ignore the option.
8537
8538 @end table
8539
8540 @exitstatus
8541
8542
8543 @node mv invocation
8544 @section @command{mv}: Move (rename) files
8545
8546 @pindex mv
8547
8548 @command{mv} moves or renames files (or directories).  Synopses:
8549
8550 @example
8551 mv [@var{option}]@dots{} [-T] @var{source} @var{dest}
8552 mv [@var{option}]@dots{} @var{source}@dots{} @var{directory}
8553 mv [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
8554 @end example
8555
8556 @itemize @bullet
8557 @item
8558 If two file names are given, @command{mv} moves the first file to the
8559 second.
8560
8561 @item
8562 If the @option{--target-directory} (@option{-t}) option is given, or
8563 failing that if the last file is a directory and the
8564 @option{--no-target-directory} (@option{-T}) option is not given,
8565 @command{mv} moves each @var{source} file to the specified
8566 directory, using the @var{source}s' names.
8567 @end itemize
8568
8569 @command{mv} can move any type of file from one file system to another.
8570 Prior to version @code{4.0} of the fileutils,
8571 @command{mv} could move only regular files between file systems.
8572 For example, now @command{mv} can move an entire directory hierarchy
8573 including special device files from one partition to another.  It first
8574 uses some of the same code that's used by @code{cp -a} to copy the
8575 requested directories and files, then (assuming the copy succeeded)
8576 it removes the originals.  If the copy fails, then the part that was
8577 copied to the destination partition is removed.  If you were to copy
8578 three directories from one partition to another and the copy of the first
8579 directory succeeded, but the second didn't, the first would be left on
8580 the destination partition and the second and third would be left on the
8581 original partition.
8582
8583 @cindex extended attributes, xattr
8584 @command{mv} always tries to copy extended attributes (xattr), which may
8585 include SELinux context, ACLs or Capabilities.
8586 Upon failure all but @samp{Operation not supported} warnings are output.
8587
8588 @cindex prompting, and @command{mv}
8589 If a destination file exists but is normally unwritable, standard input
8590 is a terminal, and the @option{-f} or @option{--force} option is not given,
8591 @command{mv} prompts the user for whether to replace the file.  (You might
8592 own the file, or have write permission on its directory.)  If the
8593 response is not affirmative, the file is skipped.
8594
8595 @emph{Warning}: Avoid specifying a source name with a trailing slash,
8596 when it might be a symlink to a directory.
8597 Otherwise, @command{mv} may do something very surprising, since
8598 its behavior depends on the underlying rename system call.
8599 On a system with a modern Linux-based kernel, it fails with
8600 @code{errno=ENOTDIR}.
8601 However, on other systems (at least FreeBSD 6.1 and Solaris 10) it silently
8602 renames not the symlink but rather the directory referenced by the symlink.
8603 @xref{Trailing slashes}.
8604
8605 The program accepts the following options.  Also see @ref{Common options}.
8606
8607 @table @samp
8608
8609 @optBackup
8610
8611 @item -f
8612 @itemx --force
8613 @opindex -f
8614 @opindex --force
8615 @cindex prompts, omitting
8616 Do not prompt the user before removing a destination file.
8617 @macro mvOptsIfn
8618 If you specify more than one of the @option{-i}, @option{-f}, @option{-n}
8619 options, only the final one takes effect.
8620 @end macro
8621 @mvOptsIfn
8622
8623 @item -i
8624 @itemx --interactive
8625 @opindex -i
8626 @opindex --interactive
8627 @cindex prompts, forcing
8628 Prompt whether to overwrite each existing destination file, regardless
8629 of its permissions.
8630 If the response is not affirmative, the file is skipped.
8631 @mvOptsIfn
8632
8633 @item -n
8634 @itemx --no-clobber
8635 @opindex -n
8636 @opindex --no-clobber
8637 @cindex prompts, omitting
8638 Do not overwrite an existing file.
8639 @mvOptsIfn
8640 This option is mutually exclusive with @option{-b} or @option{--backup} option.
8641
8642 @item -u
8643 @itemx --update
8644 @opindex -u
8645 @opindex --update
8646 @cindex newer files, moving only
8647 Do not move a non-directory that has an existing destination with the
8648 same or newer modification time.
8649 If the move is across file system boundaries, the comparison is to the
8650 source time stamp truncated to the resolutions of the destination file
8651 system and of the system calls used to update time stamps; this avoids
8652 duplicate work if several @samp{mv -u} commands are executed with the
8653 same source and destination.
8654
8655 @item -v
8656 @itemx --verbose
8657 @opindex -v
8658 @opindex --verbose
8659 Print the name of each file before moving it.
8660
8661 @optStripTrailingSlashes
8662
8663 @optBackupSuffix
8664
8665 @optTargetDirectory
8666
8667 @optNoTargetDirectory
8668
8669 @end table
8670
8671 @exitstatus
8672
8673
8674 @node rm invocation
8675 @section @command{rm}: Remove files or directories
8676
8677 @pindex rm
8678 @cindex removing files or directories
8679
8680 @command{rm} removes each given @var{file}.  By default, it does not remove
8681 directories.  Synopsis:
8682
8683 @example
8684 rm [@var{option}]@dots{} [@var{file}]@dots{}
8685 @end example
8686
8687 @cindex prompting, and @command{rm}
8688 If the @option{-I} or @option{--interactive=once} option is given,
8689 and there are more than three files or the @option{-r}, @option{-R},
8690 or @option{--recursive} are given, then @command{rm} prompts the user
8691 for whether to proceed with the entire operation.  If the response is
8692 not affirmative, the entire command is aborted.
8693
8694 Otherwise, if a file is unwritable, standard input is a terminal, and
8695 the @option{-f} or @option{--force} option is not given, or the
8696 @option{-i} or @option{--interactive=always} option @emph{is} given,
8697 @command{rm} prompts the user for whether to remove the file.
8698 If the response is not affirmative, the file is skipped.
8699
8700 Any attempt to remove a file whose last file name component is
8701 @file{.} or @file{..} is rejected without any prompting.
8702
8703 @emph{Warning}: If you use @command{rm} to remove a file, it is usually
8704 possible to recover the contents of that file.  If you want more assurance
8705 that the contents are truly unrecoverable, consider using @command{shred}.
8706
8707 The program accepts the following options.  Also see @ref{Common options}.
8708
8709 @table @samp
8710
8711 @item -f
8712 @itemx --force
8713 @opindex -f
8714 @opindex --force
8715 Ignore nonexistent files and never prompt the user.
8716 Ignore any previous @option{--interactive} (@option{-i}) option.
8717
8718 @item -i
8719 @opindex -i
8720 Prompt whether to remove each file.
8721 If the response is not affirmative, the file is skipped.
8722 Ignore any previous @option{--force} (@option{-f}) option.
8723 Equivalent to @option{--interactive=always}.
8724
8725 @item -I
8726 @opindex -I
8727 Prompt once whether to proceed with the command, if more than three
8728 files are named or if a recursive removal is requested.  Ignore any
8729 previous @option{--force} (@option{-f}) option.  Equivalent to
8730 @option{--interactive=once}.
8731
8732 @itemx --interactive [=@var{when}]
8733 @opindex --interactive
8734 Specify when to issue an interactive prompt.  @var{when} may be
8735 omitted, or one of:
8736 @itemize @bullet
8737 @item never
8738 @vindex never @r{interactive option}
8739 - Do not prompt at all.
8740 @item once
8741 @vindex once @r{interactive option}
8742 - Prompt once if more than three files are named or if a recursive
8743 removal is requested.  Equivalent to @option{-I}.
8744 @item always
8745 @vindex always @r{interactive option}
8746 - Prompt for every file being removed.  Equivalent to @option{-i}.
8747 @end itemize
8748 @option{--interactive} with no @var{when} is equivalent to
8749 @option{--interactive=always}.
8750
8751 @itemx --one-file-system
8752 @opindex --one-file-system
8753 @cindex one file system, restricting @command{rm} to
8754 When removing a hierarchy recursively, skip any directory that is on a
8755 file system different from that of the corresponding command line argument.
8756
8757 @cindex bind mount
8758 This option is useful when removing a build ``chroot'' hierarchy,
8759 which normally contains no valuable data.  However, it is not uncommon
8760 to bind-mount @file{/home} into such a hierarchy, to make it easier to
8761 use one's start-up file.  The catch is that it's easy to forget to
8762 unmount @file{/home}.  Then, when you use @command{rm -rf} to remove
8763 your normally throw-away chroot, that command will remove everything
8764 under @file{/home}, too.
8765 Use the @option{--one-file-system} option, and it will
8766 warn about and skip directories on other file systems.
8767 Of course, this will not save your @file{/home} if it and your
8768 chroot happen to be on the same file system.
8769
8770 @itemx --preserve-root
8771 @opindex --preserve-root
8772 @cindex root directory, disallow recursive destruction
8773 Fail upon any attempt to remove the root directory, @file{/},
8774 when used with the @option{--recursive} option.
8775 This is the default behavior.
8776 @xref{Treating / specially}.
8777
8778 @itemx --no-preserve-root
8779 @opindex --no-preserve-root
8780 @cindex root directory, allow recursive destruction
8781 Do not treat @file{/} specially when removing recursively.
8782 This option is not recommended unless you really want to
8783 remove all the files on your computer.
8784 @xref{Treating / specially}.
8785
8786 @item -r
8787 @itemx -R
8788 @itemx --recursive
8789 @opindex -r
8790 @opindex -R
8791 @opindex --recursive
8792 @cindex directories, removing (recursively)
8793 Remove the listed directories and their contents recursively.
8794
8795 @item -v
8796 @itemx --verbose
8797 @opindex -v
8798 @opindex --verbose
8799 Print the name of each file before removing it.
8800
8801 @end table
8802
8803 @cindex files beginning with @samp{-}, removing
8804 @cindex @samp{-}, removing files beginning with
8805 One common question is how to remove files whose names begin with a
8806 @samp{-}.  @sc{gnu} @command{rm}, like every program that uses the @code{getopt}
8807 function to parse its arguments, lets you use the @samp{--} option to
8808 indicate that all following arguments are non-options.  To remove a file
8809 called @file{-f} in the current directory, you could type either:
8810
8811 @example
8812 rm -- -f
8813 @end example
8814
8815 @noindent
8816 or:
8817
8818 @example
8819 rm ./-f
8820 @end example
8821
8822 @opindex - @r{and Unix @command{rm}}
8823 The Unix @command{rm} program's use of a single @samp{-} for this purpose
8824 predates the development of the getopt standard syntax.
8825
8826 @exitstatus
8827
8828
8829 @node shred invocation
8830 @section @command{shred}: Remove files more securely
8831
8832 @pindex shred
8833 @cindex data, erasing
8834 @cindex erasing data
8835
8836 @command{shred} overwrites devices or files, to help prevent even
8837 very expensive hardware from recovering the data.
8838
8839 Ordinarily when you remove a file (@pxref{rm invocation}), the data is
8840 not actually destroyed.  Only the index listing where the file is
8841 stored is destroyed, and the storage is made available for reuse.
8842 There are undelete utilities that will attempt to reconstruct the index
8843 and can bring the file back if the parts were not reused.
8844
8845 On a busy system with a nearly-full drive, space can get reused in a few
8846 seconds.  But there is no way to know for sure.  If you have sensitive
8847 data, you may want to be sure that recovery is not possible by actually
8848 overwriting the file with non-sensitive data.
8849
8850 However, even after doing that, it is possible to take the disk back
8851 to a laboratory and use a lot of sensitive (and expensive) equipment
8852 to look for the faint ``echoes'' of the original data underneath the
8853 overwritten data.  If the data has only been overwritten once, it's not
8854 even that hard.
8855
8856 The best way to remove something irretrievably is to destroy the media
8857 it's on with acid, melt it down, or the like.  For cheap removable media
8858 like floppy disks, this is the preferred method.  However, hard drives
8859 are expensive and hard to melt, so the @command{shred} utility tries
8860 to achieve a similar effect non-destructively.
8861
8862 This uses many overwrite passes, with the data patterns chosen to
8863 maximize the damage they do to the old data.  While this will work on
8864 floppies, the patterns are designed for best effect on hard drives.
8865 For more details, see the source code and Peter Gutmann's paper
8866 @uref{http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html,
8867 @cite{Secure Deletion of Data from Magnetic and Solid-State Memory}},
8868 from the proceedings of the Sixth @acronym{USENIX} Security Symposium (San Jose,
8869 California, July 22--25, 1996).
8870
8871 @strong{Please note} that @command{shred} relies on a very important assumption:
8872 that the file system overwrites data in place.  This is the traditional
8873 way to do things, but many modern file system designs do not satisfy this
8874 assumption.  Exceptions include:
8875
8876 @itemize @bullet
8877
8878 @item
8879 Log-structured or journaled file systems, such as those supplied with
8880 AIX and Solaris, and JFS, ReiserFS, XFS, Ext3 (in @code{data=journal} mode),
8881 BFS, NTFS, etc.@: when they are configured to journal @emph{data}.
8882
8883 @item
8884 File systems that write redundant data and carry on even if some writes
8885 fail, such as RAID-based file systems.
8886
8887 @item
8888 File systems that make snapshots, such as Network Appliance's NFS server.
8889
8890 @item
8891 File systems that cache in temporary locations, such as NFS version 3
8892 clients.
8893
8894 @item
8895 Compressed file systems.
8896 @end itemize
8897
8898 In the particular case of ext3 file systems, the above disclaimer applies (and
8899 @command{shred} is thus of limited effectiveness) only in @code{data=journal}
8900 mode, which journals file data in addition to just metadata. In both
8901 the @code{data=ordered} (default) and @code{data=writeback} modes,
8902 @command{shred} works as usual.  Ext3 journaling modes can be changed
8903 by adding the @code{data=something} option to the mount options for a
8904 particular file system in the @file{/etc/fstab} file, as documented in
8905 the mount man page (man mount).
8906
8907 If you are not sure how your file system operates, then you should assume
8908 that it does not overwrite data in place, which means that shred cannot
8909 reliably operate on regular files in your file system.
8910
8911 Generally speaking, it is more reliable to shred a device than a file,
8912 since this bypasses the problem of file system design mentioned above.
8913 However, even shredding devices is not always completely reliable.  For
8914 example, most disks map out bad sectors invisibly to the application; if
8915 the bad sectors contain sensitive data, @command{shred} won't be able to
8916 destroy it.
8917
8918 @command{shred} makes no attempt to detect or report this problem, just as
8919 it makes no attempt to do anything about backups.  However, since it is
8920 more reliable to shred devices than files, @command{shred} by default does
8921 not truncate or remove the output file.  This default is more suitable
8922 for devices, which typically cannot be truncated and should not be
8923 removed.
8924
8925 Finally, consider the risk of backups and mirrors.
8926 File system backups and remote mirrors may contain copies of the
8927 file that cannot be removed, and that will allow a shredded file
8928 to be recovered later.  So if you keep any data you may later want
8929 to destroy using @command{shred}, be sure that it is not backed up or mirrored.
8930
8931 @example
8932 shred [@var{option}]@dots{} @var{file}[@dots{}]
8933 @end example
8934
8935 The program accepts the following options.  Also see @ref{Common options}.
8936
8937 @table @samp
8938
8939 @item -f
8940 @itemx --force
8941 @opindex -f
8942 @opindex --force
8943 @cindex force deletion
8944 Override file permissions if necessary to allow overwriting.
8945
8946 @item -@var{number}
8947 @itemx -n @var{number}
8948 @itemx --iterations=@var{number}
8949 @opindex -n @var{number}
8950 @opindex --iterations=@var{number}
8951 @cindex iterations, selecting the number of
8952 By default, @command{shred} uses @value{SHRED_DEFAULT_PASSES} passes of
8953 overwrite. You can reduce this to save time, or increase it if you think it's
8954 appropriate. After 25 passes all of the internal overwrite patterns will have
8955 been used at least once.
8956
8957 @item --random-source=@var{file}
8958 @opindex --random-source
8959 @cindex random source for shredding
8960 Use @var{file} as a source of random data used to overwrite and to
8961 choose pass ordering.  @xref{Random sources}.
8962
8963 @item -s @var{bytes}
8964 @itemx --size=@var{bytes}
8965 @opindex -s @var{bytes}
8966 @opindex --size=@var{bytes}
8967 @cindex size of file to shred
8968 Shred the first @var{bytes} bytes of the file.  The default is to shred
8969 the whole file.  @var{bytes} can be followed by a size specification like
8970 @samp{K}, @samp{M}, or @samp{G} to specify a multiple.  @xref{Block size}.
8971
8972 @item -u
8973 @itemx --remove
8974 @opindex -u
8975 @opindex --remove
8976 @cindex removing files after shredding
8977 After shredding a file, truncate it (if possible) and then remove it.
8978 If a file has multiple links, only the named links will be removed.
8979
8980 @item -v
8981 @itemx --verbose
8982 @opindex -v
8983 @opindex --verbose
8984 Display to standard error all status updates as sterilization proceeds.
8985
8986 @item -x
8987 @itemx --exact
8988 @opindex -x
8989 @opindex --exact
8990 By default, @command{shred} rounds the size of a regular file up to the next
8991 multiple of the file system block size to fully erase the last block
8992 of the file.
8993 Use @option{--exact} to suppress that behavior.
8994 Thus, by default if you shred a 10-byte regular file on a system with 512-byte
8995 blocks, the resulting file will be 512 bytes long.  With this option,
8996 shred does not increase the apparent size of the file.
8997
8998 @item -z
8999 @itemx --zero
9000 @opindex -z
9001 @opindex --zero
9002 Normally, the last pass that @command{shred} writes is made up of
9003 random data.  If this would be conspicuous on your hard drive (for
9004 example, because it looks like encrypted data), or you just think
9005 it's tidier, the @option{--zero} option adds an additional overwrite pass with
9006 all zero bits.  This is in addition to the number of passes specified
9007 by the @option{--iterations} option.
9008
9009 @end table
9010
9011 You might use the following command to erase all trace of the
9012 file system you'd created on the floppy disk in your first drive.
9013 That command takes about 20 minutes to erase a ``1.44MB'' (actually
9014 1440 KiB) floppy.
9015
9016 @example
9017 shred --verbose /dev/fd0
9018 @end example
9019
9020 Similarly, to erase all data on a selected partition of
9021 your hard disk, you could give a command like this:
9022
9023 @example
9024 shred --verbose /dev/sda5
9025 @end example
9026
9027 On modern disks, a single pass should be adequate,
9028 and it will take one third the time of the default three-pass approach.
9029
9030 @example
9031 # 1 pass, write pseudo-random data; 3x faster than the default
9032 shred --verbose -n1 /dev/sda5
9033 @end example
9034
9035 To be on the safe side, use at least one pass that overwrites using
9036 pseudo-random data.  I.e., don't be tempted to use @samp{-n0 --zero},
9037 in case some disk controller optimizes the process of writing blocks
9038 of all zeros, and thereby does not clear all bytes in a block.
9039 Some SSDs may do just that.
9040
9041 A @var{file} of @samp{-} denotes standard output.
9042 The intended use of this is to shred a removed temporary file.
9043 For example:
9044
9045 @example
9046 i=`mktemp`
9047 exec 3<>"$i"
9048 rm -- "$i"
9049 echo "Hello, world" >&3
9050 shred - >&3
9051 exec 3>-
9052 @end example
9053
9054 However, the command @samp{shred - >file} does not shred the contents
9055 of @var{file}, since the shell truncates @var{file} before invoking
9056 @command{shred}.  Use the command @samp{shred file} or (if using a
9057 Bourne-compatible shell) the command @samp{shred - 1<>file} instead.
9058
9059 @exitstatus
9060
9061
9062 @node Special file types
9063 @chapter Special file types
9064
9065 @cindex special file types
9066 @cindex file types, special
9067
9068 This chapter describes commands which create special types of files (and
9069 @command{rmdir}, which removes directories, one special file type).
9070
9071 @cindex special file types
9072 @cindex file types
9073 Although Unix-like operating systems have markedly fewer special file
9074 types than others, not @emph{everything} can be treated only as the
9075 undifferentiated byte stream of @dfn{normal files}.  For example, when a
9076 file is created or removed, the system must record this information,
9077 which it does in a @dfn{directory}---a special type of file.  Although
9078 you can read directories as normal files, if you're curious, in order
9079 for the system to do its job it must impose a structure, a certain
9080 order, on the bytes of the file.  Thus it is a ``special'' type of file.
9081
9082 Besides directories, other special file types include named pipes
9083 (FIFOs), symbolic links, sockets, and so-called @dfn{special files}.
9084
9085 @menu
9086 * link invocation::             Make a hard link via the link syscall
9087 * ln invocation::               Make links between files.
9088 * mkdir invocation::            Make directories.
9089 * mkfifo invocation::           Make FIFOs (named pipes).
9090 * mknod invocation::            Make block or character special files.
9091 * readlink invocation::         Print value of a symlink or canonical file name.
9092 * rmdir invocation::            Remove empty directories.
9093 * unlink invocation::           Remove files via the unlink syscall
9094 @end menu
9095
9096
9097 @node link invocation
9098 @section @command{link}: Make a hard link via the link syscall
9099
9100 @pindex link
9101 @cindex links, creating
9102 @cindex hard links, creating
9103 @cindex creating links (hard only)
9104
9105 @command{link} creates a single hard link at a time.
9106 It is a minimalist interface to the system-provided
9107 @code{link} function.  @xref{Hard Links, , , libc,
9108 The GNU C Library Reference Manual}.
9109 It avoids the bells and whistles of the more commonly-used
9110 @command{ln} command (@pxref{ln invocation}).
9111 Synopsis:
9112
9113 @example
9114 link @var{filename} @var{linkname}
9115 @end example
9116
9117 @var{filename} must specify an existing file, and @var{linkname}
9118 must specify a nonexistent entry in an existing directory.
9119 @command{link} simply calls @code{link (@var{filename}, @var{linkname})}
9120 to create the link.
9121
9122 On a @acronym{GNU} system, this command acts like @samp{ln --directory
9123 --no-target-directory @var{filename} @var{linkname}}.  However, the
9124 @option{--directory} and @option{--no-target-directory} options are
9125 not specified by @acronym{POSIX}, and the @command{link} command is
9126 more portable in practice.
9127
9128 If @var{filename} is a symbolic link, it is unspecified whether
9129 @var{linkname} will be a hard link to the symbolic link or to the
9130 target of the symbolic link.  Use @command{ln -P} or @command{ln -L}
9131 to specify which behavior is desired.
9132
9133 @exitstatus
9134
9135
9136 @node ln invocation
9137 @section @command{ln}: Make links between files
9138
9139 @pindex ln
9140 @cindex links, creating
9141 @cindex hard links, creating
9142 @cindex symbolic (soft) links, creating
9143 @cindex creating links (hard or soft)
9144
9145 @cindex file systems and hard links
9146 @command{ln} makes links between files.  By default, it makes hard links;
9147 with the @option{-s} option, it makes symbolic (or @dfn{soft}) links.
9148 Synopses:
9149
9150 @example
9151 ln [@var{option}]@dots{} [-T] @var{target} @var{linkname}
9152 ln [@var{option}]@dots{} @var{target}
9153 ln [@var{option}]@dots{} @var{target}@dots{} @var{directory}
9154 ln [@var{option}]@dots{} -t @var{directory} @var{target}@dots{}
9155 @end example
9156
9157 @itemize @bullet
9158
9159 @item
9160 If two file names are given, @command{ln} creates a link to the first
9161 file from the second.
9162
9163 @item
9164 If one @var{target} is given, @command{ln} creates a link to that file
9165 in the current directory.
9166
9167 @item
9168 If the @option{--target-directory} (@option{-t}) option is given, or
9169 failing that if the last file is a directory and the
9170 @option{--no-target-directory} (@option{-T}) option is not given,
9171 @command{ln} creates a link to each @var{target} file in the specified
9172 directory, using the @var{target}s' names.
9173
9174 @end itemize
9175
9176 Normally @command{ln} does not remove existing files.  Use the
9177 @option{--force} (@option{-f}) option to remove them unconditionally,
9178 the @option{--interactive} (@option{-i}) option to remove them
9179 conditionally, and the @option{--backup} (@option{-b}) option to
9180 rename them.
9181
9182 @cindex hard link, defined
9183 @cindex inode, and hard links
9184 A @dfn{hard link} is another name for an existing file; the link and the
9185 original are indistinguishable.  Technically speaking, they share the
9186 same inode, and the inode contains all the information about a
9187 file---indeed, it is not incorrect to say that the inode @emph{is} the
9188 file.  Most systems prohibit making a hard link to
9189 a directory; on those where it is allowed, only the super-user can do
9190 so (and with caution, since creating a cycle will cause problems to many
9191 other utilities).  Hard links cannot cross file system boundaries.  (These
9192 restrictions are not mandated by @acronym{POSIX}, however.)
9193
9194 @cindex dereferencing symbolic links
9195 @cindex symbolic link, defined
9196 @dfn{Symbolic links} (@dfn{symlinks} for short), on the other hand, are
9197 a special file type (which not all kernels support: System V release 3
9198 (and older) systems lack symlinks) in which the link file actually
9199 refers to a different file, by name.  When most operations (opening,
9200 reading, writing, and so on) are passed the symbolic link file, the
9201 kernel automatically @dfn{dereferences} the link and operates on the
9202 target of the link.  But some operations (e.g., removing) work on the
9203 link file itself, rather than on its target.  The owner and group of a
9204 symlink are not significant to file access performed through
9205 the link, but do have implications on deleting a symbolic link from a
9206 directory with the restricted deletion bit set.  On the GNU system,
9207 the mode of a symlink has no significance and cannot be changed, but
9208 on some BSD systems, the mode can be changed and will affect whether
9209 the symlink will be traversed in file name resolution.  @xref{Symbolic Links,,,
9210 libc, The GNU C Library Reference Manual}.
9211
9212 Symbolic links can contain arbitrary strings; a @dfn{dangling symlink}
9213 occurs when the string in the symlink does not resolve to a file.
9214 There are no restrictions against creating dangling symbolic links.
9215 There are trade-offs to using absolute or relative symlinks.  An
9216 absolute symlink always points to the same file, even if the directory
9217 containing the link is moved.  However, if the symlink is visible from
9218 more than one machine (such as on a networked file system), the file
9219 pointed to might not always be the same.  A relative symbolic link is
9220 resolved in relation to the directory that contains the link, and is
9221 often useful in referring to files on the same device without regards
9222 to what name that device is mounted on when accessed via networked
9223 machines.
9224
9225 When creating a relative symlink in a different location than the
9226 current directory, the resolution of the symlink will be different
9227 than the resolution of the same string from the current directory.
9228 Therefore, many users prefer to first change directories to the
9229 location where the relative symlink will be created, so that
9230 tab-completion or other file resolution will find the same target as
9231 what will be placed in the symlink.
9232
9233 The program accepts the following options.  Also see @ref{Common options}.
9234
9235 @table @samp
9236
9237 @optBackup
9238
9239 @item -d
9240 @itemx -F
9241 @itemx --directory
9242 @opindex -d
9243 @opindex -F
9244 @opindex --directory
9245 @cindex hard links to directories
9246 Allow users with appropriate privileges to attempt to make hard links
9247 to directories.
9248 However, note that this will probably fail due to
9249 system restrictions, even for the super-user.
9250
9251 @item -f
9252 @itemx --force
9253 @opindex -f
9254 @opindex --force
9255 Remove existing destination files.
9256
9257 @item -i
9258 @itemx --interactive
9259 @opindex -i
9260 @opindex --interactive
9261 @cindex prompting, and @command{ln}
9262 Prompt whether to remove existing destination files.
9263
9264 @item -L
9265 @itemx --logical
9266 @opindex -L
9267 @opindex --logical
9268 If @option{-s} is not in effect, and the source file is a symbolic
9269 link, create the hard link to the file referred to by the symbolic
9270 link, rather than the symbolic link itself.
9271
9272 @item -n
9273 @itemx --no-dereference
9274 @opindex -n
9275 @opindex --no-dereference
9276 Do not treat the last operand specially when it is a symbolic link to
9277 a directory.  Instead, treat it as if it were a normal file.
9278
9279 When the destination is an actual directory (not a symlink to one),
9280 there is no ambiguity.  The link is created in that directory.
9281 But when the specified destination is a symlink to a directory,
9282 there are two ways to treat the user's request.  @command{ln} can
9283 treat the destination just as it would a normal directory and create
9284 the link in it.  On the other hand, the destination can be viewed as a
9285 non-directory---as the symlink itself.  In that case, @command{ln}
9286 must delete or backup that symlink before creating the new link.
9287 The default is to treat a destination that is a symlink to a directory
9288 just like a directory.
9289
9290 This option is weaker than the @option{--no-target-directory}
9291 (@option{-T}) option, so it has no effect if both options are given.
9292
9293 @item -P
9294 @itemx --physical
9295 @opindex -P
9296 @opindex --physical
9297 If @option{-s} is not in effect, and the source file is a symbolic
9298 link, create the hard link to the symbolic link itself.  On platforms
9299 where this is not supported by the kernel, this option creates a
9300 symbolic link with identical contents; since symbolic link contents
9301 cannot be edited, any file name resolution performed through either
9302 link will be the same as if a hard link had been created.
9303
9304 @item -s
9305 @itemx --symbolic
9306 @opindex -s
9307 @opindex --symbolic
9308 Make symbolic links instead of hard links.  This option merely produces
9309 an error message on systems that do not support symbolic links.
9310
9311 @optBackupSuffix
9312
9313 @optTargetDirectory
9314
9315 @optNoTargetDirectory
9316
9317 @item -v
9318 @itemx --verbose
9319 @opindex -v
9320 @opindex --verbose
9321 Print the name of each file after linking it successfully.
9322
9323 @end table
9324
9325 @cindex hard links to symbolic links
9326 @cindex symbolic links and @command{ln}
9327 If @option{-L} and @option{-P} are both given, the last one takes
9328 precedence.  If @option{-s} is also given, @option{-L} and @option{-P}
9329 are silently ignored.  If neither option is given, then this
9330 implementation defaults to @option{-P} if the system @code{link} supports
9331 hard links to symbolic links (such as the GNU system), and @option{-L}
9332 if @code{link} follows symbolic links (such as on BSD).
9333
9334 @exitstatus
9335
9336 Examples:
9337
9338 @smallexample
9339 Bad Example:
9340
9341 # Create link ../a pointing to a in that directory.
9342 # Not really useful because it points to itself.
9343 ln -s a ..
9344
9345 Better Example:
9346
9347 # Change to the target before creating symlinks to avoid being confused.
9348 cd ..
9349 ln -s adir/a .
9350
9351 Bad Example:
9352
9353 # Hard coded file names don't move well.
9354 ln -s $(pwd)/a /some/dir/
9355
9356 Better Example:
9357
9358 # Relative file names survive directory moves and also
9359 # work across networked file systems.
9360 ln -s afile anotherfile
9361 ln -s ../adir/afile yetanotherfile
9362 @end smallexample
9363
9364
9365 @node mkdir invocation
9366 @section @command{mkdir}: Make directories
9367
9368 @pindex mkdir
9369 @cindex directories, creating
9370 @cindex creating directories
9371
9372 @command{mkdir} creates directories with the specified names.  Synopsis:
9373
9374 @example
9375 mkdir [@var{option}]@dots{} @var{name}@dots{}
9376 @end example
9377
9378 @command{mkdir} creates each directory @var{name} in the order given.
9379 It reports an error if @var{name} already exists, unless the
9380 @option{-p} option is given and @var{name} is a directory.
9381
9382 The program accepts the following options.  Also see @ref{Common options}.
9383
9384 @table @samp
9385
9386 @item -m @var{mode}
9387 @itemx --mode=@var{mode}
9388 @opindex -m
9389 @opindex --mode
9390 @cindex modes of created directories, setting
9391 Set the file permission bits of created directories to @var{mode},
9392 which uses the same syntax as
9393 in @command{chmod} and uses @samp{a=rwx} (read, write and execute allowed for
9394 everyone) for the point of the departure.  @xref{File permissions}.
9395
9396 Normally the directory has the desired file mode bits at the moment it
9397 is created.  As a @acronym{GNU} extension, @var{mode} may also mention
9398 special mode bits, but in this case there may be a temporary window
9399 during which the directory exists but its special mode bits are
9400 incorrect.  @xref{Directory Setuid and Setgid}, for how the
9401 set-user-ID and set-group-ID bits of directories are inherited unless
9402 overridden in this way.
9403
9404 @item -p
9405 @itemx --parents
9406 @opindex -p
9407 @opindex --parents
9408 @cindex parent directories, creating
9409 Make any missing parent directories for each argument, setting their
9410 file permission bits to the umask modified by @samp{u+wx}.  Ignore
9411 existing parent directories, and do not change their file permission
9412 bits.
9413
9414 To set the file permission bits of any newly-created parent
9415 directories to a value that includes @samp{u+wx}, you can set the
9416 umask before invoking @command{mkdir}.  For example, if the shell
9417 command @samp{(umask u=rwx,go=rx; mkdir -p P/Q)} creates the parent
9418 @file{P} it sets the parent's permission bits to @samp{u=rwx,go=rx}.
9419 To set a parent's special mode bits as well, you can invoke
9420 @command{chmod} after @command{mkdir}.  @xref{Directory Setuid and
9421 Setgid}, for how the set-user-ID and set-group-ID bits of
9422 newly-created parent directories are inherited.
9423
9424 @item -v
9425 @itemx --verbose
9426 @opindex -v
9427 @opindex --verbose
9428 Print a message for each created directory.  This is most useful with
9429 @option{--parents}.
9430
9431 @item -Z @var{context}
9432 @itemx --context=@var{context}
9433 @opindex -Z
9434 @opindex --context
9435 @cindex SELinux
9436 @cindex security context
9437 Set the default SELinux security context to be used for created directories.
9438
9439 @end table
9440
9441 @exitstatus
9442
9443
9444 @node mkfifo invocation
9445 @section @command{mkfifo}: Make FIFOs (named pipes)
9446
9447 @pindex mkfifo
9448 @cindex FIFOs, creating
9449 @cindex named pipes, creating
9450 @cindex creating FIFOs (named pipes)
9451
9452 @command{mkfifo} creates FIFOs (also called @dfn{named pipes}) with the
9453 specified names.  Synopsis:
9454
9455 @example
9456 mkfifo [@var{option}] @var{name}@dots{}
9457 @end example
9458
9459 A @dfn{FIFO} is a special file type that permits independent processes
9460 to communicate.  One process opens the FIFO file for writing, and
9461 another for reading, after which data can flow as with the usual
9462 anonymous pipe in shells or elsewhere.
9463
9464 The program accepts the following option.  Also see @ref{Common options}.
9465
9466 @table @samp
9467
9468 @item -m @var{mode}
9469 @itemx --mode=@var{mode}
9470 @opindex -m
9471 @opindex --mode
9472 @cindex modes of created FIFOs, setting
9473 Set the mode of created FIFOs to @var{mode}, which is symbolic as in
9474 @command{chmod} and uses @samp{a=rw} (read and write allowed for everyone)
9475 for the point of departure.  @var{mode} should specify only file
9476 permission bits.  @xref{File permissions}.
9477
9478 @item -Z @var{context}
9479 @itemx --context=@var{context}
9480 @opindex -Z
9481 @opindex --context
9482 @cindex SELinux
9483 @cindex security context
9484 Set the default SELinux security context to be used for created FIFOs.
9485
9486 @end table
9487
9488 @exitstatus
9489
9490
9491 @node mknod invocation
9492 @section @command{mknod}: Make block or character special files
9493
9494 @pindex mknod
9495 @cindex block special files, creating
9496 @cindex character special files, creating
9497
9498 @command{mknod} creates a FIFO, character special file, or block special
9499 file with the specified name.  Synopsis:
9500
9501 @example
9502 mknod [@var{option}]@dots{} @var{name} @var{type} [@var{major} @var{minor}]
9503 @end example
9504
9505 @cindex special files
9506 @cindex block special files
9507 @cindex character special files
9508 Unlike the phrase ``special file type'' above, the term @dfn{special
9509 file} has a technical meaning on Unix: something that can generate or
9510 receive data.  Usually this corresponds to a physical piece of hardware,
9511 e.g., a printer or a disk.  (These files are typically created at
9512 system-configuration time.)  The @command{mknod} command is what creates
9513 files of this type.  Such devices can be read either a character at a
9514 time or a ``block'' (many characters) at a time, hence we say there are
9515 @dfn{block special} files and @dfn{character special} files.
9516
9517 @c mknod is a shell built-in at least with OpenBSD's /bin/sh
9518 @mayConflictWithShellBuiltIn{mknod}
9519
9520 The arguments after @var{name} specify the type of file to make:
9521
9522 @table @samp
9523
9524 @item p
9525 @opindex p @r{for FIFO file}
9526 for a FIFO
9527
9528 @item b
9529 @opindex b @r{for block special file}
9530 for a block special file
9531
9532 @item c
9533 @c Don't document the `u' option -- it's just a synonym for `c'.
9534 @c Do *any* versions of mknod still use it?
9535 @c @itemx u
9536 @opindex c @r{for character special file}
9537 @c @opindex u @r{for character special file}
9538 for a character special file
9539
9540 @end table
9541
9542 When making a block or character special file, the major and minor
9543 device numbers must be given after the file type.
9544 If a major or minor device number begins with @samp{0x} or @samp{0X},
9545 it is interpreted as hexadecimal; otherwise, if it begins with @samp{0},
9546 as octal; otherwise, as decimal.
9547
9548 The program accepts the following option.  Also see @ref{Common options}.
9549
9550 @table @samp
9551
9552 @item -m @var{mode}
9553 @itemx --mode=@var{mode}
9554 @opindex -m
9555 @opindex --mode
9556 Set the mode of created files to @var{mode}, which is symbolic as in
9557 @command{chmod} and uses @samp{a=rw} as the point of departure.
9558 @var{mode} should specify only file permission bits.
9559 @xref{File permissions}.
9560
9561 @item -Z @var{context}
9562 @itemx --context=@var{context}
9563 @opindex -Z
9564 @opindex --context
9565 @cindex SELinux
9566 @cindex security context
9567 Set the default SELinux security context to be used for created files.
9568
9569 @end table
9570
9571 @exitstatus
9572
9573
9574 @node readlink invocation
9575 @section @command{readlink}: Print value of a symlink or canonical file name
9576
9577 @pindex readlink
9578 @cindex displaying value of a symbolic link
9579 @cindex canonical file name
9580 @cindex canonicalize a file name
9581 @pindex realpath
9582 @findex realpath
9583
9584 @command{readlink} may work in one of two supported modes:
9585
9586 @table @samp
9587
9588 @item Readlink mode
9589
9590 @command{readlink} outputs the value of the given symbolic link.
9591 If @command{readlink} is invoked with an argument other than the name
9592 of a symbolic link, it produces no output and exits with a nonzero exit code.
9593
9594 @item Canonicalize mode
9595
9596 @command{readlink} outputs the absolute name of the given file which contains
9597 no @file{.}, @file{..} components nor any repeated separators
9598 (@file{/}) or symbolic links.
9599
9600 @end table
9601
9602 @example
9603 readlink [@var{option}] @var{file}
9604 @end example
9605
9606 By default, @command{readlink} operates in readlink mode.
9607
9608 The program accepts the following options.  Also see @ref{Common options}.
9609
9610 @table @samp
9611
9612 @item -f
9613 @itemx --canonicalize
9614 @opindex -f
9615 @opindex --canonicalize
9616 Activate canonicalize mode.
9617 If any component of the file name except the last one is missing or unavailable,
9618 @command{readlink} produces no output and exits with a nonzero exit
9619 code.  A trailing slash is ignored.
9620
9621 @item -e
9622 @itemx --canonicalize-existing
9623 @opindex -e
9624 @opindex --canonicalize-existing
9625 Activate canonicalize mode.
9626 If any component is missing or unavailable, @command{readlink} produces
9627 no output and exits with a nonzero exit code.  A trailing slash
9628 requires that the name resolve to a directory.
9629
9630 @item -m
9631 @itemx --canonicalize-missing
9632 @opindex -m
9633 @opindex --canonicalize-missing
9634 Activate canonicalize mode.
9635 If any component is missing or unavailable, @command{readlink} treats it
9636 as a directory.
9637
9638 @item -n
9639 @itemx --no-newline
9640 @opindex -n
9641 @opindex --no-newline
9642 Do not output the trailing newline.
9643
9644 @item -s
9645 @itemx -q
9646 @itemx --silent
9647 @itemx --quiet
9648 @opindex -s
9649 @opindex -q
9650 @opindex --silent
9651 @opindex --quiet
9652 Suppress most error messages.
9653
9654 @item -v
9655 @itemx --verbose
9656 @opindex -v
9657 @opindex --verbose
9658 Report error messages.
9659
9660 @end table
9661
9662 The @command{readlink} utility first appeared in OpenBSD 2.1.
9663
9664 There is a @command{realpath} command on some systems
9665 which operates like @command{readlink} in canonicalize mode.
9666
9667 @exitstatus
9668
9669
9670 @node rmdir invocation
9671 @section @command{rmdir}: Remove empty directories
9672
9673 @pindex rmdir
9674 @cindex removing empty directories
9675 @cindex directories, removing empty
9676
9677 @command{rmdir} removes empty directories.  Synopsis:
9678
9679 @example
9680 rmdir [@var{option}]@dots{} @var{directory}@dots{}
9681 @end example
9682
9683 If any @var{directory} argument does not refer to an existing empty
9684 directory, it is an error.
9685
9686 The program accepts the following options.  Also see @ref{Common options}.
9687
9688 @table @samp
9689
9690 @item --ignore-fail-on-non-empty
9691 @opindex --ignore-fail-on-non-empty
9692 @cindex directory deletion, ignoring failures
9693 Ignore each failure to remove a directory that is solely because
9694 the directory is non-empty.
9695
9696 @item -p
9697 @itemx --parents
9698 @opindex -p
9699 @opindex --parents
9700 @cindex parent directories, removing
9701 Remove @var{directory}, then try to remove each component of @var{directory}.
9702 So, for example, @samp{rmdir -p a/b/c} is similar to @samp{rmdir a/b/c a/b a}.
9703 As such, it fails if any of those directories turns out not to be empty.
9704 Use the @option{--ignore-fail-on-non-empty} option to make it so such
9705 a failure does not evoke a diagnostic and does not cause @command{rmdir} to
9706 exit unsuccessfully.
9707
9708 @item -v
9709 @itemx --verbose
9710 @opindex -v
9711 @opindex --verbose
9712 @cindex directory deletion, reporting
9713 Give a diagnostic for each successful removal.
9714 @var{directory} is removed.
9715
9716 @end table
9717
9718 @xref{rm invocation}, for how to remove non-empty directories (recursively).
9719
9720 @exitstatus
9721
9722
9723 @node unlink invocation
9724 @section @command{unlink}: Remove files via the unlink syscall
9725
9726 @pindex unlink
9727 @cindex removing files or directories (via the unlink syscall)
9728
9729 @command{unlink} deletes a single specified file name.
9730 It is a minimalist interface to the system-provided
9731 @code{unlink} function.  @xref{Deleting Files, , , libc,
9732 The GNU C Library Reference Manual}.  Synopsis:
9733 It avoids the bells and whistles of the more commonly-used
9734 @command{rm} command (@pxref{rm invocation}).
9735
9736 @example
9737 unlink @var{filename}
9738 @end example
9739
9740 On some systems @code{unlink} can be used to delete the name of a
9741 directory.  On others, it can be used that way only by a privileged user.
9742 In the GNU system @code{unlink} can never delete the name of a directory.
9743
9744 The @command{unlink} command honors the @option{--help} and
9745 @option{--version} options.  To remove a file whose name begins with
9746 @samp{-}, prefix the name with @samp{./}, e.g., @samp{unlink ./--help}.
9747
9748 @exitstatus
9749
9750
9751 @node Changing file attributes
9752 @chapter Changing file attributes
9753
9754 @cindex changing file attributes
9755 @cindex file attributes, changing
9756 @cindex attributes, file
9757
9758 A file is not merely its contents, a name, and a file type
9759 (@pxref{Special file types}).  A file also has an owner (a user ID), a
9760 group (a group ID), permissions (what the owner can do with the file,
9761 what people in the group can do, and what everyone else can do), various
9762 timestamps, and other information.  Collectively, we call these a file's
9763 @dfn{attributes}.
9764
9765 These commands change file attributes.
9766
9767 @menu
9768 * chgrp invocation::            Change file groups.
9769 * chmod invocation::            Change access permissions.
9770 * chown invocation::            Change file owners and groups.
9771 * touch invocation::            Change file timestamps.
9772 @end menu
9773
9774
9775 @node chown invocation
9776 @section @command{chown}: Change file owner and group
9777
9778 @pindex chown
9779 @cindex file ownership, changing
9780 @cindex group ownership, changing
9781 @cindex changing file ownership
9782 @cindex changing group ownership
9783
9784 @command{chown} changes the user and/or group ownership of each given @var{file}
9785 to @var{new-owner} or to the user and group of an existing reference file.
9786 Synopsis:
9787
9788 @example
9789 chown [@var{option}]@dots{} @{@var{new-owner} | --reference=@var{ref_file}@}@c
9790  @var{file}@dots{}
9791 @end example
9792
9793 If used, @var{new-owner} specifies the new owner and/or group as follows
9794 (with no embedded white space):
9795
9796 @example
9797 [@var{owner}] [ : [@var{group}] ]
9798 @end example
9799
9800 Specifically:
9801
9802 @table @var
9803 @item owner
9804 If only an @var{owner} (a user name or numeric user ID) is given, that
9805 user is made the owner of each given file, and the files' group is not
9806 changed.
9807
9808 @item owner@samp{:}group
9809 If the @var{owner} is followed by a colon and a @var{group} (a
9810 group name or numeric group ID), with no spaces between them, the group
9811 ownership of the files is changed as well (to @var{group}).
9812
9813 @item owner@samp{:}
9814 If a colon but no group name follows @var{owner}, that user is
9815 made the owner of the files and the group of the files is changed to
9816 @var{owner}'s login group.
9817
9818 @item @samp{:}group
9819 If the colon and following @var{group} are given, but the owner
9820 is omitted, only the group of the files is changed; in this case,
9821 @command{chown} performs the same function as @command{chgrp}.
9822
9823 @item @samp{:}
9824 If only a colon is given, or if @var{new-owner} is empty, neither the
9825 owner nor the group is changed.
9826
9827 @end table
9828
9829 If @var{owner} or @var{group} is intended to represent a numeric user
9830 or group ID, then you may specify it with a leading @samp{+}.
9831 @xref{Disambiguating names and IDs}.
9832
9833 Some older scripts may still use @samp{.} in place of the @samp{:} separator.
9834 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not
9835 require support for that, but for backward compatibility @acronym{GNU}
9836 @command{chown} supports @samp{.} so long as no ambiguity results.
9837 New scripts should avoid the use of @samp{.} because it is not
9838 portable, and because it has undesirable results if the entire
9839 @var{owner@samp{.}group} happens to identify a user whose name
9840 contains @samp{.}.
9841
9842 The @command{chown} command sometimes clears the set-user-ID or
9843 set-group-ID permission bits.  This behavior depends on the policy and
9844 functionality of the underlying @code{chown} system call, which may
9845 make system-dependent file mode modifications outside the control of
9846 the @command{chown} command.  For example, the @command{chown} command
9847 might not affect those bits when invoked by a user with appropriate
9848 privileges, or when the
9849 bits signify some function other than executable permission (e.g.,
9850 mandatory locking).
9851 When in doubt, check the underlying system behavior.
9852
9853 The program accepts the following options.  Also see @ref{Common options}.
9854
9855 @table @samp
9856
9857 @item -c
9858 @itemx --changes
9859 @opindex -c
9860 @opindex --changes
9861 @cindex changed owners, verbosely describing
9862 Verbosely describe the action for each @var{file} whose ownership
9863 actually changes.
9864
9865 @item -f
9866 @itemx --silent
9867 @itemx --quiet
9868 @opindex -f
9869 @opindex --silent
9870 @opindex --quiet
9871 @cindex error messages, omitting
9872 Do not print error messages about files whose ownership cannot be
9873 changed.
9874
9875 @itemx @w{@kbd{--from}=@var{old-owner}}
9876 @opindex --from
9877 @cindex symbolic links, changing owner
9878 Change a @var{file}'s ownership only if it has current attributes specified
9879 by @var{old-owner}.  @var{old-owner} has the same form as @var{new-owner}
9880 described above.
9881 This option is useful primarily from a security standpoint in that
9882 it narrows considerably the window of potential abuse.
9883 For example, to reflect a user ID numbering change for one user's files
9884 without an option like this, @code{root} might run
9885
9886 @smallexample
9887 find / -owner OLDUSER -print0 | xargs -0 chown -h NEWUSER
9888 @end smallexample
9889
9890 But that is dangerous because the interval between when the @command{find}
9891 tests the existing file's owner and when the @command{chown} is actually run
9892 may be quite large.
9893 One way to narrow the gap would be to invoke chown for each file
9894 as it is found:
9895
9896 @example
9897 find / -owner OLDUSER -exec chown -h NEWUSER @{@} \;
9898 @end example
9899
9900 But that is very slow if there are many affected files.
9901 With this option, it is safer (the gap is narrower still)
9902 though still not perfect:
9903
9904 @example
9905 chown -h -R --from=OLDUSER NEWUSER /
9906 @end example
9907
9908 @item --dereference
9909 @opindex --dereference
9910 @cindex symbolic links, changing owner
9911 @findex lchown
9912 Do not act on symbolic links themselves but rather on what they point to.
9913 This is the default.
9914
9915 @item -h
9916 @itemx --no-dereference
9917 @opindex -h
9918 @opindex --no-dereference
9919 @cindex symbolic links, changing owner
9920 @findex lchown
9921 Act on symbolic links themselves instead of what they point to.
9922 This mode relies on the @code{lchown} system call.
9923 On systems that do not provide the @code{lchown} system call,
9924 @command{chown} fails when a file specified on the command line
9925 is a symbolic link.
9926 By default, no diagnostic is issued for symbolic links encountered
9927 during a recursive traversal, but see @option{--verbose}.
9928
9929 @itemx --preserve-root
9930 @opindex --preserve-root
9931 @cindex root directory, disallow recursive modification
9932 Fail upon any attempt to recursively change the root directory, @file{/}.
9933 Without @option{--recursive}, this option has no effect.
9934 @xref{Treating / specially}.
9935
9936 @itemx --no-preserve-root
9937 @opindex --no-preserve-root
9938 @cindex root directory, allow recursive modification
9939 Cancel the effect of any preceding @option{--preserve-root} option.
9940 @xref{Treating / specially}.
9941
9942 @item --reference=@var{ref_file}
9943 @opindex --reference
9944 Change the user and group of each @var{file} to be the same as those of
9945 @var{ref_file}.  If @var{ref_file} is a symbolic link, do not use the
9946 user and group of the symbolic link, but rather those of the file it
9947 refers to.
9948
9949 @item -v
9950 @itemx --verbose
9951 @opindex -v
9952 @opindex --verbose
9953 Output a diagnostic for every file processed.
9954 If a symbolic link is encountered during a recursive traversal
9955 on a system without the @code{lchown} system call, and @option{--no-dereference}
9956 is in effect, then issue a diagnostic saying neither the symbolic link nor
9957 its referent is being changed.
9958
9959 @item -R
9960 @itemx --recursive
9961 @opindex -R
9962 @opindex --recursive
9963 @cindex recursively changing file ownership
9964 Recursively change ownership of directories and their contents.
9965
9966 @choptH
9967 @xref{Traversing symlinks}.
9968
9969 @choptL
9970 @xref{Traversing symlinks}.
9971
9972 @choptP
9973 @xref{Traversing symlinks}.
9974
9975 @end table
9976
9977 @exitstatus
9978
9979 Examples:
9980
9981 @smallexample
9982 # Change the owner of /u to "root".
9983 chown root /u
9984
9985 # Likewise, but also change its group to "staff".
9986 chown root:staff /u
9987
9988 # Change the owner of /u and subfiles to "root".
9989 chown -hR root /u
9990 @end smallexample
9991
9992
9993 @node chgrp invocation
9994 @section @command{chgrp}: Change group ownership
9995
9996 @pindex chgrp
9997 @cindex group ownership, changing
9998 @cindex changing group ownership
9999
10000 @command{chgrp} changes the group ownership of each given @var{file}
10001 to @var{group} (which can be either a group name or a numeric group ID)
10002 or to the group of an existing reference file.  Synopsis:
10003
10004 @example
10005 chgrp [@var{option}]@dots{} @{@var{group} | --reference=@var{ref_file}@}@c
10006  @var{file}@dots{}
10007 @end example
10008
10009 If @var{group} is intended to represent a
10010 numeric group ID, then you may specify it with a leading @samp{+}.
10011 @xref{Disambiguating names and IDs}.
10012
10013 The program accepts the following options.  Also see @ref{Common options}.
10014
10015 @table @samp
10016
10017 @item -c
10018 @itemx --changes
10019 @opindex -c
10020 @opindex --changes
10021 @cindex changed files, verbosely describing
10022 Verbosely describe the action for each @var{file} whose group actually
10023 changes.
10024
10025 @item -f
10026 @itemx --silent
10027 @itemx --quiet
10028 @opindex -f
10029 @opindex --silent
10030 @opindex --quiet
10031 @cindex error messages, omitting
10032 Do not print error messages about files whose group cannot be
10033 changed.
10034
10035 @item --dereference
10036 @opindex --dereference
10037 @cindex symbolic links, changing owner
10038 @findex lchown
10039 Do not act on symbolic links themselves but rather on what they point to.
10040 This is the default.
10041
10042 @item -h
10043 @itemx --no-dereference
10044 @opindex -h
10045 @opindex --no-dereference
10046 @cindex symbolic links, changing group
10047 @findex lchown
10048 Act on symbolic links themselves instead of what they point to.
10049 This mode relies on the @code{lchown} system call.
10050 On systems that do not provide the @code{lchown} system call,
10051 @command{chgrp} fails when a file specified on the command line
10052 is a symbolic link.
10053 By default, no diagnostic is issued for symbolic links encountered
10054 during a recursive traversal, but see @option{--verbose}.
10055
10056 @itemx --preserve-root
10057 @opindex --preserve-root
10058 @cindex root directory, disallow recursive modification
10059 Fail upon any attempt to recursively change the root directory, @file{/}.
10060 Without @option{--recursive}, this option has no effect.
10061 @xref{Treating / specially}.
10062
10063 @itemx --no-preserve-root
10064 @opindex --no-preserve-root
10065 @cindex root directory, allow recursive modification
10066 Cancel the effect of any preceding @option{--preserve-root} option.
10067 @xref{Treating / specially}.
10068
10069 @item --reference=@var{ref_file}
10070 @opindex --reference
10071 Change the group of each @var{file} to be the same as that of
10072 @var{ref_file}.  If @var{ref_file} is a symbolic link, do not use the
10073 group of the symbolic link, but rather that of the file it refers to.
10074
10075 @item -v
10076 @itemx --verbose
10077 @opindex -v
10078 @opindex --verbose
10079 Output a diagnostic for every file processed.
10080 If a symbolic link is encountered during a recursive traversal
10081 on a system without the @code{lchown} system call, and @option{--no-dereference}
10082 is in effect, then issue a diagnostic saying neither the symbolic link nor
10083 its referent is being changed.
10084
10085 @item -R
10086 @itemx --recursive
10087 @opindex -R
10088 @opindex --recursive
10089 @cindex recursively changing group ownership
10090 Recursively change the group ownership of directories and their contents.
10091
10092 @choptH
10093 @xref{Traversing symlinks}.
10094
10095 @choptL
10096 @xref{Traversing symlinks}.
10097
10098 @choptP
10099 @xref{Traversing symlinks}.
10100
10101 @end table
10102
10103 @exitstatus
10104
10105 Examples:
10106
10107 @smallexample
10108 # Change the group of /u to "staff".
10109 chgrp staff /u
10110
10111 # Change the group of /u and subfiles to "staff".
10112 chgrp -hR staff /u
10113 @end smallexample
10114
10115
10116 @node chmod invocation
10117 @section @command{chmod}: Change access permissions
10118
10119 @pindex chmod
10120 @cindex changing access permissions
10121 @cindex access permissions, changing
10122 @cindex permissions, changing access
10123
10124 @command{chmod} changes the access permissions of the named files.  Synopsis:
10125
10126 @example
10127 chmod [@var{option}]@dots{} @{@var{mode} | --reference=@var{ref_file}@}@c
10128  @var{file}@dots{}
10129 @end example
10130
10131 @cindex symbolic links, permissions of
10132 @command{chmod} never changes the permissions of symbolic links, since
10133 the @command{chmod} system call cannot change their permissions.
10134 This is not a problem since the permissions of symbolic links are
10135 never used.  However, for each symbolic link listed on the command
10136 line, @command{chmod} changes the permissions of the pointed-to file.
10137 In contrast, @command{chmod} ignores symbolic links encountered during
10138 recursive directory traversals.
10139
10140 A successful use of @command{chmod} clears the set-group-ID bit of a
10141 regular file if the file's group ID does not match the user's
10142 effective group ID or one of the user's supplementary group IDs,
10143 unless the user has appropriate privileges.  Additional restrictions
10144 may cause the set-user-ID and set-group-ID bits of @var{mode} or
10145 @var{ref_file} to be ignored.  This behavior depends on the policy and
10146 functionality of the underlying @code{chmod} system call.  When in
10147 doubt, check the underlying system behavior.
10148
10149 If used, @var{mode} specifies the new file mode bits.
10150 For details, see the section on @ref{File permissions}.
10151 If you really want @var{mode} to have a leading @samp{-}, you should
10152 use @option{--} first, e.g., @samp{chmod -- -w file}.  Typically,
10153 though, @samp{chmod a-w file} is preferable, and @command{chmod -w
10154 file} (without the @option{--}) complains if it behaves differently
10155 from what @samp{chmod a-w file} would do.
10156
10157 The program accepts the following options.  Also see @ref{Common options}.
10158
10159 @table @samp
10160
10161 @item -c
10162 @itemx --changes
10163 @opindex -c
10164 @opindex --changes
10165 Verbosely describe the action for each @var{file} whose permissions
10166 actually changes.
10167
10168 @item -f
10169 @itemx --silent
10170 @itemx --quiet
10171 @opindex -f
10172 @opindex --silent
10173 @opindex --quiet
10174 @cindex error messages, omitting
10175 Do not print error messages about files whose permissions cannot be
10176 changed.
10177
10178 @itemx --preserve-root
10179 @opindex --preserve-root
10180 @cindex root directory, disallow recursive modification
10181 Fail upon any attempt to recursively change the root directory, @file{/}.
10182 Without @option{--recursive}, this option has no effect.
10183 @xref{Treating / specially}.
10184
10185 @itemx --no-preserve-root
10186 @opindex --no-preserve-root
10187 @cindex root directory, allow recursive modification
10188 Cancel the effect of any preceding @option{--preserve-root} option.
10189 @xref{Treating / specially}.
10190
10191 @item -v
10192 @itemx --verbose
10193 @opindex -v
10194 @opindex --verbose
10195 Verbosely describe the action or non-action taken for every @var{file}.
10196
10197 @item --reference=@var{ref_file}
10198 @opindex --reference
10199 Change the mode of each @var{file} to be the same as that of @var{ref_file}.
10200 @xref{File permissions}.
10201 If @var{ref_file} is a symbolic link, do not use the mode
10202 of the symbolic link, but rather that of the file it refers to.
10203
10204 @item -R
10205 @itemx --recursive
10206 @opindex -R
10207 @opindex --recursive
10208 @cindex recursively changing access permissions
10209 Recursively change permissions of directories and their contents.
10210
10211 @end table
10212
10213 @exitstatus
10214
10215
10216 @node touch invocation
10217 @section @command{touch}: Change file timestamps
10218
10219 @pindex touch
10220 @cindex changing file timestamps
10221 @cindex file timestamps, changing
10222 @cindex timestamps, changing file
10223
10224 @command{touch} changes the access and/or modification times of the
10225 specified files.  Synopsis:
10226
10227 @example
10228 touch [@var{option}]@dots{} @var{file}@dots{}
10229 @end example
10230
10231 @cindex empty files, creating
10232 Any @var{file} argument that does not exist is created empty, unless
10233 option @option{--no-create} (@option{-c}) or @option{--no-dereference}
10234 (@option{-h}) was in effect.
10235
10236 A @var{file} argument string of @samp{-} is handled specially and
10237 causes @command{touch} to change the times of the file associated with
10238 standard output.
10239
10240 @cindex permissions, for changing file timestamps
10241 If changing both the access and modification times to the current
10242 time, @command{touch} can change the timestamps for files that the user
10243 running it does not own but has write permission for.  Otherwise, the
10244 user must own the files.
10245
10246 Although @command{touch} provides options for changing two of the times---the
10247 times of last access and modification---of a file, there is actually
10248 a standard third one as well: the inode change time.  This is often
10249 referred to as a file's @code{ctime}.
10250 The inode change time represents the time when the file's meta-information
10251 last changed.  One common example of this is when the permissions of a
10252 file change.  Changing the permissions doesn't access the file, so
10253 the atime doesn't change, nor does it modify the file, so the mtime
10254 doesn't change.  Yet, something about the file itself has changed,
10255 and this must be noted somewhere.  This is the job of the ctime field.
10256 This is necessary, so that, for example, a backup program can make a
10257 fresh copy of the file, including the new permissions value.
10258 Another operation that modifies a file's ctime without affecting
10259 the others is renaming.  In any case, it is not possible, in normal
10260 operations, for a user to change the ctime field to a user-specified value.
10261 Some operating systems and file systems support a fourth time: the
10262 birth time, when the file was first created; by definition, this
10263 timestamp never changes.
10264
10265 @vindex TZ
10266 Time stamps assume the time zone rules specified by the @env{TZ}
10267 environment variable, or by the system default rules if @env{TZ} is
10268 not set.  @xref{TZ Variable,, Specifying the Time Zone with @env{TZ},
10269 libc, The GNU C Library Reference Manual}.
10270 You can avoid ambiguities during
10271 daylight saving transitions by using @sc{utc} time stamps.
10272
10273 The program accepts the following options.  Also see @ref{Common options}.
10274
10275 @table @samp
10276
10277 @item -a
10278 @itemx --time=atime
10279 @itemx --time=access
10280 @itemx --time=use
10281 @opindex -a
10282 @opindex --time
10283 @opindex atime@r{, changing}
10284 @opindex access @r{time, changing}
10285 @opindex use @r{time, changing}
10286 Change the access time only.
10287
10288 @item -c
10289 @itemx --no-create
10290 @opindex -c
10291 @opindex --no-create
10292 Do not warn about or create files that do not exist.
10293
10294 @item -d
10295 @itemx --date=@var{time}
10296 @opindex -d
10297 @opindex --date
10298 @opindex time
10299 Use @var{time} instead of the current time.  It can contain month names,
10300 time zones, @samp{am} and @samp{pm}, @samp{yesterday}, etc.  For
10301 example, @option{--date="2004-02-27 14:19:13.489392193 +0530"}
10302 specifies the instant of time that is 489,392,193 nanoseconds after
10303 February 27, 2004 at 2:19:13 PM in a time zone that is 5 hours and 30
10304 minutes east of @acronym{UTC}.  @xref{Date input formats}.
10305 File systems that do not support high-resolution time stamps
10306 silently ignore any excess precision here.
10307
10308 @item -f
10309 @opindex -f
10310 @cindex BSD @command{touch} compatibility
10311 Ignored; for compatibility with BSD versions of @command{touch}.
10312
10313 @item -h
10314 @itemx --no-dereference
10315 @opindex -h
10316 @opindex --no-dereference
10317 @cindex symbolic links, changing time
10318 @findex lutimes
10319 Attempt to change the timestamps of a symbolic link, rather than what
10320 the link refers to.  When using this option, empty files are not
10321 created, but option @option{-c} must also be used to avoid warning
10322 about files that do not exist.  Not all systems support changing the
10323 timestamps of symlinks, since underlying system support for this
10324 action was not required until @acronym{POSIX} 2008.  Also, on some
10325 systems, the mere act of examining a symbolic link changes the access
10326 time, such that only changes to the modification time will persist
10327 long enough to be observable.  When coupled with option @option{-r}, a
10328 reference timestamp is taken from a symbolic link rather than the file
10329 it refers to.
10330
10331 @item -m
10332 @itemx --time=mtime
10333 @itemx --time=modify
10334 @opindex -m
10335 @opindex --time
10336 @opindex mtime@r{, changing}
10337 @opindex modify @r{time, changing}
10338 Change the modification time only.
10339
10340 @item -r @var{file}
10341 @itemx --reference=@var{file}
10342 @opindex -r
10343 @opindex --reference
10344 Use the times of the reference @var{file} instead of the current time.
10345 If this option is combined with the @option{--date=@var{time}}
10346 (@option{-d @var{time}}) option, the reference @var{file}'s time is
10347 the origin for any relative @var{time}s given, but is otherwise ignored.
10348 For example, @samp{-r foo -d '-5 seconds'} specifies a time stamp
10349 equal to five seconds before the corresponding time stamp for @file{foo}.
10350 If @var{file} is a symbolic link, the reference timestamp is taken
10351 from the target of the symlink, unless @option{-h} was also in effect.
10352
10353 @item -t [[@var{cc}]@var{yy}]@var{mmddhhmm}[.@var{ss}]
10354 Use the argument (optional four-digit or two-digit years, months,
10355 days, hours, minutes, optional seconds) instead of the current time.
10356 If the year is specified with only two digits, then @var{cc}
10357 is 20 for years in the range 0 @dots{} 68, and 19 for years in
10358 69 @dots{} 99.  If no digits of the year are specified,
10359 the argument is interpreted as a date in the current year.
10360 Note that @var{ss} may be @samp{60}, to accommodate leap seconds.
10361
10362 @end table
10363
10364 @vindex _POSIX2_VERSION
10365 On older systems, @command{touch} supports an obsolete syntax, as follows.
10366 If no timestamp is given with any of the @option{-d}, @option{-r}, or
10367 @option{-t} options, and if there are two or more @var{file}s and the
10368 first @var{file} is of the form @samp{@var{mmddhhmm}[@var{yy}]} and this
10369 would be a valid argument to the @option{-t} option (if the @var{yy}, if
10370 any, were moved to the front), and if the represented year
10371 is in the range 1969--1999, that argument is interpreted as the time
10372 for the other files instead of as a file name.
10373 This obsolete behavior can be enabled or disabled with the
10374 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
10375 conformance}), but portable scripts should avoid commands whose
10376 behavior depends on this variable.
10377 For example, use @samp{touch ./12312359 main.c} or @samp{touch -t
10378 12312359 main.c} rather than the ambiguous @samp{touch 12312359 main.c}.
10379
10380 @exitstatus
10381
10382
10383 @node Disk usage
10384 @chapter Disk usage
10385
10386 @cindex disk usage
10387
10388 No disk can hold an infinite amount of data.  These commands report
10389 how much disk storage is in use or available, report other file and
10390 file status information, and write buffers to disk.
10391
10392 @menu
10393 * df invocation::               Report file system disk space usage.
10394 * du invocation::               Estimate file space usage.
10395 * stat invocation::             Report file or file system status.
10396 * sync invocation::             Synchronize memory and disk.
10397 * truncate invocation::         Shrink or extend the size of a file.
10398 @end menu
10399
10400
10401 @node df invocation
10402 @section @command{df}: Report file system disk space usage
10403
10404 @pindex df
10405 @cindex file system disk usage
10406 @cindex disk usage by file system
10407
10408 @command{df} reports the amount of disk space used and available on
10409 file systems.  Synopsis:
10410
10411 @example
10412 df [@var{option}]@dots{} [@var{file}]@dots{}
10413 @end example
10414
10415 With no arguments, @command{df} reports the space used and available on all
10416 currently mounted file systems (of all types).  Otherwise, @command{df}
10417 reports on the file system containing each argument @var{file}.
10418
10419 Normally the disk space is printed in units of
10420 1024 bytes, but this can be overridden (@pxref{Block size}).
10421 Non-integer quantities are rounded up to the next higher unit.
10422
10423 @cindex disk device file
10424 @cindex device file, disk
10425 If an argument @var{file} is a disk device file containing a mounted
10426 file system, @command{df} shows the space available on that file system
10427 rather than on the file system containing the device node (i.e., the root
10428 file system).  @sc{gnu} @command{df} does not attempt to determine the
10429 disk usage
10430 on unmounted file systems, because on most kinds of systems doing so
10431 requires extremely nonportable intimate knowledge of file system
10432 structures.
10433
10434 The program accepts the following options.  Also see @ref{Common options}.
10435
10436 @table @samp
10437
10438 @item -a
10439 @itemx --all
10440 @opindex -a
10441 @opindex --all
10442 @cindex automounter file systems
10443 @cindex ignore file systems
10444 Include in the listing dummy file systems, which
10445 are omitted by default.  Such file systems are typically special-purpose
10446 pseudo-file-systems, such as automounter entries.
10447
10448 @item -B @var{size}
10449 @itemx --block-size=@var{size}
10450 @opindex -B
10451 @opindex --block-size
10452 @cindex file system sizes
10453 Scale sizes by @var{size} before printing them (@pxref{Block size}).
10454 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
10455
10456 @itemx --total
10457 @opindex --total
10458 @cindex grand total of disk size, usage and available space
10459 Print a grand total of all arguments after all arguments have
10460 been processed.  This can be used to find out the total disk size, usage
10461 and available space of all listed devices.
10462
10463 @optHumanReadable
10464
10465 @item -H
10466 @opindex -H
10467 Equivalent to @option{--si}.
10468
10469 @item -i
10470 @itemx --inodes
10471 @opindex -i
10472 @opindex --inodes
10473 @cindex inode usage
10474 List inode usage information instead of block usage.  An inode (short
10475 for index node) contains information about a file such as its owner,
10476 permissions, timestamps, and location on the disk.
10477
10478 @item -k
10479 @opindex -k
10480 @cindex kibibytes for file system sizes
10481 Print sizes in 1024-byte blocks, overriding the default block size
10482 (@pxref{Block size}).
10483 This option is equivalent to @option{--block-size=1K}.
10484
10485 @item -l
10486 @itemx --local
10487 @opindex -l
10488 @opindex --local
10489 @cindex file system types, limiting output to certain
10490 Limit the listing to local file systems.  By default, remote file systems
10491 are also listed.
10492
10493 @item --no-sync
10494 @opindex --no-sync
10495 @cindex file system space, retrieving old data more quickly
10496 Do not invoke the @code{sync} system call before getting any usage data.
10497 This may make @command{df} run significantly faster on systems with many
10498 disks, but on some systems (notably SunOS) the results may be slightly
10499 out of date.  This is the default.
10500
10501 @item -P
10502 @itemx --portability
10503 @opindex -P
10504 @opindex --portability
10505 @cindex one-line output format
10506 @cindex @acronym{POSIX} output format
10507 @cindex portable output format
10508 @cindex output format, portable
10509 Use the @acronym{POSIX} output format.  This is like the default format except
10510 for the following:
10511
10512 @enumerate
10513 @item
10514 The information about each file system is always printed on exactly
10515 one line; a mount device is never put on a line by itself.  This means
10516 that if the mount device name is more than 20 characters long (e.g., for
10517 some network mounts), the columns are misaligned.
10518
10519 @item
10520 The labels in the header output line are changed to conform to @acronym{POSIX}.
10521
10522 @item
10523 The default block size and output format are unaffected by the
10524 @env{DF_BLOCK_SIZE}, @env{BLOCK_SIZE} and @env{BLOCKSIZE} environment
10525 variables.  However, the default block size is still affected by
10526 @env{POSIXLY_CORRECT}: it is 512 if @env{POSIXLY_CORRECT} is set, 1024
10527 otherwise.  @xref{Block size}.
10528 @end enumerate
10529
10530 @optSi
10531
10532 @item --sync
10533 @opindex --sync
10534 @cindex file system space, retrieving current data more slowly
10535 Invoke the @code{sync} system call before getting any usage data.  On
10536 some systems (notably SunOS), doing this yields more up to date results,
10537 but in general this option makes @command{df} much slower, especially when
10538 there are many or very busy file systems.
10539
10540 @item -t @var{fstype}
10541 @itemx --type=@var{fstype}
10542 @opindex -t
10543 @opindex --type
10544 @cindex file system types, limiting output to certain
10545 Limit the listing to file systems of type @var{fstype}.  Multiple
10546 file system types can be specified by giving multiple @option{-t} options.
10547 By default, nothing is omitted.
10548
10549 @item -T
10550 @itemx --print-type
10551 @opindex -T
10552 @opindex --print-type
10553 @cindex file system types, printing
10554 Print each file system's type.  The types printed here are the same ones
10555 you can include or exclude with @option{-t} and @option{-x}.  The particular
10556 types printed are whatever is supported by the system.  Here are some of
10557 the common names (this list is certainly not exhaustive):
10558
10559 @table @samp
10560
10561 @item nfs
10562 @cindex @acronym{NFS} file system type
10563 An @acronym{NFS} file system, i.e., one mounted over a network from another
10564 machine.  This is the one type name which seems to be used uniformly by
10565 all systems.
10566
10567 @item 4.2@r{, }ufs@r{, }efs@dots{}
10568 @cindex Linux file system types
10569 @cindex local file system types
10570 @opindex 4.2 @r{file system type}
10571 @opindex ufs @r{file system type}
10572 @opindex efs @r{file system type}
10573 A file system on a locally-mounted hard disk.  (The system might even
10574 support more than one type here; Linux does.)
10575
10576 @item hsfs@r{, }cdfs
10577 @cindex CD-ROM file system type
10578 @cindex High Sierra file system
10579 @opindex hsfs @r{file system type}
10580 @opindex cdfs @r{file system type}
10581 A file system on a CD-ROM drive.  HP-UX uses @samp{cdfs}, most other
10582 systems use @samp{hsfs} (@samp{hs} for ``High Sierra'').
10583
10584 @item pcfs
10585 @cindex PC file system
10586 @cindex DOS file system
10587 @cindex MS-DOS file system
10588 @cindex diskette file system
10589 @opindex pcfs
10590 An MS-DOS file system, usually on a diskette.
10591
10592 @end table
10593
10594 @item -x @var{fstype}
10595 @itemx --exclude-type=@var{fstype}
10596 @opindex -x
10597 @opindex --exclude-type
10598 Limit the listing to file systems not of type @var{fstype}.
10599 Multiple file system types can be eliminated by giving multiple
10600 @option{-x} options.  By default, no file system types are omitted.
10601
10602 @item -v
10603 Ignored; for compatibility with System V versions of @command{df}.
10604
10605 @end table
10606
10607 @exitstatus
10608 Failure includes the case where no output is generated, so you can
10609 inspect the exit status of a command like @samp{df -t ext3 -t reiserfs
10610 @var{dir}} to test whether @var{dir} is on a file system of type
10611 @samp{ext3} or @samp{reiserfs}.
10612
10613
10614 @node du invocation
10615 @section @command{du}: Estimate file space usage
10616
10617 @pindex du
10618 @cindex file space usage
10619 @cindex disk usage for files
10620
10621 @command{du} reports the amount of disk space used by the specified files
10622 and for each subdirectory (of directory arguments).  Synopsis:
10623
10624 @example
10625 du [@var{option}]@dots{} [@var{file}]@dots{}
10626 @end example
10627
10628 With no arguments, @command{du} reports the disk space for the current
10629 directory.  Normally the disk space is printed in units of
10630 1024 bytes, but this can be overridden (@pxref{Block size}).
10631 Non-integer quantities are rounded up to the next higher unit.
10632
10633 If two or more hard links point to the same file, only one of the hard
10634 links is counted.  The @var{file} argument order affects which links
10635 are counted, and changing the argument order may change the numbers
10636 that @command{du} outputs.
10637
10638 The program accepts the following options.  Also see @ref{Common options}.
10639
10640 @table @samp
10641
10642 @item -a
10643 @itemx --all
10644 @opindex -a
10645 @opindex --all
10646 Show counts for all files, not just directories.
10647
10648 @itemx --apparent-size
10649 @opindex --apparent-size
10650 Print apparent sizes, rather than disk usage.  The apparent size of a
10651 file is the number of bytes reported by @code{wc -c} on regular files,
10652 or more generally, @code{ls -l --block-size=1} or @code{stat --format=%s}.
10653 For example, a file containing the word @samp{zoo} with no newline would,
10654 of course, have an apparent size of 3.  Such a small file may require
10655 anywhere from 0 to 16 KiB or more of disk space, depending on
10656 the type and configuration of the file system on which the file resides.
10657 However, a sparse file created with this command:
10658
10659 @example
10660 dd bs=1 seek=2GiB if=/dev/null of=big
10661 @end example
10662
10663 @noindent
10664 has an apparent size of 2 GiB, yet on most modern
10665 systems, it actually uses almost no disk space.
10666
10667 @item -b
10668 @itemx --bytes
10669 @opindex -b
10670 @opindex --bytes
10671 Equivalent to @code{--apparent-size --block-size=1}.
10672
10673 @item -B @var{size}
10674 @itemx --block-size=@var{size}
10675 @opindex -B
10676 @opindex --block-size
10677 @cindex file sizes
10678 Scale sizes by @var{size} before printing them (@pxref{Block size}).
10679 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
10680
10681 @item -c
10682 @itemx --total
10683 @opindex -c
10684 @opindex --total
10685 @cindex grand total of disk space
10686 Print a grand total of all arguments after all arguments have
10687 been processed.  This can be used to find out the total disk usage of
10688 a given set of files or directories.
10689
10690 @item -D
10691 @itemx --dereference-args
10692 @opindex -D
10693 @opindex --dereference-args
10694 Dereference symbolic links that are command line arguments.
10695 Does not affect other symbolic links.  This is helpful for finding
10696 out the disk usage of directories, such as @file{/usr/tmp}, which
10697 are often symbolic links.
10698
10699 @c --files0-from=FILE
10700 @filesZeroFromOption{du,, with the @option{--total} (@option{-c}) option}
10701
10702 @optHumanReadable
10703
10704 @item -H
10705 @opindex -H
10706 Equivalent to @option{--dereference-args} (@option{-D}).
10707
10708 @item -k
10709 @opindex -k
10710 @cindex kibibytes for file sizes
10711 Print sizes in 1024-byte blocks, overriding the default block size
10712 (@pxref{Block size}).
10713 This option is equivalent to @option{--block-size=1K}.
10714
10715 @item -l
10716 @itemx --count-links
10717 @opindex -l
10718 @opindex --count-links
10719 @cindex hard links, counting in @command{du}
10720 Count the size of all files, even if they have appeared already (as a
10721 hard link).
10722
10723 @item -L
10724 @itemx --dereference
10725 @opindex -L
10726 @opindex --dereference
10727 @cindex symbolic links, dereferencing in @command{du}
10728 Dereference symbolic links (show the disk space used by the file
10729 or directory that the link points to instead of the space used by
10730 the link).
10731
10732 @item -m
10733 @opindex -m
10734 @cindex mebibytes for file sizes
10735 Print sizes in 1,048,576-byte blocks, overriding the default block size
10736 (@pxref{Block size}).
10737 This option is equivalent to @option{--block-size=1M}.
10738
10739 @item -P
10740 @itemx --no-dereference
10741 @opindex -P
10742 @opindex --no-dereference
10743 @cindex symbolic links, dereferencing in @command{du}
10744 For each symbolic links encountered by @command{du},
10745 consider the disk space used by the symbolic link.
10746
10747 @item -d @var{depth}
10748 @item --max-depth=@var{depth}
10749 @opindex -d @var{depth}
10750 @opindex --max-depth=@var{depth}
10751 @cindex limiting output of @command{du}
10752 Show the total for each directory (and file if --all) that is at
10753 most MAX_DEPTH levels down from the root of the hierarchy.  The root
10754 is at level 0, so @code{du --max-depth=0} is equivalent to @code{du -s}.
10755
10756 @optNull{du}
10757
10758 @optSi
10759
10760 @item -s
10761 @itemx --summarize
10762 @opindex -s
10763 @opindex --summarize
10764 Display only a total for each argument.
10765
10766 @item -S
10767 @itemx --separate-dirs
10768 @opindex -S
10769 @opindex --separate-dirs
10770 Normally, in the output of @command{du} (when not using @option{--summarize}),
10771 the size listed next to a directory name, @var{d}, represents the sum
10772 of sizes of all entries beneath @var{d} as well as the size of @var{d} itself.
10773 With @option{--separate-dirs}, the size reported for a directory name,
10774 @var{d}, is merely the @code{stat.st_size}-derived size of the directory
10775 entry, @var{d}.
10776
10777 @itemx --time
10778 @opindex --time
10779 @cindex last modified dates, displaying in @command{du}
10780 Show time of the most recent modification of any file in the directory,
10781 or any of its subdirectories.
10782
10783 @itemx --time=ctime
10784 @itemx --time=status
10785 @itemx --time=use
10786 @opindex --time
10787 @opindex ctime@r{, show the most recent}
10788 @opindex status time@r{, show the most recent}
10789 @opindex use time@r{, show the most recent}
10790 Show the most recent status change time (the @samp{ctime} in the inode) of
10791 any file in the directory, instead of the modification time.
10792
10793 @itemx --time=atime
10794 @itemx --time=access
10795 @opindex --time
10796 @opindex atime@r{, show the most recent}
10797 @opindex access time@r{, show the most recent}
10798 Show the most recent access time (the @samp{atime} in the inode) of
10799 any file in the directory, instead of the modification time.
10800
10801 @item --time-style=@var{style}
10802 @opindex --time-style
10803 @cindex time style
10804 List timestamps in style @var{style}.  This option has an effect only if
10805 the @option{--time} option is also specified.  The @var{style} should
10806 be one of the following:
10807
10808 @table @samp
10809 @item +@var{format}
10810 @vindex LC_TIME
10811 List timestamps using @var{format}, where @var{format} is interpreted
10812 like the format argument of @command{date} (@pxref{date invocation}).
10813 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
10814 @command{du} to list timestamps like @samp{2002-03-30 23:45:56}.  As
10815 with @command{date}, @var{format}'s interpretation is affected by the
10816 @env{LC_TIME} locale category.
10817
10818 @item full-iso
10819 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
10820 format with nanosecond precision, e.g., @samp{2002-03-30
10821 23:45:56.477817180 -0700}.  This style is equivalent to
10822 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
10823
10824 @item long-iso
10825 List @acronym{ISO} 8601 date and time in minutes, e.g.,
10826 @samp{2002-03-30 23:45}.  These timestamps are shorter than
10827 @samp{full-iso} timestamps, and are usually good enough for everyday
10828 work.  This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
10829
10830 @item iso
10831 List @acronym{ISO} 8601 dates for timestamps, e.g., @samp{2002-03-30}.
10832 This style is equivalent to @samp{+%Y-%m-%d}.
10833 @end table
10834
10835 @vindex TIME_STYLE
10836 You can specify the default value of the @option{--time-style} option
10837 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
10838 the default style is @samp{long-iso}.  For compatibility with @command{ls},
10839 if @env{TIME_STYLE} begins with @samp{+} and contains a newline,
10840 the newline and any later characters are ignored; if @env{TIME_STYLE}
10841 begins with @samp{posix-} the @samp{posix-} is ignored; and if
10842 @env{TIME_STYLE} is @samp{locale} it is ignored.
10843
10844 @item -x
10845 @itemx --one-file-system
10846 @opindex -x
10847 @opindex --one-file-system
10848 @cindex one file system, restricting @command{du} to
10849 Skip directories that are on different file systems from the one that
10850 the argument being processed is on.
10851
10852 @item --exclude=@var{pattern}
10853 @opindex --exclude=@var{pattern}
10854 @cindex excluding files from @command{du}
10855 When recursing, skip subdirectories or files matching @var{pattern}.
10856 For example, @code{du --exclude='*.o'} excludes files whose names
10857 end in @samp{.o}.
10858
10859 @item -X @var{file}
10860 @itemx --exclude-from=@var{file}
10861 @opindex -X @var{file}
10862 @opindex --exclude-from=@var{file}
10863 @cindex excluding files from @command{du}
10864 Like @option{--exclude}, except take the patterns to exclude from @var{file},
10865 one per line.  If @var{file} is @samp{-}, take the patterns from standard
10866 input.
10867
10868 @end table
10869
10870 @cindex NFS mounts from BSD to HP-UX
10871 On BSD systems, @command{du} reports sizes that are half the correct
10872 values for files that are NFS-mounted from HP-UX systems.  On HP-UX
10873 systems, it reports sizes that are twice the correct values for
10874 files that are NFS-mounted from BSD systems.  This is due to a flaw
10875 in HP-UX; it also affects the HP-UX @command{du} program.
10876
10877 @exitstatus
10878
10879
10880 @node stat invocation
10881 @section @command{stat}: Report file or file system status
10882
10883 @pindex stat
10884 @cindex file status
10885 @cindex file system status
10886
10887 @command{stat} displays information about the specified file(s).  Synopsis:
10888
10889 @example
10890 stat [@var{option}]@dots{} [@var{file}]@dots{}
10891 @end example
10892
10893 With no option, @command{stat} reports all information about the given files.
10894 But it also can be used to report the information of the file systems the
10895 given files are located on.  If the files are links, @command{stat} can
10896 also give information about the files the links point to.
10897
10898 @mayConflictWithShellBuiltIn{stat}
10899
10900 @table @samp
10901
10902 @item -L
10903 @itemx --dereference
10904 @opindex -L
10905 @opindex --dereference
10906 @cindex symbolic links, dereferencing in @command{stat}
10907 Change how @command{stat} treats symbolic links.
10908 With this option, @command{stat} acts on the file referenced
10909 by each symbolic link argument.
10910 Without it, @command{stat} acts on any symbolic link argument directly.
10911
10912 @item -f
10913 @itemx --file-system
10914 @opindex -f
10915 @opindex --file-system
10916 @cindex file systems
10917 Report information about the file systems where the given files are located
10918 instead of information about the files themselves.
10919 This option implies the @option{-L} option.
10920
10921 @item -c
10922 @itemx --format=@var{format}
10923 @opindex -c
10924 @opindex --format=@var{format}
10925 @cindex output format
10926 Use @var{format} rather than the default format.
10927 @var{format} is automatically newline-terminated, so
10928 running a command like the following with two or more @var{file}
10929 operands produces a line of output for each operand:
10930 @example
10931 $ stat --format=%d:%i / /usr
10932 2050:2
10933 2057:2
10934 @end example
10935
10936 @itemx --printf=@var{format}
10937 @opindex --printf=@var{format}
10938 @cindex output format
10939 Use @var{format} rather than the default format.
10940 Like @option{--format}, but interpret backslash escapes,
10941 and do not output a mandatory trailing newline.
10942 If you want a newline, include @samp{\n} in the @var{format}.
10943 Here's how you would use @option{--printf} to print the device
10944 and inode numbers of @file{/} and @file{/usr}:
10945 @example
10946 $ stat --printf='%d:%i\n' / /usr
10947 2050:2
10948 2057:2
10949 @end example
10950
10951 @item -t
10952 @itemx --terse
10953 @opindex -t
10954 @opindex --terse
10955 @cindex terse output
10956 Print the information in terse form, suitable for parsing by other programs.
10957
10958 @end table
10959
10960 The valid @var{format} directives for files with @option{--format} and
10961 @option{--printf} are:
10962
10963 @itemize @bullet
10964 @item %a - Access rights in octal
10965 @item %A - Access rights in human readable form
10966 @item %b - Number of blocks allocated (see @samp{%B})
10967 @item %B - The size in bytes of each block reported by @samp{%b}
10968 @item %C - The SELinux security context of a file, if available
10969 @item %d - Device number in decimal
10970 @item %D - Device number in hex
10971 @item %f - Raw mode in hex
10972 @item %F - File type
10973 @item %g - Group ID of owner
10974 @item %G - Group name of owner
10975 @item %h - Number of hard links
10976 @item %i - Inode number
10977 @item %m - Mount point (See note below)
10978 @item %n - File name
10979 @item %N - Quoted file name with dereference if symbolic link
10980 @item %o - I/O block size
10981 @item %s - Total size, in bytes
10982 @item %t - Major device type in hex
10983 @item %T - Minor device type in hex
10984 @item %u - User ID of owner
10985 @item %U - User name of owner
10986 @item %w - Time of file birth, or @samp{-} if unknown
10987 @item %W - Time of file birth as seconds since Epoch, or @samp{0}
10988 @item %x - Time of last access
10989 @item %X - Time of last access as seconds since Epoch
10990 @item %y - Time of last modification
10991 @item %Y - Time of last modification as seconds since Epoch
10992 @item %z - Time of last change
10993 @item %Z - Time of last change as seconds since Epoch
10994 @end itemize
10995
10996 The @samp{%W}, @samp{%X}, @samp{%Y}, and @samp{%Z} formats accept a
10997 precision preceded by a period to specify the number of digits to
10998 print after the decimal point.  For example, @samp{%.3X} outputs the
10999 last access time to millisecond precision.  If a period is given but no
11000 precision, @command{stat} uses 9 digits, so @samp{%.X} is equivalent to
11001 @samp{%.9X}.  When discarding excess precision, time stamps are truncated
11002 toward minus infinity.
11003
11004 @example
11005 zero pad:
11006   $ stat -c '[%015Y]' /usr
11007   [000001288929712]
11008 space align:
11009   $ stat -c '[%15Y]' /usr
11010   [     1288929712]
11011   $ stat -c '[%-15Y]' /usr
11012   [1288929712     ]
11013 precision:
11014   $ stat -c '[%.3Y]' /usr
11015   [1288929712.114]
11016   $ stat -c '[%.Y]' /usr
11017   [1288929712.114951834]
11018 @end example
11019
11020 The mount point printed by @samp{%m} is similar to that output
11021 by @command{df}, except that:
11022 @itemize @bullet
11023 @item
11024 stat does not dereference symlinks by default
11025 (unless @option{-L} is specified)
11026 @item
11027 stat does not search for specified device nodes in the
11028 file system list, instead operating on them directly
11029 @item
11030 @cindex bind mount
11031 stat outputs the alias for a bind mounted file, rather than
11032 the initial mount point of its backing device.
11033 One can recursively call stat until there is no change in output,
11034 to get the current base mount point
11035 @end itemize
11036
11037 When listing file system information (@option{--file-system} (@option{-f})),
11038 you must use a different set of @var{format} directives:
11039
11040 @itemize @bullet
11041 @item %a - Free blocks available to non-super-user
11042 @item %b - Total data blocks in file system
11043 @item %c - Total file nodes in file system
11044 @item %d - Free file nodes in file system
11045 @item %f - Free blocks in file system
11046 @item %i - File System ID in hex
11047 @item %l - Maximum length of file names
11048 @item %n - File name
11049 @item %s - Block size (for faster transfers)
11050 @item %S - Fundamental block size (for block counts)
11051 @item %t - Type in hex
11052 @item %T - Type in human readable form
11053 @end itemize
11054
11055 @vindex TZ
11056 Time stamps are listed according to the time zone rules specified by
11057 the @env{TZ} environment variable, or by the system default rules if
11058 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
11059 with @env{TZ}, libc, The GNU C Library Reference Manual}.
11060
11061 @exitstatus
11062
11063
11064 @node sync invocation
11065 @section @command{sync}: Synchronize data on disk with memory
11066
11067 @pindex sync
11068 @cindex synchronize disk and memory
11069
11070 @cindex superblock, writing
11071 @cindex inodes, written buffered
11072 @command{sync} writes any data buffered in memory out to disk.  This can
11073 include (but is not limited to) modified superblocks, modified inodes,
11074 and delayed reads and writes.  This must be implemented by the kernel;
11075 The @command{sync} program does nothing but exercise the @code{sync} system
11076 call.
11077
11078 @cindex crashes and corruption
11079 The kernel keeps data in memory to avoid doing (relatively slow) disk
11080 reads and writes.  This improves performance, but if the computer
11081 crashes, data may be lost or the file system corrupted as a
11082 result.  The @command{sync} command ensures everything in memory
11083 is written to disk.
11084
11085 Any arguments are ignored, except for a lone @option{--help} or
11086 @option{--version} (@pxref{Common options}).
11087
11088 @exitstatus
11089
11090
11091 @node truncate invocation
11092 @section @command{truncate}: Shrink or extend the size of a file
11093
11094 @pindex truncate
11095 @cindex truncating, file sizes
11096
11097 @command{truncate} shrinks or extends the size of each @var{file} to the
11098 specified size. Synopsis:
11099
11100 @example
11101 truncate @var{option}@dots{} @var{file}@dots{}
11102 @end example
11103
11104 @cindex files, creating
11105 Any @var{file} that does not exist is created.
11106
11107 @cindex sparse files, creating
11108 @cindex holes, creating files with
11109 If a @var{file} is larger than the specified size, the extra data is lost.
11110 If a @var{file} is shorter, it is extended and the extended part (or hole)
11111 reads as zero bytes.
11112
11113 The program accepts the following options.  Also see @ref{Common options}.
11114
11115 @table @samp
11116
11117 @item -c
11118 @itemx --no-create
11119 @opindex -c
11120 @opindex --no-create
11121 Do not create files that do not exist.
11122
11123 @item -o
11124 @itemx --io-blocks
11125 @opindex -o
11126 @opindex --io-blocks
11127 Treat @var{size} as number of I/O blocks of the @var{file} rather than bytes.
11128
11129 @item -r @var{rfile}
11130 @itemx --reference=@var{rfile}
11131 @opindex -r
11132 @opindex --reference
11133 Base the size of each @var{file} on the size of @var{rfile}.
11134
11135 @item -s @var{size}
11136 @itemx --size=@var{size}
11137 @opindex -s
11138 @opindex --size
11139 Set or adjust the size of each @var{file} according to @var{size}.
11140 @multiplierSuffixesNoBlocks{size}
11141
11142 @var{size} may also be prefixed by one of the following to adjust
11143 the size of each @var{file} based on their current size:
11144 @example
11145 @samp{+}  => extend by
11146 @samp{-}  => reduce by
11147 @samp{<}  => at most
11148 @samp{>}  => at least
11149 @samp{/}  => round down to multiple of
11150 @samp{%}  => round up to multiple of
11151 @end example
11152
11153 @end table
11154
11155 @exitstatus
11156
11157
11158 @node Printing text
11159 @chapter Printing text
11160
11161 @cindex printing text, commands for
11162 @cindex commands for printing text
11163
11164 This section describes commands that display text strings.
11165
11166 @menu
11167 * echo invocation::             Print a line of text.
11168 * printf invocation::           Format and print data.
11169 * yes invocation::              Print a string until interrupted.
11170 @end menu
11171
11172
11173 @node echo invocation
11174 @section @command{echo}: Print a line of text
11175
11176 @pindex echo
11177 @cindex displaying text
11178 @cindex printing text
11179 @cindex text, displaying
11180 @cindex arbitrary text, displaying
11181
11182 @command{echo} writes each given @var{string} to standard output, with a
11183 space between each and a newline after the last one.  Synopsis:
11184
11185 @example
11186 echo [@var{option}]@dots{} [@var{string}]@dots{}
11187 @end example
11188
11189 @mayConflictWithShellBuiltIn{echo}
11190
11191 The program accepts the following options.  Also see @ref{Common options}.
11192 Options must precede operands, and the normally-special argument
11193 @samp{--} has no special meaning and is treated like any other
11194 @var{string}.
11195
11196 @table @samp
11197 @item -n
11198 @opindex -n
11199 Do not output the trailing newline.
11200
11201 @item -e
11202 @opindex -e
11203 @cindex backslash escapes
11204 Enable interpretation of the following backslash-escaped characters in
11205 each @var{string}:
11206
11207 @table @samp
11208 @item \a
11209 alert (bell)
11210 @item \b
11211 backspace
11212 @item \c
11213 produce no further output
11214 @item \e
11215 escape
11216 @item \f
11217 form feed
11218 @item \n
11219 newline
11220 @item \r
11221 carriage return
11222 @item \t
11223 horizontal tab
11224 @item \v
11225 vertical tab
11226 @item \\
11227 backslash
11228 @item \0@var{nnn}
11229 the eight-bit value that is the octal number @var{nnn}
11230 (zero to three octal digits), if @var{nnn} is
11231 a nine-bit value, the ninth bit is ignored
11232 @item \@var{nnn}
11233 the eight-bit value that is the octal number @var{nnn}
11234 (one to three octal digits), if @var{nnn} is
11235 a nine-bit value, the ninth bit is ignored
11236 @item \x@var{hh}
11237 the eight-bit value that is the hexadecimal number @var{hh}
11238 (one or two hexadecimal digits)
11239 @end table
11240
11241 @item -E
11242 @opindex -E
11243 @cindex backslash escapes
11244 Disable interpretation of backslash escapes in each @var{string}.
11245 This is the default.  If @option{-e} and @option{-E} are both
11246 specified, the last one given takes effect.
11247
11248 @end table
11249
11250 @vindex POSIXLY_CORRECT
11251 If the @env{POSIXLY_CORRECT} environment variable is set, then when
11252 @command{echo}'s first argument is not @option{-n} it outputs
11253 option-like arguments instead of treating them as options.  For
11254 example, @code{echo -ne hello} outputs @samp{-ne hello} instead of
11255 plain @samp{hello}.
11256
11257 @acronym{POSIX} does not require support for any options, and says
11258 that the behavior of @command{echo} is implementation-defined if any
11259 @var{string} contains a backslash or if the first argument is
11260 @option{-n}.  Portable programs can use the @command{printf} command
11261 if they need to omit trailing newlines or output control characters or
11262 backslashes.  @xref{printf invocation}.
11263
11264 @exitstatus
11265
11266
11267 @node printf invocation
11268 @section @command{printf}: Format and print data
11269
11270 @pindex printf
11271 @command{printf} does formatted printing of text.  Synopsis:
11272
11273 @example
11274 printf @var{format} [@var{argument}]@dots{}
11275 @end example
11276
11277 @command{printf} prints the @var{format} string, interpreting @samp{%}
11278 directives and @samp{\} escapes to format numeric and string arguments
11279 in a way that is mostly similar to the C @samp{printf} function.
11280 @xref{Output Conversion Syntax,, @command{printf} format directives,
11281 libc, The GNU C Library Reference Manual}, for details.
11282 The differences are listed below.
11283
11284 @mayConflictWithShellBuiltIn{printf}
11285
11286 @itemize @bullet
11287
11288 @item
11289 The @var{format} argument is reused as necessary to convert all the
11290 given @var{argument}s.  For example, the command @samp{printf %s a b}
11291 outputs @samp{ab}.
11292
11293 @item
11294 Missing @var{argument}s are treated as null strings or as zeros,
11295 depending on whether the context expects a string or a number.  For
11296 example, the command @samp{printf %sx%d} prints @samp{x0}.
11297
11298 @item
11299 @kindex \c
11300 An additional escape, @samp{\c}, causes @command{printf} to produce no
11301 further output.  For example, the command @samp{printf 'A%sC\cD%sF' B
11302 E} prints @samp{ABC}.
11303
11304 @item
11305 The hexadecimal escape sequence @samp{\x@var{hh}} has at most two
11306 digits, as opposed to C where it can have an unlimited number of
11307 digits.  For example, the command @samp{printf '\x07e'} prints two
11308 bytes, whereas the C statement @samp{printf ("\x07e")} prints just
11309 one.
11310
11311 @item
11312 @kindex %b
11313 @command{printf} has an additional directive, @samp{%b}, which prints its
11314 argument string with @samp{\} escapes interpreted in the same way as in
11315 the @var{format} string, except that octal escapes are of the form
11316 @samp{\0@var{ooo}} where @var{ooo} is 0 to 3 octal digits.  If
11317 @samp{\@var{ooo}} is nine-bit value, ignore the ninth bit.
11318 If a precision is also given, it limits the number of bytes printed
11319 from the converted string.
11320
11321 @item
11322 Numeric arguments must be single C constants, possibly with leading
11323 @samp{+} or @samp{-}.  For example, @samp{printf %.4d -3} outputs
11324 @samp{-0003}.
11325
11326 @item
11327 @vindex POSIXLY_CORRECT
11328 If the leading character of a numeric argument is @samp{"} or @samp{'}
11329 then its value is the numeric value of the immediately following
11330 character.  Any remaining characters are silently ignored if the
11331 @env{POSIXLY_CORRECT} environment variable is set; otherwise, a
11332 warning is printed.  For example, @samp{printf "%d" "'a"} outputs
11333 @samp{97} on hosts that use the @acronym{ASCII} character set, since
11334 @samp{a} has the numeric value 97 in @acronym{ASCII}.
11335
11336 @end itemize
11337
11338 @vindex LC_NUMERIC
11339 A floating-point argument must use a period before any fractional
11340 digits, but is printed according to the @env{LC_NUMERIC} category of the
11341 current locale.  For example, in a locale whose radix character is a
11342 comma, the command @samp{printf %g 3.14} outputs @samp{3,14} whereas
11343 the command @samp{printf %g 3,14} is an error.
11344 @xref{Floating point}.
11345
11346 @kindex \@var{ooo}
11347 @kindex \x@var{hh}
11348 @command{printf} interprets @samp{\@var{ooo}} in @var{format} as an octal number
11349 (if @var{ooo} is 1 to 3 octal digits) specifying a byte to print,
11350 and @samp{\x@var{hh}} as a hexadecimal number (if @var{hh} is 1 to 2 hex
11351 digits) specifying a character to print.
11352 Note however that when @samp{\@var{ooo}} specifies a number larger than 255,
11353 @command{printf} ignores the ninth bit.
11354 For example, @samp{printf '\400'} is equivalent to @samp{printf '\0'}.
11355
11356 @kindex \uhhhh
11357 @kindex \Uhhhhhhhh
11358 @cindex Unicode
11359 @cindex ISO/IEC 10646
11360 @vindex LC_CTYPE
11361 @command{printf} interprets two character syntaxes introduced in
11362 @acronym{ISO} C 99:
11363 @samp{\u} for 16-bit Unicode (@acronym{ISO}/@acronym{IEC} 10646)
11364 characters, specified as
11365 four hexadecimal digits @var{hhhh}, and @samp{\U} for 32-bit Unicode
11366 characters, specified as eight hexadecimal digits @var{hhhhhhhh}.
11367 @command{printf} outputs the Unicode characters
11368 according to the @env{LC_CTYPE} locale.  Unicode characters in the ranges
11369 U+0000...U+009F, U+D800...U+DFFF cannot be specified by this syntax, except
11370 for U+0024 ($), U+0040 (@@), and U+0060 (@`).
11371
11372 The processing of @samp{\u} and @samp{\U} requires a full-featured
11373 @code{iconv} facility.  It is activated on systems with glibc 2.2 (or newer),
11374 or when @code{libiconv} is installed prior to this package.  Otherwise
11375 @samp{\u} and @samp{\U} will print as-is.
11376
11377 The only options are a lone @option{--help} or
11378 @option{--version}.  @xref{Common options}.
11379 Options must precede operands.
11380
11381 The Unicode character syntaxes are useful for writing strings in a locale
11382 independent way.  For example, a string containing the Euro currency symbol
11383
11384 @example
11385 $ env printf '\u20AC 14.95'
11386 @end example
11387
11388 @noindent
11389 will be output correctly in all locales supporting the Euro symbol
11390 (@acronym{ISO}-8859-15, UTF-8, and others).  Similarly, a Chinese string
11391
11392 @example
11393 $ env printf '\u4e2d\u6587'
11394 @end example
11395
11396 @noindent
11397 will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).
11398
11399 Note that in these examples, the @command{printf} command has been
11400 invoked via @command{env} to ensure that we run the program found via
11401 your shell's search path, and not a shell alias or a built-in function.
11402
11403 For larger strings, you don't need to look up the hexadecimal code
11404 values of each character one by one.  @acronym{ASCII} characters mixed with \u
11405 escape sequences is also known as the JAVA source file encoding.  You can
11406 use GNU recode 3.5c (or newer) to convert strings to this encoding.  Here
11407 is how to convert a piece of text into a shell script which will output
11408 this text in a locale-independent way:
11409
11410 @smallexample
11411 $ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
11412     '\u4e2d\u6587\n' > sample.txt
11413 $ recode BIG5..JAVA < sample.txt \
11414     | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
11415     > sample.sh
11416 @end smallexample
11417
11418 @exitstatus
11419
11420
11421 @node yes invocation
11422 @section @command{yes}: Print a string until interrupted
11423
11424 @pindex yes
11425 @cindex repeated output of a string
11426
11427 @command{yes} prints the command line arguments, separated by spaces and
11428 followed by a newline, forever until it is killed.  If no arguments are
11429 given, it prints @samp{y} followed by a newline forever until killed.
11430
11431 Upon a write error, @command{yes} exits with status @samp{1}.
11432
11433 The only options are a lone @option{--help} or @option{--version}.
11434 To output an argument that begins with
11435 @samp{-}, precede it with @option{--}, e.g., @samp{yes -- --help}.
11436 @xref{Common options}.
11437
11438
11439 @node Conditions
11440 @chapter Conditions
11441
11442 @cindex conditions
11443 @cindex commands for exit status
11444 @cindex exit status commands
11445
11446 This section describes commands that are primarily useful for their exit
11447 status, rather than their output.  Thus, they are often used as the
11448 condition of shell @code{if} statements, or as the last command in a
11449 pipeline.
11450
11451 @menu
11452 * false invocation::            Do nothing, unsuccessfully.
11453 * true invocation::             Do nothing, successfully.
11454 * test invocation::             Check file types and compare values.
11455 * expr invocation::             Evaluate expressions.
11456 @end menu
11457
11458
11459 @node false invocation
11460 @section @command{false}: Do nothing, unsuccessfully
11461
11462 @pindex false
11463 @cindex do nothing, unsuccessfully
11464 @cindex failure exit status
11465 @cindex exit status of @command{false}
11466
11467 @command{false} does nothing except return an exit status of 1, meaning
11468 @dfn{failure}.  It can be used as a place holder in shell scripts
11469 where an unsuccessful command is needed.
11470 In most modern shells, @command{false} is a built-in command, so when
11471 you use @samp{false} in a script, you're probably using the built-in
11472 command, not the one documented here.
11473
11474 @command{false} honors the @option{--help} and @option{--version} options.
11475
11476 This version of @command{false} is implemented as a C program, and is thus
11477 more secure and faster than a shell script implementation, and may safely
11478 be used as a dummy shell for the purpose of disabling accounts.
11479
11480 Note that @command{false} (unlike all other programs documented herein)
11481 exits unsuccessfully, even when invoked with
11482 @option{--help} or @option{--version}.
11483
11484 Portable programs should not assume that the exit status of
11485 @command{false} is 1, as it is greater than 1 on some
11486 non-@acronym{GNU} hosts.
11487
11488
11489 @node true invocation
11490 @section @command{true}: Do nothing, successfully
11491
11492 @pindex true
11493 @cindex do nothing, successfully
11494 @cindex no-op
11495 @cindex successful exit
11496 @cindex exit status of @command{true}
11497
11498 @command{true} does nothing except return an exit status of 0, meaning
11499 @dfn{success}.  It can be used as a place holder in shell scripts
11500 where a successful command is needed, although the shell built-in
11501 command @code{:} (colon) may do the same thing faster.
11502 In most modern shells, @command{true} is a built-in command, so when
11503 you use @samp{true} in a script, you're probably using the built-in
11504 command, not the one documented here.
11505
11506 @command{true} honors the @option{--help} and @option{--version} options.
11507
11508 Note, however, that it is possible to cause @command{true}
11509 to exit with nonzero status: with the @option{--help} or @option{--version}
11510 option, and with standard
11511 output already closed or redirected to a file that evokes an I/O error.
11512 For example, using a Bourne-compatible shell:
11513
11514 @example
11515 $ ./true --version >&-
11516 ./true: write error: Bad file number
11517 $ ./true --version > /dev/full
11518 ./true: write error: No space left on device
11519 @end example
11520
11521 This version of @command{true} is implemented as a C program, and is thus
11522 more secure and faster than a shell script implementation, and may safely
11523 be used as a dummy shell for the purpose of disabling accounts.
11524
11525 @node test invocation
11526 @section @command{test}: Check file types and compare values
11527
11528 @pindex test
11529 @cindex check file types
11530 @cindex compare values
11531 @cindex expression evaluation
11532
11533 @command{test} returns a status of 0 (true) or 1 (false) depending on the
11534 evaluation of the conditional expression @var{expr}.  Each part of the
11535 expression must be a separate argument.
11536
11537 @command{test} has file status checks, string operators, and numeric
11538 comparison operators.
11539
11540 @command{test} has an alternate form that uses opening and closing
11541 square brackets instead a leading @samp{test}.  For example, instead
11542 of @samp{test -d /}, you can write @samp{[ -d / ]}.  The square
11543 brackets must be separate arguments; for example, @samp{[-d /]} does
11544 not have the desired effect.  Since @samp{test @var{expr}} and @samp{[
11545 @var{expr} ]} have the same meaning, only the former form is discussed
11546 below.
11547
11548 Synopses:
11549
11550 @example
11551 test @var{expression}
11552 test
11553 [ @var{expression} ]
11554 [ ]
11555 [ @var{option}
11556 @end example
11557
11558 @mayConflictWithShellBuiltIn{test}
11559
11560 If @var{expression} is omitted, @command{test} returns false.
11561 If @var{expression} is a single argument,
11562 @command{test} returns false if the argument is null and true
11563 otherwise.  The argument
11564 can be any string, including strings like @samp{-d}, @samp{-1},
11565 @samp{--}, @samp{--help}, and @samp{--version} that most other
11566 programs would treat as options.  To get help and version information,
11567 invoke the commands @samp{[ --help} and @samp{[ --version}, without
11568 the usual closing brackets.  @xref{Common options}.
11569
11570 @cindex exit status of @command{test}
11571 Exit status:
11572
11573 @display
11574 0 if the expression is true,
11575 1 if the expression is false,
11576 2 if an error occurred.
11577 @end display
11578
11579 @menu
11580 * File type tests::             -[bcdfhLpSt]
11581 * Access permission tests::     -[gkruwxOG]
11582 * File characteristic tests::   -e -s -nt -ot -ef
11583 * String tests::                -z -n = == !=
11584 * Numeric tests::               -eq -ne -lt -le -gt -ge
11585 * Connectives for test::        ! -a -o
11586 @end menu
11587
11588
11589 @node File type tests
11590 @subsection File type tests
11591
11592 @cindex file type tests
11593
11594 These options test for particular types of files.  (Everything's a file,
11595 but not all files are the same!)
11596
11597 @table @samp
11598
11599 @item -b @var{file}
11600 @opindex -b
11601 @cindex block special check
11602 True if @var{file} exists and is a block special device.
11603
11604 @item -c @var{file}
11605 @opindex -c
11606 @cindex character special check
11607 True if @var{file} exists and is a character special device.
11608
11609 @item -d @var{file}
11610 @opindex -d
11611 @cindex directory check
11612 True if @var{file} exists and is a directory.
11613
11614 @item -f @var{file}
11615 @opindex -f
11616 @cindex regular file check
11617 True if @var{file} exists and is a regular file.
11618
11619 @item -h @var{file}
11620 @itemx -L @var{file}
11621 @opindex -L
11622 @opindex -h
11623 @cindex symbolic link check
11624 True if @var{file} exists and is a symbolic link.
11625 Unlike all other file-related tests, this test does not dereference
11626 @var{file} if it is a symbolic link.
11627
11628 @item -p @var{file}
11629 @opindex -p
11630 @cindex named pipe check
11631 True if @var{file} exists and is a named pipe.
11632
11633 @item -S @var{file}
11634 @opindex -S
11635 @cindex socket check
11636 True if @var{file} exists and is a socket.
11637
11638 @item -t @var{fd}
11639 @opindex -t
11640 @cindex terminal check
11641 True if @var{fd} is a file descriptor that is associated with a
11642 terminal.
11643
11644 @end table
11645
11646
11647 @node Access permission tests
11648 @subsection Access permission tests
11649
11650 @cindex access permission tests
11651 @cindex permission tests
11652
11653 These options test for particular access permissions.
11654
11655 @table @samp
11656
11657 @item -g @var{file}
11658 @opindex -g
11659 @cindex set-group-ID check
11660 True if @var{file} exists and has its set-group-ID bit set.
11661
11662 @item -k @var{file}
11663 @opindex -k
11664 @cindex sticky bit check
11665 True if @var{file} exists and has its @dfn{sticky} bit set.
11666
11667 @item -r @var{file}
11668 @opindex -r
11669 @cindex readable file check
11670 True if @var{file} exists and read permission is granted.
11671
11672 @item -u @var{file}
11673 @opindex -u
11674 @cindex set-user-ID check
11675 True if @var{file} exists and has its set-user-ID bit set.
11676
11677 @item -w @var{file}
11678 @opindex -w
11679 @cindex writable file check
11680 True if @var{file} exists and write permission is granted.
11681
11682 @item -x @var{file}
11683 @opindex -x
11684 @cindex executable file check
11685 True if @var{file} exists and execute permission is granted
11686 (or search permission, if it is a directory).
11687
11688 @item -O @var{file}
11689 @opindex -O
11690 @cindex owned by effective user ID check
11691 True if @var{file} exists and is owned by the current effective user ID.
11692
11693 @item -G @var{file}
11694 @opindex -G
11695 @cindex owned by effective group ID check
11696 True if @var{file} exists and is owned by the current effective group ID.
11697
11698 @end table
11699
11700 @node File characteristic tests
11701 @subsection File characteristic tests
11702
11703 @cindex file characteristic tests
11704
11705 These options test other file characteristics.
11706
11707 @table @samp
11708
11709 @item -e @var{file}
11710 @opindex -e
11711 @cindex existence-of-file check
11712 True if @var{file} exists.
11713
11714 @item -s @var{file}
11715 @opindex -s
11716 @cindex nonempty file check
11717 True if @var{file} exists and has a size greater than zero.
11718
11719 @item @var{file1} -nt @var{file2}
11720 @opindex -nt
11721 @cindex newer-than file check
11722 True if @var{file1} is newer (according to modification date) than
11723 @var{file2}, or if @var{file1} exists and @var{file2} does not.
11724
11725 @item @var{file1} -ot @var{file2}
11726 @opindex -ot
11727 @cindex older-than file check
11728 True if @var{file1} is older (according to modification date) than
11729 @var{file2}, or if @var{file2} exists and @var{file1} does not.
11730
11731 @item @var{file1} -ef @var{file2}
11732 @opindex -ef
11733 @cindex same file check
11734 @cindex hard link check
11735 True if @var{file1} and @var{file2} have the same device and inode
11736 numbers, i.e., if they are hard links to each other.
11737
11738 @end table
11739
11740
11741 @node String tests
11742 @subsection String tests
11743
11744 @cindex string tests
11745
11746 These options test string characteristics.  You may need to quote
11747 @var{string} arguments for the shell.  For example:
11748
11749 @example
11750 test -n "$V"
11751 @end example
11752
11753 The quotes here prevent the wrong arguments from being passed to
11754 @command{test} if @samp{$V} is empty or contains special characters.
11755
11756 @table @samp
11757
11758 @item -z @var{string}
11759 @opindex -z
11760 @cindex zero-length string check
11761 True if the length of @var{string} is zero.
11762
11763 @item -n @var{string}
11764 @itemx @var{string}
11765 @opindex -n
11766 @cindex nonzero-length string check
11767 True if the length of @var{string} is nonzero.
11768
11769 @item @var{string1} = @var{string2}
11770 @opindex =
11771 @cindex equal string check
11772 True if the strings are equal.
11773
11774 @item @var{string1} == @var{string2}
11775 @opindex ==
11776 @cindex equal string check
11777 True if the strings are equal (synonym for =).
11778
11779 @item @var{string1} != @var{string2}
11780 @opindex !=
11781 @cindex not-equal string check
11782 True if the strings are not equal.
11783
11784 @end table
11785
11786
11787 @node Numeric tests
11788 @subsection Numeric tests
11789
11790 @cindex numeric tests
11791 @cindex arithmetic tests
11792
11793 Numeric relational operators.  The arguments must be entirely numeric
11794 (possibly negative), or the special expression @w{@code{-l @var{string}}},
11795 which evaluates to the length of @var{string}.
11796
11797 @table @samp
11798
11799 @item @var{arg1} -eq @var{arg2}
11800 @itemx @var{arg1} -ne @var{arg2}
11801 @itemx @var{arg1} -lt @var{arg2}
11802 @itemx @var{arg1} -le @var{arg2}
11803 @itemx @var{arg1} -gt @var{arg2}
11804 @itemx @var{arg1} -ge @var{arg2}
11805 @opindex -eq
11806 @opindex -ne
11807 @opindex -lt
11808 @opindex -le
11809 @opindex -gt
11810 @opindex -ge
11811 These arithmetic binary operators return true if @var{arg1} is equal,
11812 not-equal, less-than, less-than-or-equal, greater-than, or
11813 greater-than-or-equal than @var{arg2}, respectively.
11814
11815 @end table
11816
11817 For example:
11818
11819 @example
11820 test -1 -gt -2 && echo yes
11821 @result{} yes
11822 test -l abc -gt 1 && echo yes
11823 @result{} yes
11824 test 0x100 -eq 1
11825 @error{} test: integer expression expected before -eq
11826 @end example
11827
11828
11829 @node Connectives for test
11830 @subsection Connectives for @command{test}
11831
11832 @cindex logical connectives
11833 @cindex connectives, logical
11834
11835 The usual logical connectives.
11836
11837 @table @samp
11838
11839 @item ! @var{expr}
11840 @opindex !
11841 True if @var{expr} is false.
11842
11843 @item @var{expr1} -a @var{expr2}
11844 @opindex -a
11845 @cindex logical and operator
11846 @cindex and operator
11847 True if both @var{expr1} and @var{expr2} are true.
11848
11849 @item @var{expr1} -o @var{expr2}
11850 @opindex -o
11851 @cindex logical or operator
11852 @cindex or operator
11853 True if either @var{expr1} or @var{expr2} is true.
11854
11855 @end table
11856
11857
11858 @node expr invocation
11859 @section @command{expr}: Evaluate expressions
11860
11861 @pindex expr
11862 @cindex expression evaluation
11863 @cindex evaluation of expressions
11864
11865 @command{expr} evaluates an expression and writes the result on standard
11866 output.  Each token of the expression must be a separate argument.
11867
11868 Operands are either integers or strings.  Integers consist of one or
11869 more decimal digits, with an optional leading @samp{-}.
11870 @command{expr} converts
11871 anything appearing in an operand position to an integer or a string
11872 depending on the operation being applied to it.
11873
11874 Strings are not quoted for @command{expr} itself, though you may need to
11875 quote them to protect characters with special meaning to the shell,
11876 e.g., spaces.  However, regardless of whether it is quoted, a string
11877 operand should not be a parenthesis or any of @command{expr}'s
11878 operators like @code{+}, so you cannot safely pass an arbitrary string
11879 @code{$str} to expr merely by quoting it to the shell.  One way to
11880 work around this is to use the @sc{gnu} extension @code{+},
11881 (e.g., @code{+ "$str" = foo}); a more portable way is to use
11882 @code{@w{" $str"}} and to adjust the rest of the expression to take
11883 the leading space into account (e.g., @code{@w{" $str" = " foo"}}).
11884
11885 You should not pass a negative integer or a string with leading
11886 @samp{-} as @command{expr}'s first argument, as it might be
11887 misinterpreted as an option; this can be avoided by parenthesization.
11888 Also, portable scripts should not use a string operand that happens to
11889 take the form of an integer; this can be worked around by inserting
11890 leading spaces as mentioned above.
11891
11892 @cindex parentheses for grouping
11893 Operators may be given as infix symbols or prefix keywords.  Parentheses
11894 may be used for grouping in the usual manner.  You must quote
11895 parentheses and many operators to avoid the shell evaluating them,
11896 however.
11897
11898 When built with support for the GNU MP library, @command{expr} uses
11899 arbitrary-precision arithmetic; otherwise, it uses native arithmetic
11900 types and may fail due to arithmetic overflow.
11901
11902 The only options are @option{--help} and @option{--version}.  @xref{Common
11903 options}.  Options must precede operands.
11904
11905 @cindex exit status of @command{expr}
11906 Exit status:
11907
11908 @display
11909 0 if the expression is neither null nor 0,
11910 1 if the expression is null or 0,
11911 2 if the expression is invalid,
11912 3 if an internal error occurred (e.g., arithmetic overflow).
11913 @end display
11914
11915 @menu
11916 * String expressions::          + : match substr index length
11917 * Numeric expressions::         + - * / %
11918 * Relations for expr::          | & < <= = == != >= >
11919 * Examples of expr::            Examples.
11920 @end menu
11921
11922
11923 @node String expressions
11924 @subsection String expressions
11925
11926 @cindex string expressions
11927 @cindex expressions, string
11928
11929 @command{expr} supports pattern matching and other string operators.  These
11930 have higher precedence than both the numeric and relational operators (in
11931 the next sections).
11932
11933 @table @samp
11934
11935 @item @var{string} : @var{regex}
11936 @cindex pattern matching
11937 @cindex regular expression matching
11938 @cindex matching patterns
11939 Perform pattern matching.  The arguments are converted to strings and the
11940 second is considered to be a (basic, a la GNU @code{grep}) regular
11941 expression, with a @code{^} implicitly prepended.  The first argument is
11942 then matched against this regular expression.
11943
11944 If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
11945 @code{:} expression returns the part of @var{string} that matched the
11946 subexpression; otherwise, it returns the number of characters matched.
11947
11948 If the match fails, the @code{:} operator returns the null string if
11949 @samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
11950
11951 @kindex \( @r{regexp operator}
11952 Only the first @samp{\( @dots{} \)} pair is relevant to the return
11953 value; additional pairs are meaningful only for grouping the regular
11954 expression operators.
11955
11956 @kindex \+ @r{regexp operator}
11957 @kindex \? @r{regexp operator}
11958 @kindex \| @r{regexp operator}
11959 In the regular expression, @code{\+}, @code{\?}, and @code{\|} are
11960 operators which respectively match one or more, zero or one, or separate
11961 alternatives.  SunOS and other @command{expr}'s treat these as regular
11962 characters.  (@acronym{POSIX} allows either behavior.)
11963 @xref{Top, , Regular Expression Library, regex, Regex}, for details of
11964 regular expression syntax.  Some examples are in @ref{Examples of expr}.
11965
11966 @item match @var{string} @var{regex}
11967 @findex match
11968 An alternative way to do pattern matching.  This is the same as
11969 @w{@samp{@var{string} : @var{regex}}}.
11970
11971 @item substr @var{string} @var{position} @var{length}
11972 @findex substr
11973 Returns the substring of @var{string} beginning at @var{position}
11974 with length at most @var{length}.  If either @var{position} or
11975 @var{length} is negative, zero, or non-numeric, returns the null string.
11976
11977 @item index @var{string} @var{charset}
11978 @findex index
11979 Returns the first position in @var{string} where the first character in
11980 @var{charset} was found.  If no character in @var{charset} is found in
11981 @var{string}, return 0.
11982
11983 @item length @var{string}
11984 @findex length
11985 Returns the length of @var{string}.
11986
11987 @item + @var{token}
11988 @kindex +
11989 Interpret @var{token} as a string, even if it is a keyword like @var{match}
11990 or an operator like @code{/}.
11991 This makes it possible to test @code{expr length + "$x"} or
11992 @code{expr + "$x" : '.*/\(.\)'} and have it do the right thing even if
11993 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
11994 This operator is a @acronym{GNU} extension.  Portable shell scripts should use
11995 @code{@w{" $token"} : @w{' \(.*\)'}} instead of @code{+ "$token"}.
11996
11997 @end table
11998
11999 To make @command{expr} interpret keywords as strings, you must use the
12000 @code{quote} operator.
12001
12002
12003 @node Numeric expressions
12004 @subsection Numeric expressions
12005
12006 @cindex numeric expressions
12007 @cindex expressions, numeric
12008
12009 @command{expr} supports the usual numeric operators, in order of increasing
12010 precedence.  These numeric operators have lower precedence than the
12011 string operators described in the previous section, and higher precedence
12012 than the connectives (next section).
12013
12014 @table @samp
12015
12016 @item + -
12017 @kindex +
12018 @kindex -
12019 @cindex addition
12020 @cindex subtraction
12021 Addition and subtraction.  Both arguments are converted to integers;
12022 an error occurs if this cannot be done.
12023
12024 @item * / %
12025 @kindex *
12026 @kindex /
12027 @kindex %
12028 @cindex multiplication
12029 @cindex division
12030 @cindex remainder
12031 Multiplication, division, remainder.  Both arguments are converted to
12032 integers; an error occurs if this cannot be done.
12033
12034 @end table
12035
12036
12037 @node Relations for expr
12038 @subsection Relations for @command{expr}
12039
12040 @cindex connectives, logical
12041 @cindex logical connectives
12042 @cindex relations, numeric or string
12043
12044 @command{expr} supports the usual logical connectives and relations.  These
12045 have lower precedence than the string and numeric operators
12046 (previous sections).  Here is the list, lowest-precedence operator first.
12047
12048 @table @samp
12049
12050 @item |
12051 @kindex |
12052 @cindex logical or operator
12053 @cindex or operator
12054 Returns its first argument if that is neither null nor zero, otherwise
12055 its second argument if it is neither null nor zero, otherwise 0.  It
12056 does not evaluate its second argument if its first argument is neither
12057 null nor zero.
12058
12059 @item &
12060 @kindex &
12061 @cindex logical and operator
12062 @cindex and operator
12063 Return its first argument if neither argument is null or zero, otherwise
12064 0.  It does not evaluate its second argument if its first argument is
12065 null or zero.
12066
12067 @item < <= = == != >= >
12068 @kindex <
12069 @kindex <=
12070 @kindex =
12071 @kindex ==
12072 @kindex >
12073 @kindex >=
12074 @cindex comparison operators
12075 @vindex LC_COLLATE
12076 Compare the arguments and return 1 if the relation is true, 0 otherwise.
12077 @code{==} is a synonym for @code{=}.  @command{expr} first tries to convert
12078 both arguments to integers and do a numeric comparison; if either
12079 conversion fails, it does a lexicographic comparison using the character
12080 collating sequence specified by the @env{LC_COLLATE} locale.
12081
12082 @end table
12083
12084
12085 @node Examples of expr
12086 @subsection Examples of using @command{expr}
12087
12088 @cindex examples of @command{expr}
12089 Here are a few examples, including quoting for shell metacharacters.
12090
12091 To add 1 to the shell variable @code{foo}, in Bourne-compatible shells:
12092
12093 @example
12094 foo=`expr $foo + 1`
12095 @end example
12096
12097 To print the non-directory part of the file name stored in
12098 @code{$fname}, which need not contain a @code{/}:
12099
12100 @example
12101 expr $fname : '.*/\(.*\)' '|' $fname
12102 @end example
12103
12104 An example showing that @code{\+} is an operator:
12105
12106 @example
12107 expr aaa : 'a\+'
12108 @result{} 3
12109 @end example
12110
12111 @example
12112 expr abc : 'a\(.\)c'
12113 @result{} b
12114 expr index abcdef cz
12115 @result{} 3
12116 expr index index a
12117 @error{} expr: syntax error
12118 expr index + index a
12119 @result{} 0
12120 @end example
12121
12122
12123 @node Redirection
12124 @chapter Redirection
12125
12126 @cindex redirection
12127 @cindex commands for redirection
12128
12129 Unix shells commonly provide several forms of @dfn{redirection}---ways
12130 to change the input source or output destination of a command.  But one
12131 useful redirection is performed by a separate command, not by the shell;
12132 it's described here.
12133
12134 @menu
12135 * tee invocation::              Redirect output to multiple files or processes.
12136 @end menu
12137
12138
12139 @node tee invocation
12140 @section @command{tee}: Redirect output to multiple files or processes
12141
12142 @pindex tee
12143 @cindex pipe fitting
12144 @cindex destinations, multiple output
12145 @cindex read from stdin and write to stdout and files
12146
12147 The @command{tee} command copies standard input to standard output and also
12148 to any files given as arguments.  This is useful when you want not only
12149 to send some data down a pipe, but also to save a copy.  Synopsis:
12150
12151 @example
12152 tee [@var{option}]@dots{} [@var{file}]@dots{}
12153 @end example
12154
12155 If a file being written to does not already exist, it is created.  If a
12156 file being written to already exists, the data it previously contained
12157 is overwritten unless the @option{-a} option is used.
12158
12159 A @var{file} of @samp{-} causes @command{tee} to send another copy of
12160 input to standard output, but this is typically not that useful as the
12161 copies are interleaved.
12162
12163 The program accepts the following options.  Also see @ref{Common options}.
12164
12165 @table @samp
12166 @item -a
12167 @itemx --append
12168 @opindex -a
12169 @opindex --append
12170 Append standard input to the given files rather than overwriting
12171 them.
12172
12173 @item -i
12174 @itemx --ignore-interrupts
12175 @opindex -i
12176 @opindex --ignore-interrupts
12177 Ignore interrupt signals.
12178
12179 @end table
12180
12181 The @command{tee} command is useful when you happen to be transferring a large
12182 amount of data and also want to summarize that data without reading
12183 it a second time.  For example, when you are downloading a DVD image,
12184 you often want to verify its signature or checksum right away.
12185 The inefficient way to do it is simply:
12186
12187 @example
12188 wget http://example.com/some.iso && sha1sum some.iso
12189 @end example
12190
12191 One problem with the above is that it makes you wait for the
12192 download to complete before starting the time-consuming SHA1 computation.
12193 Perhaps even more importantly, the above requires reading
12194 the DVD image a second time (the first was from the network).
12195
12196 The efficient way to do it is to interleave the download
12197 and SHA1 computation.  Then, you'll get the checksum for
12198 free, because the entire process parallelizes so well:
12199
12200 @example
12201 # slightly contrived, to demonstrate process substitution
12202 wget -O - http://example.com/dvd.iso \
12203   | tee >(sha1sum > dvd.sha1) > dvd.iso
12204 @end example
12205
12206 That makes @command{tee} write not just to the expected output file,
12207 but also to a pipe running @command{sha1sum} and saving the final
12208 checksum in a file named @file{dvd.sha1}.
12209
12210 Note, however, that this example relies on a feature of modern shells
12211 called @dfn{process substitution}
12212 (the @samp{>(command)} syntax, above;
12213 @xref{Process Substitution,,Process Substitution, bashref,
12214 The Bash Reference Manual}.),
12215 so it works with @command{zsh}, @command{bash}, and @command{ksh},
12216 but not with @command{/bin/sh}.  So if you write code like this
12217 in a shell script, be sure to start the script with @samp{#!/bin/bash}.
12218
12219 Since the above example writes to one file and one process,
12220 a more conventional and portable use of @command{tee} is even better:
12221
12222 @example
12223 wget -O - http://example.com/dvd.iso \
12224   | tee dvd.iso | sha1sum > dvd.sha1
12225 @end example
12226
12227 You can extend this example to make @command{tee} write to two processes,
12228 computing MD5 and SHA1 checksums in parallel.  In this case,
12229 process substitution is required:
12230
12231 @example
12232 wget -O - http://example.com/dvd.iso \
12233   | tee >(sha1sum > dvd.sha1) \
12234         >(md5sum > dvd.md5) \
12235   > dvd.iso
12236 @end example
12237
12238 This technique is also useful when you want to make a @emph{compressed}
12239 copy of the contents of a pipe.
12240 Consider a tool to graphically summarize disk usage data from @samp{du -ak}.
12241 For a large hierarchy, @samp{du -ak} can run for a long time,
12242 and can easily produce terabytes of data, so you won't want to
12243 rerun the command unnecessarily.  Nor will you want to save
12244 the uncompressed output.
12245
12246 Doing it the inefficient way, you can't even start the GUI
12247 until after you've compressed all of the @command{du} output:
12248
12249 @example
12250 du -ak | gzip -9 > /tmp/du.gz
12251 gzip -d /tmp/du.gz | xdiskusage -a
12252 @end example
12253
12254 With @command{tee} and process substitution, you start the GUI
12255 right away and eliminate the decompression completely:
12256
12257 @example
12258 du -ak | tee >(gzip -9 > /tmp/du.gz) | xdiskusage -a
12259 @end example
12260
12261 Finally, if you regularly create more than one type of
12262 compressed tarball at once, for example when @code{make dist} creates
12263 both @command{gzip}-compressed and @command{bzip2}-compressed tarballs,
12264 there may be a better way.
12265 Typical @command{automake}-generated @file{Makefile} rules create
12266 the two compressed tar archives with commands in sequence, like this
12267 (slightly simplified):
12268
12269 @example
12270 tardir=your-pkg-M.N
12271 tar chof - "$tardir" | gzip  -9 -c > your-pkg-M.N.tar.gz
12272 tar chof - "$tardir" | bzip2 -9 -c > your-pkg-M.N.tar.bz2
12273 @end example
12274
12275 However, if the hierarchy you are archiving and compressing is larger
12276 than a couple megabytes, and especially if you are using a multi-processor
12277 system with plenty of memory, then you can do much better by reading the
12278 directory contents only once and running the compression programs in parallel:
12279
12280 @example
12281 tardir=your-pkg-M.N
12282 tar chof - "$tardir" \
12283   | tee >(gzip -9 -c > your-pkg-M.N.tar.gz) \
12284   | bzip2 -9 -c > your-pkg-M.N.tar.bz2
12285 @end example
12286
12287 @exitstatus
12288
12289
12290 @node File name manipulation
12291 @chapter File name manipulation
12292
12293 @cindex file name manipulation
12294 @cindex manipulation of file names
12295 @cindex commands for file name manipulation
12296
12297 This section describes commands that manipulate file names.
12298
12299 @menu
12300 * basename invocation::         Strip directory and suffix from a file name.
12301 * dirname invocation::          Strip last file name component.
12302 * pathchk invocation::          Check file name validity and portability.
12303 * mktemp invocation::           Create temporary file or directory.
12304 @end menu
12305
12306
12307 @node basename invocation
12308 @section @command{basename}: Strip directory and suffix from a file name
12309
12310 @pindex basename
12311 @cindex strip directory and suffix from file names
12312 @cindex directory, stripping from file names
12313 @cindex suffix, stripping from file names
12314 @cindex file names, stripping directory and suffix
12315 @cindex leading directory components, stripping
12316
12317 @command{basename} removes any leading directory components from
12318 @var{name}.  Synopsis:
12319
12320 @example
12321 basename @var{name} [@var{suffix}]
12322 @end example
12323
12324 If @var{suffix} is specified and is identical to the end of @var{name},
12325 it is removed from @var{name} as well.  Note that since trailing slashes
12326 are removed prior to suffix matching, @var{suffix} will do nothing if it
12327 contains slashes.  @command{basename} prints the result on standard
12328 output.
12329
12330 @c This test is used both here and in the section on dirname.
12331 @macro basenameAndDirname
12332 Together, @command{basename} and @command{dirname} are designed such
12333 that if @samp{ls "$name"} succeeds, then the command sequence @samp{cd
12334 "$(dirname "$name")"; ls "$(basename "$name")"} will, too.  This works
12335 for everything except file names containing a trailing newline.
12336 @end macro
12337 @basenameAndDirname
12338
12339 @acronym{POSIX} allows the implementation to define the results if
12340 @var{name} is empty or @samp{//}.  In the former case, @acronym{GNU}
12341 @command{basename} returns the empty string.  In the latter case, the
12342 result is @samp{//} on platforms where @var{//} is distinct from
12343 @var{/}, and @samp{/} on platforms where there is no difference.
12344
12345 The only options are @option{--help} and @option{--version}.  @xref{Common
12346 options}.  Options must precede operands.
12347
12348 @exitstatus
12349
12350 Examples:
12351
12352 @smallexample
12353 # Output "sort".
12354 basename /usr/bin/sort
12355
12356 # Output "stdio".
12357 basename include/stdio.h .h
12358 @end smallexample
12359
12360
12361 @node dirname invocation
12362 @section @command{dirname}: Strip last file name component
12363
12364 @pindex dirname
12365 @cindex directory components, printing
12366 @cindex stripping non-directory suffix
12367 @cindex non-directory suffix, stripping
12368
12369 @command{dirname} prints all but the final slash-delimited component of
12370 @var{name}.  Slashes on either side of the final component are also
12371 removed.  If the string contains no slash, @command{dirname} prints
12372 @samp{.} (meaning the current directory).  Synopsis:
12373
12374 @example
12375 dirname @var{name}
12376 @end example
12377
12378 @var{name} need not be a file name, but if it is, this operation
12379 effectively lists the directory that contains the final component,
12380 including the case when the final component is itself a directory.
12381
12382 @basenameAndDirname
12383
12384 @acronym{POSIX} allows the implementation to define the results if
12385 @var{name} is @samp{//}.  With @acronym{GNU} @command{dirname}, the
12386 result is @samp{//} on platforms where @var{//} is distinct from
12387 @var{/}, and @samp{/} on platforms where there is no difference.
12388
12389 The only options are @option{--help} and @option{--version}.  @xref{Common
12390 options}.
12391
12392 @exitstatus
12393
12394 Examples:
12395
12396 @smallexample
12397 # Output "/usr/bin".
12398 dirname /usr/bin/sort
12399 dirname /usr/bin//.//
12400
12401 # Output ".".
12402 dirname stdio.h
12403 @end smallexample
12404
12405
12406 @node pathchk invocation
12407 @section @command{pathchk}: Check file name validity and portability
12408
12409 @pindex pathchk
12410 @cindex file names, checking validity and portability
12411 @cindex valid file names, checking for
12412 @cindex portable file names, checking for
12413
12414 @command{pathchk} checks validity and portability of file names.  Synopsis:
12415
12416 @example
12417 pathchk [@var{option}]@dots{} @var{name}@dots{}
12418 @end example
12419
12420 For each @var{name}, @command{pathchk} prints an error message if any of
12421 these conditions is true:
12422
12423 @enumerate
12424 @item
12425 One of the existing directories in @var{name} does not have search
12426 (execute) permission,
12427 @item
12428 The length of @var{name} is larger than the maximum supported by the
12429 operating system.
12430 @item
12431 The length of one component of @var{name} is longer than
12432 its file system's maximum.
12433 @end enumerate
12434
12435 A nonexistent @var{name} is not an error, so long a file with that
12436 name could be created under the above conditions.
12437
12438 The program accepts the following options.  Also see @ref{Common options}.
12439 Options must precede operands.
12440
12441 @table @samp
12442
12443 @item -p
12444 @opindex -p
12445 Instead of performing checks based on the underlying file system,
12446 print an error message if any of these conditions is true:
12447
12448 @enumerate
12449 @item
12450 A file name is empty.
12451
12452 @item
12453 A file name contains a character outside the @acronym{POSIX} portable file
12454 name character set, namely, the ASCII letters and digits, @samp{.},
12455 @samp{_}, @samp{-}, and @samp{/}.
12456
12457 @item
12458 The length of a file name or one of its components exceeds the
12459 @acronym{POSIX} minimum limits for portability.
12460 @end enumerate
12461
12462 @item -P
12463 @opindex -P
12464 Print an error message if a file name is empty, or if it contains a component
12465 that begins with @samp{-}.
12466
12467 @item --portability
12468 @opindex --portability
12469 Print an error message if a file name is not portable to all @acronym{POSIX}
12470 hosts.  This option is equivalent to @samp{-p -P}.
12471
12472 @end table
12473
12474 @cindex exit status of @command{pathchk}
12475 Exit status:
12476
12477 @display
12478 0 if all specified file names passed all checks,
12479 1 otherwise.
12480 @end display
12481
12482 @node mktemp invocation
12483 @section @command{mktemp}: Create temporary file or directory
12484
12485 @pindex mktemp
12486 @cindex file names, creating temporary
12487 @cindex directory, creating temporary
12488 @cindex temporary files and directories
12489
12490 @command{mktemp} manages the creation of temporary files and
12491 directories.  Synopsis:
12492
12493 @example
12494 mktemp [@var{option}]@dots{} [@var{template}]
12495 @end example
12496
12497 Safely create a temporary file or directory based on @var{template},
12498 and print its name.  If given, @var{template} must include at least
12499 three consecutive @samp{X}s in the last component.  If omitted, the template
12500 @samp{tmp.XXXXXXXXXX} is used, and option @option{--tmpdir} is
12501 implied.  The final run of @samp{X}s in the @var{template} will be replaced
12502 by alpha-numeric characters; thus, on a case-sensitive file system,
12503 and with a @var{template} including a run of @var{n} instances of @samp{X},
12504 there are @samp{62**@var{n}} potential file names.
12505
12506 Older scripts used to create temporary files by simply joining the
12507 name of the program with the process id (@samp{$$}) as a suffix.
12508 However, that naming scheme is easily predictable, and suffers from a
12509 race condition where the attacker can create an appropriately named
12510 symbolic link, such that when the script then opens a handle to what
12511 it thought was an unused file, it is instead modifying an existing
12512 file.  Using the same scheme to create a directory is slightly safer,
12513 since the @command{mkdir} will fail if the target already exists, but
12514 it is still inferior because it allows for denial of service attacks.
12515 Therefore, modern scripts should use the @command{mktemp} command to
12516 guarantee that the generated name will be unpredictable, and that
12517 knowledge of the temporary file name implies that the file was created
12518 by the current script and cannot be modified by other users.
12519
12520 When creating a file, the resulting file has read and write
12521 permissions for the current user, but no permissions for the group or
12522 others; these permissions are reduced if the current umask is more
12523 restrictive.
12524
12525 Here are some examples (although note that if you repeat them, you
12526 will most likely get different file names):
12527
12528 @itemize @bullet
12529
12530 @item
12531 Create a temporary file in the current directory.
12532 @example
12533 $ mktemp file.XXXX
12534 file.H47c
12535 @end example
12536
12537 @item
12538 Create a temporary file with a known suffix.
12539 @example
12540 $ mktemp --suffix=.txt file-XXXX
12541 file-H08W.txt
12542 $ mktemp file-XXXX-XXXX.txt
12543 file-XXXX-eI9L.txt
12544 @end example
12545
12546 @item
12547 Create a secure fifo relative to the user's choice of @env{TMPDIR},
12548 but falling back to the current directory rather than @file{/tmp}.
12549 Note that @command{mktemp} does not create fifos, but can create a
12550 secure directory in which the fifo can live.  Exit the shell if the
12551 directory or fifo could not be created.
12552 @example
12553 $ dir=$(mktemp -p "$@{TMPDIR:-.@}" -d dir-XXXX) || exit 1
12554 $ fifo=$dir/fifo
12555 $ mkfifo "$fifo" || @{ rmdir "$dir"; exit 1; @}
12556 @end example
12557
12558 @item
12559 Create and use a temporary file if possible, but ignore failure.  The
12560 file will reside in the directory named by @env{TMPDIR}, if specified,
12561 or else in @file{/tmp}.
12562 @example
12563 $ file=$(mktemp -q) && @{
12564 >   # Safe to use $file only within this block.  Use quotes,
12565 >   # since $TMPDIR, and thus $file, may contain whitespace.
12566 >   echo ... > "$file"
12567 >   rm "$file"
12568 > @}
12569 @end example
12570
12571 @item
12572 Act as a semi-random character generator (it is not fully random,
12573 since it is impacted by the contents of the current directory).  To
12574 avoid security holes, do not use the resulting names to create a file.
12575 @example
12576 $ mktemp -u XXX
12577 Gb9
12578 $ mktemp -u XXX
12579 nzC
12580 @end example
12581
12582 @end itemize
12583
12584 The program accepts the following options.  Also see @ref{Common options}.
12585
12586 @table @samp
12587
12588 @item -d
12589 @itemx --directory
12590 @opindex -d
12591 @opindex --directory
12592 Create a directory rather than a file.  The directory will have read,
12593 write, and search permissions for the current user, but no permissions
12594 for the group or others; these permissions are reduced if the current
12595 umask is more restrictive.
12596
12597 @item -q
12598 @itemx --quiet
12599 @opindex -q
12600 @opindex --quiet
12601 Suppress diagnostics about failure to create a file or directory.  The
12602 exit status will still reflect whether a file was created.
12603
12604 @item -u
12605 @itemx --dry-run
12606 @opindex -u
12607 @opindex --dry-run
12608 Generate a temporary name that does not name an existing file, without
12609 changing the file system contents.  Using the output of this command
12610 to create a new file is inherently unsafe, as there is a window of
12611 time between generating the name and using it where another process
12612 can create an object by the same name.
12613
12614 @item -p @var{dir}
12615 @itemx --tmpdir[=@var{dir}]
12616 @opindex -p
12617 @opindex --tmpdir
12618 Treat @var{template} relative to the directory @var{dir}.  If
12619 @var{dir} is not specified (only possible with the long option
12620 @option{--tmpdir}) or is the empty string, use the value of
12621 @env{TMPDIR} if available, otherwise use @samp{/tmp}.  If this is
12622 specified, @var{template} must not be absolute.  However,
12623 @var{template} can still contain slashes, although intermediate
12624 directories must already exist.
12625
12626 @item --suffix=@var{suffix}
12627 @opindex --suffix
12628 Append @var{suffix} to the @var{template}.  @var{suffix} must not
12629 contain slash.  If @option{--suffix} is specified, @var{template} must
12630 end in @samp{X}; if it is not specified, then an appropriate
12631 @option{--suffix} is inferred by finding the last @samp{X} in
12632 @var{template}.  This option exists for use with the default
12633 @var{template} and for the creation of a @var{suffix} that starts with
12634 @samp{X}.
12635
12636 @item -t
12637 @opindex -t
12638 Treat @var{template} as a single file relative to the value of
12639 @env{TMPDIR} if available, or to the directory specified by
12640 @option{-p}, otherwise to @samp{/tmp}.  @var{template} must not
12641 contain slashes.  This option is deprecated; the use of @option{-p}
12642 without @option{-t} offers better defaults (by favoring the command
12643 line over @env{TMPDIR}) and more flexibility (by allowing intermediate
12644 directories).
12645
12646 @end table
12647
12648 @cindex exit status of @command{mktemp}
12649 Exit status:
12650
12651 @display
12652 0 if the file was created,
12653 1 otherwise.
12654 @end display
12655
12656
12657 @node Working context
12658 @chapter Working context
12659
12660 @cindex working context
12661 @cindex commands for printing the working context
12662
12663 This section describes commands that display or alter the context in
12664 which you are working: the current directory, the terminal settings, and
12665 so forth.  See also the user-related commands in the next section.
12666
12667 @menu
12668 * pwd invocation::              Print working directory.
12669 * stty invocation::             Print or change terminal characteristics.
12670 * printenv invocation::         Print environment variables.
12671 * tty invocation::              Print file name of terminal on standard input.
12672 @end menu
12673
12674
12675 @node pwd invocation
12676 @section @command{pwd}: Print working directory
12677
12678 @pindex pwd
12679 @cindex print name of current directory
12680 @cindex current working directory, printing
12681 @cindex working directory, printing
12682
12683
12684 @command{pwd} prints the name of the current directory.  Synopsis:
12685
12686 @example
12687 pwd [@var{option}]@dots{}
12688 @end example
12689
12690 The program accepts the following options.  Also see @ref{Common options}.
12691
12692 @table @samp
12693 @item -L
12694 @itemx --logical
12695 @opindex -L
12696 @opindex --logical
12697 If the contents of the environment variable @env{PWD} provide an
12698 absolute name of the current directory with no @samp{.} or @samp{..}
12699 components, but possibly with symbolic links, then output those
12700 contents.  Otherwise, fall back to default @option{-P} handling.
12701
12702 @item -P
12703 @itemx --physical
12704 @opindex -P
12705 @opindex --physical
12706 Print a fully resolved name for the current directory.  That is, all
12707 components of the printed name will be actual directory names---none
12708 will be symbolic links.
12709 @end table
12710
12711 @cindex symbolic links and @command{pwd}
12712 If @option{-L} and @option{-P} are both given, the last one takes
12713 precedence.  If neither option is given, then this implementation uses
12714 @option{-P} as the default unless the @env{POSIXLY_CORRECT}
12715 environment variable is set.
12716
12717 @mayConflictWithShellBuiltIn{pwd}
12718
12719 @exitstatus
12720
12721
12722 @node stty invocation
12723 @section @command{stty}: Print or change terminal characteristics
12724
12725 @pindex stty
12726 @cindex change or print terminal settings
12727 @cindex terminal settings
12728 @cindex line settings of terminal
12729
12730 @command{stty} prints or changes terminal characteristics, such as baud rate.
12731 Synopses:
12732
12733 @example
12734 stty [@var{option}] [@var{setting}]@dots{}
12735 stty [@var{option}]
12736 @end example
12737
12738 If given no line settings, @command{stty} prints the baud rate, line
12739 discipline number (on systems that support it), and line settings
12740 that have been changed from the values set by @samp{stty sane}.
12741 By default, mode reading and setting are performed on the tty line
12742 connected to standard input, although this can be modified by the
12743 @option{--file} option.
12744
12745 @command{stty} accepts many non-option arguments that change aspects of
12746 the terminal line operation, as described below.
12747
12748 The program accepts the following options.  Also see @ref{Common options}.
12749
12750 @table @samp
12751 @item -a
12752 @itemx --all
12753 @opindex -a
12754 @opindex --all
12755 Print all current settings in human-readable form.  This option may not
12756 be used in combination with any line settings.
12757
12758 @item -F @var{device}
12759 @itemx --file=@var{device}
12760 @opindex -F
12761 @opindex --file
12762 Set the line opened by the file name specified in @var{device} instead of
12763 the tty line connected to standard input.  This option is necessary
12764 because opening a @acronym{POSIX} tty requires use of the
12765 @code{O_NONDELAY} flag to prevent a @acronym{POSIX} tty from blocking
12766 until the carrier detect line is high if
12767 the @code{clocal} flag is not set.  Hence, it is not always possible
12768 to allow the shell to open the device in the traditional manner.
12769
12770 @item -g
12771 @itemx --save
12772 @opindex -g
12773 @opindex --save
12774 @cindex machine-readable @command{stty} output
12775 Print all current settings in a form that can be used as an argument to
12776 another @command{stty} command to restore the current settings.  This option
12777 may not be used in combination with any line settings.
12778
12779 @end table
12780
12781 Many settings can be turned off by preceding them with a @samp{-}.
12782 Such arguments are marked below with ``May be negated'' in their
12783 description.  The descriptions themselves refer to the positive
12784 case, that is, when @emph{not} negated (unless stated otherwise,
12785 of course).
12786
12787 Some settings are not available on all @acronym{POSIX} systems, since they use
12788 extensions.  Such arguments are marked below with
12789 ``Non-@acronym{POSIX}'' in their description.  On non-@acronym{POSIX}
12790 systems, those or other settings also may not
12791 be available, but it's not feasible to document all the variations: just
12792 try it and see.
12793
12794 @exitstatus
12795
12796 @menu
12797 * Control::                     Control settings
12798 * Input::                       Input settings
12799 * Output::                      Output settings
12800 * Local::                       Local settings
12801 * Combination::                 Combination settings
12802 * Characters::                  Special characters
12803 * Special::                     Special settings
12804 @end menu
12805
12806
12807 @node Control
12808 @subsection Control settings
12809
12810 @cindex control settings
12811 Control settings:
12812
12813 @table @samp
12814 @item parenb
12815 @opindex parenb
12816 @cindex two-way parity
12817 Generate parity bit in output and expect parity bit in input.
12818 May be negated.
12819
12820 @item parodd
12821 @opindex parodd
12822 @cindex odd parity
12823 @cindex even parity
12824 Set odd parity (even if negated).  May be negated.
12825
12826 @item cs5
12827 @itemx cs6
12828 @itemx cs7
12829 @itemx cs8
12830 @opindex cs@var{n}
12831 @cindex character size
12832 @cindex eight-bit characters
12833 Set character size to 5, 6, 7, or 8 bits.
12834
12835 @item hup
12836 @itemx hupcl
12837 @opindex hup[cl]
12838 Send a hangup signal when the last process closes the tty.  May be
12839 negated.
12840
12841 @item cstopb
12842 @opindex cstopb
12843 @cindex stop bits
12844 Use two stop bits per character (one if negated).  May be negated.
12845
12846 @item cread
12847 @opindex cread
12848 Allow input to be received.  May be negated.
12849
12850 @item clocal
12851 @opindex clocal
12852 @cindex modem control
12853 Disable modem control signals.  May be negated.
12854
12855 @item crtscts
12856 @opindex crtscts
12857 @cindex hardware flow control
12858 @cindex flow control, hardware
12859 @cindex RTS/CTS flow control
12860 Enable RTS/CTS flow control.  Non-@acronym{POSIX}.  May be negated.
12861 @end table
12862
12863
12864 @node Input
12865 @subsection Input settings
12866
12867 @cindex input settings
12868 These settings control operations on data received from the terminal.
12869
12870 @table @samp
12871 @item ignbrk
12872 @opindex ignbrk
12873 @cindex breaks, ignoring
12874 Ignore break characters.  May be negated.
12875
12876 @item brkint
12877 @opindex brkint
12878 @cindex breaks, cause interrupts
12879 Make breaks cause an interrupt signal.  May be negated.
12880
12881 @item ignpar
12882 @opindex ignpar
12883 @cindex parity, ignoring
12884 Ignore characters with parity errors.  May be negated.
12885
12886 @item parmrk
12887 @opindex parmrk
12888 @cindex parity errors, marking
12889 Mark parity errors (with a 255-0-character sequence).  May be negated.
12890
12891 @item inpck
12892 @opindex inpck
12893 Enable input parity checking.  May be negated.
12894
12895 @item istrip
12896 @opindex istrip
12897 @cindex eight-bit input
12898 Clear high (8th) bit of input characters.  May be negated.
12899
12900 @item inlcr
12901 @opindex inlcr
12902 @cindex newline, translating to return
12903 Translate newline to carriage return.  May be negated.
12904
12905 @item igncr
12906 @opindex igncr
12907 @cindex return, ignoring
12908 Ignore carriage return.  May be negated.
12909
12910 @item icrnl
12911 @opindex icrnl
12912 @cindex return, translating to newline
12913 Translate carriage return to newline.  May be negated.
12914
12915 @item iutf8
12916 @opindex iutf8
12917 @cindex input encoding, UTF-8
12918 Assume input characters are UTF-8 encoded.  May be negated.
12919
12920 @item ixon
12921 @opindex ixon
12922 @kindex C-s/C-q flow control
12923 @cindex XON/XOFF flow control
12924 Enable XON/XOFF flow control (that is, @kbd{CTRL-S}/@kbd{CTRL-Q}).  May
12925 be negated.
12926
12927 @item ixoff
12928 @itemx tandem
12929 @opindex ixoff
12930 @opindex tandem
12931 @cindex software flow control
12932 @cindex flow control, software
12933 Enable sending of @code{stop} character when the system input buffer
12934 is almost full, and @code{start} character when it becomes almost
12935 empty again.  May be negated.
12936
12937 @item iuclc
12938 @opindex iuclc
12939 @cindex uppercase, translating to lowercase
12940 Translate uppercase characters to lowercase.  Non-@acronym{POSIX}.  May be
12941 negated. Note ilcuc is not implemented, as one would not be able to issue
12942 almost any (lowercase) Unix command, after invoking it.
12943
12944 @item ixany
12945 @opindex ixany
12946 Allow any character to restart output (only the start character
12947 if negated).  Non-@acronym{POSIX}.  May be negated.
12948
12949 @item imaxbel
12950 @opindex imaxbel
12951 @cindex beeping at input buffer full
12952 Enable beeping and not flushing input buffer if a character arrives
12953 when the input buffer is full.  Non-@acronym{POSIX}.  May be negated.
12954 @end table
12955
12956
12957 @node Output
12958 @subsection Output settings
12959
12960 @cindex output settings
12961 These settings control operations on data sent to the terminal.
12962
12963 @table @samp
12964 @item opost
12965 @opindex opost
12966 Postprocess output.  May be negated.
12967
12968 @item olcuc
12969 @opindex olcuc
12970 @cindex lowercase, translating to output
12971 Translate lowercase characters to uppercase.  Non-@acronym{POSIX}.  May be
12972 negated. (Note ouclc is not currently implemented.)
12973
12974 @item ocrnl
12975 @opindex ocrnl
12976 @cindex return, translating to newline
12977 Translate carriage return to newline.  Non-@acronym{POSIX}.  May be negated.
12978
12979 @item onlcr
12980 @opindex onlcr
12981 @cindex newline, translating to crlf
12982 Translate newline to carriage return-newline.  Non-@acronym{POSIX}.  May be
12983 negated.
12984
12985 @item onocr
12986 @opindex onocr
12987 Do not print carriage returns in the first column.  Non-@acronym{POSIX}.
12988 May be negated.
12989
12990 @item onlret
12991 @opindex onlret
12992 Newline performs a carriage return.  Non-@acronym{POSIX}.  May be negated.
12993
12994 @item ofill
12995 @opindex ofill
12996 @cindex pad instead of timing for delaying
12997 Use fill (padding) characters instead of timing for delays.
12998 Non-@acronym{POSIX}.
12999 May be negated.
13000
13001 @item ofdel
13002 @opindex ofdel
13003 @cindex pad character
13004 Use @acronym{ASCII} @sc{del} characters for fill instead of
13005 @acronym{ASCII} @sc{nul} characters.  Non-@acronym{POSIX}.
13006 May be negated.
13007
13008 @item nl1
13009 @itemx nl0
13010 @opindex nl@var{n}
13011 Newline delay style.  Non-@acronym{POSIX}.
13012
13013 @item cr3
13014 @itemx cr2
13015 @itemx cr1
13016 @itemx cr0
13017 @opindex cr@var{n}
13018 Carriage return delay style.  Non-@acronym{POSIX}.
13019
13020 @item tab3
13021 @itemx tab2
13022 @itemx tab1
13023 @itemx tab0
13024 @opindex tab@var{n}
13025 Horizontal tab delay style.  Non-@acronym{POSIX}.
13026
13027 @item bs1
13028 @itemx bs0
13029 @opindex bs@var{n}
13030 Backspace delay style.  Non-@acronym{POSIX}.
13031
13032 @item vt1
13033 @itemx vt0
13034 @opindex vt@var{n}
13035 Vertical tab delay style.  Non-@acronym{POSIX}.
13036
13037 @item ff1
13038 @itemx ff0
13039 @opindex ff@var{n}
13040 Form feed delay style.  Non-@acronym{POSIX}.
13041 @end table
13042
13043
13044 @node Local
13045 @subsection Local settings
13046
13047 @cindex local settings
13048
13049 @table @samp
13050 @item isig
13051 @opindex isig
13052 Enable @code{interrupt}, @code{quit}, and @code{suspend} special
13053 characters.  May be negated.
13054
13055 @item icanon
13056 @opindex icanon
13057 Enable @code{erase}, @code{kill}, @code{werase}, and @code{rprnt}
13058 special characters.  May be negated.
13059
13060 @item iexten
13061 @opindex iexten
13062 Enable non-@acronym{POSIX} special characters.  May be negated.
13063
13064 @item echo
13065 @opindex echo
13066 Echo input characters.  May be negated.
13067
13068 @item echoe
13069 @itemx crterase
13070 @opindex echoe
13071 @opindex crterase
13072 Echo @code{erase} characters as backspace-space-backspace.  May be
13073 negated.
13074
13075 @item echok
13076 @opindex echok
13077 @cindex newline echoing after @code{kill}
13078 Echo a newline after a @code{kill} character.  May be negated.
13079
13080 @item echonl
13081 @opindex echonl
13082 @cindex newline, echoing
13083 Echo newline even if not echoing other characters.  May be negated.
13084
13085 @item noflsh
13086 @opindex noflsh
13087 @cindex flushing, disabling
13088 Disable flushing after @code{interrupt} and @code{quit} special
13089 characters.  May be negated.
13090
13091 @item xcase
13092 @opindex xcase
13093 @cindex case translation
13094 Enable input and output of uppercase characters by preceding their
13095 lowercase equivalents with @samp{\}, when @code{icanon} is set.
13096 Non-@acronym{POSIX}.  May be negated.
13097
13098 @item tostop
13099 @opindex tostop
13100 @cindex background jobs, stopping at terminal write
13101 Stop background jobs that try to write to the terminal.  Non-@acronym{POSIX}.
13102 May be negated.
13103
13104 @item echoprt
13105 @itemx prterase
13106 @opindex echoprt
13107 @opindex prterase
13108 Echo erased characters backward, between @samp{\} and @samp{/}.
13109 Non-@acronym{POSIX}.  May be negated.
13110
13111 @item echoctl
13112 @itemx ctlecho
13113 @opindex echoctl
13114 @opindex ctlecho
13115 @cindex control characters, using @samp{^@var{c}}
13116 @cindex hat notation for control characters
13117 Echo control characters in hat notation (@samp{^@var{c}}) instead
13118 of literally.  Non-@acronym{POSIX}.  May be negated.
13119
13120 @item echoke
13121 @itemx crtkill
13122 @opindex echoke
13123 @opindex crtkill
13124 Echo the @code{kill} special character by erasing each character on
13125 the line as indicated by the @code{echoprt} and @code{echoe} settings,
13126 instead of by the @code{echoctl} and @code{echok} settings.
13127 Non-@acronym{POSIX}.
13128 May be negated.
13129 @end table
13130
13131
13132 @node Combination
13133 @subsection Combination settings
13134
13135 @cindex combination settings
13136 Combination settings:
13137
13138 @table @samp
13139 @item evenp
13140 @opindex evenp
13141 @itemx parity
13142 @opindex parity
13143 Same as @code{parenb -parodd cs7}.  May be negated.  If negated, same
13144 as @code{-parenb cs8}.
13145
13146 @item oddp
13147 @opindex oddp
13148 Same as @code{parenb parodd cs7}.  May be negated.  If negated, same
13149 as @code{-parenb cs8}.
13150
13151 @item nl
13152 @opindex nl
13153 Same as @code{-icrnl -onlcr}.  May be negated.  If negated, same as
13154 @code{icrnl -inlcr -igncr onlcr -ocrnl -onlret}.
13155
13156 @item ek
13157 @opindex ek
13158 Reset the @code{erase} and @code{kill} special characters to their default
13159 values.
13160
13161 @item sane
13162 @opindex sane
13163 Same as:
13164
13165 @c This is too long to write inline.
13166 @example
13167 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff
13168 -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr
13169 -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
13170 ff0 isig icanon iexten echo echoe echok -echonl
13171 -noflsh -xcase -tostop -echoprt echoctl echoke
13172 @end example
13173
13174 @noindent
13175 and also sets all special characters to their default values.
13176
13177 @item cooked
13178 @opindex cooked
13179 Same as @code{brkint ignpar istrip icrnl ixon opost isig icanon}, plus
13180 sets the @code{eof} and @code{eol} characters to their default values
13181 if they are the same as the @code{min} and @code{time} characters.
13182 May be negated.  If negated, same as @code{raw}.
13183
13184 @item raw
13185 @opindex raw
13186 Same as:
13187
13188 @example
13189 -ignbrk -brkint -ignpar -parmrk -inpck -istrip
13190 -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
13191 -imaxbel -opost -isig -icanon -xcase min 1 time 0
13192 @end example
13193
13194 @noindent
13195 May be negated.  If negated, same as @code{cooked}.
13196
13197 @item cbreak
13198 @opindex cbreak
13199 Same as @option{-icanon}.  May be negated.  If negated, same as
13200 @code{icanon}.
13201
13202 @item pass8
13203 @opindex pass8
13204 @cindex eight-bit characters
13205 Same as @code{-parenb -istrip cs8}.  May be negated.  If negated,
13206 same as @code{parenb istrip cs7}.
13207
13208 @item litout
13209 @opindex litout
13210 Same as @option{-parenb -istrip -opost cs8}.  May be negated.
13211 If negated, same as @code{parenb istrip opost cs7}.
13212
13213 @item decctlq
13214 @opindex decctlq
13215 Same as @option{-ixany}.  Non-@acronym{POSIX}.  May be negated.
13216
13217 @item tabs
13218 @opindex tabs
13219 Same as @code{tab0}.  Non-@acronym{POSIX}.  May be negated.  If negated, same
13220 as @code{tab3}.
13221
13222 @item lcase
13223 @itemx LCASE
13224 @opindex lcase
13225 @opindex LCASE
13226 Same as @code{xcase iuclc olcuc}.  Non-@acronym{POSIX}.  May be negated.
13227 (Used for terminals with uppercase characters only.)
13228
13229 @item crt
13230 @opindex crt
13231 Same as @code{echoe echoctl echoke}.
13232
13233 @item dec
13234 @opindex dec
13235 Same as @code{echoe echoctl echoke -ixany intr ^C erase ^? kill C-u}.
13236 @end table
13237
13238
13239 @node Characters
13240 @subsection Special characters
13241
13242 @cindex special characters
13243 @cindex characters, special
13244
13245 The special characters' default values vary from system to system.
13246 They are set with the syntax @samp{name value}, where the names are
13247 listed below and the value can be given either literally, in hat
13248 notation (@samp{^@var{c}}), or as an integer which may start with
13249 @samp{0x} to indicate hexadecimal, @samp{0} to indicate octal, or
13250 any other digit to indicate decimal.
13251
13252 @cindex disabling special characters
13253 @kindex u@r{, and disabling special characters}
13254 For GNU stty, giving a value of @code{^-} or @code{undef} disables that
13255 special character.  (This is incompatible with Ultrix @command{stty},
13256 which uses  a value of @samp{u} to disable a special character.  GNU
13257 @command{stty} treats a value @samp{u} like any other, namely to set that
13258 special character to @key{U}.)
13259
13260 @table @samp
13261
13262 @item intr
13263 @opindex intr
13264 Send an interrupt signal.
13265
13266 @item quit
13267 @opindex quit
13268 Send a quit signal.
13269
13270 @item erase
13271 @opindex erase
13272 Erase the last character typed.
13273
13274 @item kill
13275 @opindex kill
13276 Erase the current line.
13277
13278 @item eof
13279 @opindex eof
13280 Send an end of file (terminate the input).
13281
13282 @item eol
13283 @opindex eol
13284 End the line.
13285
13286 @item eol2
13287 @opindex eol2
13288 Alternate character to end the line.  Non-@acronym{POSIX}.
13289
13290 @item swtch
13291 @opindex swtch
13292 Switch to a different shell layer.  Non-@acronym{POSIX}.
13293
13294 @item start
13295 @opindex start
13296 Restart the output after stopping it.
13297
13298 @item stop
13299 @opindex stop
13300 Stop the output.
13301
13302 @item susp
13303 @opindex susp
13304 Send a terminal stop signal.
13305
13306 @item dsusp
13307 @opindex dsusp
13308 Send a terminal stop signal after flushing the input.  Non-@acronym{POSIX}.
13309
13310 @item rprnt
13311 @opindex rprnt
13312 Redraw the current line.  Non-@acronym{POSIX}.
13313
13314 @item werase
13315 @opindex werase
13316 Erase the last word typed.  Non-@acronym{POSIX}.
13317
13318 @item lnext
13319 @opindex lnext
13320 Enter the next character typed literally, even if it is a special
13321 character.  Non-@acronym{POSIX}.
13322 @end table
13323
13324
13325 @node Special
13326 @subsection Special settings
13327
13328 @cindex special settings
13329
13330 @table @samp
13331 @item min @var{n}
13332 @opindex min
13333 Set the minimum number of characters that will satisfy a read until
13334 the time value has expired, when @option{-icanon} is set.
13335
13336 @item time @var{n}
13337 @opindex time
13338 Set the number of tenths of a second before reads time out if the minimum
13339 number of characters have not been read, when @option{-icanon} is set.
13340
13341 @item ispeed @var{n}
13342 @opindex ispeed
13343 Set the input speed to @var{n}.
13344
13345 @item ospeed @var{n}
13346 @opindex ospeed
13347 Set the output speed to @var{n}.
13348
13349 @item rows @var{n}
13350 @opindex rows
13351 Tell the tty kernel driver that the terminal has @var{n} rows.
13352 Non-@acronym{POSIX}.
13353
13354 @item cols @var{n}
13355 @itemx columns @var{n}
13356 @opindex cols
13357 @opindex columns
13358 Tell the kernel that the terminal has @var{n} columns.  Non-@acronym{POSIX}.
13359
13360 @item size
13361 @opindex size
13362 @vindex LINES
13363 @vindex COLUMNS
13364 Print the number of rows and columns that the kernel thinks the
13365 terminal has.  (Systems that don't support rows and columns in the kernel
13366 typically use the environment variables @env{LINES} and @env{COLUMNS}
13367 instead; however, GNU @command{stty} does not know anything about them.)
13368 Non-@acronym{POSIX}.
13369
13370 @item line @var{n}
13371 @opindex line
13372 Use line discipline @var{n}.  Non-@acronym{POSIX}.
13373
13374 @item speed
13375 @opindex speed
13376 Print the terminal speed.
13377
13378 @item @var{n}
13379 @cindex baud rate, setting
13380 Set the input and output speeds to @var{n}.  @var{n} can be one of: 0
13381 50 75 110 134 134.5 150 200 300 600 1200 1800 2400 4800 9600 19200
13382 38400 @code{exta} @code{extb}.  @code{exta} is the same as 19200;
13383 @code{extb} is the same as 38400.  Many systems, including GNU/Linux,
13384 support higher speeds.  The @command{stty} command includes support
13385 for speeds of
13386 57600,
13387 115200,
13388 230400,
13389 460800,
13390 500000,
13391 576000,
13392 921600,
13393 1000000,
13394 1152000,
13395 1500000,
13396 2000000,
13397 2500000,
13398 3000000,
13399 3500000,
13400 or
13401 4000000 where the system supports these.
13402 0 hangs up the line if @option{-clocal} is set.
13403 @end table
13404
13405
13406 @node printenv invocation
13407 @section @command{printenv}: Print all or some environment variables
13408
13409 @pindex printenv
13410 @cindex printing all or some environment variables
13411 @cindex environment variables, printing
13412
13413 @command{printenv} prints environment variable values.  Synopsis:
13414
13415 @example
13416 printenv [@var{option}] [@var{variable}]@dots{}
13417 @end example
13418
13419 If no @var{variable}s are specified, @command{printenv} prints the value of
13420 every environment variable.  Otherwise, it prints the value of each
13421 @var{variable} that is set, and nothing for those that are not set.
13422
13423 The program accepts the following option.  Also see @ref{Common options}.
13424
13425 @table @samp
13426
13427 @optNull{printenv}
13428
13429 @end table
13430
13431 @cindex exit status of @command{printenv}
13432 Exit status:
13433
13434 @display
13435 0 if all variables specified were found
13436 1 if at least one specified variable was not found
13437 2 if a write error occurred
13438 @end display
13439
13440
13441 @node tty invocation
13442 @section @command{tty}: Print file name of terminal on standard input
13443
13444 @pindex tty
13445 @cindex print terminal file name
13446 @cindex terminal file name, printing
13447
13448 @command{tty} prints the file name of the terminal connected to its standard
13449 input.  It prints @samp{not a tty} if standard input is not a terminal.
13450 Synopsis:
13451
13452 @example
13453 tty [@var{option}]@dots{}
13454 @end example
13455
13456 The program accepts the following option.  Also see @ref{Common options}.
13457
13458 @table @samp
13459
13460 @item -s
13461 @itemx --silent
13462 @itemx --quiet
13463 @opindex -s
13464 @opindex --silent
13465 @opindex --quiet
13466 Print nothing; only return an exit status.
13467
13468 @end table
13469
13470 @cindex exit status of @command{tty}
13471 Exit status:
13472
13473 @display
13474 0 if standard input is a terminal
13475 1 if standard input is not a terminal
13476 2 if given incorrect arguments
13477 3 if a write error occurs
13478 @end display
13479
13480
13481 @node User information
13482 @chapter User information
13483
13484 @cindex user information, commands for
13485 @cindex commands for printing user information
13486
13487 This section describes commands that print user-related information:
13488 logins, groups, and so forth.
13489
13490 @menu
13491 * id invocation::               Print user identity.
13492 * logname invocation::          Print current login name.
13493 * whoami invocation::           Print effective user ID.
13494 * groups invocation::           Print group names a user is in.
13495 * users invocation::            Print login names of users currently logged in.
13496 * who invocation::              Print who is currently logged in.
13497 @end menu
13498
13499
13500 @node id invocation
13501 @section @command{id}: Print user identity
13502
13503 @pindex id
13504 @cindex real user and group IDs, printing
13505 @cindex effective user and group IDs, printing
13506 @cindex printing real and effective user and group IDs
13507
13508 @command{id} prints information about the given user, or the process
13509 running it if no user is specified.  Synopsis:
13510
13511 @example
13512 id [@var{option}]@dots{} [@var{username}]
13513 @end example
13514
13515 @vindex POSIXLY_CORRECT
13516 By default, it prints the real user ID, real group ID, effective user ID
13517 if different from the real user ID, effective group ID if different from
13518 the real group ID, and supplemental group IDs.
13519 In addition, if SELinux
13520 is enabled and the @env{POSIXLY_CORRECT} environment variable is not set,
13521 then print @samp{context=@var{c}}, where @var{c} is the security context.
13522
13523 Each of these numeric values is preceded by an identifying string and
13524 followed by the corresponding user or group name in parentheses.
13525
13526 The options cause @command{id} to print only part of the above information.
13527 Also see @ref{Common options}.
13528
13529 @table @samp
13530 @item -g
13531 @itemx --group
13532 @opindex -g
13533 @opindex --group
13534 Print only the group ID.
13535
13536 @item -G
13537 @itemx --groups
13538 @opindex -G
13539 @opindex --groups
13540 Print only the group ID and the supplementary groups.
13541
13542 @item -n
13543 @itemx --name
13544 @opindex -n
13545 @opindex --name
13546 Print the user or group name instead of the ID number.  Requires
13547 @option{-u}, @option{-g}, or @option{-G}.
13548
13549 @item -r
13550 @itemx --real
13551 @opindex -r
13552 @opindex --real
13553 Print the real, instead of effective, user or group ID.  Requires
13554 @option{-u}, @option{-g}, or @option{-G}.
13555
13556 @item -u
13557 @itemx --user
13558 @opindex -u
13559 @opindex --user
13560 Print only the user ID.
13561
13562 @item -Z
13563 @itemx --context
13564 @opindex -Z
13565 @opindex --context
13566 @cindex SELinux
13567 @cindex security context
13568 Print only the security context of the current user.
13569 If SELinux is disabled then print a warning and
13570 set the exit status to 1.
13571
13572 @end table
13573
13574 @exitstatus
13575
13576 @macro primaryAndSupplementaryGroups{cmd,arg}
13577 Primary and supplementary groups for a process are normally inherited
13578 from its parent and are usually unchanged since login.  This means
13579 that if you change the group database after logging in, @command{\cmd\}
13580 will not reflect your changes within your existing login session.
13581 Running @command{\cmd\} with a \arg\ causes the user and group
13582 database to be consulted afresh, and so will give a different result.
13583 @end macro
13584 @primaryAndSupplementaryGroups{id,user argument}
13585
13586 @node logname invocation
13587 @section @command{logname}: Print current login name
13588
13589 @pindex logname
13590 @cindex printing user's login name
13591 @cindex login name, printing
13592 @cindex user name, printing
13593
13594 @flindex utmp
13595 @command{logname} prints the calling user's name, as found in a
13596 system-maintained file (often @file{/var/run/utmp} or
13597 @file{/etc/utmp}), and exits with a status of 0.  If there is no entry
13598 for the calling process, @command{logname} prints
13599 an error message and exits with a status of 1.
13600
13601 The only options are @option{--help} and @option{--version}.  @xref{Common
13602 options}.
13603
13604 @exitstatus
13605
13606
13607 @node whoami invocation
13608 @section @command{whoami}: Print effective user ID
13609
13610 @pindex whoami
13611 @cindex effective user ID, printing
13612 @cindex printing the effective user ID
13613
13614 @command{whoami} prints the user name associated with the current
13615 effective user ID.  It is equivalent to the command @samp{id -un}.
13616
13617 The only options are @option{--help} and @option{--version}.  @xref{Common
13618 options}.
13619
13620 @exitstatus
13621
13622
13623 @node groups invocation
13624 @section @command{groups}: Print group names a user is in
13625
13626 @pindex groups
13627 @cindex printing groups a user is in
13628 @cindex supplementary groups, printing
13629
13630 @command{groups} prints the names of the primary and any supplementary
13631 groups for each given @var{username}, or the current process if no names
13632 are given.  If more than one name is given, the name of each user is
13633 printed before
13634 the list of that user's groups and the user name is separated from the
13635 group list by a colon.  Synopsis:
13636
13637 @example
13638 groups [@var{username}]@dots{}
13639 @end example
13640
13641 The group lists are equivalent to the output of the command @samp{id -Gn}.
13642
13643 @primaryAndSupplementaryGroups{groups,list of users}
13644
13645 The only options are @option{--help} and @option{--version}.  @xref{Common
13646 options}.
13647
13648 @exitstatus
13649
13650
13651 @node users invocation
13652 @section @command{users}: Print login names of users currently logged in
13653
13654 @pindex users
13655 @cindex printing current usernames
13656 @cindex usernames, printing current
13657
13658 @cindex login sessions, printing users with
13659 @command{users} prints on a single line a blank-separated list of user
13660 names of users currently logged in to the current host.  Each user name
13661 corresponds to a login session, so if a user has more than one login
13662 session, that user's name will appear the same number of times in the
13663 output.  Synopsis:
13664
13665 @example
13666 users [@var{file}]
13667 @end example
13668
13669 @flindex utmp
13670 @flindex wtmp
13671 With no @var{file} argument, @command{users} extracts its information from
13672 a system-maintained file (often @file{/var/run/utmp} or
13673 @file{/etc/utmp}).  If a file argument is given, @command{users} uses
13674 that file instead.  A common choice is @file{/var/log/wtmp}.
13675
13676 The only options are @option{--help} and @option{--version}.  @xref{Common
13677 options}.
13678
13679 @exitstatus
13680
13681
13682 @node who invocation
13683 @section @command{who}: Print who is currently logged in
13684
13685 @pindex who
13686 @cindex printing current user information
13687 @cindex information, about current users
13688
13689 @command{who} prints information about users who are currently logged on.
13690 Synopsis:
13691
13692 @example
13693 @command{who} [@var{option}] [@var{file}] [am i]
13694 @end example
13695
13696 @cindex terminal lines, currently used
13697 @cindex login time
13698 @cindex remote hostname
13699 If given no non-option arguments, @command{who} prints the following
13700 information for each user currently logged on: login name, terminal
13701 line, login time, and remote hostname or X display.
13702
13703 @flindex utmp
13704 @flindex wtmp
13705 If given one non-option argument, @command{who} uses that instead of
13706 a default system-maintained file (often @file{/var/run/utmp} or
13707 @file{/etc/utmp}) as the name of the file containing the record of
13708 users logged on.  @file{/var/log/wtmp} is commonly given as an argument
13709 to @command{who} to look at who has previously logged on.
13710
13711 @opindex am i
13712 @opindex who am i
13713 If given two non-option arguments, @command{who} prints only the entry
13714 for the user running it (determined from its standard input), preceded
13715 by the hostname.  Traditionally, the two arguments given are @samp{am
13716 i}, as in @samp{who am i}.
13717
13718 @vindex TZ
13719 Time stamps are listed according to the time zone rules specified by
13720 the @env{TZ} environment variable, or by the system default rules if
13721 @env{TZ} is not set.  @xref{TZ Variable,, Specifying the Time Zone
13722 with @env{TZ}, libc, The GNU C Library Reference Manual}.
13723
13724 The program accepts the following options.  Also see @ref{Common options}.
13725
13726 @table @samp
13727
13728 @item -a
13729 @itemx --all
13730 @opindex -a
13731 @opindex --all
13732 Same as @samp{-b -d --login -p -r -t -T -u}.
13733
13734 @item -b
13735 @itemx --boot
13736 @opindex -b
13737 @opindex --boot
13738 Print the date and time of last system boot.
13739
13740 @item -d
13741 @itemx --dead
13742 @opindex -d
13743 @opindex --dead
13744 Print information corresponding to dead processes.
13745
13746 @item -H
13747 @itemx --heading
13748 @opindex -H
13749 @opindex --heading
13750 Print a line of column headings.
13751
13752 @item -l
13753 @itemx --login
13754 @opindex -l
13755 @opindex --login
13756 List only the entries that correspond to processes via which the
13757 system is waiting for a user to login.  The user name is always @samp{LOGIN}.
13758
13759 @itemx --lookup
13760 @opindex --lookup
13761 Attempt to canonicalize hostnames found in utmp through a DNS lookup.  This
13762 is not the default because it can cause significant delays on systems with
13763 automatic dial-up internet access.
13764
13765 @item -m
13766 @opindex -m
13767 Same as @samp{who am i}.
13768
13769 @item -p
13770 @itemx --process
13771 @opindex -p
13772 @opindex --process
13773 List active processes spawned by init.
13774
13775 @item -q
13776 @itemx --count
13777 @opindex -q
13778 @opindex --count
13779 Print only the login names and the number of users logged on.
13780 Overrides all other options.
13781
13782 @item -r
13783 @itemx --runlevel
13784 @opindex -r
13785 @opindex --runlevel
13786 Print the current (and maybe previous) run-level of the init process.
13787
13788 @item -s
13789 @opindex -s
13790 Ignored; for compatibility with other versions of @command{who}.
13791
13792 @item -t
13793 @itemx --time
13794 @opindex -t
13795 @opindex --time
13796 Print last system clock change.
13797
13798 @itemx -u
13799 @opindex -u
13800 @cindex idle time
13801 After the login time, print the number of hours and minutes that the
13802 user has been idle.  @samp{.} means the user was active in the last minute.
13803 @samp{old} means the user has been idle for more than 24 hours.
13804
13805 @item -w
13806 @itemx -T
13807 @itemx --mesg
13808 @itemx --message
13809 @itemx --writable
13810 @opindex -w
13811 @opindex -T
13812 @opindex --mesg
13813 @opindex --message
13814 @opindex --writable
13815 @cindex message status
13816 @pindex write@r{, allowed}
13817 After each login name print a character indicating the user's message status:
13818
13819 @display
13820 @samp{+} allowing @code{write} messages
13821 @samp{-} disallowing @code{write} messages
13822 @samp{?} cannot find terminal device
13823 @end display
13824
13825 @end table
13826
13827 @exitstatus
13828
13829
13830 @node System context
13831 @chapter System context
13832
13833 @cindex system context
13834 @cindex context, system
13835 @cindex commands for system context
13836
13837 This section describes commands that print or change system-wide
13838 information.
13839
13840 @menu
13841 * date invocation::             Print or set system date and time.
13842 * arch invocation::             Print machine hardware name.
13843 * nproc invocation::            Print the number of processors.
13844 * uname invocation::            Print system information.
13845 * hostname invocation::         Print or set system name.
13846 * hostid invocation::           Print numeric host identifier.
13847 * uptime invocation::           Print system uptime and load.
13848 @end menu
13849
13850 @node date invocation
13851 @section @command{date}: Print or set system date and time
13852
13853 @pindex date
13854 @cindex time, printing or setting
13855 @cindex printing the current time
13856
13857 Synopses:
13858
13859 @example
13860 date [@var{option}]@dots{} [+@var{format}]
13861 date [-u|--utc|--universal] @c this avoids a newline in the output
13862 [ MMDDhhmm[[CC]YY][.ss] ]
13863 @end example
13864
13865 @vindex LC_TIME
13866 Invoking @command{date} with no @var{format} argument is equivalent to invoking
13867 it with a default format that depends on the @env{LC_TIME} locale category.
13868 In the default C locale, this format is @samp{'+%a %b %e %H:%M:%S %Z %Y'},
13869 so the output looks like @samp{Thu Mar @ 3 13:47:51 PST 2005}.
13870
13871 @vindex TZ
13872 Normally, @command{date} uses the time zone rules indicated by the
13873 @env{TZ} environment variable, or the system default rules if @env{TZ}
13874 is not set.  @xref{TZ Variable,, Specifying the Time Zone with
13875 @env{TZ}, libc, The GNU C Library Reference Manual}.
13876
13877 @findex strftime @r{and @command{date}}
13878 @cindex time formats
13879 @cindex formatting times
13880 If given an argument that starts with a @samp{+}, @command{date} prints the
13881 current date and time (or the date and time specified by the
13882 @option{--date} option, see below) in the format defined by that argument,
13883 which is similar to that of the @code{strftime} function.  Except for
13884 conversion specifiers, which start with @samp{%}, characters in the
13885 format string are printed unchanged.  The conversion specifiers are
13886 described below.
13887
13888 @exitstatus
13889
13890 @menu
13891 * Time conversion specifiers::     %[HIklMNpPrRsSTXzZ]
13892 * Date conversion specifiers::     %[aAbBcCdDeFgGhjmuUVwWxyY]
13893 * Literal conversion specifiers::  %[%nt]
13894 * Padding and other flags::        Pad with zeros, spaces, etc.
13895 * Setting the time::               Changing the system clock.
13896 * Options for date::               Instead of the current time.
13897 @detailmenu
13898 * Date input formats::             Specifying date strings.
13899 @end detailmenu
13900 * Examples of date::               Examples.
13901 @end menu
13902
13903 @node Time conversion specifiers
13904 @subsection Time conversion specifiers
13905
13906 @cindex time conversion specifiers
13907 @cindex conversion specifiers, time
13908
13909 @command{date} conversion specifiers related to times.
13910
13911 @table @samp
13912 @item %H
13913 hour (@samp{00}@dots{}@samp{23})
13914 @item %I
13915 hour (@samp{01}@dots{}@samp{12})
13916 @item %k
13917 hour (@samp{ 0}@dots{}@samp{23}).
13918 This is a @acronym{GNU} extension.
13919 @item %l
13920 hour (@samp{ 1}@dots{}@samp{12}).
13921 This is a @acronym{GNU} extension.
13922 @item %M
13923 minute (@samp{00}@dots{}@samp{59})
13924 @item %N
13925 nanoseconds (@samp{000000000}@dots{}@samp{999999999}).
13926 This is a @acronym{GNU} extension.
13927 @item %p
13928 locale's equivalent of either @samp{AM} or @samp{PM};
13929 blank in many locales.
13930 Noon is treated as @samp{PM} and midnight as @samp{AM}.
13931 @item %P
13932 like @samp{%p}, except lower case.
13933 This is a @acronym{GNU} extension.
13934 @item %r
13935 locale's 12-hour clock time (e.g., @samp{11:11:04 PM})
13936 @item %R
13937 24-hour hour and minute.  Same as @samp{%H:%M}.
13938 This is a @acronym{GNU} extension.
13939 @item %s
13940 @cindex epoch, seconds since
13941 @cindex seconds since the epoch
13942 @cindex beginning of time
13943 seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
13944 Leap seconds are not counted unless leap second support is available.
13945 @xref{%s-examples}, for examples.
13946 This is a @acronym{GNU} extension.
13947 @item %S
13948 second (@samp{00}@dots{}@samp{60}).
13949 This may be @samp{60} if leap seconds are supported.
13950 @item %T
13951 24-hour hour, minute, and second.  Same as @samp{%H:%M:%S}.
13952 @item %X
13953 locale's time representation (e.g., @samp{23:13:48})
13954 @item %z
13955 @w{@acronym{RFC} 2822/@acronym{ISO} 8601} style numeric time zone
13956 (e.g., @samp{-0600} or @samp{+0530}), or nothing if no
13957 time zone is determinable.  This value reflects the numeric time zone
13958 appropriate for the current time, using the time zone rules specified
13959 by the @env{TZ} environment variable.
13960 The time (and optionally, the time zone rules) can be overridden
13961 by the @option{--date} option.
13962 This is a @acronym{GNU} extension.
13963 @item %:z
13964 @w{@acronym{RFC} 3339/@acronym{ISO} 8601} style numeric time zone with
13965 @samp{:} (e.g., @samp{-06:00} or @samp{+05:30}), or nothing if no time
13966 zone is determinable.
13967 This is a @acronym{GNU} extension.
13968 @item %::z
13969 Numeric time zone to the nearest second with @samp{:} (e.g.,
13970 @samp{-06:00:00} or @samp{+05:30:00}), or nothing if no time zone is
13971 determinable.
13972 This is a @acronym{GNU} extension.
13973 @item %:::z
13974 Numeric time zone with @samp{:} using the minimum necessary precision
13975 (e.g., @samp{-06}, @samp{+05:30}, or @samp{-04:56:02}), or nothing if
13976 no time zone is determinable.
13977 This is a @acronym{GNU} extension.
13978 @item %Z
13979 alphabetic time zone abbreviation (e.g., @samp{EDT}), or nothing if no
13980 time zone is determinable.  See @samp{%z} for how it is determined.
13981 @end table
13982
13983
13984 @node Date conversion specifiers
13985 @subsection Date conversion specifiers
13986
13987 @cindex date conversion specifiers
13988 @cindex conversion specifiers, date
13989
13990 @command{date} conversion specifiers related to dates.
13991
13992 @table @samp
13993 @item %a
13994 locale's abbreviated weekday name (e.g., @samp{Sun})
13995 @item %A
13996 locale's full weekday name, variable length (e.g., @samp{Sunday})
13997 @item %b
13998 locale's abbreviated month name (e.g., @samp{Jan})
13999 @item %B
14000 locale's full month name, variable length (e.g., @samp{January})
14001 @item %c
14002 locale's date and time (e.g., @samp{Thu Mar @ 3 23:05:25 2005})
14003 @item %C
14004 century.  This is like @samp{%Y}, except the last two digits are omitted.
14005 For example, it is @samp{20} if @samp{%Y} is @samp{2000},
14006 and is @samp{-0} if @samp{%Y} is @samp{-001}.
14007 It is normally at least two characters, but it may be more.
14008 @item %d
14009 day of month (e.g., @samp{01})
14010 @item %D
14011 date; same as @samp{%m/%d/%y}
14012 @item %e
14013 day of month, space padded; same as @samp{%_d}
14014 @item %F
14015 full date in @acronym{ISO} 8601 format; same as @samp{%Y-%m-%d}.
14016 This is a good choice for a date format, as it is standard and
14017 is easy to sort in the usual case where years are in the range
14018 0000@dots{}9999.
14019 This is a @acronym{GNU} extension.
14020 @item %g
14021 year corresponding to the @acronym{ISO} week number, but without the century
14022 (range @samp{00} through @samp{99}).  This has the same format and value
14023 as @samp{%y}, except that if the @acronym{ISO} week number (see
14024 @samp{%V}) belongs
14025 to the previous or next year, that year is used instead.
14026 This is a @acronym{GNU} extension.
14027 @item %G
14028 year corresponding to the @acronym{ISO} week number.  This has the
14029 same format and value as @samp{%Y}, except that if the @acronym{ISO}
14030 week number (see
14031 @samp{%V}) belongs to the previous or next year, that year is used
14032 instead.
14033 It is normally useful only if @samp{%V} is also used;
14034 for example, the format @samp{%G-%m-%d} is probably a mistake,
14035 since it combines the ISO week number year with the conventional month and day.
14036 This is a @acronym{GNU} extension.
14037 @item %h
14038 same as @samp{%b}
14039 @item %j
14040 day of year (@samp{001}@dots{}@samp{366})
14041 @item %m
14042 month (@samp{01}@dots{}@samp{12})
14043 @item %u
14044 day of week (@samp{1}@dots{}@samp{7}) with @samp{1} corresponding to Monday
14045 @item %U
14046 week number of year, with Sunday as the first day of the week
14047 (@samp{00}@dots{}@samp{53}).
14048 Days in a new year preceding the first Sunday are in week zero.
14049 @item %V
14050 @acronym{ISO} week number, that is, the
14051 week number of year, with Monday as the first day of the week
14052 (@samp{01}@dots{}@samp{53}).
14053 If the week containing January 1 has four or more days in
14054 the new year, then it is considered week 1; otherwise, it is week 53 of
14055 the previous year, and the next week is week 1.  (See the @acronym{ISO} 8601
14056 standard.)
14057 @item %w
14058 day of week (@samp{0}@dots{}@samp{6}) with 0 corresponding to Sunday
14059 @item %W
14060 week number of year, with Monday as first day of week
14061 (@samp{00}@dots{}@samp{53}).
14062 Days in a new year preceding the first Monday are in week zero.
14063 @item %x
14064 locale's date representation (e.g., @samp{12/31/99})
14065 @item %y
14066 last two digits of year (@samp{00}@dots{}@samp{99})
14067 @item %Y
14068 year.  This is normally at least four characters, but it may be more.
14069 Year @samp{0000} precedes year @samp{0001}, and year @samp{-001}
14070 precedes year @samp{0000}.
14071 @end table
14072
14073
14074 @node Literal conversion specifiers
14075 @subsection Literal conversion specifiers
14076
14077 @cindex literal conversion specifiers
14078 @cindex conversion specifiers, literal
14079
14080 @command{date} conversion specifiers that produce literal strings.
14081
14082 @table @samp
14083 @item %%
14084 a literal %
14085 @item %n
14086 a newline
14087 @item %t
14088 a horizontal tab
14089 @end table
14090
14091
14092 @node Padding and other flags
14093 @subsection Padding and other flags
14094
14095 @cindex numeric field padding
14096 @cindex padding of numeric fields
14097 @cindex fields, padding numeric
14098
14099 Unless otherwise specified, @command{date} normally pads numeric fields
14100 with zeros, so that, for
14101 example, numeric months are always output as two digits.
14102 Seconds since the epoch are not padded, though,
14103 since there is no natural width for them.
14104
14105 As a @acronym{GNU} extension, @command{date} recognizes any of the
14106 following optional flags after the @samp{%}:
14107
14108 @table @samp
14109 @item -
14110 (hyphen) Do not pad the field; useful if the output is intended for
14111 human consumption.
14112 @item _
14113 (underscore) Pad with spaces; useful if you need a fixed
14114 number of characters in the output, but zeros are too distracting.
14115 @item 0
14116 (zero) Pad with zeros even if the conversion specifier
14117 would normally pad with spaces.
14118 @item ^
14119 Use upper case characters if possible.
14120 @item #
14121 Use opposite case characters if possible.
14122 A field that is normally upper case becomes lower case, and vice versa.
14123 @end table
14124
14125 @noindent
14126 Here are some examples of padding:
14127
14128 @example
14129 date +%d/%m -d "Feb 1"
14130 @result{} 01/02
14131 date +%-d/%-m -d "Feb 1"
14132 @result{} 1/2
14133 date +%_d/%_m -d "Feb 1"
14134 @result{}  1/ 2
14135 @end example
14136
14137 As a @acronym{GNU} extension, you can specify the field width
14138 (after any flag, if present) as a decimal number.  If the natural size of the
14139 output of the field has less than the specified number of characters,
14140 the result is written right adjusted and padded to the given
14141 size.  For example, @samp{%9B} prints the right adjusted month name in
14142 a field of width 9.
14143
14144 An optional modifier can follow the optional flag and width
14145 specification.  The modifiers are:
14146
14147 @table @samp
14148 @item E
14149 Use the locale's alternate representation for date and time.  This
14150 modifier applies to the @samp{%c}, @samp{%C}, @samp{%x}, @samp{%X},
14151 @samp{%y} and @samp{%Y} conversion specifiers.  In a Japanese locale, for
14152 example, @samp{%Ex} might yield a date format based on the Japanese
14153 Emperors' reigns.
14154
14155 @item O
14156 Use the locale's alternate numeric symbols for numbers.  This modifier
14157 applies only to numeric conversion specifiers.
14158 @end table
14159
14160 If the format supports the modifier but no alternate representation
14161 is available, it is ignored.
14162
14163
14164 @node Setting the time
14165 @subsection Setting the time
14166
14167 @cindex setting the time
14168 @cindex time setting
14169 @cindex appropriate privileges
14170
14171 If given an argument that does not start with @samp{+}, @command{date} sets
14172 the system clock to the date and time specified by that argument (as
14173 described below).  You must have appropriate privileges to set the
14174 system clock.  The @option{--date} and @option{--set} options may not be
14175 used with such an argument.  The @option{--universal} option may be used
14176 with such an argument to indicate that the specified date and time are
14177 relative to Coordinated Universal Time rather than to the local time
14178 zone.
14179
14180 The argument must consist entirely of digits, which have the following
14181 meaning:
14182
14183 @table @samp
14184 @item MM
14185 month
14186 @item DD
14187 day within month
14188 @item hh
14189 hour
14190 @item mm
14191 minute
14192 @item CC
14193 first two digits of year (optional)
14194 @item YY
14195 last two digits of year (optional)
14196 @item ss
14197 second (optional)
14198 @end table
14199
14200 The @option{--set} option also sets the system clock; see the next section.
14201
14202
14203 @node Options for date
14204 @subsection Options for @command{date}
14205
14206 @cindex @command{date} options
14207 @cindex options for @command{date}
14208
14209 The program accepts the following options.  Also see @ref{Common options}.
14210
14211 @table @samp
14212
14213 @item -d @var{datestr}
14214 @itemx --date=@var{datestr}
14215 @opindex -d
14216 @opindex --date
14217 @cindex parsing date strings
14218 @cindex date strings, parsing
14219 @cindex arbitrary date strings, parsing
14220 @opindex yesterday
14221 @opindex tomorrow
14222 @opindex next @var{day}
14223 @opindex last @var{day}
14224 Display the date and time specified in @var{datestr} instead of the
14225 current date and time.  @var{datestr} can be in almost any common
14226 format.  It can contain month names, time zones, @samp{am} and @samp{pm},
14227 @samp{yesterday}, etc.  For example, @option{--date="2004-02-27
14228 14:19:13.489392193 +0530"} specifies the instant of time that is
14229 489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
14230 time zone that is 5 hours and 30 minutes east of @acronym{UTC}.@*
14231 Note: input currently must be in locale independent format. E.g., the
14232 LC_TIME=C below is needed to print back the correct date in many locales:
14233 @example
14234 date -d "$(LC_TIME=C date)"
14235 @end example
14236 @xref{Date input formats}.
14237
14238 @item -f @var{datefile}
14239 @itemx --file=@var{datefile}
14240 @opindex -f
14241 @opindex --file
14242 Parse each line in @var{datefile} as with @option{-d} and display the
14243 resulting date and time.  If @var{datefile} is @samp{-}, use standard
14244 input.  This is useful when you have many dates to process, because the
14245 system overhead of starting up the @command{date} executable many times can
14246 be considerable.
14247
14248 @item -r @var{file}
14249 @itemx --reference=@var{file}
14250 @opindex -r
14251 @opindex --reference
14252 Display the date and time of the last modification of @var{file},
14253 instead of the current date and time.
14254
14255 @item -R
14256 @itemx --rfc-822
14257 @itemx --rfc-2822
14258 @opindex -R
14259 @opindex --rfc-822
14260 @opindex --rfc-2822
14261 Display the date and time using the format @samp{%a, %d %b %Y %H:%M:%S
14262 %z}, evaluated in the C locale so abbreviations are always in English.
14263 For example:
14264
14265 @example
14266 Fri, 09 Sep 2005 13:51:39 -0700
14267 @end example
14268
14269 This format conforms to
14270 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc2822.txt, Internet
14271 @acronym{RFCs} 2822} and
14272 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc822.txt, 822}, the
14273 current and previous standards for Internet email.
14274
14275 @item --rfc-3339=@var{timespec}
14276 @opindex --rfc-3339=@var{timespec}
14277 Display the date using a format specified by
14278 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3339.txt, Internet
14279 @acronym{RFC} 3339}.  This is a subset of the @acronym{ISO} 8601
14280 format, except that it also permits applications to use a space rather
14281 than a @samp{T} to separate dates from times.  Unlike the other
14282 standard formats, @acronym{RFC} 3339 format is always suitable as
14283 input for the @option{--date} (@option{-d}) and @option{--file}
14284 (@option{-f}) options, regardless of the current locale.
14285
14286 The argument @var{timespec} specifies how much of the time to include.
14287 It can be one of the following:
14288
14289 @table @samp
14290 @item date
14291 Print just the full-date, e.g., @samp{2005-09-14}.
14292 This is equivalent to the format @samp{%Y-%m-%d}.
14293
14294 @item seconds
14295 Print the full-date and full-time separated by a space, e.g.,
14296 @samp{2005-09-14 00:56:06+05:30}.  The output ends with a numeric
14297 time-offset; here the @samp{+05:30} means that local time is five
14298 hours and thirty minutes east of @acronym{UTC}.  This is equivalent to
14299 the format @samp{%Y-%m-%d %H:%M:%S%:z}.
14300
14301 @item ns
14302 Like @samp{seconds}, but also print nanoseconds, e.g.,
14303 @samp{2005-09-14 00:56:06.998458565+05:30}.
14304 This is equivalent to the format @samp{%Y-%m-%d %H:%M:%S.%N%:z}.
14305
14306 @end table
14307
14308 @item -s @var{datestr}
14309 @itemx --set=@var{datestr}
14310 @opindex -s
14311 @opindex --set
14312 Set the date and time to @var{datestr}.  See @option{-d} above.
14313
14314 @item -u
14315 @itemx --utc
14316 @itemx --universal
14317 @opindex -u
14318 @opindex --utc
14319 @opindex --universal
14320 @cindex Coordinated Universal Time
14321 @cindex UTC
14322 @cindex Greenwich Mean Time
14323 @cindex GMT
14324 @vindex TZ
14325 Use Coordinated Universal Time (@acronym{UTC}) by operating as if the
14326 @env{TZ} environment variable were set to the string @samp{UTC0}.
14327 Coordinated
14328 Universal Time is often called ``Greenwich Mean Time'' (@sc{gmt}) for
14329 historical reasons.
14330 @end table
14331
14332
14333 @node Examples of date
14334 @subsection Examples of @command{date}
14335
14336 @cindex examples of @command{date}
14337
14338 Here are a few examples.  Also see the documentation for the @option{-d}
14339 option in the previous section.
14340
14341 @itemize @bullet
14342
14343 @item
14344 To print the date of the day before yesterday:
14345
14346 @example
14347 date --date='2 days ago'
14348 @end example
14349
14350 @item
14351 To print the date of the day three months and one day hence:
14352
14353 @example
14354 date --date='3 months 1 day'
14355 @end example
14356
14357 @item
14358 To print the day of year of Christmas in the current year:
14359
14360 @example
14361 date --date='25 Dec' +%j
14362 @end example
14363
14364 @item
14365 To print the current full month name and the day of the month:
14366
14367 @example
14368 date '+%B %d'
14369 @end example
14370
14371 But this may not be what you want because for the first nine days of
14372 the month, the @samp{%d} expands to a zero-padded two-digit field,
14373 for example @samp{date -d 1may '+%B %d'} will print @samp{May 01}.
14374
14375 @item
14376 To print a date without the leading zero for one-digit days
14377 of the month, you can use the (@acronym{GNU} extension)
14378 @samp{-} flag to suppress
14379 the padding altogether:
14380
14381 @example
14382 date -d 1may '+%B %-d
14383 @end example
14384
14385 @item
14386 To print the current date and time in the format required by many
14387 non-@acronym{GNU} versions of @command{date} when setting the system clock:
14388
14389 @example
14390 date +%m%d%H%M%Y.%S
14391 @end example
14392
14393 @item
14394 To set the system clock forward by two minutes:
14395
14396 @example
14397 date --set='+2 minutes'
14398 @end example
14399
14400 @item
14401 To print the date in @acronym{RFC} 2822 format,
14402 use @samp{date --rfc-2822}.  Here is some example output:
14403
14404 @example
14405 Fri, 09 Sep 2005 13:51:39 -0700
14406 @end example
14407
14408 @anchor{%s-examples}
14409 @item
14410 To convert a date string to the number of seconds since the epoch
14411 (which is 1970-01-01 00:00:00 UTC), use the @option{--date} option with
14412 the @samp{%s} format.  That can be useful in sorting and/or graphing
14413 and/or comparing data by date.  The following command outputs the
14414 number of the seconds since the epoch for the time two minutes after the
14415 epoch:
14416
14417 @example
14418 date --date='1970-01-01 00:02:00 +0000' +%s
14419 120
14420 @end example
14421
14422 If you do not specify time zone information in the date string,
14423 @command{date} uses your computer's idea of the time zone when
14424 interpreting the string.  For example, if your computer's time zone is
14425 that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
14426 seconds) behind UTC:
14427
14428 @example
14429 # local time zone used
14430 date --date='1970-01-01 00:02:00' +%s
14431 18120
14432 @end example
14433
14434 @item
14435 If you're sorting or graphing dated data, your raw date values may be
14436 represented as seconds since the epoch.  But few people can look at
14437 the date @samp{946684800} and casually note ``Oh, that's the first second
14438 of the year 2000 in Greenwich, England.''
14439
14440 @example
14441 date --date='2000-01-01 UTC' +%s
14442 946684800
14443 @end example
14444
14445 An alternative is to use the @option{--utc} (@option{-u}) option.
14446 Then you may omit @samp{UTC} from the date string.  Although this
14447 produces the same result for @samp{%s} and many other format sequences,
14448 with a time zone offset different from zero, it would give a different
14449 result for zone-dependent formats like @samp{%z}.
14450
14451 @example
14452 date -u --date=2000-01-01 +%s
14453 946684800
14454 @end example
14455
14456 To convert such an unwieldy number of seconds back to
14457 a more readable form, use a command like this:
14458
14459 @smallexample
14460 # local time zone used
14461 date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
14462 1999-12-31 19:00:00 -0500
14463 @end smallexample
14464
14465 Or if you do not mind depending on the @samp{@@} feature present since
14466 coreutils 5.3.0, you could shorten this to:
14467
14468 @smallexample
14469 date -d @@946684800 +"%F %T %z"
14470 1999-12-31 19:00:00 -0500
14471 @end smallexample
14472
14473 Often it is better to output UTC-relative date and time:
14474
14475 @smallexample
14476 date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
14477 2000-01-01 00:00:00 +0000
14478 @end smallexample
14479
14480 @end itemize
14481
14482
14483 @node arch invocation
14484 @section @command{arch}: Print machine hardware name
14485
14486 @pindex arch
14487 @cindex print machine hardware name
14488 @cindex system information, printing
14489
14490 @command{arch} prints the machine hardware name,
14491 and is equivalent to @samp{uname -m}.
14492 Synopsis:
14493
14494 @example
14495 arch [@var{option}]
14496 @end example
14497
14498 The program accepts the @ref{Common options} only.
14499
14500 @exitstatus
14501
14502
14503 @node nproc invocation
14504 @section @command{nproc}: Print the number of available processors
14505
14506 @pindex nproc
14507 @cindex Print the number of processors
14508 @cindex system information, printing
14509
14510 Print the number of processing units available to the current process,
14511 which may be less than the number of online processors.
14512 If this information is not accessible, then print the number of
14513 processors installed.  If the @env{OMP_NUM_THREADS} environment variable is
14514 set, then it will determine the returned value.  The result is guaranteed to be
14515 greater than zero.  Synopsis:
14516
14517 @example
14518 nproc [@var{option}]
14519 @end example
14520
14521 The program accepts the following options.  Also see @ref{Common options}.
14522
14523 @table @samp
14524
14525 @item --all
14526 @opindex --all
14527 Print the number of installed processors on the system, which may
14528 be greater than the number online or available to the current process.
14529 The @env{OMP_NUM_THREADS} environment variable is not honored in this case.
14530
14531 @item --ignore=@var{number}
14532 @opindex --ignore
14533 If possible, exclude this @var{number} of processing units.
14534
14535 @end table
14536
14537 @exitstatus
14538
14539
14540 @node uname invocation
14541 @section @command{uname}: Print system information
14542
14543 @pindex uname
14544 @cindex print system information
14545 @cindex system information, printing
14546
14547 @command{uname} prints information about the machine and operating system
14548 it is run on.  If no options are given, @command{uname} acts as if the
14549 @option{-s} option were given.  Synopsis:
14550
14551 @example
14552 uname [@var{option}]@dots{}
14553 @end example
14554
14555 If multiple options or @option{-a} are given, the selected information is
14556 printed in this order:
14557
14558 @example
14559 @var{kernel-name} @var{nodename} @var{kernel-release} @var{kernel-version}
14560 @var{machine} @var{processor} @var{hardware-platform} @var{operating-system}
14561 @end example
14562
14563 The information may contain internal spaces, so such output cannot be
14564 parsed reliably.  In the following example, @var{release} is
14565 @samp{2.2.18ss.e820-bda652a #4 SMP Tue Jun 5 11:24:08 PDT 2001}:
14566
14567 @smallexample
14568 uname -a
14569 @result{} Linux dum 2.2.18 #4 SMP Tue Jun 5 11:24:08 PDT 2001 i686@c
14570  unknown unknown GNU/Linux
14571 @end smallexample
14572
14573
14574 The program accepts the following options.  Also see @ref{Common options}.
14575
14576 @table @samp
14577
14578 @item -a
14579 @itemx --all
14580 @opindex -a
14581 @opindex --all
14582 Print all of the below information, except omit the processor type
14583 and the hardware platform name if they are unknown.
14584
14585 @item -i
14586 @itemx --hardware-platform
14587 @opindex -i
14588 @opindex --hardware-platform
14589 @cindex implementation, hardware
14590 @cindex hardware platform
14591 @cindex platform, hardware
14592 Print the hardware platform name
14593 (sometimes called the hardware implementation).
14594 Print @samp{unknown} if the kernel does not make this information
14595 easily available, as is the case with Linux kernels.
14596
14597 @item -m
14598 @itemx --machine
14599 @opindex -m
14600 @opindex --machine
14601 @cindex machine type
14602 @cindex hardware class
14603 @cindex hardware type
14604 Print the machine hardware name (sometimes called the hardware class
14605 or hardware type).
14606
14607 @item -n
14608 @itemx --nodename
14609 @opindex -n
14610 @opindex --nodename
14611 @cindex hostname
14612 @cindex node name
14613 @cindex network node name
14614 Print the network node hostname.
14615
14616 @item -p
14617 @itemx --processor
14618 @opindex -p
14619 @opindex --processor
14620 @cindex host processor type
14621 Print the processor type (sometimes called the instruction set
14622 architecture or ISA).
14623 Print @samp{unknown} if the kernel does not make this information
14624 easily available, as is the case with Linux kernels.
14625
14626 @item -o
14627 @itemx --operating-system
14628 @opindex -o
14629 @opindex --operating-system
14630 @cindex operating system name
14631 Print the name of the operating system.
14632
14633 @item -r
14634 @itemx --kernel-release
14635 @opindex -r
14636 @opindex --kernel-release
14637 @cindex kernel release
14638 @cindex release of kernel
14639 Print the kernel release.
14640
14641 @item -s
14642 @itemx --kernel-name
14643 @opindex -s
14644 @opindex --kernel-name
14645 @cindex kernel name
14646 @cindex name of kernel
14647 Print the kernel name.
14648 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) calls this
14649 ``the implementation of the operating system'', because the
14650 @acronym{POSIX} specification itself has no notion of ``kernel''.
14651 The kernel name might be the same as the operating system name printed
14652 by the @option{-o} or @option{--operating-system} option, but it might
14653 differ.  Some operating systems (e.g., FreeBSD, HP-UX) have the same
14654 name as their underlying kernels; others (e.g., GNU/Linux, Solaris)
14655 do not.
14656
14657 @item -v
14658 @itemx --kernel-version
14659 @opindex -v
14660 @opindex --kernel-version
14661 @cindex kernel version
14662 @cindex version of kernel
14663 Print the kernel version.
14664
14665 @end table
14666
14667 @exitstatus
14668
14669
14670 @node hostname invocation
14671 @section @command{hostname}: Print or set system name
14672
14673 @pindex hostname
14674 @cindex setting the hostname
14675 @cindex printing the hostname
14676 @cindex system name, printing
14677 @cindex appropriate privileges
14678
14679 With no arguments, @command{hostname} prints the name of the current host
14680 system.  With one argument, it sets the current host name to the
14681 specified string.  You must have appropriate privileges to set the host
14682 name.  Synopsis:
14683
14684 @example
14685 hostname [@var{name}]
14686 @end example
14687
14688 The only options are @option{--help} and @option{--version}.  @xref{Common
14689 options}.
14690
14691 @exitstatus
14692
14693
14694 @node hostid invocation
14695 @section @command{hostid}: Print numeric host identifier
14696
14697 @pindex hostid
14698 @cindex printing the host identifier
14699
14700 @command{hostid} prints the numeric identifier of the current host
14701 in hexadecimal.  This command accepts no arguments.
14702 The only options are @option{--help} and @option{--version}.
14703 @xref{Common options}.
14704
14705 For example, here's what it prints on one system I use:
14706
14707 @example
14708 $ hostid
14709 1bac013d
14710 @end example
14711
14712 On that system, the 32-bit quantity happens to be closely
14713 related to the system's Internet address, but that isn't always
14714 the case.
14715
14716 @exitstatus
14717
14718 @node uptime invocation
14719 @section @command{uptime}: Print system uptime and load
14720
14721 @pindex uptime
14722 @cindex printing the system uptime and load
14723
14724 @command{uptime} prints the current time, the system's uptime, the
14725 number of logged-in users and the current load average.
14726
14727 If an argument is specified, it is used as the file to be read
14728 to discover how many users are logged in.  If no argument is
14729 specified, a system default is used (@command{uptime --help} indicates
14730 the default setting).
14731
14732 The only options are @option{--help} and @option{--version}.
14733 @xref{Common options}.
14734
14735 For example, here's what it prints right now on one system I use:
14736
14737 @example
14738 $ uptime
14739  14:07  up   3:35,  3 users,  load average: 1.39, 1.15, 1.04
14740 @end example
14741
14742 The precise method of calculation of load average varies somewhat
14743 between systems.  Some systems calculate it as the average number of
14744 runnable processes over the last 1, 5 and 15 minutes, but some systems
14745 also include processes in the uninterruptible sleep state (that is,
14746 those processes which are waiting for disk I/O).  The Linux kernel
14747 includes uninterruptible processes.
14748
14749 @node SELinux context
14750 @chapter SELinux context
14751
14752 @cindex SELinux context
14753 @cindex SELinux, context
14754 @cindex commands for SELinux context
14755
14756 This section describes commands for operations with SELinux
14757 contexts.
14758
14759 @menu
14760 * chcon invocation::            Change SELinux context of file
14761 * runcon invocation::           Run a command in specified SELinux context
14762 @end menu
14763
14764 @node chcon invocation
14765 @section @command{chcon}: Change SELinux context of file
14766
14767 @pindex chcon
14768 @cindex changing security context
14769 @cindex change SELinux context
14770
14771 @command{chcon} changes the SELinux security context of the selected files.
14772 Synopses:
14773
14774 @smallexample
14775 chcon [@var{option}]@dots{} @var{context} @var{file}@dots{}
14776 chcon [@var{option}]@dots{} [-u @var{user}] [-r @var{role}] [-l @var{range}]@c
14777  [-t @var{type}] @var{file}@dots{}
14778 chcon [@var{option}]@dots{} --reference=@var{rfile} @var{file}@dots{}
14779 @end smallexample
14780
14781 Change the SELinux security context of each @var{file} to @var{context}.
14782 With @option{--reference}, change the security context of each @var{file}
14783 to that of @var{rfile}.
14784
14785 The program accepts the following options.  Also see @ref{Common options}.
14786
14787 @table @samp
14788
14789 @item -h
14790 @itemx --no-dereference
14791 @opindex -h
14792 @opindex --no-dereference
14793 @cindex no dereference
14794 Affect symbolic links instead of any referenced file.
14795
14796 @item --reference=@var{rfile}
14797 @opindex --reference
14798 @cindex reference file
14799 Use @var{rfile}'s security context rather than specifying a @var{context} value.
14800
14801 @item -R
14802 @itemx --recursive
14803 @opindex -R
14804 @opindex --recursive
14805 Operate on files and directories recursively.
14806
14807 @choptH
14808 @xref{Traversing symlinks}.
14809
14810 @choptL
14811 @xref{Traversing symlinks}.
14812
14813 @choptP
14814 @xref{Traversing symlinks}.
14815
14816 @item -v
14817 @itemx --verbose
14818 @opindex -v
14819 @opindex --verbose
14820 @cindex diagnostic
14821 Output a diagnostic for every file processed.
14822
14823 @item -u @var{user}
14824 @itemx --user=@var{user}
14825 @opindex -u
14826 @opindex --user
14827 Set user @var{user} in the target security context.
14828
14829 @item -r @var{role}
14830 @itemx --role=@var{role}
14831 @opindex -r
14832 @opindex --role
14833 Set role @var{role} in the target security context.
14834
14835 @item -t @var{type}
14836 @itemx --type=@var{type}
14837 @opindex -t
14838 @opindex --type
14839 Set type @var{type} in the target security context.
14840
14841 @item -l @var{range}
14842 @itemx --range=@var{range}
14843 @opindex -l
14844 @opindex --range
14845 Set range @var{range} in the target security context.
14846
14847 @end table
14848
14849 @exitstatus
14850
14851 @node runcon invocation
14852 @section @command{runcon}: Run a command in specified SELinux context
14853
14854 @pindex runcon
14855 @cindex run with security context
14856
14857
14858 @command{runcon} runs file in specified SELinux security context.
14859
14860 Synopses:
14861 @smallexample
14862 runcon @var{context} @var{command} [@var{args}]
14863 runcon [ -c ] [-u @var{user}] [-r @var{role}] [-t @var{type}]@c
14864  [-l @var{range}] @var{command} [@var{args}]
14865 @end smallexample
14866
14867 Run @var{command} with completely-specified @var{context}, or with
14868 current or transitioned security context modified by one or more of @var{level},
14869 @var{role}, @var{type} and @var{user}.
14870
14871 If none of @option{-c}, @option{-t}, @option{-u}, @option{-r}, or @option{-l}
14872 is specified, the first argument is used as the complete context.
14873 Any additional arguments after @var{command}
14874 are interpreted as arguments to the command.
14875
14876 With neither @var{context} nor @var{command}, print the current
14877 security context.
14878
14879 The program accepts the following options.  Also see @ref{Common options}.
14880
14881 @table @samp
14882
14883 @item -c
14884 @itemx --compute
14885 @opindex -c
14886 @opindex --compute
14887 Compute process transition context before modifying.
14888
14889 @item -u @var{user}
14890 @itemx --user=@var{user}
14891 @opindex -u
14892 @opindex --user
14893 Set user @var{user} in the target security context.
14894
14895 @item -r @var{role}
14896 @itemx --role=@var{role}
14897 @opindex -r
14898 @opindex --role
14899 Set role @var{role} in the target security context.
14900
14901 @item -t @var{type}
14902 @itemx --type=@var{type}
14903 @opindex -t
14904 @opindex --type
14905 Set type @var{type} in the target security context.
14906
14907 @item -l @var{range}
14908 @itemx --range=@var{range}
14909 @opindex -l
14910 @opindex --range
14911 Set range @var{range} in the target security context.
14912
14913 @end table
14914
14915 @cindex exit status of @command{runcon}
14916 Exit status:
14917
14918 @display
14919 126 if @var{command} is found but cannot be invoked
14920 127 if @command{runcon} itself fails or if @var{command} cannot be found
14921 the exit status of @var{command} otherwise
14922 @end display
14923
14924 @node Modified command invocation
14925 @chapter Modified command invocation
14926
14927 @cindex modified command invocation
14928 @cindex invocation of commands, modified
14929 @cindex commands for invoking other commands
14930
14931 This section describes commands that run other commands in some context
14932 different than the current one: a modified environment, as a different
14933 user, etc.
14934
14935 @menu
14936 * chroot invocation::           Modify the root directory.
14937 * env invocation::              Modify environment variables.
14938 * nice invocation::             Modify niceness.
14939 * nohup invocation::            Immunize to hangups.
14940 * stdbuf invocation::           Modify buffering of standard streams.
14941 * su invocation::               Modify user and group ID.
14942 * timeout invocation::          Run with time limit.
14943 @end menu
14944
14945
14946 @node chroot invocation
14947 @section @command{chroot}: Run a command with a different root directory
14948
14949 @pindex chroot
14950 @cindex running a program in a specified root directory
14951 @cindex root directory, running a program in a specified
14952
14953 @command{chroot} runs a command with a specified root directory.
14954 On many systems, only the super-user can do this.@footnote{However,
14955 some systems (e.g., FreeBSD) can be configured to allow certain regular
14956 users to use the @code{chroot} system call, and hence to run this program.
14957 Also, on Cygwin, anyone can run the @command{chroot} command, because the
14958 underlying function is non-privileged due to lack of support in MS-Windows.}
14959 Synopses:
14960
14961 @example
14962 chroot @var{option} @var{newroot} [@var{command} [@var{args}]@dots{}]
14963 chroot @var{option}
14964 @end example
14965
14966 Ordinarily, file names are looked up starting at the root of the
14967 directory structure, i.e., @file{/}.  @command{chroot} changes the root to
14968 the directory @var{newroot} (which must exist) and then runs
14969 @var{command} with optional @var{args}.  If @var{command} is not
14970 specified, the default is the value of the @env{SHELL} environment
14971 variable or @command{/bin/sh} if not set, invoked with the @option{-i} option.
14972 @var{command} must not be a special built-in utility
14973 (@pxref{Special built-in utilities}).
14974
14975 The program accepts the following options.  Also see @ref{Common options}.
14976 Options must precede operands.
14977
14978 @table @samp
14979
14980 @itemx --userspec=@var{user}[:@var{group}]
14981 @opindex --userspec
14982 By default, @var{command} is run with the same credentials
14983 as the invoking process.
14984 Use this option to run it as a different @var{user} and/or with a
14985 different primary @var{group}.
14986
14987 @itemx --groups=@var{groups}
14988 @opindex --groups
14989 Use this option to specify the supplementary @var{groups} to be
14990 used by the new process.
14991 The items in the list (names or numeric IDs) must be separated by commas.
14992
14993 @end table
14994
14995 Here are a few tips to help avoid common problems in using chroot.
14996 To start with a simple example, make @var{command} refer to a statically
14997 linked binary.  If you were to use a dynamically linked executable, then
14998 you'd have to arrange to have the shared libraries in the right place under
14999 your new root directory.
15000
15001 For example, if you create a statically linked @command{ls} executable,
15002 and put it in @file{/tmp/empty}, you can run this command as root:
15003
15004 @example
15005 $ chroot /tmp/empty /ls -Rl /
15006 @end example
15007
15008 Then you'll see output like this:
15009
15010 @example
15011 /:
15012 total 1023
15013 -rwxr-xr-x 1 0 0 1041745 Aug 16 11:17 ls
15014 @end example
15015
15016 If you want to use a dynamically linked executable, say @command{bash},
15017 then first run @samp{ldd bash} to see what shared objects it needs.
15018 Then, in addition to copying the actual binary, also copy the listed
15019 files to the required positions under your intended new root directory.
15020 Finally, if the executable requires any other files (e.g., data, state,
15021 device files), copy them into place, too.
15022
15023 @cindex exit status of @command{chroot}
15024 Exit status:
15025
15026 @display
15027 125 if @command{chroot} itself fails
15028 126 if @var{command} is found but cannot be invoked
15029 127 if @var{command} cannot be found
15030 the exit status of @var{command} otherwise
15031 @end display
15032
15033
15034 @node env invocation
15035 @section @command{env}: Run a command in a modified environment
15036
15037 @pindex env
15038 @cindex environment, running a program in a modified
15039 @cindex modified environment, running a program in a
15040 @cindex running a program in a modified environment
15041
15042 @command{env} runs a command with a modified environment.  Synopses:
15043
15044 @example
15045 env [@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c
15046 [@var{command} [@var{args}]@dots{}]
15047 env
15048 @end example
15049
15050 Operands of the form @samp{@var{variable}=@var{value}} set
15051 the environment variable @var{variable} to value @var{value}.
15052 @var{value} may be empty (@samp{@var{variable}=}).  Setting a variable
15053 to an empty value is different from unsetting it.
15054 These operands are evaluated left-to-right, so if two operands
15055 mention the same variable the earlier is ignored.
15056
15057 Environment variable names can be empty, and can contain any
15058 characters other than @samp{=} and @acronym{ASCII} @sc{nul}.
15059 However, it is wise to limit yourself to names that
15060 consist solely of underscores, digits, and @acronym{ASCII} letters,
15061 and that begin with a non-digit, as applications like the shell do not
15062 work well with other names.
15063
15064 @vindex PATH
15065 The first operand that does not contain the character @samp{=}
15066 specifies the program to invoke; it is
15067 searched for according to the @env{PATH} environment variable.  Any
15068 remaining arguments are passed as arguments to that program.
15069 The program should not be a special built-in utility
15070 (@pxref{Special built-in utilities}).
15071
15072 Modifications to @env{PATH} take effect prior to searching for
15073 @var{command}.  Use caution when reducing @env{PATH}; behavior is
15074 not portable when @env{PATH} is undefined or omits key directories
15075 such as @file{/bin}.
15076
15077 In the rare case that a utility contains a @samp{=} in the name, the
15078 only way to disambiguate it from a variable assignment is to use an
15079 intermediate command for @var{command}, and pass the problematic
15080 program name via @var{args}.  For example, if @file{./prog=} is an
15081 executable in the current @env{PATH}:
15082
15083 @example
15084 env prog= true # runs 'true', with prog= in environment
15085 env ./prog= true # runs 'true', with ./prog= in environment
15086 env -- prog= true # runs 'true', with prog= in environment
15087 env sh -c '\prog= true' # runs 'prog=' with argument 'true'
15088 env sh -c 'exec "$@@"' sh prog= true # also runs 'prog='
15089 @end example
15090
15091 @cindex environment, printing
15092
15093 If no command name is specified following the environment
15094 specifications, the resulting environment is printed.  This is like
15095 specifying the @command{printenv} program.
15096
15097 For some examples, suppose the environment passed to @command{env}
15098 contains @samp{LOGNAME=rms}, @samp{EDITOR=emacs}, and
15099 @samp{PATH=.:/gnubin:/hacks}:
15100
15101 @itemize @bullet
15102
15103 @item
15104 Output the current environment.
15105 @example
15106 $ env | LC_ALL=C sort
15107 EDITOR=emacs
15108 LOGNAME=rms
15109 PATH=.:/gnubin:/hacks
15110 @end example
15111
15112 @item
15113 Run @command{foo} with a reduced environment, preserving only the
15114 original @env{PATH} to avoid problems in locating @command{foo}.
15115 @example
15116 env - PATH="$PATH" foo
15117 @end example
15118
15119 @item
15120 Run @command{foo} with the environment containing @samp{LOGNAME=rms},
15121 @samp{EDITOR=emacs}, and @samp{PATH=.:/gnubin:/hacks}, and guarantees
15122 that @command{foo} was found in the file system rather than as a shell
15123 built-in.
15124 @example
15125 env foo
15126 @end example
15127
15128 @item
15129 Run @command{nemacs} with the environment containing @samp{LOGNAME=foo},
15130 @samp{EDITOR=emacs}, @samp{PATH=.:/gnubin:/hacks}, and
15131 @samp{DISPLAY=gnu:0}.
15132 @example
15133 env DISPLAY=gnu:0 LOGNAME=foo nemacs
15134 @end example
15135
15136 @item
15137 Attempt to run the program @command{/energy/--} (as that is the only
15138 possible path search result); if the command exists, the environment
15139 will contain @samp{LOGNAME=rms} and @samp{PATH=/energy}, and the
15140 arguments will be @samp{e=mc2}, @samp{bar}, and @samp{baz}.
15141 @example
15142 env -u EDITOR PATH=/energy -- e=mc2 bar baz
15143 @end example
15144
15145 @end itemize
15146
15147
15148 The program accepts the following options.  Also see @ref{Common options}.
15149 Options must precede operands.
15150
15151 @table @samp
15152
15153 @optNull{env}
15154
15155 @item -u @var{name}
15156 @itemx --unset=@var{name}
15157 @opindex -u
15158 @opindex --unset
15159 Remove variable @var{name} from the environment, if it was in the
15160 environment.
15161
15162 @item -
15163 @itemx -i
15164 @itemx --ignore-environment
15165 @opindex -
15166 @opindex -i
15167 @opindex --ignore-environment
15168 Start with an empty environment, ignoring the inherited environment.
15169
15170 @end table
15171
15172 @cindex exit status of @command{env}
15173 Exit status:
15174
15175 @display
15176 0   if no @var{command} is specified and the environment is output
15177 125 if @command{env} itself fails
15178 126 if @var{command} is found but cannot be invoked
15179 127 if @var{command} cannot be found
15180 the exit status of @var{command} otherwise
15181 @end display
15182
15183
15184 @node nice invocation
15185 @section @command{nice}: Run a command with modified niceness
15186
15187 @pindex nice
15188 @cindex niceness
15189 @cindex scheduling, affecting
15190 @cindex appropriate privileges
15191
15192 @command{nice} prints or modifies a process's @dfn{niceness},
15193 a parameter that affects whether the process is scheduled favorably.
15194 Synopsis:
15195
15196 @example
15197 nice [@var{option}]@dots{} [@var{command} [@var{arg}]@dots{}]
15198 @end example
15199
15200 If no arguments are given, @command{nice} prints the current niceness.
15201 Otherwise, @command{nice} runs the given @var{command} with its
15202 niceness adjusted.  By default, its niceness is incremented by 10.
15203
15204 Niceness values range at least from @minus{}20 (process has high priority
15205 and gets more resources, thus slowing down other processes) through 19
15206 (process has lower priority and runs slowly itself, but has less impact
15207 on the speed of other running processes).  Some systems
15208 may have a wider range of nicenesses; conversely, other systems may
15209 enforce more restrictive limits.  An attempt to set the niceness
15210 outside the supported range is treated as an attempt to use the
15211 minimum or maximum supported value.
15212
15213 A niceness should not be confused with a scheduling priority, which
15214 lets applications determine the order in which threads are scheduled
15215 to run.  Unlike a priority, a niceness is merely advice to the
15216 scheduler, which the scheduler is free to ignore.  Also, as a point of
15217 terminology, @acronym{POSIX} defines the behavior of @command{nice} in
15218 terms of a @dfn{nice value}, which is the nonnegative difference
15219 between a niceness and the minimum niceness.  Though @command{nice}
15220 conforms to @acronym{POSIX}, its documentation and diagnostics use the
15221 term ``niceness'' for compatibility with historical practice.
15222
15223 @var{command} must not be a special built-in utility (@pxref{Special
15224 built-in utilities}).
15225
15226 @mayConflictWithShellBuiltIn{nice}
15227
15228 The program accepts the following option.  Also see @ref{Common options}.
15229 Options must precede operands.
15230
15231 @table @samp
15232 @item -n @var{adjustment}
15233 @itemx --adjustment=@var{adjustment}
15234 @opindex -n
15235 @opindex --adjustment
15236 Add @var{adjustment} instead of 10 to the command's niceness.  If
15237 @var{adjustment} is negative and you lack appropriate privileges,
15238 @command{nice} issues a warning but otherwise acts as if you specified
15239 a zero adjustment.
15240
15241 For compatibility @command{nice} also supports an obsolete
15242 option syntax @option{-@var{adjustment}}.  New scripts should use
15243 @option{-n @var{adjustment}} instead.
15244
15245 @end table
15246
15247 @cindex exit status of @command{nice}
15248 Exit status:
15249
15250 @display
15251 0   if no @var{command} is specified and the niceness is output
15252 125 if @command{nice} itself fails
15253 126 if @var{command} is found but cannot be invoked
15254 127 if @var{command} cannot be found
15255 the exit status of @var{command} otherwise
15256 @end display
15257
15258 It is sometimes useful to run a non-interactive program with reduced niceness.
15259
15260 @example
15261 $ nice factor 4611686018427387903
15262 @end example
15263
15264 Since @command{nice} prints the current niceness,
15265 you can invoke it through itself to demonstrate how it works.
15266
15267 The default behavior is to increase the niceness by @samp{10}:
15268
15269 @example
15270 $ nice
15271 0
15272 $ nice nice
15273 10
15274 $ nice -n 10 nice
15275 10
15276 @end example
15277
15278 The @var{adjustment} is relative to the current niceness.  In the
15279 next example, the first @command{nice} invocation runs the second one
15280 with niceness 10, and it in turn runs the final one with a niceness
15281 that is 3 more:
15282
15283 @example
15284 $ nice nice -n 3 nice
15285 13
15286 @end example
15287
15288 Specifying a niceness larger than the supported range
15289 is the same as specifying the maximum supported value:
15290
15291 @example
15292 $ nice -n 10000000000 nice
15293 19
15294 @end example
15295
15296 Only a privileged user may run a process with lower niceness:
15297
15298 @example
15299 $ nice -n -1 nice
15300 nice: cannot set niceness: Permission denied
15301 0
15302 $ sudo nice -n -1 nice
15303 -1
15304 @end example
15305
15306
15307 @node nohup invocation
15308 @section @command{nohup}: Run a command immune to hangups
15309
15310 @pindex nohup
15311 @cindex hangups, immunity to
15312 @cindex immunity to hangups
15313 @cindex logging out and continuing to run
15314
15315 @flindex nohup.out
15316 @command{nohup} runs the given @var{command} with hangup signals ignored,
15317 so that the command can continue running in the background after you log
15318 out.  Synopsis:
15319
15320 @example
15321 nohup @var{command} [@var{arg}]@dots{}
15322 @end example
15323
15324 If standard input is a terminal, it is redirected from
15325 @file{/dev/null} so that terminal sessions do not mistakenly consider
15326 the terminal to be used by the command.  This is a @acronym{GNU}
15327 extension; programs intended to be portable to non-@acronym{GNU} hosts
15328 should use @samp{nohup @var{command} [@var{arg}]@dots{} </dev/null}
15329 instead.
15330
15331 @flindex nohup.out
15332 If standard output is a terminal, the command's standard output is appended
15333 to the file @file{nohup.out}; if that cannot be written to, it is appended
15334 to the file @file{$HOME/nohup.out}; and if that cannot be written to, the
15335 command is not run.
15336 Any @file{nohup.out} or @file{$HOME/nohup.out} file created by
15337 @command{nohup} is made readable and writable only to the user,
15338 regardless of the current umask settings.
15339
15340 If standard error is a terminal, it is normally redirected to the same file
15341 descriptor as the (possibly-redirected) standard output.
15342 However, if standard output is closed, standard error terminal output
15343 is instead appended to the file @file{nohup.out} or
15344 @file{$HOME/nohup.out} as above.
15345
15346 To capture the command's output to a file other than @file{nohup.out}
15347 you can redirect it.  For example, to capture the output of
15348 @command{make}:
15349
15350 @example
15351 nohup make > make.log
15352 @end example
15353
15354 @command{nohup} does not automatically put the command it runs in the
15355 background; you must do that explicitly, by ending the command line
15356 with an @samp{&}.  Also, @command{nohup} does not alter the
15357 niceness of @var{command}; use @command{nice} for that,
15358 e.g., @samp{nohup nice @var{command}}.
15359
15360 @var{command} must not be a special built-in utility (@pxref{Special
15361 built-in utilities}).
15362
15363 The only options are @option{--help} and @option{--version}.  @xref{Common
15364 options}.  Options must precede operands.
15365
15366 @cindex exit status of @command{nohup}
15367 Exit status:
15368
15369 @display
15370 125 if @command{nohup} itself fails, and @env{POSIXLY_CORRECT} is not set
15371 126 if @var{command} is found but cannot be invoked
15372 127 if @var{command} cannot be found
15373 the exit status of @var{command} otherwise
15374 @end display
15375
15376 If @env{POSIXLY_CORRECT} is set, internal failures give status 127
15377 instead of 125.
15378
15379
15380 @node stdbuf invocation
15381 @section @command{stdbuf}: Run a command with modified I/O stream buffering
15382
15383 @pindex stdbuf
15384 @cindex standard streams, buffering
15385 @cindex line buffered
15386
15387 @command{stdbuf} allows one to modify the buffering operations of the
15388 three standard I/O streams associated with a program.  Synopsis:
15389
15390 @example
15391 stdbuf @var{option}@dots{} @var{command}
15392 @end example
15393
15394 Any additional @var{arg}s are passed as additional arguments to the
15395 @var{command}.
15396
15397 The program accepts the following options.  Also see @ref{Common options}.
15398
15399 @table @samp
15400
15401 @item -i @var{mode}
15402 @itemx --input=@var{mode}
15403 @opindex -i
15404 @opindex --input
15405 Adjust the standard input stream buffering.
15406
15407 @item -o @var{mode}
15408 @itemx --output=@var{mode}
15409 @opindex -o
15410 @opindex --output
15411 Adjust the standard output stream buffering.
15412
15413 @item -e @var{mode}
15414 @itemx --error=@var{mode}
15415 @opindex -e
15416 @opindex --error
15417 Adjust the standard error stream buffering.
15418
15419 @end table
15420
15421 The @var{mode} can be specified as follows:
15422
15423 @table @samp
15424
15425 @item L
15426 Set the stream to line buffered mode.
15427 In this mode data is coalesced until a newline is output or
15428 input is read from any stream attached to a terminal device.
15429 This option is invalid with standard input.
15430
15431 @item 0
15432 Disable buffering of the selected stream.
15433 In this mode data is output immediately and only the
15434 amount of data requested is read from input.
15435
15436 @item @var{size}
15437 Specify the size of the buffer to use in fully buffered mode.
15438 @multiplierSuffixesNoBlocks{size}
15439
15440 @end table
15441
15442 NOTE: If @var{command} adjusts the buffering of its standard streams
15443 (@command{tee} does for e.g.) then that will override corresponding settings
15444 changed by @command{stdbuf}.  Also some filters (like @command{dd} and
15445 @command{cat} etc.) don't use streams for I/O, and are thus unaffected
15446 by @command{stdbuf} settings.
15447
15448 @cindex exit status of @command{stdbuf}
15449 Exit status:
15450
15451 @display
15452 125 if @command{stdbuf} itself fails
15453 126 if @var{command} is found but cannot be invoked
15454 127 if @var{command} cannot be found
15455 the exit status of @var{command} otherwise
15456 @end display
15457
15458
15459 @node su invocation
15460 @section @command{su}: Run a command with substitute user and group ID
15461
15462 @pindex su
15463 @cindex substitute user and group IDs
15464 @cindex user ID, switching
15465 @cindex super-user, becoming
15466 @cindex root, becoming
15467
15468 @command{su} allows one user to temporarily become another user.  It runs a
15469 command (often an interactive shell) with the real and effective user
15470 ID, group ID, and supplemental groups of a given @var{user}.  Synopsis:
15471
15472 @example
15473 su [@var{option}]@dots{} [@var{user} [@var{arg}]@dots{}]
15474 @end example
15475
15476 @cindex passwd entry, and @command{su} shell
15477 @flindex /bin/sh
15478 @flindex /etc/passwd
15479 If no @var{user} is given, the default is @code{root}, the super-user.
15480 The shell to use is taken from @var{user}'s @code{passwd} entry, or
15481 @file{/bin/sh} if none is specified there.  If @var{user} has a
15482 password, @command{su} prompts for the password unless run by a user with
15483 effective user ID of zero (the super-user).
15484
15485 @vindex HOME
15486 @vindex SHELL
15487 @vindex USER
15488 @vindex LOGNAME
15489 @cindex login shell
15490 By default, @command{su} does not change the current directory.
15491 It sets the environment variables @env{HOME} and @env{SHELL}
15492 from the password entry for @var{user}, and if @var{user} is not
15493 the super-user, sets @env{USER} and @env{LOGNAME} to @var{user}.
15494 By default, the shell is not a login shell.
15495
15496 Any additional @var{arg}s are passed as additional arguments to the
15497 shell.
15498
15499 @cindex @option{-su}
15500 GNU @command{su} does not treat @file{/bin/sh} or any other shells specially
15501 (e.g., by setting @code{argv[0]} to @option{-su}, passing @option{-c} only
15502 to certain shells, etc.).
15503
15504 @findex syslog
15505 @command{su} can optionally be compiled to use @code{syslog} to report
15506 failed, and optionally successful, @command{su} attempts.  (If the system
15507 supports @code{syslog}.)  However, GNU @command{su} does not check if the
15508 user is a member of the @code{wheel} group; see below.
15509
15510 The program accepts the following options.  Also see @ref{Common options}.
15511
15512 @table @samp
15513 @item -c @var{command}
15514 @itemx --command=@var{command}
15515 @opindex -c
15516 @opindex --command
15517 Pass @var{command}, a single command line to run, to the shell with
15518 a @option{-c} option instead of starting an interactive shell.
15519
15520 @item -f
15521 @itemx --fast
15522 @opindex -f
15523 @opindex --fast
15524 @flindex .cshrc
15525 @cindex file name pattern expansion, disabled
15526 @cindex globbing, disabled
15527 Pass the @option{-f} option to the shell.  This probably only makes sense
15528 if the shell run is @command{csh} or @command{tcsh}, for which the @option{-f}
15529 option prevents reading the startup file (@file{.cshrc}).  With
15530 Bourne-like shells, the @option{-f} option disables file name pattern
15531 expansion (globbing), which is not likely to be useful.
15532
15533 @item -
15534 @itemx -l
15535 @itemx --login
15536 @opindex -
15537 @opindex -l
15538 @opindex --login
15539 @c other variables already indexed above
15540 @vindex TERM
15541 @vindex PATH
15542 @cindex login shell, creating
15543 Make the shell a login shell.  This means the following.  Unset all
15544 environment variables except @env{TERM}, @env{HOME}, and @env{SHELL}
15545 (which are set as described above), and @env{USER} and @env{LOGNAME}
15546 (which are set, even for the super-user, as described above), and set
15547 @env{PATH} to a compiled-in default value.  Change to @var{user}'s home
15548 directory.  Prepend @samp{-} to the shell's name, intended to make it
15549 read its login startup file(s).
15550
15551 @item -m
15552 @itemx -p
15553 @itemx --preserve-environment
15554 @opindex -m
15555 @opindex -p
15556 @opindex --preserve-environment
15557 @cindex environment, preserving
15558 @flindex /etc/shells
15559 @cindex restricted shell
15560 Do not change the environment variables @env{HOME}, @env{USER},
15561 @env{LOGNAME}, or @env{SHELL}.  Run the shell given in the environment
15562 variable @env{SHELL} instead of the shell from @var{user}'s passwd
15563 entry, unless the user running @command{su} is not the super-user and
15564 @var{user}'s shell is restricted.  A @dfn{restricted shell} is one that
15565 is not listed in the file @file{/etc/shells}, or in a compiled-in list
15566 if that file does not exist.  Parts of what this option does can be
15567 overridden by @option{--login} and @option{--shell}.
15568
15569 @item -s @var{shell}
15570 @itemx --shell=@var{shell}
15571 @opindex -s
15572 @opindex --shell
15573 Run @var{shell} instead of the shell from @var{user}'s passwd entry,
15574 unless the user running @command{su} is not the super-user and @var{user}'s
15575 shell is restricted (see @option{-m} just above).
15576
15577 @end table
15578
15579 @cindex exit status of @command{su}
15580 Exit status:
15581
15582 @display
15583 125 if @command{su} itself fails
15584 126 if subshell is found but cannot be invoked
15585 127 if subshell cannot be found
15586 the exit status of the subshell otherwise
15587 @end display
15588
15589 @cindex wheel group, not supported
15590 @cindex group wheel, not supported
15591 @cindex fascism
15592 @subsection Why GNU @command{su} does not support the @samp{wheel} group
15593
15594 (This section is by Richard Stallman.)
15595
15596 @cindex Twenex
15597 @cindex MIT AI lab
15598 Sometimes a few of the users try to hold total power over all the
15599 rest.  For example, in 1984, a few users at the MIT AI lab decided to
15600 seize power by changing the operator password on the Twenex system and
15601 keeping it secret from everyone else.  (I was able to thwart this coup
15602 and give power back to the users by patching the kernel, but I
15603 wouldn't know how to do that in Unix.)
15604
15605 However, occasionally the rulers do tell someone.  Under the usual
15606 @command{su} mechanism, once someone learns the root password who
15607 sympathizes with the ordinary users, he or she can tell the rest.  The
15608 ``wheel group'' feature would make this impossible, and thus cement the
15609 power of the rulers.
15610
15611 I'm on the side of the masses, not that of the rulers.  If you are
15612 used to supporting the bosses and sysadmins in whatever they do, you
15613 might find this idea strange at first.
15614
15615
15616 @node timeout invocation
15617 @section @command{timeout}: Run a command with a time limit
15618
15619 @pindex timeout
15620 @cindex time limit
15621 @cindex run commands with bounded time
15622
15623 @command{timeout} runs the given @var{command} and kills it if it is
15624 still running after the specified time interval.  Synopsis:
15625
15626 @example
15627 timeout [@var{option}] @var{duration} @var{command} [@var{arg}]@dots{}
15628 @end example
15629
15630 @var{command} must not be a special built-in utility (@pxref{Special
15631 built-in utilities}).
15632
15633 The program accepts the following options.  Also see @ref{Common options}.
15634 Options must precede operands.
15635
15636 @table @samp
15637 @item -k @var{duration}
15638 @itemx --kill-after=@var{duration}
15639 @opindex -k
15640 @opindex --kill-after
15641 Ensure the monitored @var{command} is killed by also sending a @samp{KILL}
15642 signal, after the specified @var{duration}.  Without this option, if the
15643 selected signal proves not to be fatal, @command{timeout} does not kill
15644 the @var{command}.
15645
15646 @item -s @var{signal}
15647 @itemx --signal=@var{signal}
15648 @opindex -s
15649 @opindex --signal
15650 Send this @var{signal} to @var{command} on timeout, rather than the
15651 default @samp{TERM} signal. @var{signal} may be a name like @samp{HUP}
15652 or a number. Also see @xref{Signal specifications}.
15653 @end table
15654
15655 @cindex time units
15656 @var{duration} is an integer followed by an optional unit:
15657 @display
15658 @samp{s} for seconds (the default)
15659 @samp{m} for minutes
15660 @samp{h} for hours
15661 @samp{d} for days
15662 @end display
15663 A duration of 0 disables the associated timeout.
15664
15665 @cindex exit status of @command{timeout}
15666 Exit status:
15667
15668 @display
15669 124 if @var{command} times out
15670 125 if @command{timeout} itself fails
15671 126 if @var{command} is found but cannot be invoked
15672 127 if @var{command} cannot be found
15673 the exit status of @var{command} otherwise
15674 @end display
15675
15676
15677 @node Process control
15678 @chapter Process control
15679
15680 @cindex processes, commands for controlling
15681 @cindex commands for controlling processes
15682
15683 @menu
15684 * kill invocation::             Sending a signal to processes.
15685 @end menu
15686
15687
15688 @node kill invocation
15689 @section @command{kill}: Send a signal to processes
15690
15691 @pindex kill
15692 @cindex send a signal to processes
15693
15694 The @command{kill} command sends a signal to processes, causing them
15695 to terminate or otherwise act upon receiving the signal in some way.
15696 Alternatively, it lists information about signals.  Synopses:
15697
15698 @example
15699 kill [-s @var{signal} | --signal @var{signal} | -@var{signal}] @var{pid}@dots{}
15700 kill [-l | --list | -t | --table] [@var{signal}]@dots{}
15701 @end example
15702
15703 @mayConflictWithShellBuiltIn{kill}
15704
15705 The first form of the @command{kill} command sends a signal to all
15706 @var{pid} arguments.  The default signal to send if none is specified
15707 is @samp{TERM}.  The special signal number @samp{0} does not denote a
15708 valid signal, but can be used to test whether the @var{pid} arguments
15709 specify processes to which a signal could be sent.
15710
15711 If @var{pid} is positive, the signal is sent to the process with the
15712 process ID @var{pid}.  If @var{pid} is zero, the signal is sent to all
15713 processes in the process group of the current process.  If @var{pid}
15714 is @minus{}1, the signal is sent to all processes for which the user has
15715 permission to send a signal.  If @var{pid} is less than @minus{}1, the signal
15716 is sent to all processes in the process group that equals the absolute
15717 value of @var{pid}.
15718
15719 If @var{pid} is not positive, a system-dependent set of system
15720 processes is excluded from the list of processes to which the signal
15721 is sent.
15722
15723 If a negative @var{pid} argument is desired as the first one, it
15724 should be preceded by @option{--}.  However, as a common extension to
15725 @acronym{POSIX}, @option{--} is not required with @samp{kill
15726 -@var{signal} -@var{pid}}.  The following commands are equivalent:
15727
15728 @example
15729 kill -15 -1
15730 kill -TERM -1
15731 kill -s TERM -- -1
15732 kill -- -1
15733 @end example
15734
15735 The first form of the @command{kill} command succeeds if every @var{pid}
15736 argument specifies at least one process that the signal was sent to.
15737
15738 The second form of the @command{kill} command lists signal information.
15739 Either the @option{-l} or @option{--list} option, or the @option{-t}
15740 or @option{--table} option must be specified.  Without any
15741 @var{signal} argument, all supported signals are listed.  The output
15742 of @option{-l} or @option{--list} is a list of the signal names, one
15743 per line; if @var{signal} is already a name, the signal number is
15744 printed instead.  The output of @option{-t} or @option{--table} is a
15745 table of signal numbers, names, and descriptions.  This form of the
15746 @command{kill} command succeeds if all @var{signal} arguments are valid
15747 and if there is no output error.
15748
15749 The @command{kill} command also supports the @option{--help} and
15750 @option{--version} options.  @xref{Common options}.
15751
15752 A @var{signal} may be a signal name like @samp{HUP}, or a signal
15753 number like @samp{1}, or an exit status of a process terminated by the
15754 signal.  A signal name can be given in canonical form or prefixed by
15755 @samp{SIG}.  The case of the letters is ignored, except for the
15756 @option{-@var{signal}} option which must use upper case to avoid
15757 ambiguity with lower case option letters.  For a list of supported
15758 signal names and numbers see @xref{Signal specifications}.
15759
15760 @node Delaying
15761 @chapter Delaying
15762
15763 @cindex delaying commands
15764 @cindex commands for delaying
15765
15766 @c Perhaps @command{wait} or other commands should be described here also?
15767
15768 @menu
15769 * sleep invocation::            Delay for a specified time.
15770 @end menu
15771
15772
15773 @node sleep invocation
15774 @section @command{sleep}: Delay for a specified time
15775
15776 @pindex sleep
15777 @cindex delay for a specified time
15778
15779 @command{sleep} pauses for an amount of time specified by the sum of
15780 the values of the command line arguments.
15781 Synopsis:
15782
15783 @example
15784 sleep @var{number}[smhd]@dots{}
15785 @end example
15786
15787 @cindex time units
15788 Each argument is a number followed by an optional unit; the default
15789 is seconds.  The units are:
15790
15791 @table @samp
15792 @item s
15793 seconds
15794 @item m
15795 minutes
15796 @item h
15797 hours
15798 @item d
15799 days
15800 @end table
15801
15802 Historical implementations of @command{sleep} have required that
15803 @var{number} be an integer, and only accepted a single argument
15804 without a suffix.  However, GNU @command{sleep} accepts
15805 arbitrary floating point numbers.  @xref{Floating point}.
15806
15807 The only options are @option{--help} and @option{--version}.  @xref{Common
15808 options}.
15809
15810 @c sleep is a shell built-in at least with Solaris 11's /bin/sh
15811 @mayConflictWithShellBuiltIn{sleep}
15812
15813 @exitstatus
15814
15815
15816 @node Numeric operations
15817 @chapter Numeric operations
15818
15819 @cindex numeric operations
15820 These programs do numerically-related operations.
15821
15822 @menu
15823 * factor invocation::              Show factors of numbers.
15824 * seq invocation::                 Print sequences of numbers.
15825 @end menu
15826
15827
15828 @node factor invocation
15829 @section @command{factor}: Print prime factors
15830
15831 @pindex factor
15832 @cindex prime factors
15833
15834 @command{factor} prints prime factors.  Synopses:
15835
15836 @example
15837 factor [@var{number}]@dots{}
15838 factor @var{option}
15839 @end example
15840
15841 If no @var{number} is specified on the command line, @command{factor} reads
15842 numbers from standard input, delimited by newlines, tabs, or spaces.
15843
15844 The @command{factor} command supports only a small number of options:
15845
15846 @table @samp
15847 @item --help
15848 Print a short help on standard output, then exit without further
15849 processing.
15850
15851 @item --version
15852 Print the program version on standard output, then exit without further
15853 processing.
15854 @end table
15855
15856 Factoring the product of the eighth and ninth Mersenne primes
15857 takes about 30 milliseconds of CPU time on a 2.2 GHz Athlon.
15858
15859 @example
15860 M8=`echo 2^31-1|bc` ; M9=`echo 2^61-1|bc`
15861 /usr/bin/time -f '%U' factor $(echo "$M8 * $M9" | bc)
15862 4951760154835678088235319297: 2147483647 2305843009213693951
15863 0.03
15864 @end example
15865
15866 Similarly, factoring the eighth Fermat number @math{2^{256}+1} takes
15867 about 20 seconds on the same machine.
15868
15869 Factoring large numbers is, in general, hard.  The Pollard Rho
15870 algorithm used by @command{factor} is particularly effective for
15871 numbers with relatively small factors.  If you wish to factor large
15872 numbers which do not have small factors (for example, numbers which
15873 are the product of two large primes), other methods are far better.
15874
15875 If @command{factor} is built without using GNU MP, only
15876 single-precision arithmetic is available, and so large numbers
15877 (typically @math{2^{64}} and above) will not be supported.  The single-precision
15878 code uses an algorithm which is designed for factoring smaller
15879 numbers.
15880
15881 @exitstatus
15882
15883
15884 @node seq invocation
15885 @section @command{seq}: Print numeric sequences
15886
15887 @pindex seq
15888 @cindex numeric sequences
15889 @cindex sequence of numbers
15890
15891 @command{seq} prints a sequence of numbers to standard output.  Synopses:
15892
15893 @example
15894 seq [@var{option}]@dots{} @var{last}
15895 seq [@var{option}]@dots{} @var{first} @var{last}
15896 seq [@var{option}]@dots{} @var{first} @var{increment} @var{last}
15897 @end example
15898
15899 @command{seq} prints the numbers from @var{first} to @var{last} by
15900 @var{increment}.  By default, each number is printed on a separate line.
15901 When @var{increment} is not specified, it defaults to @samp{1},
15902 even when @var{first} is larger than @var{last}.
15903 @var{first} also defaults to @samp{1}.  So @code{seq 1} prints
15904 @samp{1}, but @code{seq 0} and @code{seq 10 5} produce no output.
15905 Floating-point numbers may be specified.  @xref{Floating point}.
15906
15907 The program accepts the following options.  Also see @ref{Common options}.
15908 Options must precede operands.
15909
15910 @table @samp
15911 @item -f @var{format}
15912 @itemx --format=@var{format}
15913 @opindex -f @var{format}
15914 @opindex --format=@var{format}
15915 @cindex formatting of numbers in @command{seq}
15916 Print all numbers using @var{format}.
15917 @var{format} must contain exactly one of the @samp{printf}-style
15918 floating point conversion specifications @samp{%a}, @samp{%e},
15919 @samp{%f}, @samp{%g}, @samp{%A}, @samp{%E}, @samp{%F}, @samp{%G}.
15920 The @samp{%} may be followed by zero or more flags taken from the set
15921 @samp{-+#0 '}, then an optional width containing one or more digits,
15922 then an optional precision consisting of a @samp{.} followed by zero
15923 or more digits.  @var{format} may also contain any number of @samp{%%}
15924 conversion specifications.  All conversion specifications have the
15925 same meaning as with @samp{printf}.
15926
15927 The default format is derived from @var{first}, @var{step}, and
15928 @var{last}.  If these all use a fixed point decimal representation,
15929 the default format is @samp{%.@var{p}f}, where @var{p} is the minimum
15930 precision that can represent the output numbers exactly.  Otherwise,
15931 the default format is @samp{%g}.
15932
15933 @item -s @var{string}
15934 @itemx --separator=@var{string}
15935 @cindex separator for numbers in @command{seq}
15936 Separate numbers with @var{string}; default is a newline.
15937 The output always terminates with a newline.
15938
15939 @item -w
15940 @itemx --equal-width
15941 Print all numbers with the same width, by padding with leading zeros.
15942 @var{first}, @var{step}, and @var{last} should all use a fixed point
15943 decimal representation.
15944 (To have other kinds of padding, use @option{--format}).
15945
15946 @end table
15947
15948 You can get finer-grained control over output with @option{-f}:
15949
15950 @example
15951 $ seq -f '(%9.2E)' -9e5 1.1e6 1.3e6
15952 (-9.00E+05)
15953 ( 2.00E+05)
15954 ( 1.30E+06)
15955 @end example
15956
15957 If you want hexadecimal integer output, you can use @command{printf}
15958 to perform the conversion:
15959
15960 @example
15961 $ printf '%x\n' `seq 1048575 1024 1050623`
15962 fffff
15963 1003ff
15964 1007ff
15965 @end example
15966
15967 For very long lists of numbers, use xargs to avoid
15968 system limitations on the length of an argument list:
15969
15970 @example
15971 $ seq 1000000 | xargs printf '%x\n' | tail -n 3
15972 f423e
15973 f423f
15974 f4240
15975 @end example
15976
15977 To generate octal output, use the printf @code{%o} format instead
15978 of @code{%x}.
15979
15980 On most systems, seq can produce whole-number output for values up to
15981 at least @math{2^{53}}.  Larger integers are approximated.  The details
15982 differ depending on your floating-point implementation.
15983 @xref{Floating point}.  A common
15984 case is that @command{seq} works with integers through @math{2^{64}},
15985 and larger integers may not be numerically correct:
15986
15987 @example
15988 $ seq 18446744073709551616 1 18446744073709551618
15989 18446744073709551616
15990 18446744073709551616
15991 18446744073709551618
15992 @end example
15993
15994 Be careful when using @command{seq} with outlandish values: otherwise
15995 you may see surprising results, as @command{seq} uses floating point
15996 internally.  For example, on the x86 platform, where the internal
15997 representation uses a 64-bit fraction, the command:
15998
15999 @example
16000 seq 1 0.0000000000000000001 1.0000000000000000009
16001 @end example
16002
16003 outputs 1.0000000000000000007 twice and skips 1.0000000000000000008.
16004
16005 @exitstatus
16006
16007
16008 @node File permissions
16009 @chapter File permissions
16010 @include perm.texi
16011
16012 @include parse-datetime.texi
16013
16014 @c              What's GNU?
16015 @c              Arnold Robbins
16016 @node Opening the software toolbox
16017 @chapter Opening the Software Toolbox
16018
16019 An earlier version of this chapter appeared in
16020 @uref{http://www.linuxjournal.com/article.php?sid=2762, the
16021 @cite{What's GNU?} column of @cite{Linux Journal}, 2 (June, 1994)}.
16022 It was written by Arnold Robbins.
16023
16024 @menu
16025 * Toolbox introduction::        Toolbox introduction
16026 * I/O redirection::             I/O redirection
16027 * The who command::             The @command{who} command
16028 * The cut command::             The @command{cut} command
16029 * The sort command::            The @command{sort} command
16030 * The uniq command::            The @command{uniq} command
16031 * Putting the tools together::  Putting the tools together
16032 @end menu
16033
16034
16035 @node Toolbox introduction
16036 @unnumberedsec Toolbox Introduction
16037
16038 This month's column is only peripherally related to the GNU Project, in
16039 that it describes a number of the GNU tools on your GNU/Linux system
16040 and how they
16041 might be used.  What it's really about is the ``Software Tools'' philosophy
16042 of program development and usage.
16043
16044 The software tools philosophy was an important and integral concept
16045 in the initial design and development of Unix (of which Linux and GNU are
16046 essentially clones).  Unfortunately, in the modern day press of
16047 Internetworking and flashy GUIs, it seems to have fallen by the
16048 wayside.  This is a shame, since it provides a powerful mental model
16049 for solving many kinds of problems.
16050
16051 Many people carry a Swiss Army knife around in their pants pockets (or
16052 purse).  A Swiss Army knife is a handy tool to have: it has several knife
16053 blades, a screwdriver, tweezers, toothpick, nail file, corkscrew, and perhaps
16054 a number of other things on it.  For the everyday, small miscellaneous jobs
16055 where you need a simple, general purpose tool, it's just the thing.
16056
16057 On the other hand, an experienced carpenter doesn't build a house using
16058 a Swiss Army knife.  Instead, he has a toolbox chock full of specialized
16059 tools---a saw, a hammer, a screwdriver, a plane, and so on.  And he knows
16060 exactly when and where to use each tool; you won't catch him hammering nails
16061 with the handle of his screwdriver.
16062
16063 The Unix developers at Bell Labs were all professional programmers and trained
16064 computer scientists.  They had found that while a one-size-fits-all program
16065 might appeal to a user because there's only one program to use, in practice
16066 such programs are
16067
16068 @enumerate a
16069 @item
16070 difficult to write,
16071
16072 @item
16073 difficult to maintain and
16074 debug, and
16075
16076 @item
16077 difficult to extend to meet new situations.
16078 @end enumerate
16079
16080 Instead, they felt that programs should be specialized tools.  In short, each
16081 program ``should do one thing well.''  No more and no less.  Such programs are
16082 simpler to design, write, and get right---they only do one thing.
16083
16084 Furthermore, they found that with the right machinery for hooking programs
16085 together, that the whole was greater than the sum of the parts.  By combining
16086 several special purpose programs, you could accomplish a specific task
16087 that none of the programs was designed for, and accomplish it much more
16088 quickly and easily than if you had to write a special purpose program.
16089 We will see some (classic) examples of this further on in the column.
16090 (An important additional point was that, if necessary, take a detour
16091 and build any software tools you may need first, if you don't already
16092 have something appropriate in the toolbox.)
16093
16094 @node I/O redirection
16095 @unnumberedsec I/O Redirection
16096
16097 Hopefully, you are familiar with the basics of I/O redirection in the
16098 shell, in particular the concepts of ``standard input,'' ``standard output,''
16099 and ``standard error''.  Briefly, ``standard input'' is a data source, where
16100 data comes from.  A program should not need to either know or care if the
16101 data source is a disk file, a keyboard, a magnetic tape, or even a punched
16102 card reader.  Similarly, ``standard output'' is a data sink, where data goes
16103 to.  The program should neither know nor care where this might be.
16104 Programs that only read their standard input, do something to the data,
16105 and then send it on, are called @dfn{filters}, by analogy to filters in a
16106 water pipeline.
16107
16108 With the Unix shell, it's very easy to set up data pipelines:
16109
16110 @smallexample
16111 program_to_create_data | filter1 | ... | filterN > final.pretty.data
16112 @end smallexample
16113
16114 We start out by creating the raw data; each filter applies some successive
16115 transformation to the data, until by the time it comes out of the pipeline,
16116 it is in the desired form.
16117
16118 This is fine and good for standard input and standard output.  Where does the
16119 standard error come in to play?  Well, think about @command{filter1} in
16120 the pipeline above.  What happens if it encounters an error in the data it
16121 sees?  If it writes an error message to standard output, it will just
16122 disappear down the pipeline into @command{filter2}'s input, and the
16123 user will probably never see it.  So programs need a place where they can send
16124 error messages so that the user will notice them.  This is standard error,
16125 and it is usually connected to your console or window, even if you have
16126 redirected standard output of your program away from your screen.
16127
16128 For filter programs to work together, the format of the data has to be
16129 agreed upon.  The most straightforward and easiest format to use is simply
16130 lines of text.  Unix data files are generally just streams of bytes, with
16131 lines delimited by the @acronym{ASCII} @sc{lf} (Line Feed) character,
16132 conventionally called a ``newline'' in the Unix literature.  (This is
16133 @code{'\n'} if you're a C programmer.)  This is the format used by all
16134 the traditional filtering programs.  (Many earlier operating systems
16135 had elaborate facilities and special purpose programs for managing
16136 binary data.  Unix has always shied away from such things, under the
16137 philosophy that it's easiest to simply be able to view and edit your
16138 data with a text editor.)
16139
16140 OK, enough introduction.  Let's take a look at some of the tools, and then
16141 we'll see how to hook them together in interesting ways.   In the following
16142 discussion, we will only present those command line options that interest
16143 us.  As you should always do, double check your system documentation
16144 for the full story.
16145
16146 @node The who command
16147 @unnumberedsec The @command{who} Command
16148
16149 The first program is the @command{who} command.  By itself, it generates a
16150 list of the users who are currently logged in.  Although I'm writing
16151 this on a single-user system, we'll pretend that several people are
16152 logged in:
16153
16154 @example
16155 $ who
16156 @print{} arnold   console Jan 22 19:57
16157 @print{} miriam   ttyp0   Jan 23 14:19(:0.0)
16158 @print{} bill     ttyp1   Jan 21 09:32(:0.0)
16159 @print{} arnold   ttyp2   Jan 23 20:48(:0.0)
16160 @end example
16161
16162 Here, the @samp{$} is the usual shell prompt, at which I typed @samp{who}.
16163 There are three people logged in, and I am logged in twice.  On traditional
16164 Unix systems, user names are never more than eight characters long.  This
16165 little bit of trivia will be useful later.  The output of @command{who} is nice,
16166 but the data is not all that exciting.
16167
16168 @node The cut command
16169 @unnumberedsec The @command{cut} Command
16170
16171 The next program we'll look at is the @command{cut} command.  This program
16172 cuts out columns or fields of input data.  For example, we can tell it
16173 to print just the login name and full name from the @file{/etc/passwd}
16174 file.  The @file{/etc/passwd} file has seven fields, separated by
16175 colons:
16176
16177 @example
16178 arnold:xyzzy:2076:10:Arnold D. Robbins:/home/arnold:/bin/bash
16179 @end example
16180
16181 To get the first and fifth fields, we would use @command{cut} like this:
16182
16183 @example
16184 $ cut -d: -f1,5 /etc/passwd
16185 @print{} root:Operator
16186 @dots{}
16187 @print{} arnold:Arnold D. Robbins
16188 @print{} miriam:Miriam A. Robbins
16189 @dots{}
16190 @end example
16191
16192 With the @option{-c} option, @command{cut} will cut out specific characters
16193 (i.e., columns) in the input lines.  This is useful for input data
16194 that has fixed width fields, and does not have a field separator.  For
16195 example, list the Monday dates for the current month:
16196
16197 @c Is using cal ok?  Looked at gcal, but I don't like it.
16198 @example
16199 $ cal | cut -c 3-5
16200 @print{}Mo
16201 @print{}
16202 @print{}  6
16203 @print{} 13
16204 @print{} 20
16205 @print{} 27
16206 @end example
16207
16208 @node The sort command
16209 @unnumberedsec The @command{sort} Command
16210
16211 Next we'll look at the @command{sort} command.  This is one of the most
16212 powerful commands on a Unix-style system; one that you will often find
16213 yourself using when setting up fancy data plumbing.
16214
16215 The @command{sort}
16216 command reads and sorts each file named on the command line.  It then
16217 merges the sorted data and writes it to standard output.  It will read
16218 standard input if no files are given on the command line (thus
16219 making it into a filter).  The sort is based on the character collating
16220 sequence or based on user-supplied ordering criteria.
16221
16222
16223 @node The uniq command
16224 @unnumberedsec The @command{uniq} Command
16225
16226 Finally (at least for now), we'll look at the @command{uniq} program.  When
16227 sorting data, you will often end up with duplicate lines, lines that
16228 are identical.  Usually, all you need is one instance of each line.
16229 This is where @command{uniq} comes in.  The @command{uniq} program reads its
16230 standard input.  It prints only one
16231 copy of each repeated line.  It does have several options.  Later on,
16232 we'll use the @option{-c} option, which prints each unique line, preceded
16233 by a count of the number of times that line occurred in the input.
16234
16235
16236 @node Putting the tools together
16237 @unnumberedsec Putting the Tools Together
16238
16239 Now, let's suppose this is a large ISP server system with dozens of users
16240 logged in.  The management wants the system administrator to write a
16241 program that will
16242 generate a sorted list of logged in users.  Furthermore, even if a user
16243 is logged in multiple times, his or her name should only show up in the
16244 output once.
16245
16246 The administrator could sit down with the system documentation and write a C
16247 program that did this.  It would take perhaps a couple of hundred lines
16248 of code and about two hours to write it, test it, and debug it.
16249 However, knowing the software toolbox, the administrator can instead start out
16250 by generating just a list of logged on users:
16251
16252 @example
16253 $ who | cut -c1-8
16254 @print{} arnold
16255 @print{} miriam
16256 @print{} bill
16257 @print{} arnold
16258 @end example
16259
16260 Next, sort the list:
16261
16262 @example
16263 $ who | cut -c1-8 | sort
16264 @print{} arnold
16265 @print{} arnold
16266 @print{} bill
16267 @print{} miriam
16268 @end example
16269
16270 Finally, run the sorted list through @command{uniq}, to weed out duplicates:
16271
16272 @example
16273 $ who | cut -c1-8 | sort | uniq
16274 @print{} arnold
16275 @print{} bill
16276 @print{} miriam
16277 @end example
16278
16279 The @command{sort} command actually has a @option{-u} option that does what
16280 @command{uniq} does.  However, @command{uniq} has other uses for which one
16281 cannot substitute @samp{sort -u}.
16282
16283 The administrator puts this pipeline into a shell script, and makes it
16284 available for
16285 all the users on the system (@samp{#} is the system administrator,
16286 or @code{root}, prompt):
16287
16288 @example
16289 # cat > /usr/local/bin/listusers
16290 who | cut -c1-8 | sort | uniq
16291 ^D
16292 # chmod +x /usr/local/bin/listusers
16293 @end example
16294
16295 There are four major points to note here.  First, with just four
16296 programs, on one command line, the administrator was able to save about two
16297 hours worth of work.  Furthermore, the shell pipeline is just about as
16298 efficient as the C program would be, and it is much more efficient in
16299 terms of programmer time.  People time is much more expensive than
16300 computer time, and in our modern ``there's never enough time to do
16301 everything'' society, saving two hours of programmer time is no mean
16302 feat.
16303
16304 Second, it is also important to emphasize that with the
16305 @emph{combination} of the tools, it is possible to do a special
16306 purpose job never imagined by the authors of the individual programs.
16307
16308 Third, it is also valuable to build up your pipeline in stages, as we did here.
16309 This allows you to view the data at each stage in the pipeline, which helps
16310 you acquire the confidence that you are indeed using these tools correctly.
16311
16312 Finally, by bundling the pipeline in a shell script, other users can use
16313 your command, without having to remember the fancy plumbing you set up for
16314 them.  In terms of how you run them, shell scripts and compiled programs are
16315 indistinguishable.
16316
16317 After the previous warm-up exercise, we'll look at two additional, more
16318 complicated pipelines.  For them, we need to introduce two more tools.
16319
16320 The first is the @command{tr} command, which stands for ``transliterate.''
16321 The @command{tr} command works on a character-by-character basis, changing
16322 characters.  Normally it is used for things like mapping upper case to
16323 lower case:
16324
16325 @example
16326 $ echo ThIs ExAmPlE HaS MIXED case! | tr '[:upper:]' '[:lower:]'
16327 @print{} this example has mixed case!
16328 @end example
16329
16330 There are several options of interest:
16331
16332 @table @code
16333 @item -c
16334 work on the complement of the listed characters, i.e.,
16335 operations apply to characters not in the given set
16336
16337 @item -d
16338 delete characters in the first set from the output
16339
16340 @item -s
16341 squeeze repeated characters in the output into just one character.
16342 @end table
16343
16344 We will be using all three options in a moment.
16345
16346 The other command we'll look at is @command{comm}.  The @command{comm}
16347 command takes two sorted input files as input data, and prints out the
16348 files' lines in three columns.  The output columns are the data lines
16349 unique to the first file, the data lines unique to the second file, and
16350 the data lines that are common to both.  The @option{-1}, @option{-2}, and
16351 @option{-3} command line options @emph{omit} the respective columns.  (This is
16352 non-intuitive and takes a little getting used to.)  For example:
16353
16354 @example
16355 $ cat f1
16356 @print{} 11111
16357 @print{} 22222
16358 @print{} 33333
16359 @print{} 44444
16360 $ cat f2
16361 @print{} 00000
16362 @print{} 22222
16363 @print{} 33333
16364 @print{} 55555
16365 $ comm f1 f2
16366 @print{}         00000
16367 @print{} 11111
16368 @print{}                 22222
16369 @print{}                 33333
16370 @print{} 44444
16371 @print{}         55555
16372 @end example
16373
16374 The file name @file{-} tells @command{comm} to read standard input
16375 instead of a regular file.
16376
16377 Now we're ready to build a fancy pipeline.  The first application is a word
16378 frequency counter.  This helps an author determine if he or she is over-using
16379 certain words.
16380
16381 The first step is to change the case of all the letters in our input file
16382 to one case.  ``The'' and ``the'' are the same word when doing counting.
16383
16384 @example
16385 $ tr '[:upper:]' '[:lower:]' < whats.gnu | ...
16386 @end example
16387
16388 The next step is to get rid of punctuation.  Quoted words and unquoted words
16389 should be treated identically; it's easiest to just get the punctuation out of
16390 the way.
16391
16392 @smallexample
16393 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' | ...
16394 @end smallexample
16395
16396 The second @command{tr} command operates on the complement of the listed
16397 characters, which are all the letters, the digits, the underscore, and
16398 the blank.  The @samp{\n} represents the newline character; it has to
16399 be left alone.  (The @acronym{ASCII} tab character should also be included for
16400 good measure in a production script.)
16401
16402 At this point, we have data consisting of words separated by blank space.
16403 The words only contain alphanumeric characters (and the underscore).  The
16404 next step is break the data apart so that we have one word per line.  This
16405 makes the counting operation much easier, as we will see shortly.
16406
16407 @smallexample
16408 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
16409 > tr -s ' ' '\n' | ...
16410 @end smallexample
16411
16412 This command turns blanks into newlines.  The @option{-s} option squeezes
16413 multiple newline characters in the output into just one.  This helps us
16414 avoid blank lines.  (The @samp{>} is the shell's ``secondary prompt.''
16415 This is what the shell prints when it notices you haven't finished
16416 typing in all of a command.)
16417
16418 We now have data consisting of one word per line, no punctuation, all one
16419 case.  We're ready to count each word:
16420
16421 @smallexample
16422 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
16423 > tr -s ' ' '\n' | sort | uniq -c | ...
16424 @end smallexample
16425
16426 At this point, the data might look something like this:
16427
16428 @example
16429      60 a
16430       2 able
16431       6 about
16432       1 above
16433       2 accomplish
16434       1 acquire
16435       1 actually
16436       2 additional
16437 @end example
16438
16439 The output is sorted by word, not by count!  What we want is the most
16440 frequently used words first.  Fortunately, this is easy to accomplish,
16441 with the help of two more @command{sort} options:
16442
16443 @table @code
16444 @item -n
16445 do a numeric sort, not a textual one
16446
16447 @item -r
16448 reverse the order of the sort
16449 @end table
16450
16451 The final pipeline looks like this:
16452
16453 @smallexample
16454 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
16455 > tr -s ' ' '\n' | sort | uniq -c | sort -n -r
16456 @print{}    156 the
16457 @print{}     60 a
16458 @print{}     58 to
16459 @print{}     51 of
16460 @print{}     51 and
16461 @dots{}
16462 @end smallexample
16463
16464 Whew!  That's a lot to digest.  Yet, the same principles apply.  With six
16465 commands, on two lines (really one long one split for convenience), we've
16466 created a program that does something interesting and useful, in much
16467 less time than we could have written a C program to do the same thing.
16468
16469 A minor modification to the above pipeline can give us a simple spelling
16470 checker!  To determine if you've spelled a word correctly, all you have to
16471 do is look it up in a dictionary.  If it is not there, then chances are
16472 that your spelling is incorrect.  So, we need a dictionary.
16473 The conventional location for a dictionary is @file{/usr/dict/words}.
16474 On my GNU/Linux system,@footnote{Redhat Linux 6.1, for the November 2000
16475 revision of this article.}
16476 this is a sorted, 45,402 word dictionary.
16477
16478 Now, how to compare our file with the dictionary?  As before, we generate
16479 a sorted list of words, one per line:
16480
16481 @smallexample
16482 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
16483 > tr -s ' ' '\n' | sort -u | ...
16484 @end smallexample
16485
16486 Now, all we need is a list of words that are @emph{not} in the
16487 dictionary.  Here is where the @command{comm} command comes in.
16488
16489 @smallexample
16490 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
16491 > tr -s ' ' '\n' | sort -u |
16492 > comm -23 - /usr/dict/words
16493 @end smallexample
16494
16495 The @option{-2} and @option{-3} options eliminate lines that are only in the
16496 dictionary (the second file), and lines that are in both files.  Lines
16497 only in the first file (standard input, our stream of words), are
16498 words that are not in the dictionary.  These are likely candidates for
16499 spelling errors.  This pipeline was the first cut at a production
16500 spelling checker on Unix.
16501
16502 There are some other tools that deserve brief mention.
16503
16504 @table @command
16505 @item grep
16506 search files for text that matches a regular expression
16507
16508 @item wc
16509 count lines, words, characters
16510
16511 @item tee
16512 a T-fitting for data pipes, copies data to files and to standard output
16513
16514 @item sed
16515 the stream editor, an advanced tool
16516
16517 @item awk
16518 a data manipulation language, another advanced tool
16519 @end table
16520
16521 The software tools philosophy also espoused the following bit of
16522 advice: ``Let someone else do the hard part.''  This means, take
16523 something that gives you most of what you need, and then massage it the
16524 rest of the way until it's in the form that you want.
16525
16526 To summarize:
16527
16528 @enumerate 1
16529 @item
16530 Each program should do one thing well.  No more, no less.
16531
16532 @item
16533 Combining programs with appropriate plumbing leads to results where
16534 the whole is greater than the sum of the parts.  It also leads to novel
16535 uses of programs that the authors might never have imagined.
16536
16537 @item
16538 Programs should never print extraneous header or trailer data, since these
16539 could get sent on down a pipeline.  (A point we didn't mention earlier.)
16540
16541 @item
16542 Let someone else do the hard part.
16543
16544 @item
16545 Know your toolbox!  Use each program appropriately.  If you don't have an
16546 appropriate tool, build one.
16547 @end enumerate
16548
16549 As of this writing, all the programs we've discussed are available via
16550 anonymous @command{ftp} from: @*
16551 @uref{ftp://gnudist.gnu.org/textutils/textutils-1.22.tar.gz}.  (There may
16552 be more recent versions available now.)
16553
16554 None of what I have presented in this column is new.  The Software Tools
16555 philosophy was first introduced in the book @cite{Software Tools}, by
16556 Brian Kernighan and P.J. Plauger (Addison-Wesley, ISBN 0-201-03669-X).
16557 This book showed how to write and use software tools.  It was written in
16558 1976, using a preprocessor for FORTRAN named @command{ratfor} (RATional
16559 FORtran).  At the time, C was not as ubiquitous as it is now; FORTRAN
16560 was.  The last chapter presented a @command{ratfor} to FORTRAN
16561 processor, written in @command{ratfor}.  @command{ratfor} looks an awful
16562 lot like C; if you know C, you won't have any problem following the
16563 code.
16564
16565 In 1981, the book was updated and made available as @cite{Software Tools
16566 in Pascal} (Addison-Wesley, ISBN 0-201-10342-7).  Both books are
16567 still in print and are well worth
16568 reading if you're a programmer.  They certainly made a major change in
16569 how I view programming.
16570
16571 The programs in both books are available from
16572 @uref{http://cm.bell-labs.com/who/bwk, Brian Kernighan's home page}.
16573 For a number of years, there was an active
16574 Software Tools Users Group, whose members had ported the original
16575 @command{ratfor} programs to essentially every computer system with a
16576 FORTRAN compiler.  The popularity of the group waned in the middle 1980s
16577 as Unix began to spread beyond universities.
16578
16579 With the current proliferation of GNU code and other clones of Unix programs,
16580 these programs now receive little attention; modern C versions are
16581 much more efficient and do more than these programs do.  Nevertheless, as
16582 exposition of good programming style, and evangelism for a still-valuable
16583 philosophy, these books are unparalleled, and I recommend them highly.
16584
16585 Acknowledgment: I would like to express my gratitude to Brian Kernighan
16586 of Bell Labs, the original Software Toolsmith, for reviewing this column.
16587
16588 @node GNU Free Documentation License
16589 @appendix GNU Free Documentation License
16590
16591 @include fdl.texi
16592
16593 @node Concept index
16594 @unnumbered Index
16595
16596 @printindex cp
16597
16598 @bye
16599
16600 @c Local variables:
16601 @c texinfo-column-for-description: 32
16602 @c End: