3 @setfilename coreutils.info
4 @settitle @sc{gnu} Coreutils
9 @include constants.texi
11 @c Define new indices.
15 @c Put everything in one index (arbitrarily chosen to be the concept index).
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.
31 @dircategory Individual utilities
33 * basename: (coreutils)basename invocation. Strip directory and suffix.
34 * cat: (coreutils)cat invocation. Concatenate and write files.
35 * chgrp: (coreutils)chgrp invocation. Change file groups.
36 * chmod: (coreutils)chmod invocation. Change file permissions.
37 * chown: (coreutils)chown invocation. Change file owners/groups.
38 * chroot: (coreutils)chroot invocation. Specify the root directory.
39 * cksum: (coreutils)cksum invocation. Print POSIX CRC checksum.
40 * comm: (coreutils)comm invocation. Compare sorted files by line.
41 * cp: (coreutils)cp invocation. Copy files.
42 * csplit: (coreutils)csplit invocation. Split by context.
43 * cut: (coreutils)cut invocation. Print selected parts of lines.
44 * date: (coreutils)date invocation. Print/set system date and time.
45 * dd: (coreutils)dd invocation. Copy and convert a file.
46 * df: (coreutils)df invocation. Report filesystem disk usage.
47 * dir: (coreutils)dir invocation. List directories briefly.
48 * dircolors: (coreutils)dircolors invocation. Color setup for ls.
49 * dirname: (coreutils)dirname invocation. Strip non-directory suffix.
50 * du: (coreutils)du invocation. Report on disk usage.
51 * echo: (coreutils)echo invocation. Print a line of text.
52 * env: (coreutils)env invocation. Modify the environment.
53 * expand: (coreutils)expand invocation. Convert tabs to spaces.
54 * expr: (coreutils)expr invocation. Evaluate expressions.
55 * factor: (coreutils)factor invocation. Print prime factors
56 * false: (coreutils)false invocation. Do nothing, unsuccessfully.
57 * fmt: (coreutils)fmt invocation. Reformat paragraph text.
58 * fold: (coreutils)fold invocation. Wrap long input lines.
59 * groups: (coreutils)groups invocation. Print group names a user is in.
60 * head: (coreutils)head invocation. Output the first part of files.
61 * hostid: (coreutils)hostid invocation. Print numeric host identifier.
62 * hostname: (coreutils)hostname invocation. Print or set system name.
63 * id: (coreutils)id invocation. Print real/effective uid/gid.
64 * install: (coreutils)install invocation. Copy and change attributes.
65 * join: (coreutils)join invocation. Join lines on a common field.
66 * kill: (coreutils)kill invocation. Send a signal to processes.
67 * link: (coreutils)link invocation. Make hard links between files.
68 * ln: (coreutils)ln invocation. Make links between files.
69 * logname: (coreutils)logname invocation. Print current login name.
70 * ls: (coreutils)ls invocation. List directory contents.
71 * md5sum: (coreutils)md5sum invocation. Print or check message-digests.
72 * mkdir: (coreutils)mkdir invocation. Create directories.
73 * mkfifo: (coreutils)mkfifo invocation. Create FIFOs (named pipes).
74 * mknod: (coreutils)mknod invocation. Create special files.
75 * mv: (coreutils)mv invocation. Rename files.
76 * nice: (coreutils)nice invocation. Modify scheduling priority.
77 * nl: (coreutils)nl invocation. Number lines and write files.
78 * nohup: (coreutils)nohup invocation. Immunize to hangups.
79 * od: (coreutils)od invocation. Dump files in octal, etc.
80 * paste: (coreutils)paste invocation. Merge lines of files.
81 * pathchk: (coreutils)pathchk invocation. Check file name portability.
82 * pr: (coreutils)pr invocation. Paginate or columnate files.
83 * printenv: (coreutils)printenv invocation. Print environment variables.
84 * printf: (coreutils)printf invocation. Format and print data.
85 * ptx: (coreutils)ptx invocation. Produce permuted indexes.
86 * pwd: (coreutils)pwd invocation. Print working directory.
87 * readlink: (coreutils)readlink invocation. Print referent of a symlink.
88 * rm: (coreutils)rm invocation. Remove files.
89 * rmdir: (coreutils)rmdir invocation. Remove empty directories.
90 * seq: (coreutils)seq invocation. Print numeric sequences
91 * shred: (coreutils)shred invocation. Remove files more securely.
92 * sleep: (coreutils)sleep invocation. Delay for a specified time.
93 * sort: (coreutils)sort invocation. Sort text files.
94 * split: (coreutils)split invocation. Split into fixed-size pieces.
95 * stat: (coreutils)stat invocation. Report file(system) status.
96 * stty: (coreutils)stty invocation. Print/change terminal settings.
97 * su: (coreutils)su invocation. Modify user and group id.
98 * sum: (coreutils)sum invocation. Print traditional checksum.
99 * sync: (coreutils)sync invocation. Synchronize memory and disk.
100 * tac: (coreutils)tac invocation. Reverse files.
101 * tail: (coreutils)tail invocation. Output the last part of files.
102 * tee: (coreutils)tee invocation. Redirect to multiple files.
103 * test: (coreutils)test invocation. File/string tests.
104 * touch: (coreutils)touch invocation. Change file timestamps.
105 * tr: (coreutils)tr invocation. Translate characters.
106 * true: (coreutils)true invocation. Do nothing, successfully.
107 * tsort: (coreutils)tsort invocation. Topological sort.
108 * tty: (coreutils)tty invocation. Print terminal name.
109 * uname: (coreutils)uname invocation. Print system information.
110 * unexpand: (coreutils)unexpand invocation. Convert spaces to tabs.
111 * uniq: (coreutils)uniq invocation. Uniquify files.
112 * unlink: (coreutils)unlink invocation. Removal via unlink(2).
113 * users: (coreutils)users invocation. Print current user names.
114 * vdir: (coreutils)vdir invocation. List directories verbosely.
115 * wc: (coreutils)wc invocation. Line, word, and byte counts.
116 * who: (coreutils)who invocation. Print who is logged in.
117 * whoami: (coreutils)whoami invocation. Print effective user id.
118 * yes: (coreutils)yes invocation. Print a string indefinitely.
122 This manual documents version @value{VERSION} of the @sc{gnu} core
123 utilities, including the standard programs for text and file manipulation.
125 Copyright @copyright{} 1994, 1995, 1996, 2000, 2001, 2002, 2003, 2004
126 Free Software Foundation, Inc.
129 Permission is granted to copy, distribute and/or modify this document
130 under the terms of the GNU Free Documentation License, Version 1.1 or
131 any later version published by the Free Software Foundation; with no
132 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
133 Texts. A copy of the license is included in the section entitled ``GNU
134 Free Documentation License''.
139 @title @sc{gnu} @code{Coreutils}
140 @subtitle Core GNU utilities
141 @subtitle for version @value{VERSION}, @value{UPDATED}
142 @author David MacKenzie et al.
145 @vskip 0pt plus 1filll
157 @cindex core utilities
158 @cindex text utilities
159 @cindex shell utilities
160 @cindex file utilities
163 * Introduction:: Caveats, overview, and authors.
164 * Common options:: Common options.
165 * Output of entire files:: cat tac nl od
166 * Formatting file contents:: fmt pr fold
167 * Output of parts of files:: head tail split csplit
168 * Summarizing files:: wc sum cksum md5sum
169 * Operating on sorted files:: sort uniq comm ptx tsort
170 * Operating on fields within a line:: cut paste join
171 * Operating on characters:: tr expand unexpand
172 * Directory listing:: ls dir vdir d v dircolors
173 * Basic operations:: cp dd install mv rm shred
174 * Special file types:: ln mkdir rmdir mkfifo mknod
175 * Changing file attributes:: chgrp chmod chown touch
176 * Disk usage:: df du stat sync
177 * Printing text:: echo printf yes
178 * Conditions:: false true test expr
180 * File name manipulation:: dirname basename pathchk
181 * Working context:: pwd stty printenv tty
182 * User information:: id logname whoami groups users who
183 * System context:: date uname hostname
184 * Modified command invocation:: chroot env nice nohup su
185 * Process control:: kill
187 * Numeric operations:: factor seq
188 * File permissions:: Access modes.
189 * Date input formats:: Specifying date strings.
190 * Opening the software toolbox:: The software tools philosophy.
191 * GNU Free Documentation License:: The license for this documentation.
192 * Index:: General index.
195 --- The Detailed Node Listing ---
199 * Exit status:: Indicating program success or failure.
200 * Backup options:: Backup options
201 * Block size:: Block size
202 * Target directory:: Target directory
203 * Trailing slashes:: Trailing slashes
204 * Traversing symlinks:: Traversing symlinks to directories
205 * Treating / specially:: Treating / specially
206 * Standards conformance:: Standards conformance
208 Output of entire files
210 * cat invocation:: Concatenate and write files.
211 * tac invocation:: Concatenate and write files in reverse.
212 * nl invocation:: Number lines and write files.
213 * od invocation:: Write files in octal or other formats.
215 Formatting file contents
217 * fmt invocation:: Reformat paragraph text.
218 * pr invocation:: Paginate or columnate files for printing.
219 * fold invocation:: Wrap input lines to fit in specified width.
221 Output of parts of files
223 * head invocation:: Output the first part of files.
224 * tail invocation:: Output the last part of files.
225 * split invocation:: Split a file into fixed-size pieces.
226 * csplit invocation:: Split a file into context-determined pieces.
230 * wc invocation:: Print newline, word, and byte counts.
231 * sum invocation:: Print checksum and block counts.
232 * cksum invocation:: Print CRC checksum and byte counts.
233 * md5sum invocation:: Print or check message-digests.
235 Operating on sorted files
237 * sort invocation:: Sort text files.
238 * uniq invocation:: Uniquify files.
239 * comm invocation:: Compare two sorted files line by line.
240 * ptx invocation:: Produce a permuted index of file contents.
241 * tsort invocation:: Topological sort.
243 @command{ptx}: Produce permuted indexes
245 * General options in ptx:: Options which affect general program behavior.
246 * Charset selection in ptx:: Underlying character set considerations.
247 * Input processing in ptx:: Input fields, contexts, and keyword selection.
248 * Output formatting in ptx:: Types of output format, and sizing the fields.
249 * Compatibility in ptx:: The GNU extensions to @command{ptx}
251 Operating on fields within a line
253 * cut invocation:: Print selected parts of lines.
254 * paste invocation:: Merge lines of files.
255 * join invocation:: Join lines on a common field.
257 Operating on characters
259 * tr invocation:: Translate, squeeze, and/or delete characters.
260 * expand invocation:: Convert tabs to spaces.
261 * unexpand invocation:: Convert spaces to tabs.
263 @command{tr}: Translate, squeeze, and/or delete characters
265 * Character sets:: Specifying sets of characters.
266 * Translating:: Changing one set of characters to another.
267 * Squeezing:: Squeezing repeats and deleting.
268 * Warnings in tr:: Warning messages.
272 * ls invocation:: List directory contents
273 * dir invocation:: Briefly list directory contents
274 * vdir invocation:: Verbosely list directory contents
275 * dircolors invocation:: Color setup for @command{ls}
277 @command{ls}: List directory contents
279 * Which files are listed:: Which files are listed
280 * What information is listed:: What information is listed
281 * Sorting the output:: Sorting the output
282 * More details about version sort:: More details about version sort
283 * General output formatting:: General output formatting
284 * Formatting the file names:: Formatting the file names
288 * cp invocation:: Copy files and directories
289 * dd invocation:: Convert and copy a file
290 * install invocation:: Copy files and set attributes
291 * mv invocation:: Move (rename) files
292 * rm invocation:: Remove files or directories
293 * shred invocation:: Remove files more securely
297 * link invocation:: Make a hard link via the link syscall
298 * ln invocation:: Make links between files
299 * mkdir invocation:: Make directories
300 * mkfifo invocation:: Make FIFOs (named pipes)
301 * mknod invocation:: Make block or character special files
302 * readlink invocation:: Print the referent of a symbolic link
303 * rmdir invocation:: Remove empty directories
304 * unlink invocation:: Remove files via unlink syscall
306 Changing file attributes
308 * chown invocation:: Change file owner and group
309 * chgrp invocation:: Change group ownership
310 * chmod invocation:: Change access permissions
311 * touch invocation:: Change file timestamps
315 * df invocation:: Report filesystem disk space usage
316 * du invocation:: Estimate file space usage
317 * stat invocation:: Report file or filesystem status
318 * sync invocation:: Synchronize data on disk with memory
322 * echo invocation:: Print a line of text
323 * printf invocation:: Format and print data
324 * yes invocation:: Print a string until interrupted
328 * false invocation:: Do nothing, unsuccessfully
329 * true invocation:: Do nothing, successfully
330 * test invocation:: Check file types and compare values
331 * expr invocation:: Evaluate expressions
333 @command{test}: Check file types and compare values
335 * File type tests:: File type tests
336 * Access permission tests:: Access permission tests
337 * File characteristic tests:: File characteristic tests
338 * String tests:: String tests
339 * Numeric tests:: Numeric tests
341 @command{expr}: Evaluate expression
343 * String expressions:: + : match substr index length
344 * Numeric expressions:: + - * / %
345 * Relations for expr:: | & < <= = == != >= >
346 * Examples of expr:: Examples of using @command{expr}
350 * tee invocation:: Redirect output to multiple files
352 File name manipulation
354 * basename invocation:: Strip directory and suffix from a file name
355 * dirname invocation:: Strip non-directory suffix from a file name
356 * pathchk invocation:: Check file name portability
360 * pwd invocation:: Print working directory
361 * stty invocation:: Print or change terminal characteristics
362 * printenv invocation:: Print all or some environment variables
363 * tty invocation:: Print file name of terminal on standard input
365 @command{stty}: Print or change terminal characteristics
367 * Control:: Control settings
368 * Input:: Input settings
369 * Output:: Output settings
370 * Local:: Local settings
371 * Combination:: Combination settings
372 * Characters:: Special characters
373 * Special:: Special settings
377 * id invocation:: Print real and effective uid and gid
378 * logname invocation:: Print current login name
379 * whoami invocation:: Print effective user id
380 * groups invocation:: Print group names a user is in
381 * users invocation:: Print login names of users currently logged in
382 * who invocation:: Print who is currently logged in
386 * date invocation:: Print or set system date and time
387 * uname invocation:: Print system information
388 * hostname invocation:: Print or set system name
389 * hostid invocation:: Print numeric host identifier.
391 @command{date}: Print or set system date and time
393 * Time directives:: Time directives
394 * Date directives:: Date directives
395 * Literal directives:: Literal directives
397 * Setting the time:: Setting the time
398 * Options for date:: Options for @command{date}
399 * Examples of date:: Examples of @command{date}
401 Modified command invocation
403 * chroot invocation:: Run a command with a different root directory
404 * env invocation:: Run a command in a modified environment
405 * nice invocation:: Run a command with modified scheduling priority
406 * nohup invocation:: Run a command immune to hangups
407 * su invocation:: Run a command with substitute user and group id
411 * kill invocation:: Sending a signal to processes.
415 * sleep invocation:: Delay for a specified time
419 * factor invocation:: Print prime factors
420 * seq invocation:: Print numeric sequences
424 * Mode Structure:: Structure of File Permissions
425 * Symbolic Modes:: Mnemonic permissions representation
426 * Numeric Modes:: Permissions as octal numbers
430 * General date syntax: General date syntax
431 * Calendar date items: Calendar date items
432 * Time of day items: Time of day items
433 * Time zone items: Time zone items
434 * Day of week items: Day of week items
435 * Relative items in date strings: Relative items in date strings
436 * Pure numbers in date strings: Pure numbers in date strings
437 * Authors of getdate: Authors of getdate
439 Opening the software toolbox
441 * Toolbox introduction:: Toolbox introduction
442 * I/O redirection:: I/O redirection
443 * The who command:: The @command{who} command
444 * The cut command:: The @command{cut} command
445 * The sort command:: The @command{sort} command
446 * The uniq command:: The @command{uniq} command
447 * Putting the tools together:: Putting the tools together
449 GNU Free Documentation License
451 * How to use this License for your documents::
458 @chapter Introduction
460 This manual is a work in progress: many sections make no attempt to explain
461 basic concepts in a way suitable for novices. Thus, if you are interested,
462 please get involved in improving this manual. The entire @sc{gnu} community
465 @cindex @acronym{POSIX}
466 The @sc{gnu} utilities documented here are mostly compatible with the
467 @acronym{POSIX} standard.
468 @cindex bugs, reporting
469 Please report bugs to @email{bug-coreutils@@gnu.org}. Remember
470 to include the version number, machine architecture, input files, and
471 any other information needed to reproduce the bug: your input, what you
472 expected, what you got, and why it is wrong. Diffs are welcome, but
473 please include a description of the problem as well, since this is
474 sometimes difficult to infer. @xref{Bugs, , , gcc, Using and Porting GNU CC}.
480 @cindex MacKenzie, D.
483 This manual was originally derived from the Unix man pages in the
484 distributions, which were written by David MacKenzie and updated by Jim
485 Meyering. What you are reading now is the authoritative documentation
486 for these utilities; the man pages are no longer being maintained. The
487 original @command{fmt} man page was written by Ross Paterson. Fran@,{c}ois
488 Pinard did the initial conversion to Texinfo format. Karl Berry did the
489 indexing, some reorganization, and editing of the results. Brian
490 Youmans of the Free Software Foundation office staff combined the
491 manuals for textutils, fileutils, and sh-utils to produce the present
492 omnibus manual. Richard Stallman contributed his usual invaluable
493 insights to the overall process.
496 @chapter Common options
500 @itemx @w{@kbd{--backup}[=@var{method}]}
503 @vindex VERSION_CONTROL
504 @cindex backups, making
505 @xref{Backup options}.
506 Make a backup of each file that would otherwise be overwritten or removed.
509 @macro optBackupSuffix
510 @item -S @var{suffix}
511 @itemx --suffix=@var{suffix}
514 Append @var{suffix} to each backup file made with @option{-b}.
515 @xref{Backup options}.
518 @macro optTargetDirectory
519 @itemx @w{@kbd{--target-directory}=@var{directory}}
520 @opindex --target-directory
521 @cindex target directory
522 @cindex destination directory
523 Specify the destination @var{directory}.
524 @xref{Target directory}.
531 Append an SI-style abbreviation to each size, such as @samp{MB} for
532 megabytes. Powers of 1000 are used, not 1024; @samp{MB} stands for
533 1,000,000 bytes. This option is equivalent to
534 @option{--block-size=si}. Use the @option{-h} or
535 @option{--human-readable} option if
536 you prefer powers of 1024.
539 @macro optHumanReadable
541 @itemx --human-readable
543 @opindex --human-readable
544 @cindex human-readable output
545 Append a size letter to each size, such as @samp{M} for mebibytes.
546 Powers of 1024 are used, not 1000; @samp{M} stands for 1,048,576 bytes.
547 Use the @option{--si} option if you prefer powers of 1000.
550 @macro optStripTrailingSlashes
551 @itemx @w{@kbd{--strip-trailing-slashes}}
552 @opindex --strip-trailing-slashes
553 @cindex stripping trailing slashes
554 Remove any trailing slashes from each @var{source} argument.
555 @xref{Trailing slashes}.
558 @macro optVersionControl
559 @item -V @var{method}
560 @itemx --version-control=@var{method}
562 @opindex --version-control
563 Change the type of backups made with @option{-b}. The @var{method}
564 argument can be @samp{none} (or @samp{off}), @samp{numbered} (or
565 @samp{t}), @samp{existing} (or @samp{nil}), or @samp{never} (or
566 @samp{simple}). @xref{Backup options}.
569 @cindex common options
571 Certain options are available in all of these programs. Rather than
572 writing identical descriptions for each of the programs, they are
573 described here. (In fact, every @sc{gnu} program accepts (or should accept)
576 @vindex POSIXLY_CORRECT
577 Normally options and operands can appear in any order, and programs act
578 as if all the options appear before any operands. For example,
579 @samp{sort -r passwd -t :} acts like @samp{sort -r -t : passwd}, since
580 @samp{:} is an option-argument of @option{-t}. However, if the
581 @env{POSIXLY_CORRECT} environment variable is set, options must appear
582 before operands, unless otherwise specified for a particular command.
584 Some of these programs recognize the @option{--help} and @option{--version}
585 options only when one of them is the sole command line argument.
592 Print a usage message listing all available options, then exit successfully.
596 @cindex version number, finding
597 Print the version number, then exit successfully.
601 @cindex option delimiter
602 Delimit the option list. Later arguments, if any, are treated as
603 operands even if they begin with @samp{-}. For example, @samp{sort --
604 -r} reads from the file named @file{-r}.
608 @cindex standard input
609 @cindex standard output
610 A single @samp{-} is not really an option, though it looks like one. It
611 stands for standard input, or for standard output if that is clear from
612 the context, and it can be used either as an operand or as an
613 option-argument. For example, @samp{sort -o - -} outputs to standard
614 output and reads from standard input, and is equivalent to plain
615 @samp{sort}. Unless otherwise specified, @samp{-} can appear in any
616 context that requires a file name.
619 * Exit status:: Indicating program success or failure.
620 * Backup options:: -b -S -V, in some programs.
621 * Block size:: BLOCK_SIZE and --block-size, in some programs.
622 * Target directory:: --target-directory, in some programs.
623 * Trailing slashes:: --strip-trailing-slashes, in some programs.
624 * Traversing symlinks:: -H, -L, or -P, in some programs.
625 * Treating / specially:: --preserve-root and --no-preserve-root.
626 * Standards conformance:: Conformance to the @acronym{POSIX} standard.
634 An exit status of zero indicates success,
635 and a nonzero value indicates failure.
638 Nearly every command invocation yields an integral @dfn{exit status}
639 that can be used to change how other commands work.
640 For the vast majority of commands, an exit status of zero indicates
641 success. Failure is indicated by a nonzero value---typically
642 @samp{1}, though it may differ on unusual platforms as @acronym{POSIX}
643 requires only that it be nonzero.
645 However, some of the programs documented here do produce
646 other exit status values and a few associate different
647 meanings with the values @samp{0} and @samp{1}.
648 Here are some of the exceptions:
649 @command{chroot}, @command{env}, @command{expr},
650 @command{nice}, @command{nohup}, @command{printenv},
651 @command{sort}, @command{su}, @command{test}, @command{tty}.
655 @section Backup options
657 @cindex backup options
659 Some @sc{gnu} programs (at least @command{cp}, @command{install},
660 @command{ln}, and @command{mv}) optionally make backups of files
661 before writing new versions.
662 These options control the details of these backups. The options are also
663 briefly mentioned in the descriptions of the particular programs.
668 @itemx @w{@kbd{--backup}[=@var{method}]}
671 @vindex VERSION_CONTROL
672 @cindex backups, making
673 Make a backup of each file that would otherwise be overwritten or removed.
674 Without this option, the original versions are destroyed.
675 Use @var{method} to determine the type of backups to make.
676 When this option is used but @var{method} is not specified,
677 then the value of the @env{VERSION_CONTROL}
678 environment variable is used. And if @env{VERSION_CONTROL} is not set,
679 the default backup type is @samp{existing}.
681 Note that the short form of this option, @option{-b} does not accept any
682 argument. Using @option{-b} is equivalent to using @option{--backup=existing}.
684 @vindex version-control @r{Emacs variable}
685 This option corresponds to the Emacs variable @samp{version-control};
686 the values for @var{method} are the same as those used in Emacs.
687 This option also accepts more descriptive names.
688 The valid @var{method}s are (unique abbreviations are accepted):
693 @opindex none @r{backup method}
698 @opindex numbered @r{backup method}
699 Always make numbered backups.
703 @opindex existing @r{backup method}
704 Make numbered backups of files that already have them, simple backups
709 @opindex simple @r{backup method}
710 Always make simple backups. Please note @samp{never} is not to be
711 confused with @samp{none}.
715 @item -S @var{suffix}
716 @itemx --suffix=@var{suffix}
719 @cindex backup suffix
720 @vindex SIMPLE_BACKUP_SUFFIX
721 Append @var{suffix} to each backup file made with @option{-b}. If this
722 option is not specified, the value of the @env{SIMPLE_BACKUP_SUFFIX}
723 environment variable is used. And if @env{SIMPLE_BACKUP_SUFFIX} is not
724 set, the default is @samp{~}, just as in Emacs.
726 @itemx --version-control=@var{method}
727 @opindex --version-control
728 @c FIXME: remove this block one or two releases after the actual
729 @c removal from the code.
730 This option is obsolete and will be removed in a future release.
731 It has been replaced with @w{@kbd{--backup}}.
740 Some @sc{gnu} programs (at least @command{df}, @command{du}, and
741 @command{ls}) display sizes in ``blocks''. You can adjust the block size
742 and method of display to make sizes easier to read. The block size
743 used for display is independent of any filesystem block size.
744 Fractional block counts are rounded up to the nearest integer.
746 @opindex --block-size=@var{size}
748 @vindex DF_BLOCK_SIZE
749 @vindex DU_BLOCK_SIZE
750 @vindex LS_BLOCK_SIZE
751 @vindex POSIXLY_CORRECT@r{, and block size}
753 The default block size is chosen by examining the following environment
754 variables in turn; the first one that is set determines the block size.
759 This specifies the default block size for the @command{df} command.
760 Similarly, @env{DU_BLOCK_SIZE} specifies the default for @command{du} and
761 @env{LS_BLOCK_SIZE} for @command{ls}.
764 This specifies the default block size for all three commands, if the
765 above command-specific environment variables are not set.
767 @item POSIXLY_CORRECT
768 If neither the @env{@var{command}_BLOCK_SIZE} nor the @env{BLOCK_SIZE}
769 variables are set, but this variable is set, the block size defaults to 512.
773 If none of the above environment variables are set, the block size
774 currently defaults to 1024 bytes in most contexts, but this number may
775 change in the future. For @command{ls} file sizes, the block size
778 @cindex human-readable output
781 A block size specification can be a positive integer specifying the number
782 of bytes per block, or it can be @code{human-readable} or @code{si} to
783 select a human-readable format. Integers may be followed by suffixes
784 that are upward compatible with the
785 @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI prefixes}
786 for decimal multiples and with the
787 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
788 prefixes for binary multiples}.
790 With human-readable formats, output sizes are followed by a size letter
791 such as @samp{M} for megabytes. @code{BLOCK_SIZE=human-readable} uses
792 powers of 1024; @samp{M} stands for 1,048,576 bytes.
793 @code{BLOCK_SIZE=si} is similar, but uses powers of 1000 and appends
794 @samp{B}; @samp{MB} stands for 1,000,000 bytes.
797 A block size specification preceded by @samp{'} causes output sizes to
798 be displayed with thousands separators. The @env{LC_NUMERIC} locale
799 specifies the thousands separator and grouping. For example, in an
800 American English locale, @samp{--block-size="'1kB"} would cause a size
801 of 1234000 bytes to be displayed as @samp{1,234}. In the default C
802 locale, there is no thousands separator so a leading @samp{'} has no
805 An integer block size can be followed by a suffix to specify a
806 multiple of that size. A bare size letter,
807 or one followed by @samp{iB}, specifies
808 a multiple using powers of 1024. A size letter followed by @samp{B}
809 specifies powers of 1000 instead. For example, @samp{1M} and
810 @samp{1MiB} are equivalent to @samp{1048576}, whereas @samp{1MB} is
811 equivalent to @samp{1000000}.
813 A plain suffix without a preceding integer acts as if @samp{1} were
814 prepended, except that it causes a size indication to be appended to
815 the output. For example, @samp{--block-size="kB"} displays 3000 as
818 The following suffixes are defined. Large sizes like @code{1Y}
819 may be rejected by your computer due to limitations of its arithmetic.
823 @cindex kilobyte, definition of
824 kilobyte: @math{10^3 = 1000}.
828 @cindex kibibyte, definition of
829 kibibyte: @math{2^10 = 1024}. @samp{K} is special: the SI prefix is
830 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
831 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
833 @cindex megabyte, definition of
834 megabyte: @math{10^6 = 1,000,000}.
837 @cindex mebibyte, definition of
838 mebibyte: @math{2^20 = 1,048,576}.
840 @cindex gigabyte, definition of
841 gigabyte: @math{10^9 = 1,000,000,000}.
844 @cindex gibibyte, definition of
845 gibibyte: @math{2^30 = 1,073,741,824}.
847 @cindex terabyte, definition of
848 terabyte: @math{10^12 = 1,000,000,000,000}.
851 @cindex tebibyte, definition of
852 tebibyte: @math{2^40 = 1,099,511,627,776}.
854 @cindex petabyte, definition of
855 petabyte: @math{10^15 = 1,000,000,000,000,000}.
858 @cindex pebibyte, definition of
859 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
861 @cindex exabyte, definition of
862 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
865 @cindex exbibyte, definition of
866 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
868 @cindex zettabyte, definition of
869 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
872 @math{2^70 = 1,180,591,620,717,411,303,424}.
873 (@samp{Zi} is a GNU extension to IEC 60027-2.)
875 @cindex yottabyte, definition of
876 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
879 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
880 (@samp{Yi} is a GNU extension to IEC 60027-2.)
885 @opindex --block-size
886 @opindex --human-readable
889 Block size defaults can be overridden by an explicit
890 @option{--block-size=@var{size}} option. The @option{-k}
891 option is equivalent to @option{--block-size=1K}, which
892 is the default unless the @env{POSIXLY_CORRECT} environment variable is
893 set. The @option{-h} or @option{--human-readable} option is equivalent to
894 @option{--block-size=human-readable}. The @option{--si} option is
895 equivalent to @option{--block-size=si}.
897 @node Target directory
898 @section Target directory
900 @cindex target directory
902 Some @sc{gnu} programs (at least @command{cp}, @command{install},
903 @command{ln}, and @command{mv}) allow you to specify the target directory
908 @itemx @w{@kbd{--target-directory}=@var{directory}}
909 @opindex --target-directory
910 @cindex target directory
911 @cindex destination directory
912 Specify the destination @var{directory}.
914 The interface for most programs is that after processing options and a
915 finite (possibly zero) number of fixed-position arguments, the remaining
916 argument list is either expected to be empty, or is a list of items
917 (usually files) that will all be handled identically. The @command{xargs}
918 program is designed to work well with this convention.
920 The commands in the @command{mv}-family are unusual in that they take
921 a variable number of arguments with a special case at the @emph{end}
922 (namely, the target directory). This makes it nontrivial to perform some
923 operations, e.g., ``move all files from here to ../d/'', because
924 @code{mv * ../d/} might exhaust the argument space, and @code{ls | xargs ...}
925 doesn't have a clean way to specify an extra final argument for each
926 invocation of the subject command. (It can be done by going through a
927 shell command, but that requires more human labor and brain power than
930 The @w{@kbd{--target-directory}} option allows the @command{cp},
931 @command{install}, @command{ln}, and @command{mv} programs to be used
932 conveniently with @command{xargs}. For example, you can move the files
933 from the current directory to a sibling directory, @code{d} like this:
934 (However, this doesn't move files whose names begin with @samp{.}.)
937 ls |xargs mv --target-directory=../d
940 If you use the @sc{gnu} @command{find} program, you can move @emph{all}
941 files with this command:
943 find . -mindepth 1 -maxdepth 1 \
944 | xargs mv --target-directory=../d
947 But that will fail if there are no files in the current directory
948 or if any file has a name containing a newline character.
949 The following example removes those limitations and requires both
950 @sc{gnu} @command{find} and @sc{gnu} @command{xargs}:
952 find . -mindepth 1 -maxdepth 1 -print0 \
953 | xargs --null --no-run-if-empty \
954 mv --target-directory=../d
959 @node Trailing slashes
960 @section Trailing slashes
962 @cindex trailing slashes
964 Some @sc{gnu} programs (at least @command{cp} and @command{mv}) allow you to
965 remove any trailing slashes from each @var{source} argument before
966 operating on it. The @w{@kbd{--strip-trailing-slashes}} option enables
969 This is useful when a @var{source} argument may have a trailing slash and
970 @c FIXME: mv's behavior in this case is system-dependent
971 specify a symbolic link to a directory. This scenario is in fact rather
972 common because some shells can automatically append a trailing slash when
973 performing file name completion on such symbolic links. Without this
974 option, @command{mv}, for example, (via the system's rename function) must
975 interpret a trailing slash as a request to dereference the symbolic link
976 and so must rename the indirectly referenced @emph{directory} and not
977 the symbolic link. Although it may seem surprising that such behavior
978 be the default, it is required by @acronym{POSIX} and is consistent with
979 other parts of that standard.
981 @node Traversing symlinks
982 @section Traversing symlinks
984 @cindex symbolic link to directory, controlling traversal of
986 The following options modify how @command{chown} and @command{chgrp}
987 @c FIXME: note that `du' has these options, too, but they have slightly
988 @c different meaning.
989 traverse a hierarchy when the @option{--recursive} (@option{-R})
990 option is also specified.
991 If more than one of the following options is specified, only the final
993 These options specify whether processing a symbolic link to a directory
994 entails operating on just the symbolic link or on all files in the
995 hierarchy rooted at that directory.
997 These options are independent of @option{--dereference} and
998 @option{--no-dereference} (@option{-h}), which control whether to modify
999 a symlink or its referent.
1006 @cindex symbolic link to directory, traverse each that is specified on the command line
1007 If @option{--recursive} (@option{-R}) is specified and
1008 a command line argument is a symbolic link to a directory, traverse it.
1015 @cindex symbolic link to directory, traverse each that is encountered
1016 In a recursive traversal, traverse every symbolic link to a directory
1017 that is encountered.
1024 @cindex symbolic link to directory, never traverse
1025 Do not traverse any symbolic links.
1026 This is the default if none of @option{-H}, @option{-L},
1027 or @option{-P} is specified.
1034 @node Treating / specially
1035 @section Treating / specially
1037 Certain commands can operate destructively on entire hierarchies.
1038 For example, if a user with appropriate privileges mistakenly runs
1039 @samp{rm -rf / tmp/junk} or @samp{cd /bin; rm -rf ../}, that may remove
1040 all files on the entire system. Since there are so few
1041 @footnote{If you know of one, please write to @email{bug-coreutils@@gnu.org}.}
1042 legitimate uses for such a command,
1043 @sc{gnu} @command{rm} provides the @option{--preserve-root} option
1044 to make it so @command{rm} declines to operate on any directory
1045 that resolves to @file{/}. The default is still to allow
1046 @samp{rm -rf /} to operate unimpeded.
1047 Another new option, @option{--no-preserve-root}, cancels the
1048 effect of any preceding @option{--preserve-root} option.
1049 Note that the @option{--preserve-root} behavior may become the default
1052 The commands @command{chgrp}, @command{chmod} and @command{chown}
1053 can also operate destructively on entire hierarchies, so they too
1054 support these options. Although, unlike @command{rm}, they don't
1055 actually unlink files, these commands are arguably more dangerous
1056 when operating recursively on @file{/}, since they often work much
1057 more quickly, and hence damage more files before an alert user can
1060 @node Standards conformance
1061 @section Standards conformance
1063 @vindex POSIXLY_CORRECT
1064 In a few cases, the @sc{gnu} utilities' default behavior is
1065 incompatible with the @acronym{POSIX} standard. To suppress these
1066 incompatibilities, define the @env{POSIXLY_CORRECT} environment
1067 variable. Unless you are checking for @acronym{POSIX} conformance, you
1068 probably do not need to define @env{POSIXLY_CORRECT}.
1070 Newer versions of @acronym{POSIX} are occasionally incompatible with older
1071 versions. For example, older versions of @acronym{POSIX} required the
1072 command @samp{sort +1} to sort based on the second and succeeding
1073 fields in each input line, but starting with @acronym{POSIX} 1003.1-2001
1074 the same command is required to sort the file named @file{+1}, and you
1075 must instead use the command @samp{sort -k 2} to get the field-based
1078 @vindex _POSIX2_VERSION
1079 The @sc{gnu} utilities normally conform to the version of @acronym{POSIX}
1080 that is standard for your system. To cause them to conform to a
1081 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
1082 environment variable to a value of the form @var{yyyymm} specifying
1083 the year and month the standard was adopted. Two values are currently
1084 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
1085 @acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX}
1086 1003.1-2001. For example, if you are running older software that
1087 assumes an older version of @acronym{POSIX} and uses @samp{sort +1},
1088 @samp{head -1}, or @samp{tail +1}, you
1089 can work around the compatibility problems by setting
1090 @samp{_POSIX2_VERSION=199209} in your environment.
1092 @node Output of entire files
1093 @chapter Output of entire files
1095 @cindex output of entire files
1096 @cindex entire files, output of
1098 These commands read and write entire files, possibly transforming them
1102 * cat invocation:: Concatenate and write files.
1103 * tac invocation:: Concatenate and write files in reverse.
1104 * nl invocation:: Number lines and write files.
1105 * od invocation:: Write files in octal or other formats.
1108 @node cat invocation
1109 @section @command{cat}: Concatenate and write files
1112 @cindex concatenate and write files
1113 @cindex copying files
1115 @command{cat} copies each @var{file} (@samp{-} means standard input), or
1116 standard input if none are given, to standard output. Synopsis:
1119 cat [@var{option}] [@var{file}]@dots{}
1122 The program accepts the following options. Also see @ref{Common options}.
1130 Equivalent to @option{-vET}.
1136 @cindex binary and text I/O in cat
1137 On MS-DOS and MS-Windows only, read and write the files in binary mode.
1138 By default, @command{cat} on MS-DOS/MS-Windows uses binary mode only when
1139 standard output is redirected to a file or a pipe; this option overrides
1140 that. Binary file I/O is used so that the files retain their format
1141 (Unix text as opposed to DOS text and binary), because @command{cat} is
1142 frequently used as a file-copying program. Some options (see below)
1143 cause @command{cat} to read and write files in text mode because in those
1144 cases the original file contents aren't important (e.g., when lines are
1145 numbered by @command{cat}, or when line endings should be marked). This is
1146 so these options work as DOS/Windows users would expect; for example,
1147 DOS-style text files have their lines end with the CR-LF pair of
1148 characters, which won't be processed as an empty line by @option{-b} unless
1149 the file is read in text mode.
1152 @itemx --number-nonblank
1154 @opindex --number-nonblank
1155 Number all nonblank output lines, starting with 1. On MS-DOS and
1156 MS-Windows, this option causes @command{cat} to read and write files in
1161 Equivalent to @option{-vE}.
1166 @opindex --show-ends
1167 Display a @samp{$} after the end of each line. On MS-DOS and
1168 MS-Windows, this option causes @command{cat} to read and write files in
1175 Number all output lines, starting with 1. On MS-DOS and MS-Windows,
1176 this option causes @command{cat} to read and write files in text mode.
1179 @itemx --squeeze-blank
1181 @opindex --squeeze-blank
1182 @cindex squeezing blank lines
1183 Replace multiple adjacent blank lines with a single blank line. On
1184 MS-DOS and MS-Windows, this option causes @command{cat} to read and write
1189 Equivalent to @option{-vT}.
1194 @opindex --show-tabs
1195 Display TAB characters as @samp{^I}.
1199 Ignored; for Unix compatibility.
1202 @itemx --show-nonprinting
1204 @opindex --show-nonprinting
1205 Display control characters except for LFD and TAB using
1206 @samp{^} notation and precede characters that have the high bit set with
1207 @samp{M-}. On MS-DOS and MS-Windows, this option causes @command{cat} to
1208 read files and standard input in DOS binary mode, so the CR
1209 characters at the end of each line are also visible.
1216 @node tac invocation
1217 @section @command{tac}: Concatenate and write files in reverse
1220 @cindex reversing files
1222 @command{tac} copies each @var{file} (@samp{-} means standard input), or
1223 standard input if none are given, to standard output, reversing the
1224 records (lines by default) in each separately. Synopsis:
1227 tac [@var{option}]@dots{} [@var{file}]@dots{}
1230 @dfn{Records} are separated by instances of a string (newline by
1231 default). By default, this separator string is attached to the end of
1232 the record that it follows in the file.
1234 The program accepts the following options. Also see @ref{Common options}.
1242 The separator is attached to the beginning of the record that it
1243 precedes in the file.
1249 Treat the separator string as a regular expression. Users of @command{tac}
1250 on MS-DOS/MS-Windows should note that, since @command{tac} reads files in
1251 binary mode, each line of a text file might end with a CR/LF pair
1252 instead of the Unix-style LF.
1254 @item -s @var{separator}
1255 @itemx --separator=@var{separator}
1257 @opindex --separator
1258 Use @var{separator} as the record separator, instead of newline.
1266 @section @command{nl}: Number lines and write files
1269 @cindex numbering lines
1270 @cindex line numbering
1272 @command{nl} writes each @var{file} (@samp{-} means standard input), or
1273 standard input if none are given, to standard output, with line numbers
1274 added to some or all of the lines. Synopsis:
1277 nl [@var{option}]@dots{} [@var{file}]@dots{}
1280 @cindex logical pages, numbering on
1281 @command{nl} decomposes its input into (logical) pages; by default, the
1282 line number is reset to 1 at the top of each logical page. @command{nl}
1283 treats all of the input files as a single document; it does not reset
1284 line numbers or logical pages between files.
1286 @cindex headers, numbering
1287 @cindex body, numbering
1288 @cindex footers, numbering
1289 A logical page consists of three sections: header, body, and footer.
1290 Any of the sections can be empty. Each can be numbered in a different
1291 style from the others.
1293 The beginnings of the sections of logical pages are indicated in the
1294 input file by a line containing exactly one of these delimiter strings:
1305 The two characters from which these strings are made can be changed from
1306 @samp{\} and @samp{:} via options (see below), but the pattern and
1307 length of each string cannot be changed.
1309 A section delimiter is replaced by an empty line on output. Any text
1310 that comes before the first section delimiter string in the input file
1311 is considered to be part of a body section, so @command{nl} treats a
1312 file that contains no section delimiters as a single body section.
1314 The program accepts the following options. Also see @ref{Common options}.
1318 @item -b @var{style}
1319 @itemx --body-numbering=@var{style}
1321 @opindex --body-numbering
1322 Select the numbering style for lines in the body section of each
1323 logical page. When a line is not numbered, the current line number
1324 is not incremented, but the line number separator character is still
1325 prepended to the line. The styles are:
1331 number only nonempty lines (default for body),
1333 do not number lines (default for header and footer),
1335 number only lines that contain a match for @var{regexp}.
1339 @itemx --section-delimiter=@var{cd}
1341 @opindex --section-delimiter
1342 @cindex section delimiters of pages
1343 Set the section delimiter characters to @var{cd}; default is
1344 @samp{\:}. If only @var{c} is given, the second remains @samp{:}.
1345 (Remember to protect @samp{\} or other metacharacters from shell
1346 expansion with quotes or extra backslashes.)
1348 @item -f @var{style}
1349 @itemx --footer-numbering=@var{style}
1351 @opindex --footer-numbering
1352 Analogous to @option{--body-numbering}.
1354 @item -h @var{style}
1355 @itemx --header-numbering=@var{style}
1357 @opindex --header-numbering
1358 Analogous to @option{--body-numbering}.
1360 @item -i @var{number}
1361 @itemx --page-increment=@var{number}
1363 @opindex --page-increment
1364 Increment line numbers by @var{number} (default 1).
1366 @item -l @var{number}
1367 @itemx --join-blank-lines=@var{number}
1369 @opindex --join-blank-lines
1370 @cindex empty lines, numbering
1371 @cindex blank lines, numbering
1372 Consider @var{number} (default 1) consecutive empty lines to be one
1373 logical line for numbering, and only number the last one. Where fewer
1374 than @var{number} consecutive empty lines occur, do not number them.
1375 An empty line is one that contains no characters, not even spaces
1378 @item -n @var{format}
1379 @itemx --number-format=@var{format}
1381 @opindex --number-format
1382 Select the line numbering format (default is @code{rn}):
1386 @opindex ln @r{format for @command{nl}}
1387 left justified, no leading zeros;
1389 @opindex rn @r{format for @command{nl}}
1390 right justified, no leading zeros;
1392 @opindex rz @r{format for @command{nl}}
1393 right justified, leading zeros.
1397 @itemx --no-renumber
1399 @opindex --no-renumber
1400 Do not reset the line number at the start of a logical page.
1402 @item -s @var{string}
1403 @itemx --number-separator=@var{string}
1405 @opindex --number-separator
1406 Separate the line number from the text line in the output with
1407 @var{string} (default is the TAB character).
1409 @item -v @var{number}
1410 @itemx --starting-line-number=@var{number}
1412 @opindex --starting-line-number
1413 Set the initial line number on each logical page to @var{number} (default 1).
1415 @item -w @var{number}
1416 @itemx --number-width=@var{number}
1418 @opindex --number-width
1419 Use @var{number} characters for line numbers (default 6).
1427 @section @command{od}: Write files in octal or other formats
1430 @cindex octal dump of files
1431 @cindex hex dump of files
1432 @cindex ASCII dump of files
1433 @cindex file contents, dumping unambiguously
1435 @command{od} writes an unambiguous representation of each @var{file}
1436 (@samp{-} means standard input), or standard input if none are given.
1440 od [@var{option}]@dots{} [@var{file}]@dots{}
1441 od --traditional [@var{file}] [[+]@var{offset} [[+]@var{label}]]
1444 Each line of output consists of the offset in the input, followed by
1445 groups of data from the file. By default, @command{od} prints the offset in
1446 octal, and each group of file data is two bytes of input printed as a
1447 single octal number.
1449 The program accepts the following options. Also see @ref{Common options}.
1453 @item -A @var{radix}
1454 @itemx --address-radix=@var{radix}
1456 @opindex --address-radix
1457 @cindex radix for file offsets
1458 @cindex file offset radix
1459 Select the base in which file offsets are printed. @var{radix} can
1460 be one of the following:
1470 none (do not print offsets).
1473 The default is octal.
1475 @item -j @var{bytes}
1476 @itemx --skip-bytes=@var{bytes}
1478 @opindex --skip-bytes
1479 Skip @var{bytes} input bytes before formatting and writing. If
1480 @var{bytes} begins with @samp{0x} or @samp{0X}, it is interpreted in
1481 hexadecimal; otherwise, if it begins with @samp{0}, in octal; otherwise,
1482 in decimal. Appending @samp{b} multiplies @var{bytes} by 512, @samp{k}
1483 by 1024, and @samp{m} by 1048576.
1485 @item -N @var{bytes}
1486 @itemx --read-bytes=@var{bytes}
1488 @opindex --read-bytes
1489 Output at most @var{bytes} bytes of the input. Prefixes and suffixes on
1490 @code{bytes} are interpreted as for the @option{-j} option.
1493 @itemx --strings[=@var{n}]
1496 @cindex string constants, outputting
1497 Instead of the normal output, output only @dfn{string constants}: at
1498 least @var{n} consecutive @acronym{ASCII} graphic characters,
1499 followed by a null (zero) byte.
1501 If @var{n} is omitted with @option{--strings}, the default is 3. On
1502 older systems, @sc{gnu} @command{od} instead supports an obsolete
1503 option @option{-s[@var{n}]}, where @var{n} also defaults to 3.
1504 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow
1505 @option{-s} without an argument; use @option{--strings} instead.
1508 @itemx --format=@var{type}
1511 Select the format in which to output the file data. @var{type} is a
1512 string of one or more of the below type indicator characters. If you
1513 include more than one type indicator character in a single @var{type}
1514 string, or use this option more than once, @command{od} writes one copy
1515 of each output line using each of the data types that you specified,
1516 in the order that you specified.
1518 Adding a trailing ``z'' to any type specification appends a display
1519 of the @acronym{ASCII} character representation of the printable characters
1520 to the output line generated by the type specification.
1526 @acronym{ASCII} character or backslash escape,
1539 The type @code{a} outputs things like @samp{sp} for space, @samp{nl} for
1540 newline, and @samp{nul} for a null (zero) byte. Type @code{c} outputs
1541 @samp{ }, @samp{\n}, and @code{\0}, respectively.
1544 Except for types @samp{a} and @samp{c}, you can specify the number
1545 of bytes to use in interpreting each number in the given data type
1546 by following the type indicator character with a decimal integer.
1547 Alternately, you can specify the size of one of the C compiler's
1548 built-in data types by following the type indicator character with
1549 one of the following characters. For integers (@samp{d}, @samp{o},
1550 @samp{u}, @samp{x}):
1563 For floating point (@code{f}):
1575 @itemx --output-duplicates
1577 @opindex --output-duplicates
1578 Output consecutive lines that are identical. By default, when two or
1579 more consecutive output lines would be identical, @command{od} outputs only
1580 the first line, and puts just an asterisk on the following line to
1581 indicate the elision.
1584 @itemx --width[=@var{n}]
1587 Dump @code{n} input bytes per output line. This must be a multiple of
1588 the least common multiple of the sizes associated with the specified
1591 If this option is not given at all, the default is 16. If @var{n} is
1592 omitted with @option{--width}, the default is 32. On older systems,
1593 @sc{gnu} @command{od} instead supports an obsolete option
1594 @option{-w[@var{n}]}, where @var{n} also defaults to 32. @acronym{POSIX}
1595 1003.1-2001 (@pxref{Standards conformance}) does not allow @option{-w}
1596 without an argument; use @option{--width} instead.
1600 The next several options are shorthands for format specifications.
1601 @sc{gnu} @command{od} accepts any combination of shorthands and format
1602 specification options. These options accumulate.
1608 Output as named characters. Equivalent to @option{-ta}.
1612 Output as octal bytes. Equivalent to @option{-toC}.
1616 Output as @acronym{ASCII} characters or backslash escapes. Equivalent to
1621 Output as unsigned decimal shorts. Equivalent to @option{-tu2}.
1625 Output as floats. Equivalent to @option{-tfF}.
1629 Output as hexadecimal shorts. Equivalent to @option{-tx2}.
1633 Output as decimal shorts. Equivalent to @option{-td2}.
1637 Output as decimal longs. Equivalent to @option{-td4}.
1641 Output as octal shorts. Equivalent to @option{-to2}.
1645 Output as hexadecimal shorts. Equivalent to @option{-tx2}.
1648 @opindex --traditional
1649 Recognize the non-option arguments that traditional @command{od}
1650 accepted. The following syntax:
1653 od --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1657 can be used to specify at most one file and optional arguments
1658 specifying an offset and a pseudo-start address, @var{label}. By
1659 default, @var{offset} is interpreted as an octal number specifying how
1660 many input bytes to skip before formatting and writing. The optional
1661 trailing decimal point forces the interpretation of @var{offset} as a
1662 decimal number. If no decimal is specified and the offset begins with
1663 @samp{0x} or @samp{0X} it is interpreted as a hexadecimal number. If
1664 there is a trailing @samp{b}, the number of bytes skipped will be
1665 @var{offset} multiplied by 512. The @var{label} argument is interpreted
1666 just like @var{offset}, but it specifies an initial pseudo-address. The
1667 pseudo-addresses are displayed in parentheses following any normal
1675 @node Formatting file contents
1676 @chapter Formatting file contents
1678 @cindex formatting file contents
1680 These commands reformat the contents of files.
1683 * fmt invocation:: Reformat paragraph text.
1684 * pr invocation:: Paginate or columnate files for printing.
1685 * fold invocation:: Wrap input lines to fit in specified width.
1689 @node fmt invocation
1690 @section @command{fmt}: Reformat paragraph text
1693 @cindex reformatting paragraph text
1694 @cindex paragraphs, reformatting
1695 @cindex text, reformatting
1697 @command{fmt} fills and joins lines to produce output lines of (at most)
1698 a given number of characters (75 by default). Synopsis:
1701 fmt [@var{option}]@dots{} [@var{file}]@dots{}
1704 @command{fmt} reads from the specified @var{file} arguments (or standard
1705 input if none are given), and writes to standard output.
1707 By default, blank lines, spaces between words, and indentation are
1708 preserved in the output; successive input lines with different
1709 indentation are not joined; tabs are expanded on input and introduced on
1712 @cindex line-breaking
1713 @cindex sentences and line-breaking
1714 @cindex Knuth, Donald E.
1715 @cindex Plass, Michael F.
1716 @command{fmt} prefers breaking lines at the end of a sentence, and tries to
1717 avoid line breaks after the first word of a sentence or before the last
1718 word of a sentence. A @dfn{sentence break} is defined as either the end
1719 of a paragraph or a word ending in any of @samp{.?!}, followed by two
1720 spaces or end of line, ignoring any intervening parentheses or quotes.
1721 Like @TeX{}, @command{fmt} reads entire ``paragraphs'' before choosing line
1722 breaks; the algorithm is a variant of that in ``Breaking Paragraphs Into
1723 Lines'' (Donald E. Knuth and Michael F. Plass, @cite{Software---Practice
1724 and Experience}, 11 (1981), 1119--1184).
1726 The program accepts the following options. Also see @ref{Common options}.
1731 @itemx --crown-margin
1733 @opindex --crown-margin
1734 @cindex crown margin
1735 @dfn{Crown margin} mode: preserve the indentation of the first two
1736 lines within a paragraph, and align the left margin of each subsequent
1737 line with that of the second line.
1740 @itemx --tagged-paragraph
1742 @opindex --tagged-paragraph
1743 @cindex tagged paragraphs
1744 @dfn{Tagged paragraph} mode: like crown margin mode, except that if
1745 indentation of the first line of a paragraph is the same as the
1746 indentation of the second, the first line is treated as a one-line
1752 @opindex --split-only
1753 Split lines only. Do not join short lines to form longer ones. This
1754 prevents sample lines of code, and other such ``formatted'' text from
1755 being unduly combined.
1758 @itemx --uniform-spacing
1760 @opindex --uniform-spacing
1761 Uniform spacing. Reduce spacing between words to one space, and spacing
1762 between sentences to two spaces.
1765 @itemx -w @var{width}
1766 @itemx --width=@var{width}
1767 @opindex -@var{width}
1770 Fill output lines up to @var{width} characters (default 75). @command{fmt}
1771 initially tries to make lines about 7% shorter than this, to give it
1772 room to balance line lengths.
1774 @item -p @var{prefix}
1775 @itemx --prefix=@var{prefix}
1776 Only lines beginning with @var{prefix} (possibly preceded by whitespace)
1777 are subject to formatting. The prefix and any preceding whitespace are
1778 stripped for the formatting and then re-attached to each formatted output
1779 line. One use is to format certain kinds of program comments, while
1780 leaving the code unchanged.
1788 @section @command{pr}: Paginate or columnate files for printing
1791 @cindex printing, preparing files for
1792 @cindex multicolumn output, generating
1793 @cindex merging files in parallel
1795 @command{pr} writes each @var{file} (@samp{-} means standard input), or
1796 standard input if none are given, to standard output, paginating and
1797 optionally outputting in multicolumn format; optionally merges all
1798 @var{file}s, printing all in parallel, one per column. Synopsis:
1801 pr [@var{option}]@dots{} [@var{file}]@dots{}
1805 By default, a 5-line header is printed at each page: two blank lines;
1806 a line with the date, the filename, and the page count; and two more
1807 blank lines. A footer of five blank lines is also printed.
1808 With the @option{-F}
1809 option, a 3-line header is printed: the leading two blank lines are
1810 omitted; no footer is used. The default @var{page_length} in both cases is 66
1811 lines. The default number of text lines changes from 56 (without @option{-F})
1812 to 63 (with @option{-F}). The text line of the header takes the form
1813 @samp{@var{date} @var{string} @var{page}}, with spaces inserted around
1814 @var{string} so that the line takes up the full @var{page_width}. Here,
1815 @var{date} is the date (see the @option{-D} or @option{--date-format}
1816 option for details), @var{string} is the centered header string, and
1817 @var{page} identifies the page number. The @env{LC_MESSAGES} locale
1818 category affects the spelling of @var{page}; in the default C locale, it
1819 is @samp{Page @var{number}} where @var{number} is the decimal page
1822 Form feeds in the input cause page breaks in the output. Multiple form
1823 feeds produce empty pages.
1825 Columns are of equal width, separated by an optional string (default
1826 is @samp{space}). For multicolumn output, lines will always be truncated to
1827 @var{page_width} (default 72), unless you use the @option{-J} option.
1829 column output no line truncation occurs by default. Use @option{-W} option to
1830 truncate lines in that case.
1832 The following changes were made in version 1.22i and apply to later
1833 versions of @command{pr}:
1834 @c FIXME: this whole section here sounds very awkward to me. I
1835 @c made a few small changes, but really it all needs to be redone. - Brian
1836 @c OK, I fixed another sentence or two, but some of it I just don't understand.
1841 Some small @var{letter options} (@option{-s}, @option{-w}) have been
1842 redefined for better @acronym{POSIX} compliance. The output of some further
1843 cases has been adapted to other Unix systems. These changes are not
1844 compatible with earlier versions of the program.
1847 Some @var{new capital letter} options (@option{-J}, @option{-S}, @option{-W})
1848 have been introduced to turn off unexpected interferences of small letter
1849 options. The @option{-N} option and the second argument @var{last_page}
1850 of @samp{+FIRST_PAGE} offer more flexibility. The detailed handling of
1851 form feeds set in the input files requires the @option{-T} option.
1854 Capital letter options override small letter ones.
1857 Some of the option-arguments (compare @option{-s}, @option{-e},
1858 @option{-i}, @option{-n}) cannot be specified as separate arguments from the
1859 preceding option letter (already stated in the @acronym{POSIX} specification).
1862 The program accepts the following options. Also see @ref{Common options}.
1866 @item +@var{first_page}[:@var{last_page}]
1867 @itemx --pages=@var{first_page}[:@var{last_page}]
1868 @c The two following @opindex lines evoke warnings because they contain `:'
1869 @c The `info' spec does not permit that. If we use those lines, we end
1870 @c up with truncated index entries that don't work.
1871 @c @opindex +@var{first_page}[:@var{last_page}]
1872 @c @opindex --pages=@var{first_page}[:@var{last_page}]
1873 @opindex +@var{page_range}
1874 @opindex --pages=@var{page_range}
1875 Begin printing with page @var{first_page} and stop with @var{last_page}.
1876 Missing @samp{:@var{last_page}} implies end of file. While estimating
1877 the number of skipped pages each form feed in the input file results
1878 in a new page. Page counting with and without @samp{+@var{first_page}}
1879 is identical. By default, counting starts with the first page of input
1880 file (not first page printed). Line numbering may be altered by @option{-N}
1884 @itemx --columns=@var{column}
1885 @opindex -@var{column}
1887 @cindex down columns
1888 With each single @var{file}, produce @var{column} columns of output
1889 (default is 1) and print columns down, unless @option{-a} is used. The
1890 column width is automatically decreased as @var{column} increases; unless
1891 you use the @option{-W/-w} option to increase @var{page_width} as well.
1892 This option might well cause some lines to be truncated. The number of
1893 lines in the columns on each page are balanced. The options @option{-e}
1894 and @option{-i} are on for multiple text-column output. Together with
1895 @option{-J} option column alignment and line truncation is turned off.
1896 Lines of full length are joined in a free field format and @option{-S}
1897 option may set field separators. @option{-@var{column}} may not be used
1898 with @option{-m} option.
1904 @cindex across columns
1905 With each single @var{file}, print columns across rather than down. The
1906 @option{-@var{column}} option must be given with @var{column} greater than one.
1907 If a line is too long to fit in a column, it is truncated.
1910 @itemx --show-control-chars
1912 @opindex --show-control-chars
1913 Print control characters using hat notation (e.g., @samp{^G}); print
1914 other nonprinting characters in octal backslash notation. By default,
1915 nonprinting characters are not changed.
1918 @itemx --double-space
1920 @opindex --double-space
1921 @cindex double spacing
1922 Double space the output.
1924 @item -D @var{format}
1925 @itemx --date-format=@var{format}
1926 @cindex time formats
1927 @cindex formatting times
1928 Format header dates using @var{format}, using the same conventions as
1929 for the the command @samp{date +@var{format}}; @xref{date invocation}.
1930 Except for directives, which start with
1931 @samp{%}, characters in @var{format} are printed unchanged. You can use
1932 this option to specify an arbitrary string in place of the header date,
1933 e.g., @option{--date-format="Monday morning"}.
1935 @vindex POSIXLY_CORRECT
1937 If the @env{POSIXLY_CORRECT} environment variable is not set, the date
1938 format defaults to @samp{%Y-%m-%d %H:%M} (for example, @samp{2001-12-04
1939 23:59}); otherwise, the format depends on the @env{LC_TIME} locale
1940 category, with the default being @samp{%b %e %H:%M %Y} (for example,
1941 @samp{Dec@ @ 4 23:59 2001}.
1943 @item -e[@var{in-tabchar}[@var{in-tabwidth}]]
1944 @itemx --expand-tabs[=@var{in-tabchar}[@var{in-tabwidth}]]
1946 @opindex --expand-tabs
1948 Expand @var{tab}s to spaces on input. Optional argument @var{in-tabchar} is
1949 the input tab character (default is the TAB character). Second optional
1950 argument @var{in-tabwidth} is the input tab character's width (default
1958 @opindex --form-feed
1959 Use a form feed instead of newlines to separate output pages. The default
1960 page length of 66 lines is not altered. But the number of lines of text
1961 per page changes from default 56 to 63 lines.
1963 @item -h @var{HEADER}
1964 @itemx --header=@var{HEADER}
1967 Replace the filename in the header with the centered string @var{header}.
1968 When using the shell, @var{header} should be quoted and should be
1969 separated from @option{-h} by a space.
1971 @item -i[@var{out-tabchar}[@var{out-tabwidth}]]
1972 @itemx --output-tabs[=@var{out-tabchar}[@var{out-tabwidth}]]
1974 @opindex --output-tabs
1976 Replace spaces with @var{tab}s on output. Optional argument @var{out-tabchar}
1977 is the output tab character (default is the TAB character). Second optional
1978 argument @var{out-tabwidth} is the output tab character's width (default
1984 @opindex --join-lines
1985 Merge lines of full length. Used together with the column options
1986 @option{-@var{column}}, @option{-a -@var{column}} or @option{-m}. Turns off
1987 @option{-W/-w} line truncation;
1988 no column alignment used; may be used with
1989 @option{--sep-string[=@var{string}]}. @option{-J} has been introduced
1990 (together with @option{-W} and @option{--sep-string})
1991 to disentangle the old (@acronym{POSIX}-compliant) options @option{-w} and
1992 @option{-s} along with the three column options.
1995 @item -l @var{page_length}
1996 @itemx --length=@var{page_length}
1999 Set the page length to @var{page_length} (default 66) lines, including
2000 the lines of the header [and the footer]. If @var{page_length} is less
2001 than or equal to 10 (or <= 3 with @option{-F}), the header and footer are
2002 omitted, and all form feeds set in input files are eliminated, as if
2003 the @option{-T} option had been given.
2009 Merge and print all @var{file}s in parallel, one in each column. If a
2010 line is too long to fit in a column, it is truncated, unless the @option{-J}
2011 option is used. @option{--sep-string[=@var{string}]} may be used.
2013 some @var{file}s (form feeds set) produce empty columns, still marked
2014 by @var{string}. The result is a continuous line numbering and column
2015 marking throughout the whole merged file. Completely empty merged pages
2016 show no separators or line numbers. The default header becomes
2017 @samp{@var{date} @var{page}} with spaces inserted in the middle; this
2018 may be used with the @option{-h} or @option{--header} option to fill up
2019 the middle blank part.
2021 @item -n[@var{number-separator}[@var{digits}]]
2022 @itemx --number-lines[=@var{number-separator}[@var{digits}]]
2024 @opindex --number-lines
2025 Provide @var{digits} digit line numbering (default for @var{digits} is
2026 5). With multicolumn output the number occupies the first @var{digits}
2027 column positions of each text column or only each line of @option{-m}
2028 output. With single column output the number precedes each line just as
2029 @option{-m} does. Default counting of the line numbers starts with the
2030 first line of the input file (not the first line printed, compare the
2031 @option{--page} option and @option{-N} option).
2032 Optional argument @var{number-separator} is the character appended to
2033 the line number to separate it from the text followed. The default
2034 separator is the TAB character. In a strict sense a TAB is always
2035 printed with single column output only. The @var{TAB}-width varies
2036 with the @var{TAB}-position, e.g. with the left @var{margin} specified
2037 by @option{-o} option. With multicolumn output priority is given to
2038 @samp{equal width of output columns} (a @acronym{POSIX} specification).
2039 The @var{TAB}-width is fixed to the value of the first column and does
2040 not change with different values of left @var{margin}. That means a
2041 fixed number of spaces is always printed in the place of the
2042 @var{number-separator tab}. The tabification depends upon the output
2045 @item -N @var{line_number}
2046 @itemx --first-line-number=@var{line_number}
2048 @opindex --first-line-number
2049 Start line counting with the number @var{line_number} at first line of
2050 first page printed (in most cases not the first line of the input file).
2052 @item -o @var{margin}
2053 @itemx --indent=@var{margin}
2056 @cindex indenting lines
2058 Indent each line with a margin @var{margin} spaces wide (default is zero).
2059 The total page width is the size of the margin plus the @var{page_width}
2060 set with the @option{-W/-w} option. A limited overflow may occur with
2061 numbered single column output (compare @option{-n} option).
2064 @itemx --no-file-warnings
2066 @opindex --no-file-warnings
2067 Do not print a warning message when an argument @var{file} cannot be
2068 opened. (The exit status will still be nonzero, however.)
2070 @item -s[@var{char}]
2071 @itemx --separator[=@var{char}]
2073 @opindex --separator
2074 Separate columns by a single character @var{char}. The default for
2075 @var{char} is the TAB character without @option{-w} and @samp{no
2076 character} with @option{-w}. Without @option{-s} the default separator
2077 @samp{space} is set. @option{-s[char]} turns off line truncation of all
2078 three column options (@option{-COLUMN}|@option{-a -COLUMN}|@option{-m}) unless
2079 @option{-w} is set. This is a @acronym{POSIX}-compliant formulation.
2082 @item -S @var{string}
2083 @itemx --sep-string[=@var{string}]
2085 @opindex --sep-string
2086 Use @var{string} to separate output columns. The @option{-S} option doesn't
2087 affect the @option{-W/-w} option, unlike the @option{-s} option which does. It
2088 does not affect line truncation or column alignment.
2089 Without @option{-S}, and with @option{-J}, @command{pr} uses the default output
2091 Without @option{-S} or @option{-J}, @command{pr} uses a @samp{space}
2092 (same as @option{-S"@w{ }"}). With @option{-S@var{string}},
2093 @var{string} must be nonempty; @option{--sep-string} with no
2094 @var{string} is equivalent to @option{--sep-string=""}.
2096 On older systems, @command{pr} instead supports an obsolete option
2097 @option{-S[@var{string}]}, where @var{string} is optional. @acronym{POSIX}
2098 1003.1-2001 (@pxref{Standards conformance}) does not allow this older
2099 usage. To specify an empty @var{string} portably, use
2100 @option{--sep-string}.
2103 @itemx --omit-header
2105 @opindex --omit-header
2106 Do not print the usual header [and footer] on each page, and do not fill
2107 out the bottom of pages (with blank lines or a form feed). No page
2108 structure is produced, but form feeds set in the input files are retained.
2109 The predefined pagination is not changed. @option{-t} or @option{-T} may be
2110 useful together with other options; e.g.: @option{-t -e4}, expand TAB characters
2111 in the input file to 4 spaces but don't make any other changes. Use of
2112 @option{-t} overrides @option{-h}.
2115 @itemx --omit-pagination
2117 @opindex --omit-pagination
2118 Do not print header [and footer]. In addition eliminate all form feeds
2119 set in the input files.
2122 @itemx --show-nonprinting
2124 @opindex --show-nonprinting
2125 Print nonprinting characters in octal backslash notation.
2127 @item -w @var{page_width}
2128 @itemx --width=@var{page_width}
2131 Set page width to @var{page_width} characters for multiple text-column
2132 output only (default for @var{page_width} is 72). @option{-s[CHAR]} turns
2133 off the default page width and any line truncation and column alignment.
2134 Lines of full length are merged, regardless of the column options
2135 set. No @var{page_width} setting is possible with single column output.
2136 A @acronym{POSIX}-compliant formulation.
2138 @item -W @var{page_width}
2139 @itemx --page_width=@var{page_width}
2141 @opindex --page_width
2142 Set the page width to @var{page_width} characters. That's valid with and
2143 without a column option. Text lines are truncated, unless @option{-J}
2144 is used. Together with one of the three column options
2145 (@option{-@var{column}}, @option{-a -@var{column}} or @option{-m}) column
2146 alignment is always used. The separator options @option{-S} or @option{-s}
2147 don't affect the @option{-W} option. Default is 72 characters. Without
2148 @option{-W @var{page_width}} and without any of the column options NO line
2149 truncation is used (defined to keep downward compatibility and to meet
2150 most frequent tasks). That's equivalent to @option{-W 72 -J}. The header
2151 line is never truncated.
2158 @node fold invocation
2159 @section @command{fold}: Wrap input lines to fit in specified width
2162 @cindex wrapping long input lines
2163 @cindex folding long input lines
2165 @command{fold} writes each @var{file} (@option{-} means standard input), or
2166 standard input if none are given, to standard output, breaking long
2170 fold [@var{option}]@dots{} [@var{file}]@dots{}
2173 By default, @command{fold} breaks lines wider than 80 columns. The output
2174 is split into as many lines as necessary.
2176 @cindex screen columns
2177 @command{fold} counts screen columns by default; thus, a tab may count more
2178 than one column, backspace decreases the column count, and carriage
2179 return sets the column to zero.
2181 The program accepts the following options. Also see @ref{Common options}.
2189 Count bytes rather than columns, so that tabs, backspaces, and carriage
2190 returns are each counted as taking up one column, just like other
2197 Break at word boundaries: the line is broken after the last blank before
2198 the maximum line length. If the line contains no such blanks, the line
2199 is broken at the maximum line length as usual.
2201 @item -w @var{width}
2202 @itemx --width=@var{width}
2205 Use a maximum line length of @var{width} columns instead of 80.
2207 On older systems, @command{fold} supports an obsolete option
2208 @option{-@var{width}}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards
2209 conformance}) does not allow this; use @option{-w @var{width}}
2217 @node Output of parts of files
2218 @chapter Output of parts of files
2220 @cindex output of parts of files
2221 @cindex parts of files, output of
2223 These commands output pieces of the input.
2226 * head invocation:: Output the first part of files.
2227 * tail invocation:: Output the last part of files.
2228 * split invocation:: Split a file into fixed-size pieces.
2229 * csplit invocation:: Split a file into context-determined pieces.
2232 @node head invocation
2233 @section @command{head}: Output the first part of files
2236 @cindex initial part of files, outputting
2237 @cindex first part of files, outputting
2239 @command{head} prints the first part (10 lines by default) of each
2240 @var{file}; it reads from standard input if no files are given or
2241 when given a @var{file} of @option{-}. Synopsis:
2244 head [@var{option}]@dots{} [@var{file}]@dots{}
2247 If more than one @var{file} is specified, @command{head} prints a
2248 one-line header consisting of
2250 ==> @var{file name} <==
2253 before the output for each @var{file}.
2255 The program accepts the following options. Also see @ref{Common options}.
2260 @itemx --bytes=@var{n}
2263 Print the first @var{n} bytes, instead of initial lines. Appending
2264 @samp{b} multiplies @var{n} by 512, @samp{k} by 1024, and @samp{m}
2266 However, if @var{n} starts with a @samp{-},
2267 print all but the last @var{n} bytes of each file.
2270 @itemx --lines=@var{n}
2273 Output the first @var{n} lines.
2274 However, if @var{n} starts with a @samp{-},
2275 print all but the last @var{n} lines of each file.
2283 Never print file name headers.
2289 Always print file name headers.
2293 On older systems, @command{head} supports an obsolete option
2294 @option{-@var{count}@var{options}}, which is recognized only if it is
2295 specified first. @var{count} is a decimal number optionally followed
2296 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
2297 @samp{l} to mean count by lines, or other option letters (@samp{cqv}).
2298 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow
2299 this; use @option{-c @var{count}} or @option{-n @var{count}} instead.
2304 @node tail invocation
2305 @section @command{tail}: Output the last part of files
2308 @cindex last part of files, outputting
2310 @command{tail} prints the last part (10 lines by default) of each
2311 @var{file}; it reads from standard input if no files are given or
2312 when given a @var{file} of @samp{-}. Synopsis:
2315 tail [@var{option}]@dots{} [@var{file}]@dots{}
2318 If more than one @var{file} is specified, @command{tail} prints a
2319 one-line header consisting of
2321 ==> @var{file name} <==
2324 before the output for each @var{file}.
2326 @cindex BSD @command{tail}
2327 @sc{gnu} @command{tail} can output any amount of data (some other versions of
2328 @command{tail} cannot). It also has no @option{-r} option (print in
2329 reverse), since reversing a file is really a different job from printing
2330 the end of a file; BSD @command{tail} (which is the one with @option{-r}) can
2331 only reverse files that are at most as large as its buffer, which is
2332 typically 32 KiB. A more reliable and versatile way to reverse files is
2333 the @sc{gnu} @command{tac} command.
2335 If any option-argument is a number @var{n} starting with a @samp{+},
2336 @command{tail} begins printing with the @var{n}th item from the start of
2337 each file, instead of from the end.
2339 The program accepts the following options. Also see @ref{Common options}.
2343 @item -c @var{bytes}
2344 @itemx --bytes=@var{bytes}
2347 Output the last @var{bytes} bytes, instead of final lines. Appending
2348 @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and @samp{m}
2352 @itemx --follow[=@var{how}]
2355 @cindex growing files
2356 @vindex name @r{follow option}
2357 @vindex descriptor @r{follow option}
2358 Loop forever trying to read more characters at the end of the file,
2359 presumably because the file is growing. This option is ignored when
2360 reading from a pipe.
2361 If more than one file is given, @command{tail} prints a header whenever it
2362 gets output from a different file, to indicate which file that output is
2365 There are two ways to specify how you'd like to track files with this option,
2366 but that difference is noticeable only when a followed file is removed or
2368 If you'd like to continue to track the end of a growing file even after
2369 it has been unlinked, use @option{--follow=descriptor}. This is the default
2370 behavior, but it is not useful if you're tracking a log file that may be
2371 rotated (removed or renamed, then reopened). In that case, use
2372 @option{--follow=name} to track the named file by reopening it periodically
2373 to see if it has been removed and recreated by some other program.
2375 No matter which method you use, if the tracked file is determined to have
2376 shrunk, @command{tail} prints a message saying the file has been truncated
2377 and resumes tracking the end of the file from the newly-determined endpoint.
2379 When a file is removed, @command{tail}'s behavior depends on whether it is
2380 following the name or the descriptor. When following by name, tail can
2381 detect that a file has been removed and gives a message to that effect,
2382 and if @option{--retry} has been specified it will continue checking
2383 periodically to see if the file reappears.
2384 When following a descriptor, tail does not detect that the file has
2385 been unlinked or renamed and issues no message; even though the file
2386 may no longer be accessible via its original name, it may still be
2389 The option values @samp{descriptor} and @samp{name} may be specified only
2390 with the long form of the option, not with @option{-f}.
2394 This option is the same as @option{--follow=name --retry}. That is, tail
2395 will attempt to reopen a file when it is removed. Should this fail, tail
2396 will keep trying until it becomes accessible again.
2400 This option is meaningful only when following by name.
2401 Without this option, when tail encounters a file that doesn't
2402 exist or is otherwise inaccessible, it reports that fact and
2403 never checks it again.
2405 @itemx --sleep-interval=@var{number}
2406 @opindex --sleep-interval
2407 Change the number of seconds to wait between iterations (the default is 1.0).
2408 During one iteration, every specified file is checked to see if it has
2410 Historical implementations of @command{tail} have required that
2411 @var{number} be an integer. However, GNU @command{tail} accepts
2412 an arbitrary floating point number (using a period before any
2415 @itemx --pid=@var{pid}
2417 When following by name or by descriptor, you may specify the process ID,
2418 @var{pid}, of the sole writer of all @var{file} arguments. Then, shortly
2419 after that process terminates, tail will also terminate. This will
2420 work properly only if the writer and the tailing process are running on
2421 the same machine. For example, to save the output of a build in a file
2422 and to watch the file grow, if you invoke @command{make} and @command{tail}
2423 like this then the tail process will stop when your build completes.
2424 Without this option, you would have had to kill the @code{tail -f}
2427 $ make >& makerr & tail --pid=$! -f makerr
2429 If you specify a @var{pid} that is not in use or that does not correspond
2430 to the process that is writing to the tailed files, then @command{tail}
2431 may terminate long before any @var{file}s stop growing or it may not
2432 terminate until long after the real writer has terminated.
2433 Note that @option{--pid} cannot be supported on some systems; @command{tail}
2434 will print a warning if this is the case.
2436 @itemx --max-unchanged-stats=@var{n}
2437 @opindex --max-unchanged-stats
2438 When tailing a file by name, if there have been @var{n} (default
2439 n=@value{DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS}) consecutive
2440 iterations for which the size has remained the same, then
2441 @code{open}/@code{fstat} the file to determine if that file name is
2442 still associated with the same device/inode-number pair as before.
2443 When following a log file that is rotated, this is approximately the
2444 number of seconds between when tail prints the last pre-rotation lines
2445 and when it prints the lines that have accumulated in the new log file.
2446 This option is meaningful only when following by name.
2449 @itemx --lines=@var{n}
2452 Output the last @var{n} lines.
2460 Never print file name headers.
2466 Always print file name headers.
2470 On older systems, @command{tail} supports an obsolete option
2471 @option{-@var{count}@var{options}}, which is recognized only if it is
2472 specified first. @var{count} is a decimal number optionally followed
2473 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
2474 @samp{l} to mean count by lines, or other option letters
2475 (@samp{cfqv}). Some older @command{tail} implementations also support
2476 an obsolete option @option{+@var{count}} with the same meaning as
2477 @option{-+@var{count}}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards
2478 conformance}) does not allow these options; use @option{-c
2479 @var{count}} or @option{-n @var{count}} instead.
2484 @node split invocation
2485 @section @command{split}: Split a file into fixed-size pieces
2488 @cindex splitting a file into pieces
2489 @cindex pieces, splitting a file into
2491 @command{split} creates output files containing consecutive sections of
2492 @var{input} (standard input if none is given or @var{input} is
2493 @samp{-}). Synopsis:
2496 split [@var{option}] [@var{input} [@var{prefix}]]
2499 By default, @command{split} puts 1000 lines of @var{input} (or whatever is
2500 left over for the last section), into each output file.
2502 @cindex output file name prefix
2503 The output files' names consist of @var{prefix} (@samp{x} by default)
2504 followed by a group of characters (@samp{aa}, @samp{ab}, @dots{} by
2505 default), such that concatenating the output files in traditional
2506 sorted order by file name produces
2507 the original input file. If the output file names are exhausted,
2508 @command{split} reports an error without deleting the output files
2511 The program accepts the following options. Also see @ref{Common options}.
2515 @item -a @var{length}
2516 @itemx --suffix-length=@var{length}
2518 @opindex --suffix-length
2519 Use suffixes of length @var{length}. The default @var{length} is 2.
2521 @item -l @var{lines}
2522 @itemx --lines=@var{lines}
2525 Put @var{lines} lines of @var{input} into each output file.
2527 On older systems, @command{split} supports an obsolete option
2528 @option{-@var{lines}}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards
2529 conformance}) does not allow this; use @option{-l @var{lines}}
2532 @item -b @var{bytes}
2533 @itemx --bytes=@var{bytes}
2536 Put the first @var{bytes} bytes of @var{input} into each output file.
2537 Appending @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and
2538 @samp{m} by 1048576.
2540 @item -C @var{bytes}
2541 @itemx --line-bytes=@var{bytes}
2543 @opindex --line-bytes
2544 Put into each output file as many complete lines of @var{input} as
2545 possible without exceeding @var{bytes} bytes. For lines longer than
2546 @var{bytes} bytes, put @var{bytes} bytes into each output file until
2547 less than @var{bytes} bytes of the line are left, then continue
2548 normally. @var{bytes} has the same format as for the @option{--bytes}
2552 @itemx --numeric-suffixes
2554 @opindex --numeric-suffixes
2555 Use digits in suffixes rather than lower-case letters.
2559 Write a diagnostic to standard error just before each output file is opened.
2566 @node csplit invocation
2567 @section @command{csplit}: Split a file into context-determined pieces
2570 @cindex context splitting
2571 @cindex splitting a file into pieces by context
2573 @command{csplit} creates zero or more output files containing sections of
2574 @var{input} (standard input if @var{input} is @samp{-}). Synopsis:
2577 csplit [@var{option}]@dots{} @var{input} @var{pattern}@dots{}
2580 The contents of the output files are determined by the @var{pattern}
2581 arguments, as detailed below. An error occurs if a @var{pattern}
2582 argument refers to a nonexistent line of the input file (e.g., if no
2583 remaining line matches a given regular expression). After every
2584 @var{pattern} has been matched, any remaining input is copied into one
2587 By default, @command{csplit} prints the number of bytes written to each
2588 output file after it has been created.
2590 The types of pattern arguments are:
2595 Create an output file containing the input up to but not including line
2596 @var{n} (a positive integer). If followed by a repeat count, also
2597 create an output file containing the next @var{n} lines of the input
2598 file once for each repeat.
2600 @item /@var{regexp}/[@var{offset}]
2601 Create an output file containing the current line up to (but not
2602 including) the next line of the input file that contains a match for
2603 @var{regexp}. The optional @var{offset} is an integer.
2604 If it is given, the input up to (but not including) the
2605 matching line plus or minus @var{offset} is put into the output file,
2606 and the line after that begins the next section of input.
2608 @item %@var{regexp}%[@var{offset}]
2609 Like the previous type, except that it does not create an output
2610 file, so that section of the input file is effectively ignored.
2612 @item @{@var{repeat-count}@}
2613 Repeat the previous pattern @var{repeat-count} additional
2614 times. @var{repeat-count} can either be a positive integer or an
2615 asterisk, meaning repeat as many times as necessary until the input is
2620 The output files' names consist of a prefix (@samp{xx} by default)
2621 followed by a suffix. By default, the suffix is an ascending sequence
2622 of two-digit decimal numbers from @samp{00} to @samp{99}. In any case,
2623 concatenating the output files in sorted order by filename produces the
2624 original input file.
2626 By default, if @command{csplit} encounters an error or receives a hangup,
2627 interrupt, quit, or terminate signal, it removes any output files
2628 that it has created so far before it exits.
2630 The program accepts the following options. Also see @ref{Common options}.
2634 @item -f @var{prefix}
2635 @itemx --prefix=@var{prefix}
2638 @cindex output file name prefix
2639 Use @var{prefix} as the output file name prefix.
2641 @item -b @var{suffix}
2642 @itemx --suffix=@var{suffix}
2645 @cindex output file name suffix
2646 Use @var{suffix} as the output file name suffix. When this option is
2647 specified, the suffix string must include exactly one
2648 @code{printf(3)}-style conversion specification, possibly including
2649 format specification flags, a field width, a precision specifications,
2650 or all of these kinds of modifiers. The format letter must convert a
2651 binary integer argument to readable form; thus, only @samp{d}, @samp{i},
2652 @samp{u}, @samp{o}, @samp{x}, and @samp{X} conversions are allowed. The
2653 entire @var{suffix} is given (with the current output file number) to
2654 @code{sprintf(3)} to form the file name suffixes for each of the
2655 individual output files in turn. If this option is used, the
2656 @option{--digits} option is ignored.
2658 @item -n @var{digits}
2659 @itemx --digits=@var{digits}
2662 Use output file names containing numbers that are @var{digits} digits
2663 long instead of the default 2.
2668 @opindex --keep-files
2669 Do not remove output files when errors are encountered.
2672 @itemx --elide-empty-files
2674 @opindex --elide-empty-files
2675 Suppress the generation of zero-length output files. (In cases where
2676 the section delimiters of the input file are supposed to mark the first
2677 lines of each of the sections, the first output file will generally be a
2678 zero-length file unless you use this option.) The output file sequence
2679 numbers always run consecutively starting from 0, even when this option
2690 Do not print counts of output file sizes.
2697 @node Summarizing files
2698 @chapter Summarizing files
2700 @cindex summarizing files
2702 These commands generate just a few numbers representing entire
2706 * wc invocation:: Print newline, word, and byte counts.
2707 * sum invocation:: Print checksum and block counts.
2708 * cksum invocation:: Print CRC checksum and byte counts.
2709 * md5sum invocation:: Print or check message-digests.
2714 @section @command{wc}: Print newline, word, and byte counts
2718 @cindex character count
2722 @command{wc} counts the number of bytes, characters, whitespace-separated
2723 words, and newlines in each given @var{file}, or standard input if none
2724 are given or for a @var{file} of @samp{-}. Synopsis:
2727 wc [@var{option}]@dots{} [@var{file}]@dots{}
2730 @cindex total counts
2731 @command{wc} prints one line of counts for each file, and if the file was
2732 given as an argument, it prints the file name following the counts. If
2733 more than one @var{file} is given, @command{wc} prints a final line
2734 containing the cumulative counts, with the file name @file{total}. The
2735 counts are printed in this order: newlines, words, characters, bytes.
2736 Each count is printed right-justified in a field with at least one
2737 space between fields so that the numbers and file names normally line
2738 up nicely in columns. The width of the count fields varies depending
2739 on the inputs, so you should not depend on a particular field width.
2740 However, as a @acronym{GNU} extension, if only one count is printed,
2741 it is guaranteed to be printed without leading spaces.
2743 By default, @command{wc} prints three counts: the newline, words, and byte
2744 counts. Options can specify that only certain counts be printed.
2745 Options do not undo others previously given, so
2752 prints both the byte counts and the word counts.
2754 With the @option{--max-line-length} option, @command{wc} prints the length
2755 of the longest line per file, and if there is more than one file it
2756 prints the maximum (not the sum) of those lengths.
2758 The program accepts the following options. Also see @ref{Common options}.
2766 Print only the byte counts.
2772 Print only the character counts.
2778 Print only the word counts.
2784 Print only the newline counts.
2787 @itemx --max-line-length
2789 @opindex --max-line-length
2790 Print only the maximum line lengths.
2797 @node sum invocation
2798 @section @command{sum}: Print checksum and block counts
2801 @cindex 16-bit checksum
2802 @cindex checksum, 16-bit
2804 @command{sum} computes a 16-bit checksum for each given @var{file}, or
2805 standard input if none are given or for a @var{file} of @samp{-}. Synopsis:
2808 sum [@var{option}]@dots{} [@var{file}]@dots{}
2811 @command{sum} prints the checksum for each @var{file} followed by the
2812 number of blocks in the file (rounded up). If more than one @var{file}
2813 is given, file names are also printed (by default). (With the
2814 @option{--sysv} option, corresponding file names are printed when there is
2815 at least one file argument.)
2817 By default, @sc{gnu} @command{sum} computes checksums using an algorithm
2818 compatible with BSD @command{sum} and prints file sizes in units of
2821 The program accepts the following options. Also see @ref{Common options}.
2827 @cindex BSD @command{sum}
2828 Use the default (BSD compatible) algorithm. This option is included for
2829 compatibility with the System V @command{sum}. Unless @option{-s} was also
2830 given, it has no effect.
2836 @cindex System V @command{sum}
2837 Compute checksums using an algorithm compatible with System V
2838 @command{sum}'s default, and print file sizes in units of 512-byte blocks.
2842 @command{sum} is provided for compatibility; the @command{cksum} program (see
2843 next section) is preferable in new applications.
2848 @node cksum invocation
2849 @section @command{cksum}: Print CRC checksum and byte counts
2852 @cindex cyclic redundancy check
2853 @cindex CRC checksum
2855 @command{cksum} computes a cyclic redundancy check (CRC) checksum for each
2856 given @var{file}, or standard input if none are given or for a
2857 @var{file} of @samp{-}. Synopsis:
2860 cksum [@var{option}]@dots{} [@var{file}]@dots{}
2863 @command{cksum} prints the CRC checksum for each file along with the number
2864 of bytes in the file, and the filename unless no arguments were given.
2866 @command{cksum} is typically used to ensure that files
2867 transferred by unreliable means (e.g., netnews) have not been corrupted,
2868 by comparing the @command{cksum} output for the received files with the
2869 @command{cksum} output for the original files (typically given in the
2872 The CRC algorithm is specified by the @acronym{POSIX} standard. It is not
2873 compatible with the BSD or System V @command{sum} algorithms (see the
2874 previous section); it is more robust.
2876 The only options are @option{--help} and @option{--version}. @xref{Common
2882 @node md5sum invocation
2883 @section @command{md5sum}: Print or check message-digests
2886 @cindex 128-bit checksum
2887 @cindex checksum, 128-bit
2888 @cindex fingerprint, 128-bit
2889 @cindex message-digest, 128-bit
2891 @command{md5sum} computes a 128-bit checksum (or @dfn{fingerprint} or
2892 @dfn{message-digest}) for each specified @var{file}.
2893 If a @var{file} is specified as @samp{-} or if no files are given
2894 @command{md5sum} computes the checksum for the standard input.
2895 @command{md5sum} can also determine whether a file and checksum are
2896 consistent. Synopses:
2899 md5sum [@var{option}]@dots{} [@var{file}]@dots{}
2900 md5sum [@var{option}]@dots{} --check [@var{file}]
2903 For each @var{file}, @samp{md5sum} outputs the MD5 checksum, a flag
2904 indicating a binary or text input file, and the filename.
2905 If @var{file} is omitted or specified as @samp{-}, standard input is read.
2907 The program accepts the following options. Also see @ref{Common options}.
2915 @cindex binary input files
2916 Treat all input files as binary. This option has no effect on Unix
2917 systems, since they don't distinguish between binary and text files.
2918 This option is useful on systems that have different internal and
2919 external character representations. On MS-DOS and MS-Windows, this is
2924 Read filenames and checksum information from the single @var{file}
2925 (or from stdin if no @var{file} was specified) and report whether
2926 each named file and the corresponding checksum data are consistent.
2927 The input to this mode of @command{md5sum} is usually the output of
2928 a prior, checksum-generating run of @samp{md5sum}.
2929 Each valid line of input consists of an MD5 checksum, a binary/text
2930 flag, and then a filename.
2931 Binary files are marked with @samp{*}, text with @samp{ }.
2932 For each such line, @command{md5sum} reads the named file and computes its
2933 MD5 checksum. Then, if the computed message digest does not match the
2934 one on the line with the filename, the file is noted as having
2935 failed the test. Otherwise, the file passes the test.
2936 By default, for each valid line, one line is written to standard
2937 output indicating whether the named file passed the test.
2938 After all checks have been performed, if there were any failures,
2939 a warning is issued to standard error.
2940 Use the @option{--status} option to inhibit that output.
2941 If any listed file cannot be opened or read, if any valid line has
2942 an MD5 checksum inconsistent with the associated file, or if no valid
2943 line is found, @command{md5sum} exits with nonzero status. Otherwise,
2944 it exits successfully.
2948 @cindex verifying MD5 checksums
2949 This option is useful only when verifying checksums.
2950 When verifying checksums, don't generate the default one-line-per-file
2951 diagnostic and don't output the warning summarizing any failures.
2952 Failures to open or read a file still evoke individual diagnostics to
2954 If all listed files are readable and are consistent with the associated
2955 MD5 checksums, exit successfully. Otherwise exit with a status code
2956 indicating there was a failure.
2962 @cindex text input files
2963 Treat all input files as text files. This is the reverse of
2970 @cindex verifying MD5 checksums
2971 When verifying checksums, warn about improperly formatted MD5 checksum lines.
2972 This option is useful only if all but a few lines in the checked input
2980 @node Operating on sorted files
2981 @chapter Operating on sorted files
2983 @cindex operating on sorted files
2984 @cindex sorted files, operations on
2986 These commands work with (or produce) sorted files.
2989 * sort invocation:: Sort text files.
2990 * uniq invocation:: Uniquify files.
2991 * comm invocation:: Compare two sorted files line by line.
2992 * ptx invocation:: Produce a permuted index of file contents.
2993 * tsort invocation:: Topological sort.
2994 * tsort background:: Where tsort came from.
2998 @node sort invocation
2999 @section @command{sort}: Sort text files
3002 @cindex sorting files
3004 @command{sort} sorts, merges, or compares all the lines from the given
3005 files, or standard input if none are given or for a @var{file} of
3006 @samp{-}. By default, @command{sort} writes the results to standard
3010 sort [@var{option}]@dots{} [@var{file}]@dots{}
3013 @command{sort} has three modes of operation: sort (the default), merge,
3014 and check for sortedness. The following options change the operation
3023 @cindex checking for sortedness
3024 Check whether the given files are already sorted: if they are not all
3025 sorted, print an error message and exit with a status of 1.
3026 Otherwise, exit successfully.
3032 @cindex merging sorted files
3033 Merge the given files by sorting them as a group. Each input file must
3034 always be individually sorted. It always works to sort instead of
3035 merge; merging is provided because it is faster, in the case where it
3042 A pair of lines is compared as follows: if any key fields have
3043 been specified, @command{sort} compares each pair of fields, in the
3044 order specified on the command line, according to the associated
3045 ordering options, until a difference is found or no fields are left.
3046 Unless otherwise specified, all comparisons use the character collating
3047 sequence specified by the @env{LC_COLLATE} locale. @footnote{If you
3048 use a non-@acronym{POSIX} locale (e.g., by setting @env{LC_ALL}
3049 to @samp{en_US}), then @command{sort} may produce output that is sorted
3050 differently than you're accustomed to. In that case, set the @env{LC_ALL}
3051 environment variable to @samp{C}. Note that setting only @env{LC_COLLATE}
3052 has two problems. First, it is ineffective if @env{LC_ALL} is also set.
3053 Second, it has undefined behavior if @env{LC_CTYPE} (or @env{LANG}, if
3054 @env{LC_CTYPE} is unset) is set to an incompatible value. For example,
3055 you get undefined behavior if @env{LC_CTYPE} is @code{ja_JP.PCK} but
3056 @env{LC_COLLATE} is @code{en_US.UTF-8}. }
3058 If any of the global options @samp{bdfgiMnr} are given but no key fields
3059 are specified, @command{sort} compares the entire lines according to the
3062 Finally, as a last resort when all keys compare equal (or if no ordering
3063 options were specified at all), @command{sort} compares the entire lines.
3064 The last resort comparison honors the @option{--reverse} (@option{-r})
3065 global option. The @option{--stable} (@option{-s}) option disables this
3066 last-resort comparison so that lines in which all fields compare equal
3067 are left in their original relative order. If no fields or global
3068 options are specified, @option{--stable} (@option{-s}) has no effect.
3070 @sc{gnu} @command{sort} (as specified for all @sc{gnu} utilities) has no
3071 limit on input line length or restrictions on bytes allowed within lines.
3072 In addition, if the final byte of an input file is not a newline, @sc{gnu}
3073 @command{sort} silently supplies one. A line's trailing newline is not
3074 part of the line for comparison purposes.
3076 @cindex exit status of @command{sort}
3080 0 if no error occurred
3081 1 if invoked with @option{-c} and the input is not properly sorted
3082 2 if an error occurred
3086 If the environment variable @env{TMPDIR} is set, @command{sort} uses its
3087 value as the directory for temporary files instead of @file{/tmp}. The
3088 @option{--temporary-directory} (@option{-T}) option in turn overrides
3089 the environment variable.
3092 The following options affect the ordering of output lines. They may be
3093 specified globally or as part of a specific key field. If no key
3094 fields are specified, global options apply to comparison of entire
3095 lines; otherwise the global options are inherited by key fields that do
3096 not specify any special options of their own. In pre-@acronym{POSIX}
3097 versions of @command{sort}, global options affect only later key fields,
3098 so portable shell scripts should specify global options first.
3103 @itemx --ignore-leading-blanks
3105 @opindex --ignore-leading-blanks
3106 @cindex blanks, ignoring leading
3108 Ignore leading blanks when finding sort keys in each line.
3109 The @env{LC_CTYPE} locale determines character types.
3112 @itemx --dictionary-order
3114 @opindex --dictionary-order
3115 @cindex dictionary order
3116 @cindex phone directory order
3117 @cindex telephone directory order
3119 Sort in @dfn{phone directory} order: ignore all characters except
3120 letters, digits and blanks when sorting.
3121 The @env{LC_CTYPE} locale determines character types.
3124 @itemx --ignore-case
3126 @opindex --ignore-case
3127 @cindex ignoring case
3128 @cindex case folding
3130 Fold lowercase characters into the equivalent uppercase characters when
3131 comparing so that, for example, @samp{b} and @samp{B} sort as equal.
3132 The @env{LC_CTYPE} locale determines character types.
3135 @itemx --general-numeric-sort
3137 @opindex --general-numeric-sort
3138 @cindex general numeric sort
3140 Sort numerically, using the standard C function @code{strtod} to convert
3141 a prefix of each line to a double-precision floating point number.
3142 This allows floating point numbers to be specified in scientific notation,
3143 like @code{1.0e-34} and @code{10e100}.
3144 The @env{LC_NUMERIC} locale determines the decimal-point character.
3145 Do not report overflow, underflow, or conversion errors.
3146 Use the following collating sequence:
3150 Lines that do not start with numbers (all considered to be equal).
3152 NaNs (``Not a Number'' values, in IEEE floating point arithmetic)
3153 in a consistent but machine-dependent order.
3157 Finite numbers in ascending numeric order (with @math{-0} and @math{+0} equal).
3162 Use this option only if there is no alternative; it is much slower than
3163 @option{--numeric-sort} (@option{-n}) and it can lose information when
3164 converting to floating point.
3167 @itemx --ignore-nonprinting
3169 @opindex --ignore-nonprinting
3170 @cindex nonprinting characters, ignoring
3171 @cindex unprintable characters, ignoring
3173 Ignore nonprinting characters.
3174 The @env{LC_CTYPE} locale determines character types.
3175 This option has no effect if the stronger @option{--dictionary-order}
3176 (@option{-d}) option is also given.
3181 @opindex --month-sort
3182 @cindex months, sorting by
3184 An initial string, consisting of any amount of blanks, followed
3185 by a month name abbreviation, is folded to UPPER case and
3186 compared in the order @samp{JAN} < @samp{FEB} < @dots{} < @samp{DEC}.
3187 Invalid names compare low to valid names. The @env{LC_TIME} locale
3188 category determines the month spellings.
3191 @itemx --numeric-sort
3193 @opindex --numeric-sort
3194 @cindex numeric sort
3196 Sort numerically: the number begins each line; specifically, it consists
3197 of optional blanks, an optional @samp{-} sign, and zero or more
3198 digits possibly separated by thousands separators, optionally followed
3199 by a decimal-point character and zero or more digits. The @env{LC_NUMERIC}
3200 locale specifies the decimal-point character and thousands separator.
3202 Numeric sort uses what might be considered an unconventional method to
3203 compare strings representing floating point numbers. Rather than first
3204 converting each string to the C @code{double} type and then comparing
3205 those values, @command{sort} aligns the decimal-point characters in the
3206 two strings and compares the strings a character at a time. One benefit
3207 of using this approach is its speed. In practice this is much more
3208 efficient than performing the two corresponding string-to-double (or
3209 even string-to-integer) conversions and then comparing doubles. In
3210 addition, there is no corresponding loss of precision. Converting each
3211 string to @code{double} before comparison would limit precision to about
3212 16 digits on most systems.
3214 Neither a leading @samp{+} nor exponential notation is recognized.
3215 To compare such strings numerically, use the
3216 @option{--general-numeric-sort} (@option{-g}) option.
3222 @cindex reverse sorting
3223 Reverse the result of comparison, so that lines with greater key values
3224 appear earlier in the output instead of later.
3232 @item -o @var{output-file}
3233 @itemx --output=@var{output-file}
3236 @cindex overwriting of input, allowed
3237 Write output to @var{output-file} instead of standard output.
3238 If necessary, @command{sort} reads input before opening
3239 @var{output-file}, so you can safely sort a file in place by using
3240 commands like @code{sort -o F F} and @code{cat F | sort -o F}.
3242 @vindex POSIXLY_CORRECT
3243 On newer systems, @option{-o} cannot appear after an input file if
3244 @env{POSIXLY_CORRECT} is set, e.g., @samp{sort F -o F}. Portable
3245 scripts should specify @option{-o @var{output-file}} before any input
3252 @cindex sort stability
3253 @cindex disabling sort's last-resort comparison
3255 Make @command{sort} stable by disabling the last-resort
3256 comparison that is performed in some cases.
3257 By default, when lines compare equal based on command line options
3258 that affect ordering, those lines are ordered using
3259 a @dfn{last-resort comparison} that takes the entire
3260 line as the key and acts as if no ordering options were specified.
3261 But if @option{--reverse} (@option{-r}) was specified along with other
3262 ordering options, then the last-resort comparison does use @option{--reverse}.
3263 In any case, when no ordering option is specified or when only
3264 @option{--reverse} is specified, the last-resort comparison is not performed
3267 @itemx --buffer-size=@var{size}
3269 @opindex --buffer-size
3270 @cindex size for main memory sorting
3271 Use a main-memory sort buffer of the given @var{size}. By default,
3272 @var{size} is in units of 1024 bytes. Appending @samp{%} causes
3273 @var{size} to be interpreted as a percentage of physical memory.
3274 Appending @samp{K} multiplies @var{size} by 1024 (the default),
3275 @samp{M} by 1,048,576, @samp{G} by 1,073,741,824, and so on for
3276 @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}. Appending
3277 @samp{b} causes @var{size} to be interpreted as a byte count, with no
3280 This option can improve the performance of @command{sort} by causing it
3281 to start with a larger or smaller sort buffer than the default.
3282 However, this option affects only the initial buffer size. The buffer
3283 grows beyond @var{size} if @command{sort} encounters input lines larger
3286 @item -t @var{separator}
3287 @itemx --field-separator=@var{separator}
3289 @opindex --field-separator
3290 @cindex field separator character
3291 Use character @var{separator} as the field separator when finding the
3292 sort keys in each line. By default, fields are separated by the empty
3293 string between a non-blank character and a blank character.
3294 That is, given the input line @w{@samp{ foo bar}}, @command{sort} breaks it
3295 into fields @w{@samp{ foo}} and @w{@samp{ bar}}. The field separator is
3296 not considered to be part of either the field preceding or the field
3297 following. But note that sort fields that extend to the end of the line,
3298 as @option{-k 2}, or sort fields consisting of a range, as @option{-k 2,3},
3299 retain the field separators present between the endpoints of the range.
3301 To specify a zero byte (@acronym{ASCII} @sc{nul} (Null) character) as
3302 the field separator, use the two-character string @samp{\0}, e.g.,
3303 @samp{sort -t '\0'}.
3305 @item -T @var{tempdir}
3306 @itemx --temporary-directory=@var{tempdir}
3308 @opindex --temporary-directory
3309 @cindex temporary directory
3311 Use directory @var{tempdir} to store temporary files, overriding the
3312 @env{TMPDIR} environment variable. If this option is given more than
3313 once, temporary files are stored in all the directories given. If you
3314 have a large sort or merge that is I/O-bound, you can often improve
3315 performance by using this option to specify directories on different
3316 disks and controllers.
3322 @cindex uniquifying output
3324 Normally, output only the first of a sequence of lines that compare
3325 equal. For the @option{--check} (@option{-c}) option,
3326 check that no pair of consecutive lines compares equal.
3328 @item -k @var{pos1}[,@var{pos2}]
3329 @itemx --key=@var{pos1}[,@var{pos2}]
3333 Specify a sort field that consists of the part of the line between
3334 @var{pos1} and @var{pos2} (or the end of the line, if @var{pos2} is
3335 omitted), @emph{inclusive}. Fields and character positions are numbered
3336 starting with 1. So to sort on the second field, you'd use
3337 @option{--key=2,2} (@option{-k 2,2}). See below for more examples.
3340 @itemx --zero-terminated
3342 @opindex --zero-terminated
3343 @cindex sort zero-terminated lines
3344 Treat the input as a set of lines, each terminated by a zero byte
3345 (@acronym{ASCII} @sc{nul} (Null) character) instead of an
3346 @acronym{ASCII} @sc{lf} (Line Feed).
3347 This option can be useful in conjunction with @samp{perl -0} or
3348 @samp{find -print0} and @samp{xargs -0} which do the same in order to
3349 reliably handle arbitrary pathnames (even those which contain Line Feed
3354 Historical (BSD and System V) implementations of @command{sort} have
3355 differed in their interpretation of some options, particularly
3356 @option{-b}, @option{-f}, and @option{-n}. @sc{gnu} sort follows the @acronym{POSIX}
3357 behavior, which is usually (but not always!) like the System V behavior.
3358 According to @acronym{POSIX}, @option{-n} no longer implies @option{-b}. For
3359 consistency, @option{-M} has been changed in the same way. This may
3360 affect the meaning of character positions in field specifications in
3361 obscure cases. The only fix is to add an explicit @option{-b}.
3363 A position in a sort field specified with the @option{-k}
3364 option has the form @samp{@var{f}.@var{c}}, where @var{f} is the number
3365 of the field to use and @var{c} is the number of the first character
3366 from the beginning of the field. In a start position, an omitted
3367 @samp{.@var{c}} stands for the field's first character. In an end
3368 position, an omitted or zero @samp{.@var{c}} stands for the field's
3369 last character. If the
3370 @option{-b} option was specified, the @samp{.@var{c}} part of a field
3371 specification is counted from the first nonblank character of the field.
3373 A sort key position may also have any of the option letters @samp{Mbdfinr}
3374 appended to it, in which case the global ordering options are not used
3375 for that particular field. The @option{-b} option may be independently
3376 attached to either or both of the start and
3377 end positions of a field specification, and if it is inherited
3378 from the global options it will be attached to both.
3379 Keys may span multiple fields.
3381 On older systems, @command{sort} supports an obsolete origin-zero
3382 syntax @samp{+@var{pos1} [-@var{pos2}]} for specifying sort keys.
3383 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow
3384 this; use @option{-k} instead.
3386 Here are some examples to illustrate various combinations of options.
3391 Sort in descending (reverse) numeric order.
3398 Sort alphabetically, omitting the first and second fields.
3399 This uses a single key composed of the characters beginning
3400 at the start of field three and extending to the end of each line.
3407 Sort numerically on the second field and resolve ties by sorting
3408 alphabetically on the third and fourth characters of field five.
3409 Use @samp{:} as the field delimiter.
3412 sort -t : -k 2,2n -k 5.3,5.4
3415 Note that if you had written @option{-k 2} instead of @option{-k 2,2}
3416 @command{sort} would have used all characters beginning in the second field
3417 and extending to the end of the line as the primary @emph{numeric}
3418 key. For the large majority of applications, treating keys spanning
3419 more than one field as numeric will not do what you expect.
3421 Also note that the @samp{n} modifier was applied to the field-end
3422 specifier for the first key. It would have been equivalent to
3423 specify @option{-k 2n,2} or @option{-k 2n,2n}. All modifiers except
3424 @samp{b} apply to the associated @emph{field}, regardless of whether
3425 the modifier character is attached to the field-start and/or the
3426 field-end part of the key specifier.
3429 Sort the password file on the fifth field and ignore any
3430 leading blanks. Sort lines with equal values in field five
3431 on the numeric user ID in field three.
3434 sort -t : -k 5b,5 -k 3,3n /etc/passwd
3437 An alternative is to use the global numeric modifier @option{-n}.
3440 sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
3444 Generate a tags file in case-insensitive sorted order.
3447 find src -type f -print0 | sort -t / -z -f | xargs -0 etags --append
3450 The use of @option{-print0}, @option{-z}, and @option{-0} in this case means
3451 that pathnames that contain Line Feed characters will not get broken up
3452 by the sort operation.
3454 Finally, to ignore both leading and trailing blanks, you
3455 could have applied the @samp{b} modifier to the field-end specifier
3459 sort -t : -n -k 5b,5b -k 3,3 /etc/passwd
3462 or by using the global @option{-b} modifier instead of @option{-n}
3463 and an explicit @samp{n} with the second key specifier.
3466 sort -t : -b -k 5,5 -k 3,3n /etc/passwd
3469 @c This example is a bit contrived and needs more explanation.
3471 @c Sort records separated by an arbitrary string by using a pipe to convert
3472 @c each record delimiter string to @samp{\0}, then using sort's -z option,
3473 @c and converting each @samp{\0} back to the original record delimiter.
3476 @c printf 'c\n\nb\n\na\n'|perl -0pe 's/\n\n/\n\0/g'|sort -z|perl -0pe 's/\0/\n/g'
3482 @node uniq invocation
3483 @section @command{uniq}: Uniquify files
3486 @cindex uniquify files
3488 @command{uniq} writes the unique lines in the given @file{input}, or
3489 standard input if nothing is given or for an @var{input} name of
3493 uniq [@var{option}]@dots{} [@var{input} [@var{output}]]
3496 By default, @command{uniq} prints its input lines, except that
3497 it discards all but the first of adjacent repeated lines, so that
3498 no output lines are repeated. Optionally, it can instead discard
3499 lines that are not repeated, or all repeated lines.
3501 The input need not be sorted, but repeated input lines are detected
3502 only if they are adjacent. If you want to discard non-adjacent
3503 duplicate lines, perhaps you want to use @code{sort -u}.
3506 Comparisons use the character collating sequence specified by the
3507 @env{LC_COLLATE} locale category.
3509 If no @var{output} file is specified, @command{uniq} writes to standard
3512 The program accepts the following options. Also see @ref{Common options}.
3517 @itemx --skip-fields=@var{n}
3519 @opindex --skip-fields
3520 Skip @var{n} fields on each line before checking for uniqueness. Use
3521 a null string for comparison if a line has fewer than @var{n} fields. Fields
3522 are sequences of non-space non-tab characters that are separated from
3523 each other by at least one space or tab.
3525 On older systems, @command{uniq} supports an obsolete option
3526 @option{-@var{n}}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance})
3527 does not allow this; use @option{-f @var{n}} instead.
3530 @itemx --skip-chars=@var{n}
3532 @opindex --skip-chars
3533 Skip @var{n} characters before checking for uniqueness. Use a null string
3534 for comparison if a line has fewer than @var{n} characters. If you use both
3535 the field and character skipping options, fields are skipped over first.
3537 On older systems, @command{uniq} supports an obsolete option
3538 @option{+@var{n}}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance})
3539 does not allow this; use @option{-s @var{n}} instead.
3545 Print the number of times each line occurred along with the line.
3548 @itemx --ignore-case
3550 @opindex --ignore-case
3551 Ignore differences in case when comparing lines.
3557 @cindex repeated lines, outputting
3558 Discard lines that are not repeated. When used by itself, this option
3559 causes @command{uniq} to print the first copy of each repeated line,
3563 @itemx --all-repeated[=@var{delimit-method}]
3565 @opindex --all-repeated
3566 @cindex all repeated lines, outputting
3567 Do not discard the second and subsequent repeated input lines,
3568 but discard lines that are not repeated.
3569 This option is useful mainly in conjunction with other options e.g.,
3570 to ignore case or to compare only selected fields.
3571 The optional @var{delimit-method} tells how to delimit
3572 groups of repeated lines, and must be one of the following:
3577 Do not delimit groups of repeated lines.
3578 This is equivalent to @option{--all-repeated} (@option{-D}).
3581 Output a newline before each group of repeated lines.
3584 Separate groups of repeated lines with a single newline.
3585 This is the same as using @samp{prepend}, except that
3586 there is no newline before the first group, and hence
3587 may be better suited for output direct to users.
3590 Note that when groups are delimited and the input stream contains
3591 two or more consecutive blank lines, then the output is ambiguous.
3592 To avoid that, filter the input through @samp{tr -s '\n'} to replace
3593 each sequence of consecutive newlines with a single newline.
3595 This is a @sc{gnu} extension.
3596 @c FIXME: give an example showing *how* it's useful
3602 @cindex unique lines, outputting
3603 Discard the first repeated line. When used by itself, this option
3604 causes @command{uniq} to print unique lines, and nothing else.
3607 @itemx --check-chars=@var{n}
3609 @opindex --check-chars
3610 Compare at most @var{n} characters on each line (after skipping any specified
3611 fields and characters). By default the entire rest of the lines are
3619 @node comm invocation
3620 @section @command{comm}: Compare two sorted files line by line
3623 @cindex line-by-line comparison
3624 @cindex comparing sorted files
3626 @command{comm} writes to standard output lines that are common, and lines
3627 that are unique, to two input files; a file name of @samp{-} means
3628 standard input. Synopsis:
3631 comm [@var{option}]@dots{} @var{file1} @var{file2}
3635 Before @command{comm} can be used, the input files must be sorted using the
3636 collating sequence specified by the @env{LC_COLLATE} locale.
3637 If an input file ends in a non-newline
3638 character, a newline is silently appended. The @command{sort} command with
3639 no options always outputs a file that is suitable input to @command{comm}.
3641 @cindex differing lines
3642 @cindex common lines
3643 With no options, @command{comm} produces three-column output. Column one
3644 contains lines unique to @var{file1}, column two contains lines unique
3645 to @var{file2}, and column three contains lines common to both files.
3646 Columns are separated by a single TAB character.
3647 @c FIXME: when there's an option to supply an alternative separator
3648 @c string, append `by default' to the above sentence.
3653 The options @option{-1}, @option{-2}, and @option{-3} suppress printing of
3654 the corresponding columns. Also see @ref{Common options}.
3656 Unlike some other comparison utilities, @command{comm} has an exit
3657 status that does not depend on the result of the comparison.
3658 Upon normal completion @command{comm} produces an exit code of zero.
3659 If there is an error it exits with nonzero status.
3662 @node tsort invocation
3663 @section @command{tsort}: Topological sort
3666 @cindex topological sort
3668 @command{tsort} performs a topological sort on the given @var{file}, or
3669 standard input if no input file is given or for a @var{file} of
3670 @samp{-}. For more details and some history, see @ref{tsort background}.
3674 tsort [@var{option}] [@var{file}]
3677 @command{tsort} reads its input as pairs of strings, separated by blanks,
3678 indicating a partial ordering. The output is a total ordering that
3679 corresponds to the given partial ordering.
3693 will produce the output
3704 Consider a more realistic example.
3705 You have a large set of functions all in one file, and they may all be
3706 declared static except one. Currently that one (say @code{main}) is the
3707 first function defined in the file, and the ones it calls directly follow
3708 it, followed by those they call, etc. Let's say that you are determined
3709 to take advantage of prototypes, so you have to choose between declaring
3710 all of those functions (which means duplicating a lot of information from
3711 the definitions) and rearranging the functions so that as many as possible
3712 are defined before they are used. One way to automate the latter process
3713 is to get a list for each function of the functions it calls directly.
3714 Many programs can generate such lists. They describe a call graph.
3715 Consider the following list, in which a given line indicates that the
3716 function on the left calls the one on the right directly.
3722 tail_file pretty_name
3723 tail_file write_header
3725 tail_forever recheck
3726 tail_forever pretty_name
3727 tail_forever write_header
3728 tail_forever dump_remainder
3731 tail_lines start_lines
3732 tail_lines dump_remainder
3733 tail_lines file_lines
3734 tail_lines pipe_lines
3736 tail_bytes start_bytes
3737 tail_bytes dump_remainder
3738 tail_bytes pipe_bytes
3739 file_lines dump_remainder
3743 then you can use @command{tsort} to produce an ordering of those
3744 functions that satisfies your requirement.
3747 example$ tsort call-graph | tac
3767 @command{tsort} detects any cycles in the input and writes the first cycle
3768 encountered to standard error.
3770 Note that for a given partial ordering, generally there is no unique
3771 total ordering. In the context of the call graph above, the function
3772 @code{parse_options} may be placed anywhere in the list as long as it
3773 precedes @code{main}.
3775 The only options are @option{--help} and @option{--version}. @xref{Common
3778 @node tsort background
3779 @section @command{tsort}: Background
3781 @command{tsort} exists because very early versions of the Unix linker processed
3782 an archive file exactly once, and in order. As @command{ld} read each object
3783 in the archive, it decided whether it was needed in the program based on
3784 whether it defined any symbols which were undefined at that point in
3787 This meant that dependencies within the archive had to be handled
3788 specially. For example, @code{scanf} probably calls @code{read}. That means
3789 that in a single pass through an archive, it was important for @code{scanf.o}
3790 to appear before read.o, because otherwise a program which calls
3791 @code{scanf} but not @code{read} might end up with an unexpected unresolved
3792 reference to @code{read}.
3794 The way to address this problem was to first generate a set of
3795 dependencies of one object file on another. This was done by a shell
3796 script called @command{lorder}. The GNU tools don't provide a version of
3797 lorder, as far as I know, but you can still find it in BSD
3800 Then you ran @command{tsort} over the @command{lorder} output, and you used the
3801 resulting sort to define the order in which you added objects to the archive.
3803 This whole procedure has been obsolete since about 1980, because
3804 Unix archives now contain a symbol table (traditionally built by
3805 @command{ranlib}, now generally built by @command{ar} itself), and the Unix
3806 linker uses the symbol table to effectively make multiple passes over
3809 Anyhow, that's where tsort came from. To solve an old problem with
3810 the way the linker handled archive files, which has since been solved
3816 @node ptx invocation
3817 @section @command{ptx}: Produce permuted indexes
3821 @command{ptx} reads a text file and essentially produces a permuted index, with
3822 each keyword in its context. The calling sketch is either one of:
3825 ptx [@var{option} @dots{}] [@var{file} @dots{}]
3826 ptx -G [@var{option} @dots{}] [@var{input} [@var{output}]]
3829 The @option{-G} (or its equivalent: @option{--traditional}) option disables
3830 all @sc{gnu} extensions and reverts to traditional mode, thus introducing some
3831 limitations and changing several of the program's default option values.
3832 When @option{-G} is not specified, @sc{gnu} extensions are always enabled.
3833 @sc{gnu} extensions to @command{ptx} are documented wherever appropriate in this
3834 document. For the full list, see @xref{Compatibility in ptx}.
3836 Individual options are explained in the following sections.
3838 When @sc{gnu} extensions are enabled, there may be zero, one or several
3839 @var{file}s after the options. If there is no @var{file}, the program
3840 reads the standard input. If there is one or several @var{file}s, they
3841 give the name of input files which are all read in turn, as if all the
3842 input files were concatenated. However, there is a full contextual
3843 break between each file and, when automatic referencing is requested,
3844 file names and line numbers refer to individual text input files. In
3845 all cases, the program outputs the permuted index to the standard
3848 When @sc{gnu} extensions are @emph{not} enabled, that is, when the program
3849 operates in traditional mode, there may be zero, one or two parameters
3850 besides the options. If there are no parameters, the program reads the
3851 standard input and outputs the permuted index to the standard output.
3852 If there is only one parameter, it names the text @var{input} to be read
3853 instead of the standard input. If two parameters are given, they give
3854 respectively the name of the @var{input} file to read and the name of
3855 the @var{output} file to produce. @emph{Be very careful} to note that,
3856 in this case, the contents of file given by the second parameter is
3857 destroyed. This behavior is dictated by System V @command{ptx}
3858 compatibility; @sc{gnu} Standards normally discourage output parameters not
3859 introduced by an option.
3861 Note that for @emph{any} file named as the value of an option or as an
3862 input text file, a single dash @kbd{-} may be used, in which case
3863 standard input is assumed. However, it would not make sense to use this
3864 convention more than once per program invocation.
3867 * General options in ptx:: Options which affect general program behavior.
3868 * Charset selection in ptx:: Underlying character set considerations.
3869 * Input processing in ptx:: Input fields, contexts, and keyword selection.
3870 * Output formatting in ptx:: Types of output format, and sizing the fields.
3871 * Compatibility in ptx::
3875 @node General options in ptx
3876 @subsection General options
3882 Print a short note about the copyright and copying conditions, then
3883 exit without further processing.
3886 @itemx --traditional
3887 As already explained, this option disables all @sc{gnu} extensions to
3888 @command{ptx} and switches to traditional mode.
3891 Print a short help on standard output, then exit without further
3895 Print the program version on standard output, then exit without further
3903 @node Charset selection in ptx
3904 @subsection Charset selection
3906 @c FIXME: People don't necessarily know what an IBM-PC was these days.
3907 As it is set up now, the program assumes that the input file is coded
3908 using 8-bit ISO 8859-1 code, also known as Latin-1 character set,
3909 @emph{unless} it is compiled for MS-DOS, in which case it uses the
3910 character set of the IBM-PC. (@sc{gnu} @command{ptx} is not known to work on
3911 smaller MS-DOS machines anymore.) Compared to 7-bit @acronym{ASCII}, the set
3912 of characters which are letters is different; this alters the behavior
3913 of regular expression matching. Thus, the default regular expression
3914 for a keyword allows foreign or diacriticized letters. Keyword sorting,
3915 however, is still crude; it obeys the underlying character set ordering
3921 @itemx --ignore-case
3922 Fold lower case letters to upper case for sorting.
3927 @node Input processing in ptx
3928 @subsection Word selection and input processing
3933 @item --break-file=@var{file}
3935 This option provides an alternative (to @option{-W}) method of describing
3936 which characters make up words. It introduces the name of a
3937 file which contains a list of characters which can@emph{not} be part of
3938 one word; this file is called the @dfn{Break file}. Any character which
3939 is not part of the Break file is a word constituent. If both options
3940 @option{-b} and @option{-W} are specified, then @option{-W} has precedence and
3941 @option{-b} is ignored.
3943 When @sc{gnu} extensions are enabled, the only way to avoid newline as a
3944 break character is to write all the break characters in the file with no
3945 newline at all, not even at the end of the file. When @sc{gnu} extensions
3946 are disabled, spaces, tabs and newlines are always considered as break
3947 characters even if not included in the Break file.
3950 @itemx --ignore-file=@var{file}
3952 The file associated with this option contains a list of words which will
3953 never be taken as keywords in concordance output. It is called the
3954 @dfn{Ignore file}. The file contains exactly one word in each line; the
3955 end of line separation of words is not subject to the value of the
3958 There is a default Ignore file used by @command{ptx} when this option is
3959 not specified, usually found in @file{/usr/local/lib/eign} if this has
3960 not been changed at installation time. If you want to deactivate the
3961 default Ignore file, specify @code{/dev/null} instead.
3964 @itemx --only-file=@var{file}
3966 The file associated with this option contains a list of words which will
3967 be retained in concordance output; any word not mentioned in this file
3968 is ignored. The file is called the @dfn{Only file}. The file contains
3969 exactly one word in each line; the end of line separation of words is
3970 not subject to the value of the @option{-S} option.
3972 There is no default for the Only file. When both an Only file and an
3973 Ignore file are specified, a word is considered a keyword only
3974 if it is listed in the Only file and not in the Ignore file.
3979 On each input line, the leading sequence of non-white space characters will be
3980 taken to be a reference that has the purpose of identifying this input
3981 line in the resulting permuted index. For more information about reference
3982 production, see @xref{Output formatting in ptx}.
3983 Using this option changes the default value for option @option{-S}.
3985 Using this option, the program does not try very hard to remove
3986 references from contexts in output, but it succeeds in doing so
3987 @emph{when} the context ends exactly at the newline. If option
3988 @option{-r} is used with @option{-S} default value, or when @sc{gnu} extensions
3989 are disabled, this condition is always met and references are completely
3990 excluded from the output contexts.
3992 @item -S @var{regexp}
3993 @itemx --sentence-regexp=@var{regexp}
3995 This option selects which regular expression will describe the end of a
3996 line or the end of a sentence. In fact, this regular expression is not
3997 the only distinction between end of lines or end of sentences, and input
3998 line boundaries have no special significance outside this option. By
3999 default, when @sc{gnu} extensions are enabled and if @option{-r} option is not
4000 used, end of sentences are used. In this case, this @var{regex} is
4001 imported from @sc{gnu} Emacs:
4004 [.?!][]\"')@}]*\\($\\|\t\\| \\)[ \t\n]*
4007 Whenever @sc{gnu} extensions are disabled or if @option{-r} option is used, end
4008 of lines are used; in this case, the default @var{regexp} is just:
4014 Using an empty @var{regexp} is equivalent to completely disabling end of
4015 line or end of sentence recognition. In this case, the whole file is
4016 considered to be a single big line or sentence. The user might want to
4017 disallow all truncation flag generation as well, through option @option{-F
4018 ""}. @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4021 When the keywords happen to be near the beginning of the input line or
4022 sentence, this often creates an unused area at the beginning of the
4023 output context line; when the keywords happen to be near the end of the
4024 input line or sentence, this often creates an unused area at the end of
4025 the output context line. The program tries to fill those unused areas
4026 by wrapping around context in them; the tail of the input line or
4027 sentence is used to fill the unused area on the left of the output line;
4028 the head of the input line or sentence is used to fill the unused area
4029 on the right of the output line.
4031 As a matter of convenience to the user, many usual backslashed escape
4032 sequences from the C language are recognized and converted to the
4033 corresponding characters by @command{ptx} itself.
4035 @item -W @var{regexp}
4036 @itemx --word-regexp=@var{regexp}
4038 This option selects which regular expression will describe each keyword.
4039 By default, if @sc{gnu} extensions are enabled, a word is a sequence of
4040 letters; the @var{regexp} used is @samp{\w+}. When @sc{gnu} extensions are
4041 disabled, a word is by default anything which ends with a space, a tab
4042 or a newline; the @var{regexp} used is @samp{[^ \t\n]+}.
4044 An empty @var{regexp} is equivalent to not using this option.
4045 @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4048 As a matter of convenience to the user, many usual backslashed escape
4049 sequences, as found in the C language, are recognized and converted to
4050 the corresponding characters by @command{ptx} itself.
4055 @node Output formatting in ptx
4056 @subsection Output formatting
4058 Output format is mainly controlled by the @option{-O} and @option{-T} options
4059 described in the table below. When neither @option{-O} nor @option{-T} are
4060 selected, and if @sc{gnu} extensions are enabled, the program chooses an
4061 output format suitable for a dumb terminal. Each keyword occurrence is
4062 output to the center of one line, surrounded by its left and right
4063 contexts. Each field is properly justified, so the concordance output
4064 can be readily observed. As a special feature, if automatic
4065 references are selected by option @option{-A} and are output before the
4066 left context, that is, if option @option{-R} is @emph{not} selected, then
4067 a colon is added after the reference; this nicely interfaces with @sc{gnu}
4068 Emacs @code{next-error} processing. In this default output format, each
4069 white space character, like newline and tab, is merely changed to
4070 exactly one space, with no special attempt to compress consecutive
4071 spaces. This might change in the future. Except for those white space
4072 characters, every other character of the underlying set of 256
4073 characters is transmitted verbatim.
4075 Output format is further controlled by the following options.
4079 @item -g @var{number}
4080 @itemx --gap-size=@var{number}
4082 Select the size of the minimum white space gap between the fields on the
4085 @item -w @var{number}
4086 @itemx --width=@var{number}
4088 Select the maximum output width of each final line. If references are
4089 used, they are included or excluded from the maximum output width
4090 depending on the value of option @option{-R}. If this option is not
4091 selected, that is, when references are output before the left context,
4092 the maximum output width takes into account the maximum length of all
4093 references. If this option is selected, that is, when references are
4094 output after the right context, the maximum output width does not take
4095 into account the space taken by references, nor the gap that precedes
4099 @itemx --auto-reference
4101 Select automatic references. Each input line will have an automatic
4102 reference made up of the file name and the line ordinal, with a single
4103 colon between them. However, the file name will be empty when standard
4104 input is being read. If both @option{-A} and @option{-r} are selected, then
4105 the input reference is still read and skipped, but the automatic
4106 reference is used at output time, overriding the input reference.
4109 @itemx --right-side-refs
4111 In the default output format, when option @option{-R} is not used, any
4112 references produced by the effect of options @option{-r} or @option{-A} are
4113 placed to the far right of output lines, after the right context. With
4114 default output format, when the @option{-R} option is specified, references
4115 are rather placed at the beginning of each output line, before the left
4116 context. For any other output format, option @option{-R} is
4117 ignored, with one exception: with @option{-R} the width of references
4118 is @emph{not} taken into account in total output width given by @option{-w}.
4120 This option is automatically selected whenever @sc{gnu} extensions are
4123 @item -F @var{string}
4124 @itemx --flac-truncation=@var{string}
4126 This option will request that any truncation in the output be reported
4127 using the string @var{string}. Most output fields theoretically extend
4128 towards the beginning or the end of the current line, or current
4129 sentence, as selected with option @option{-S}. But there is a maximum
4130 allowed output line width, changeable through option @option{-w}, which is
4131 further divided into space for various output fields. When a field has
4132 to be truncated because it cannot extend beyond the beginning or the end of
4133 the current line to fit in, then a truncation occurs. By default,
4134 the string used is a single slash, as in @option{-F /}.
4136 @var{string} may have more than one character, as in @option{-F ...}.
4137 Also, in the particular case when @var{string} is empty (@option{-F ""}),
4138 truncation flagging is disabled, and no truncation marks are appended in
4141 As a matter of convenience to the user, many usual backslashed escape
4142 sequences, as found in the C language, are recognized and converted to
4143 the corresponding characters by @command{ptx} itself.
4145 @item -M @var{string}
4146 @itemx --macro-name=@var{string}
4148 Select another @var{string} to be used instead of @samp{xx}, while
4149 generating output suitable for @command{nroff}, @command{troff} or @TeX{}.
4152 @itemx --format=roff
4154 Choose an output format suitable for @command{nroff} or @command{troff}
4155 processing. Each output line will look like:
4158 .xx "@var{tail}" "@var{before}" "@var{keyword_and_after}" "@var{head}" "@var{ref}"
4161 so it will be possible to write a @samp{.xx} roff macro to take care of
4162 the output typesetting. This is the default output format when @sc{gnu}
4163 extensions are disabled. Option @option{-M} can be used to change
4164 @samp{xx} to another macro name.
4166 In this output format, each non-graphical character, like newline and
4167 tab, is merely changed to exactly one space, with no special attempt to
4168 compress consecutive spaces. Each quote character: @kbd{"} is doubled
4169 so it will be correctly processed by @command{nroff} or @command{troff}.
4174 Choose an output format suitable for @TeX{} processing. Each output
4175 line will look like:
4178 \xx @{@var{tail}@}@{@var{before}@}@{@var{keyword}@}@{@var{after}@}@{@var{head}@}@{@var{ref}@}
4182 so it will be possible to write a @code{\xx} definition to take care of
4183 the output typesetting. Note that when references are not being
4184 produced, that is, neither option @option{-A} nor option @option{-r} is
4185 selected, the last parameter of each @code{\xx} call is inhibited.
4186 Option @option{-M} can be used to change @samp{xx} to another macro
4189 In this output format, some special characters, like @kbd{$}, @kbd{%},
4190 @kbd{&}, @kbd{#} and @kbd{_} are automatically protected with a
4191 backslash. Curly brackets @kbd{@{}, @kbd{@}} are protected with a
4192 backslash and a pair of dollar signs (to force mathematical mode). The
4193 backslash itself produces the sequence @code{\backslash@{@}}.
4194 Circumflex and tilde diacritical marks produce the sequence @code{^\@{ @}} and
4195 @code{~\@{ @}} respectively. Other diacriticized characters of the
4196 underlying character set produce an appropriate @TeX{} sequence as far
4197 as possible. The other non-graphical characters, like newline and tab,
4198 and all other characters which are not part of @acronym{ASCII}, are merely
4199 changed to exactly one space, with no special attempt to compress
4200 consecutive spaces. Let me know how to improve this special character
4201 processing for @TeX{}.
4206 @node Compatibility in ptx
4207 @subsection The @sc{gnu} extensions to @command{ptx}
4209 This version of @command{ptx} contains a few features which do not exist in
4210 System V @command{ptx}. These extra features are suppressed by using the
4211 @option{-G} command line option, unless overridden by other command line
4212 options. Some @sc{gnu} extensions cannot be recovered by overriding, so the
4213 simple rule is to avoid @option{-G} if you care about @sc{gnu} extensions.
4214 Here are the differences between this program and System V @command{ptx}.
4219 This program can read many input files at once, it always writes the
4220 resulting concordance on standard output. On the other hand, System V
4221 @command{ptx} reads only one file and sends the result to standard output
4222 or, if a second @var{file} parameter is given on the command, to that
4225 Having output parameters not introduced by options is a dangerous
4226 practice which @sc{gnu} avoids as far as possible. So, for using @command{ptx}
4227 portably between @sc{gnu} and System V, you should always use it with a
4228 single input file, and always expect the result on standard output. You
4229 might also want to automatically configure in a @option{-G} option to
4230 @command{ptx} calls in products using @command{ptx}, if the configurator finds
4231 that the installed @command{ptx} accepts @option{-G}.
4234 The only options available in System V @command{ptx} are options @option{-b},
4235 @option{-f}, @option{-g}, @option{-i}, @option{-o}, @option{-r}, @option{-t} and
4236 @option{-w}. All other options are @sc{gnu} extensions and are not repeated in
4237 this enumeration. Moreover, some options have a slightly different
4238 meaning when @sc{gnu} extensions are enabled, as explained below.
4241 By default, concordance output is not formatted for @command{troff} or
4242 @command{nroff}. It is rather formatted for a dumb terminal. @command{troff}
4243 or @command{nroff} output may still be selected through option @option{-O}.
4246 Unless @option{-R} option is used, the maximum reference width is
4247 subtracted from the total output line width. With @sc{gnu} extensions
4248 disabled, width of references is not taken into account in the output
4249 line width computations.
4252 All 256 characters, even @kbd{NUL}s, are always read and processed from
4253 input file with no adverse effect, even if @sc{gnu} extensions are disabled.
4254 However, System V @command{ptx} does not accept 8-bit characters, a few
4255 control characters are rejected, and the tilde @kbd{~} is also rejected.
4258 Input line length is only limited by available memory, even if @sc{gnu}
4259 extensions are disabled. However, System V @command{ptx} processes only
4260 the first 200 characters in each line.
4263 The break (non-word) characters default to be every character except all
4264 letters of the underlying character set, diacriticized or not. When @sc{gnu}
4265 extensions are disabled, the break characters default to space, tab and
4269 The program makes better use of output line width. If @sc{gnu} extensions
4270 are disabled, the program rather tries to imitate System V @command{ptx},
4271 but still, there are some slight disposition glitches this program does
4272 not completely reproduce.
4275 The user can specify both an Ignore file and an Only file. This is not
4276 allowed with System V @command{ptx}.
4281 @node Operating on fields within a line
4282 @chapter Operating on fields within a line
4285 * cut invocation:: Print selected parts of lines.
4286 * paste invocation:: Merge lines of files.
4287 * join invocation:: Join lines on a common field.
4291 @node cut invocation
4292 @section @command{cut}: Print selected parts of lines
4295 @command{cut} writes to standard output selected parts of each line of each
4296 input file, or standard input if no files are given or for a file name of
4300 cut [@var{option}]@dots{} [@var{file}]@dots{}
4303 In the table which follows, the @var{byte-list}, @var{character-list},
4304 and @var{field-list} are one or more numbers or ranges (two numbers
4305 separated by a dash) separated by commas. Bytes, characters, and
4306 fields are numbered starting at 1. Incomplete ranges may be
4307 given: @option{-@var{m}} means @samp{1-@var{m}}; @samp{@var{n}-} means
4308 @samp{@var{n}} through end of line or last field. The list elements
4309 can be repeated, can overlap, and can be specified in any order; but
4310 the selected input is written in the same order that it is read, and
4311 is written exactly once.
4313 The program accepts the following options. Also see @ref{Common
4318 @item -b @var{byte-list}
4319 @itemx --bytes=@var{byte-list}
4322 Print only the bytes in positions listed in @var{byte-list}. Tabs and
4323 backspaces are treated like any other character; they take up 1 byte.
4324 If an output delimiter is specified, (see the description of
4325 @option{--output-delimiter}), then output that string between
4326 ranges of selected bytes.
4328 @item -c @var{character-list}
4329 @itemx --characters=@var{character-list}
4331 @opindex --characters
4332 Print only characters in positions listed in @var{character-list}.
4333 The same as @option{-b} for now, but internationalization will change
4334 that. Tabs and backspaces are treated like any other character; they
4335 take up 1 character.
4336 If an output delimiter is specified, (see the description of
4337 @option{--output-delimiter}), then output that string between
4338 ranges of selected bytes.
4340 @item -f @var{field-list}
4341 @itemx --fields=@var{field-list}
4344 Print only the fields listed in @var{field-list}. Fields are
4345 separated by a TAB character by default.
4346 Also print any line that contains no delimiter character, unless
4347 the @option{--only-delimited} (@option{-s}) option is specified
4349 @item -d @var{input_delim_byte}
4350 @itemx --delimiter=@var{input_delim_byte}
4352 @opindex --delimiter
4353 For @option{-f}, fields are separated in the input by the first character
4354 in @var{input_delim_byte} (default is TAB).
4358 Do not split multi-byte characters (no-op for now).
4361 @itemx --only-delimited
4363 @opindex --only-delimited
4364 For @option{-f}, do not print lines that do not contain the field separator
4365 character. Normally, any line without a field separator is printed verbatim.
4367 @itemx --output-delimiter=@var{output_delim_string}
4368 @opindex --output-delimiter
4369 With @option{-f}, output fields are separated by @var{output_delim_string}.
4370 The default with @option{-f} is to use the input delimiter.
4371 When using @option{-b} or @option{-c} to select ranges of byte or
4372 character offsets (as opposed to ranges of fields),
4373 output @var{output_delim_string} between ranges of selected bytes.
4381 @node paste invocation
4382 @section @command{paste}: Merge lines of files
4385 @cindex merging files
4387 @command{paste} writes to standard output lines consisting of sequentially
4388 corresponding lines of each given file, separated by a TAB character.
4389 Standard input is used for a file name of @samp{-} or if no input files
4411 paste [@var{option}]@dots{} [@var{file}]@dots{}
4414 The program accepts the following options. Also see @ref{Common options}.
4422 Paste the lines of one file at a time rather than one line from each
4423 file. Using the above example data:
4426 $ paste -s num2 let3
4431 @item -d @var{delim-list}
4432 @itemx --delimiters=@var{delim-list}
4434 @opindex --delimiters
4435 Consecutively use the characters in @var{delim-list} instead of
4436 TAB to separate merged lines. When @var{delim-list} is
4437 exhausted, start again at its beginning. Using the above example data:
4440 $ paste -d '%_' num2 let3 num2
4451 @node join invocation
4452 @section @command{join}: Join lines on a common field
4455 @cindex common field, joining on
4457 @command{join} writes to standard output a line for each pair of input
4458 lines that have identical join fields. Synopsis:
4461 join [@var{option}]@dots{} @var{file1} @var{file2}
4465 Either @var{file1} or @var{file2} (but not both) can be @samp{-},
4466 meaning standard input. @var{file1} and @var{file2} should be
4467 sorted on the join fields.
4469 Normally, the sort order is that of the
4470 collating sequence specified by the @env{LC_COLLATE} locale. Unless
4471 the @option{-t} option is given, the sort comparison ignores blanks at
4472 the start of the join field, as in @code{sort -b}. If the
4473 @option{--ignore-case} option is given, the sort comparison ignores
4474 the case of characters in the join field, as in @code{sort -f}.
4476 However, as a GNU extension, if the input has no unpairable lines the
4477 sort order can be any order that considers two fields to be equal if and
4478 only if the sort comparison described above considers them to be equal.
4496 The defaults are: the join field is the first field in each line;
4497 fields in the input are separated by one or more blanks, with leading
4498 blanks on the line ignored; fields in the output are separated by a
4499 space; each output line consists of the join field, the remaining
4500 fields from @var{file1}, then the remaining fields from @var{file2}.
4502 The program accepts the following options. Also see @ref{Common options}.
4506 @item -a @var{file-number}
4508 Print a line for each unpairable line in file @var{file-number} (either
4509 @samp{1} or @samp{2}), in addition to the normal output.
4511 @item -e @var{string}
4513 Replace those output fields that are missing in the input with
4517 @itemx --ignore-case
4519 @opindex --ignore-case
4520 Ignore differences in case when comparing keys.
4521 With this option, the lines of the input files must be ordered in the same way.
4522 Use @samp{sort -f} to produce this ordering.
4524 @item -1 @var{field}
4526 Join on field @var{field} (a positive integer) of file 1.
4528 @item -2 @var{field}
4530 Join on field @var{field} (a positive integer) of file 2.
4532 @item -j @var{field}
4533 Equivalent to @option{-1 @var{field} -2 @var{field}}.
4535 @item -o @var{field-list}
4536 Construct each output line according to the format in @var{field-list}.
4537 Each element in @var{field-list} is either the single character @samp{0} or
4538 has the form @var{m.n} where the file number, @var{m}, is @samp{1} or
4539 @samp{2} and @var{n} is a positive field number.
4541 A field specification of @samp{0} denotes the join field.
4542 In most cases, the functionality of the @samp{0} field spec
4543 may be reproduced using the explicit @var{m.n} that corresponds
4544 to the join field. However, when printing unpairable lines
4545 (using either of the @option{-a} or @option{-v} options), there is no way
4546 to specify the join field using @var{m.n} in @var{field-list}
4547 if there are unpairable lines in both files.
4548 To give @command{join} that functionality, @acronym{POSIX} invented the @samp{0}
4549 field specification notation.
4551 The elements in @var{field-list}
4552 are separated by commas or blanks.
4553 All output lines -- including those printed because of any -a or -v
4554 option -- are subject to the specified @var{field-list}.
4557 Use character @var{char} as the input and output field separator.
4558 Treat as significant each occurrence of @var{char} in the input file.
4560 @item -v @var{file-number}
4561 Print a line for each unpairable line in file @var{file-number}
4562 (either @samp{1} or @samp{2}), instead of the normal output.
4566 In addition, when @sc{gnu} @command{join} is invoked with exactly one argument,
4567 the @option{--help} and @option{--version} options are recognized.
4568 @xref{Common options}.
4573 @node Operating on characters
4574 @chapter Operating on characters
4576 @cindex operating on characters
4578 This commands operate on individual characters.
4581 * tr invocation:: Translate, squeeze, and/or delete characters.
4582 * expand invocation:: Convert tabs to spaces.
4583 * unexpand invocation:: Convert spaces to tabs.
4588 @section @command{tr}: Translate, squeeze, and/or delete characters
4595 tr [@var{option}]@dots{} @var{set1} [@var{set2}]
4598 @command{tr} copies standard input to standard output, performing
4599 one of the following operations:
4603 translate, and optionally squeeze repeated characters in the result,
4605 squeeze repeated characters,
4609 delete characters, then squeeze repeated characters from the result.
4612 The @var{set1} and (if given) @var{set2} arguments define ordered
4613 sets of characters, referred to below as @var{set1} and @var{set2}. These
4614 sets are the characters of the input that @command{tr} operates on.
4615 The @option{--complement} (@option{-c}) option replaces @var{set1} with its
4616 complement (all of the characters that are not in @var{set1}).
4621 * Character sets:: Specifying sets of characters.
4622 * Translating:: Changing one set of characters to another.
4623 * Squeezing:: Squeezing repeats and deleting.
4624 * Warnings in tr:: Warning messages.
4628 @node Character sets
4629 @subsection Specifying sets of characters
4631 @cindex specifying sets of characters
4633 The format of the @var{set1} and @var{set2} arguments resembles
4634 the format of regular expressions; however, they are not regular
4635 expressions, only lists of characters. Most characters simply
4636 represent themselves in these strings, but the strings can contain
4637 the shorthands listed below, for convenience. Some of them can be
4638 used only in @var{set1} or @var{set2}, as noted below.
4642 @item Backslash escapes
4643 @cindex backslash escapes
4645 A backslash followed by a character not listed below causes an error
4664 The character with the value given by @var{ooo}, which is 1 to 3
4673 The notation @samp{@var{m}-@var{n}} expands to all of the characters
4674 from @var{m} through @var{n}, in ascending order. @var{m} should
4675 collate before @var{n}; if it doesn't, an error results. As an example,
4676 @samp{0-9} is the same as @samp{0123456789}.
4678 @sc{gnu} @command{tr} does not support the System V syntax that uses square
4679 brackets to enclose ranges. Translations specified in that format
4680 sometimes work as expected, since the brackets are often transliterated
4681 to themselves. However, they should be avoided because they sometimes
4682 behave unexpectedly. For example, @samp{tr -d '[0-9]'} deletes brackets
4685 Many historically common and even accepted uses of ranges are not
4686 portable. For example, on @acronym{EBCDIC} hosts using the @samp{A-Z}
4687 range will not do what most would expect because @samp{A} through @samp{Z}
4688 are not contiguous as they are in @acronym{ASCII}.
4689 If you can rely on a @acronym{POSIX} compliant version of @command{tr}, then
4690 the best way to work around this is to use character classes (see below).
4691 Otherwise, it is most portable (and most ugly) to enumerate the members
4694 @item Repeated characters
4695 @cindex repeated characters
4697 The notation @samp{[@var{c}*@var{n}]} in @var{set2} expands to @var{n}
4698 copies of character @var{c}. Thus, @samp{[y*6]} is the same as
4699 @samp{yyyyyy}. The notation @samp{[@var{c}*]} in @var{string2} expands
4700 to as many copies of @var{c} as are needed to make @var{set2} as long as
4701 @var{set1}. If @var{n} begins with @samp{0}, it is interpreted in
4702 octal, otherwise in decimal.
4704 @item Character classes
4705 @cindex character classes
4707 The notation @samp{[:@var{class}:]} expands to all of the characters in
4708 the (predefined) class @var{class}. The characters expand in no
4709 particular order, except for the @code{upper} and @code{lower} classes,
4710 which expand in ascending order. When the @option{--delete} (@option{-d})
4711 and @option{--squeeze-repeats} (@option{-s}) options are both given, any
4712 character class can be used in @var{set2}. Otherwise, only the
4713 character classes @code{lower} and @code{upper} are accepted in
4714 @var{set2}, and then only if the corresponding character class
4715 (@code{upper} and @code{lower}, respectively) is specified in the same
4716 relative position in @var{set1}. Doing this specifies case conversion.
4717 The class names are given below; an error results when an invalid class
4729 Horizontal whitespace.
4738 Printable characters, not including space.
4744 Printable characters, including space.
4747 Punctuation characters.
4750 Horizontal or vertical whitespace.
4759 @item Equivalence classes
4760 @cindex equivalence classes
4762 The syntax @samp{[=@var{c}=]} expands to all of the characters that are
4763 equivalent to @var{c}, in no particular order. Equivalence classes are
4764 a relatively recent invention intended to support non-English alphabets.
4765 But there seems to be no standard way to define them or determine their
4766 contents. Therefore, they are not fully implemented in @sc{gnu} @command{tr};
4767 each character's equivalence class consists only of that character,
4768 which is of no particular use.
4774 @subsection Translating
4776 @cindex translating characters
4778 @command{tr} performs translation when @var{set1} and @var{set2} are
4779 both given and the @option{--delete} (@option{-d}) option is not given.
4780 @command{tr} translates each character of its input that is in @var{set1}
4781 to the corresponding character in @var{set2}. Characters not in
4782 @var{set1} are passed through unchanged. When a character appears more
4783 than once in @var{set1} and the corresponding characters in @var{set2}
4784 are not all the same, only the final one is used. For example, these
4785 two commands are equivalent:
4792 A common use of @command{tr} is to convert lowercase characters to
4793 uppercase. This can be done in many ways. Here are three of them:
4796 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
4798 tr '[:lower:]' '[:upper:]'
4802 But note that using ranges like @code{a-z} above is not portable.
4804 When @command{tr} is performing translation, @var{set1} and @var{set2}
4805 typically have the same length. If @var{set1} is shorter than
4806 @var{set2}, the extra characters at the end of @var{set2} are ignored.
4808 On the other hand, making @var{set1} longer than @var{set2} is not
4809 portable; @acronym{POSIX} says that the result is undefined. In this situation,
4810 BSD @command{tr} pads @var{set2} to the length of @var{set1} by repeating
4811 the last character of @var{set2} as many times as necessary. System V
4812 @command{tr} truncates @var{set1} to the length of @var{set2}.
4814 By default, @sc{gnu} @command{tr} handles this case like BSD @command{tr}.
4815 When the @option{--truncate-set1} (@option{-t}) option is given,
4816 @sc{gnu} @command{tr} handles this case like the System V @command{tr}
4817 instead. This option is ignored for operations other than translation.
4819 Acting like System V @command{tr} in this case breaks the relatively common
4823 tr -cs A-Za-z0-9 '\012'
4827 because it converts only zero bytes (the first element in the
4828 complement of @var{set1}), rather than all non-alphanumerics, to
4832 By the way, the above idiom is not portable because it uses ranges.
4833 Assuming a @acronym{POSIX} compliant @command{tr}, here is a better way to write it:
4836 tr -cs '[:alnum:]' '[\n*]'
4841 @subsection Squeezing repeats and deleting
4843 @cindex squeezing repeat characters
4844 @cindex deleting characters
4846 When given just the @option{--delete} (@option{-d}) option, @command{tr}
4847 removes any input characters that are in @var{set1}.
4849 When given just the @option{--squeeze-repeats} (@option{-s}) option,
4850 @command{tr} replaces each input sequence of a repeated character that
4851 is in @var{set1} with a single occurrence of that character.
4853 When given both @option{--delete} and @option{--squeeze-repeats}, @command{tr}
4854 first performs any deletions using @var{set1}, then squeezes repeats
4855 from any remaining characters using @var{set2}.
4857 The @option{--squeeze-repeats} option may also be used when translating,
4858 in which case @command{tr} first performs translation, then squeezes
4859 repeats from any remaining characters using @var{set2}.
4861 Here are some examples to illustrate various combinations of options:
4866 Remove all zero bytes:
4873 Put all words on lines by themselves. This converts all
4874 non-alphanumeric characters to newlines, then squeezes each string
4875 of repeated newlines into a single newline:
4878 tr -cs '[:alnum:]' '[\n*]'
4882 Convert each sequence of repeated newlines to a single newline:
4889 Find doubled occurrences of words in a document.
4890 For example, people often write ``the the'' with the repeated words
4891 separated by a newline. The bourne shell script below works first
4892 by converting each sequence of punctuation and blank characters to a
4893 single newline. That puts each ``word'' on a line by itself.
4894 Next it maps all uppercase characters to lower case, and finally it
4895 runs @command{uniq} with the @option{-d} option to print out only the words
4901 | tr -s '[:punct:][:blank:]' '\n' \
4902 | tr '[:upper:]' '[:lower:]' \
4907 Deleting a small set of characters is usually straightforward. For example,
4908 to remove all @samp{a}s, @samp{x}s, and @samp{M}s you would do this:
4914 However, when @samp{-} is one of those characters, it can be tricky because
4915 @samp{-} has special meanings. Performing the same task as above but also
4916 removing all @samp{-} characters, we might try @code{tr -d -axM}, but
4917 that would fail because @command{tr} would try to interpret @option{-a} as
4918 a command-line option. Alternatively, we could try putting the hyphen
4919 inside the string, @code{tr -d a-xM}, but that wouldn't work either because
4920 it would make @command{tr} interpret @code{a-x} as the range of characters
4921 @samp{a}@dots{}@samp{x} rather than the three.
4922 One way to solve the problem is to put the hyphen at the end of the list
4929 More generally, use the character class notation @code{[=c=]}
4930 with @samp{-} (or any other character) in place of the @samp{c}:
4936 Note how single quotes are used in the above example to protect the
4937 square brackets from interpretation by a shell.
4942 @node Warnings in tr
4943 @subsection Warning messages
4945 @vindex POSIXLY_CORRECT
4946 Setting the environment variable @env{POSIXLY_CORRECT} turns off the
4947 following warning and error messages, for strict compliance with
4948 @acronym{POSIX}. Otherwise, the following diagnostics are issued:
4953 When the @option{--delete} option is given but @option{--squeeze-repeats}
4954 is not, and @var{set2} is given, @sc{gnu} @command{tr} by default prints
4955 a usage message and exits, because @var{set2} would not be used.
4956 The @acronym{POSIX} specification says that @var{set2} must be ignored in
4957 this case. Silently ignoring arguments is a bad idea.
4960 When an ambiguous octal escape is given. For example, @samp{\400}
4961 is actually @samp{\40} followed by the digit @samp{0}, because the
4962 value 400 octal does not fit into a single byte.
4966 @sc{gnu} @command{tr} does not provide complete BSD or System V compatibility.
4967 For example, it is impossible to disable interpretation of the @acronym{POSIX}
4968 constructs @samp{[:alpha:]}, @samp{[=c=]}, and @samp{[c*10]}. Also, @sc{gnu}
4969 @command{tr} does not delete zero bytes automatically, unlike traditional
4970 Unix versions, which provide no way to preserve zero bytes.
4973 @node expand invocation
4974 @section @command{expand}: Convert tabs to spaces
4977 @cindex tabs to spaces, converting
4978 @cindex converting tabs to spaces
4980 @command{expand} writes the contents of each given @var{file}, or standard
4981 input if none are given or for a @var{file} of @samp{-}, to standard
4982 output, with tab characters converted to the appropriate number of
4986 expand [@var{option}]@dots{} [@var{file}]@dots{}
4989 By default, @command{expand} converts all tabs to spaces. It preserves
4990 backspace characters in the output; they decrement the column count for
4991 tab calculations. The default action is equivalent to @option{-t 8} (set
4992 tabs every 8 columns).
4994 The program accepts the following options. Also see @ref{Common options}.
4998 @item -t @var{tab1}[,@var{tab2}]@dots{}
4999 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5002 @cindex tabstops, setting
5003 If only one tab stop is given, set the tabs @var{tab1} spaces apart
5004 (default is 8). Otherwise, set the tabs at columns @var{tab1},
5005 @var{tab2}, @dots{} (numbered from 0), and replace any tabs beyond the
5006 last tabstop given with single spaces. Tabstops can be separated by
5007 blanks as well as by commas.
5009 On older systems, @command{expand} supports an obsolete option
5010 @option{-@var{tab1}[,@var{tab2}]@dots{}}, where tabstops must be
5011 separated by commas. @acronym{POSIX} 1003.1-2001 (@pxref{Standards
5012 conformance}) does not allow this; use @option{-t
5013 @var{tab1}[,@var{tab2}]@dots{}} instead.
5019 @cindex initial tabs, converting
5020 Only convert initial tabs (those that precede all non-space or non-tab
5021 characters) on each line to spaces.
5028 @node unexpand invocation
5029 @section @command{unexpand}: Convert spaces to tabs
5033 @command{unexpand} writes the contents of each given @var{file}, or
5034 standard input if none are given or for a @var{file} of @samp{-}, to
5035 standard output, with strings of two or more space or tab characters
5036 converted to as many tabs as possible followed by as many spaces as are
5040 unexpand [@var{option}]@dots{} [@var{file}]@dots{}
5043 By default, @command{unexpand} converts only initial spaces and tabs (those
5044 that precede all non space or tab characters) on each line. It
5045 preserves backspace characters in the output; they decrement the column
5046 count for tab calculations. By default, tabs are set at every 8th
5049 The program accepts the following options. Also see @ref{Common options}.
5053 @item -t @var{tab1}[,@var{tab2}]@dots{}
5054 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5057 If only one tab stop is given, set the tabs @var{tab1} spaces apart
5058 instead of the default 8. Otherwise, set the tabs at columns
5059 @var{tab1}, @var{tab2}, @dots{} (numbered from 0), and leave spaces and
5060 tabs beyond the tabstops given unchanged. Tabstops can be separated by
5061 blanks as well as by commas. This option implies the @option{-a} option.
5063 On older systems, @command{unexpand} supports an obsolete option
5064 @option{-@var{tab1}[,@var{tab2}]@dots{}}, where tabstops must be
5065 separated by commas. (Unlike @option{-t}, this obsolete option does
5066 not imply @option{-a}.) @acronym{POSIX} 1003.1-2001 (@pxref{Standards
5067 conformance}) does not allow this; use @option{--first-only -t
5068 @var{tab1}[,@var{tab2}]@dots{}} instead.
5074 Convert all strings of two or more spaces or tabs, not just initial
5082 @node Directory listing
5083 @chapter Directory listing
5085 This chapter describes the @command{ls} command and its variants @command{dir}
5086 and @command{vdir}, which list information about files.
5089 * ls invocation:: List directory contents.
5090 * dir invocation:: Briefly ls.
5091 * vdir invocation:: Verbosely ls.
5092 * dircolors invocation:: Color setup for ls, etc.
5097 @section @command{ls}: List directory contents
5100 @cindex directory listing
5102 The @command{ls} program lists information about files (of any type,
5103 including directories). Options and file arguments can be intermixed
5104 arbitrarily, as usual.
5106 For non-option command-line arguments that are directories, by default
5107 @command{ls} lists the contents of directories, not recursively, and
5108 omitting files with names beginning with @samp{.}. For other non-option
5109 arguments, by default @command{ls} lists just the file name. If no
5110 non-option argument is specified, @command{ls} operates on the current
5111 directory, acting as if it had been invoked with a single argument of @samp{.}.
5114 By default, the output is sorted alphabetically, according to the locale
5115 settings in effect. @footnote{If you use a non-@acronym{POSIX}
5116 locale (e.g., by setting @env{LC_ALL} to @samp{en_US}), then @command{ls} may
5117 produce output that is sorted differently than you're accustomed to.
5118 In that case, set the @env{LC_ALL} environment variable to @samp{C}.}
5119 If standard output is
5120 a terminal, the output is in columns (sorted vertically) and control
5121 characters are output as question marks; otherwise, the output is listed
5122 one per line and control characters are output as-is.
5124 Because @command{ls} is such a fundamental program, it has accumulated many
5125 options over the years. They are described in the subsections below;
5126 within each section, options are listed alphabetically (ignoring case).
5127 The division of options into the subsections is not absolute, since some
5128 options affect more than one aspect of @command{ls}'s operation.
5132 Also see @ref{Common options}.
5135 * Which files are listed::
5136 * What information is listed::
5137 * Sorting the output::
5138 * More details about version sort::
5139 * General output formatting::
5140 * Formatting file timestamps::
5141 * Formatting the file names::
5145 @node Which files are listed
5146 @subsection Which files are listed
5148 These options determine which files @command{ls} lists information for.
5149 By default, any files and the contents of any directories on the command
5158 List all files in directories, including files that start with @samp{.}.
5163 @opindex --almost-all
5164 List all files in directories except for @file{.} and @file{..}.
5167 @itemx --ignore-backups
5169 @opindex --ignore-backups
5170 @cindex backup files, ignoring
5171 Do not list files that end with @samp{~}, unless they are given on the
5177 @opindex --directory
5178 List just the names of directories, as with other types of files, rather
5179 than listing their contents.
5180 @c The following sentence is the same as the one for -F.
5181 Do not follow symbolic links listed on the
5182 command line unless the @option{--dereference-command-line} (@option{-H}),
5183 @option{--dereference} (@option{-L}), or
5184 @option{--dereference-command-line-symlink-to-dir} options are specified.
5187 @itemx --dereference-command-line
5189 @opindex --dereference-command-line
5190 @cindex symbolic links, dereferencing
5191 If a command line argument specifies a symbolic link, show information
5192 for the file the link references rather than for the link itself.
5194 @itemx --dereference-command-line-symlink-to-dir
5195 @opindex --dereference-command-line-symlink-to-dir
5196 @cindex symbolic links, dereferencing
5197 Do not dereference symbolic links, with one exception:
5198 if a command line argument specifies a symbolic link that refers to
5199 a directory, show information for that directory rather than for the
5201 This is the default behavior when no other dereferencing-related
5202 option has been specified (@option{--classify} (@option{-F}),
5203 @option{--directory} (@option{-d}),
5205 @option{--dereference} (@option{-L}), or
5206 @option{--dereference-command-line} (@option{-H})).
5209 @itemx --ignore=PATTERN
5211 @opindex --ignore=@var{pattern}
5212 Do not list files whose names match the shell pattern (not regular
5213 expression) @var{pattern} unless they are given on the command line. As
5214 in the shell, an initial @samp{.} in a file name does not match a
5215 wildcard at the start of @var{pattern}. Sometimes it is useful
5216 to give this option several times. For example,
5219 $ ls --ignore='.??*' --ignore='.[^.]' --ignore='#*'
5222 The first option ignores names of length 3 or more that start with @samp{.},
5223 the second ignores all two-character names that start with @samp{.}
5224 except @samp{..}, and the third ignores names that start with @samp{#}.
5227 @itemx --dereference
5229 @opindex --dereference
5230 @cindex symbolic links, dereferencing
5231 When showing file information for a symbolic link, show information
5232 for the file the link references rather than the link itself.
5233 However, even with this option, @command{ls} still prints the name
5234 of the link itself, not the name of the file that the link points to.
5239 @opindex --recursive
5240 @cindex recursive directory listing
5241 @cindex directory listing, recursive
5242 List the contents of all directories recursively.
5247 @node What information is listed
5248 @subsection What information is listed
5250 These options affect the information that @command{ls} displays. By
5251 default, only file names are shown.
5257 @cindex hurd, author, printing
5258 List each file's author when producing long format directory listings.
5259 In GNU/Hurd, file authors can differ from their owners, but in other
5260 operating systems the two are the same.
5266 @cindex dired Emacs mode support
5267 With the long listing (@option{-l}) format, print an additional line after
5271 //DIRED// @var{beg1} @var{end1} @var{beg2} @var{end2} @dots{}
5275 The @var{begN} and @var{endN} are unsigned integers that record the
5276 byte position of the beginning and end of each file name in the output.
5277 This makes it easy for Emacs to find the names, even when they contain
5278 unusual characters such as space or newline, without fancy searching.
5280 If directories are being listed recursively (@option{-R}), output a similar
5281 line with offsets for each subdirectory name:
5283 //SUBDIRED// @var{beg1} @var{end1} @dots{}
5286 Finally, output a line of the form:
5288 //DIRED-OPTIONS// --quoting-style=@var{word}
5290 where @var{word} is the quoting style (@pxref{Formatting the file names}).
5292 Here is an actual example:
5295 $ mkdir -p a/sub/deeper a/sub2
5297 $ touch a/sub/deeper/file
5298 $ ls -gloRF --dired a
5301 -rw-r--r-- 1 0 Dec 3 00:50 f1
5302 -rw-r--r-- 1 0 Dec 3 00:50 f2
5303 drwxr-xr-x 3 4096 Dec 3 00:50 sub/
5304 drwxr-xr-x 2 4096 Dec 3 00:50 sub2/
5308 drwxr-xr-x 2 4096 Dec 3 00:50 deeper/
5312 -rw-r--r-- 1 0 Dec 3 00:50 file
5316 //DIRED// 49 51 86 88 123 126 162 166 222 228 288 292
5317 //SUBDIRED// 2 3 171 176 233 245 296 302
5318 //DIRED-OPTIONS// --quoting-style=literal
5321 Note that the pairs of offsets on the @samp{//DIRED//} line above delimit
5322 these names: @file{f1}, @file{f2}, @file{sub}, @file{sub2}, @file{deeper},
5324 The offsets on the @samp{//SUBDIRED//} line delimit the following
5325 directory names: @file{a}, @file{a/sub}, @file{a/sub/deeper}, @file{a/sub2}.
5327 Here is an example of how to extract the fifth entry name, @samp{deeper},
5328 corresponding to the pair of offsets, 222 and 228:
5331 $ ls -gloRF --dired a > out
5332 $ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
5336 Note that although the listing above includes a trailing slash
5337 for the @samp{deeper} entry, the offsets select the name without
5338 the trailing slash. However, if you invoke @command{ls} with @option{--dired}
5339 along with an option like @option{--escape} (aka @option{-b}) and operate
5340 on a file whose name contains special characters, notice that the backslash
5345 $ ls -blog --dired 'a b'
5346 -rw-r--r-- 1 0 Dec 3 00:52 a\ b
5348 //DIRED-OPTIONS// --quoting-style=escape
5351 If you use a quoting style that adds quote marks
5352 (e.g., @option{--quoting-style=c}), then the offsets include the quote marks.
5353 So beware that the user may select the quoting style via the environment
5354 variable @env{QUOTING_STYLE}. Hence, applications using @option{--dired}
5355 should either specify an explicit @option{--quoting-style=literal} option
5356 (aka @option{-N} or @option{--literal}) on the command line, or else be
5357 prepared to parse the escaped names.
5360 @opindex --full-time
5361 Produce long format directory listings, and list times in full. It is
5362 equivalent to using @option{--format=long} with
5363 @option{--time-style=full-iso} (@pxref{Formatting file timestamps}).
5367 Produce long format directory listings, but don't display owner information.
5373 Inhibit display of group information in a long format directory listing.
5374 (This is the default in some non-@sc{gnu} versions of @command{ls}, so we
5375 provide this option for compatibility.)
5383 @cindex inode number, printing
5384 Print the inode number (also called the file serial number and index
5385 number) of each file to the left of the file name. (This number
5386 uniquely identifies each file within a particular filesystem.)
5389 @itemx --format=long
5390 @itemx --format=verbose
5393 @opindex long ls @r{format}
5394 @opindex verbose ls @r{format}
5395 In addition to the name of each file, print the file type, permissions,
5396 number of hard links, owner name, group name, size, and
5397 timestamp (@pxref{Formatting file timestamps}), normally
5398 the modification time.
5400 Normally the size is printed as a byte count without punctuation, but
5401 this can be overridden (@pxref{Block size}). For example, @option{-h}
5402 prints an abbreviated, human-readable count, and
5403 @samp{--block-size="'1"} prints a byte count with the thousands
5404 separator of the current locale.
5406 For each directory that is listed, preface the files with a line
5407 @samp{total @var{blocks}}, where @var{blocks} is the total disk allocation
5408 for all files in that directory. The block size currently defaults to 1024
5409 bytes, but this can be overridden (@pxref{Block size}).
5410 The @var{blocks} computed counts each hard link separately;
5411 this is arguably a deficiency.
5413 @cindex permissions, output by @command{ls}
5414 The permissions listed are similar to symbolic mode specifications
5415 (@pxref{Symbolic Modes}). But @command{ls} combines multiple bits into the
5416 third character of each set of permissions as follows:
5419 If the setuid or setgid bit and the corresponding executable bit
5423 If the setuid or setgid bit is set but the corresponding executable bit
5427 If the sticky bit and the other-executable bit are both set.
5430 If the sticky bit is set but the other-executable bit is not set.
5433 If the executable bit is set and none of the above apply.
5439 Following the permission bits is a single character that specifies
5440 whether an alternate access method applies to the file. When that
5441 character is a space, there is no alternate access method. When it
5442 is a printing character (e.g., @samp{+}), then there is such a method.
5445 @itemx --numeric-uid-gid
5447 @opindex --numeric-uid-gid
5448 @cindex numeric uid and gid
5449 Produce long format directory listings, but
5450 display numeric UIDs and GIDs instead of the owner and group names.
5454 Produce long format directory listings, but don't display group information.
5455 It is equivalent to using @option{--format=long} with @option{--no-group} .
5461 @cindex disk allocation
5462 @cindex size of files, reporting
5463 Print the disk allocation of each file to the left of the file name.
5464 This is the amount of disk space used by the file, which is usually a
5465 bit more than the file's size, but it can be less if the file has holes.
5467 Normally the disk allocation is printed in units of
5468 1024 bytes, but this can be overridden (@pxref{Block size}).
5470 @cindex NFS mounts from BSD to HP-UX
5471 For files that are NFS-mounted from an HP-UX system to a BSD system,
5472 this option reports sizes that are half the correct values. On HP-UX
5473 systems, it reports sizes that are twice the correct values for files
5474 that are NFS-mounted from BSD systems. This is due to a flaw in HP-UX;
5475 it also affects the HP-UX @command{ls} program.
5482 @node Sorting the output
5483 @subsection Sorting the output
5485 @cindex sorting @command{ls} output
5486 These options change the order in which @command{ls} sorts the information
5487 it outputs. By default, sorting is done by character code
5488 (e.g., @acronym{ASCII} order).
5494 @itemx --time=status
5498 @opindex ctime@r{, printing or sorting by}
5499 @opindex status time@r{, printing or sorting by}
5500 @opindex use time@r{, printing or sorting files by}
5501 If the long listing format (e.g., @option{-l}, @option{-o}) is being used,
5502 print the status change time (the @samp{ctime} in the inode) instead of
5503 the modification time.
5504 When explicitly sorting by time (@option{--sort=time} or @option{-t})
5505 or when not using a long listing format,
5506 sort according to the status change time.
5510 @cindex unsorted directory listing
5511 @cindex directory order, listing by
5512 Primarily, like @option{-U}---do not sort; list the files in whatever
5513 order they are stored in the directory. But also enable @option{-a} (list
5514 all files) and disable @option{-l}, @option{--color}, and @option{-s} (if they
5515 were specified before the @option{-f}).
5521 @cindex reverse sorting
5522 Reverse whatever the sorting method is---e.g., list files in reverse
5523 alphabetical order, youngest first, smallest first, or whatever.
5525 @itemx --sort=directory
5527 @opindex directories@r{, sorting files by}
5528 Show directories first, then other files.
5534 @opindex size of files@r{, sorting files by}
5535 Sort by file size, largest first.
5541 @opindex modification time@r{, sorting files by}
5542 Sort by modification time (the @samp{mtime} in the inode), newest first.
5546 @itemx --time=access
5549 @opindex use time@r{, printing or sorting files by}
5550 @opindex atime@r{, printing or sorting files by}
5551 @opindex access time@r{, printing or sorting files by}
5552 If the long listing format (e.g., @option{--format=long}) is being used,
5553 print the last access time (the @samp{atime} in the inode).
5554 When explicitly sorting by time (@option{--sort=time} or @option{-t})
5555 or when not using a long listing format, sort according to the access time.
5561 @opindex none@r{, sorting option for @command{ls}}
5562 Do not sort; list the files in whatever order they are
5563 stored in the directory. (Do not do any of the other unrelated things
5564 that @option{-f} does.) This is especially useful when listing very large
5565 directories, since not doing any sorting can be noticeably faster.
5568 @itemx --sort=version
5571 @opindex version@r{, sorting option for @command{ls}}
5572 Sort by version name and number, lowest first. It behaves like a default
5573 sort, except that each sequence of decimal digits is treated numerically
5574 as an index/version number. (@xref{More details about version sort}.)
5577 @itemx --sort=extension
5580 @opindex extension@r{, sorting files by}
5581 Sort directory contents alphabetically by file extension (characters
5582 after the last @samp{.}); files with no extension are sorted first.
5587 @node More details about version sort
5588 @subsection More details about version sort
5590 The version sort takes into account the fact that file names frequently include
5591 indices or version numbers. Standard sorting functions usually do not produce
5592 the ordering that people expect because comparisons are made on a
5593 character-by-character basis. The version
5594 sort addresses this problem, and is especially useful when browsing
5595 directories that contain many files with indices/version numbers in their
5600 foo.zml-1.gz foo.zml-1.gz
5601 foo.zml-100.gz foo.zml-2.gz
5602 foo.zml-12.gz foo.zml-6.gz
5603 foo.zml-13.gz foo.zml-12.gz
5604 foo.zml-2.gz foo.zml-13.gz
5605 foo.zml-25.gz foo.zml-25.gz
5606 foo.zml-6.gz foo.zml-100.gz
5609 Note also that numeric parts with leading zeroes are considered as
5614 abc-1.007.tgz abc-1.007.tgz
5615 abc-1.012b.tgz abc-1.01a.tgz
5616 abc-1.01a.tgz abc-1.012b.tgz
5619 This functionality is implemented using the @code{strverscmp} function.
5620 @xref{String/Array Comparison, , , libc, The GNU C Library Reference Manual}.
5621 One result of that implementation decision is that @code{ls -v} does not
5622 use the locale category, @env{LC_COLLATE}. As a result, non-numeric prefixes
5623 are sorted as if @env{LC_COLLATE} were set to @code{C}.
5625 @node General output formatting
5626 @subsection General output formatting
5628 These options affect the appearance of the overall output.
5633 @itemx --format=single-column
5636 @opindex single-column @r{output of files}
5637 List one file per line. This is the default for @command{ls} when standard
5638 output is not a terminal.
5641 @itemx --format=vertical
5644 @opindex vertical @r{sorted files in columns}
5645 List files in columns, sorted vertically. This is the default for
5646 @command{ls} if standard output is a terminal. It is always the default
5647 for the @command{dir} and @command{d} programs.
5648 @sc{gnu} @command{ls} uses variable width columns to display as many files as
5649 possible in the fewest lines.
5651 @item --color [=@var{when}]
5653 @cindex color, distinguishing file types with
5654 Specify whether to use color for distinguishing file types. @var{when}
5655 may be omitted, or one of:
5658 @vindex none @r{color option}
5659 - Do not use color at all. This is the default.
5661 @vindex auto @r{color option}
5662 @cindex terminal, using color iff
5663 - Only use color if standard output is a terminal.
5665 @vindex always @r{color option}
5668 Specifying @option{--color} and no @var{when} is equivalent to
5669 @option{--color=always}.
5670 Piping a colorized listing through a pager like @command{more} or
5671 @command{less} usually produces unreadable results. However, using
5672 @code{more -f} does seem to work.
5676 @itemx --indicator-style=classify
5679 @opindex --indicator-style
5680 @cindex file type and executables, marking
5681 @cindex executables and file type, marking
5682 Append a character to each file name indicating the file type. Also,
5683 for regular files that are executable, append @samp{*}. The file type
5684 indicators are @samp{/} for directories, @samp{@@} for symbolic links,
5685 @samp{|} for FIFOs, @samp{=} for sockets, and nothing for regular files.
5686 @c The following sentence is the same as the one for -d.
5687 Do not follow symbolic links listed on the
5688 command line unless the @option{--dereference-command-line} (@option{-H}),
5689 @option{--dereference} (@option{-L}), or
5690 @option{--dereference-command-line-symlink-to-dir} options are specified.
5692 @item --indicator-style=@var{word}
5693 @opindex --indicator-style
5694 Append a character indicator with style @var{word} to entry names,
5698 Do not append any character indicator; this is the default.
5700 Append @samp{/} for directories, @samp{@@} for symbolic links, @samp{|}
5701 for FIFOs, @samp{=} for sockets, and nothing for regular files. This is
5702 the same as the @option{-p} or @option{--file-type} option.
5704 Append @samp{*} for executable regular files, otherwise behave as for
5705 @samp{file-type}. This is the same as the @option{-F} or
5706 @option{--classify} option.
5711 Print file sizes in 1024-byte blocks, overriding the default block
5712 size (@pxref{Block size}).
5713 This option is equivalent to @option{--block-size=1K}.
5716 @itemx --format=commas
5719 @opindex commas@r{, outputting between files}
5720 List files horizontally, with as many as will fit on each line,
5721 separated by @samp{, } (a comma and a space).
5725 @itemx --indicator-style=file-type
5726 @opindex --file-type
5727 @opindex --indicator-style
5728 @cindex file type, marking
5729 Append a character to each file name indicating the file type. This is
5730 like @option{-F}, except that executables are not marked.
5732 @item -x @var{format}
5733 @itemx --format=across
5734 @itemx --format=horizontal
5737 @opindex across@r{, listing files}
5738 @opindex horizontal@r{, listing files}
5739 List the files in columns, sorted horizontally.
5742 @itemx --tabsize=@var{cols}
5745 Assume that each tabstop is @var{cols} columns wide. The default is 8.
5746 @command{ls} uses tabs where possible in the output, for efficiency. If
5747 @var{cols} is zero, do not use tabs at all.
5750 @itemx --width=@var{cols}
5754 Assume the screen is @var{cols} columns wide. The default is taken
5755 from the terminal settings if possible; otherwise the environment
5756 variable @env{COLUMNS} is used if it is set; otherwise the default
5762 @node Formatting file timestamps
5763 @subsection Formatting file timestamps
5765 By default, file timestamps are listed in abbreviated form. Most
5766 locales use a timestamp like @samp{2002-03-30 23:45}. However, the
5767 default @acronym{POSIX} locale uses a date like @samp{Mar 30@ @ 2002}
5768 for non-recent timestamps, and a date-without-year and time like
5769 @samp{Mar 30 23:45} for recent timestamps.
5771 A timestamp is considered to be @dfn{recent} if it is less than six
5772 months old, and is not dated in the future. If a timestamp dated
5773 today is not listed in recent form, the timestamp is in the future,
5774 which means you probably have clock skew problems which may break
5775 programs like @command{make} that rely on file timestamps.
5777 The following option changes how file timestamps are printed.
5780 @item --time-style=@var{style}
5781 @opindex --time-style
5783 List timestamps in style @var{style}. The @var{style} should
5784 be one of the following:
5789 List timestamps using @var{format}, where @var{format} is interpreted
5790 like the format argument of @command{date} (@pxref{date invocation}).
5791 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
5792 @command{ls} to list timestamps like @samp{2002-03-30 23:45:56}. As
5793 with @command{date}, @var{format}'s interpretation is affected by the
5794 @env{LC_TIME} locale category.
5796 If @var{format} contains two format strings separated by a newline,
5797 the former is used for non-recent files and the latter for recent
5798 files; if you want output columns to line up, you may need to insert
5799 spaces in one of the two formats.
5802 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
5803 format with nanosecond precision, e.g., @samp{2002-03-30
5804 23:45:56.477817180 -0700}. This style is equivalent to
5805 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
5807 This is useful because the time output includes all the information that
5808 is available from the operating system. For example, this can help
5809 explain @command{make}'s behavior, since @acronym{GNU} @command{make}
5810 uses the full timestamp to determine whether a file is out of date.
5813 List @acronym{ISO} 8601 date and time in minutes, e.g.,
5814 @samp{2002-03-30 23:45}. These timestamps are shorter than
5815 @samp{full-iso} timestamps, and are usually good enough for everyday
5816 work. This style is equivalent to @samp{%Y-%m-%d %H:%M}.
5819 List @acronym{ISO} 8601 dates for non-recent timestamps (e.g.,
5820 @samp{2002-03-30@ }), and @acronym{ISO} 8601 month, day, hour, and
5821 minute for recent timestamps (e.g., @samp{03-30 23:45}). These
5822 timestamps are uglier than @samp{long-iso} timestamps, but they carry
5823 nearly the same information in a smaller space and their brevity helps
5824 @command{ls} output fit within traditional 80-column output lines.
5825 The following two @command{ls} invocations are equivalent:
5830 ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
5831 ls -l --time-style="iso"
5836 List timestamps in a locale-dependent form. For example, a Finnish
5837 locale might list non-recent timestamps like @samp{maalis 30@ @ 2002}
5838 and recent timestamps like @samp{maalis 30 23:45}. Locale-dependent
5839 timestamps typically consume more space than @samp{iso} timestamps and
5840 are harder for programs to parse because locale conventions vary so
5841 widely, but they are easier for many people to read.
5843 The @env{LC_TIME} locale category specifies the timestamp format. The
5844 default @acronym{POSIX} locale uses timestamps like @samp{Mar 30@
5845 @ 2002} and @samp{Mar 30 23:45}; in this locale, the following two
5846 @command{ls} invocations are equivalent:
5851 ls -l --time-style="+%b %e %Y$newline%b %e %H:%M"
5852 ls -l --time-style="locale"
5855 Other locales behave differently. For example, in a German locale,
5856 @option{--time-style="locale"} might be equivalent to
5857 @option{--time-style="+%e. %b %Y $newline%e. %b %H:%M"}
5858 and might generate timestamps like @samp{30. M@"ar 2002@ } and
5859 @samp{30. M@"ar 23:45}.
5861 @item posix-@var{style}
5863 List @acronym{POSIX}-locale timestamps if the @env{LC_TIME} locale
5864 category is @acronym{POSIX}, @var{style} timestamps otherwise. For
5865 example, the default style, which is @samp{posix-long-iso}, lists
5866 timestamps like @samp{Mar 30@ @ 2002} and @samp{Mar 30 23:45} when in
5867 the @acronym{POSIX} locale, and like @samp{2002-03-30 23:45} otherwise.
5872 You can specify the default value of the @option{--time-style} option
5873 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
5874 the default style is @samp{posix-long-iso}. @acronym{GNU} Emacs 21 and
5875 later can parse @acronym{ISO} dates, but older Emacs versions do not, so if
5876 you are using an older version of Emacs and specify a non-@acronym{POSIX}
5877 locale, you may need to set @samp{TIME_STYLE="locale"}.
5880 @node Formatting the file names
5881 @subsection Formatting the file names
5883 These options change how file names themselves are printed.
5889 @itemx --quoting-style=escape
5892 @opindex --quoting-style
5893 @cindex backslash sequences for file names
5894 Quote nongraphic characters in file names using alphabetic and octal
5895 backslash sequences like those used in C.
5899 @itemx --quoting-style=literal
5902 @opindex --quoting-style
5903 Do not quote file names.
5906 @itemx --hide-control-chars
5908 @opindex --hide-control-chars
5909 Print question marks instead of nongraphic characters in file names.
5910 This is the default if the output is a terminal and the program is
5915 @itemx --quoting-style=c
5917 @opindex --quote-name
5918 @opindex --quoting-style
5919 Enclose file names in double quotes and quote nongraphic characters as
5922 @item --quoting-style=@var{word}
5923 @opindex --quoting-style
5924 @cindex quoting style
5925 Use style @var{word} to quote file names and other strings that may
5926 contain arbitrary characters. The @var{word} should
5927 be one of the following:
5930 Output strings as-is; this is the same as the @option{-N} or
5931 @option{--literal} option.
5933 Quote strings for the shell if they contain shell metacharacters or would
5934 cause ambiguous output.
5935 The quoting is suitable for @acronym{POSIX}-compatible shells like
5936 @command{bash}, but it does not always work for incompatible shells
5939 Quote strings for the shell, even if they would normally not require quoting.
5941 Quote strings as for C character string literals, including the
5942 surrounding double-quote characters; this is the same as the
5943 @option{-Q} or @option{--quote-name} option.
5945 Quote strings as for C character string literals, except omit the
5946 surrounding double-quote
5947 characters; this is the same as the @option{-b} or @option{--escape} option.
5949 Quote strings as for C character string literals, except use
5950 surrounding quotation marks appropriate for the
5953 @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
5954 Quote strings as for C character string literals, except use
5955 surrounding quotation marks appropriate for the locale, and quote
5956 @t{`like this'} instead of @t{"like
5957 this"} in the default C locale. This looks nicer on many displays.
5960 You can specify the default value of the @option{--quoting-style} option
5961 with the environment variable @env{QUOTING_STYLE}. If that environment
5962 variable is not set, the default value is @samp{literal}, but this
5963 default may change to @samp{shell} in a future version of this package.
5965 @item --show-control-chars
5966 @opindex --show-control-chars
5967 Print nongraphic characters as-is in file names.
5968 This is the default unless the output is a terminal and the program is
5974 @node dir invocation
5975 @section @command{dir}: Briefly list directory contents
5978 @cindex directory listing, brief
5980 @command{dir} (also installed as @command{d}) is equivalent to @code{ls -C
5981 -b}; that is, by default files are listed in columns, sorted vertically,
5982 and special characters are represented by backslash escape sequences.
5984 @xref{ls invocation, @command{ls}}.
5987 @node vdir invocation
5988 @section @command{vdir}: Verbosely list directory contents
5991 @cindex directory listing, verbose
5993 @command{vdir} (also installed as @command{v}) is equivalent to @code{ls -l
5994 -b}; that is, by default files are listed in long format and special
5995 characters are represented by backslash escape sequences.
5997 @node dircolors invocation
5998 @section @command{dircolors}: Color setup for @command{ls}
6002 @cindex setup for color
6004 @command{dircolors} outputs a sequence of shell commands to set up the
6005 terminal for color output from @command{ls} (and @command{dir}, etc.).
6009 eval `dircolors [@var{option}]@dots{} [@var{file}]`
6012 If @var{file} is specified, @command{dircolors} reads it to determine which
6013 colors to use for which file types and extensions. Otherwise, a
6014 precompiled database is used. For details on the format of these files,
6015 run @samp{dircolors --print-database}.
6018 @vindex SHELL @r{environment variable, and color}
6019 The output is a shell command to set the @env{LS_COLORS} environment
6020 variable. You can specify the shell syntax to use on the command line,
6021 or @command{dircolors} will guess it from the value of the @env{SHELL}
6022 environment variable.
6024 The program accepts the following options. Also see @ref{Common options}.
6029 @itemx --bourne-shell
6032 @opindex --bourne-shell
6033 @cindex Bourne shell syntax for color setup
6034 @cindex @command{sh} syntax for color setup
6035 Output Bourne shell commands. This is the default if the @env{SHELL}
6036 environment variable is set and does not end with @samp{csh} or
6045 @cindex C shell syntax for color setup
6046 @cindex @command{csh} syntax for color setup
6047 Output C shell commands. This is the default if @code{SHELL} ends with
6048 @command{csh} or @command{tcsh}.
6051 @itemx --print-database
6053 @opindex --print-database
6054 @cindex color database, printing
6055 @cindex database for color setup, printing
6056 @cindex printing color database
6057 Print the (compiled-in) default color configuration database. This
6058 output is itself a valid configuration file, and is fairly descriptive
6059 of the possibilities.
6066 @node Basic operations
6067 @chapter Basic operations
6069 @cindex manipulating files
6071 This chapter describes the commands for basic file manipulation:
6072 copying, moving (renaming), and deleting (removing).
6075 * cp invocation:: Copy files.
6076 * dd invocation:: Convert and copy a file.
6077 * install invocation:: Copy files and set attributes.
6078 * mv invocation:: Move (rename) files.
6079 * rm invocation:: Remove files or directories.
6080 * shred invocation:: Remove files more securely.
6085 @section @command{cp}: Copy files and directories
6088 @cindex copying files and directories
6089 @cindex files, copying
6090 @cindex directories, copying
6092 @command{cp} copies files (or, optionally, directories). The copy is
6093 completely independent of the original. You can either copy one file to
6094 another, or copy arbitrarily many files to a destination directory.
6098 cp [@var{option}]@dots{} @var{source} @var{dest}
6099 cp [@var{option}]@dots{} @var{source}@dots{} @var{directory}
6102 If the last argument names an existing directory, @command{cp} copies each
6103 @var{source} file into that directory (retaining the same name).
6104 Otherwise, if only two files are given, it copies the first onto the
6105 second. It is an error if the last argument is not a directory and more
6106 than two non-option arguments are given.
6108 Generally, files are written just as they are read. For exceptions,
6109 see the @option{--sparse} option below.
6111 By default, @command{cp} does not copy directories. However, the
6112 @option{-R}, @option{-a}, and @option{-r} options cause @command{cp} to
6113 copy recursively by descending into source directories and copying files
6114 to corresponding destination directories.
6116 By default, @command{cp} follows symbolic links only when not copying
6117 recursively. This default can be overridden with the
6118 @option{--archive} (@option{-a}), @option{-d}, @option{--dereference}
6119 (@option{-L}), @option{--no-dereference} (@option{-P}), and
6120 @option{-H} options. If more than one of these options is specified,
6121 the last one silently overrides the others.
6123 By default, @command{cp} copies the contents of special files only
6124 when not copying recursively. This default can be overridden with the
6125 @option{--copy-contents} option.
6127 @cindex self-backups
6128 @cindex backups, making only
6129 @command{cp} generally refuses to copy a file onto itself, with the
6130 following exception: if @option{--force --backup} is specified with
6131 @var{source} and @var{dest} identical, and referring to a regular file,
6132 @command{cp} will make a backup file, either regular or numbered, as
6133 specified in the usual ways (@pxref{Backup options}). This is useful when
6134 you simply want to make a backup of an existing file before changing it.
6136 The program accepts the following options. Also see @ref{Common options}.
6143 Preserve as much as possible of the structure and attributes of the
6144 original files in the copy (but do not attempt to preserve internal
6145 directory structure; i.e., @samp{ls -U} may list the entries in a copied
6146 directory in a different order).
6147 Equivalent to @option{-dpPR}.
6150 @itemx @w{@kbd{--backup}[=@var{method}]}
6153 @vindex VERSION_CONTROL
6154 @cindex backups, making
6155 @xref{Backup options}.
6156 Make a backup of each file that would otherwise be overwritten or removed.
6157 As a special case, @command{cp} makes a backup of @var{source} when the force
6158 and backup options are given and @var{source} and @var{dest} are the same
6159 name for an existing, regular file. One useful application of this
6160 combination of options is this tiny Bourne shell script:
6164 # Usage: backup FILE...
6165 # Create a @sc{gnu}-style backup of each listed FILE.
6167 cp --backup --force "$i" "$i"
6171 @item --copy-contents
6172 @cindex directories, copying recursively
6173 @cindex copying directories recursively
6174 @cindex recursively copying directories
6175 @cindex non-directories, copying as special files
6176 If copying recursively, copy the contents of any special files (e.g.,
6177 FIFOs and device files) as if they were regular files. This means
6178 trying to read the data in each source file and writing it to the
6179 destination. It is usually a mistake to use this option, as it
6180 normally has undesirable effects on special files like FIFOs and the
6181 ones typically found in the @file{/dev} directory. In most cases,
6182 @code{cp -R --copy-contents} will hang indefinitely trying to read
6183 from FIFOs and special files like @file{/dev/console}, and it will
6184 fill up your destination disk if you use it to copy @file{/dev/zero}.
6185 This option has no effect unless copying recursively, and it does not
6186 affect the copying of symbolic links.
6190 @cindex symbolic links, copying
6191 @cindex hard links, preserving
6192 Copy symbolic links as symbolic links rather than copying the files that
6193 they point to, and preserve hard links between source files in the copies.
6194 Equivalent to @option{--no-dereference --preserve=links}.
6200 When copying without this option and an existing destination file cannot
6201 be opened for writing, the copy fails. However, with @option{--force}),
6202 when a destination file cannot be opened, @command{cp} then unlinks it and
6203 tries to open it again. Contrast this behavior with that enabled by
6204 @option{--link} and @option{--symbolic-link}, whereby the destination file
6205 is never opened but rather is unlinked unconditionally. Also see the
6206 description of @option{--remove-destination}.
6210 If a command line argument specifies a symbolic link, then copy the
6211 file it points to rather than the symbolic link itself. However,
6212 copy (preserving its nature) any symbolic link that is encountered
6213 via recursive traversal.
6216 @itemx --interactive
6218 @opindex --interactive
6219 Prompt whether to overwrite existing regular destination files.
6225 Make hard links instead of copies of non-directories.
6228 @itemx --dereference
6230 @opindex --dereference
6231 Always follow symbolic links.
6234 @itemx --no-dereference
6236 @opindex --no-dereference
6237 @cindex symbolic links, copying
6238 Copy symbolic links as symbolic links rather than copying the files that
6242 @itemx @w{@kbd{--preserve}[=@var{attribute_list}]}
6245 @cindex file information, preserving
6246 Preserve the specified attributes of the original files.
6247 If specified, the @var{attribute_list} must be a comma-separated list
6248 of one or more of the following strings:
6252 Preserve the permission attributes.
6254 Preserve the owner and group. On most modern systems,
6255 only the super-user may change the owner of a file, and regular users
6256 may preserve the group ownership of a file only if they happen to be
6257 a member of the desired group.
6259 Preserve the times of last access and last modification.
6261 Preserve in the destination files
6262 any links between corresponding source files.
6263 @c Give examples illustrating how hard links are preserved.
6264 @c Also, show how soft links map to hard links with -L and -H.
6266 Preserve all file attributes.
6267 Equivalent to specifying all of the above.
6268 @c Mention ACLs here.
6271 Using @option{--preserve} with no @var{attribute_list} is equivalent
6272 to @option{--preserve=mode,ownership,timestamps}.
6274 In the absence of this option, each destination file is created with the
6275 permissions of the corresponding source file, minus the bits set in the
6276 umask and minus the set-user-id and set-group-id bits. @xref{File permissions}.
6278 @itemx @w{@kbd{--no-preserve}=@var{attribute_list}}
6279 @cindex file information, preserving
6280 Do not preserve the specified attributes. The @var{attribute_list}
6281 has the same form as for @option{--preserve}.
6285 @cindex parent directories and @command{cp}
6286 Form the name of each destination file by appending to the target
6287 directory a slash and the specified name of the source file. The last
6288 argument given to @command{cp} must be the name of an existing directory.
6289 For example, the command:
6292 cp --parents a/b/c existing_dir
6296 copies the file @file{a/b/c} to @file{existing_dir/a/b/c}, creating
6297 any missing intermediate directories.
6299 @itemx @w{@kbd{--reply}[=@var{how}]}
6301 @cindex interactivity
6302 Using @option{--reply=yes} makes @command{cp} act as if @samp{yes} were
6303 given as a response to every prompt about a destination file. That effectively
6304 cancels any preceding @option{--interactive} or @option{-i} option.
6305 Specify @option{--reply=no} to make @command{cp} act as if @samp{no} were
6306 given as a response to every prompt about a destination file.
6307 Specify @option{--reply=query} to make @command{cp} prompt the user
6308 about each existing destination file.
6315 @opindex --recursive
6316 @cindex directories, copying recursively
6317 @cindex copying directories recursively
6318 @cindex recursively copying directories
6319 @cindex non-directories, copying as special files
6320 Copy directories recursively. Symbolic links are not followed by
6321 default; see the @option{--archive} (@option{-a}), @option{-d},
6322 @option{--dereference} (@option{-L}), @option{--no-dereference}
6323 (@option{-P}), and @option{-H} options. Special files are copied by
6324 creating a destination file of the same type as the source; see the
6325 @option{--copy-contents} option. It is not portable to use
6326 @option{-r} to copy symbolic links or special files. On some
6327 non-@sc{gnu} systems, @option{-r} implies the equivalent of
6328 @option{-L} and @option{--copy-contents} for historical reasons.
6329 Also, it is not portable to use @option{-R} to copy symbolic links
6330 unless you also specify @option{-P}, as @acronym{POSIX} allows
6331 implementations that dereference symbolic links by default.
6333 @item --remove-destination
6334 @opindex --remove-destination
6335 Remove each existing destination file before attempting to open it
6336 (contrast with @option{-f} above).
6338 @item --sparse=@var{when}
6339 @opindex --sparse=@var{when}
6340 @cindex sparse files, copying
6341 @cindex holes, copying files with
6342 @findex read @r{system call, and holes}
6343 A @dfn{sparse file} contains @dfn{holes}---a sequence of zero bytes that
6344 does not occupy any physical disk blocks; the @samp{read} system call
6345 reads these as zeroes. This can both save considerable disk space and
6346 increase speed, since many binary files contain lots of consecutive zero
6347 bytes. By default, @command{cp} detects holes in input source files via a crude
6348 heuristic and makes the corresponding output file sparse as well.
6350 The @var{when} value can be one of the following:
6353 The default behavior: the output file is sparse if the input file is sparse.
6356 Always make the output file sparse. This is useful when the input
6357 file resides on a filesystem that does not support sparse files (the
6358 most notable example is @samp{efs} filesystems in SGI IRIX 5.3 and
6359 earlier), but the output file is on another type of filesystem.
6362 Never make the output file sparse.
6363 This is useful in creating a file for use with the @command{mkswap} command,
6364 since such a file must not have any holes.
6367 @optStripTrailingSlashes
6370 @itemx --symbolic-link
6372 @opindex --symbolic-link
6373 @cindex symbolic links, copying with
6374 Make symbolic links instead of copies of non-directories. All source
6375 file names must be absolute (starting with @samp{/}) unless the
6376 destination files are in the current directory. This option merely
6377 results in an error message on systems that do not support symbolic links.
6387 Print the name of each file before copying it.
6392 @itemx --one-file-system
6394 @opindex --one-file-system
6395 @cindex filesystems, omitting copying to different
6396 Skip subdirectories that are on different filesystems from the one that
6397 the copy started on.
6398 However, mount point directories @emph{are} copied.
6406 @section @command{dd}: Convert and copy a file
6409 @cindex converting while copying a file
6411 @command{dd} copies a file (from standard input to standard output, by
6412 default) with a changeable I/O block size, while optionally performing
6413 conversions on it. Synopsis:
6416 dd [@var{option}]@dots{}
6419 The program accepts the following options. Also see @ref{Common options}.
6421 @cindex multipliers after numbers
6422 The numeric-valued options below (@var{bytes} and @var{blocks}) can be
6423 followed by a multiplier: @samp{b}=512, @samp{c}=1,
6424 @samp{w}=2, @samp{x@var{m}}=@var{m}, or any of the
6425 standard block size suffixes like @samp{k}=1024 (@pxref{Block size}).
6427 Use different @command{dd} invocations to use different block sizes for
6428 skipping and I/O. For example, the following shell commands copy data
6429 in 512 KiB blocks between a disk and a tape, but do not save or restore a
6430 4 KiB label at the start of the disk:
6433 disk=/dev/rdsk/c0t1d0s2
6436 # Copy all but the label from disk to tape.
6437 (dd bs=4k skip=1 count=0 && dd bs=512k) <$disk >$tape
6439 # Copy from tape back to disk, but leave the disk label alone.
6440 (dd bs=4k seek=1 count=0 && dd bs=512k) <$tape >$disk
6443 Note that sending a @samp{SIGUSR1} signal to a running @command{dd}
6444 process makes it print to standard error the number of records read
6445 and written so far, then to resume copying. In the example below,
6446 @command{dd} is run in the background to copy 10 million blocks.
6447 The @command{kill} command makes it output the first pair of
6448 intermediate record counts,
6449 and when @command{dd} completes, it outputs the final pair.
6452 $ dd if=/dev/zero of=/dev/null count=10M & pid=$!
6453 $ kill -USR1 $pid; sleep 99
6454 5403604+0 records in
6455 5403604+0 records out
6456 10485760+0 records in
6457 10485760+0 records out
6465 Read from @var{file} instead of standard input.
6469 Write to @var{file} instead of standard output. Unless
6470 @samp{conv=notrunc} is given, @command{dd} truncates @var{file} to zero
6471 bytes (or the size specified with @samp{seek=}).
6473 @item ibs=@var{bytes}
6475 @cindex block size of input
6476 @cindex input block size
6477 Read @var{bytes} bytes at a time.
6479 @item obs=@var{bytes}
6481 @cindex block size of output
6482 @cindex output block size
6483 Write @var{bytes} bytes at a time.
6485 @item bs=@var{bytes}
6488 Both read and write @var{bytes} bytes at a time. This overrides
6489 @samp{ibs} and @samp{obs}.
6491 @item cbs=@var{bytes}
6493 @cindex block size of conversion
6494 @cindex conversion block size
6495 Convert @var{bytes} bytes at a time.
6497 @item skip=@var{blocks}
6499 Skip @var{blocks} @samp{ibs}-byte blocks in the input file before copying.
6501 @item seek=@var{blocks}
6503 Skip @var{blocks} @samp{obs}-byte blocks in the output file before copying.
6505 @item count=@var{blocks}
6507 Copy @var{blocks} @samp{ibs}-byte blocks from the input file, instead
6508 of everything until the end of the file.
6510 @item conv=@var{conversion}[,@var{conversion}]@dots{}
6512 Convert the file as specified by the @var{conversion} argument(s).
6513 (No spaces around any comma(s).)
6520 @opindex ascii@r{, converting to}
6521 Convert @acronym{EBCDIC} to @acronym{ASCII}.
6524 @opindex ebcdic@r{, converting to}
6525 Convert @acronym{ASCII} to @acronym{EBCDIC}.
6528 @opindex alternate ebcdic@r{, converting to}
6529 Convert @acronym{ASCII} to alternate @acronym{EBCDIC}.
6532 @opindex block @r{(space-padding)}
6533 For each line in the input, output @samp{cbs} bytes, replacing the
6534 input newline with a space and padding with spaces as necessary.
6538 Replace trailing spaces in each @samp{cbs}-sized input block with a
6542 @opindex lcase@r{, converting to}
6543 Change uppercase letters to lowercase.
6546 @opindex ucase@r{, converting to}
6547 Change lowercase letters to uppercase.
6550 @opindex swab @r{(byte-swapping)}
6551 @cindex byte-swapping
6552 Swap every pair of input bytes. @sc{gnu} @command{dd}, unlike others, works
6553 when an odd number of bytes are read---the last byte is simply copied
6554 (since there is nothing to swap it with).
6558 @cindex read errors, ignoring
6559 Continue after read errors.
6563 @cindex truncating output file, avoiding
6564 Do not truncate the output file.
6567 @opindex sync @r{(padding with nulls)}
6568 Pad every input block to size of @samp{ibs} with trailing zero bytes.
6569 When used with @samp{block} or @samp{unblock}, pad with spaces instead of
6578 @node install invocation
6579 @section @command{install}: Copy files and set attributes
6582 @cindex copying files and setting attributes
6584 @command{install} copies files while setting their permission modes and, if
6585 possible, their owner and group. Synopses:
6588 install [@var{option}]@dots{} @var{source} @var{dest}
6589 install [@var{option}]@dots{} @var{source}@dots{} @var{directory}
6590 install -d [@var{option}]@dots{} @var{directory}@dots{}
6593 In the first of these, the @var{source} file is copied to the @var{dest}
6594 target file. In the second, each of the @var{source} files are copied
6595 to the destination @var{directory}. In the last, each @var{directory}
6596 (and any missing parent directories) is created.
6598 @cindex Makefiles, installing programs in
6599 @command{install} is similar to @command{cp}, but allows you to control the
6600 attributes of destination files. It is typically used in Makefiles to
6601 copy programs into their destination directories. It refuses to copy
6602 files onto themselves.
6604 The program accepts the following options. Also see @ref{Common options}.
6612 Ignored; for compatibility with old Unix versions of @command{install}.
6617 @opindex --directory
6618 @cindex directories, creating with given attributes
6619 @cindex parent directories, creating missing
6620 @cindex leading directories, creating missing
6621 Create each given directory and any missing parent directories, setting
6622 the owner, group and mode as given on the command line or to the
6623 defaults. It also gives any parent directories it creates those
6624 attributes. (This is different from the SunOS 4.x @command{install}, which
6625 gives directories that it creates the default attributes.)
6627 @item -g @var{group}
6628 @itemx --group=@var{group}
6631 @cindex group ownership of installed files, setting
6632 Set the group ownership of installed files or directories to
6633 @var{group}. The default is the process' current group. @var{group}
6634 may be either a group name or a numeric group id.
6637 @itemx --mode=@var{mode}
6640 @cindex permissions of installed files, setting
6641 Set the permissions for the installed file or directory to @var{mode},
6642 which can be either an octal number, or a symbolic mode as in
6643 @command{chmod}, with 0 as the point of departure (@pxref{File
6644 permissions}). The default mode is @samp{u=rwx,go=rx}---read, write,
6645 and execute for the owner, and read and execute for group and other.
6647 @item -o @var{owner}
6648 @itemx --owner=@var{owner}
6651 @cindex ownership of installed files, setting
6652 @cindex appropriate privileges
6653 @vindex root @r{as default owner}
6654 If @command{install} has appropriate privileges (is run as root), set the
6655 ownership of installed files or directories to @var{owner}. The default
6656 is @code{root}. @var{owner} may be either a user name or a numeric user
6660 @itemx --preserve-timestamps
6662 @opindex --preserve-timestamps
6663 @cindex timestamps of installed files, preserving
6664 Set the time of last access and the time of last modification of each
6665 installed file to match those of each corresponding original file.
6666 When a file is installed without this option, its last access and
6667 last modification times are both set to the time of installation.
6668 This option is useful if you want to use the last modification times
6669 of installed files to keep track of when they were last built as opposed
6670 to when they were last installed.
6676 @cindex symbol table information, stripping
6677 @cindex stripping symbol table information
6678 Strip the symbol tables from installed binary executables.
6688 Print the name of each file before copying it.
6698 @section @command{mv}: Move (rename) files
6702 @command{mv} moves or renames files (or directories). Synopsis:
6705 mv [@var{option}]@dots{} @var{source} @var{dest}
6706 mv [@var{option}]@dots{} @var{source}@dots{} @var{directory}
6709 If the last argument names an existing directory, @command{mv} moves each
6710 other given file into a file with the same name in that directory.
6711 Otherwise, if only two files are given, it renames the first as
6712 the second. It is an error if the last argument is not a directory
6713 and more than two files are given.
6715 @command{mv} can move any type of file from one filesystem to another.
6716 Prior to version @code{4.0} of the fileutils,
6717 @command{mv} could move only regular files between filesystems.
6718 For example, now @command{mv} can move an entire directory hierarchy
6719 including special device files from one partition to another. It first
6720 uses some of the same code that's used by @code{cp -a} to copy the
6721 requested directories and files, then (assuming the copy succeeded)
6722 it removes the originals. If the copy fails, then the part that was
6723 copied to the destination partition is removed. If you were to copy
6724 three directories from one partition to another and the copy of the first
6725 directory succeeded, but the second didn't, the first would be left on
6726 the destination partition and the second and third would be left on the
6729 @cindex prompting, and @command{mv}
6730 If a destination file exists but is normally unwritable, standard input
6731 is a terminal, and the @option{-f} or @option{--force} option is not given,
6732 @command{mv} prompts the user for whether to replace the file. (You might
6733 own the file, or have write permission on its directory.) If the
6734 response does not begin with @samp{y} or @samp{Y}, the file is skipped.
6736 @emph{Warning}: If you try to move a symlink that points to a directory,
6737 and you specify the symlink with a trailing slash, then @command{mv}
6738 doesn't move the symlink but instead moves the directory referenced
6739 by the symlink. @xref{Trailing slashes}.
6741 The program accepts the following options. Also see @ref{Common options}.
6751 @cindex prompts, omitting
6752 Do not prompt the user before removing a destination file.
6755 @itemx --interactive
6757 @opindex --interactive
6758 @cindex prompts, forcing
6759 Prompt whether to overwrite each existing destination file, regardless
6760 of its permissions. If the response does not begin with @samp{y} or
6761 @samp{Y}, the file is skipped.
6763 @itemx @w{@kbd{--reply}[=@var{how}]}
6765 @cindex interactivity
6766 Specifying @option{--reply=yes} is equivalent to using @option{--force}.
6767 Specify @option{--reply=no} to make @command{mv} act as if @samp{no} were
6768 given as a response to every prompt about a destination file.
6769 Specify @option{--reply=query} to make @command{mv} prompt the user
6770 about each existing destination file.
6776 @cindex newer files, moving only
6777 Do not move a non-directory that has an existing destination with the
6778 same or newer modification time.
6784 Print the name of each file before moving it.
6786 @optStripTrailingSlashes
6800 @section @command{rm}: Remove files or directories
6803 @cindex removing files or directories
6805 @command{rm} removes each given @var{file}. By default, it does not remove
6806 directories. Synopsis:
6809 rm [@var{option}]@dots{} [@var{file}]@dots{}
6812 @cindex prompting, and @command{rm}
6813 If a file is unwritable, standard input is a terminal, and the @option{-f}
6814 or @option{--force} option is not given, or the @option{-i} or
6815 @option{--interactive} option @emph{is} given, @command{rm} prompts the user
6816 for whether to remove the file. If the response does not begin with
6817 @samp{y} or @samp{Y}, the file is skipped.
6819 @emph{Warning}: If you use @command{rm} to remove a file, it is usually
6820 possible to recover the contents of that file. If you want more assurance
6821 that the contents are truly unrecoverable, consider using @command{shred}.
6823 The program accepts the following options. Also see @ref{Common options}.
6830 @opindex --directory
6831 @cindex directories, removing with @code{unlink}
6834 Attempt to remove directories using the @code{unlink} function rather than
6835 the @code{rmdir} function, and
6836 don't require a directory to be empty before trying to unlink it. This works
6837 only if you have appropriate privileges and if your operating system supports
6838 @code{unlink} for directories. Because unlinking a directory causes any files
6839 in the deleted directory to become unreferenced, it is wise to @command{fsck}
6840 the filesystem after doing this.
6846 Ignore nonexistent files and never prompt the user.
6847 Ignore any previous @option{--interactive} (@option{-i}) option.
6850 @itemx --interactive
6852 @opindex --interactive
6853 Prompt whether to remove each file. If the response does not begin
6854 with @samp{y} or @samp{Y}, the file is skipped.
6855 Ignore any previous @option{--force} (@option{-f}) option.
6857 @itemx --preserve-root
6858 @opindex --preserve-root
6859 @cindex root directory, disallow recursive destruction
6860 Fail upon any attempt to remove the filesystem root, @file{/},
6861 when used with the @option{--recursive} option.
6862 Without @option{--recursive}, this option has no effect.
6863 @xref{Treating / specially}.
6865 @itemx --no-preserve-root
6866 @opindex --no-preserve-root
6867 @cindex root directory, allow recursive destruction
6868 Cancel the effect of any preceding @option{--preserve-root} option.
6869 @xref{Treating / specially}.
6876 @opindex --recursive
6877 @cindex directories, removing (recursively)
6878 Remove the contents of directories recursively.
6884 Print the name of each file before removing it.
6888 @cindex files beginning with @samp{-}, removing
6889 @cindex @samp{-}, removing files beginning with
6890 One common question is how to remove files whose names begin with a
6891 @samp{-}. @sc{gnu} @command{rm}, like every program that uses the @code{getopt}
6892 function to parse its arguments, lets you use the @samp{--} option to
6893 indicate that all following arguments are non-options. To remove a file
6894 called @file{-f} in the current directory, you could type either:
6907 @opindex - @r{and Unix @command{rm}}
6908 The Unix @command{rm} program's use of a single @samp{-} for this purpose
6909 predates the development of the getopt standard syntax.
6914 @node shred invocation
6915 @section @command{shred}: Remove files more securely
6918 @cindex data, erasing
6919 @cindex erasing data
6921 @command{shred} overwrites devices or files, to help prevent even
6922 very expensive hardware from recovering the data.
6924 Ordinarily when you remove a file (@pxref{rm invocation}), the data is
6925 not actually destroyed. Only the index listing where the file is
6926 stored is destroyed, and the storage is made available for reuse.
6927 There are undelete utilities that will attempt to reconstruct the index
6928 and can bring the file back if the parts were not reused.
6930 On a busy system with a nearly-full drive, space can get reused in a few
6931 seconds. But there is no way to know for sure. If you have sensitive
6932 data, you may want to be sure that recovery is not possible by actually
6933 overwriting the file with non-sensitive data.
6935 However, even after doing that, it is possible to take the disk back
6936 to a laboratory and use a lot of sensitive (and expensive) equipment
6937 to look for the faint ``echoes'' of the original data underneath the
6938 overwritten data. If the data has only been overwritten once, it's not
6941 The best way to remove something irretrievably is to destroy the media
6942 it's on with acid, melt it down, or the like. For cheap removable media
6943 like floppy disks, this is the preferred method. However, hard drives
6944 are expensive and hard to melt, so the @command{shred} utility tries
6945 to achieve a similar effect non-destructively.
6947 This uses many overwrite passes, with the data patterns chosen to
6948 maximize the damage they do to the old data. While this will work on
6949 floppies, the patterns are designed for best effect on hard drives.
6950 For more details, see the source code and Peter Gutmann's paper
6951 @cite{Secure Deletion of Data from Magnetic and Solid-State Memory},
6952 from the proceedings of the Sixth USENIX Security Symposium (San Jose,
6953 California, 22--25 July, 1996). The paper is also available online
6954 @url{http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html}.
6956 @strong{Please note} that @command{shred} relies on a very important assumption:
6957 that the filesystem overwrites data in place. This is the traditional
6958 way to do things, but many modern filesystem designs do not satisfy this
6959 assumption. Exceptions include:
6964 Log-structured or journaled filesystems, such as those supplied with
6965 AIX and Solaris, and JFS, ReiserFS, XFS, Ext3, etc.
6968 Filesystems that write redundant data and carry on even if some writes
6969 fail, such as RAID-based filesystems.
6972 Filesystems that make snapshots, such as Network Appliance's NFS server.
6975 Filesystems that cache in temporary locations, such as NFS version 3
6979 Compressed filesystems.
6982 If you are not sure how your filesystem operates, then you should assume
6983 that it does not overwrite data in place, which means that shred cannot
6984 reliably operate on regular files in your filesystem.
6986 Generally speaking, it is more reliable to shred a device than a file,
6987 since this bypasses the problem of filesystem design mentioned above.
6988 However, even shredding devices is not always completely reliable. For
6989 example, most disks map out bad sectors invisibly to the application; if
6990 the bad sectors contain sensitive data, @command{shred} won't be able to
6993 @command{shred} makes no attempt to detect or report this problem, just as
6994 it makes no attempt to do anything about backups. However, since it is
6995 more reliable to shred devices than files, @command{shred} by default does
6996 not truncate or remove the output file. This default is more suitable
6997 for devices, which typically cannot be truncated and should not be
7000 Finally, consider the risk of backups and mirrors.
7001 File system backups and remote mirrors may contain copies of the
7002 file that cannot be removed, and that will allow a shredded file
7003 to be recovered later. So if you keep any data you may later want
7004 to destroy using @command{shred}, be sure that it is not backed up or mirrored.
7007 shred [@var{option}]@dots{} @var{file}[@dots{}]
7010 The program accepts the following options. Also see @ref{Common options}.
7018 @cindex force deletion
7019 Override file permissions if necessary to allow overwriting.
7022 @itemx -n @var{NUMBER}
7023 @itemx --iterations=@var{NUMBER}
7024 @opindex -n @var{NUMBER}
7025 @opindex --iterations=@var{NUMBER}
7026 @cindex iterations, selecting the number of
7027 By default, @command{shred} uses 25 passes of overwrite. This is enough
7028 for all of the useful overwrite patterns to be used at least once.
7029 You can reduce this to save time, or increase it if you have a lot of
7032 @item -s @var{BYTES}
7033 @itemx --size=@var{BYTES}
7034 @opindex -s @var{BYTES}
7035 @opindex --size=@var{BYTES}
7036 @cindex size of file to shred
7037 Shred the first @var{BYTES} bytes of the file. The default is to shred
7038 the whole file. @var{BYTES} can be followed by a size specification like
7039 @samp{K}, @samp{M}, or @samp{G} to specify a multiple. @xref{Block size}.
7045 @cindex removing files after shredding
7046 After shredding a file, truncate it (if possible) and then remove it.
7047 If a file has multiple links, only the named links will be removed.
7053 Display status updates as sterilization proceeds.
7059 By default, @command{shred} rounds the size of a regular file up to the next
7060 multiple of the filesystem block size to fully erase the last block of the file.
7061 Use @option{--exact} to suppress that behavior.
7062 Thus, by default if you shred a 10-byte regular file on a system with 512-byte
7063 blocks, the resulting file will be 512 bytes long. With this option,
7064 shred does not increase the apparent size of the file.
7070 Normally, the last pass that @command{shred} writes is made up of
7071 random data. If this would be conspicuous on your hard drive (for
7072 example, because it looks like encrypted data), or you just think
7073 it's tidier, the @option{--zero} option adds an additional overwrite pass with
7074 all zero bits. This is in addition to the number of passes specified
7075 by the @option{--iterations} option.
7079 Shred standard output.
7081 This argument is considered an option. If the common @samp{--} option has
7082 been used to indicate the end of options on the command line, then @samp{-}
7083 will be interpreted as an ordinary file name.
7085 The intended use of this is to shred a removed temporary file.
7089 i=`tempfile -m 0600`
7092 echo "Hello, world" >&3
7097 Note that the shell command @samp{shred - >file} does not shred the
7098 contents of @var{file}, since it truncates @var{file} before invoking
7099 @command{shred}. Use the command @samp{shred file} or (if using a
7100 Bourne-compatible shell) the command @samp{shred - 1<>file} instead.
7104 You might use the following command to erase all trace of the
7105 filesystem you'd created on the floppy disk in your first drive.
7106 That command takes about 20 minutes to erase a ``1.44MB'' (actually
7110 shred --verbose /dev/fd0
7113 Similarly, to erase all data on a selected partition of
7114 your hard disk, you could give a command like this:
7117 shred --verbose /dev/sda5
7123 @node Special file types
7124 @chapter Special file types
7126 @cindex special file types
7127 @cindex file types, special
7129 This chapter describes commands which create special types of files (and
7130 @command{rmdir}, which removes directories, one special file type).
7132 @cindex special file types
7134 Although Unix-like operating systems have markedly fewer special file
7135 types than others, not @emph{everything} can be treated only as the
7136 undifferentiated byte stream of @dfn{normal files}. For example, when a
7137 file is created or removed, the system must record this information,
7138 which it does in a @dfn{directory}---a special type of file. Although
7139 you can read directories as normal files, if you're curious, in order
7140 for the system to do its job it must impose a structure, a certain
7141 order, on the bytes of the file. Thus it is a ``special'' type of file.
7143 Besides directories, other special file types include named pipes
7144 (FIFOs), symbolic links, sockets, and so-called @dfn{special files}.
7147 * link invocation:: Make a hard link via the link syscall
7148 * ln invocation:: Make links between files.
7149 * mkdir invocation:: Make directories.
7150 * mkfifo invocation:: Make FIFOs (named pipes).
7151 * mknod invocation:: Make block or character special files.
7152 * readlink invocation:: Print the referent of a symbolic link.
7153 * rmdir invocation:: Remove empty directories.
7154 * unlink invocation:: Remove files via the unlink syscall
7158 @node link invocation
7159 @section @command{link}: Make a hard link via the link syscall
7162 @cindex links, creating
7163 @cindex hard links, creating
7164 @cindex creating links (hard only)
7166 @command{link} creates a single hard link at a time.
7167 It is a minimalist interface to the system-provided
7168 @code{link} function. @xref{Hard Links, , , libc,
7169 The GNU C Library Reference Manual}.
7173 link @var{filename} @var{linkname}
7176 @var{filename} must specify an existing file, and @var{linkname}
7177 must specify a nonexistent entry in an existing directory.
7178 @command{link} simply calls @code{link (@var{filename}, @var{linkname})}
7185 @section @command{ln}: Make links between files
7188 @cindex links, creating
7189 @cindex hard links, creating
7190 @cindex symbolic (soft) links, creating
7191 @cindex creating links (hard or soft)
7193 @cindex filesystems and hard links
7194 @command{ln} makes links between files. By default, it makes hard links;
7195 with the @option{-s} option, it makes symbolic (or @dfn{soft}) links.
7199 ln [@var{option}]@dots{} @var{target} [@var{linkname}]
7200 ln [@var{option}]@dots{} @var{target}@dots{} @var{directory}
7205 @item If the last argument names an existing directory, @command{ln} creates a
7206 link to each @var{target} file in that directory, using the
7207 @var{target}s' names. (But see the description of the
7208 @option{--no-dereference} option below.)
7210 @item If two filenames are given, @command{ln} creates a link from the
7211 second to the first.
7213 @item If one @var{target} is given, @command{ln} creates a link to that
7214 file in the current directory.
7216 @item It is an error if the last argument is not a directory and more
7217 than two files are given. Without @option{-f} or @option{-i} (see below),
7218 @command{ln} will not remove an existing file. Use the @option{--backup}
7219 option to make @command{ln} rename existing files.
7223 @cindex hard link, defined
7224 @cindex inode, and hard links
7225 A @dfn{hard link} is another name for an existing file; the link and the
7226 original are indistinguishable. Technically speaking, they share the
7227 same inode, and the inode contains all the information about a
7228 file---indeed, it is not incorrect to say that the inode @emph{is} the
7229 file. On all existing implementations, you cannot make a hard link to
7230 a directory, and hard links cannot cross filesystem boundaries. (These
7231 restrictions are not mandated by @acronym{POSIX}, however.)
7233 @cindex dereferencing symbolic links
7234 @cindex symbolic link, defined
7235 @dfn{Symbolic links} (@dfn{symlinks} for short), on the other hand, are
7236 a special file type (which not all kernels support: System V release 3
7237 (and older) systems lack symlinks) in which the link file actually
7238 refers to a different file, by name. When most operations (opening,
7239 reading, writing, and so on) are passed the symbolic link file, the
7240 kernel automatically @dfn{dereferences} the link and operates on the
7241 target of the link. But some operations (e.g., removing) work on the
7242 link file itself, rather than on its target. @xref{Symbolic Links,,,
7243 libc, The GNU C Library Reference Manual}.
7245 The program accepts the following options. Also see @ref{Common options}.
7256 @opindex --directory
7257 @cindex hard links to directories
7258 Allow the super-user to attempt to make hard links to directories.
7259 However, note that this will probably fail due to
7260 system restrictions, even for the super-user.
7266 Remove existing destination files.
7269 @itemx --interactive
7271 @opindex --interactive
7272 @cindex prompting, and @command{ln}
7273 Prompt whether to remove existing destination files.
7276 @itemx --no-dereference
7278 @opindex --no-dereference
7279 When given an explicit destination that is a symlink to a directory,
7280 treat that destination as if it were a normal file.
7282 When the destination is an actual directory (not a symlink to one),
7283 there is no ambiguity. The link is created in that directory.
7284 But when the specified destination is a symlink to a directory,
7285 there are two ways to treat the user's request. @command{ln} can
7286 treat the destination just as it would a normal directory and create
7287 the link in it. On the other hand, the destination can be viewed as a
7288 non-directory---as the symlink itself. In that case, @command{ln}
7289 must delete or backup that symlink before creating the new link.
7290 The default is to treat a destination that is a symlink to a directory
7291 just like a directory.
7297 Make symbolic links instead of hard links. This option merely produces
7298 an error message on systems that do not support symbolic links.
7308 Print the name of each file before linking it.
7317 ln -s /some/name # creates link ./name pointing to /some/name
7318 ln -s /some/name myname # creates link ./myname pointing to /some/name
7319 ln -s a b .. # creates links ../a and ../b pointing to ./a and ./b
7325 @node mkdir invocation
7326 @section @command{mkdir}: Make directories
7329 @cindex directories, creating
7330 @cindex creating directories
7332 @command{mkdir} creates directories with the specified names. Synopsis:
7335 mkdir [@var{option}]@dots{} @var{name}@dots{}
7338 If a @var{name} is an existing file but not a directory, @command{mkdir} prints
7339 a warning message on stderr and will exit with a status of 1 after
7340 processing any remaining @var{name}s. The same is done when a @var{name} is an
7341 existing directory and the -p option is not given. If a @var{name} is an
7342 existing directory and the -p option is given, @command{mkdir} will ignore it.
7343 That is, @command{mkdir} will not print a warning, raise an error, or change
7344 the mode of the directory (even if the -m option is given), and will
7345 move on to processing any remaining @var{name}s.
7347 The program accepts the following options. Also see @ref{Common options}.
7352 @itemx --mode=@var{mode}
7355 @cindex modes of created directories, setting
7356 Set the mode of created directories to @var{mode}, which is symbolic as
7357 in @command{chmod} and uses @samp{a=rwx} (read, write and execute allowed for
7358 everyone) minus the bits set in the umask for the point of the
7359 departure. @xref{File permissions}.
7365 @cindex parent directories, creating
7366 Make any missing parent directories for each argument. The mode for parent
7367 directories is set to the umask modified by @samp{u+wx}.
7368 Ignore arguments corresponding to existing directories.
7374 Print a message for each created directory. This is most useful with
7381 @node mkfifo invocation
7382 @section @command{mkfifo}: Make FIFOs (named pipes)
7385 @cindex FIFOs, creating
7386 @cindex named pipes, creating
7387 @cindex creating FIFOs (named pipes)
7389 @command{mkfifo} creates FIFOs (also called @dfn{named pipes}) with the
7390 specified names. Synopsis:
7393 mkfifo [@var{option}] @var{name}@dots{}
7396 A @dfn{FIFO} is a special file type that permits independent processes
7397 to communicate. One process opens the FIFO file for writing, and
7398 another for reading, after which data can flow as with the usual
7399 anonymous pipe in shells or elsewhere.
7401 The program accepts the following option. Also see @ref{Common options}.
7406 @itemx --mode=@var{mode}
7409 @cindex modes of created FIFOs, setting
7410 Set the mode of created FIFOs to @var{mode}, which is symbolic as in
7411 @command{chmod} and uses @samp{a=rw} (read and write allowed for everyone) minus
7412 the bits set in the umask for the point of departure. @xref{File permissions}.
7419 @node mknod invocation
7420 @section @command{mknod}: Make block or character special files
7423 @cindex block special files, creating
7424 @cindex character special files, creating
7426 @command{mknod} creates a FIFO, character special file, or block special
7427 file with the specified name. Synopsis:
7430 mknod [@var{option}]@dots{} @var{name} @var{type} [@var{major} @var{minor}]
7433 @cindex special files
7434 @cindex block special files
7435 @cindex character special files
7436 Unlike the phrase ``special file type'' above, the term @dfn{special
7437 file} has a technical meaning on Unix: something that can generate or
7438 receive data. Usually this corresponds to a physical piece of hardware,
7439 e.g., a printer or a disk. (These files are typically created at
7440 system-configuration time.) The @command{mknod} command is what creates
7441 files of this type. Such devices can be read either a character at a
7442 time or a ``block'' (many characters) at a time, hence we say there are
7443 @dfn{block special} files and @dfn{character special} files.
7445 The arguments after @var{name} specify the type of file to make:
7450 @opindex p @r{for FIFO file}
7454 @opindex b @r{for block special file}
7455 for a block special file
7458 @c Don't document the `u' option -- it's just a synonym for `c'.
7459 @c Do *any* versions of mknod still use it?
7461 @opindex c @r{for character special file}
7462 @c @opindex u @r{for character special file}
7463 for a character special file
7467 When making a block or character special file, the major and minor
7468 device numbers must be given after the file type.
7469 If a major or minor device number begins with @samp{0x} or @samp{0X},
7470 it is interpreted as hexadecimal; otherwise, if it begins with @samp{0},
7471 as octal; otherwise, as decimal.
7473 The program accepts the following option. Also see @ref{Common options}.
7478 @itemx --mode=@var{mode}
7481 Set the mode of created files to @var{mode}, which is symbolic as in
7482 @command{chmod} and uses @samp{a=rw} minus the bits set in the umask as the point
7483 of departure. @xref{File permissions}.
7490 @node readlink invocation
7491 @section @command{readlink}: Print the referent of a symbolic link
7494 @cindex displaying value of a symbolic link
7496 @command{readlink} may work in one of two supported modes:
7502 @command{readlink} outputs the value of the given symbolic link.
7503 If @command{readlink} is invoked with an argument other than the pathname
7504 of a symbolic link, it produces no output and exits with a nonzero exit code.
7506 @item Canonicalize mode
7508 @command{readlink} outputs the absolute name of the given file which contains
7509 no `.', `..' components nor any repeated path separators (`/') or symlinks.
7510 If any path component is missing or unavailable,
7511 it produces no output and exits with a nonzero exit code.
7516 readlink [@var{option}] @var{file}
7519 By default, @command{readlink} operates in readlink mode.
7521 The program accepts the following options. Also see @ref{Common options}.
7526 @itemx --canonicalize
7528 @opindex --canonicalize
7529 Activate canonicalize mode.
7534 @opindex --no-newline
7535 Do not output the trailing newline.
7545 Suppress most error messages.
7551 Report error messages.
7555 The @command{readlink} utility first appeared in OpenBSD 2.1.
7560 @node rmdir invocation
7561 @section @command{rmdir}: Remove empty directories
7564 @cindex removing empty directories
7565 @cindex directories, removing empty
7567 @command{rmdir} removes empty directories. Synopsis:
7570 rmdir [@var{option}]@dots{} @var{directory}@dots{}
7573 If any @var{directory} argument does not refer to an existing empty
7574 directory, it is an error.
7576 The program accepts the following option. Also see @ref{Common options}.
7580 @item --ignore-fail-on-non-empty
7581 @opindex --ignore-fail-on-non-empty
7582 @cindex directory deletion, ignoring failures
7583 Ignore each failure to remove a directory that is solely because
7584 the directory is non-empty.
7590 @cindex parent directories, removing
7591 Remove @var{directory}, then try to remove each component of @var{directory}.
7592 So, for example, @samp{rmdir -p a/b/c} is similar to @samp{rmdir a/b/c a/b a}.
7593 As such, it fails if any of those directories turns out not to be empty.
7594 Use the @option{--ignore-fail-on-non-empty} option to make it so such
7595 a failure does not evoke a diagnostic and does not cause @command{rmdir} to
7596 exit unsuccessfully.
7602 @cindex directory deletion, reporting
7603 Give a diagnostic for each successful removal.
7604 @var{directory} is removed.
7608 @xref{rm invocation}, for how to remove non-empty directories (recursively).
7613 @node unlink invocation
7614 @section @command{unlink}: Remove files via the unlink syscall
7617 @cindex removing files or directories (via the unlink syscall)
7619 @command{unlink} deletes a single specified file name.
7620 It is a minimalist interface to the system-provided
7621 @code{unlink} function. @xref{Deleting Files, , , libc,
7622 The GNU C Library Reference Manual}. Synopsis:
7625 unlink @var{filename}
7628 On some systems @code{unlink} can be used to delete the name of a
7629 directory. On others, it can be used that way only by a privileged user.
7630 In the GNU system @code{unlink} can never delete the name of a directory.
7632 By default, @command{unlink} honors the @option{--help} and @option{--version}
7633 options. That makes it a little harder to remove files named
7634 @option{--help} and @option{--version}, so when the environment variable
7635 @env{POSIXLY_CORRECT} is set, @command{unlink} treats such a command line
7636 arguments not as an option, but as an operand.
7641 @node Changing file attributes
7642 @chapter Changing file attributes
7644 @cindex changing file attributes
7645 @cindex file attributes, changing
7646 @cindex attributes, file
7648 A file is not merely its contents, a name, and a file type
7649 (@pxref{Special file types}). A file also has an owner (a userid), a
7650 group (a group id), permissions (what the owner can do with the file,
7651 what people in the group can do, and what everyone else can do), various
7652 timestamps, and other information. Collectively, we call these a file's
7655 These commands change file attributes.
7658 * chgrp invocation:: Change file groups.
7659 * chmod invocation:: Change access permissions.
7660 * chown invocation:: Change file owners and groups.
7661 * touch invocation:: Change file timestamps.
7665 @node chown invocation
7666 @section @command{chown}: Change file owner and group
7669 @cindex file ownership, changing
7670 @cindex group ownership, changing
7671 @cindex changing file ownership
7672 @cindex changing group ownership
7674 @command{chown} changes the user and/or group ownership of each given @var{file}
7675 to @var{new-owner} or to the user and group of an existing reference file.
7679 chown [@var{option}]@dots{} @{@var{new-owner} | --reference=@var{ref_file}@} @var{file}@dots{}
7682 If used, @var{new-owner} specifies the new owner and/or group as follows
7683 (with no embedded white space):
7686 [@var{owner}] [ [:] [@var{group}] ]
7693 If only an @var{owner} (a user name or numeric user id) is given, that
7694 user is made the owner of each given file, and the files' group is not
7697 @itemx owner@samp{:}group
7698 If the @var{owner} is followed by a colon and a @var{group} (a
7699 group name or numeric group id), with no spaces between them, the group
7700 ownership of the files is changed as well (to @var{group}).
7702 @itemx owner@samp{:}
7703 If a colon but no group name follows @var{owner}, that user is
7704 made the owner of the files and the group of the files is changed to
7705 @var{owner}'s login group.
7707 @itemx @samp{:}group
7708 If the colon and following @var{group} are given, but the owner
7709 is omitted, only the group of the files is changed; in this case,
7710 @command{chown} performs the same function as @command{chgrp}.
7714 Some older scripts may still use @samp{.} in place of the @samp{:} separator.
7715 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not allow that,
7716 but you may be able to work around the compatibility problems by setting
7717 @samp{_POSIX2_VERSION=199209} in your environment.
7718 New scripts should avoid the use of @samp{.} because @sc{gnu} @command{chown}
7719 may fail if @var{owner} contains @samp{.} characters.
7721 Warning: The @command{chown} command may clear the set-user-ID or
7722 set-group-ID bits on some systems. The @command{chown} command is
7723 dependent upon the policy and functionality of the underlying system
7724 which may make system-dependent file mode modifications outside the
7725 control of the @command{chown} command. On some systems (e.g., Linux)
7726 the @command{chown} command clears the set-UID and set-GID bits because
7727 the underlying, system @code{chown} function clears them. On other
7728 systems (e.g., HP-UX and Solaris) the @command{chown} command does not
7729 affect those bits when operated as the superuser. On systems which allow
7730 non-privileged use of chown those bits are always cleared by the system.
7731 When in doubt, check the underlying system behavior.
7733 The program accepts the following options. Also see @ref{Common options}.
7741 @cindex changed owners, verbosely describing
7742 Verbosely describe the action for each @var{file} whose ownership
7751 @cindex error messages, omitting
7752 Do not print error messages about files whose ownership cannot be
7755 @itemx @w{@kbd{--from}=@var{old-owner}}
7757 @cindex symbolic links, changing owner
7758 Change a @var{file}'s ownership only if it has current attributes specified
7759 by @var{old-owner}. @var{old-owner} has the same form as @var{new-owner}
7761 This option is useful primarily from a security standpoint in that
7762 it narrows considerably the window of potential abuse.
7763 For example, to reflect a UID numbering change for one user's files
7764 without an option like this, @code{root} might run
7767 find / -owner OLDUSER -print0 | xargs -0 chown NEWUSER
7770 But that is dangerous because the interval between when the @command{find}
7771 tests the existing file's owner and when the @command{chown} is actually run
7773 One way to narrow the gap would be to invoke chown for each file
7777 find / -owner OLDUSER -exec chown NEWUSER @{@} \;
7780 But that is very slow if there are many affected files.
7781 With this option, it is safer (the gap is narrower still)
7782 though still not perfect:
7785 chown -R --from=OLDUSER NEWUSER /
7789 @opindex --dereference
7790 @cindex symbolic links, changing owner
7792 Do not act on symbolic links themselves but rather on what they point to.
7795 @itemx --no-dereference
7797 @opindex --no-dereference
7798 @cindex symbolic links, changing owner
7800 Act on symbolic links themselves instead of what they point to.
7801 This is the default.
7802 This mode relies on the @code{lchown} system call.
7803 On systems that do not provide the @code{lchown} system call,
7804 @command{chown} fails when a file specified on the command line
7806 By default, no diagnostic is issued for symbolic links encountered
7807 during a recursive traversal, but see @option{--verbose}.
7809 @itemx --preserve-root
7810 @opindex --preserve-root
7811 @cindex root directory, disallow recursive modification
7812 Fail upon any attempt to recursively change the filesystem root, @file{/}.
7813 Without @option{--recursive}, this option has no effect.
7814 @xref{Treating / specially}.
7816 @itemx --no-preserve-root
7817 @opindex --no-preserve-root
7818 @cindex root directory, allow recursive modification
7819 Cancel the effect of any preceding @option{--preserve-root} option.
7820 @xref{Treating / specially}.
7822 @item --reference=@var{ref_file}
7823 @opindex --reference
7824 Change the user and group of each @var{file} to be the same as those of
7825 @var{ref_file}. If @var{ref_file} is a symbolic link, do not use the
7826 user and group of the symbolic link, but rather those of the file it
7833 Output a diagnostic for every file processed.
7834 If a symbolic link is encountered during a recursive traversal
7835 on a system without the @code{lchown} system call, and @option{--no-dereference}
7836 is in effect, then issue a diagnostic saying neither the symbolic link nor
7837 its referent is being changed.
7842 @opindex --recursive
7843 @cindex recursively changing file ownership
7844 Recursively change ownership of directories and their contents.
7847 @xref{Traversing symlinks}.
7850 @xref{Traversing symlinks}.
7853 @xref{Traversing symlinks}.
7860 @node chgrp invocation
7861 @section @command{chgrp}: Change group ownership
7864 @cindex group ownership, changing
7865 @cindex changing group ownership
7867 @command{chgrp} changes the group ownership of each given @var{file}
7868 to @var{group} (which can be either a group name or a numeric group id)
7869 or to the group of an existing reference file. Synopsis:
7872 chgrp [@var{option}]@dots{} @{@var{group} | --reference=@var{ref_file}@} @var{file}@dots{}
7875 The program accepts the following options. Also see @ref{Common options}.
7883 @cindex changed files, verbosely describing
7884 Verbosely describe the action for each @var{file} whose group actually
7893 @cindex error messages, omitting
7894 Do not print error messages about files whose group cannot be
7898 @opindex --dereference
7899 @cindex symbolic links, changing owner
7901 Do not act on symbolic links themselves but rather on what they point to.
7904 @itemx --no-dereference
7906 @opindex --no-dereference
7907 @cindex symbolic links, changing group
7909 Act on symbolic links themselves instead of what they point to.
7910 This is the default.
7911 This mode relies on the @code{lchown} system call.
7912 On systems that do not provide the @code{lchown} system call,
7913 @command{chgrp} fails when a file specified on the command line
7915 By default, no diagnostic is issued for symbolic links encountered
7916 during a recursive traversal, but see @option{--verbose}.
7918 @itemx --preserve-root
7919 @opindex --preserve-root
7920 @cindex root directory, disallow recursive modification
7921 Fail upon any attempt to recursively change the filesystem root, @file{/}.
7922 Without @option{--recursive}, this option has no effect.
7923 @xref{Treating / specially}.
7925 @itemx --no-preserve-root
7926 @opindex --no-preserve-root
7927 @cindex root directory, allow recursive modification
7928 Cancel the effect of any preceding @option{--preserve-root} option.
7929 @xref{Treating / specially}.
7931 @item --reference=@var{ref_file}
7932 @opindex --reference
7933 Change the group of each @var{file} to be the same as that of
7934 @var{ref_file}. If @var{ref_file} is a symbolic link, do not use the
7935 group of the symbolic link, but rather that of the file it refers to.
7941 Output a diagnostic for every file processed.
7942 If a symbolic link is encountered during a recursive traversal
7943 on a system without the @code{lchown} system call, and @option{--no-dereference}
7944 is in effect, then issue a diagnostic saying neither the symbolic link nor
7945 its referent is being changed.
7950 @opindex --recursive
7951 @cindex recursively changing group ownership
7952 Recursively change the group ownership of directories and their contents.
7955 @xref{Traversing symlinks}.
7958 @xref{Traversing symlinks}.
7961 @xref{Traversing symlinks}.
7968 @node chmod invocation
7969 @section @command{chmod}: Change access permissions
7972 @cindex changing access permissions
7973 @cindex access permissions, changing
7974 @cindex permissions, changing access
7976 @command{chmod} changes the access permissions of the named files. Synopsis:
7979 chmod [@var{option}]@dots{} @{@var{mode} | --reference=@var{ref_file}@} @var{file}@dots{}
7982 @cindex symbolic links, permissions of
7983 @command{chmod} never changes the permissions of symbolic links, since
7984 the @command{chmod} system call cannot change their permissions.
7985 This is not a problem since the permissions of symbolic links are
7986 never used. However, for each symbolic link listed on the command
7987 line, @command{chmod} changes the permissions of the pointed-to file.
7988 In contrast, @command{chmod} ignores symbolic links encountered during
7989 recursive directory traversals.
7991 If used, @var{mode} specifies the new permissions.
7992 For details, see the section on @ref{File permissions}.
7994 The program accepts the following options. Also see @ref{Common options}.
8002 Verbosely describe the action for each @var{file} whose permissions
8011 @cindex error messages, omitting
8012 Do not print error messages about files whose permissions cannot be
8015 @itemx --preserve-root
8016 @opindex --preserve-root
8017 @cindex root directory, disallow recursive modification
8018 Fail upon any attempt to recursively change the filesystem root, @file{/}.
8019 Without @option{--recursive}, this option has no effect.
8020 @xref{Treating / specially}.
8022 @itemx --no-preserve-root
8023 @opindex --no-preserve-root
8024 @cindex root directory, allow recursive modification
8025 Cancel the effect of any preceding @option{--preserve-root} option.
8026 @xref{Treating / specially}.
8032 Verbosely describe the action or non-action taken for every @var{file}.
8034 @item --reference=@var{ref_file}
8035 @opindex --reference
8036 Change the mode of each @var{file} to be the same as that of @var{ref_file}.
8037 @xref{File permissions}.
8038 If @var{ref_file} is a symbolic link, do not use the mode
8039 of the symbolic link, but rather that of the file it refers to.
8044 @opindex --recursive
8045 @cindex recursively changing access permissions
8046 Recursively change permissions of directories and their contents.
8053 @node touch invocation
8054 @section @command{touch}: Change file timestamps
8057 @cindex changing file timestamps
8058 @cindex file timestamps, changing
8059 @cindex timestamps, changing file
8061 @command{touch} changes the access and/or modification times of the
8062 specified files. Synopsis:
8065 touch [@var{option}]@dots{} @var{file}@dots{}
8068 On older systems, @command{touch} supports an obsolete syntax, as follows.
8069 If the first @var{file} would be a valid argument to the @option{-t}
8070 option and no timestamp is given with any of the @option{-d}, @option{-r},
8071 or @option{-t} options and the @samp{--} argument is not given, that
8072 argument is interpreted as the time for the other files instead of
8073 as a file name. @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance})
8074 does not allow this; use @option{-t} instead.
8076 @cindex empty files, creating
8077 Any @var{file} that does not exist is created empty.
8079 @cindex permissions, for changing file timestamps
8080 If changing both the access and modification times to the current
8081 time, @command{touch} can change the timestamps for files that the user
8082 running it does not own but has write permission for. Otherwise, the
8083 user must own the files.
8085 Although @command{touch} provides options for changing two of the times --
8086 the times of last access and modification -- of a file, there is actually
8087 a third one as well: the inode change time. This is often referred to
8088 as a file's @code{ctime}.
8089 The inode change time represents the time when the file's meta-information
8090 last changed. One common example of this is when the permissions of a
8091 file change. Changing the permissions doesn't access the file, so
8092 the atime doesn't change, nor does it modify the file, so the mtime
8093 doesn't change. Yet, something about the file itself has changed,
8094 and this must be noted somewhere. This is the job of the ctime field.
8095 This is necessary, so that, for example, a backup program can make a
8096 fresh copy of the file, including the new permissions value.
8097 Another operation that modifies a file's ctime without affecting
8098 the others is renaming. In any case, it is not possible, in normal
8099 operations, for a user to change the ctime field to a user-specified value.
8101 The program accepts the following options. Also see @ref{Common options}.
8107 @itemx --time=access
8111 @opindex atime@r{, changing}
8112 @opindex access @r{time, changing}
8113 @opindex use @r{time, changing}
8114 Change the access time only.
8119 @opindex --no-create
8120 Do not create files that do not exist.
8127 Use @var{time} instead of the current time. It can contain month names,
8128 time zones, @samp{am} and @samp{pm}, etc. @xref{Date input formats}.
8132 @cindex BSD @command{touch} compatibility
8133 Ignored; for compatibility with BSD versions of @command{touch}.
8137 @itemx --time=modify
8140 @opindex mtime@r{, changing}
8141 @opindex modify @r{time, changing}
8142 Change the modification time only.
8145 @itemx --reference=@var{file}
8147 @opindex --reference
8148 Use the times of the reference @var{file} instead of the current time.
8149 If this option is combined with the @option{--date=@var{time}}
8150 (@option{-d @var{time}}) option, the reference @var{file}'s time is
8151 the origin for any relative @var{time}s given, but is otherwise ignored.
8152 For example, @samp{-r foo -d '-5 seconds'} specifies a time stamp
8153 equal to five seconds before the corresponding time stamp for @file{foo}.
8155 @item -t [[CC]YY]MMDDhhmm[.ss]
8156 Use the argument (optional four-digit or two-digit years, months,
8157 days, hours, minutes, optional seconds) instead of the current time.
8158 If the year is specified with only two digits, then @var{CC}
8159 is 20 for years in the range 0 @dots{} 68, and 19 for years in
8160 69 @dots{} 99. If no digits of the year are specified,
8161 the argument is interpreted as a date in the current year.
8173 No disk can hold an infinite amount of data. These commands report on
8174 how much disk storage is in use or available. (This has nothing much to
8175 do with how much @emph{main memory}, i.e., RAM, a program is using when
8176 it runs; for that, you want @command{ps} or @command{pstat} or @command{swap}
8177 or some such command.)
8180 * df invocation:: Report filesystem disk space usage.
8181 * du invocation:: Estimate file space usage.
8182 * stat invocation:: Report file or filesystem status.
8183 * sync invocation:: Synchronize memory and disk.
8188 @section @command{df}: Report filesystem disk space usage
8191 @cindex filesystem disk usage
8192 @cindex disk usage by filesystem
8194 @command{df} reports the amount of disk space used and available on
8195 filesystems. Synopsis:
8198 df [@var{option}]@dots{} [@var{file}]@dots{}
8201 With no arguments, @command{df} reports the space used and available on all
8202 currently mounted filesystems (of all types). Otherwise, @command{df}
8203 reports on the filesystem containing each argument @var{file}.
8205 Normally the disk space is printed in units of
8206 1024 bytes, but this can be overridden (@pxref{Block size}).
8207 Non-integer quantities are rounded up to the next higher unit.
8209 @cindex disk device file
8210 @cindex device file, disk
8211 If an argument @var{file} is a disk device file containing a mounted
8212 filesystem, @command{df} shows the space available on that filesystem
8213 rather than on the filesystem containing the device node (i.e., the root
8214 filesystem). @sc{gnu} @command{df} does not attempt to determine the disk usage
8215 on unmounted filesystems, because on most kinds of systems doing so
8216 requires extremely nonportable intimate knowledge of filesystem
8219 The program accepts the following options. Also see @ref{Common options}.
8227 @cindex automounter filesystems
8228 @cindex ignore filesystems
8229 Include in the listing filesystems that have a size of 0 blocks, which
8230 are omitted by default. Such filesystems are typically special-purpose
8231 pseudo-filesystems, such as automounter entries. Also, filesystems of
8232 type ``ignore'' or ``auto'', supported by some operating systems, are
8233 only included if this option is specified.
8236 @itemx --block-size=@var{size}
8238 @opindex --block-size
8239 @cindex filesystem sizes
8240 Scale sizes by @var{size} before printing them (@pxref{Block size}).
8241 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
8247 Equivalent to @option{--si}.
8254 List inode usage information instead of block usage. An inode (short
8255 for index node) contains information about a file such as its owner,
8256 permissions, timestamps, and location on the disk.
8260 @cindex kibibytes for filesystem sizes
8261 Print sizes in 1024-byte blocks, overriding the default block size
8262 (@pxref{Block size}).
8263 This option is equivalent to @option{--block-size=1K}.
8269 @cindex filesystem types, limiting output to certain
8270 Limit the listing to local filesystems. By default, remote filesystems
8275 @cindex filesystem space, retrieving old data more quickly
8276 Do not invoke the @code{sync} system call before getting any usage data.
8277 This may make @command{df} run significantly faster on systems with many
8278 disks, but on some systems (notably SunOS) the results may be slightly
8279 out of date. This is the default.
8282 @itemx --portability
8284 @opindex --portability
8285 @cindex one-line output format
8286 @cindex @acronym{POSIX} output format
8287 @cindex portable output format
8288 @cindex output format, portable
8289 Use the @acronym{POSIX} output format. This is like the default format except
8294 The information about each filesystem is always printed on exactly
8295 one line; a mount device is never put on a line by itself. This means
8296 that if the mount device name is more than 20 characters long (e.g., for
8297 some network mounts), the columns are misaligned.
8300 The labels in the header output line are changed to conform to @acronym{POSIX}.
8307 @cindex filesystem space, retrieving current data more slowly
8308 Invoke the @code{sync} system call before getting any usage data. On
8309 some systems (notably SunOS), doing this yields more up to date results,
8310 but in general this option makes @command{df} much slower, especially when
8311 there are many or very busy filesystems.
8313 @item -t @var{fstype}
8314 @itemx --type=@var{fstype}
8317 @cindex filesystem types, limiting output to certain
8318 Limit the listing to filesystems of type @var{fstype}. Multiple
8319 filesystem types can be specified by giving multiple @option{-t} options.
8320 By default, nothing is omitted.
8325 @opindex --print-type
8326 @cindex filesystem types, printing
8327 Print each filesystem's type. The types printed here are the same ones
8328 you can include or exclude with @option{-t} and @option{-x}. The particular
8329 types printed are whatever is supported by the system. Here are some of
8330 the common names (this list is certainly not exhaustive):
8335 @cindex NFS filesystem type
8336 An NFS filesystem, i.e., one mounted over a network from another
8337 machine. This is the one type name which seems to be used uniformly by
8340 @item 4.2@r{, }ufs@r{, }efs@dots{}
8341 @cindex Linux filesystem types
8342 @cindex local filesystem types
8343 @opindex 4.2 @r{filesystem type}
8344 @opindex ufs @r{filesystem type}
8345 @opindex efs @r{filesystem type}
8346 A filesystem on a locally-mounted hard disk. (The system might even
8347 support more than one type here; Linux does.)
8349 @item hsfs@r{, }cdfs
8350 @cindex CD-ROM filesystem type
8351 @cindex High Sierra filesystem
8352 @opindex hsfs @r{filesystem type}
8353 @opindex cdfs @r{filesystem type}
8354 A filesystem on a CD-ROM drive. HP-UX uses @samp{cdfs}, most other
8355 systems use @samp{hsfs} (@samp{hs} for ``High Sierra'').
8358 @cindex PC filesystem
8359 @cindex DOS filesystem
8360 @cindex MS-DOS filesystem
8361 @cindex diskette filesystem
8363 An MS-DOS filesystem, usually on a diskette.
8367 @item -x @var{fstype}
8368 @itemx --exclude-type=@var{fstype}
8370 @opindex --exclude-type
8371 Limit the listing to filesystems not of type @var{fstype}.
8372 Multiple filesystem types can be eliminated by giving multiple
8373 @option{-x} options. By default, no filesystem types are omitted.
8376 Ignored; for compatibility with System V versions of @command{df}.
8384 @section @command{du}: Estimate file space usage
8387 @cindex file space usage
8388 @cindex disk usage for files
8390 @command{du} reports the amount of disk space used by the specified files
8391 and for each subdirectory (of directory arguments). Synopsis:
8394 du [@var{option}]@dots{} [@var{file}]@dots{}
8397 With no arguments, @command{du} reports the disk space for the current
8398 directory. Normally the disk space is printed in units of
8399 1024 bytes, but this can be overridden (@pxref{Block size}).
8400 Non-integer quantities are rounded up to the next higher unit.
8402 The program accepts the following options. Also see @ref{Common options}.
8410 Show counts for all files, not just directories.
8412 @itemx --apparent-size
8413 @opindex --apparent-size
8414 Print apparent sizes, rather than disk usage. The apparent size of a
8415 file is the number of bytes reported by @code{wc -c} on regular files,
8416 or more generally, @code{ls -l --block-size=1} or @code{stat --format=%s}.
8417 For example, a file containing the word @samp{zoo} with no newline would,
8418 of course, have an apparent size of 3. Such a small file may require
8419 anywhere from zero to 16 or more kilobytes of disk space, depending on
8420 the type and configuration of the file system on which the file resides.
8421 However, a sparse file created with this command
8423 : | dd bs=1 seek=`echo '2^31'|bc` of=big
8425 has an apparent size of 2 gigabytes, yet on most modern
8426 systems, it actually uses almost no disk space.
8432 Equivalent to @code{--apparent-size --block-size=1}.
8435 @itemx --block-size=@var{size}
8437 @opindex --block-size
8439 Scale sizes by @var{size} before printing them (@pxref{Block size}).
8440 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
8446 @cindex grand total of disk space
8447 Print a grand total of all arguments after all arguments have
8448 been processed. This can be used to find out the total disk usage of
8449 a given set of files or directories.
8452 @itemx --dereference-args
8454 @opindex --dereference-args
8455 Dereference symbolic links that are command line arguments.
8456 Does not affect other symbolic links. This is helpful for finding
8457 out the disk usage of directories, such as @file{/usr/tmp}, which
8458 are often symbolic links.
8464 Currently @option{-H} is the same as @option{--si},
8465 but @option{-H} evokes a warning.
8466 This option will be changed to be equivalent to
8467 @option{--dereference-args} (@option{-D}).
8471 @cindex kibibytes for file sizes
8472 Print sizes in 1024-byte blocks, overriding the default block size
8473 (@pxref{Block size}).
8474 This option is equivalent to @option{--block-size=1K}.
8477 @itemx --count-links
8479 @opindex --count-links
8480 @cindex hard links, counting in @command{du}
8481 Count the size of all files, even if they have appeared already (as a
8485 @itemx --dereference
8487 @opindex --dereference
8488 @cindex symbolic links, dereferencing in @command{du}
8489 Dereference symbolic links (show the disk space used by the file
8490 or directory that the link points to instead of the space used by
8494 @itemx --no-dereference
8496 @opindex --no-dereference
8497 @cindex symbolic links, dereferencing in @command{du}
8498 For each symbolic links encountered by @command{du},
8499 consider the disk space used by the symbolic link.
8501 @item --max-depth=@var{DEPTH}
8502 @opindex --max-depth=@var{DEPTH}
8503 @cindex limiting output of @command{du}
8504 Show the total for each directory (and file if --all) that is at
8505 most MAX_DEPTH levels down from the root of the hierarchy. The root
8506 is at level 0, so @code{du --max-depth=0} is equivalent to @code{du -s}.
8512 @cindex output NUL-terminated lines
8513 Output the zero byte (@kbd{NUL}) at the end of each line, rather than a newline.
8514 This option enables other programs to parse the output of @command{du}
8515 even when that output would contain file names with embedded newlines.
8520 Append an SI-style abbreviation to each size, such as @samp{MB} for
8521 megabytes. Powers of 1000 are used, not 1024; @samp{MB} stands for
8522 1,000,000 bytes. Use the @option{-h} or @option{--human-readable} option if
8523 you prefer powers of 1024.
8528 @opindex --summarize
8529 Display only a total for each argument.
8532 @itemx --separate-dirs
8534 @opindex --separate-dirs
8535 Report the size of each directory separately, not including the sizes
8539 @itemx --one-file-system
8541 @opindex --one-file-system
8542 @cindex one filesystem, restricting @command{du} to
8543 Skip directories that are on different filesystems from the one that
8544 the argument being processed is on.
8546 @item --exclude=@var{PATTERN}
8547 @opindex --exclude=@var{PATTERN}
8548 @cindex excluding files from @command{du}
8549 When recursing, skip subdirectories or files matching @var{PATTERN}.
8550 For example, @code{du --exclude='*.o'} excludes files whose names
8554 @itemx --exclude-from=@var{FILE}
8555 @opindex -X @var{FILE}
8556 @opindex --exclude-from=@var{FILE}
8557 @cindex excluding files from @command{du}
8558 Like @option{--exclude}, except take the patterns to exclude from @var{FILE},
8559 one per line. If @var{FILE} is @samp{-}, take the patterns from standard
8564 @cindex NFS mounts from BSD to HP-UX
8565 On BSD systems, @command{du} reports sizes that are half the correct
8566 values for files that are NFS-mounted from HP-UX systems. On HP-UX
8567 systems, it reports sizes that are twice the correct values for
8568 files that are NFS-mounted from BSD systems. This is due to a flaw
8569 in HP-UX; it also affects the HP-UX @command{du} program.
8574 @node stat invocation
8575 @section @command{stat}: Report file or filesystem status
8579 @cindex filesystem status
8581 @command{stat} displays information about the specified file(s). Synopsis:
8584 stat [@var{option}]@dots{} [@var{file}]@dots{}
8587 With no option, @command{stat} reports all information about the given files.
8588 But it also can be used to report the information of the filesystems the
8589 given files are located on. If the files are links, @command{stat} can
8590 also give information about the files the links point to.
8598 @opindex --filesystem
8600 Report information about the filesystems where the given files are located
8601 instead of information about the files themselves.
8604 @itemx --dereference
8606 @opindex --dereference
8607 @cindex symbolic links, dereferencing in @command{stat}
8608 Change how @command{stat} treats symbolic links.
8609 With this option, @command{stat} acts on the file referenced
8610 by each symbolic link argument.
8611 Without it, @command{stat} acts on any symbolic link argument directly.
8617 @cindex terse output
8618 Print the information in terse form, suitable for parsing by other programs.
8624 @cindex output format
8625 Allow user to specify the output format.
8627 Interpreted sequences for file stat are:
8629 @item %a - Access rights in octal
8630 @item %A - Access rights in human readable form
8631 @item %b - Number of blocks allocated (see @samp{%B})
8632 @item %B - The size in bytes of each block reported by @samp{%b}
8633 @item %d - Device number in decimal
8634 @item %D - Device number in hex
8635 @item %f - raw mode in hex
8636 @item %F - File type
8637 @item %g - Group Id of owner
8638 @item %G - Group name of owner
8639 @item %h - Number of hard links
8640 @item %i - Inode number
8641 @item %n - File name
8642 @item %N - Quoted File name with dereference if symbolic link
8643 @item %o - IO block size
8644 @item %s - Total size, in bytes
8645 @item %t - Major device type in hex
8646 @item %T - Minor device type in hex
8647 @item %u - User Id of owner
8648 @item %U - User name of owner
8649 @item %x - Time of last access
8650 @item %X - Time of last access as seconds since Epoch
8651 @item %y - Time of last modification
8652 @item %Y - Time of last modification as seconds since Epoch
8653 @item %z - Time of last change
8654 @item %Z - Time of last change as seconds since Epoch
8657 Interpreted sequences for filesystem stat are:
8659 @item %n - File name
8660 @item %i - File System id in hex
8661 @item %l - Maximum length of filenames
8662 @item %t - Type in hex
8663 @item %T - Type in human readable form
8664 @item %b - Total data blocks in file system
8665 @item %f - Free blocks in file system
8666 @item %a - Free blocks available to non-superuser
8667 @item %s - Optimal transfer block size
8668 @item %c - Total file nodes in file system
8675 @node sync invocation
8676 @section @command{sync}: Synchronize data on disk with memory
8679 @cindex synchronize disk and memory
8681 @cindex superblock, writing
8682 @cindex inodes, written buffered
8683 @command{sync} writes any data buffered in memory out to disk. This can
8684 include (but is not limited to) modified superblocks, modified inodes,
8685 and delayed reads and writes. This must be implemented by the kernel;
8686 The @command{sync} program does nothing but exercise the @code{sync} system
8689 @cindex crashes and corruption
8690 The kernel keeps data in memory to avoid doing (relatively slow) disk
8691 reads and writes. This improves performance, but if the computer
8692 crashes, data may be lost or the filesystem corrupted as a
8693 result. @command{sync} ensures everything in memory is written to disk.
8695 Any arguments are ignored, except for a lone @option{--help} or
8696 @option{--version} (@pxref{Common options}).
8702 @chapter Printing text
8704 @cindex printing text, commands for
8705 @cindex commands for printing text
8707 This section describes commands that display text strings.
8710 * echo invocation:: Print a line of text.
8711 * printf invocation:: Format and print data.
8712 * yes invocation:: Print a string until interrupted.
8716 @node echo invocation
8717 @section @command{echo}: Print a line of text
8720 @cindex displaying text
8721 @cindex printing text
8722 @cindex text, displaying
8723 @cindex arbitrary text, displaying
8725 @command{echo} writes each given @var{string} to standard output, with a
8726 space between each and a newline after the last one. Synopsis:
8729 echo [@var{option}]@dots{} [@var{string}]@dots{}
8732 The program accepts the following options. Also see @ref{Common options}.
8737 Do not output the trailing newline.
8741 @cindex backslash escapes
8742 Enable interpretation of the following backslash-escaped characters in
8751 suppress trailing newline
8765 the character whose @acronym{ASCII} code is @var{nnn} (octal); if @var{nnn} is not
8766 a valid octal number, it is printed literally.
8774 @node printf invocation
8775 @section @command{printf}: Format and print data
8778 @command{printf} does formatted printing of text. Synopsis:
8781 printf @var{format} [@var{argument}]@dots{}
8784 @command{printf} prints the @var{format} string, interpreting @samp{%}
8785 directives and @samp{\} escapes in the same way as the C @command{printf}
8786 function. The @var{format} argument is re-used as necessary to convert
8787 all of the given @var{argument}s.
8790 A floating-point argument must use a period before any fractional
8791 digits, but is printed according to the @env{LC_NUMERIC} category of the
8792 current locale. For example, in a locale whose radix character is a
8793 comma, the command @samp{printf %g 3.14} outputs @samp{3,14} whereas
8794 the command @samp{printf %g 3,14} is an error.
8796 @command{printf} has one additional directive, @samp{%b}, which prints its
8797 argument string with @samp{\} escapes interpreted in the same way as in
8798 the @var{format} string, except that octal escapes are of the form
8799 @samp{\0@var{ooo}} where @var{ooo} is 0 to 3 octal digits.
8804 @command{printf} interprets @samp{\@var{ooo}} in @var{format} as an octal number
8805 (if @var{ooo} is 1 to 3 octal digits) specifying a character to print,
8806 and @samp{\x@var{hh}} as a hexadecimal number (if @var{hh} is 1 to 2 hex
8807 digits) specifying a character to print.
8811 @command{printf} interprets two character syntaxes introduced in ISO C 99:
8812 @samp{\u} for 16-bit Unicode characters, specified as 4 hex digits
8813 @var{hhhh}, and @samp{\U} for 32-bit Unicode characters, specified as 8 hex
8814 digits @var{hhhhhhhh}. @command{printf} outputs the Unicode characters
8815 according to the LC_CTYPE part of the current locale, i.e. depending
8816 on the values of the environment variables @env{LC_ALL}, @env{LC_CTYPE},
8819 The processing of @samp{\u} and @samp{\U} requires a full-featured
8820 @code{iconv} facility. It is activated on systems with glibc 2.2 (or newer),
8821 or when @code{libiconv} is installed prior to this package. Otherwise the
8822 use of @samp{\u} and @samp{\U} will give an error message.
8825 An additional escape, @samp{\c}, causes @command{printf} to produce no
8828 The only options are a lone @option{--help} or
8829 @option{--version}. @xref{Common options}.
8831 The Unicode character syntaxes are useful for writing strings in a locale
8832 independent way. For example, a string containing the Euro currency symbol
8835 $ /usr/local/bin/printf '\u20AC 14.95'
8839 will be output correctly in all locales supporting the Euro symbol
8840 (ISO-8859-15, UTF-8, and others). Similarly, a Chinese string
8843 $ /usr/local/bin/printf '\u4e2d\u6587'
8847 will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).
8849 Note that in these examples, the full pathname of @command{printf} has been
8850 given, to distinguish it from the GNU @code{bash} builtin function
8853 For larger strings, you don't need to look up the hexadecimal code
8854 values of each character one by one. @acronym{ASCII} characters mixed with \u
8855 escape sequences is also known as the JAVA source file encoding. You can
8856 use GNU recode 3.5c (or newer) to convert strings to this encoding. Here
8857 is how to convert a piece of text into a shell script which will output
8858 this text in a locale-independent way:
8861 $ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
8862 '\u4e2d\u6587\n' > sample.txt
8863 $ recode BIG5..JAVA < sample.txt \
8864 | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
8871 @node yes invocation
8872 @section @command{yes}: Print a string until interrupted
8875 @cindex repeated output of a string
8877 @command{yes} prints the command line arguments, separated by spaces and
8878 followed by a newline, forever until it is killed. If no arguments are
8879 given, it prints @samp{y} followed by a newline forever until killed.
8881 Upon a write error, @command{yes} exits with status @samp{1}.
8883 The only options are a lone @option{--help} or @option{--version}.
8884 @xref{Common options}.
8891 @cindex commands for exit status
8892 @cindex exit status commands
8894 This section describes commands that are primarily useful for their exit
8895 status, rather than their output. Thus, they are often used as the
8896 condition of shell @code{if} statements, or as the last command in a
8900 * false invocation:: Do nothing, unsuccessfully.
8901 * true invocation:: Do nothing, successfully.
8902 * test invocation:: Check file types and compare values.
8903 * expr invocation:: Evaluate expressions.
8907 @node false invocation
8908 @section @command{false}: Do nothing, unsuccessfully
8911 @cindex do nothing, unsuccessfully
8912 @cindex failure exit status
8913 @cindex exit status of @command{false}
8915 @command{false} does nothing except return an exit status of 1, meaning
8916 @dfn{failure}. It can be used as a place holder in shell scripts
8917 where an unsuccessful command is needed.
8919 By default, @command{false} honors the @option{--help} and @option{--version}
8920 options. However, that is contrary to @acronym{POSIX}, so when the environment
8921 variable @env{POSIXLY_CORRECT} is set, @command{false} ignores @emph{all}
8922 command line arguments, including @option{--help} and @option{--version}.
8924 This version of @command{false} is implemented as a C program, and is thus
8925 more secure and faster than a shell script implementation, and may safely
8926 be used as a dummy shell for the purpose of disabling accounts.
8928 Note that @command{false} (unlike all other programs documented herein)
8929 exits unsuccessfully, even when invoked with
8930 @option{--help} or @option{--version}.
8933 @node true invocation
8934 @section @command{true}: Do nothing, successfully
8937 @cindex do nothing, successfully
8939 @cindex successful exit
8940 @cindex exit status of @command{true}
8942 @command{true} does nothing except return an exit status of 0, meaning
8943 @dfn{success}. It can be used as a place holder in shell scripts
8944 where a successful command is needed, although the shell built-in
8945 command @code{:} (colon) may do the same thing faster.
8946 In most modern shells, @command{true} is a built-in command, so when
8947 you use @samp{true} in a script, you're probably using the built-in
8948 command, not the one documented here.
8950 By default, @command{true} honors the @option{--help} and @option{--version}
8951 options. However, that is contrary to @acronym{POSIX}, so when the environment
8952 variable @env{POSIXLY_CORRECT} is set, @command{true} ignores @emph{all}
8953 command line arguments, including @option{--help} and @option{--version}.
8955 Note, however, that it is possible to cause @command{true}
8956 to exit with nonzero status: when invoked in non-@acronym{POSIX} mode,
8957 with the @option{--help} or @option{--version} option, and with standard
8958 output already closed or redirected to a file that evokes an I/O error.
8959 For example, using a Bourne-compatible shell:
8962 $ ./true --version >&-
8963 ./true: write error: Bad file number
8964 $ ./true --version > /dev/full
8965 ./true: write error: No space left on device
8968 This version of @command{true} is implemented as a C program, and is thus
8969 more secure and faster than a shell script implementation, and may safely
8970 be used as a dummy shell for the purpose of disabling accounts.
8972 @node test invocation
8973 @section @command{test}: Check file types and compare values
8976 @cindex check file types
8977 @cindex compare values
8978 @cindex expression evaluation
8980 @command{test} returns a status of 0 (true) or 1 (false) depending on the
8981 evaluation of the conditional expression @var{expr}. Each part of the
8982 expression must be a separate argument.
8984 @command{test} has file status checks, string operators, and numeric
8985 comparison operators.
8987 @command{test} has an alternate form that uses opening and closing
8988 square brackets instead a leading @samp{test}. For example, instead
8989 of @samp{test -d /}, you can write @samp{[ -d / ]}. The square
8990 brackets must be separate arguments; for example, @samp{[-d /]} does
8991 not have the desired effect. Since @samp{test @var{expr}} and @samp{[
8992 @var{expr} ]} have the same meaning, only the former form is discussed
8995 @cindex conflicts with shell built-ins
8996 @cindex built-in shell commands, conflicts with
8997 Because most shells have a built-in command by the same name, using the
8998 unadorned command name in a script or interactively may get you
8999 different functionality than that described here.
9001 Besides the options below, a single argument is also allowed:
9002 @command{test} returns true if the argument is not null. The argument
9003 can be any string, including strings like @samp{-d}, @samp{-1},
9004 @samp{--}, @samp{--help}, and @samp{--version} that most other
9005 programs would treat as options. To get help and version information,
9006 invoke the commands @samp{[ --help} and @samp{[ --version}, without
9007 the usual closing brackets. @xref{Common options}.
9009 @cindex exit status of @command{test}
9013 0 if the expression is true,
9014 1 if the expression is false,
9015 2 if an error occurred.
9019 * File type tests:: -[bcdfhLpSt]
9020 * Access permission tests:: -[gkruwxOG]
9021 * File characteristic tests:: -e -s -nt -ot -ef
9022 * String tests:: -z -n = !=
9023 * Numeric tests:: -eq -ne -lt -le -gt -ge
9024 * Connectives for test:: ! -a -o
9028 @node File type tests
9029 @subsection File type tests
9031 @cindex file type tests
9033 These options test for particular types of files. (Everything's a file,
9034 but not all files are the same!)
9040 @cindex block special check
9041 True if @var{file} exists and is a block special device.
9045 @cindex character special check
9046 True if @var{file} exists and is a character special device.
9050 @cindex directory check
9051 True if @var{file} exists and is a directory.
9055 @cindex regular file check
9056 True if @var{file} exists and is a regular file.
9059 @itemx -L @var{file}
9062 @cindex symbolic link check
9063 True if @var{file} exists and is a symbolic link.
9067 @cindex named pipe check
9068 True if @var{file} exists and is a named pipe.
9072 @cindex socket check
9073 True if @var{file} exists and is a socket.
9077 @cindex terminal check
9078 True if @var{fd} is a file descriptor that is associated with a
9084 @node Access permission tests
9085 @subsection Access permission tests
9087 @cindex access permission tests
9088 @cindex permission tests
9090 These options test for particular access permissions.
9096 @cindex set-group-id check
9097 True if @var{file} exists and has its set-group-id bit set.
9101 @cindex sticky bit check
9102 True if @var{file} has its @dfn{sticky} bit set.
9106 @cindex readable file check
9107 True if @var{file} exists and is readable.
9111 @cindex set-user-id check
9112 True if @var{file} exists and has its set-user-id bit set.
9116 @cindex writable file check
9117 True if @var{file} exists and is writable.
9121 @cindex executable file check
9122 True if @var{file} exists and is executable.
9126 @cindex owned by effective uid check
9127 True if @var{file} exists and is owned by the current effective user id.
9131 @cindex owned by effective gid check
9132 True if @var{file} exists and is owned by the current effective group id.
9136 @node File characteristic tests
9137 @subsection File characteristic tests
9139 @cindex file characteristic tests
9141 These options test other file characteristics.
9147 @cindex existence-of-file check
9148 True if @var{file} exists.
9152 @cindex nonempty file check
9153 True if @var{file} exists and has a size greater than zero.
9155 @item @var{file1} -nt @var{file2}
9157 @cindex newer-than file check
9158 True if @var{file1} is newer (according to modification date) than
9159 @var{file2}, or if @var{file1} exists and @var{file2} does not.
9161 @item @var{file1} -ot @var{file2}
9163 @cindex older-than file check
9164 True if @var{file1} is older (according to modification date) than
9165 @var{file2}, or if @var{file2} exists and @var{file1} does not.
9167 @item @var{file1} -ef @var{file2}
9169 @cindex same file check
9170 @cindex hard link check
9171 True if @var{file1} and @var{file2} have the same device and inode
9172 numbers, i.e., if they are hard links to each other.
9178 @subsection String tests
9180 @cindex string tests
9182 These options test string characteristics. Strings are not quoted for
9183 @command{test}, though you may need to quote them to protect characters
9184 with special meaning to the shell, e.g., spaces.
9188 @item -z @var{string}
9190 @cindex zero-length string check
9191 True if the length of @var{string} is zero.
9193 @item -n @var{string}
9196 @cindex nonzero-length string check
9197 True if the length of @var{string} is nonzero.
9199 @item @var{string1} = @var{string2}
9201 @cindex equal string check
9202 True if the strings are equal.
9204 @item @var{string1} != @var{string2}
9206 @cindex not-equal string check
9207 True if the strings are not equal.
9213 @subsection Numeric tests
9215 @cindex numeric tests
9216 @cindex arithmetic tests
9218 Numeric relationals. The arguments must be entirely numeric (possibly
9219 negative), or the special expression @w{@code{-l @var{string}}}, which
9220 evaluates to the length of @var{string}.
9224 @item @var{arg1} -eq @var{arg2}
9225 @itemx @var{arg1} -ne @var{arg2}
9226 @itemx @var{arg1} -lt @var{arg2}
9227 @itemx @var{arg1} -le @var{arg2}
9228 @itemx @var{arg1} -gt @var{arg2}
9229 @itemx @var{arg1} -ge @var{arg2}
9236 These arithmetic binary operators return true if @var{arg1} is equal,
9237 not-equal, less-than, less-than-or-equal, greater-than, or
9238 greater-than-or-equal than @var{arg2}, respectively.
9245 test -1 -gt -2 && echo yes
9247 test -l abc -gt 1 && echo yes
9250 @error{} test: integer expression expected before -eq
9254 @node Connectives for test
9255 @subsection Connectives for @command{test}
9257 @cindex logical connectives
9258 @cindex connectives, logical
9260 The usual logical connectives.
9266 True if @var{expr} is false.
9268 @item @var{expr1} -a @var{expr2}
9270 @cindex logical and operator
9271 @cindex and operator
9272 True if both @var{expr1} and @var{expr2} are true.
9274 @item @var{expr1} -o @var{expr2}
9276 @cindex logical or operator
9278 True if either @var{expr1} or @var{expr2} is true.
9283 @node expr invocation
9284 @section @command{expr}: Evaluate expressions
9287 @cindex expression evaluation
9288 @cindex evaluation of expressions
9290 @command{expr} evaluates an expression and writes the result on standard
9291 output. Each token of the expression must be a separate argument.
9293 Operands are either integers or strings. Integers consist of one or
9294 more decimal digits, with an optional leading @samp{-}.
9295 @command{expr} converts
9296 anything appearing in an operand position to an integer or a string
9297 depending on the operation being applied to it.
9299 Strings are not quoted for @command{expr} itself, though you may need to
9300 quote them to protect characters with special meaning to the shell,
9301 e.g., spaces. However, regardless of whether it is quoted, a string
9302 operand should not be a parenthesis or any of @command{expr}'s
9303 operators like @code{+}, so you cannot safely pass an arbitrary string
9304 @code{$str} to expr merely by quoting it to the shell. One way to
9305 work around this is to use the @command{GNU} extension @code{+},
9306 (e.g., @code{+ "$str" = foo}); a more portable way is to use
9307 @code{@w{" $str"}} and to adjust the rest of the expression to take
9308 the leading space into account (e.g., @code{@w{" $str" = " foo"}}).
9310 You should not pass a negative integer or a string with leading
9311 @samp{-} as @command{expr}'s first argument, as it might be
9312 misinterpreted as an option; this can be avoided by parenthesization.
9313 Also, portable scripts should not use a string operand that happens to
9314 take the form of an integer; this can be worked around by inserting
9315 leading spaces as mentioned above.
9317 @cindex parentheses for grouping
9318 Operators may be given as infix symbols or prefix keywords. Parentheses
9319 may be used for grouping in the usual manner. You must quote
9320 parentheses and many operators to avoid the shell evaluating them,
9323 @cindex exit status of @command{expr}
9327 0 if the expression is neither null nor 0,
9328 1 if the expression is null or 0,
9329 2 if the expression is syntactically invalid,
9330 3 if an error occurred.
9334 * String expressions:: + : match substr index length
9335 * Numeric expressions:: + - * / %
9336 * Relations for expr:: | & < <= = == != >= >
9337 * Examples of expr:: Examples.
9341 @node String expressions
9342 @subsection String expressions
9344 @cindex string expressions
9345 @cindex expressions, string
9347 @command{expr} supports pattern matching and other string operators. These
9348 have lower precedence than both the numeric and relational operators (in
9353 @item @var{string} : @var{regex}
9354 @cindex pattern matching
9355 @cindex regular expression matching
9356 @cindex matching patterns
9357 Perform pattern matching. The arguments are converted to strings and the
9358 second is considered to be a (basic, a la GNU @code{grep}) regular
9359 expression, with a @code{^} implicitly prepended. The first argument is
9360 then matched against this regular expression.
9362 If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
9363 @code{:} expression returns the part of @var{string} that matched the
9364 subexpression; otherwise, it returns the number of characters matched.
9366 If the match fails, the @code{:} operator returns the null string if
9367 @samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
9369 @kindex \( @r{regexp operator}
9370 Only the first @samp{\( @dots{} \)} pair is relevant to the return
9371 value; additional pairs are meaningful only for grouping the regular
9372 expression operators.
9374 @kindex \+ @r{regexp operator}
9375 @kindex \? @r{regexp operator}
9376 @kindex \| @r{regexp operator}
9377 In the regular expression, @code{\+}, @code{\?}, and @code{\|} are
9378 operators which respectively match one or more, zero or one, or separate
9379 alternatives. SunOS and other @command{expr}'s treat these as regular
9380 characters. (@acronym{POSIX} allows either behavior.)
9381 @xref{Top, , Regular Expression Library, regex, Regex}, for details of
9382 regular expression syntax. Some examples are in @ref{Examples of expr}.
9384 @item match @var{string} @var{regex}
9386 An alternative way to do pattern matching. This is the same as
9387 @w{@samp{@var{string} : @var{regex}}}.
9389 @item substr @var{string} @var{position} @var{length}
9391 Returns the substring of @var{string} beginning at @var{position}
9392 with length at most @var{length}. If either @var{position} or
9393 @var{length} is negative, zero, or non-numeric, returns the null string.
9395 @item index @var{string} @var{charset}
9397 Returns the first position in @var{string} where the first character in
9398 @var{charset} was found. If no character in @var{charset} is found in
9399 @var{string}, return 0.
9401 @item length @var{string}
9403 Returns the length of @var{string}.
9407 Interpret @var{token} as a string, even if it is a keyword like @var{match}
9408 or an operator like @code{/}.
9409 This makes it possible to test @code{expr length + "$x"} or
9410 @code{expr + "$x" : '.*/\(.\)'} and have it do the right thing even if
9411 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
9412 This operator is a GNU extension. Portable shell scripts should use
9413 @code{@w{" $token"} : @w{' \(.*\)'}} instead of @code{+ "$token"}.
9417 To make @command{expr} interpret keywords as strings, you must use the
9418 @code{quote} operator.
9421 @node Numeric expressions
9422 @subsection Numeric expressions
9424 @cindex numeric expressions
9425 @cindex expressions, numeric
9427 @command{expr} supports the usual numeric operators, in order of increasing
9428 precedence. The string operators (previous section) have lower precedence,
9429 the connectives (next section) have higher.
9438 Addition and subtraction. Both arguments are converted to integers;
9439 an error occurs if this cannot be done.
9445 @cindex multiplication
9448 Multiplication, division, remainder. Both arguments are converted to
9449 integers; an error occurs if this cannot be done.
9454 @node Relations for expr
9455 @subsection Relations for @command{expr}
9457 @cindex connectives, logical
9458 @cindex logical connectives
9459 @cindex relations, numeric or string
9461 @command{expr} supports the usual logical connectives and relations. These
9462 are higher precedence than either the string or numeric operators
9463 (previous sections). Here is the list, lowest-precedence operator first.
9469 @cindex logical or operator
9471 Returns its first argument if that is neither null nor zero, otherwise
9472 its second argument if it is neither null nor zero, otherwise 0. It
9473 does not evaluate its second argument if its first argument is neither
9478 @cindex logical and operator
9479 @cindex and operator
9480 Return its first argument if neither argument is null or zero, otherwise
9481 0. It does not evaluate its second argument if its first argument is
9484 @item < <= = == != >= >
9491 @cindex comparison operators
9493 Compare the arguments and return 1 if the relation is true, 0 otherwise.
9494 @code{==} is a synonym for @code{=}. @command{expr} first tries to convert
9495 both arguments to integers and do a numeric comparison; if either
9496 conversion fails, it does a lexicographic comparison using the character
9497 collating sequence specified by the @env{LC_COLLATE} locale.
9502 @node Examples of expr
9503 @subsection Examples of using @command{expr}
9505 @cindex examples of @command{expr}
9506 Here are a few examples, including quoting for shell metacharacters.
9508 To add 1 to the shell variable @code{foo}, in Bourne-compatible shells:
9513 To print the non-directory part of the file name stored in
9514 @code{$fname}, which need not contain a @code{/}.
9516 expr $fname : '.*/\(.*\)' '|' $fname
9519 An example showing that @code{\+} is an operator:
9526 expr abc : 'a\(.\)c'
9528 expr index abcdef cz
9531 @error{} expr: syntax error
9532 expr index quote index a
9538 @chapter Redirection
9541 @cindex commands for redirection
9543 Unix shells commonly provide several forms of @dfn{redirection}---ways
9544 to change the input source or output destination of a command. But one
9545 useful redirection is performed by a separate command, not by the shell;
9546 it's described here.
9549 * tee invocation:: Redirect output to multiple files.
9553 @node tee invocation
9554 @section @command{tee}: Redirect output to multiple files
9557 @cindex pipe fitting
9558 @cindex destinations, multiple output
9559 @cindex read from stdin and write to stdout and files
9561 The @command{tee} command copies standard input to standard output and also
9562 to any files given as arguments. This is useful when you want not only
9563 to send some data down a pipe, but also to save a copy. Synopsis:
9566 tee [@var{option}]@dots{} [@var{file}]@dots{}
9569 If a file being written to does not already exist, it is created. If a
9570 file being written to already exists, the data it previously contained
9571 is overwritten unless the @option{-a} option is used.
9573 The program accepts the following options. Also see @ref{Common options}.
9580 Append standard input to the given files rather than overwriting
9584 @itemx --ignore-interrupts
9586 @opindex --ignore-interrupts
9587 Ignore interrupt signals.
9594 @node File name manipulation
9595 @chapter File name manipulation
9597 @cindex file name manipulation
9598 @cindex manipulation of file names
9599 @cindex commands for file name manipulation
9601 This section describes commands that manipulate file names.
9604 * basename invocation:: Strip directory and suffix from a file name.
9605 * dirname invocation:: Strip non-directory suffix from a file name.
9606 * pathchk invocation:: Check file name portability.
9610 @node basename invocation
9611 @section @command{basename}: Strip directory and suffix from a file name
9614 @cindex strip directory and suffix from file names
9615 @cindex directory, stripping from file names
9616 @cindex suffix, stripping from file names
9617 @cindex file names, stripping directory and suffix
9618 @cindex leading directory components, stripping
9620 @command{basename} removes any leading directory components from
9621 @var{name}. Synopsis:
9624 basename @var{name} [@var{suffix}]
9627 If @var{suffix} is specified and is identical to the end of @var{name},
9628 it is removed from @var{name} as well. @command{basename} prints the
9629 result on standard output.
9631 The only options are @option{--help} and @option{--version}. @xref{Common
9637 @node dirname invocation
9638 @section @command{dirname}: Strip non-directory suffix from a file name
9641 @cindex directory components, printing
9642 @cindex stripping non-directory suffix
9643 @cindex non-directory suffix, stripping
9645 @command{dirname} prints all but the final slash-delimited component of
9646 a string (presumably a filename). Synopsis:
9652 If @var{name} is a single component, @command{dirname} prints @samp{.}
9653 (meaning the current directory).
9655 The only options are @option{--help} and @option{--version}. @xref{Common
9661 @node pathchk invocation
9662 @section @command{pathchk}: Check file name portability
9665 @cindex file names, checking validity and portability
9666 @cindex valid file names, checking for
9667 @cindex portable file names, checking for
9669 @command{pathchk} checks portability of filenames. Synopsis:
9672 pathchk [@var{option}]@dots{} @var{name}@dots{}
9675 For each @var{name}, @command{pathchk} prints a message if any of
9676 these conditions is true:
9679 one of the existing directories in @var{name} does not have search
9680 (execute) permission,
9682 the length of @var{name} is larger than its filesystem's maximum
9685 the length of one component of @var{name}, corresponding to an
9686 existing directory name, is larger than its filesystem's maximum
9687 length for a file name component.
9690 The program accepts the following option. Also see @ref{Common options}.
9695 @itemx --portability
9697 @opindex --portability
9698 Instead of performing length checks on the underlying filesystem,
9699 test the length of each file name and its components against the
9700 @acronym{POSIX} minimum limits for portability. Also check that the file
9701 name contains no characters not in the portable file name character set.
9705 @cindex exit status of @command{pathchk}
9709 0 if all specified file names passed all of the tests,
9714 @node Working context
9715 @chapter Working context
9717 @cindex working context
9718 @cindex commands for printing the working context
9720 This section describes commands that display or alter the context in
9721 which you are working: the current directory, the terminal settings, and
9722 so forth. See also the user-related commands in the next section.
9725 * pwd invocation:: Print working directory.
9726 * stty invocation:: Print or change terminal characteristics.
9727 * printenv invocation:: Print environment variables.
9728 * tty invocation:: Print file name of terminal on standard input.
9732 @node pwd invocation
9733 @section @command{pwd}: Print working directory
9736 @cindex print name of current directory
9737 @cindex current working directory, printing
9738 @cindex working directory, printing
9740 @cindex symbolic links and @command{pwd}
9741 @command{pwd} prints the fully resolved name of the current directory.
9742 That is, all components of the printed name will be actual directory
9743 names---none will be symbolic links.
9745 @cindex conflicts with shell built-ins
9746 @cindex built-in shell commands, conflicts with
9747 Because most shells have a built-in command by the same name, using the
9748 unadorned command name in a script or interactively may get you
9749 different functionality than that described here.
9751 The only options are a lone @option{--help} or
9752 @option{--version}. @xref{Common options}.
9757 @node stty invocation
9758 @section @command{stty}: Print or change terminal characteristics
9761 @cindex change or print terminal settings
9762 @cindex terminal settings
9763 @cindex line settings of terminal
9765 @command{stty} prints or changes terminal characteristics, such as baud rate.
9769 stty [@var{option}] [@var{setting}]@dots{}
9773 If given no line settings, @command{stty} prints the baud rate, line
9774 discipline number (on systems that support it), and line settings
9775 that have been changed from the values set by @samp{stty sane}.
9776 By default, mode reading and setting are performed on the tty line
9777 connected to standard input, although this can be modified by the
9778 @option{--file} option.
9780 @command{stty} accepts many non-option arguments that change aspects of
9781 the terminal line operation, as described below.
9783 The program accepts the following options. Also see @ref{Common options}.
9790 Print all current settings in human-readable form. This option may not
9791 be used in combination with any line settings.
9793 @item -F @var{device}
9794 @itemx --file=@var{device}
9797 Set the line opened by the filename specified in @var{device} instead of
9798 the tty line connected to standard input. This option is necessary
9799 because opening a @acronym{POSIX} tty requires use of the @code{O_NONDELAY} flag to
9800 prevent a @acronym{POSIX} tty from blocking until the carrier detect line is high if
9801 the @code{clocal} flag is not set. Hence, it is not always possible
9802 to allow the shell to open the device in the traditional manner.
9808 @cindex machine-readable @command{stty} output
9809 Print all current settings in a form that can be used as an argument to
9810 another @command{stty} command to restore the current settings. This option
9811 may not be used in combination with any line settings.
9815 Many settings can be turned off by preceding them with a @samp{-}.
9816 Such arguments are marked below with ``May be negated'' in their
9817 description. The descriptions themselves refer to the positive
9818 case, that is, when @emph{not} negated (unless stated otherwise,
9821 Some settings are not available on all @acronym{POSIX} systems, since they use
9822 extensions. Such arguments are marked below with ``Non-@acronym{POSIX}'' in their
9823 description. On non-@acronym{POSIX} systems, those or other settings also may not
9824 be available, but it's not feasible to document all the variations: just
9830 * Control:: Control settings
9831 * Input:: Input settings
9832 * Output:: Output settings
9833 * Local:: Local settings
9834 * Combination:: Combination settings
9835 * Characters:: Special characters
9836 * Special:: Special settings
9841 @subsection Control settings
9843 @cindex control settings
9849 @cindex two-way parity
9850 Generate parity bit in output and expect parity bit in input.
9857 Set odd parity (even if negated). May be negated.
9864 @cindex character size
9865 @cindex eight-bit characters
9866 Set character size to 5, 6, 7, or 8 bits.
9871 Send a hangup signal when the last process closes the tty. May be
9877 Use two stop bits per character (one if negated). May be negated.
9881 Allow input to be received. May be negated.
9885 @cindex modem control
9886 Disable modem control signals. May be negated.
9890 @cindex hardware flow control
9891 @cindex flow control, hardware
9892 @cindex RTS/CTS flow control
9893 Enable RTS/CTS flow control. Non-@acronym{POSIX}. May be negated.
9898 @subsection Input settings
9900 @cindex input settings
9905 @cindex breaks, ignoring
9906 Ignore break characters. May be negated.
9910 @cindex breaks, cause interrupts
9911 Make breaks cause an interrupt signal. May be negated.
9915 @cindex parity, ignoring
9916 Ignore characters with parity errors. May be negated.
9920 @cindex parity errors, marking
9921 Mark parity errors (with a 255-0-character sequence). May be negated.
9925 Enable input parity checking. May be negated.
9929 @cindex eight-bit input
9930 Clear high (8th) bit of input characters. May be negated.
9934 @cindex newline, translating to return
9935 Translate newline to carriage return. May be negated.
9939 @cindex return, ignoring
9940 Ignore carriage return. May be negated.
9944 @cindex return, translating to newline
9945 Translate carriage return to newline. May be negated.
9949 @kindex C-s/C-q flow control
9950 @cindex XON/XOFF flow control
9951 Enable XON/XOFF flow control (that is, @kbd{CTRL-S}/@kbd{CTRL-Q}). May
9958 @cindex software flow control
9959 @cindex flow control, software
9960 Enable sending of @code{stop} character when the system input buffer
9961 is almost full, and @code{start} character when it becomes almost
9962 empty again. May be negated.
9966 @cindex uppercase, translating to lowercase
9967 Translate uppercase characters to lowercase. Non-@acronym{POSIX}. May be
9972 Allow any character to restart output (only the start character
9973 if negated). Non-@acronym{POSIX}. May be negated.
9977 @cindex beeping at input buffer full
9978 Enable beeping and not flushing input buffer if a character arrives
9979 when the input buffer is full. Non-@acronym{POSIX}. May be negated.
9984 @subsection Output settings
9986 @cindex output settings
9987 These arguments specify output-related operations.
9992 Postprocess output. May be negated.
9996 @cindex lowercase, translating to output
9997 Translate lowercase characters to uppercase. Non-@acronym{POSIX}. May be
10002 @cindex return, translating to newline
10003 Translate carriage return to newline. Non-@acronym{POSIX}. May be negated.
10007 @cindex newline, translating to crlf
10008 Translate newline to carriage return-newline. Non-@acronym{POSIX}. May be
10013 Do not print carriage returns in the first column. Non-@acronym{POSIX}.
10018 Newline performs a carriage return. Non-@acronym{POSIX}. May be negated.
10022 @cindex pad instead of timing for delaying
10023 Use fill (padding) characters instead of timing for delays. Non-@acronym{POSIX}.
10028 @cindex pad character
10029 Use delete characters for fill instead of null characters. Non-@acronym{POSIX}.
10035 Newline delay style. Non-@acronym{POSIX}.
10042 Carriage return delay style. Non-@acronym{POSIX}.
10048 @opindex tab@var{n}
10049 Horizontal tab delay style. Non-@acronym{POSIX}.
10054 Backspace delay style. Non-@acronym{POSIX}.
10059 Vertical tab delay style. Non-@acronym{POSIX}.
10064 Form feed delay style. Non-@acronym{POSIX}.
10069 @subsection Local settings
10071 @cindex local settings
10076 Enable @code{interrupt}, @code{quit}, and @code{suspend} special
10077 characters. May be negated.
10081 Enable @code{erase}, @code{kill}, @code{werase}, and @code{rprnt}
10082 special characters. May be negated.
10086 Enable non-@acronym{POSIX} special characters. May be negated.
10090 Echo input characters. May be negated.
10096 Echo @code{erase} characters as backspace-space-backspace. May be
10101 @cindex newline echoing after @code{kill}
10102 Echo a newline after a @code{kill} character. May be negated.
10106 @cindex newline, echoing
10107 Echo newline even if not echoing other characters. May be negated.
10111 @cindex flushing, disabling
10112 Disable flushing after @code{interrupt} and @code{quit} special
10113 characters. May be negated.
10117 @cindex case translation
10118 Enable input and output of uppercase characters by preceding their
10119 lowercase equivalents with @samp{\}, when @code{icanon} is set.
10120 Non-@acronym{POSIX}. May be negated.
10124 @cindex background jobs, stopping at terminal write
10125 Stop background jobs that try to write to the terminal. Non-@acronym{POSIX}.
10132 Echo erased characters backward, between @samp{\} and @samp{/}.
10133 Non-@acronym{POSIX}. May be negated.
10139 @cindex control characters, using @samp{^@var{c}}
10140 @cindex hat notation for control characters
10141 Echo control characters in hat notation (@samp{^@var{c}}) instead
10142 of literally. Non-@acronym{POSIX}. May be negated.
10148 Echo the @code{kill} special character by erasing each character on
10149 the line as indicated by the @code{echoprt} and @code{echoe} settings,
10150 instead of by the @code{echoctl} and @code{echok} settings. Non-@acronym{POSIX}.
10156 @subsection Combination settings
10158 @cindex combination settings
10159 Combination settings:
10166 Same as @code{parenb -parodd cs7}. May be negated. If negated, same
10167 as @code{-parenb cs8}.
10171 Same as @code{parenb parodd cs7}. May be negated. If negated, same
10172 as @code{-parenb cs8}.
10176 Same as @code{-icrnl -onlcr}. May be negated. If negated, same as
10177 @code{icrnl -inlcr -igncr onlcr -ocrnl -onlret}.
10181 Reset the @code{erase} and @code{kill} special characters to their default
10187 @c This is too long to write inline.
10189 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff
10190 -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr
10191 -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
10192 ff0 isig icanon iexten echo echoe echok -echonl
10193 -noflsh -xcase -tostop -echoprt echoctl echoke
10195 @noindent and also sets all special characters to their default values.
10199 Same as @code{brkint ignpar istrip icrnl ixon opost isig icanon}, plus
10200 sets the @code{eof} and @code{eol} characters to their default values
10201 if they are the same as the @code{min} and @code{time} characters.
10202 May be negated. If negated, same as @code{raw}.
10208 -ignbrk -brkint -ignpar -parmrk -inpck -istrip
10209 -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
10210 -imaxbel -opost -isig -icanon -xcase min 1 time 0
10212 @noindent May be negated. If negated, same as @code{cooked}.
10216 Same as @option{-icanon}. May be negated. If negated, same as
10221 @cindex eight-bit characters
10222 Same as @code{-parenb -istrip cs8}. May be negated. If negated,
10223 same as @code{parenb istrip cs7}.
10227 Same as @option{-parenb -istrip -opost cs8}. May be negated.
10228 If negated, same as @code{parenb istrip opost cs7}.
10232 Same as @option{-ixany}. Non-@acronym{POSIX}. May be negated.
10236 Same as @code{tab0}. Non-@acronym{POSIX}. May be negated. If negated, same
10243 Same as @code{xcase iuclc olcuc}. Non-@acronym{POSIX}. May be negated.
10247 Same as @code{echoe echoctl echoke}.
10251 Same as @code{echoe echoctl echoke -ixany intr ^C erase ^? kill C-u}.
10256 @subsection Special characters
10258 @cindex special characters
10259 @cindex characters, special
10261 The special characters' default values vary from system to system.
10262 They are set with the syntax @samp{name value}, where the names are
10263 listed below and the value can be given either literally, in hat
10264 notation (@samp{^@var{c}}), or as an integer which may start with
10265 @samp{0x} to indicate hexadecimal, @samp{0} to indicate octal, or
10266 any other digit to indicate decimal.
10268 @cindex disabling special characters
10269 @kindex u@r{, and disabling special characters}
10270 For GNU stty, giving a value of @code{^-} or @code{undef} disables that
10271 special character. (This is incompatible with Ultrix @command{stty},
10272 which uses a value of @samp{u} to disable a special character. GNU
10273 @command{stty} treats a value @samp{u} like any other, namely to set that
10274 special character to @key{U}.)
10280 Send an interrupt signal.
10284 Send a quit signal.
10288 Erase the last character typed.
10292 Erase the current line.
10296 Send an end of file (terminate the input).
10304 Alternate character to end the line. Non-@acronym{POSIX}.
10308 Switch to a different shell layer. Non-@acronym{POSIX}.
10312 Restart the output after stopping it.
10320 Send a terminal stop signal.
10324 Send a terminal stop signal after flushing the input. Non-@acronym{POSIX}.
10328 Redraw the current line. Non-@acronym{POSIX}.
10332 Erase the last word typed. Non-@acronym{POSIX}.
10336 Enter the next character typed literally, even if it is a special
10337 character. Non-@acronym{POSIX}.
10342 @subsection Special settings
10344 @cindex special settings
10349 Set the minimum number of characters that will satisfy a read until
10350 the time value has expired, when @option{-icanon} is set.
10354 Set the number of tenths of a second before reads time out if the minimum
10355 number of characters have not been read, when @option{-icanon} is set.
10357 @item ispeed @var{n}
10359 Set the input speed to @var{n}.
10361 @item ospeed @var{n}
10363 Set the output speed to @var{n}.
10367 Tell the tty kernel driver that the terminal has @var{n} rows. Non-@acronym{POSIX}.
10370 @itemx columns @var{n}
10373 Tell the kernel that the terminal has @var{n} columns. Non-@acronym{POSIX}.
10379 Print the number of rows and columns that the kernel thinks the
10380 terminal has. (Systems that don't support rows and columns in the kernel
10381 typically use the environment variables @env{LINES} and @env{COLUMNS}
10382 instead; however, GNU @command{stty} does not know anything about them.)
10383 Non-@acronym{POSIX}.
10387 Use line discipline @var{n}. Non-@acronym{POSIX}.
10391 Print the terminal speed.
10394 @cindex baud rate, setting
10395 @c FIXME: Is this still true that the baud rate can't be set
10396 @c higher than 38400?
10397 Set the input and output speeds to @var{n}. @var{n} can be one
10398 of: 0 50 75 110 134 134.5 150 200 300 600 1200 1800 2400 4800 9600
10399 19200 38400 @code{exta} @code{extb}. @code{exta} is the same as
10400 19200; @code{extb} is the same as 38400. 0 hangs up the line if
10401 @option{-clocal} is set.
10405 @node printenv invocation
10406 @section @command{printenv}: Print all or some environment variables
10409 @cindex printing all or some environment variables
10410 @cindex environment variables, printing
10412 @command{printenv} prints environment variable values. Synopsis:
10415 printenv [@var{option}] [@var{variable}]@dots{}
10418 If no @var{variable}s are specified, @command{printenv} prints the value of
10419 every environment variable. Otherwise, it prints the value of each
10420 @var{variable} that is set, and nothing for those that are not set.
10422 The only options are a lone @option{--help} or @option{--version}.
10423 @xref{Common options}.
10425 @cindex exit status of @command{printenv}
10429 0 if all variables specified were found
10430 1 if at least one specified variable was not found
10431 2 if a write error occurred
10435 @node tty invocation
10436 @section @command{tty}: Print file name of terminal on standard input
10439 @cindex print terminal file name
10440 @cindex terminal file name, printing
10442 @command{tty} prints the file name of the terminal connected to its standard
10443 input. It prints @samp{not a tty} if standard input is not a terminal.
10447 tty [@var{option}]@dots{}
10450 The program accepts the following option. Also see @ref{Common options}.
10460 Print nothing; only return an exit status.
10464 @cindex exit status of @command{tty}
10468 0 if standard input is a terminal
10469 1 if standard input is not a terminal
10470 2 if given incorrect arguments
10471 3 if a write error occurs
10475 @node User information
10476 @chapter User information
10478 @cindex user information, commands for
10479 @cindex commands for printing user information
10481 This section describes commands that print user-related information:
10482 logins, groups, and so forth.
10485 * id invocation:: Print real and effective uid and gid.
10486 * logname invocation:: Print current login name.
10487 * whoami invocation:: Print effective user id.
10488 * groups invocation:: Print group names a user is in.
10489 * users invocation:: Print login names of users currently logged in.
10490 * who invocation:: Print who is currently logged in.
10494 @node id invocation
10495 @section @command{id}: Print real and effective uid and gid
10498 @cindex real uid and gid, printing
10499 @cindex effective uid and gid, printing
10500 @cindex printing real and effective uid and gid
10502 @command{id} prints information about the given user, or the process
10503 running it if no user is specified. Synopsis:
10506 id [@var{option}]@dots{} [@var{username}]
10509 By default, it prints the real user id, real group id, effective user id
10510 if different from the real user id, effective group id if different from
10511 the real group id, and supplemental group ids.
10513 Each of these numeric values is preceded by an identifying string and
10514 followed by the corresponding user or group name in parentheses.
10516 The options cause @command{id} to print only part of the above information.
10517 Also see @ref{Common options}.
10524 Print only the group id.
10530 Print only the supplementary groups.
10536 Print the user or group name instead of the ID number. Requires
10537 @option{-u}, @option{-g}, or @option{-G}.
10543 Print the real, instead of effective, user or group id. Requires
10544 @option{-u}, @option{-g}, or @option{-G}.
10550 Print only the user id.
10557 @node logname invocation
10558 @section @command{logname}: Print current login name
10561 @cindex printing user's login name
10562 @cindex login name, printing
10563 @cindex user name, printing
10568 @command{logname} prints the calling user's name, as found in the file
10569 @file{/etc/utmp}, and exits with a status of 0. If there is no
10570 @file{/etc/utmp} entry for the calling process, @command{logname} prints
10571 an error message and exits with a status of 1.
10573 The only options are @option{--help} and @option{--version}. @xref{Common
10579 @node whoami invocation
10580 @section @command{whoami}: Print effective user id
10583 @cindex effective UID, printing
10584 @cindex printing the effective UID
10586 @command{whoami} prints the user name associated with the current
10587 effective user id. It is equivalent to the command @samp{id -un}.
10589 The only options are @option{--help} and @option{--version}. @xref{Common
10595 @node groups invocation
10596 @section @command{groups}: Print group names a user is in
10599 @cindex printing groups a user is in
10600 @cindex supplementary groups, printing
10602 @command{groups} prints the names of the primary and any supplementary
10603 groups for each given @var{username}, or the current process if no names
10604 are given. If names are given, the name of each user is printed before
10605 the list of that user's groups. Synopsis:
10608 groups [@var{username}]@dots{}
10611 The group lists are equivalent to the output of the command @samp{id -Gn}.
10613 The only options are @option{--help} and @option{--version}. @xref{Common
10619 @node users invocation
10620 @section @command{users}: Print login names of users currently logged in
10623 @cindex printing current usernames
10624 @cindex usernames, printing current
10626 @cindex login sessions, printing users with
10627 @command{users} prints on a single line a blank-separated list of user
10628 names of users currently logged in to the current host. Each user name
10629 corresponds to a login session, so if a user has more than one login
10630 session, that user's name will appear the same number of times in the
10639 With no @var{file} argument, @command{users} extracts its information from
10640 the file @file{/etc/utmp}. If a file argument is given, @command{users}
10641 uses that file instead. A common choice is @file{/etc/wtmp}.
10643 The only options are @option{--help} and @option{--version}. @xref{Common
10649 @node who invocation
10650 @section @command{who}: Print who is currently logged in
10653 @cindex printing current user information
10654 @cindex information, about current users
10656 @command{who} prints information about users who are currently logged on.
10660 @command{who} [@var{option}] [@var{file}] [am i]
10663 @cindex terminal lines, currently used
10665 @cindex remote hostname
10666 If given no non-option arguments, @command{who} prints the following
10667 information for each user currently logged on: login name, terminal
10668 line, login time, and remote hostname or X display.
10672 If given one non-option argument, @command{who} uses that instead of
10673 @file{/etc/utmp} as the name of the file containing the record of
10674 users logged on. @file{/etc/wtmp} is commonly given as an argument
10675 to @command{who} to look at who has previously logged on.
10679 If given two non-option arguments, @command{who} prints only the entry
10680 for the user running it (determined from its standard input), preceded
10681 by the hostname. Traditionally, the two arguments given are @samp{am
10682 i}, as in @samp{who am i}.
10684 The program accepts the following options. Also see @ref{Common options}.
10692 Same as @samp{-b -d --login -p -r -t -T -u}.
10698 Print the date and time of last system boot.
10704 Print information corresponding to dead processes.
10710 Print column headings.
10716 Include idle time in HOURS:MINUTES, @samp{.} (to indicate
10717 current process), or @samp{old}.
10721 Same as @samp{who am i}.
10727 Print only the login names and the number of users logged on.
10728 Overrides all other options.
10732 Ignored; for compatibility with other versions of @command{who}.
10741 After the login time, print the number of hours and minutes that the
10742 user has been idle. @samp{.} means the user was active in last minute.
10743 @samp{old} means the user was idle for more than 24 hours.
10749 List only the entries that correspond to processes via which the
10750 system is waiting for a user to login. The user name is always @samp{LOGIN}.
10754 Attempt to canonicalize hostnames found in utmp through a DNS lookup. This
10755 is not the default because it can cause significant delays on systems with
10756 automatic dial-up internet access.
10762 Print a line of column headings.
10773 @opindex --writable
10774 @cindex message status
10775 @pindex write@r{, allowed}
10776 After each login name print a character indicating the user's message status:
10779 @samp{+} allowing @code{write} messages
10780 @samp{-} disallowing @code{write} messages
10781 @samp{?} cannot find terminal device
10789 @node System context
10790 @chapter System context
10792 @cindex system context
10793 @cindex context, system
10794 @cindex commands for system context
10796 This section describes commands that print or change system-wide
10800 * date invocation:: Print or set system date and time.
10801 * uname invocation:: Print system information.
10802 * hostname invocation:: Print or set system name.
10803 * hostid invocation:: Print numeric host identifier.
10807 @node date invocation
10808 @section @command{date}: Print or set system date and time
10811 @cindex time, printing or setting
10812 @cindex printing the current time
10817 date [@var{option}]@dots{} [+@var{format}]
10818 date [-u|--utc|--universal] @c this avoids a newline in the output
10819 [ MMDDhhmm[[CC]YY][.ss] ]
10822 Invoking @command{date} with no @var{format} argument is equivalent to invoking
10823 @samp{date '+%a %b %e %H:%M:%S %Z %Y'}.
10825 @findex strftime @r{and @command{date}}
10826 @cindex time formats
10827 @cindex formatting times
10828 If given an argument that starts with a @samp{+}, @command{date} prints the
10829 current time and date (or the time and date specified by the
10830 @option{--date} option, see below) in the format defined by that argument,
10831 which is the same as in the @code{strftime} function. Except for
10832 directives, which start with @samp{%}, characters in the format string
10833 are printed unchanged. The directives are described below.
10838 * Time directives:: %[HIklMprsSTXzZ]
10839 * Date directives:: %[aAbBcdDhjmUwWxyY]
10840 * Literal directives:: %[%nt]
10841 * Padding:: Pad with zeroes, spaces (%_), or nothing (%-).
10842 * Setting the time:: Changing the system clock.
10843 * Options for date:: Instead of the current time.
10844 * Examples of date:: Examples.
10847 @node Time directives
10848 @subsection Time directives
10850 @cindex time directives
10851 @cindex directives, time
10853 @command{date} directives related to times.
10865 minute (00@dots{}59)
10867 nanoseconds (000000000@dots{}999999999)
10869 locale's upper case @samp{AM} or @samp{PM} (blank in many locales)
10871 locale's lower case @samp{am} or @samp{pm} (blank in many locales)
10873 time, 12-hour (hh:mm:ss [AP]M)
10875 time, 24-hour (hh:mm). Same as @code{%H:%M}.
10877 @cindex epoch, seconds since
10878 @cindex seconds since the epoch
10879 @cindex beginning of time
10880 seconds since the epoch, i.e., 1 January 1970 00:00:00 UTC (a GNU extension).
10881 For examples, @xref{%s-examples}.
10883 second (00@dots{}60). The range is [00@dots{}60], and not [00@dots{}59],
10884 in order to accommodate the occasional positive leap second.
10886 time, 24-hour (hh:mm:ss)
10888 locale's time representation (%H:%M:%S)
10890 RFC-2822 style numeric time zone (e.g., -0600 or +0100), or nothing if no
10891 time zone is determinable. This value reflects the @emph{current} time
10892 zone. It isn't changed by the @option{--date} option.
10894 time zone (e.g., EDT), or nothing if no time zone is
10896 Note that this value reflects the @emph{current} time zone.
10897 It isn't changed by the @option{--date} option.
10901 @node Date directives
10902 @subsection Date directives
10904 @cindex date directives
10905 @cindex directives, date
10907 @command{date} directives related to dates.
10911 locale's abbreviated weekday name (Sun@dots{}Sat)
10913 locale's full weekday name, variable length (Sunday@dots{}Saturday)
10915 locale's abbreviated month name (Jan@dots{}Dec)
10917 locale's full month name, variable length (January@dots{}December)
10919 locale's date and time (Sat Nov 04 12:02:33 EST 1989)
10921 century (year divided by 100 and truncated to an integer) (00@dots{}99)
10923 day of month (01@dots{}31)
10927 blank-padded day of month (1@dots{}31)
10929 the @w{ISO 8601} standard date format: @code{%Y-%m-%d}.
10930 This is the preferred form for all uses.
10932 The year corresponding to the ISO week number, but without the century
10933 (range @code{00} through @code{99}). This has the same format and value
10934 as @code{%y}, except that if the ISO week number (see @code{%V}) belongs
10935 to the previous or next year, that year is used instead.
10937 The year corresponding to the ISO week number. This has the same format
10938 and value as @code{%Y}, except that if the ISO week number (see
10939 @code{%V}) belongs to the previous or next year, that year is used
10944 day of year (001@dots{}366)
10946 month (01@dots{}12)
10948 day of week (1@dots{}7) with 1 corresponding to Monday
10950 week number of year with Sunday as first day of week (00@dots{}53).
10951 Days in a new year preceding the first Sunday are in week zero.
10953 week number of year with Monday as first day of the week as a decimal
10954 (01@dots{}53). If the week containing January 1 has four or more days in
10955 the new year, then it is considered week 1; otherwise, it is week 53 of
10956 the previous year, and the next week is week 1. (See the @acronym{ISO} 8601
10959 day of week (0@dots{}6) with 0 corresponding to Sunday
10961 week number of year with Monday as first day of week (00@dots{}53).
10962 Days in a new year preceding the first Monday are in week zero.
10964 locale's date representation (mm/dd/yy)
10966 last two digits of year (00@dots{}99)
10968 year (1970@dots{}.)
10972 @node Literal directives
10973 @subsection Literal directives
10975 @cindex literal directives
10976 @cindex directives, literal
10978 @command{date} directives that produce literal strings.
10991 @subsection Padding
10993 @cindex numeric field padding
10994 @cindex padding of numeric fields
10995 @cindex fields, padding numeric
10997 By default, @command{date} pads numeric fields with zeroes, so that, for
10998 example, numeric months are always output as two digits. GNU @command{date}
10999 recognizes the following numeric modifiers between the @samp{%} and the
11004 (hyphen) do not pad the field; useful if the output is intended for
11007 (underscore) pad the field with spaces; useful if you need a fixed
11008 number of characters in the output, but zeroes are too distracting.
11012 These are GNU extensions.
11014 Here is an example illustrating the differences:
11017 date +%d/%m -d "Feb 1"
11019 date +%-d/%-m -d "Feb 1"
11021 date +%_d/%_m -d "Feb 1"
11026 @node Setting the time
11027 @subsection Setting the time
11029 @cindex setting the time
11030 @cindex time setting
11031 @cindex appropriate privileges
11033 If given an argument that does not start with @samp{+}, @command{date} sets
11034 the system clock to the time and date specified by that argument (as
11035 described below). You must have appropriate privileges to set the
11036 system clock. The @option{--date} and @option{--set} options may not be
11037 used with such an argument. The @option{--universal} option may be used
11038 with such an argument to indicate that the specified time and date are
11039 relative to Coordinated Universal Time rather than to the local time
11042 The argument must consist entirely of digits, which have the following
11055 first two digits of year (optional)
11057 last two digits of year (optional)
11062 The @option{--set} option also sets the system clock; see the next section.
11065 @node Options for date
11066 @subsection Options for @command{date}
11068 @cindex @command{date} options
11069 @cindex options for @command{date}
11071 The program accepts the following options. Also see @ref{Common options}.
11075 @item -d @var{datestr}
11076 @itemx --date=@var{datestr}
11079 @cindex parsing date strings
11080 @cindex date strings, parsing
11081 @cindex arbitrary date strings, parsing
11084 @opindex next @var{day}
11085 @opindex last @var{day}
11086 Display the time and date specified in @var{datestr} instead of the
11087 current time and date. @var{datestr} can be in almost any common
11088 format. It can contain month names, time zones, @samp{am} and @samp{pm},
11089 @samp{yesterday}, @samp{ago}, @samp{next}, etc. @xref{Date input formats}.
11091 @item -f @var{datefile}
11092 @itemx --file=@var{datefile}
11095 Parse each line in @var{datefile} as with @option{-d} and display the
11096 resulting time and date. If @var{datefile} is @samp{-}, use standard
11097 input. This is useful when you have many dates to process, because the
11098 system overhead of starting up the @command{date} executable many times can
11101 @item -I @var{timespec}
11102 @itemx --iso-8601[=@var{timespec}]
11103 @opindex -I @var{timespec}
11104 @opindex --iso-8601[=@var{timespec}]
11105 Display the date using the @acronym{ISO} 8601 format, @samp{%Y-%m-%d}.
11107 The argument @var{timespec} specifies the number of additional
11108 terms of the time to include. It can be one of the following:
11111 The default behavior: print just the date.
11114 Append the hour of the day to the date.
11117 Append the hours and minutes.
11120 Append the hours, minutes, and seconds.
11123 If showing any time terms, then include the time zone using the format
11126 If @var{timespec} is omitted with @option{--iso-8601}, the default is
11127 @samp{auto}. On older systems, @sc{gnu} @command{date} instead
11128 supports an obsolete option @option{-I[@var{timespec}]}, where
11129 @var{timespec} defaults to @samp{auto}. @acronym{POSIX} 1003.1-2001
11130 (@pxref{Standards conformance}) does not allow @option{-I} without an
11131 argument; use @option{--iso-8601} instead.
11138 @opindex --rfc-2822
11139 Display the time and date using the format @samp{%a, %d %b %Y %H:%M:%S
11140 %z}, evaluated in the C locale so abbreviations are always in English.
11144 Fri,@ @ 1 Aug 2003 23:05:56 -0700
11147 This format conforms to
11148 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc2822.txt, RFC 2822} and
11149 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc822.txt, RFC 822}, the
11150 current and previous standards for Internet email.
11152 @item -r @var{file}
11153 @itemx --reference=@var{file}
11155 @opindex --reference
11156 Display the time and date reference according to the last modification
11157 time of @var{file}, instead of the current time and date.
11159 @item -s @var{datestr}
11160 @itemx --set=@var{datestr}
11163 Set the time and date to @var{datestr}. See @option{-d} above.
11170 @opindex --universal
11171 @cindex Coordinated Universal Time
11173 @cindex Greenwich Mean Time
11175 Use Coordinated Universal Time (@acronym{UTC}) by operating as if the
11176 @env{TZ} environment variable were set to the string @samp{UTC0}.
11177 Normally, @command{date} operates in the time zone indicated by
11178 @env{TZ}, or the system default if @env{TZ} is not set. Coordinated
11179 Universal Time is often called ``Greenwich Mean Time'' (@sc{gmt}) for
11180 historical reasons.
11184 @node Examples of date
11185 @subsection Examples of @command{date}
11187 @cindex examples of @command{date}
11189 Here are a few examples. Also see the documentation for the @option{-d}
11190 option in the previous section.
11195 To print the date of the day before yesterday:
11198 date --date='2 days ago'
11202 To print the date of the day three months and one day hence:
11204 date --date='3 months 1 day'
11208 To print the day of year of Christmas in the current year:
11210 date --date='25 Dec' +%j
11214 To print the current full month name and the day of the month:
11219 But this may not be what you want because for the first nine days of
11220 the month, the @samp{%d} expands to a zero-padded two-digit field,
11221 for example @samp{date -d 1may '+%B %d'} will print @samp{May 01}.
11224 To print a date without the leading zero for one-digit days
11225 of the month, you can use the (GNU extension) @code{-} modifier to suppress
11226 the padding altogether.
11228 date -d 1may '+%B %-d
11232 To print the current date and time in the format required by many
11233 non-GNU versions of @command{date} when setting the system clock:
11235 date +%m%d%H%M%Y.%S
11239 To set the system clock forward by two minutes:
11241 date --set='+2 minutes'
11245 To print the date in the format specified by RFC-2822,
11246 use @samp{date --rfc-2822}. I just did and saw this:
11249 Thu, 31 Jul 2003 13:13:05 -0700
11252 @anchor{%s-examples}
11254 To convert a date string to the number of seconds since the epoch
11255 (which is 1970-01-01 00:00:00 UTC), use the @option{--date} option with
11256 the @samp{%s} format. That can be useful in sorting and/or graphing
11257 and/or comparing data by date. The following command outputs the
11258 number of the seconds since the epoch for the time two minutes after the
11262 date --date='1970-01-01 00:02:00 +0000' +%s
11266 If you do not specify time zone information in the date string,
11267 @command{date} uses your computer's idea of the time zone when
11268 interpreting the string. For example, if your computer's time zone is
11269 that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
11270 seconds) behind UTC:
11273 # local time zone used
11274 date --date='1970-01-01 00:02:00' +%s
11279 If you're sorting or graphing dated data, your raw date values may be
11280 represented as seconds since the epoch. But few people can look at
11281 the date @samp{946684800} and casually note ``Oh, that's the first second
11282 of the year 2000 in Greenwich, England.''
11285 date --date='2000-01-01 UTC' +%s
11289 An alternative is to use the @option{--utc} (@option{-u}) option.
11290 Then you may omit @samp{UTC} from the date string. Although this
11291 produces the same result for @samp{%s} and many other format sequences,
11292 with a time zone offset different from zero, it would give a different
11293 result for zone-dependent formats like @samp{%z}.
11296 date -u --date=2000-01-01 +%s
11300 To convert such an unwieldy number of seconds back to
11301 a more readable form, use a command like this:
11304 # local time zone used
11305 date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
11306 1999-12-31 19:00:00 -0500
11309 Often it is better to output UTC-relative date and time:
11312 date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
11313 2000-01-01 00:00:00 +0000
11319 @node uname invocation
11320 @section @command{uname}: Print system information
11323 @cindex print system information
11324 @cindex system information, printing
11326 @command{uname} prints information about the machine and operating system
11327 it is run on. If no options are given, @command{uname} acts as if the
11328 @option{-s} option were given. Synopsis:
11331 uname [@var{option}]@dots{}
11334 If multiple options or @option{-a} are given, the selected information is
11335 printed in this order:
11338 @var{kernel-name} @var{nodename} @var{kernel-release} @var{kernel-version} @var{machine} @var{processor} @var{hardware-platform} @var{operating-system}
11341 The information may contain internal spaces, so such output cannot be
11342 parsed reliably. In the following example, @var{release} is
11343 @samp{2.2.18ss.e820-bda652a #4 SMP Tue Jun 5 11:24:08 PDT 2001}:
11347 @result{} Linux dum 2.2.18ss.e820-bda652a #4 SMP Tue Jun 5 11:24:08 PDT 2001 i686 unknown unknown GNU/Linux
11351 The program accepts the following options. Also see @ref{Common options}.
11359 Print all of the below information.
11362 @itemx --hardware-platform
11364 @opindex --hardware-platform
11365 @cindex implementation, hardware
11366 @cindex hardware platform
11367 @cindex platform, hardware
11368 Print the hardware platform name
11369 (sometimes called the hardware implementation).
11375 @cindex machine type
11376 @cindex hardware class
11377 @cindex hardware type
11378 Print the machine hardware name (sometimes called the hardware class
11384 @opindex --nodename
11387 @cindex network node name
11388 Print the network node hostname.
11393 @opindex --processor
11394 @cindex host processor type
11395 Print the processor type (sometimes called the instruction set
11396 architecture or ISA).
11399 @itemx --operating-system
11401 @opindex --operating-system
11402 @cindex operating system name
11403 Print the name of the operating system.
11406 @itemx --kernel-release
11408 @opindex --kernel-release
11409 @cindex kernel release
11410 @cindex release of kernel
11411 Print the kernel release.
11414 @itemx --kernel-name
11416 @opindex --kernel-name
11417 @cindex kernel name
11418 @cindex name of kernel
11419 Print the kernel name.
11420 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) calls this
11421 ``the implementation of the operating system'', because the
11422 @acronym{POSIX} specification itself has no notion of ``kernel''.
11423 The kernel name might be the same as the operating system name printed
11424 by the @option{-o} or @option{--operating-system} option, but it might
11425 differ. Some operating systems (e.g., FreeBSD, HP-UX) have the same
11426 name as their underlying kernels; others (e.g., GNU/Linux, Solaris)
11430 @itemx --kernel-version
11432 @opindex --kernel-version
11433 @cindex kernel version
11434 @cindex version of kernel
11435 Print the kernel version.
11442 @node hostname invocation
11443 @section @command{hostname}: Print or set system name
11446 @cindex setting the hostname
11447 @cindex printing the hostname
11448 @cindex system name, printing
11449 @cindex appropriate privileges
11451 With no arguments, @command{hostname} prints the name of the current host
11452 system. With one argument, it sets the current host name to the
11453 specified string. You must have appropriate privileges to set the host
11457 hostname [@var{name}]
11460 The only options are @option{--help} and @option{--version}. @xref{Common
11466 @node hostid invocation
11467 @section @command{hostid}: Print numeric host identifier.
11470 @cindex printing the host identifier
11472 @command{hostid} prints the numeric identifier of the current host
11473 in hexadecimal. This command accepts no arguments.
11474 The only options are @option{--help} and @option{--version}.
11475 @xref{Common options}.
11477 For example, here's what it prints on one system I use:
11484 On that system, the 32-bit quantity happens to be closely
11485 related to the system's Internet address, but that isn't always
11491 @node Modified command invocation
11492 @chapter Modified command invocation
11494 @cindex modified command invocation
11495 @cindex invocation of commands, modified
11496 @cindex commands for invoking other commands
11498 This section describes commands that run other commands in some context
11499 different than the current one: a modified environment, as a different
11503 * chroot invocation:: Modify the root directory.
11504 * env invocation:: Modify environment variables.
11505 * nice invocation:: Modify scheduling priority.
11506 * nohup invocation:: Immunize to hangups.
11507 * su invocation:: Modify user and group id.
11511 @node chroot invocation
11512 @section @command{chroot}: Run a command with a different root directory
11515 @cindex running a program in a specified root directory
11516 @cindex root directory, running a program in a specified
11518 @command{chroot} runs a command with a specified root directory.
11519 On many systems, only the super-user can do this.
11523 chroot @var{newroot} [@var{command} [@var{args}]@dots{}]
11524 chroot @var{option}
11527 Ordinarily, filenames are looked up starting at the root of the
11528 directory structure, i.e., @file{/}. @command{chroot} changes the root to
11529 the directory @var{newroot} (which must exist) and then runs
11530 @var{command} with optional @var{args}. If @var{command} is not
11531 specified, the default is the value of the @env{SHELL} environment
11532 variable or @command{/bin/sh} if not set, invoked with the @option{-i} option.
11534 The only options are @option{--help} and @option{--version}. @xref{Common
11537 Here are a few tips to help avoid common problems in using chroot.
11538 To start with a simple example, make @var{command} refer to a statically
11539 linked binary. If you were to use a dynamically linked executable, then
11540 you'd have to arrange to have the shared libraries in the right place under
11541 your new root directory.
11543 For example, if you create a statically linked `ls' executable,
11544 and put it in /tmp/empty, you can run this command as root:
11547 $ chroot /tmp/empty /ls -Rl /
11550 Then you'll see output like this:
11555 -rwxr-xr-x 1 0 0 1041745 Aug 16 11:17 ls
11558 If you want to use a dynamically linked executable, say @command{bash},
11559 then first run @samp{ldd bash} to see what shared objects it needs.
11560 Then, in addition to copying the actual binary, also copy the listed
11561 files to the required positions under your intended new root directory.
11562 Finally, if the executable requires any other files (e.g., data, state,
11563 device files), copy them into place, too.
11565 @cindex exit status of @command{chroot}
11569 1 if @command{chroot} itself fails
11570 126 if @var{command} is found but cannot be invoked
11571 127 if @var{command} cannot be found
11572 the exit status of @var{command} otherwise
11576 @node env invocation
11577 @section @command{env}: Run a command in a modified environment
11580 @cindex environment, running a program in a modified
11581 @cindex modified environment, running a program in a
11582 @cindex running a program in a modified environment
11584 @command{env} runs a command with a modified environment. Synopses:
11587 env [@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c
11588 [@var{command} [@var{args}]@dots{}]
11592 Arguments of the form @samp{@var{variable}=@var{value}} set
11593 the environment variable @var{variable} to value @var{value}.
11594 @var{value} may be empty (@samp{@var{variable}=}). Setting a variable
11595 to an empty value is different from unsetting it.
11598 The first remaining argument specifies the program name to invoke; it is
11599 searched for according to the @env{PATH} environment variable. Any
11600 remaining arguments are passed as arguments to that program.
11602 @cindex environment, printing
11604 If no command name is specified following the environment
11605 specifications, the resulting environment is printed. This is like
11606 specifying a command name of @command{printenv}.
11608 The program accepts the following options. Also see @ref{Common options}.
11612 @item -u @var{name}
11613 @itemx --unset=@var{name}
11616 Remove variable @var{name} from the environment, if it was in the
11621 @itemx --ignore-environment
11624 @opindex --ignore-environment
11625 Start with an empty environment, ignoring the inherited environment.
11629 @cindex exit status of @command{env}
11633 0 if no @var{command} is specified and the environment is output
11634 1 if @command{env} itself fails
11635 126 if @var{command} is found but cannot be invoked
11636 127 if @var{command} cannot be found
11637 the exit status of @var{command} otherwise
11641 @node nice invocation
11642 @section @command{nice}: Run a command with modified scheduling priority
11645 @cindex modifying scheduling priority
11646 @cindex scheduling priority, modifying
11647 @cindex priority, modifying
11648 @cindex appropriate privileges
11650 @command{nice} prints or modifies the scheduling priority of a job.
11654 nice [@var{option}]@dots{} [@var{command} [@var{arg}]@dots{}]
11657 If no arguments are given, @command{nice} prints the current scheduling
11658 priority, which it inherited. Otherwise, @command{nice} runs the given
11659 @var{command} with its scheduling priority adjusted. If no
11660 @var{adjustment} is given, the priority of the command is incremented by
11661 10. You must have appropriate privileges to specify a negative
11662 adjustment. The priority can be adjusted by @command{nice} over the range
11663 of -20 (the highest priority) to 19 (the lowest).
11665 @cindex conflicts with shell built-ins
11666 @cindex built-in shell commands, conflicts with
11667 Because most shells have a built-in command by the same name, using the
11668 unadorned command name in a script or interactively may get you
11669 different functionality than that described here.
11671 The program accepts the following option. Also see @ref{Common options}.
11674 @item -n @var{adjustment}
11675 @itemx --adjustment=@var{adjustment}
11677 @opindex --adjustment
11678 Add @var{adjustment} instead of 10 to the command's priority.
11680 On older systems, @command{nice} supports an obsolete option
11681 @option{-@var{adjustment}}. @acronym{POSIX} 1003.1-2001 (@pxref{Standards
11682 conformance}) does not allow this; use @option{-n @var{adjustment}}
11687 @cindex exit status of @command{nice}
11691 0 if no @var{command} is specified and the current priority is output
11692 1 if @command{nice} itself fails
11693 126 if @var{command} is found but cannot be invoked
11694 127 if @var{command} cannot be found
11695 the exit status of @var{command} otherwise
11698 It is sometimes useful to run non-interactive programs with reduced priority.
11701 $ nice factor `echo '2^997 - 1'|bc`
11704 Since @command{nice} prints the current priority,
11705 we can invoke it through itself to demonstrate how it works:
11707 The default behavior is to reduce priority by @samp{10}.
11719 The @var{adjustment} is relative to the current priority.
11720 Here, the first @command{nice} invocation runs the second one at priority
11721 @samp{10}, and it in turn runs the final one at a priority lowered by
11725 $ nice nice -n 3 nice
11729 Specifying a priority larger than @samp{19} is the same as specifying @samp{19}.
11736 Only a privileged user may run a process with higher priority.
11740 nice: cannot set priority: Permission denied
11741 $ sudo nice -n -1 nice
11746 @node nohup invocation
11747 @section @command{nohup}: Run a command immune to hangups
11750 @cindex hangups, immunity to
11751 @cindex immunity to hangups
11752 @cindex logging out and continuing to run
11755 @command{nohup} runs the given @var{command} with hangup signals ignored,
11756 so that the command can continue running in the background after you log
11760 nohup @var{command} [@var{arg}]@dots{}
11764 If standard output is a terminal, it is redirected so that it is appended
11765 to the file @file{nohup.out}; if that cannot be written to, it is appended
11766 to the file @file{$HOME/nohup.out}. If that cannot be written to, the
11767 command is not run.
11768 If standard output is not a terminal, then the standard output of
11769 @var{command} will be the same as that of @command{nohup}.
11771 If @command{nohup} creates either @file{nohup.out} or
11772 @file{$HOME/nohup.out}, it creates it with no ``group'' or ``other''
11773 access permissions. It does not change the permissions if the output
11774 file already existed.
11776 If standard error is a terminal, it is redirected to the same file
11777 descriptor as the standard output.
11779 @command{nohup} does not automatically put the command it runs in the
11780 background; you must do that explicitly, by ending the command line
11781 with an @samp{&}. Also, @command{nohup} does not change the
11782 scheduling priority of @var{command}; use @command{nice} for that,
11783 e.g., @samp{nohup nice @var{command}}.
11785 The only options are @option{--help} and @option{--version}. @xref{Common
11788 @cindex exit status of @command{nohup}
11792 126 if @var{command} is found but cannot be invoked
11793 127 if @command{nohup} itself fails or if @var{command} cannot be found
11794 the exit status of @var{command} otherwise
11798 @node su invocation
11799 @section @command{su}: Run a command with substitute user and group id
11802 @cindex substitute user and group ids
11803 @cindex user id, switching
11804 @cindex super-user, becoming
11805 @cindex root, becoming
11807 @command{su} allows one user to temporarily become another user. It runs a
11808 command (often an interactive shell) with the real and effective user
11809 id, group id, and supplemental groups of a given @var{user}. Synopsis:
11812 su [@var{option}]@dots{} [@var{user} [@var{arg}]@dots{}]
11815 @cindex passwd entry, and @command{su} shell
11817 @flindex /etc/passwd
11818 If no @var{user} is given, the default is @code{root}, the super-user.
11819 The shell to use is taken from @var{user}'s @code{passwd} entry, or
11820 @file{/bin/sh} if none is specified there. If @var{user} has a
11821 password, @command{su} prompts for the password unless run by a user with
11822 effective user id of zero (the super-user).
11828 @cindex login shell
11829 By default, @command{su} does not change the current directory.
11830 It sets the environment variables @env{HOME} and @env{SHELL}
11831 from the password entry for @var{user}, and if @var{user} is not
11832 the super-user, sets @env{USER} and @env{LOGNAME} to @var{user}.
11833 By default, the shell is not a login shell.
11835 Any additional @var{arg}s are passed as additional arguments to the
11838 @cindex @option{-su}
11839 GNU @command{su} does not treat @file{/bin/sh} or any other shells specially
11840 (e.g., by setting @code{argv[0]} to @option{-su}, passing @option{-c} only
11841 to certain shells, etc.).
11844 @command{su} can optionally be compiled to use @code{syslog} to report
11845 failed, and optionally successful, @command{su} attempts. (If the system
11846 supports @code{syslog}.) However, GNU @command{su} does not check if the
11847 user is a member of the @code{wheel} group; see below.
11849 The program accepts the following options. Also see @ref{Common options}.
11852 @item -c @var{command}
11853 @itemx --command=@var{command}
11856 Pass @var{command}, a single command line to run, to the shell with
11857 a @option{-c} option instead of starting an interactive shell.
11864 @cindex file name pattern expansion, disabled
11865 @cindex globbing, disabled
11866 Pass the @option{-f} option to the shell. This probably only makes sense
11867 if the shell run is @command{csh} or @command{tcsh}, for which the @option{-f}
11868 option prevents reading the startup file (@file{.cshrc}). With
11869 Bourne-like shells, the @option{-f} option disables file name pattern
11870 expansion (globbing), which is not likely to be useful.
11878 @c other variables already indexed above
11881 @cindex login shell, creating
11882 Make the shell a login shell. This means the following. Unset all
11883 environment variables except @env{TERM}, @env{HOME}, and @env{SHELL}
11884 (which are set as described above), and @env{USER} and @env{LOGNAME}
11885 (which are set, even for the super-user, as described above), and set
11886 @env{PATH} to a compiled-in default value. Change to @var{user}'s home
11887 directory. Prepend @samp{-} to the shell's name, intended to make it
11888 read its login startup file(s).
11892 @itemx --preserve-environment
11895 @opindex --preserve-environment
11896 @cindex environment, preserving
11897 @flindex /etc/shells
11898 @cindex restricted shell
11899 Do not change the environment variables @env{HOME}, @env{USER},
11900 @env{LOGNAME}, or @env{SHELL}. Run the shell given in the environment
11901 variable @env{SHELL} instead of the shell from @var{user}'s passwd
11902 entry, unless the user running @command{su} is not the superuser and
11903 @var{user}'s shell is restricted. A @dfn{restricted shell} is one that
11904 is not listed in the file @file{/etc/shells}, or in a compiled-in list
11905 if that file does not exist. Parts of what this option does can be
11906 overridden by @option{--login} and @option{--shell}.
11908 @item -s @var{shell}
11909 @itemx --shell=@var{shell}
11912 Run @var{shell} instead of the shell from @var{user}'s passwd entry,
11913 unless the user running @command{su} is not the superuser and @var{user}'s
11914 shell is restricted (see @option{-m} just above).
11918 @cindex exit status of @command{su}
11922 1 if @command{su} itself fails
11923 126 if subshell is found but cannot be invoked
11924 127 if subshell cannot be found
11925 the exit status of the subshell otherwise
11928 @cindex wheel group, not supported
11929 @cindex group wheel, not supported
11931 @subsection Why GNU @command{su} does not support the @samp{wheel} group
11933 (This section is by Richard Stallman.)
11937 Sometimes a few of the users try to hold total power over all the
11938 rest. For example, in 1984, a few users at the MIT AI lab decided to
11939 seize power by changing the operator password on the Twenex system and
11940 keeping it secret from everyone else. (I was able to thwart this coup
11941 and give power back to the users by patching the kernel, but I
11942 wouldn't know how to do that in Unix.)
11944 However, occasionally the rulers do tell someone. Under the usual
11945 @command{su} mechanism, once someone learns the root password who
11946 sympathizes with the ordinary users, he or she can tell the rest. The
11947 ``wheel group'' feature would make this impossible, and thus cement the
11948 power of the rulers.
11950 I'm on the side of the masses, not that of the rulers. If you are
11951 used to supporting the bosses and sysadmins in whatever they do, you
11952 might find this idea strange at first.
11955 @node Process control
11956 @chapter Process control
11958 @cindex processes, commands for controlling
11959 @cindex commands for controlling processes
11962 * kill invocation:: Sending a signal to processes.
11966 @node kill invocation
11967 @section @command{kill}: Send a signal to processes
11970 @cindex send a signal to processes
11972 The @command{kill} command sends a signal to processes, causing them
11973 to terminate or otherwise act upon receiving the signal in some way.
11974 Alternatively, it lists information about signals. Synopses:
11977 kill [-s @var{signal} | --signal @var{signal} | -@var{signal}] @var{pid}@dots{}
11978 kill [-l | --list | -t | --table] [@var{signal}]@dots{}
11981 The first form of the @command{kill} command sends a signal to all
11982 @var{pid} arguments. The default signal to send if none is specified
11983 is @samp{TERM}. The special signal number @samp{0} does not denote a
11984 valid signal, but can be used to test whether the @var{pid} arguments
11985 specify processes to which a signal could be sent.
11987 If @var{pid} is positive, the signal is sent to the process with the
11988 process id @var{pid}. If @var{pid} is zero, the signal is sent to all
11989 processes in the process group of the current process. If @var{pid}
11990 is -1, the signal is sent to all processes for which the user has
11991 permission to send a signal. If @var{pid} is less than -1, the signal
11992 is sent to all processes in the process group that equals the absolute
11993 value of @var{pid}.
11995 If @var{pid} is not positive, a system-dependent set of system
11996 processes is excluded from the list of processes to which the signal
11999 If a negative @var{PID} argument is desired as the first one, either a
12000 signal must be specified as well, or the option parsing
12001 must be interrupted with `--' before the first @var{pid} argument.
12002 The following three commands are equivalent:
12010 The first form of the @command{kill} command succeeds if every @var{pid}
12011 argument specifies at least one process that the signal was sent to.
12013 The second form of the @command{kill} command lists signal information.
12014 Either the @option{-l} or @option{--list} option, or the @option{-t}
12015 or @option{--table} option must be specified. Without any
12016 @var{signal} argument, all supported signals are listed. The output
12017 of @option{-l} or @option{--list} is a list of the signal names, one
12018 per line; if @var{signal} is already a name, the signal number is
12019 printed instead. The output of @option{-t} or @option{--table} is a
12020 table of signal numbers, names, and descriptions. This form of the
12021 @command{kill} command succeeds if all @var{signal} arguments are valid
12022 and if there is no output error.
12024 The @command{kill} command also supports the @option{--help} and
12025 @option{--version} options. @xref{Common options}.
12027 A @var{signal} may be a signal name like @samp{HUP}, or a signal
12028 number like @samp{1}, or an exit status of a process terminated by the
12029 signal. A signal name can be given in canonical form or prefixed by
12030 @samp{SIG}. The case of the letters is ignored, except for the
12031 @option{-@var{signal}} option which must use upper case to avoid
12032 ambiguity with lower case option letters. The following signal names
12033 and numbers are supported on all @acronym{POSIX} compliant systems:
12039 2. Terminal interrupt.
12045 9. Kill (cannot be caught or ignored).
12053 Other supported signal names have system-dependent corresponding
12054 numbers. All systems conforming to @acronym{POSIX} 1003.1-2001 also
12055 support the following signals:
12059 Access to an undefined portion of a memory object.
12061 Child process terminated, stopped, or continued.
12063 Continue executing, if stopped.
12065 Erroneous arithmetic operation.
12067 Illegal Instruction.
12069 Write on a pipe with no one to read it.
12071 Invalid memory reference.
12073 Stop executing (cannot be caught or ignored).
12077 Background process attempting read.
12079 Background process attempting write.
12081 High bandwidth data is available at a socket.
12083 User-defined signal 1.
12085 User-defined signal 2.
12089 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XSI} extension
12090 also support the following signals:
12096 Profiling timer expired.
12100 Trace/breakpoint trap.
12102 Virtual timer expired.
12104 CPU time limit exceeded.
12106 File size limit exceeded.
12110 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XRT} extension
12111 also support at least eight real-time signals called @samp{RTMIN},
12112 @samp{RTMIN+1}, @dots{}, @samp{RTMAX-1}, @samp{RTMAX}.
12118 @cindex delaying commands
12119 @cindex commands for delaying
12121 @c Perhaps @command{wait} or other commands should be described here also?
12124 * sleep invocation:: Delay for a specified time.
12128 @node sleep invocation
12129 @section @command{sleep}: Delay for a specified time
12132 @cindex delay for a specified time
12134 @command{sleep} pauses for an amount of time specified by the sum of
12135 the values of the command line arguments.
12139 sleep @var{number}[smhd]@dots{}
12143 Each argument is a number followed by an optional unit; the default
12144 is seconds. The units are:
12157 Historical implementations of @command{sleep} have required that
12158 @var{number} be an integer. However, GNU @command{sleep} accepts
12159 arbitrary floating point numbers (using a period before any fractional
12162 The only options are @option{--help} and @option{--version}. @xref{Common
12168 @node Numeric operations
12169 @chapter Numeric operations
12171 @cindex numeric operations
12172 These programs do numerically-related operations.
12175 * factor invocation:: Show factors of numbers.
12176 * seq invocation:: Print sequences of numbers.
12180 @node factor invocation
12181 @section @command{factor}: Print prime factors
12184 @cindex prime factors
12186 @command{factor} prints prime factors. Synopses:
12189 factor [@var{number}]@dots{}
12190 factor @var{option}
12193 If no @var{number} is specified on the command line, @command{factor} reads
12194 numbers from standard input, delimited by newlines, tabs, or spaces.
12196 The only options are @option{--help} and @option{--version}. @xref{Common
12199 The algorithm it uses is not very sophisticated, so for some inputs
12200 @command{factor} runs for a long time. The hardest numbers to factor are
12201 the products of large primes. Factoring the product of the two largest 32-bit
12202 prime numbers takes about 80 seconds of CPU time on a 1.6 GHz Athlon.
12205 $ p=`echo '4294967279 * 4294967291'|bc`
12207 18446743979220271189: 4294967279 4294967291
12210 Similarly, it takes about 80 seconds for GNU factor (from coreutils-5.1.2)
12211 to ``factor'' the largest 64-bit prime:
12213 $ factor 18446744073709551557
12214 18446744073709551557: 18446744073709551557
12217 In contrast, @command{factor} factors the largest 64-bit number in just
12218 over a tenth of a second:
12221 $ factor `echo '2^64-1'|bc`
12222 18446744073709551615: 3 5 17 257 641 65537 6700417
12228 @node seq invocation
12229 @section @command{seq}: Print numeric sequences
12232 @cindex numeric sequences
12233 @cindex sequence of numbers
12235 @command{seq} prints a sequence of numbers to standard output. Synopses:
12238 seq [@var{option}]@dots{} @var{last}
12239 seq [@var{option}]@dots{} @var{first} @var{last}
12240 seq [@var{option}]@dots{} @var{first} @var{increment} @var{last}
12243 @command{seq} prints the numbers from @var{first} to @var{last} by
12244 @var{increment}. By default, each number is printed on a separate line.
12245 When @var{increment} is not specified, it defaults to @samp{1},
12246 even when @var{first} is larger than @var{last}.
12247 @var{first} also defaults to @samp{1}. So @code{seq 1} prints
12248 @samp{1}, but @code{seq 0} and @code{seq 10 5} produce no output.
12249 Floating-point numbers
12250 may be specified (using a period before any fractional digits).
12252 The program accepts the following options. Also see @ref{Common options}.
12255 @item -f @var{format}
12256 @itemx --format=@var{format}
12257 @opindex -f @var{format}
12258 @opindex --format=@var{format}
12259 @cindex formatting of numbers in @command{seq}
12260 Print all numbers using @var{format}; default @samp{%g}.
12261 @var{format} must contain exactly one of the floating point
12262 output formats @samp{%e}, @samp{%f}, or @samp{%g}.
12264 @item -s @var{string}
12265 @itemx --separator=@var{string}
12266 @cindex separator for numbers in @command{seq}
12267 Separate numbers with @var{string}; default is a newline.
12268 The output always terminates with a newline.
12271 @itemx --equal-width
12272 Print all numbers with the same width, by padding with leading zeroes.
12273 (To have other kinds of padding, use @option{--format}).
12277 If you want to use @command{seq} to print sequences of large integer values,
12278 don't use the default @samp{%g} format since it can result in
12282 $ seq 1000000 1000001
12287 Instead, you can use the format, @samp{%1.f},
12288 to print large decimal numbers with no exponent and no decimal point.
12291 $ seq --format=%1.f 1000000 1000001
12296 If you want hexadecimal output, you can use @command{printf}
12297 to perform the conversion:
12300 $ printf %x'\n' `seq -f %1.f 1048575 1024 1050623`
12306 For very long lists of numbers, use xargs to avoid
12307 system limitations on the length of an argument list:
12310 $ seq -f %1.f 1000000 | xargs printf %x'\n' | tail -n 3
12316 To generate octal output, use the printf @code{%o} format instead
12317 of @code{%x}. Note however that using printf works only for numbers
12318 smaller than @code{2^32}:
12321 $ printf "%x\n" `seq -f %1.f 4294967295 4294967296`
12323 bash: printf: 4294967296: Numerical result out of range
12326 On most systems, seq can produce whole-number output for values up to
12327 @code{2^53}, so here's a more general approach to base conversion that
12328 also happens to be more robust for such large numbers. It works by
12329 using @code{bc} and setting its output radix variable, @var{obase},
12330 to @samp{16} in this case to produce hexadecimal output.
12333 $ (echo obase=16; seq -f %1.f 4294967295 4294967296)|bc
12338 Be careful when using @command{seq} with a fractional @var{increment},
12339 otherwise you may see surprising results. Most people would expect to
12340 see @code{0.3} printed as the last number in this example:
12343 $ seq -s' ' 0 .1 .3
12347 But that doesn't happen on most systems because @command{seq} is
12348 implemented using binary floating point arithmetic (via the C
12349 @code{double} type) -- which means some decimal numbers like @code{.1}
12350 cannot be represented exactly. That in turn means some nonintuitive
12351 conditions like @w{@code{.1 * 3 > .3}} will end up being true.
12353 To work around that in the above example, use a slightly larger number as
12354 the @var{last} value:
12357 $ seq -s' ' 0 .1 .31
12361 In general, when using an @var{increment} with a fractional part, where
12362 (@var{last} - @var{first}) / @var{increment} is (mathematically) a whole
12363 number, specify a slightly larger (or smaller, if @var{increment} is negative)
12364 value for @var{last} to ensure that @var{last} is the final value printed
12370 @node File permissions
12371 @chapter File permissions
12374 @include getdate.texi
12378 @node Opening the software toolbox
12379 @chapter Opening the Software Toolbox
12381 This chapter originally appeared in @cite{Linux Journal}, volume 1,
12382 number 2, in the @cite{What's GNU?} column. It was written by Arnold
12386 * Toolbox introduction:: Toolbox introduction
12387 * I/O redirection:: I/O redirection
12388 * The who command:: The @command{who} command
12389 * The cut command:: The @command{cut} command
12390 * The sort command:: The @command{sort} command
12391 * The uniq command:: The @command{uniq} command
12392 * Putting the tools together:: Putting the tools together
12396 @node Toolbox introduction
12397 @unnumberedsec Toolbox Introduction
12399 This month's column is only peripherally related to the GNU Project, in
12400 that it describes a number of the GNU tools on your GNU/Linux system and how they
12401 might be used. What it's really about is the ``Software Tools'' philosophy
12402 of program development and usage.
12404 The software tools philosophy was an important and integral concept
12405 in the initial design and development of Unix (of which Linux and GNU are
12406 essentially clones). Unfortunately, in the modern day press of
12407 Internetworking and flashy GUIs, it seems to have fallen by the
12408 wayside. This is a shame, since it provides a powerful mental model
12409 for solving many kinds of problems.
12411 Many people carry a Swiss Army knife around in their pants pockets (or
12412 purse). A Swiss Army knife is a handy tool to have: it has several knife
12413 blades, a screwdriver, tweezers, toothpick, nail file, corkscrew, and perhaps
12414 a number of other things on it. For the everyday, small miscellaneous jobs
12415 where you need a simple, general purpose tool, it's just the thing.
12417 On the other hand, an experienced carpenter doesn't build a house using
12418 a Swiss Army knife. Instead, he has a toolbox chock full of specialized
12419 tools---a saw, a hammer, a screwdriver, a plane, and so on. And he knows
12420 exactly when and where to use each tool; you won't catch him hammering nails
12421 with the handle of his screwdriver.
12423 The Unix developers at Bell Labs were all professional programmers and trained
12424 computer scientists. They had found that while a one-size-fits-all program
12425 might appeal to a user because there's only one program to use, in practice
12430 difficult to write,
12433 difficult to maintain and
12437 difficult to extend to meet new situations.
12440 Instead, they felt that programs should be specialized tools. In short, each
12441 program ``should do one thing well.'' No more and no less. Such programs are
12442 simpler to design, write, and get right---they only do one thing.
12444 Furthermore, they found that with the right machinery for hooking programs
12445 together, that the whole was greater than the sum of the parts. By combining
12446 several special purpose programs, you could accomplish a specific task
12447 that none of the programs was designed for, and accomplish it much more
12448 quickly and easily than if you had to write a special purpose program.
12449 We will see some (classic) examples of this further on in the column.
12450 (An important additional point was that, if necessary, take a detour
12451 and build any software tools you may need first, if you don't already
12452 have something appropriate in the toolbox.)
12454 @node I/O redirection
12455 @unnumberedsec I/O Redirection
12457 Hopefully, you are familiar with the basics of I/O redirection in the
12458 shell, in particular the concepts of ``standard input,'' ``standard output,''
12459 and ``standard error''. Briefly, ``standard input'' is a data source, where
12460 data comes from. A program should not need to either know or care if the
12461 data source is a disk file, a keyboard, a magnetic tape, or even a punched
12462 card reader. Similarly, ``standard output'' is a data sink, where data goes
12463 to. The program should neither know nor care where this might be.
12464 Programs that only read their standard input, do something to the data,
12465 and then send it on, are called @dfn{filters}, by analogy to filters in a
12468 With the Unix shell, it's very easy to set up data pipelines:
12471 program_to_create_data | filter1 | .... | filterN > final.pretty.data
12474 We start out by creating the raw data; each filter applies some successive
12475 transformation to the data, until by the time it comes out of the pipeline,
12476 it is in the desired form.
12478 This is fine and good for standard input and standard output. Where does the
12479 standard error come in to play? Well, think about @command{filter1} in
12480 the pipeline above. What happens if it encounters an error in the data it
12481 sees? If it writes an error message to standard output, it will just
12482 disappear down the pipeline into @command{filter2}'s input, and the
12483 user will probably never see it. So programs need a place where they can send
12484 error messages so that the user will notice them. This is standard error,
12485 and it is usually connected to your console or window, even if you have
12486 redirected standard output of your program away from your screen.
12488 For filter programs to work together, the format of the data has to be
12489 agreed upon. The most straightforward and easiest format to use is simply
12490 lines of text. Unix data files are generally just streams of bytes, with
12491 lines delimited by the @acronym{ASCII} @sc{lf} (Line Feed) character,
12492 conventionally called a ``newline'' in the Unix literature. (This is
12493 @code{'\n'} if you're a C programmer.) This is the format used by all
12494 the traditional filtering programs. (Many earlier operating systems
12495 had elaborate facilities and special purpose programs for managing
12496 binary data. Unix has always shied away from such things, under the
12497 philosophy that it's easiest to simply be able to view and edit your
12498 data with a text editor.)
12500 OK, enough introduction. Let's take a look at some of the tools, and then
12501 we'll see how to hook them together in interesting ways. In the following
12502 discussion, we will only present those command line options that interest
12503 us. As you should always do, double check your system documentation
12504 for the full story.
12506 @node The who command
12507 @unnumberedsec The @command{who} Command
12509 The first program is the @command{who} command. By itself, it generates a
12510 list of the users who are currently logged in. Although I'm writing
12511 this on a single-user system, we'll pretend that several people are
12516 @print{} arnold console Jan 22 19:57
12517 @print{} miriam ttyp0 Jan 23 14:19(:0.0)
12518 @print{} bill ttyp1 Jan 21 09:32(:0.0)
12519 @print{} arnold ttyp2 Jan 23 20:48(:0.0)
12522 Here, the @samp{$} is the usual shell prompt, at which I typed @samp{who}.
12523 There are three people logged in, and I am logged in twice. On traditional
12524 Unix systems, user names are never more than eight characters long. This
12525 little bit of trivia will be useful later. The output of @command{who} is nice,
12526 but the data is not all that exciting.
12528 @node The cut command
12529 @unnumberedsec The @command{cut} Command
12531 The next program we'll look at is the @command{cut} command. This program
12532 cuts out columns or fields of input data. For example, we can tell it
12533 to print just the login name and full name from the @file{/etc/passwd}
12534 file. The @file{/etc/passwd} file has seven fields, separated by
12538 arnold:xyzzy:2076:10:Arnold D. Robbins:/home/arnold:/bin/bash
12541 To get the first and fifth fields, we would use @command{cut} like this:
12544 $ cut -d: -f1,5 /etc/passwd
12545 @print{} root:Operator
12547 @print{} arnold:Arnold D. Robbins
12548 @print{} miriam:Miriam A. Robbins
12552 With the @option{-c} option, @command{cut} will cut out specific characters
12553 (i.e., columns) in the input lines. This is useful for input data
12554 that has fixed width fields, and does not have a field separator. For
12555 example, list the Monday dates for the current month:
12557 @c Is using cal ok? Looked at gcal, but I don't like it.
12568 @node The sort command
12569 @unnumberedsec The @command{sort} Command
12571 Next we'll look at the @command{sort} command. This is one of the most
12572 powerful commands on a Unix-style system; one that you will often find
12573 yourself using when setting up fancy data plumbing.
12576 command reads and sorts each file named on the command line. It then
12577 merges the sorted data and writes it to standard output. It will read
12578 standard input if no files are given on the command line (thus
12579 making it into a filter). The sort is based on the character collating
12580 sequence or based on user-supplied ordering criteria.
12583 @node The uniq command
12584 @unnumberedsec The @command{uniq} Command
12586 Finally (at least for now), we'll look at the @command{uniq} program. When
12587 sorting data, you will often end up with duplicate lines, lines that
12588 are identical. Usually, all you need is one instance of each line.
12589 This is where @command{uniq} comes in. The @command{uniq} program reads its
12590 standard input. It prints only one
12591 copy of each repeated line. It does have several options. Later on,
12592 we'll use the @option{-c} option, which prints each unique line, preceded
12593 by a count of the number of times that line occurred in the input.
12596 @node Putting the tools together
12597 @unnumberedsec Putting the Tools Together
12599 Now, let's suppose this is a large ISP server system with dozens of users
12600 logged in. The management wants the system administrator to write a program that will
12601 generate a sorted list of logged in users. Furthermore, even if a user
12602 is logged in multiple times, his or her name should only show up in the
12605 The administrator could sit down with the system documentation and write a C
12606 program that did this. It would take perhaps a couple of hundred lines
12607 of code and about two hours to write it, test it, and debug it.
12608 However, knowing the software toolbox, the administrator can instead start out
12609 by generating just a list of logged on users:
12619 Next, sort the list:
12622 $ who | cut -c1-8 | sort
12629 Finally, run the sorted list through @command{uniq}, to weed out duplicates:
12632 $ who | cut -c1-8 | sort | uniq
12638 The @command{sort} command actually has a @option{-u} option that does what
12639 @command{uniq} does. However, @command{uniq} has other uses for which one
12640 cannot substitute @samp{sort -u}.
12642 The administrator puts this pipeline into a shell script, and makes it available for
12643 all the users on the system (@samp{#} is the system administrator,
12644 or @code{root}, prompt):
12647 # cat > /usr/local/bin/listusers
12648 who | cut -c1-8 | sort | uniq
12650 # chmod +x /usr/local/bin/listusers
12653 There are four major points to note here. First, with just four
12654 programs, on one command line, the administrator was able to save about two
12655 hours worth of work. Furthermore, the shell pipeline is just about as
12656 efficient as the C program would be, and it is much more efficient in
12657 terms of programmer time. People time is much more expensive than
12658 computer time, and in our modern ``there's never enough time to do
12659 everything'' society, saving two hours of programmer time is no mean
12662 Second, it is also important to emphasize that with the
12663 @emph{combination} of the tools, it is possible to do a special
12664 purpose job never imagined by the authors of the individual programs.
12666 Third, it is also valuable to build up your pipeline in stages, as we did here.
12667 This allows you to view the data at each stage in the pipeline, which helps
12668 you acquire the confidence that you are indeed using these tools correctly.
12670 Finally, by bundling the pipeline in a shell script, other users can use
12671 your command, without having to remember the fancy plumbing you set up for
12672 them. In terms of how you run them, shell scripts and compiled programs are
12675 After the previous warm-up exercise, we'll look at two additional, more
12676 complicated pipelines. For them, we need to introduce two more tools.
12678 The first is the @command{tr} command, which stands for ``transliterate.''
12679 The @command{tr} command works on a character-by-character basis, changing
12680 characters. Normally it is used for things like mapping upper case to
12684 $ echo ThIs ExAmPlE HaS MIXED case! | tr '[A-Z]' '[a-z]'
12685 @print{} this example has mixed case!
12688 There are several options of interest:
12692 work on the complement of the listed characters, i.e.,
12693 operations apply to characters not in the given set
12696 delete characters in the first set from the output
12699 squeeze repeated characters in the output into just one character.
12702 We will be using all three options in a moment.
12704 The other command we'll look at is @command{comm}. The @command{comm}
12705 command takes two sorted input files as input data, and prints out the
12706 files' lines in three columns. The output columns are the data lines
12707 unique to the first file, the data lines unique to the second file, and
12708 the data lines that are common to both. The @option{-1}, @option{-2}, and
12709 @option{-3} command line options @emph{omit} the respective columns. (This is
12710 non-intuitive and takes a little getting used to.) For example:
12732 The single dash as a filename tells @command{comm} to read standard input
12733 instead of a regular file.
12735 Now we're ready to build a fancy pipeline. The first application is a word
12736 frequency counter. This helps an author determine if he or she is over-using
12739 The first step is to change the case of all the letters in our input file
12740 to one case. ``The'' and ``the'' are the same word when doing counting.
12743 $ tr '[A-Z]' '[a-z]' < whats.gnu | ...
12746 The next step is to get rid of punctuation. Quoted words and unquoted words
12747 should be treated identically; it's easiest to just get the punctuation out of
12751 $ tr '[A-Z]' '[a-z]' < whats.gnu | tr -cd '[A-Za-z0-9_ \012]' | ...
12754 The second @command{tr} command operates on the complement of the listed
12755 characters, which are all the letters, the digits, the underscore, and
12756 the blank. The @samp{\012} represents the newline character; it has to
12757 be left alone. (The @acronym{ASCII} tab character should also be included for
12758 good measure in a production script.)
12760 At this point, we have data consisting of words separated by blank space.
12761 The words only contain alphanumeric characters (and the underscore). The
12762 next step is break the data apart so that we have one word per line. This
12763 makes the counting operation much easier, as we will see shortly.
12766 $ tr '[A-Z]' '[a-z]' < whats.gnu | tr -cd '[A-Za-z0-9_ \012]' |
12767 > tr -s '[ ]' '\012' | ...
12770 This command turns blanks into newlines. The @option{-s} option squeezes
12771 multiple newline characters in the output into just one. This helps us
12772 avoid blank lines. (The @samp{>} is the shell's ``secondary prompt.''
12773 This is what the shell prints when it notices you haven't finished
12774 typing in all of a command.)
12776 We now have data consisting of one word per line, no punctuation, all one
12777 case. We're ready to count each word:
12780 $ tr '[A-Z]' '[a-z]' < whats.gnu | tr -cd '[A-Za-z0-9_ \012]' |
12781 > tr -s '[ ]' '\012' | sort | uniq -c | ...
12784 At this point, the data might look something like this:
12797 The output is sorted by word, not by count! What we want is the most
12798 frequently used words first. Fortunately, this is easy to accomplish,
12799 with the help of two more @command{sort} options:
12803 do a numeric sort, not a textual one
12806 reverse the order of the sort
12809 The final pipeline looks like this:
12812 $ tr '[A-Z]' '[a-z]' < whats.gnu | tr -cd '[A-Za-z0-9_ \012]' |
12813 > tr -s '[ ]' '\012' | sort | uniq -c | sort -nr
12822 Whew! That's a lot to digest. Yet, the same principles apply. With six
12823 commands, on two lines (really one long one split for convenience), we've
12824 created a program that does something interesting and useful, in much
12825 less time than we could have written a C program to do the same thing.
12827 A minor modification to the above pipeline can give us a simple spelling
12828 checker! To determine if you've spelled a word correctly, all you have to
12829 do is look it up in a dictionary. If it is not there, then chances are
12830 that your spelling is incorrect. So, we need a dictionary.
12831 The conventional location for a dictionary is @file{/usr/dict/words}.
12832 On my GNU/Linux system,@footnote{Redhat Linux 6.1, for the November 2000
12833 revision of this article.}
12834 this is a is a sorted, 45,402 word dictionary.
12836 Now, how to compare our file with the dictionary? As before, we generate
12837 a sorted list of words, one per line:
12840 $ tr '[A-Z]' '[a-z]' < whats.gnu | tr -cd '[A-Za-z0-9_ \012]' |
12841 > tr -s '[ ]' '\012' | sort -u | ...
12844 Now, all we need is a list of words that are @emph{not} in the
12845 dictionary. Here is where the @command{comm} command comes in.
12848 $ tr '[A-Z]' '[a-z]' < whats.gnu | tr -cd '[A-Za-z0-9_ \012]' |
12849 > tr -s '[ ]' '\012' | sort -u |
12850 > comm -23 - /usr/dict/words
12853 The @option{-2} and @option{-3} options eliminate lines that are only in the
12854 dictionary (the second file), and lines that are in both files. Lines
12855 only in the first file (standard input, our stream of words), are
12856 words that are not in the dictionary. These are likely candidates for
12857 spelling errors. This pipeline was the first cut at a production
12858 spelling checker on Unix.
12860 There are some other tools that deserve brief mention.
12864 search files for text that matches a regular expression
12867 count lines, words, characters
12870 a T-fitting for data pipes, copies data to files and to standard output
12873 the stream editor, an advanced tool
12876 a data manipulation language, another advanced tool
12879 The software tools philosophy also espoused the following bit of
12880 advice: ``Let someone else do the hard part.'' This means, take
12881 something that gives you most of what you need, and then massage it the
12882 rest of the way until it's in the form that you want.
12888 Each program should do one thing well. No more, no less.
12891 Combining programs with appropriate plumbing leads to results where
12892 the whole is greater than the sum of the parts. It also leads to novel
12893 uses of programs that the authors might never have imagined.
12896 Programs should never print extraneous header or trailer data, since these
12897 could get sent on down a pipeline. (A point we didn't mention earlier.)
12900 Let someone else do the hard part.
12903 Know your toolbox! Use each program appropriately. If you don't have an
12904 appropriate tool, build one.
12907 As of this writing, all the programs we've discussed are available via
12908 anonymous @command{ftp} from: @*
12909 @uref{ftp://gnudist.gnu.org/textutils/textutils-1.22.tar.gz}. (There may
12910 be more recent versions available now.)
12912 None of what I have presented in this column is new. The Software Tools
12913 philosophy was first introduced in the book @cite{Software Tools}, by
12914 Brian Kernighan and P.J. Plauger (Addison-Wesley, ISBN 0-201-03669-X).
12915 This book showed how to write and use software tools. It was written in
12916 1976, using a preprocessor for FORTRAN named @command{ratfor} (RATional
12917 FORtran). At the time, C was not as ubiquitous as it is now; FORTRAN
12918 was. The last chapter presented a @command{ratfor} to FORTRAN
12919 processor, written in @command{ratfor}. @command{ratfor} looks an awful
12920 lot like C; if you know C, you won't have any problem following the
12923 In 1981, the book was updated and made available as @cite{Software Tools
12924 in Pascal} (Addison-Wesley, ISBN 0-201-10342-7). The first book is
12925 still in print; the second, alas, is not. Both books are well worth
12926 reading if you're a programmer. They certainly made a major change in
12927 how I view programming.
12929 Initially, the programs in both books were available (on 9-track tape)
12930 from Addison-Wesley. Unfortunately, this is no longer the case,
12931 although the @command{ratfor} versions are available from
12932 @uref{http://cm.bell-labs.come/who/bwk, Brian Kernighan's home page},
12933 and you might be able to find copies of the Pascal versions floating
12934 around the Internet. For a number of years, there was an active
12935 Software Tools Users Group, whose members had ported the original
12936 @command{ratfor} programs to essentially every computer system with a
12937 FORTRAN compiler. The popularity of the group waned in the middle 1980s
12938 as Unix began to spread beyond universities.
12940 With the current proliferation of GNU code and other clones of Unix programs,
12941 these programs now receive little attention; modern C versions are
12942 much more efficient and do more than these programs do. Nevertheless, as
12943 exposition of good programming style, and evangelism for a still-valuable
12944 philosophy, these books are unparalleled, and I recommend them highly.
12946 Acknowledgment: I would like to express my gratitude to Brian Kernighan
12947 of Bell Labs, the original Software Toolsmith, for reviewing this column.
12949 @include doclicense.texi
12960 @c Local variables:
12961 @c texinfo-column-for-description: 32