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 @c FIXME: the following need documentation
32 @c * [: (coreutils)[ invocation. File/string tests.
33 @c * pinky: (coreutils)pinky invocation. FIXME.
34 @c * uptime: (coreutils)uptime invocation. FIXME.
36 @dircategory Individual utilities
38 * base64: (coreutils)base64 invocation. Base64 encode/decode data.
39 * basename: (coreutils)basename invocation. Strip directory and suffix.
40 * cat: (coreutils)cat invocation. Concatenate and write files.
41 * chgrp: (coreutils)chgrp invocation. Change file groups.
42 * chmod: (coreutils)chmod invocation. Change file permissions.
43 * chown: (coreutils)chown invocation. Change file owners/groups.
44 * chroot: (coreutils)chroot invocation. Specify the root directory.
45 * cksum: (coreutils)cksum invocation. Print POSIX CRC checksum.
46 * comm: (coreutils)comm invocation. Compare sorted files by line.
47 * cp: (coreutils)cp invocation. Copy files.
48 * csplit: (coreutils)csplit invocation. Split by context.
49 * cut: (coreutils)cut invocation. Print selected parts of lines.
50 * date: (coreutils)date invocation. Print/set system date and time.
51 * dd: (coreutils)dd invocation. Copy and convert a file.
52 * df: (coreutils)df invocation. Report file system disk usage.
53 * dir: (coreutils)dir invocation. List directories briefly.
54 * dircolors: (coreutils)dircolors invocation. Color setup for ls.
55 * dirname: (coreutils)dirname invocation. Strip non-directory suffix.
56 * du: (coreutils)du invocation. Report on disk usage.
57 * echo: (coreutils)echo invocation. Print a line of text.
58 * env: (coreutils)env invocation. Modify the environment.
59 * expand: (coreutils)expand invocation. Convert tabs to spaces.
60 * expr: (coreutils)expr invocation. Evaluate expressions.
61 * factor: (coreutils)factor invocation. Print prime factors
62 * false: (coreutils)false invocation. Do nothing, unsuccessfully.
63 * fmt: (coreutils)fmt invocation. Reformat paragraph text.
64 * fold: (coreutils)fold invocation. Wrap long input lines.
65 * groups: (coreutils)groups invocation. Print group names a user is in.
66 * head: (coreutils)head invocation. Output the first part of files.
67 * hostid: (coreutils)hostid invocation. Print numeric host identifier.
68 * hostname: (coreutils)hostname invocation. Print or set system name.
69 * id: (coreutils)id invocation. Print user identity.
70 * install: (coreutils)install invocation. Copy and change attributes.
71 * join: (coreutils)join invocation. Join lines on a common field.
72 * kill: (coreutils)kill invocation. Send a signal to processes.
73 * link: (coreutils)link invocation. Make hard links between files.
74 * ln: (coreutils)ln invocation. Make links between files.
75 * logname: (coreutils)logname invocation. Print current login name.
76 * ls: (coreutils)ls invocation. List directory contents.
77 * md5sum: (coreutils)md5sum invocation. Print or check MD5 digests.
78 * mkdir: (coreutils)mkdir invocation. Create directories.
79 * mkfifo: (coreutils)mkfifo invocation. Create FIFOs (named pipes).
80 * mknod: (coreutils)mknod invocation. Create special files.
81 * mv: (coreutils)mv invocation. Rename files.
82 * nice: (coreutils)nice invocation. Modify niceness.
83 * nl: (coreutils)nl invocation. Number lines and write files.
84 * nohup: (coreutils)nohup invocation. Immunize to hangups.
85 * od: (coreutils)od invocation. Dump files in octal, etc.
86 * paste: (coreutils)paste invocation. Merge lines of files.
87 * pathchk: (coreutils)pathchk invocation. Check file name portability.
88 * pr: (coreutils)pr invocation. Paginate or columnate files.
89 * printenv: (coreutils)printenv invocation. Print environment variables.
90 * printf: (coreutils)printf invocation. Format and print data.
91 * ptx: (coreutils)ptx invocation. Produce permuted indexes.
92 * pwd: (coreutils)pwd invocation. Print working directory.
93 * readlink: (coreutils)readlink invocation. Print referent of a symlink.
94 * rm: (coreutils)rm invocation. Remove files.
95 * rmdir: (coreutils)rmdir invocation. Remove empty directories.
96 * seq: (coreutils)seq invocation. Print numeric sequences
97 * sha1sum: (coreutils)sha1sum invocation. Print or check SHA-1 digests.
98 * sha2: (coreutils)sha2 utilities. Print or check SHA-2 digests.
99 * shred: (coreutils)shred invocation. Remove files more securely.
100 * sleep: (coreutils)sleep invocation. Delay for a specified time.
101 * sort: (coreutils)sort invocation. Sort text files.
102 * split: (coreutils)split invocation. Split into fixed-size pieces.
103 * stat: (coreutils)stat invocation. Report file(system) status.
104 * stty: (coreutils)stty invocation. Print/change terminal settings.
105 * su: (coreutils)su invocation. Modify user and group ID.
106 * sum: (coreutils)sum invocation. Print traditional checksum.
107 * sync: (coreutils)sync invocation. Synchronize memory and disk.
108 * tac: (coreutils)tac invocation. Reverse files.
109 * tail: (coreutils)tail invocation. Output the last part of files.
110 * tee: (coreutils)tee invocation. Redirect to multiple files.
111 * test: (coreutils)test invocation. File/string tests.
112 * touch: (coreutils)touch invocation. Change file timestamps.
113 * tr: (coreutils)tr invocation. Translate characters.
114 * true: (coreutils)true invocation. Do nothing, successfully.
115 * tsort: (coreutils)tsort invocation. Topological sort.
116 * tty: (coreutils)tty invocation. Print terminal name.
117 * uname: (coreutils)uname invocation. Print system information.
118 * unexpand: (coreutils)unexpand invocation. Convert spaces to tabs.
119 * uniq: (coreutils)uniq invocation. Uniquify files.
120 * unlink: (coreutils)unlink invocation. Removal via unlink(2).
121 * users: (coreutils)users invocation. Print current user names.
122 * vdir: (coreutils)vdir invocation. List directories verbosely.
123 * wc: (coreutils)wc invocation. Line, word, and byte counts.
124 * who: (coreutils)who invocation. Print who is logged in.
125 * whoami: (coreutils)whoami invocation. Print effective user ID.
126 * yes: (coreutils)yes invocation. Print a string indefinitely.
130 This manual documents version @value{VERSION} of the @sc{gnu} core
131 utilities, including the standard programs for text and file manipulation.
133 Copyright @copyright{} 1994, 1995, 1996, 2000, 2001, 2002, 2003, 2004,
134 2005, 2006 Free Software Foundation, Inc.
137 Permission is granted to copy, distribute and/or modify this document
138 under the terms of the GNU Free Documentation License, Version 1.1 or
139 any later version published by the Free Software Foundation; with no
140 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
141 Texts. A copy of the license is included in the section entitled ``GNU
142 Free Documentation License''.
147 @title @sc{gnu} @code{Coreutils}
148 @subtitle Core GNU utilities
149 @subtitle for version @value{VERSION}, @value{UPDATED}
150 @author David MacKenzie et al.
153 @vskip 0pt plus 1filll
165 @cindex core utilities
166 @cindex text utilities
167 @cindex shell utilities
168 @cindex file utilities
171 * Introduction:: Caveats, overview, and authors.
172 * Common options:: Common options.
173 * Output of entire files:: cat tac nl od
174 * Formatting file contents:: fmt pr fold
175 * Output of parts of files:: head tail split csplit
176 * Summarizing files:: wc sum cksum md5sum sha1sum sha2
177 * Operating on sorted files:: sort uniq comm ptx tsort
178 * Operating on fields within a line:: cut paste join
179 * Operating on characters:: tr expand unexpand
180 * Directory listing:: ls dir vdir dircolors
181 * Basic operations:: cp dd install mv rm shred
182 * Special file types:: ln mkdir rmdir mkfifo mknod
183 * Changing file attributes:: chgrp chmod chown touch
184 * Disk usage:: df du stat sync
185 * Printing text:: echo printf yes
186 * Conditions:: false true test expr
188 * File name manipulation:: dirname basename pathchk
189 * Working context:: pwd stty printenv tty
190 * User information:: id logname whoami groups users who
191 * System context:: date uname hostname hostid
192 * Modified command invocation:: chroot env nice nohup su
193 * Process control:: kill
195 * Numeric operations:: factor seq
196 * File permissions:: Access modes.
197 * Date input formats:: Specifying date strings.
198 * Opening the software toolbox:: The software tools philosophy.
199 * GNU Free Documentation License:: The license for this documentation.
200 * Index:: General index.
203 --- The Detailed Node Listing ---
207 * Exit status:: Indicating program success or failure.
208 * Backup options:: Backup options
209 * Block size:: Block size
210 * Target directory:: Target directory
211 * Trailing slashes:: Trailing slashes
212 * Traversing symlinks:: Traversing symlinks to directories
213 * Treating / specially:: Treating / specially
214 * Standards conformance:: Standards conformance
216 Output of entire files
218 * cat invocation:: Concatenate and write files.
219 * tac invocation:: Concatenate and write files in reverse.
220 * nl invocation:: Number lines and write files.
221 * od invocation:: Write files in octal or other formats.
222 * base64 invocation:: Transform data into printable data.
224 Formatting file contents
226 * fmt invocation:: Reformat paragraph text.
227 * pr invocation:: Paginate or columnate files for printing.
228 * fold invocation:: Wrap input lines to fit in specified width.
230 Output of parts of files
232 * head invocation:: Output the first part of files.
233 * tail invocation:: Output the last part of files.
234 * split invocation:: Split a file into fixed-size pieces.
235 * csplit invocation:: Split a file into context-determined pieces.
239 * wc invocation:: Print newline, word, and byte counts.
240 * sum invocation:: Print checksum and block counts.
241 * cksum invocation:: Print CRC checksum and byte counts.
242 * md5sum invocation:: Print or check MD5 digests.
243 * sha1sum invocation:: Print or check SHA-1 digests.
244 * sha2 utilities:: Print or check SHA-2 digests.
246 Operating on sorted files
248 * sort invocation:: Sort text files.
249 * uniq invocation:: Uniquify files.
250 * comm invocation:: Compare two sorted files line by line.
251 * ptx invocation:: Produce a permuted index of file contents.
252 * tsort invocation:: Topological sort.
254 @command{ptx}: Produce permuted indexes
256 * General options in ptx:: Options which affect general program behavior.
257 * Charset selection in ptx:: Underlying character set considerations.
258 * Input processing in ptx:: Input fields, contexts, and keyword selection.
259 * Output formatting in ptx:: Types of output format, and sizing the fields.
260 * Compatibility in ptx:: The @acronym{GNU} extensions to @command{ptx}
262 Operating on fields within a line
264 * cut invocation:: Print selected parts of lines.
265 * paste invocation:: Merge lines of files.
266 * join invocation:: Join lines on a common field.
268 Operating on characters
270 * tr invocation:: Translate, squeeze, and/or delete characters.
271 * expand invocation:: Convert tabs to spaces.
272 * unexpand invocation:: Convert spaces to tabs.
274 @command{tr}: Translate, squeeze, and/or delete characters
276 * Character sets:: Specifying sets of characters.
277 * Translating:: Changing one set of characters to another.
278 * Squeezing:: Squeezing repeats and deleting.
282 * ls invocation:: List directory contents
283 * dir invocation:: Briefly list directory contents
284 * vdir invocation:: Verbosely list directory contents
285 * dircolors invocation:: Color setup for @command{ls}
287 @command{ls}: List directory contents
289 * Which files are listed:: Which files are listed
290 * What information is listed:: What information is listed
291 * Sorting the output:: Sorting the output
292 * More details about version sort:: More details about version sort
293 * General output formatting:: General output formatting
294 * Formatting the file names:: Formatting the file names
298 * cp invocation:: Copy files and directories
299 * dd invocation:: Convert and copy a file
300 * install invocation:: Copy files and set attributes
301 * mv invocation:: Move (rename) files
302 * rm invocation:: Remove files or directories
303 * shred invocation:: Remove files more securely
307 * link invocation:: Make a hard link via the link syscall
308 * ln invocation:: Make links between files
309 * mkdir invocation:: Make directories
310 * mkfifo invocation:: Make FIFOs (named pipes)
311 * mknod invocation:: Make block or character special files
312 * readlink invocation:: Print the referent of a symbolic link
313 * rmdir invocation:: Remove empty directories
314 * unlink invocation:: Remove files via unlink syscall
316 Changing file attributes
318 * chown invocation:: Change file owner and group
319 * chgrp invocation:: Change group ownership
320 * chmod invocation:: Change access permissions
321 * touch invocation:: Change file timestamps
325 * df invocation:: Report file system disk space usage
326 * du invocation:: Estimate file space usage
327 * stat invocation:: Report file or file system status
328 * sync invocation:: Synchronize data on disk with memory
332 * echo invocation:: Print a line of text
333 * printf invocation:: Format and print data
334 * yes invocation:: Print a string until interrupted
338 * false invocation:: Do nothing, unsuccessfully
339 * true invocation:: Do nothing, successfully
340 * test invocation:: Check file types and compare values
341 * expr invocation:: Evaluate expressions
343 @command{test}: Check file types and compare values
345 * File type tests:: File type tests
346 * Access permission tests:: Access permission tests
347 * File characteristic tests:: File characteristic tests
348 * String tests:: String tests
349 * Numeric tests:: Numeric tests
351 @command{expr}: Evaluate expression
353 * String expressions:: + : match substr index length
354 * Numeric expressions:: + - * / %
355 * Relations for expr:: | & < <= = == != >= >
356 * Examples of expr:: Examples of using @command{expr}
360 * tee invocation:: Redirect output to multiple files
362 File name manipulation
364 * basename invocation:: Strip directory and suffix from a file name
365 * dirname invocation:: Strip non-directory suffix from a file name
366 * pathchk invocation:: Check file name portability
370 * pwd invocation:: Print working directory
371 * stty invocation:: Print or change terminal characteristics
372 * printenv invocation:: Print all or some environment variables
373 * tty invocation:: Print file name of terminal on standard input
375 @command{stty}: Print or change terminal characteristics
377 * Control:: Control settings
378 * Input:: Input settings
379 * Output:: Output settings
380 * Local:: Local settings
381 * Combination:: Combination settings
382 * Characters:: Special characters
383 * Special:: Special settings
387 * id invocation:: Print user identity
388 * logname invocation:: Print current login name
389 * whoami invocation:: Print effective user ID
390 * groups invocation:: Print group names a user is in
391 * users invocation:: Print login names of users currently logged in
392 * who invocation:: Print who is currently logged in
396 * date invocation:: Print or set system date and time
397 * uname invocation:: Print system information
398 * hostname invocation:: Print or set system name
399 * hostid invocation:: Print numeric host identifier.
401 @command{date}: Print or set system date and time
403 * Time conversion specifiers:: %[HIklMNpPrRsSTXzZ]
404 * Date conversion specifiers:: %[aAbBcCdDeFgGhjmuUVwWxyY]
405 * Literal conversion specifiers:: %[%nt]
406 * Padding and other flags:: Pad with zeros, spaces, etc.
407 * Setting the time:: Changing the system clock.
408 * Options for date:: Instead of the current time.
409 * Examples of date:: Examples.
411 Modified command invocation
413 * chroot invocation:: Run a command with a different root directory
414 * env invocation:: Run a command in a modified environment
415 * nice invocation:: Run a command with modified niceness
416 * nohup invocation:: Run a command immune to hangups
417 * su invocation:: Run a command with substitute user and group ID
421 * kill invocation:: Sending a signal to processes.
425 * sleep invocation:: Delay for a specified time
429 * factor invocation:: Print prime factors
430 * seq invocation:: Print numeric sequences
434 * Mode Structure:: Structure of file mode bits.
435 * Symbolic Modes:: Mnemonic representation of file mode bits.
436 * Numeric Modes:: File mode bits as octal numbers.
437 * Directory Setuid and Setgid:: Set-user-ID and set-group-ID on directories.
441 * General date syntax:: Common rules.
442 * Calendar date items:: 19 Dec 1994.
443 * Time of day items:: 9:20pm.
444 * Time zone items:: @sc{est}, @sc{pdt}, @sc{gmt}.
445 * Day of week items:: Monday and others.
446 * Relative items in date strings:: next tuesday, 2 years ago.
447 * Pure numbers in date strings:: 19931219, 1440.
448 * Seconds since the Epoch:: @@1078100502.
449 * Specifying time zone rules:: TZ="America/New_York", TZ="UTC0".
450 * Authors of get_date:: Bellovin, Eggert, Salz, Berets, et al.
452 Opening the software toolbox
454 * Toolbox introduction:: Toolbox introduction
455 * I/O redirection:: I/O redirection
456 * The who command:: The @command{who} command
457 * The cut command:: The @command{cut} command
458 * The sort command:: The @command{sort} command
459 * The uniq command:: The @command{uniq} command
460 * Putting the tools together:: Putting the tools together
462 GNU Free Documentation License
464 * How to use this License for your documents::
471 @chapter Introduction
473 This manual is a work in progress: many sections make no attempt to explain
474 basic concepts in a way suitable for novices. Thus, if you are interested,
475 please get involved in improving this manual. The entire @sc{gnu} community
478 @cindex @acronym{POSIX}
479 The @sc{gnu} utilities documented here are mostly compatible with the
480 @acronym{POSIX} standard.
481 @cindex bugs, reporting
482 Please report bugs to @email{bug-coreutils@@gnu.org}. Remember
483 to include the version number, machine architecture, input files, and
484 any other information needed to reproduce the bug: your input, what you
485 expected, what you got, and why it is wrong. Diffs are welcome, but
486 please include a description of the problem as well, since this is
487 sometimes difficult to infer. @xref{Bugs, , , gcc, Using and Porting GNU CC}.
493 @cindex MacKenzie, D.
496 This manual was originally derived from the Unix man pages in the
497 distributions, which were written by David MacKenzie and updated by Jim
498 Meyering. What you are reading now is the authoritative documentation
499 for these utilities; the man pages are no longer being maintained. The
500 original @command{fmt} man page was written by Ross Paterson. Fran@,{c}ois
501 Pinard did the initial conversion to Texinfo format. Karl Berry did the
502 indexing, some reorganization, and editing of the results. Brian
503 Youmans of the Free Software Foundation office staff combined the
504 manuals for textutils, fileutils, and sh-utils to produce the present
505 omnibus manual. Richard Stallman contributed his usual invaluable
506 insights to the overall process.
509 @chapter Common options
513 @itemx @w{@kbd{--backup}[=@var{method}]}
516 @vindex VERSION_CONTROL
517 @cindex backups, making
518 @xref{Backup options}.
519 Make a backup of each file that would otherwise be overwritten or removed.
522 @macro optBackupSuffix
523 @item -S @var{suffix}
524 @itemx --suffix=@var{suffix}
527 Append @var{suffix} to each backup file made with @option{-b}.
528 @xref{Backup options}.
531 @macro optTargetDirectory
532 @item -t @var{directory}
533 @itemx @w{@kbd{--target-directory}=@var{directory}}
535 @opindex --target-directory
536 @cindex target directory
537 @cindex destination directory
538 Specify the destination @var{directory}.
539 @xref{Target directory}.
542 @macro optNoTargetDirectory
544 @itemx --no-target-directory
546 @opindex --no-target-directory
547 @cindex target directory
548 @cindex destination directory
549 Do not treat the last operand specially when it is a directory or a
550 symbolic link to a directory. @xref{Target directory}.
557 Append an SI-style abbreviation to each size, such as @samp{MB} for
558 megabytes. Powers of 1000 are used, not 1024; @samp{MB} stands for
559 1,000,000 bytes. This option is equivalent to
560 @option{--block-size=si}. Use the @option{-h} or
561 @option{--human-readable} option if
562 you prefer powers of 1024.
565 @macro optHumanReadable
567 @itemx --human-readable
569 @opindex --human-readable
570 @cindex human-readable output
571 Append a size letter to each size, such as @samp{M} for mebibytes.
572 Powers of 1024 are used, not 1000; @samp{M} stands for 1,048,576 bytes.
573 Use the @option{--si} option if you prefer powers of 1000.
576 @macro optStripTrailingSlashes
577 @itemx @w{@kbd{--strip-trailing-slashes}}
578 @opindex --strip-trailing-slashes
579 @cindex stripping trailing slashes
580 Remove any trailing slashes from each @var{source} argument.
581 @xref{Trailing slashes}.
584 @cindex common options
586 Certain options are available in all of these programs. Rather than
587 writing identical descriptions for each of the programs, they are
588 described here. (In fact, every @sc{gnu} program accepts (or should accept)
591 @vindex POSIXLY_CORRECT
592 Normally options and operands can appear in any order, and programs act
593 as if all the options appear before any operands. For example,
594 @samp{sort -r passwd -t :} acts like @samp{sort -r -t : passwd}, since
595 @samp{:} is an option-argument of @option{-t}. However, if the
596 @env{POSIXLY_CORRECT} environment variable is set, options must appear
597 before operands, unless otherwise specified for a particular command.
599 A few programs can usefully have trailing operands with leading
600 @samp{-}. With such a program, options must precede operands even if
601 @env{POSIXLY_CORRECT} is not set, and this fact is noted in the
602 program description. For example, the @command{env} command's options
603 must appear before its operands, since in some cases the operands
604 specify a command that itself contains options.
606 Some of these programs recognize the @option{--help} and @option{--version}
607 options only when one of them is the sole command line argument.
614 Print a usage message listing all available options, then exit successfully.
618 @cindex version number, finding
619 Print the version number, then exit successfully.
623 @cindex option delimiter
624 Delimit the option list. Later arguments, if any, are treated as
625 operands even if they begin with @samp{-}. For example, @samp{sort --
626 -r} reads from the file named @file{-r}.
630 @cindex standard input
631 @cindex standard output
632 A single @samp{-} operand is not really an option, though it looks like one. It
633 stands for standard input, or for standard output if that is clear from
634 the context. For example, @samp{sort -} reads from standard input,
635 and is equivalent to plain @samp{sort}, and @samp{tee -} writes an
636 extra copy of its input to standard output. Unless otherwise
637 specified, @samp{-} can appear as any operand that requires a file
641 * Exit status:: Indicating program success or failure.
642 * Backup options:: -b -S, in some programs.
643 * Block size:: BLOCK_SIZE and --block-size, in some programs.
644 * Target directory:: Specifying a target directory, in some programs.
645 * Trailing slashes:: --strip-trailing-slashes, in some programs.
646 * Traversing symlinks:: -H, -L, or -P, in some programs.
647 * Treating / specially:: --preserve-root and --no-preserve-root.
648 * Special built-in utilities:: @command{break}, @command{:}, @command{eval}, @dots{}
649 * Standards conformance:: Conformance to the @acronym{POSIX} standard.
657 An exit status of zero indicates success,
658 and a nonzero value indicates failure.
661 Nearly every command invocation yields an integral @dfn{exit status}
662 that can be used to change how other commands work.
663 For the vast majority of commands, an exit status of zero indicates
664 success. Failure is indicated by a nonzero value---typically
665 @samp{1}, though it may differ on unusual platforms as @acronym{POSIX}
666 requires only that it be nonzero.
668 However, some of the programs documented here do produce
669 other exit status values and a few associate different
670 meanings with the values @samp{0} and @samp{1}.
671 Here are some of the exceptions:
672 @command{chroot}, @command{env}, @command{expr},
673 @command{nice}, @command{nohup}, @command{printenv},
674 @command{sort}, @command{su}, @command{test}, @command{tty}.
678 @section Backup options
680 @cindex backup options
682 Some @sc{gnu} programs (at least @command{cp}, @command{install},
683 @command{ln}, and @command{mv}) optionally make backups of files
684 before writing new versions.
685 These options control the details of these backups. The options are also
686 briefly mentioned in the descriptions of the particular programs.
691 @itemx @w{@kbd{--backup}[=@var{method}]}
694 @vindex VERSION_CONTROL
695 @cindex backups, making
696 Make a backup of each file that would otherwise be overwritten or removed.
697 Without this option, the original versions are destroyed.
698 Use @var{method} to determine the type of backups to make.
699 When this option is used but @var{method} is not specified,
700 then the value of the @env{VERSION_CONTROL}
701 environment variable is used. And if @env{VERSION_CONTROL} is not set,
702 the default backup type is @samp{existing}.
704 Note that the short form of this option, @option{-b} does not accept any
705 argument. Using @option{-b} is equivalent to using @option{--backup=existing}.
707 @vindex version-control @r{Emacs variable}
708 This option corresponds to the Emacs variable @samp{version-control};
709 the values for @var{method} are the same as those used in Emacs.
710 This option also accepts more descriptive names.
711 The valid @var{method}s are (unique abbreviations are accepted):
716 @opindex none @r{backup method}
721 @opindex numbered @r{backup method}
722 Always make numbered backups.
726 @opindex existing @r{backup method}
727 Make numbered backups of files that already have them, simple backups
732 @opindex simple @r{backup method}
733 Always make simple backups. Please note @samp{never} is not to be
734 confused with @samp{none}.
738 @item -S @var{suffix}
739 @itemx --suffix=@var{suffix}
742 @cindex backup suffix
743 @vindex SIMPLE_BACKUP_SUFFIX
744 Append @var{suffix} to each backup file made with @option{-b}. If this
745 option is not specified, the value of the @env{SIMPLE_BACKUP_SUFFIX}
746 environment variable is used. And if @env{SIMPLE_BACKUP_SUFFIX} is not
747 set, the default is @samp{~}, just as in Emacs.
756 Some @sc{gnu} programs (at least @command{df}, @command{du}, and
757 @command{ls}) display sizes in ``blocks''. You can adjust the block size
758 and method of display to make sizes easier to read. The block size
759 used for display is independent of any file system block size.
760 Fractional block counts are rounded up to the nearest integer.
762 @opindex --block-size=@var{size}
765 @vindex DF_BLOCK_SIZE
766 @vindex DU_BLOCK_SIZE
767 @vindex LS_BLOCK_SIZE
768 @vindex POSIXLY_CORRECT@r{, and block size}
770 The default block size is chosen by examining the following environment
771 variables in turn; the first one that is set determines the block size.
776 This specifies the default block size for the @command{df} command.
777 Similarly, @env{DU_BLOCK_SIZE} specifies the default for @command{du} and
778 @env{LS_BLOCK_SIZE} for @command{ls}.
781 This specifies the default block size for all three commands, if the
782 above command-specific environment variables are not set.
785 This specifies the default block size for all values that are normally
786 printed as blocks, if neither @env{BLOCK_SIZE} nor the above
787 command-specific environment variables are set. Unlike the other
788 environment variables, @env{BLOCKSIZE} does not affect values that are
789 normally printed as byte counts, e.g., the file sizes contained in
792 @item POSIXLY_CORRECT
793 If neither @env{@var{command}_BLOCK_SIZE}, nor @env{BLOCK_SIZE}, nor
794 @env{BLOCKSIZE} is set, but this variable is set, the block size
799 If none of the above environment variables are set, the block size
800 currently defaults to 1024 bytes in most contexts, but this number may
801 change in the future. For @command{ls} file sizes, the block size
804 @cindex human-readable output
807 A block size specification can be a positive integer specifying the number
808 of bytes per block, or it can be @code{human-readable} or @code{si} to
809 select a human-readable format. Integers may be followed by suffixes
810 that are upward compatible with the
811 @uref{http://www.bipm.fr/enus/3_SI/si-prefixes.html, SI prefixes}
812 for decimal multiples and with the
813 @uref{http://physics.nist.gov/cuu/Units/binary.html, IEC 60027-2
814 prefixes for binary multiples}.
816 With human-readable formats, output sizes are followed by a size letter
817 such as @samp{M} for megabytes. @code{BLOCK_SIZE=human-readable} uses
818 powers of 1024; @samp{M} stands for 1,048,576 bytes.
819 @code{BLOCK_SIZE=si} is similar, but uses powers of 1000 and appends
820 @samp{B}; @samp{MB} stands for 1,000,000 bytes.
823 A block size specification preceded by @samp{'} causes output sizes to
824 be displayed with thousands separators. The @env{LC_NUMERIC} locale
825 specifies the thousands separator and grouping. For example, in an
826 American English locale, @samp{--block-size="'1kB"} would cause a size
827 of 1234000 bytes to be displayed as @samp{1,234}. In the default C
828 locale, there is no thousands separator so a leading @samp{'} has no
831 An integer block size can be followed by a suffix to specify a
832 multiple of that size. A bare size letter,
833 or one followed by @samp{iB}, specifies
834 a multiple using powers of 1024. A size letter followed by @samp{B}
835 specifies powers of 1000 instead. For example, @samp{1M} and
836 @samp{1MiB} are equivalent to @samp{1048576}, whereas @samp{1MB} is
837 equivalent to @samp{1000000}.
839 A plain suffix without a preceding integer acts as if @samp{1} were
840 prepended, except that it causes a size indication to be appended to
841 the output. For example, @samp{--block-size="kB"} displays 3000 as
844 The following suffixes are defined. Large sizes like @code{1Y}
845 may be rejected by your computer due to limitations of its arithmetic.
849 @cindex kilobyte, definition of
850 kilobyte: @math{10^3 = 1000}.
854 @cindex kibibyte, definition of
855 kibibyte: @math{2^10 = 1024}. @samp{K} is special: the SI prefix is
856 @samp{k} and the IEC 60027-2 prefix is @samp{Ki}, but tradition and
857 @acronym{POSIX} use @samp{k} to mean @samp{KiB}.
859 @cindex megabyte, definition of
860 megabyte: @math{10^6 = 1,000,000}.
863 @cindex mebibyte, definition of
864 mebibyte: @math{2^20 = 1,048,576}.
866 @cindex gigabyte, definition of
867 gigabyte: @math{10^9 = 1,000,000,000}.
870 @cindex gibibyte, definition of
871 gibibyte: @math{2^30 = 1,073,741,824}.
873 @cindex terabyte, definition of
874 terabyte: @math{10^12 = 1,000,000,000,000}.
877 @cindex tebibyte, definition of
878 tebibyte: @math{2^40 = 1,099,511,627,776}.
880 @cindex petabyte, definition of
881 petabyte: @math{10^15 = 1,000,000,000,000,000}.
884 @cindex pebibyte, definition of
885 pebibyte: @math{2^50 = 1,125,899,906,842,624}.
887 @cindex exabyte, definition of
888 exabyte: @math{10^18 = 1,000,000,000,000,000,000}.
891 @cindex exbibyte, definition of
892 exbibyte: @math{2^60 = 1,152,921,504,606,846,976}.
894 @cindex zettabyte, definition of
895 zettabyte: @math{10^21 = 1,000,000,000,000,000,000,000}
898 @math{2^70 = 1,180,591,620,717,411,303,424}.
899 (@samp{Zi} is a @acronym{GNU} extension to IEC 60027-2.)
901 @cindex yottabyte, definition of
902 yottabyte: @math{10^24 = 1,000,000,000,000,000,000,000,000}.
905 @math{2^80 = 1,208,925,819,614,629,174,706,176}.
906 (@samp{Yi} is a @acronym{GNU} extension to IEC 60027-2.)
911 @opindex --block-size
912 @opindex --human-readable
915 Block size defaults can be overridden by an explicit
916 @option{--block-size=@var{size}} option. The @option{-k}
917 option is equivalent to @option{--block-size=1K}, which
918 is the default unless the @env{POSIXLY_CORRECT} environment variable is
919 set. The @option{-h} or @option{--human-readable} option is equivalent to
920 @option{--block-size=human-readable}. The @option{--si} option is
921 equivalent to @option{--block-size=si}.
923 @node Target directory
924 @section Target directory
926 @cindex target directory
928 The @command{cp}, @command{install}, @command{ln}, and @command{mv}
929 commands normally treat the last operand specially when it is a
930 directory or a symbolic link to a directory. For example, @samp{cp
931 source dest} is equivalent to @samp{cp source dest/source} if
932 @file{dest} is a directory. Sometimes this behavior is not exactly
933 what is wanted, so these commands support the following options to
934 allow more fine-grained control:
939 @itemx --no-target-directory
940 @opindex --no-target-directory
941 @cindex target directory
942 @cindex destination directory
943 Do not treat the last operand specially when it is a directory or a
944 symbolic link to a directory. This can help avoid race conditions in
945 programs that operate in a shared area. For example, when the command
946 @samp{mv /tmp/source /tmp/dest} succeeds, there is no guarantee that
947 @file{/tmp/source} was renamed to @file{/tmp/dest}: it could have been
948 renamed to @file{/tmp/dest/source} instead, if some other process
949 created @file{/tmp/dest} as a directory. However, if @file{mv
950 -T /tmp/source /tmp/dest} succeeds, there is no
951 question that @file{/tmp/source} was renamed to @file{/tmp/dest}.
953 In the opposite situation, where you want the last operand to be
954 treated as a directory and want a diagnostic otherwise, you can use
955 the @option{--target-directory} (@option{-t}) option.
957 @item -t @var{directory}
958 @itemx @w{@kbd{--target-directory}=@var{directory}}
959 @opindex --target-directory
960 @cindex target directory
961 @cindex destination directory
962 Use @var{directory} as the directory component of each destination
965 The interface for most programs is that after processing options and a
966 finite (possibly zero) number of fixed-position arguments, the remaining
967 argument list is either expected to be empty, or is a list of items
968 (usually files) that will all be handled identically. The @command{xargs}
969 program is designed to work well with this convention.
971 The commands in the @command{mv}-family are unusual in that they take
972 a variable number of arguments with a special case at the @emph{end}
973 (namely, the target directory). This makes it nontrivial to perform some
974 operations, e.g., ``move all files from here to ../d/'', because
975 @code{mv * ../d/} might exhaust the argument space, and @code{ls | xargs ...}
976 doesn't have a clean way to specify an extra final argument for each
977 invocation of the subject command. (It can be done by going through a
978 shell command, but that requires more human labor and brain power than
981 The @w{@kbd{--target-directory}} (@option{-t}) option allows the @command{cp},
982 @command{install}, @command{ln}, and @command{mv} programs to be used
983 conveniently with @command{xargs}. For example, you can move the files
984 from the current directory to a sibling directory, @code{d} like this:
987 ls | xargs mv -t ../d --
990 However, this doesn't move files whose names begin with @samp{.}.
991 If you use the @sc{gnu} @command{find} program, you can move those
992 files too, with this command:
995 find . -mindepth 1 -maxdepth 1 \
999 But both of the above approaches fail if there are no files in the
1000 current directory, or if any file has a name containing a blank or
1001 some other special characters.
1002 The following example removes those limitations and requires both
1003 @sc{gnu} @command{find} and @sc{gnu} @command{xargs}:
1006 find . -mindepth 1 -maxdepth 1 -print0 \
1007 | xargs --null --no-run-if-empty \
1014 The @option{--target-directory} (@option{-t}) and
1015 @option{--no-target-directory} (@option{-T})
1016 options cannot be combined.
1018 @node Trailing slashes
1019 @section Trailing slashes
1021 @cindex trailing slashes
1023 Some @sc{gnu} programs (at least @command{cp} and @command{mv}) allow you to
1024 remove any trailing slashes from each @var{source} argument before
1025 operating on it. The @w{@kbd{--strip-trailing-slashes}} option enables
1028 This is useful when a @var{source} argument may have a trailing slash and
1029 @c FIXME: mv's behavior in this case is system-dependent
1030 specify a symbolic link to a directory. This scenario is in fact rather
1031 common because some shells can automatically append a trailing slash when
1032 performing file name completion on such symbolic links. Without this
1033 option, @command{mv}, for example, (via the system's rename function) must
1034 interpret a trailing slash as a request to dereference the symbolic link
1035 and so must rename the indirectly referenced @emph{directory} and not
1036 the symbolic link. Although it may seem surprising that such behavior
1037 be the default, it is required by @acronym{POSIX} and is consistent with
1038 other parts of that standard.
1040 @node Traversing symlinks
1041 @section Traversing symlinks
1043 @cindex symbolic link to directory, controlling traversal of
1045 The following options modify how @command{chown} and @command{chgrp}
1046 @c FIXME: note that `du' has these options, too, but they have slightly
1047 @c different meaning.
1048 traverse a hierarchy when the @option{--recursive} (@option{-R})
1049 option is also specified.
1050 If more than one of the following options is specified, only the final
1052 These options specify whether processing a symbolic link to a directory
1053 entails operating on just the symbolic link or on all files in the
1054 hierarchy rooted at that directory.
1056 These options are independent of @option{--dereference} and
1057 @option{--no-dereference} (@option{-h}), which control whether to modify
1058 a symlink or its referent.
1065 @cindex symbolic link to directory, traverse each that is specified on the command line
1066 If @option{--recursive} (@option{-R}) is specified and
1067 a command line argument is a symbolic link to a directory, traverse it.
1074 @cindex symbolic link to directory, traverse each that is encountered
1075 In a recursive traversal, traverse every symbolic link to a directory
1076 that is encountered.
1083 @cindex symbolic link to directory, never traverse
1084 Do not traverse any symbolic links.
1085 This is the default if none of @option{-H}, @option{-L},
1086 or @option{-P} is specified.
1093 @node Treating / specially
1094 @section Treating / specially
1096 Certain commands can operate destructively on entire hierarchies.
1097 For example, if a user with appropriate privileges mistakenly runs
1098 @samp{rm -rf / tmp/junk} or @samp{cd /bin; rm -rf ../}, that may remove
1099 all files on the entire system. Since there are so few
1100 @footnote{If you know of one, please write to @email{bug-coreutils@@gnu.org}.}
1101 legitimate uses for such a command,
1102 @sc{gnu} @command{rm} provides the @option{--preserve-root} option
1103 to make it so @command{rm} declines to operate on any directory
1104 that resolves to @file{/}. The default is still to allow
1105 @samp{rm -rf /} to operate unimpeded.
1106 Another new option, @option{--no-preserve-root}, cancels the
1107 effect of any preceding @option{--preserve-root} option.
1108 Note that the @option{--preserve-root} behavior may become the default
1111 The commands @command{chgrp}, @command{chmod} and @command{chown}
1112 can also operate destructively on entire hierarchies, so they too
1113 support these options. Although, unlike @command{rm}, they don't
1114 actually unlink files, these commands are arguably more dangerous
1115 when operating recursively on @file{/}, since they often work much
1116 more quickly, and hence damage more files before an alert user can
1119 @node Special built-in utilities
1120 @section Special built-in utilities
1122 Some programs like @command{nice} can invoke other programs; for
1123 example, the command @samp{nice cat file} invokes the program
1124 @command{cat} by executing the command @samp{cat file}. However,
1125 @dfn{special built-in utilities} like @command{exit} cannot be invoked
1126 this way. For example, the command @samp{nice exit} does not have a
1127 well-defined behavior: it may generate an error message instead of
1130 Here is a list of the special built-in utilities that are standardized
1131 by @acronym{POSIX} 1003.1-2004.
1134 @t{.@: : break continue eval exec exit export readonly
1135 return set shift times trap unset}
1138 For example, because @samp{.}, @samp{:}, and @samp{exec} are special,
1139 the commands @samp{nice . foo.sh}, @samp{nice :}, and @samp{nice exec
1140 pwd} do not work as you might expect.
1142 Many shells extend this list. For example, Bash has several extra
1143 special built-in utilities like @command{history}, and
1144 @command{suspend}, and with Bash the command @samp{nice suspend}
1145 generates an error message instead of suspending.
1147 @node Standards conformance
1148 @section Standards conformance
1150 @vindex POSIXLY_CORRECT
1151 In a few cases, the @sc{gnu} utilities' default behavior is
1152 incompatible with the @acronym{POSIX} standard. To suppress these
1153 incompatibilities, define the @env{POSIXLY_CORRECT} environment
1154 variable. Unless you are checking for @acronym{POSIX} conformance, you
1155 probably do not need to define @env{POSIXLY_CORRECT}.
1157 Newer versions of @acronym{POSIX} are occasionally incompatible with older
1158 versions. For example, older versions of @acronym{POSIX} required the
1159 command @samp{sort +1} to sort based on the second and succeeding
1160 fields in each input line, but starting with @acronym{POSIX} 1003.1-2001
1161 the same command is required to sort the file named @file{+1}, and you
1162 must instead use the command @samp{sort -k 2} to get the field-based
1165 @vindex _POSIX2_VERSION
1166 The @sc{gnu} utilities normally conform to the version of @acronym{POSIX}
1167 that is standard for your system. To cause them to conform to a
1168 different version of @acronym{POSIX}, define the @env{_POSIX2_VERSION}
1169 environment variable to a value of the form @var{yyyymm} specifying
1170 the year and month the standard was adopted. Two values are currently
1171 supported for @env{_POSIX2_VERSION}: @samp{199209} stands for
1172 @acronym{POSIX} 1003.2-1992, and @samp{200112} stands for @acronym{POSIX}
1173 1003.1-2001. For example, if you have a newer system but are running software
1174 that assumes an older version of @acronym{POSIX} and uses @samp{sort +1}
1175 or @samp{tail +10}, you can work around any compatibility problems by setting
1176 @samp{_POSIX2_VERSION=199209} in your environment.
1178 @node Output of entire files
1179 @chapter Output of entire files
1181 @cindex output of entire files
1182 @cindex entire files, output of
1184 These commands read and write entire files, possibly transforming them
1188 * cat invocation:: Concatenate and write files.
1189 * tac invocation:: Concatenate and write files in reverse.
1190 * nl invocation:: Number lines and write files.
1191 * od invocation:: Write files in octal or other formats.
1192 * base64 invocation:: Transform data into printable data.
1195 @node cat invocation
1196 @section @command{cat}: Concatenate and write files
1199 @cindex concatenate and write files
1200 @cindex copying files
1202 @command{cat} copies each @var{file} (@samp{-} means standard input), or
1203 standard input if none are given, to standard output. Synopsis:
1206 cat [@var{option}] [@var{file}]@dots{}
1209 The program accepts the following options. Also see @ref{Common options}.
1217 Equivalent to @option{-vET}.
1220 @itemx --number-nonblank
1222 @opindex --number-nonblank
1223 Number all nonblank output lines, starting with 1.
1227 Equivalent to @option{-vE}.
1232 @opindex --show-ends
1233 Display a @samp{$} after the end of each line.
1239 Number all output lines, starting with 1.
1242 @itemx --squeeze-blank
1244 @opindex --squeeze-blank
1245 @cindex squeezing blank lines
1246 Replace multiple adjacent blank lines with a single blank line.
1250 Equivalent to @option{-vT}.
1255 @opindex --show-tabs
1256 Display TAB characters as @samp{^I}.
1260 Ignored; for @acronym{POSIX} compatibility.
1263 @itemx --show-nonprinting
1265 @opindex --show-nonprinting
1266 Display control characters except for LFD and TAB using
1267 @samp{^} notation and precede characters that have the high bit set with
1272 On systems like MS-DOS that distinguish between text and binary files,
1273 @command{cat} normally reads and writes in binary mode. However,
1274 @command{cat} reads in text mode if one of the options
1275 @option{-bensAE} is used or if @command{cat} is reading from standard
1276 input and standard input is a terminal. Similarly, @command{cat}
1277 writes in text mode if one of the options @option{-bensAE} is used or
1278 if standard output is a terminal.
1285 # Output f's contents, then standard input, then g's contents.
1288 # Copy standard input to standard output.
1293 @node tac invocation
1294 @section @command{tac}: Concatenate and write files in reverse
1297 @cindex reversing files
1299 @command{tac} copies each @var{file} (@samp{-} means standard input), or
1300 standard input if none are given, to standard output, reversing the
1301 records (lines by default) in each separately. Synopsis:
1304 tac [@var{option}]@dots{} [@var{file}]@dots{}
1307 @dfn{Records} are separated by instances of a string (newline by
1308 default). By default, this separator string is attached to the end of
1309 the record that it follows in the file.
1311 The program accepts the following options. Also see @ref{Common options}.
1319 The separator is attached to the beginning of the record that it
1320 precedes in the file.
1326 Treat the separator string as a regular expression. Users of @command{tac}
1327 on MS-DOS/MS-Windows should note that, since @command{tac} reads files in
1328 binary mode, each line of a text file might end with a CR/LF pair
1329 instead of the Unix-style LF.
1331 @item -s @var{separator}
1332 @itemx --separator=@var{separator}
1334 @opindex --separator
1335 Use @var{separator} as the record separator, instead of newline.
1343 @section @command{nl}: Number lines and write files
1346 @cindex numbering lines
1347 @cindex line numbering
1349 @command{nl} writes each @var{file} (@samp{-} means standard input), or
1350 standard input if none are given, to standard output, with line numbers
1351 added to some or all of the lines. Synopsis:
1354 nl [@var{option}]@dots{} [@var{file}]@dots{}
1357 @cindex logical pages, numbering on
1358 @command{nl} decomposes its input into (logical) pages; by default, the
1359 line number is reset to 1 at the top of each logical page. @command{nl}
1360 treats all of the input files as a single document; it does not reset
1361 line numbers or logical pages between files.
1363 @cindex headers, numbering
1364 @cindex body, numbering
1365 @cindex footers, numbering
1366 A logical page consists of three sections: header, body, and footer.
1367 Any of the sections can be empty. Each can be numbered in a different
1368 style from the others.
1370 The beginnings of the sections of logical pages are indicated in the
1371 input file by a line containing exactly one of these delimiter strings:
1382 The two characters from which these strings are made can be changed from
1383 @samp{\} and @samp{:} via options (see below), but the pattern and
1384 length of each string cannot be changed.
1386 A section delimiter is replaced by an empty line on output. Any text
1387 that comes before the first section delimiter string in the input file
1388 is considered to be part of a body section, so @command{nl} treats a
1389 file that contains no section delimiters as a single body section.
1391 The program accepts the following options. Also see @ref{Common options}.
1395 @item -b @var{style}
1396 @itemx --body-numbering=@var{style}
1398 @opindex --body-numbering
1399 Select the numbering style for lines in the body section of each
1400 logical page. When a line is not numbered, the current line number
1401 is not incremented, but the line number separator character is still
1402 prepended to the line. The styles are:
1408 number only nonempty lines (default for body),
1410 do not number lines (default for header and footer),
1412 number only lines that contain a match for the basic regular
1413 expression @var{bre}.
1414 @xref{Regular Expressions, , Regular Expressions, grep, The GNU Grep Manual}.
1418 @itemx --section-delimiter=@var{cd}
1420 @opindex --section-delimiter
1421 @cindex section delimiters of pages
1422 Set the section delimiter characters to @var{cd}; default is
1423 @samp{\:}. If only @var{c} is given, the second remains @samp{:}.
1424 (Remember to protect @samp{\} or other metacharacters from shell
1425 expansion with quotes or extra backslashes.)
1427 @item -f @var{style}
1428 @itemx --footer-numbering=@var{style}
1430 @opindex --footer-numbering
1431 Analogous to @option{--body-numbering}.
1433 @item -h @var{style}
1434 @itemx --header-numbering=@var{style}
1436 @opindex --header-numbering
1437 Analogous to @option{--body-numbering}.
1439 @item -i @var{number}
1440 @itemx --page-increment=@var{number}
1442 @opindex --page-increment
1443 Increment line numbers by @var{number} (default 1).
1445 @item -l @var{number}
1446 @itemx --join-blank-lines=@var{number}
1448 @opindex --join-blank-lines
1449 @cindex empty lines, numbering
1450 @cindex blank lines, numbering
1451 Consider @var{number} (default 1) consecutive empty lines to be one
1452 logical line for numbering, and only number the last one. Where fewer
1453 than @var{number} consecutive empty lines occur, do not number them.
1454 An empty line is one that contains no characters, not even spaces
1457 @item -n @var{format}
1458 @itemx --number-format=@var{format}
1460 @opindex --number-format
1461 Select the line numbering format (default is @code{rn}):
1465 @opindex ln @r{format for @command{nl}}
1466 left justified, no leading zeros;
1468 @opindex rn @r{format for @command{nl}}
1469 right justified, no leading zeros;
1471 @opindex rz @r{format for @command{nl}}
1472 right justified, leading zeros.
1476 @itemx --no-renumber
1478 @opindex --no-renumber
1479 Do not reset the line number at the start of a logical page.
1481 @item -s @var{string}
1482 @itemx --number-separator=@var{string}
1484 @opindex --number-separator
1485 Separate the line number from the text line in the output with
1486 @var{string} (default is the TAB character).
1488 @item -v @var{number}
1489 @itemx --starting-line-number=@var{number}
1491 @opindex --starting-line-number
1492 Set the initial line number on each logical page to @var{number} (default 1).
1494 @item -w @var{number}
1495 @itemx --number-width=@var{number}
1497 @opindex --number-width
1498 Use @var{number} characters for line numbers (default 6).
1506 @section @command{od}: Write files in octal or other formats
1509 @cindex octal dump of files
1510 @cindex hex dump of files
1511 @cindex ASCII dump of files
1512 @cindex file contents, dumping unambiguously
1514 @command{od} writes an unambiguous representation of each @var{file}
1515 (@samp{-} means standard input), or standard input if none are given.
1519 od [@var{option}]@dots{} [@var{file}]@dots{}
1520 od [-abcdfilosx]@dots{} [@var{file}] [[+]@var{offset}[.][b]]
1521 od [@var{option}]@dots{} --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1524 Each line of output consists of the offset in the input, followed by
1525 groups of data from the file. By default, @command{od} prints the offset in
1526 octal, and each group of file data is a C @code{short int}'s worth of input
1527 printed as a single octal number.
1529 If @var{offset} is given, it specifies how many input bytes to skip
1530 before formatting and writing. By default, it is interpreted as an
1531 octal number, but the optional trailing decimal point causes it to be
1532 interpreted as decimal. If no decimal is specified and the offset
1533 begins with @samp{0x} or @samp{0X} it is interpreted as a hexadecimal
1534 number. If there is a trailing @samp{b}, the number of bytes skipped
1535 will be @var{offset} multiplied by 512.
1537 If a command is of both the first and second forms, the second form is
1538 assumed if the last operand begins with @samp{+} or (if there are two
1539 operands) a digit. For example, in @samp{od foo 10} and @samp{od +10}
1540 the @samp{10} is an offset, whereas in @samp{od 10} the @samp{10} is a
1543 The program accepts the following options. Also see @ref{Common options}.
1547 @item -A @var{radix}
1548 @itemx --address-radix=@var{radix}
1550 @opindex --address-radix
1551 @cindex radix for file offsets
1552 @cindex file offset radix
1553 Select the base in which file offsets are printed. @var{radix} can
1554 be one of the following:
1564 none (do not print offsets).
1567 The default is octal.
1569 @item -j @var{bytes}
1570 @itemx --skip-bytes=@var{bytes}
1572 @opindex --skip-bytes
1573 Skip @var{bytes} input bytes before formatting and writing. If
1574 @var{bytes} begins with @samp{0x} or @samp{0X}, it is interpreted in
1575 hexadecimal; otherwise, if it begins with @samp{0}, in octal; otherwise,
1576 in decimal. Appending @samp{b} multiplies @var{bytes} by 512, @samp{k}
1577 by 1024, and @samp{m} by 1048576.
1579 @item -N @var{bytes}
1580 @itemx --read-bytes=@var{bytes}
1582 @opindex --read-bytes
1583 Output at most @var{bytes} bytes of the input. Prefixes and suffixes on
1584 @code{bytes} are interpreted as for the @option{-j} option.
1587 @itemx --strings[=@var{n}]
1590 @cindex string constants, outputting
1591 Instead of the normal output, output only @dfn{string constants}: at
1592 least @var{n} consecutive @acronym{ASCII} graphic characters,
1593 followed by a null (zero) byte.
1595 If @var{n} is omitted with @option{--strings}, the default is 3.
1598 @itemx --format=@var{type}
1601 Select the format in which to output the file data. @var{type} is a
1602 string of one or more of the below type indicator characters. If you
1603 include more than one type indicator character in a single @var{type}
1604 string, or use this option more than once, @command{od} writes one copy
1605 of each output line using each of the data types that you specified,
1606 in the order that you specified.
1608 Adding a trailing ``z'' to any type specification appends a display
1609 of the @acronym{ASCII} character representation of the printable characters
1610 to the output line generated by the type specification.
1614 named character, ignoring high-order bit
1616 @acronym{ASCII} character or backslash escape,
1629 The type @code{a} outputs things like @samp{sp} for space, @samp{nl} for
1630 newline, and @samp{nul} for a null (zero) byte. Only the least significant
1631 seven bits of each byte is used; the high-order bit is ignored.
1632 Type @code{c} outputs
1633 @samp{ }, @samp{\n}, and @code{\0}, respectively.
1636 Except for types @samp{a} and @samp{c}, you can specify the number
1637 of bytes to use in interpreting each number in the given data type
1638 by following the type indicator character with a decimal integer.
1639 Alternately, you can specify the size of one of the C compiler's
1640 built-in data types by following the type indicator character with
1641 one of the following characters. For integers (@samp{d}, @samp{o},
1642 @samp{u}, @samp{x}):
1655 For floating point (@code{f}):
1667 @itemx --output-duplicates
1669 @opindex --output-duplicates
1670 Output consecutive lines that are identical. By default, when two or
1671 more consecutive output lines would be identical, @command{od} outputs only
1672 the first line, and puts just an asterisk on the following line to
1673 indicate the elision.
1676 @itemx --width[=@var{n}]
1679 Dump @code{n} input bytes per output line. This must be a multiple of
1680 the least common multiple of the sizes associated with the specified
1683 If this option is not given at all, the default is 16. If @var{n} is
1684 omitted, the default is 32.
1688 The next several options are shorthands for format specifications.
1689 @sc{gnu} @command{od} accepts any combination of shorthands and format
1690 specification options. These options accumulate.
1696 Output as named characters. Equivalent to @samp{-t a}.
1700 Output as octal bytes. Equivalent to @samp{-t o1}.
1704 Output as @acronym{ASCII} characters or backslash escapes. Equivalent to
1709 Output as unsigned decimal two-byte units. Equivalent to @samp{-t u2}.
1713 Output as floats. Equivalent to @samp{-t fF}.
1717 Output as decimal ints. Equivalent to @samp{-t dI}.
1721 Output as decimal long ints. Equivalent to @samp{-t dL}.
1725 Output as octal two-byte units. Equivalent to @option{-t o2}.
1729 Output as decimal two-byte units. Equivalent to @option{-t d2}.
1733 Output as hexadecimal two-byte units. Equivalent to @samp{-t x2}.
1736 @opindex --traditional
1737 Recognize the non-option label argument that traditional @command{od}
1738 accepted. The following syntax:
1741 od --traditional [@var{file}] [[+]@var{offset}[.][b] [[+]@var{label}[.][b]]]
1745 can be used to specify at most one file and optional arguments
1746 specifying an offset and a pseudo-start address, @var{label}.
1747 The @var{label} argument is interpreted
1748 just like @var{offset}, but it specifies an initial pseudo-address. The
1749 pseudo-addresses are displayed in parentheses following any normal
1756 @node base64 invocation
1757 @section @command{base64}: Transform data into printable data.
1760 @cindex base64 encoding
1762 @command{base64} transforms data read from a file, or standard input,
1763 into (or from) base64 encoded form. The base64 encoded form uses
1764 printable @acronym{ASCII} characters to represent binary data, see
1765 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3548.txt, RFC 3548}.
1769 base64 [@var{option}]@dots{} [@var{file}]
1770 base64 --decode [@var{option}]@dots{} [@var{file}]
1773 The base64 encoding expands data to roughly 133% of the original.
1775 The program accepts the following options. Also see @ref{Common options}.
1780 @itemx --wrap=@var{COLS}
1784 @cindex column to wrap data after
1785 During encoding, wrap lines after @var{COLS} characters. This must be
1788 The default is to wrap after 76 characters. Use the value 0 to
1789 disable line wrapping altogether.
1795 @cindex Decode base64 data
1796 @cindex Base64 decoding
1797 Change the mode of operation, from the default of encoding data, to
1798 decoding data. Input is expected to be base64 encoded data, and the
1799 output will be the original data.
1802 @itemx --ignore-garbage
1804 @opindex --ignore-garbage
1805 @cindex Ignore garbage in base64 stream
1806 During decoding, ignore unrecognized characters (including newline),
1807 to permit distorted data to be decoded.
1814 @node Formatting file contents
1815 @chapter Formatting file contents
1817 @cindex formatting file contents
1819 These commands reformat the contents of files.
1822 * fmt invocation:: Reformat paragraph text.
1823 * pr invocation:: Paginate or columnate files for printing.
1824 * fold invocation:: Wrap input lines to fit in specified width.
1828 @node fmt invocation
1829 @section @command{fmt}: Reformat paragraph text
1832 @cindex reformatting paragraph text
1833 @cindex paragraphs, reformatting
1834 @cindex text, reformatting
1836 @command{fmt} fills and joins lines to produce output lines of (at most)
1837 a given number of characters (75 by default). Synopsis:
1840 fmt [@var{option}]@dots{} [@var{file}]@dots{}
1843 @command{fmt} reads from the specified @var{file} arguments (or standard
1844 input if none are given), and writes to standard output.
1846 By default, blank lines, spaces between words, and indentation are
1847 preserved in the output; successive input lines with different
1848 indentation are not joined; tabs are expanded on input and introduced on
1851 @cindex line-breaking
1852 @cindex sentences and line-breaking
1853 @cindex Knuth, Donald E.
1854 @cindex Plass, Michael F.
1855 @command{fmt} prefers breaking lines at the end of a sentence, and tries to
1856 avoid line breaks after the first word of a sentence or before the last
1857 word of a sentence. A @dfn{sentence break} is defined as either the end
1858 of a paragraph or a word ending in any of @samp{.?!}, followed by two
1859 spaces or end of line, ignoring any intervening parentheses or quotes.
1860 Like @TeX{}, @command{fmt} reads entire ``paragraphs'' before choosing line
1861 breaks; the algorithm is a variant of that given by Donald E. Knuth
1862 and Michael F. Plass in ``Breaking Paragraphs Into Lines'',
1863 @cite{Software---Practice & Experience} @b{11}, 11 (November 1981),
1866 The program accepts the following options. Also see @ref{Common options}.
1871 @itemx --crown-margin
1873 @opindex --crown-margin
1874 @cindex crown margin
1875 @dfn{Crown margin} mode: preserve the indentation of the first two
1876 lines within a paragraph, and align the left margin of each subsequent
1877 line with that of the second line.
1880 @itemx --tagged-paragraph
1882 @opindex --tagged-paragraph
1883 @cindex tagged paragraphs
1884 @dfn{Tagged paragraph} mode: like crown margin mode, except that if
1885 indentation of the first line of a paragraph is the same as the
1886 indentation of the second, the first line is treated as a one-line
1892 @opindex --split-only
1893 Split lines only. Do not join short lines to form longer ones. This
1894 prevents sample lines of code, and other such ``formatted'' text from
1895 being unduly combined.
1898 @itemx --uniform-spacing
1900 @opindex --uniform-spacing
1901 Uniform spacing. Reduce spacing between words to one space, and spacing
1902 between sentences to two spaces.
1905 @itemx -w @var{width}
1906 @itemx --width=@var{width}
1907 @opindex -@var{width}
1910 Fill output lines up to @var{width} characters (default 75). @command{fmt}
1911 initially tries to make lines about 7% shorter than this, to give it
1912 room to balance line lengths.
1914 @item -p @var{prefix}
1915 @itemx --prefix=@var{prefix}
1916 Only lines beginning with @var{prefix} (possibly preceded by whitespace)
1917 are subject to formatting. The prefix and any preceding whitespace are
1918 stripped for the formatting and then re-attached to each formatted output
1919 line. One use is to format certain kinds of program comments, while
1920 leaving the code unchanged.
1928 @section @command{pr}: Paginate or columnate files for printing
1931 @cindex printing, preparing files for
1932 @cindex multicolumn output, generating
1933 @cindex merging files in parallel
1935 @command{pr} writes each @var{file} (@samp{-} means standard input), or
1936 standard input if none are given, to standard output, paginating and
1937 optionally outputting in multicolumn format; optionally merges all
1938 @var{file}s, printing all in parallel, one per column. Synopsis:
1941 pr [@var{option}]@dots{} [@var{file}]@dots{}
1945 By default, a 5-line header is printed at each page: two blank lines;
1946 a line with the date, the file name, and the page count; and two more
1947 blank lines. A footer of five blank lines is also printed.
1948 With the @option{-F}
1949 option, a 3-line header is printed: the leading two blank lines are
1950 omitted; no footer is used. The default @var{page_length} in both cases is 66
1951 lines. The default number of text lines changes from 56 (without @option{-F})
1952 to 63 (with @option{-F}). The text line of the header takes the form
1953 @samp{@var{date} @var{string} @var{page}}, with spaces inserted around
1954 @var{string} so that the line takes up the full @var{page_width}. Here,
1955 @var{date} is the date (see the @option{-D} or @option{--date-format}
1956 option for details), @var{string} is the centered header string, and
1957 @var{page} identifies the page number. The @env{LC_MESSAGES} locale
1958 category affects the spelling of @var{page}; in the default C locale, it
1959 is @samp{Page @var{number}} where @var{number} is the decimal page
1962 Form feeds in the input cause page breaks in the output. Multiple form
1963 feeds produce empty pages.
1965 Columns are of equal width, separated by an optional string (default
1966 is @samp{space}). For multicolumn output, lines will always be truncated to
1967 @var{page_width} (default 72), unless you use the @option{-J} option.
1969 column output no line truncation occurs by default. Use @option{-W} option to
1970 truncate lines in that case.
1972 The following changes were made in version 1.22i and apply to later
1973 versions of @command{pr}:
1974 @c FIXME: this whole section here sounds very awkward to me. I
1975 @c made a few small changes, but really it all needs to be redone. - Brian
1976 @c OK, I fixed another sentence or two, but some of it I just don't understand.
1981 Some small @var{letter options} (@option{-s}, @option{-w}) have been
1982 redefined for better @acronym{POSIX} compliance. The output of some further
1983 cases has been adapted to other Unix systems. These changes are not
1984 compatible with earlier versions of the program.
1987 Some @var{new capital letter} options (@option{-J}, @option{-S}, @option{-W})
1988 have been introduced to turn off unexpected interferences of small letter
1989 options. The @option{-N} option and the second argument @var{last_page}
1990 of @samp{+FIRST_PAGE} offer more flexibility. The detailed handling of
1991 form feeds set in the input files requires the @option{-T} option.
1994 Capital letter options override small letter ones.
1997 Some of the option-arguments (compare @option{-s}, @option{-e},
1998 @option{-i}, @option{-n}) cannot be specified as separate arguments from the
1999 preceding option letter (already stated in the @acronym{POSIX} specification).
2002 The program accepts the following options. Also see @ref{Common options}.
2006 @item +@var{first_page}[:@var{last_page}]
2007 @itemx --pages=@var{first_page}[:@var{last_page}]
2008 @c The two following @opindex lines evoke warnings because they contain `:'
2009 @c The `info' spec does not permit that. If we use those lines, we end
2010 @c up with truncated index entries that don't work.
2011 @c @opindex +@var{first_page}[:@var{last_page}]
2012 @c @opindex --pages=@var{first_page}[:@var{last_page}]
2013 @opindex +@var{page_range}
2014 @opindex --pages=@var{page_range}
2015 Begin printing with page @var{first_page} and stop with @var{last_page}.
2016 Missing @samp{:@var{last_page}} implies end of file. While estimating
2017 the number of skipped pages each form feed in the input file results
2018 in a new page. Page counting with and without @samp{+@var{first_page}}
2019 is identical. By default, counting starts with the first page of input
2020 file (not first page printed). Line numbering may be altered by @option{-N}
2024 @itemx --columns=@var{column}
2025 @opindex -@var{column}
2027 @cindex down columns
2028 With each single @var{file}, produce @var{column} columns of output
2029 (default is 1) and print columns down, unless @option{-a} is used. The
2030 column width is automatically decreased as @var{column} increases; unless
2031 you use the @option{-W/-w} option to increase @var{page_width} as well.
2032 This option might well cause some lines to be truncated. The number of
2033 lines in the columns on each page are balanced. The options @option{-e}
2034 and @option{-i} are on for multiple text-column output. Together with
2035 @option{-J} option column alignment and line truncation is turned off.
2036 Lines of full length are joined in a free field format and @option{-S}
2037 option may set field separators. @option{-@var{column}} may not be used
2038 with @option{-m} option.
2044 @cindex across columns
2045 With each single @var{file}, print columns across rather than down. The
2046 @option{-@var{column}} option must be given with @var{column} greater than one.
2047 If a line is too long to fit in a column, it is truncated.
2050 @itemx --show-control-chars
2052 @opindex --show-control-chars
2053 Print control characters using hat notation (e.g., @samp{^G}); print
2054 other nonprinting characters in octal backslash notation. By default,
2055 nonprinting characters are not changed.
2058 @itemx --double-space
2060 @opindex --double-space
2061 @cindex double spacing
2062 Double space the output.
2064 @item -D @var{format}
2065 @itemx --date-format=@var{format}
2066 @cindex time formats
2067 @cindex formatting times
2068 Format header dates using @var{format}, using the same conventions as
2069 for the command @samp{date +@var{format}}; @xref{date invocation}.
2070 Except for directives, which start with
2071 @samp{%}, characters in @var{format} are printed unchanged. You can use
2072 this option to specify an arbitrary string in place of the header date,
2073 e.g., @option{--date-format="Monday morning"}.
2075 @vindex POSIXLY_CORRECT
2078 format defaults to @samp{%Y-%m-%d %H:%M} (for example, @samp{2001-12-04
2079 23:59}); but if the @env{POSIXLY_CORRECT} environment variable is set
2080 and the @env{LC_TIME} locale category specifies the @acronym{POSIX}
2081 locale, the default is @samp{%b %e %H:%M %Y} (for example,
2082 @samp{Dec@ @ 4 23:59 2001}.
2085 Time stamps are listed according to the time zone rules specified by
2086 the @env{TZ} environment variable, or by the system default rules if
2087 @env{TZ} is not set. @xref{TZ Variable,, Specifying the Time Zone
2088 with @env{TZ}, libc, The GNU C Library}.
2090 @item -e[@var{in-tabchar}[@var{in-tabwidth}]]
2091 @itemx --expand-tabs[=@var{in-tabchar}[@var{in-tabwidth}]]
2093 @opindex --expand-tabs
2095 Expand @var{tab}s to spaces on input. Optional argument @var{in-tabchar} is
2096 the input tab character (default is the TAB character). Second optional
2097 argument @var{in-tabwidth} is the input tab character's width (default
2105 @opindex --form-feed
2106 Use a form feed instead of newlines to separate output pages. The default
2107 page length of 66 lines is not altered. But the number of lines of text
2108 per page changes from default 56 to 63 lines.
2110 @item -h @var{HEADER}
2111 @itemx --header=@var{HEADER}
2114 Replace the file name in the header with the centered string @var{header}.
2115 When using the shell, @var{header} should be quoted and should be
2116 separated from @option{-h} by a space.
2118 @item -i[@var{out-tabchar}[@var{out-tabwidth}]]
2119 @itemx --output-tabs[=@var{out-tabchar}[@var{out-tabwidth}]]
2121 @opindex --output-tabs
2123 Replace spaces with @var{tab}s on output. Optional argument @var{out-tabchar}
2124 is the output tab character (default is the TAB character). Second optional
2125 argument @var{out-tabwidth} is the output tab character's width (default
2131 @opindex --join-lines
2132 Merge lines of full length. Used together with the column options
2133 @option{-@var{column}}, @option{-a -@var{column}} or @option{-m}. Turns off
2134 @option{-W/-w} line truncation;
2135 no column alignment used; may be used with
2136 @option{--sep-string[=@var{string}]}. @option{-J} has been introduced
2137 (together with @option{-W} and @option{--sep-string})
2138 to disentangle the old (@acronym{POSIX}-compliant) options @option{-w} and
2139 @option{-s} along with the three column options.
2142 @item -l @var{page_length}
2143 @itemx --length=@var{page_length}
2146 Set the page length to @var{page_length} (default 66) lines, including
2147 the lines of the header [and the footer]. If @var{page_length} is less
2148 than or equal to 10 (or <= 3 with @option{-F}), the header and footer are
2149 omitted, and all form feeds set in input files are eliminated, as if
2150 the @option{-T} option had been given.
2156 Merge and print all @var{file}s in parallel, one in each column. If a
2157 line is too long to fit in a column, it is truncated, unless the @option{-J}
2158 option is used. @option{--sep-string[=@var{string}]} may be used.
2160 some @var{file}s (form feeds set) produce empty columns, still marked
2161 by @var{string}. The result is a continuous line numbering and column
2162 marking throughout the whole merged file. Completely empty merged pages
2163 show no separators or line numbers. The default header becomes
2164 @samp{@var{date} @var{page}} with spaces inserted in the middle; this
2165 may be used with the @option{-h} or @option{--header} option to fill up
2166 the middle blank part.
2168 @item -n[@var{number-separator}[@var{digits}]]
2169 @itemx --number-lines[=@var{number-separator}[@var{digits}]]
2171 @opindex --number-lines
2172 Provide @var{digits} digit line numbering (default for @var{digits} is
2173 5). With multicolumn output the number occupies the first @var{digits}
2174 column positions of each text column or only each line of @option{-m}
2175 output. With single column output the number precedes each line just as
2176 @option{-m} does. Default counting of the line numbers starts with the
2177 first line of the input file (not the first line printed, compare the
2178 @option{--page} option and @option{-N} option).
2179 Optional argument @var{number-separator} is the character appended to
2180 the line number to separate it from the text followed. The default
2181 separator is the TAB character. In a strict sense a TAB is always
2182 printed with single column output only. The @var{TAB}-width varies
2183 with the @var{TAB}-position, e.g., with the left @var{margin} specified
2184 by @option{-o} option. With multicolumn output priority is given to
2185 @samp{equal width of output columns} (a @acronym{POSIX} specification).
2186 The @var{TAB}-width is fixed to the value of the first column and does
2187 not change with different values of left @var{margin}. That means a
2188 fixed number of spaces is always printed in the place of the
2189 @var{number-separator tab}. The tabification depends upon the output
2192 @item -N @var{line_number}
2193 @itemx --first-line-number=@var{line_number}
2195 @opindex --first-line-number
2196 Start line counting with the number @var{line_number} at first line of
2197 first page printed (in most cases not the first line of the input file).
2199 @item -o @var{margin}
2200 @itemx --indent=@var{margin}
2203 @cindex indenting lines
2205 Indent each line with a margin @var{margin} spaces wide (default is zero).
2206 The total page width is the size of the margin plus the @var{page_width}
2207 set with the @option{-W/-w} option. A limited overflow may occur with
2208 numbered single column output (compare @option{-n} option).
2211 @itemx --no-file-warnings
2213 @opindex --no-file-warnings
2214 Do not print a warning message when an argument @var{file} cannot be
2215 opened. (The exit status will still be nonzero, however.)
2217 @item -s[@var{char}]
2218 @itemx --separator[=@var{char}]
2220 @opindex --separator
2221 Separate columns by a single character @var{char}. The default for
2222 @var{char} is the TAB character without @option{-w} and @samp{no
2223 character} with @option{-w}. Without @option{-s} the default separator
2224 @samp{space} is set. @option{-s[char]} turns off line truncation of all
2225 three column options (@option{-COLUMN}|@option{-a -COLUMN}|@option{-m}) unless
2226 @option{-w} is set. This is a @acronym{POSIX}-compliant formulation.
2229 @item -S@var{string}
2230 @itemx --sep-string[=@var{string}]
2232 @opindex --sep-string
2233 Use @var{string} to separate output columns. The @option{-S} option doesn't
2234 affect the @option{-W/-w} option, unlike the @option{-s} option which does. It
2235 does not affect line truncation or column alignment.
2236 Without @option{-S}, and with @option{-J}, @command{pr} uses the default output
2238 Without @option{-S} or @option{-J}, @command{pr} uses a @samp{space}
2239 (same as @option{-S"@w{ }"}). @option{--sep-string} with no
2240 @samp{=@var{string}} is equivalent to @option{--sep-string=""}.
2243 @itemx --omit-header
2245 @opindex --omit-header
2246 Do not print the usual header [and footer] on each page, and do not fill
2247 out the bottom of pages (with blank lines or a form feed). No page
2248 structure is produced, but form feeds set in the input files are retained.
2249 The predefined pagination is not changed. @option{-t} or @option{-T} may be
2250 useful together with other options; e.g.: @option{-t -e4}, expand TAB characters
2251 in the input file to 4 spaces but don't make any other changes. Use of
2252 @option{-t} overrides @option{-h}.
2255 @itemx --omit-pagination
2257 @opindex --omit-pagination
2258 Do not print header [and footer]. In addition eliminate all form feeds
2259 set in the input files.
2262 @itemx --show-nonprinting
2264 @opindex --show-nonprinting
2265 Print nonprinting characters in octal backslash notation.
2267 @item -w @var{page_width}
2268 @itemx --width=@var{page_width}
2271 Set page width to @var{page_width} characters for multiple text-column
2272 output only (default for @var{page_width} is 72). @option{-s[CHAR]} turns
2273 off the default page width and any line truncation and column alignment.
2274 Lines of full length are merged, regardless of the column options
2275 set. No @var{page_width} setting is possible with single column output.
2276 A @acronym{POSIX}-compliant formulation.
2278 @item -W @var{page_width}
2279 @itemx --page_width=@var{page_width}
2281 @opindex --page_width
2282 Set the page width to @var{page_width} characters. That's valid with and
2283 without a column option. Text lines are truncated, unless @option{-J}
2284 is used. Together with one of the three column options
2285 (@option{-@var{column}}, @option{-a -@var{column}} or @option{-m}) column
2286 alignment is always used. The separator options @option{-S} or @option{-s}
2287 don't affect the @option{-W} option. Default is 72 characters. Without
2288 @option{-W @var{page_width}} and without any of the column options NO line
2289 truncation is used (defined to keep downward compatibility and to meet
2290 most frequent tasks). That's equivalent to @option{-W 72 -J}. The header
2291 line is never truncated.
2298 @node fold invocation
2299 @section @command{fold}: Wrap input lines to fit in specified width
2302 @cindex wrapping long input lines
2303 @cindex folding long input lines
2305 @command{fold} writes each @var{file} (@option{-} means standard input), or
2306 standard input if none are given, to standard output, breaking long
2310 fold [@var{option}]@dots{} [@var{file}]@dots{}
2313 By default, @command{fold} breaks lines wider than 80 columns. The output
2314 is split into as many lines as necessary.
2316 @cindex screen columns
2317 @command{fold} counts screen columns by default; thus, a tab may count more
2318 than one column, backspace decreases the column count, and carriage
2319 return sets the column to zero.
2321 The program accepts the following options. Also see @ref{Common options}.
2329 Count bytes rather than columns, so that tabs, backspaces, and carriage
2330 returns are each counted as taking up one column, just like other
2337 Break at word boundaries: the line is broken after the last blank before
2338 the maximum line length. If the line contains no such blanks, the line
2339 is broken at the maximum line length as usual.
2341 @item -w @var{width}
2342 @itemx --width=@var{width}
2345 Use a maximum line length of @var{width} columns instead of 80.
2347 For compatibility @command{fold} supports an obsolete option syntax
2348 @option{-@var{width}}. New scripts should use @option{-w @var{width}}
2356 @node Output of parts of files
2357 @chapter Output of parts of files
2359 @cindex output of parts of files
2360 @cindex parts of files, output of
2362 These commands output pieces of the input.
2365 * head invocation:: Output the first part of files.
2366 * tail invocation:: Output the last part of files.
2367 * split invocation:: Split a file into fixed-size pieces.
2368 * csplit invocation:: Split a file into context-determined pieces.
2371 @node head invocation
2372 @section @command{head}: Output the first part of files
2375 @cindex initial part of files, outputting
2376 @cindex first part of files, outputting
2378 @command{head} prints the first part (10 lines by default) of each
2379 @var{file}; it reads from standard input if no files are given or
2380 when given a @var{file} of @option{-}. Synopsis:
2383 head [@var{option}]@dots{} [@var{file}]@dots{}
2386 If more than one @var{file} is specified, @command{head} prints a
2387 one-line header consisting of:
2390 ==> @var{file name} <==
2394 before the output for each @var{file}.
2396 The program accepts the following options. Also see @ref{Common options}.
2401 @itemx --bytes=@var{n}
2404 Print the first @var{n} bytes, instead of initial lines. Appending
2405 @samp{b} multiplies @var{n} by 512, @samp{k} by 1024, and @samp{m}
2407 However, if @var{n} starts with a @samp{-},
2408 print all but the last @var{n} bytes of each file.
2411 @itemx --lines=@var{n}
2414 Output the first @var{n} lines.
2415 However, if @var{n} starts with a @samp{-},
2416 print all but the last @var{n} lines of each file.
2424 Never print file name headers.
2430 Always print file name headers.
2434 For compatibility @command{head} also supports an obsolete option syntax
2435 @option{-@var{count}@var{options}}, which is recognized only if it is
2436 specified first. @var{count} is a decimal number optionally followed
2437 by a size letter (@samp{b}, @samp{k}, @samp{m}) as in @option{-c}, or
2438 @samp{l} to mean count by lines, or other option letters (@samp{cqv}).
2439 Scripts intended for standard hosts should use @option{-c @var{count}}
2440 or @option{-n @var{count}} instead. If your script must also run on
2441 hosts that support only the obsolete syntax, it is usually simpler to
2442 avoid @command{head}, e.g., by using @samp{sed 5q} instead of
2448 @node tail invocation
2449 @section @command{tail}: Output the last part of files
2452 @cindex last part of files, outputting
2454 @command{tail} prints the last part (10 lines by default) of each
2455 @var{file}; it reads from standard input if no files are given or
2456 when given a @var{file} of @samp{-}. Synopsis:
2459 tail [@var{option}]@dots{} [@var{file}]@dots{}
2462 If more than one @var{file} is specified, @command{tail} prints a
2463 one-line header consisting of:
2466 ==> @var{file name} <==
2470 before the output for each @var{file}.
2472 @cindex BSD @command{tail}
2473 @sc{gnu} @command{tail} can output any amount of data (some other versions of
2474 @command{tail} cannot). It also has no @option{-r} option (print in
2475 reverse), since reversing a file is really a different job from printing
2476 the end of a file; BSD @command{tail} (which is the one with @option{-r}) can
2477 only reverse files that are at most as large as its buffer, which is
2478 typically 32 KiB@. A more reliable and versatile way to reverse files is
2479 the @sc{gnu} @command{tac} command.
2481 If any option-argument is a number @var{n} starting with a @samp{+},
2482 @command{tail} begins printing with the @var{n}th item from the start of
2483 each file, instead of from the end.
2485 The program accepts the following options. Also see @ref{Common options}.
2489 @item -c @var{bytes}
2490 @itemx --bytes=@var{bytes}
2493 Output the last @var{bytes} bytes, instead of final lines. Appending
2494 @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and @samp{m}
2498 @itemx --follow[=@var{how}]
2501 @cindex growing files
2502 @vindex name @r{follow option}
2503 @vindex descriptor @r{follow option}
2504 Loop forever trying to read more characters at the end of the file,
2505 presumably because the file is growing.
2506 If more than one file is given, @command{tail} prints a header whenever it
2507 gets output from a different file, to indicate which file that output is
2510 There are two ways to specify how you'd like to track files with this option,
2511 but that difference is noticeable only when a followed file is removed or
2513 If you'd like to continue to track the end of a growing file even after
2514 it has been unlinked, use @option{--follow=descriptor}. This is the default
2515 behavior, but it is not useful if you're tracking a log file that may be
2516 rotated (removed or renamed, then reopened). In that case, use
2517 @option{--follow=name} to track the named file by reopening it periodically
2518 to see if it has been removed and recreated by some other program.
2520 No matter which method you use, if the tracked file is determined to have
2521 shrunk, @command{tail} prints a message saying the file has been truncated
2522 and resumes tracking the end of the file from the newly-determined endpoint.
2524 When a file is removed, @command{tail}'s behavior depends on whether it is
2525 following the name or the descriptor. When following by name, tail can
2526 detect that a file has been removed and gives a message to that effect,
2527 and if @option{--retry} has been specified it will continue checking
2528 periodically to see if the file reappears.
2529 When following a descriptor, tail does not detect that the file has
2530 been unlinked or renamed and issues no message; even though the file
2531 may no longer be accessible via its original name, it may still be
2534 The option values @samp{descriptor} and @samp{name} may be specified only
2535 with the long form of the option, not with @option{-f}.
2537 @vindex POSIXLY_CORRECT
2538 If @env{POSIXLY_CORRECT} is set, the @option{-f} option is ignored if
2539 no @var{file} operand is specified and standard input is a pipe.
2543 This option is the same as @option{--follow=name --retry}. That is, tail
2544 will attempt to reopen a file when it is removed. Should this fail, tail
2545 will keep trying until it becomes accessible again.
2549 This option is useful mainly when following by name (i.e., with
2550 @option{--follow=name}).
2551 Without this option, when tail encounters a file that doesn't
2552 exist or is otherwise inaccessible, it reports that fact and
2553 never checks it again.
2555 @itemx --sleep-interval=@var{number}
2556 @opindex --sleep-interval
2557 Change the number of seconds to wait between iterations (the default is 1.0).
2558 During one iteration, every specified file is checked to see if it has
2560 Historical implementations of @command{tail} have required that
2561 @var{number} be an integer. However, GNU @command{tail} accepts
2562 an arbitrary floating point number (using a period before any
2565 @itemx --pid=@var{pid}
2567 When following by name or by descriptor, you may specify the process ID,
2568 @var{pid}, of the sole writer of all @var{file} arguments. Then, shortly
2569 after that process terminates, tail will also terminate. This will
2570 work properly only if the writer and the tailing process are running on
2571 the same machine. For example, to save the output of a build in a file
2572 and to watch the file grow, if you invoke @command{make} and @command{tail}
2573 like this then the tail process will stop when your build completes.
2574 Without this option, you would have had to kill the @code{tail -f}
2578 $ make >& makerr & tail --pid=$! -f makerr
2581 If you specify a @var{pid} that is not in use or that does not correspond
2582 to the process that is writing to the tailed files, then @command{tail}
2583 may terminate long before any @var{file}s stop growing or it may not
2584 terminate until long after the real writer has terminated.
2585 Note that @option{--pid} cannot be supported on some systems; @command{tail}
2586 will print a warning if this is the case.
2588 @itemx --max-unchanged-stats=@var{n}
2589 @opindex --max-unchanged-stats
2590 When tailing a file by name, if there have been @var{n} (default
2591 n=@value{DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS}) consecutive
2592 iterations for which the file has not changed, then
2593 @code{open}/@code{fstat} the file to determine if that file name is
2594 still associated with the same device/inode-number pair as before.
2595 When following a log file that is rotated, this is approximately the
2596 number of seconds between when tail prints the last pre-rotation lines
2597 and when it prints the lines that have accumulated in the new log file.
2598 This option is meaningful only when following by name.
2601 @itemx --lines=@var{n}
2604 Output the last @var{n} lines.
2612 Never print file name headers.
2618 Always print file name headers.
2622 For compatibility @command{tail} also supports an obsolete usage
2623 @samp{tail -[@var{count}][bcl][f] [@var{file}]}, which is recognized
2624 only if it does not conflict with the usage described
2625 above. This obsolete form uses exactly one option and at most one
2626 file. In the option, @var{count} is an optional decimal number optionally
2627 followed by a size letter (@samp{b}, @samp{c}, @samp{l}) to mean count
2628 by 512-byte blocks, bytes, or lines, optionally followed by @samp{f}
2629 which has the same meaning as @option{-f}.
2631 @vindex _POSIX2_VERSION
2632 On older systems, the leading @samp{-} can be replaced by @samp{+} in
2633 the obsolete option syntax with the same meaning as in counts, and
2634 obsolete usage overrides normal usage when the two conflict.
2635 This obsolete behavior can be enabled or disabled with the
2636 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
2639 Scripts intended for use on standard hosts should avoid obsolete
2640 syntax and should use @option{-c @var{count}[b]}, @option{-n
2641 @var{count}}, and/or @option{-f} instead. If your script must also
2642 run on hosts that support only the obsolete syntax, you can often
2643 rewrite it to avoid problematic usages, e.g., by using @samp{sed -n
2644 '$p'} rather than @samp{tail -1}. If that's not possible, the script
2645 can use a test like @samp{if tail -c +1 </dev/null >/dev/null 2>&1;
2646 then @dots{}} to decide which syntax to use.
2648 Even if your script assumes the standard behavior, you should still
2649 beware usages whose behaviors differ depending on the @acronym{POSIX}
2650 version. For example, avoid @samp{tail - main.c}, since it might be
2651 interpreted as either @samp{tail main.c} or as @samp{tail -- -
2652 main.c}; avoid @samp{tail -c 4}, since it might mean either @samp{tail
2653 -c4} or @samp{tail -c 10 4}; and avoid @samp{tail +4}, since it might
2654 mean either @samp{tail ./+4} or @samp{tail -n +4}.
2659 @node split invocation
2660 @section @command{split}: Split a file into fixed-size pieces
2663 @cindex splitting a file into pieces
2664 @cindex pieces, splitting a file into
2666 @command{split} creates output files containing consecutive sections of
2667 @var{input} (standard input if none is given or @var{input} is
2668 @samp{-}). Synopsis:
2671 split [@var{option}] [@var{input} [@var{prefix}]]
2674 By default, @command{split} puts 1000 lines of @var{input} (or whatever is
2675 left over for the last section), into each output file.
2677 @cindex output file name prefix
2678 The output files' names consist of @var{prefix} (@samp{x} by default)
2679 followed by a group of characters (@samp{aa}, @samp{ab}, @dots{} by
2680 default), such that concatenating the output files in traditional
2681 sorted order by file name produces
2682 the original input file. If the output file names are exhausted,
2683 @command{split} reports an error without deleting the output files
2686 The program accepts the following options. Also see @ref{Common options}.
2690 @item -a @var{length}
2691 @itemx --suffix-length=@var{length}
2693 @opindex --suffix-length
2694 Use suffixes of length @var{length}. The default @var{length} is 2.
2696 @item -l @var{lines}
2697 @itemx --lines=@var{lines}
2700 Put @var{lines} lines of @var{input} into each output file.
2702 For compatibility @command{split} also supports an obsolete
2703 option syntax @option{-@var{lines}}. New scripts should use @option{-l
2704 @var{lines}} instead.
2706 @item -b @var{bytes}
2707 @itemx --bytes=@var{bytes}
2710 Put the first @var{bytes} bytes of @var{input} into each output file.
2711 Appending @samp{b} multiplies @var{bytes} by 512, @samp{k} by 1024, and
2712 @samp{m} by 1048576.
2714 @item -C @var{bytes}
2715 @itemx --line-bytes=@var{bytes}
2717 @opindex --line-bytes
2718 Put into each output file as many complete lines of @var{input} as
2719 possible without exceeding @var{bytes} bytes. For lines longer than
2720 @var{bytes} bytes, put @var{bytes} bytes into each output file until
2721 less than @var{bytes} bytes of the line are left, then continue
2722 normally. @var{bytes} has the same format as for the @option{--bytes}
2726 @itemx --numeric-suffixes
2728 @opindex --numeric-suffixes
2729 Use digits in suffixes rather than lower-case letters.
2733 Write a diagnostic to standard error just before each output file is opened.
2740 @node csplit invocation
2741 @section @command{csplit}: Split a file into context-determined pieces
2744 @cindex context splitting
2745 @cindex splitting a file into pieces by context
2747 @command{csplit} creates zero or more output files containing sections of
2748 @var{input} (standard input if @var{input} is @samp{-}). Synopsis:
2751 csplit [@var{option}]@dots{} @var{input} @var{pattern}@dots{}
2754 The contents of the output files are determined by the @var{pattern}
2755 arguments, as detailed below. An error occurs if a @var{pattern}
2756 argument refers to a nonexistent line of the input file (e.g., if no
2757 remaining line matches a given regular expression). After every
2758 @var{pattern} has been matched, any remaining input is copied into one
2761 By default, @command{csplit} prints the number of bytes written to each
2762 output file after it has been created.
2764 The types of pattern arguments are:
2769 Create an output file containing the input up to but not including line
2770 @var{n} (a positive integer). If followed by a repeat count, also
2771 create an output file containing the next @var{n} lines of the input
2772 file once for each repeat.
2774 @item /@var{regexp}/[@var{offset}]
2775 Create an output file containing the current line up to (but not
2776 including) the next line of the input file that contains a match for
2777 @var{regexp}. The optional @var{offset} is an integer.
2778 If it is given, the input up to (but not including) the
2779 matching line plus or minus @var{offset} is put into the output file,
2780 and the line after that begins the next section of input.
2782 @item %@var{regexp}%[@var{offset}]
2783 Like the previous type, except that it does not create an output
2784 file, so that section of the input file is effectively ignored.
2786 @item @{@var{repeat-count}@}
2787 Repeat the previous pattern @var{repeat-count} additional
2788 times. The @var{repeat-count} can either be a positive integer or an
2789 asterisk, meaning repeat as many times as necessary until the input is
2794 The output files' names consist of a prefix (@samp{xx} by default)
2795 followed by a suffix. By default, the suffix is an ascending sequence
2796 of two-digit decimal numbers from @samp{00} to @samp{99}. In any case,
2797 concatenating the output files in sorted order by file name produces the
2798 original input file.
2800 By default, if @command{csplit} encounters an error or receives a hangup,
2801 interrupt, quit, or terminate signal, it removes any output files
2802 that it has created so far before it exits.
2804 The program accepts the following options. Also see @ref{Common options}.
2808 @item -f @var{prefix}
2809 @itemx --prefix=@var{prefix}
2812 @cindex output file name prefix
2813 Use @var{prefix} as the output file name prefix.
2815 @item -b @var{suffix}
2816 @itemx --suffix=@var{suffix}
2819 @cindex output file name suffix
2820 Use @var{suffix} as the output file name suffix. When this option is
2821 specified, the suffix string must include exactly one
2822 @code{printf(3)}-style conversion specification, possibly including
2823 format specification flags, a field width, a precision specifications,
2824 or all of these kinds of modifiers. The format letter must convert a
2825 binary integer argument to readable form; thus, only @samp{d}, @samp{i},
2826 @samp{u}, @samp{o}, @samp{x}, and @samp{X} conversions are allowed. The
2827 entire @var{suffix} is given (with the current output file number) to
2828 @code{sprintf(3)} to form the file name suffixes for each of the
2829 individual output files in turn. If this option is used, the
2830 @option{--digits} option is ignored.
2832 @item -n @var{digits}
2833 @itemx --digits=@var{digits}
2836 Use output file names containing numbers that are @var{digits} digits
2837 long instead of the default 2.
2842 @opindex --keep-files
2843 Do not remove output files when errors are encountered.
2846 @itemx --elide-empty-files
2848 @opindex --elide-empty-files
2849 Suppress the generation of zero-length output files. (In cases where
2850 the section delimiters of the input file are supposed to mark the first
2851 lines of each of the sections, the first output file will generally be a
2852 zero-length file unless you use this option.) The output file sequence
2853 numbers always run consecutively starting from 0, even when this option
2864 Do not print counts of output file sizes.
2871 @node Summarizing files
2872 @chapter Summarizing files
2874 @cindex summarizing files
2876 These commands generate just a few numbers representing entire
2880 * wc invocation:: Print newline, word, and byte counts.
2881 * sum invocation:: Print checksum and block counts.
2882 * cksum invocation:: Print CRC checksum and byte counts.
2883 * md5sum invocation:: Print or check MD5 digests.
2884 * sha1sum invocation:: Print or check SHA-1 digests.
2885 * sha2 utilities:: Print or check SHA-2 digests.
2890 @section @command{wc}: Print newline, word, and byte counts
2894 @cindex character count
2898 @command{wc} counts the number of bytes, characters, whitespace-separated
2899 words, and newlines in each given @var{file}, or standard input if none
2900 are given or for a @var{file} of @samp{-}. Synopsis:
2903 wc [@var{option}]@dots{} [@var{file}]@dots{}
2906 @cindex total counts
2907 @command{wc} prints one line of counts for each file, and if the file was
2908 given as an argument, it prints the file name following the counts. If
2909 more than one @var{file} is given, @command{wc} prints a final line
2910 containing the cumulative counts, with the file name @file{total}. The
2911 counts are printed in this order: newlines, words, characters, bytes.
2912 Each count is printed right-justified in a field with at least one
2913 space between fields so that the numbers and file names normally line
2914 up nicely in columns. The width of the count fields varies depending
2915 on the inputs, so you should not depend on a particular field width.
2916 However, as a @acronym{GNU} extension, if only one count is printed,
2917 it is guaranteed to be printed without leading spaces.
2919 By default, @command{wc} prints three counts: the newline, words, and byte
2920 counts. Options can specify that only certain counts be printed.
2921 Options do not undo others previously given, so
2928 prints both the byte counts and the word counts.
2930 With the @option{--max-line-length} option, @command{wc} prints the length
2931 of the longest line per file, and if there is more than one file it
2932 prints the maximum (not the sum) of those lengths.
2934 The program accepts the following options. Also see @ref{Common options}.
2942 Print only the byte counts.
2948 Print only the character counts.
2954 Print only the word counts.
2960 Print only the newline counts.
2963 @itemx --max-line-length
2965 @opindex --max-line-length
2966 Print only the maximum line lengths.
2968 @itemx --files0-from=@var{FILE}
2969 @opindex --files0-from=@var{FILE}
2970 @cindex including files from @command{du}
2971 Rather than processing files named on the command line, process those
2972 named in file @var{FILE}; each name is terminated by a null byte.
2974 the list of file names is so long that it may exceed a command line
2976 In such cases, running @command{wc} via @command{xargs} is undesirable
2977 because it splits the list into pieces and makes @command{wc} print a
2978 total for each sublist rather than for the entire list.
2979 One way to produce a list of null-byte-terminated file names is with @sc{gnu}
2980 @command{find}, using its @option{-print0} predicate. For example, to find
2981 the length of the longest line in any @file{.c} or @file{.h} file in the
2982 current hierarchy, do this:
2985 find . -name '*.[ch]' -print0 | wc -L --files0-from=- | tail -n1
2988 Do not specify any @var{FILE} on the command line when using this option.
2995 @node sum invocation
2996 @section @command{sum}: Print checksum and block counts
2999 @cindex 16-bit checksum
3000 @cindex checksum, 16-bit
3002 @command{sum} computes a 16-bit checksum for each given @var{file}, or
3003 standard input if none are given or for a @var{file} of @samp{-}. Synopsis:
3006 sum [@var{option}]@dots{} [@var{file}]@dots{}
3009 @command{sum} prints the checksum for each @var{file} followed by the
3010 number of blocks in the file (rounded up). If more than one @var{file}
3011 is given, file names are also printed (by default). (With the
3012 @option{--sysv} option, corresponding file names are printed when there is
3013 at least one file argument.)
3015 By default, @sc{gnu} @command{sum} computes checksums using an algorithm
3016 compatible with BSD @command{sum} and prints file sizes in units of
3019 The program accepts the following options. Also see @ref{Common options}.
3025 @cindex BSD @command{sum}
3026 Use the default (BSD compatible) algorithm. This option is included for
3027 compatibility with the System V @command{sum}. Unless @option{-s} was also
3028 given, it has no effect.
3034 @cindex System V @command{sum}
3035 Compute checksums using an algorithm compatible with System V
3036 @command{sum}'s default, and print file sizes in units of 512-byte blocks.
3040 @command{sum} is provided for compatibility; the @command{cksum} program (see
3041 next section) is preferable in new applications.
3046 @node cksum invocation
3047 @section @command{cksum}: Print CRC checksum and byte counts
3050 @cindex cyclic redundancy check
3051 @cindex CRC checksum
3053 @command{cksum} computes a cyclic redundancy check (CRC) checksum for each
3054 given @var{file}, or standard input if none are given or for a
3055 @var{file} of @samp{-}. Synopsis:
3058 cksum [@var{option}]@dots{} [@var{file}]@dots{}
3061 @command{cksum} prints the CRC checksum for each file along with the number
3062 of bytes in the file, and the file name unless no arguments were given.
3064 @command{cksum} is typically used to ensure that files
3065 transferred by unreliable means (e.g., netnews) have not been corrupted,
3066 by comparing the @command{cksum} output for the received files with the
3067 @command{cksum} output for the original files (typically given in the
3070 The CRC algorithm is specified by the @acronym{POSIX} standard. It is not
3071 compatible with the BSD or System V @command{sum} algorithms (see the
3072 previous section); it is more robust.
3074 The only options are @option{--help} and @option{--version}. @xref{Common
3080 @node md5sum invocation
3081 @section @command{md5sum}: Print or check MD5 digests
3085 @cindex 128-bit checksum
3086 @cindex checksum, 128-bit
3087 @cindex fingerprint, 128-bit
3088 @cindex message-digest, 128-bit
3090 @command{md5sum} computes a 128-bit checksum (or @dfn{fingerprint} or
3091 @dfn{message-digest}) for each specified @var{file}.
3093 Note: The MD5 digest is more reliable than a simple CRC (provided by
3094 the @command{cksum} command) for detecting accidental file corruption,
3095 as the chances of accidentally having two files with identical MD5
3096 are vanishingly small. However, it should not be considered truly
3097 secure against malicious tampering: although finding a file with a
3098 given MD5 fingerprint, or modifying a file so as to retain its MD5 are
3099 considered infeasible at the moment, it is known how to produce
3100 different files with identical MD5 (a ``collision''), something which
3101 can be a security issue in certain contexts. For more secure hashes,
3102 consider using SHA-1 or SHA-2. @xref{sha1sum invocation}, and
3103 @ref{sha2 utilities}.
3105 If a @var{file} is specified as @samp{-} or if no files are given
3106 @command{md5sum} computes the checksum for the standard input.
3107 @command{md5sum} can also determine whether a file and checksum are
3108 consistent. Synopsis:
3111 md5sum [@var{option}]@dots{} [@var{file}]@dots{}
3114 For each @var{file}, @samp{md5sum} outputs the MD5 checksum, a flag
3115 indicating a binary or text input file, and the file name.
3116 If @var{file} is omitted or specified as @samp{-}, standard input is read.
3118 The program accepts the following options. Also see @ref{Common options}.
3126 @cindex binary input files
3127 Treat each input file as binary, by reading it in binary mode and
3128 outputting a @samp{*} flag. This is the inverse of @option{--text}.
3129 On systems like @acronym{GNU} that do not distinguish between binary
3130 and text files, this option merely flags each input file as binary:
3131 the MD5 checksum is unaffected. This option is the default on systems
3132 like MS-DOS that distinguish between binary and text files, except
3133 for reading standard input when standard input is a terminal.
3137 Read file names and checksum information (not data) from each
3138 @var{file} (or from stdin if no @var{file} was specified) and report
3139 whether the checksums match the contents of the named files.
3140 The input to this mode of @command{md5sum} is usually the output of
3141 a prior, checksum-generating run of @samp{md5sum}.
3142 Each valid line of input consists of an MD5 checksum, a binary/text
3143 flag, and then a file name.
3144 Binary files are marked with @samp{*}, text with @samp{ }.
3145 For each such line, @command{md5sum} reads the named file and computes its
3146 MD5 checksum. Then, if the computed message digest does not match the
3147 one on the line with the file name, the file is noted as having
3148 failed the test. Otherwise, the file passes the test.
3149 By default, for each valid line, one line is written to standard
3150 output indicating whether the named file passed the test.
3151 After all checks have been performed, if there were any failures,
3152 a warning is issued to standard error.
3153 Use the @option{--status} option to inhibit that output.
3154 If any listed file cannot be opened or read, if any valid line has
3155 an MD5 checksum inconsistent with the associated file, or if no valid
3156 line is found, @command{md5sum} exits with nonzero status. Otherwise,
3157 it exits successfully.
3161 @cindex verifying MD5 checksums
3162 This option is useful only when verifying checksums.
3163 When verifying checksums, don't generate the default one-line-per-file
3164 diagnostic and don't output the warning summarizing any failures.
3165 Failures to open or read a file still evoke individual diagnostics to
3167 If all listed files are readable and are consistent with the associated
3168 MD5 checksums, exit successfully. Otherwise exit with a status code
3169 indicating there was a failure.
3175 @cindex text input files
3176 Treat each input file as text, by reading it in text mode and
3177 outputting a @samp{ } flag. This is the inverse of @option{--binary}.
3178 This option is the default on systems like @acronym{GNU} that do not
3179 distinguish between binary and text files. On other systems, it is
3180 the default for reading standard input when standard input is a
3187 @cindex verifying MD5 checksums
3188 When verifying checksums, warn about improperly formatted MD5 checksum lines.
3189 This option is useful only if all but a few lines in the checked input
3197 @node sha1sum invocation
3198 @section @command{sha1sum}: Print or check SHA-1 digests
3202 @cindex 160-bit checksum
3203 @cindex checksum, 160-bit
3204 @cindex fingerprint, 160-bit
3205 @cindex message-digest, 160-bit
3207 @command{sha1sum} computes a 160-bit checksum for each specified
3208 @var{file}. The usage and options of this command are precisely the
3209 same as for @command{md5sum}. @xref{md5sum invocation}.
3211 Note: The SHA-1 digest is more secure than MD5, and no collisions of
3212 it are known (different files having the same fingerprint). However,
3213 it is known that they can be produced with considerable, but not
3214 unreasonable, resources. For this reason, it is generally considered
3215 that SHA-1 should be gradually phased out in favor of the more secure
3216 SHA-2 hash algorithms. @xref{sha2 utilities}.
3219 @node sha2 utilities
3220 @section sha2 utilities: Print or check SHA-2 digests
3227 @cindex 224-bit checksum
3228 @cindex 256-bit checksum
3229 @cindex 384-bit checksum
3230 @cindex 512-bit checksum
3231 @cindex checksum, 224-bit
3232 @cindex checksum, 256-bit
3233 @cindex checksum, 384-bit
3234 @cindex checksum, 512-bit
3235 @cindex fingerprint, 224-bit
3236 @cindex fingerprint, 256-bit
3237 @cindex fingerprint, 384-bit
3238 @cindex fingerprint, 512-bit
3239 @cindex message-digest, 224-bit
3240 @cindex message-digest, 256-bit
3241 @cindex message-digest, 384-bit
3242 @cindex message-digest, 512-bit
3244 The commands @command{sha224sum}, @command{sha256sum},
3245 @command{sha384sum} and @command{sha512sum} compute checksums of
3246 various lengths (respectively 224, 256, 384 and 512 bits),
3247 collectively known as the SHA-2 hashes. The usage and options of
3248 these commands are precisely the same as for @command{md5sum}.
3249 @xref{md5sum invocation}.
3251 Note: The SHA384 and SHA512 digests are considerably slower to
3252 compute, especially on 32-bit computers, than SHA224 or SHA256.
3255 @node Operating on sorted files
3256 @chapter Operating on sorted files
3258 @cindex operating on sorted files
3259 @cindex sorted files, operations on
3261 These commands work with (or produce) sorted files.
3264 * sort invocation:: Sort text files.
3265 * uniq invocation:: Uniquify files.
3266 * comm invocation:: Compare two sorted files line by line.
3267 * ptx invocation:: Produce a permuted index of file contents.
3268 * tsort invocation:: Topological sort.
3269 * tsort background:: Where tsort came from.
3273 @node sort invocation
3274 @section @command{sort}: Sort text files
3277 @cindex sorting files
3279 @command{sort} sorts, merges, or compares all the lines from the given
3280 files, or standard input if none are given or for a @var{file} of
3281 @samp{-}. By default, @command{sort} writes the results to standard
3285 sort [@var{option}]@dots{} [@var{file}]@dots{}
3288 @command{sort} has three modes of operation: sort (the default), merge,
3289 and check for sortedness. The following options change the operation
3298 @cindex checking for sortedness
3299 Check whether the given files are already sorted: if they are not all
3300 sorted, print an error message and exit with a status of 1.
3301 Otherwise, exit successfully.
3307 @cindex merging sorted files
3308 Merge the given files by sorting them as a group. Each input file must
3309 always be individually sorted. It always works to sort instead of
3310 merge; merging is provided because it is faster, in the case where it
3315 @cindex sort stability
3316 @cindex sort's last-resort comparison
3317 A pair of lines is compared as follows:
3318 @command{sort} compares each pair of fields, in the
3319 order specified on the command line, according to the associated
3320 ordering options, until a difference is found or no fields are left.
3321 If no key fields are specified, @command{sort} uses a default key of
3322 the entire line. Finally, as a last resort when all keys compare
3323 equal, @command{sort} compares entire lines as if no ordering options
3324 other than @option{--reverse} (@option{-r}) were specified. The
3325 @option{--stable} (@option{-s}) option disables this @dfn{last-resort
3326 comparison} so that lines in which all fields compare equal are left
3327 in their original relative order. The @option{--unique}
3328 (@option{-u}) option also disables the last-resort comparison.
3332 Unless otherwise specified, all comparisons use the character collating
3333 sequence specified by the @env{LC_COLLATE} locale.@footnote{If you
3334 use a non-@acronym{POSIX} locale (e.g., by setting @env{LC_ALL}
3335 to @samp{en_US}), then @command{sort} may produce output that is sorted
3336 differently than you're accustomed to. In that case, set the @env{LC_ALL}
3337 environment variable to @samp{C}. Note that setting only @env{LC_COLLATE}
3338 has two problems. First, it is ineffective if @env{LC_ALL} is also set.
3339 Second, it has undefined behavior if @env{LC_CTYPE} (or @env{LANG}, if
3340 @env{LC_CTYPE} is unset) is set to an incompatible value. For example,
3341 you get undefined behavior if @env{LC_CTYPE} is @code{ja_JP.PCK} but
3342 @env{LC_COLLATE} is @code{en_US.UTF-8}.}
3344 @sc{gnu} @command{sort} (as specified for all @sc{gnu} utilities) has no
3345 limit on input line length or restrictions on bytes allowed within lines.
3346 In addition, if the final byte of an input file is not a newline, @sc{gnu}
3347 @command{sort} silently supplies one. A line's trailing newline is not
3348 part of the line for comparison purposes.
3350 @cindex exit status of @command{sort}
3354 0 if no error occurred
3355 1 if invoked with @option{-c} and the input is not properly sorted
3356 2 if an error occurred
3360 If the environment variable @env{TMPDIR} is set, @command{sort} uses its
3361 value as the directory for temporary files instead of @file{/tmp}. The
3362 @option{--temporary-directory} (@option{-T}) option in turn overrides
3363 the environment variable.
3366 The following options affect the ordering of output lines. They may be
3367 specified globally or as part of a specific key field. If no key
3368 fields are specified, global options apply to comparison of entire
3369 lines; otherwise the global options are inherited by key fields that do
3370 not specify any special options of their own. In pre-@acronym{POSIX}
3371 versions of @command{sort}, global options affect only later key fields,
3372 so portable shell scripts should specify global options first.
3377 @itemx --ignore-leading-blanks
3379 @opindex --ignore-leading-blanks
3380 @cindex blanks, ignoring leading
3382 Ignore leading blanks when finding sort keys in each line.
3383 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3387 @itemx --dictionary-order
3389 @opindex --dictionary-order
3390 @cindex dictionary order
3391 @cindex phone directory order
3392 @cindex telephone directory order
3394 Sort in @dfn{phone directory} order: ignore all characters except
3395 letters, digits and blanks when sorting.
3396 By default letters and digits are those of @acronym{ASCII} and a blank
3397 is a space or a tab, but the @env{LC_CTYPE} locale can change this.
3400 @itemx --ignore-case
3402 @opindex --ignore-case
3403 @cindex ignoring case
3404 @cindex case folding
3406 Fold lowercase characters into the equivalent uppercase characters when
3407 comparing so that, for example, @samp{b} and @samp{B} sort as equal.
3408 The @env{LC_CTYPE} locale determines character types.
3411 @itemx --general-numeric-sort
3413 @opindex --general-numeric-sort
3414 @cindex general numeric sort
3416 Sort numerically, using the standard C function @code{strtod} to convert
3417 a prefix of each line to a double-precision floating point number.
3418 This allows floating point numbers to be specified in scientific notation,
3419 like @code{1.0e-34} and @code{10e100}.
3420 The @env{LC_NUMERIC} locale determines the decimal-point character.
3421 Do not report overflow, underflow, or conversion errors.
3422 Use the following collating sequence:
3426 Lines that do not start with numbers (all considered to be equal).
3428 NaNs (``Not a Number'' values, in IEEE floating point arithmetic)
3429 in a consistent but machine-dependent order.
3433 Finite numbers in ascending numeric order (with @math{-0} and @math{+0} equal).
3438 Use this option only if there is no alternative; it is much slower than
3439 @option{--numeric-sort} (@option{-n}) and it can lose information when
3440 converting to floating point.
3443 @itemx --ignore-nonprinting
3445 @opindex --ignore-nonprinting
3446 @cindex nonprinting characters, ignoring
3447 @cindex unprintable characters, ignoring
3449 Ignore nonprinting characters.
3450 The @env{LC_CTYPE} locale determines character types.
3451 This option has no effect if the stronger @option{--dictionary-order}
3452 (@option{-d}) option is also given.
3457 @opindex --month-sort
3458 @cindex months, sorting by
3460 An initial string, consisting of any amount of blanks, followed
3461 by a month name abbreviation, is folded to UPPER case and
3462 compared in the order @samp{JAN} < @samp{FEB} < @dots{} < @samp{DEC}.
3463 Invalid names compare low to valid names. The @env{LC_TIME} locale
3464 category determines the month spellings.
3465 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3469 @itemx --numeric-sort
3471 @opindex --numeric-sort
3472 @cindex numeric sort
3474 Sort numerically: the number begins each line; specifically, it consists
3475 of optional blanks, an optional @samp{-} sign, and zero or more
3476 digits possibly separated by thousands separators, optionally followed
3477 by a decimal-point character and zero or more digits. A string of
3478 no digits is interpreted as @samp{0}. The @env{LC_NUMERIC}
3479 locale specifies the decimal-point character and thousands separator.
3480 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3483 Numeric sort uses what might be considered an unconventional method to
3484 compare strings representing floating point numbers. Rather than first
3485 converting each string to the C @code{double} type and then comparing
3486 those values, @command{sort} aligns the decimal-point characters in the
3487 two strings and compares the strings a character at a time. One benefit
3488 of using this approach is its speed. In practice this is much more
3489 efficient than performing the two corresponding string-to-double (or
3490 even string-to-integer) conversions and then comparing doubles. In
3491 addition, there is no corresponding loss of precision. Converting each
3492 string to @code{double} before comparison would limit precision to about
3493 16 digits on most systems.
3495 Neither a leading @samp{+} nor exponential notation is recognized.
3496 To compare such strings numerically, use the
3497 @option{--general-numeric-sort} (@option{-g}) option.
3503 @cindex reverse sorting
3504 Reverse the result of comparison, so that lines with greater key values
3505 appear earlier in the output instead of later.
3508 @itemx --random-sort
3510 @opindex --random-sort
3512 Sort by hashing the input keys and then sorting the hash values. This
3513 is much like a random shuffle of the inputs, except that keys with the
3514 same value sort together. The hash function is chosen at random.
3522 @item -k @var{pos1}[,@var{pos2}]
3523 @itemx --key=@var{pos1}[,@var{pos2}]
3527 Specify a sort field that consists of the part of the line between
3528 @var{pos1} and @var{pos2} (or the end of the line, if @var{pos2} is
3529 omitted), @emph{inclusive}. Fields and character positions are numbered
3530 starting with 1. So to sort on the second field, you'd use
3531 @option{--key=2,2} (@option{-k 2,2}). See below for more examples.
3533 @item -o @var{output-file}
3534 @itemx --output=@var{output-file}
3537 @cindex overwriting of input, allowed
3538 Write output to @var{output-file} instead of standard output.
3539 Normally, @command{sort} reads all input before opening
3540 @var{output-file}, so you can safely sort a file in place by using
3541 commands like @code{sort -o F F} and @code{cat F | sort -o F}.
3542 However, @command{sort} with @option{--merge} (@option{-m}) can open
3543 the output file before reading all input, so a command like @code{cat
3544 F | sort -m -o F - G} is not safe as @command{sort} might start
3545 writing @file{F} before @command{cat} is done reading it.
3547 @vindex POSIXLY_CORRECT
3548 On newer systems, @option{-o} cannot appear after an input file if
3549 @env{POSIXLY_CORRECT} is set, e.g., @samp{sort F -o F}. Portable
3550 scripts should specify @option{-o @var{output-file}} before any input
3557 @cindex sort stability
3558 @cindex sort's last-resort comparison
3560 Make @command{sort} stable by disabling its last-resort comparison.
3561 This option has no effect if no fields or global ordering options
3562 other than @option{--reverse} (@option{-R}) are specified.
3565 @itemx --buffer-size=@var{size}
3567 @opindex --buffer-size
3568 @cindex size for main memory sorting
3569 Use a main-memory sort buffer of the given @var{size}. By default,
3570 @var{size} is in units of 1024 bytes. Appending @samp{%} causes
3571 @var{size} to be interpreted as a percentage of physical memory.
3572 Appending @samp{K} multiplies @var{size} by 1024 (the default),
3573 @samp{M} by 1,048,576, @samp{G} by 1,073,741,824, and so on for
3574 @samp{T}, @samp{P}, @samp{E}, @samp{Z}, and @samp{Y}. Appending
3575 @samp{b} causes @var{size} to be interpreted as a byte count, with no
3578 This option can improve the performance of @command{sort} by causing it
3579 to start with a larger or smaller sort buffer than the default.
3580 However, this option affects only the initial buffer size. The buffer
3581 grows beyond @var{size} if @command{sort} encounters input lines larger
3584 @item -t @var{separator}
3585 @itemx --field-separator=@var{separator}
3587 @opindex --field-separator
3588 @cindex field separator character
3589 Use character @var{separator} as the field separator when finding the
3590 sort keys in each line. By default, fields are separated by the empty
3591 string between a non-blank character and a blank character.
3592 By default a blank is a space or a tab, but the @env{LC_CTYPE} locale
3595 That is, given the input line @w{@samp{ foo bar}}, @command{sort} breaks it
3596 into fields @w{@samp{ foo}} and @w{@samp{ bar}}. The field separator is
3597 not considered to be part of either the field preceding or the field
3598 following, so with @samp{sort @w{-t " "}} the same input line has
3599 three fields: an empty field, @samp{foo}, and @samp{bar}.
3600 However, fields that extend to the end of the line,
3601 as @option{-k 2}, or fields consisting of a range, as @option{-k 2,3},
3602 retain the field separators present between the endpoints of the range.
3604 To specify a null character (@acronym{ASCII} @sc{nul}) as
3605 the field separator, use the two-character string @samp{\0}, e.g.,
3606 @samp{sort -t '\0'}.
3608 @item -T @var{tempdir}
3609 @itemx --temporary-directory=@var{tempdir}
3611 @opindex --temporary-directory
3612 @cindex temporary directory
3614 Use directory @var{tempdir} to store temporary files, overriding the
3615 @env{TMPDIR} environment variable. If this option is given more than
3616 once, temporary files are stored in all the directories given. If you
3617 have a large sort or merge that is I/O-bound, you can often improve
3618 performance by using this option to specify directories on different
3619 disks and controllers.
3625 @cindex uniquifying output
3627 Normally, output only the first of a sequence of lines that compare
3628 equal. For the @option{--check} (@option{-c}) option,
3629 check that no pair of consecutive lines compares equal.
3631 This option also disables the default last-resort comparison.
3633 The commands @code{sort -u} and @code{sort | uniq} are equivalent, but
3634 this equivalence does not extend to arbitrary @command{sort} options.
3635 For example, @code{sort -n -u} inspects only the value of the initial
3636 numeric string when checking for uniqueness, whereas @code{sort -n |
3637 uniq} inspects the entire line. @xref{uniq invocation}.
3640 @itemx --zero-terminated
3642 @opindex --zero-terminated
3643 @cindex sort zero-terminated lines
3644 Treat the input as a set of lines, each terminated by a null character
3645 (@acronym{ASCII} @sc{nul}) instead of a line feed
3646 (@acronym{ASCII} @sc{lf}).
3647 This option can be useful in conjunction with @samp{perl -0} or
3648 @samp{find -print0} and @samp{xargs -0} which do the same in order to
3649 reliably handle arbitrary file names (even those containing blanks
3650 or other special characters).
3654 Historical (BSD and System V) implementations of @command{sort} have
3655 differed in their interpretation of some options, particularly
3656 @option{-b}, @option{-f}, and @option{-n}. @sc{gnu} sort follows the @acronym{POSIX}
3657 behavior, which is usually (but not always!) like the System V behavior.
3658 According to @acronym{POSIX}, @option{-n} no longer implies @option{-b}. For
3659 consistency, @option{-M} has been changed in the same way. This may
3660 affect the meaning of character positions in field specifications in
3661 obscure cases. The only fix is to add an explicit @option{-b}.
3663 A position in a sort field specified with the @option{-k}
3664 option has the form @samp{@var{f}.@var{c}}, where @var{f} is the number
3665 of the field to use and @var{c} is the number of the first character
3666 from the beginning of the field. In a start position, an omitted
3667 @samp{.@var{c}} stands for the field's first character. In an end
3668 position, an omitted or zero @samp{.@var{c}} stands for the field's
3669 last character. If the start field falls after the end of the line
3670 or after the end field, the field is empty. If the
3671 @option{-b} option was specified, the @samp{.@var{c}} part of a field
3672 specification is counted from the first nonblank character of the field.
3674 A sort key position may also have any of the option letters @samp{Mbdfinr}
3675 appended to it, in which case the global ordering options are not used
3676 for that particular field. The @option{-b} option may be independently
3677 attached to either or both of the start and
3678 end positions of a field specification, and if it is inherited
3679 from the global options it will be attached to both.
3680 If input lines can contain leading or adjacent blanks and @option{-t}
3681 is not used, then @option{-k} is typically combined with @option{-b},
3682 @option{-g}, @option{-M}, or @option{-n}; otherwise the varying
3683 numbers of leading blanks in fields can cause confusing results.
3685 Keys can span multiple fields.
3687 @vindex _POSIX2_VERSION
3688 @vindex POSIXLY_CORRECT
3689 On older systems, @command{sort} supports an obsolete origin-zero
3690 syntax @samp{+@var{pos1} [-@var{pos2}]} for specifying sort keys.
3691 This obsolete behavior can be enabled or disabled with the
3692 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
3693 conformance}); it can also be enabled when @env{POSIXLY_CORRECT} is
3694 not set by using the obsolete syntax with @samp{-@var{pos2}} present.
3696 Scripts intended for use on standard hosts should avoid obsolete
3697 syntax and should use @option{-k} instead. For example, avoid
3698 @samp{sort +2}, since it might be interpreted as either @samp{sort
3699 ./+2} or @samp{sort -k 3}. If your script must also run on hosts that
3700 support only the obsolete syntax, it can use a test like @samp{if sort
3701 -k 1 </dev/null >/dev/null 2>&1; then @dots{}} to decide which syntax
3704 Here are some examples to illustrate various combinations of options.
3709 Sort in descending (reverse) numeric order.
3716 Sort alphabetically, omitting the first and second fields
3717 and the blanks at the start of the third field.
3718 This uses a single key composed of the characters beginning
3719 at the start of the first nonblank character in field three
3720 and extending to the end of each line.
3727 Sort numerically on the second field and resolve ties by sorting
3728 alphabetically on the third and fourth characters of field five.
3729 Use @samp{:} as the field delimiter.
3732 sort -t : -k 2,2n -k 5.3,5.4
3735 Note that if you had written @option{-k 2n} instead of @option{-k 2,2n}
3736 @command{sort} would have used all characters beginning in the second field
3737 and extending to the end of the line as the primary @emph{numeric}
3738 key. For the large majority of applications, treating keys spanning
3739 more than one field as numeric will not do what you expect.
3741 Also note that the @samp{n} modifier was applied to the field-end
3742 specifier for the first key. It would have been equivalent to
3743 specify @option{-k 2n,2} or @option{-k 2n,2n}. All modifiers except
3744 @samp{b} apply to the associated @emph{field}, regardless of whether
3745 the modifier character is attached to the field-start and/or the
3746 field-end part of the key specifier.
3749 Sort the password file on the fifth field and ignore any
3750 leading blanks. Sort lines with equal values in field five
3751 on the numeric user ID in field three. Fields are separated
3755 sort -t : -k 5b,5 -k 3,3n /etc/passwd
3756 sort -t : -n -k 5b,5 -k 3,3 /etc/passwd
3757 sort -t : -b -k 5,5 -k 3,3n /etc/passwd
3760 These three commands have equivalent effect. The first specifies that
3761 the first key's start position ignores leading blanks and the second
3762 key is sorted numerically. The other two commands rely on global
3763 options being inherited by sort keys that lack modifiers. The inheritance
3764 works in this case because @option{-k 5b,5b} and @option{-k 5b,5} are
3765 equivalent, as the location of a field-end lacking a @samp{.@var{c}}
3766 character position is not affected by whether initial blanks are
3770 Sort a set of log files, primarily by IPv4 address and secondarily by
3771 time stamp. If two lines' primary and secondary keys are identical,
3772 output the lines in the same order that they were input. The log
3773 files contain lines that look like this:
3776 4.150.156.3 - - [01/Apr/2004:06:31:51 +0000] message 1
3777 211.24.3.231 - - [24/Apr/2004:20:17:39 +0000] message 2
3780 Fields are separated by exactly one space. Sort IPv4 addresses
3781 lexicographically, e.g., 212.61.52.2 sorts before 212.129.233.201
3782 because 61 is less than 129.
3785 sort -s -t ' ' -k 4.9n -k 4.5M -k 4.2n -k 4.14,4.21 file*.log |
3786 sort -s -t '.' -k 1,1n -k 2,2n -k 3,3n -k 4,4n
3789 This example cannot be done with a single @command{sort} invocation,
3790 since IPv4 address components are separated by @samp{.} while dates
3791 come just after a space. So it is broken down into two invocations of
3792 @command{sort}: the first sorts by time stamp and the second by IPv4
3793 address. The time stamp is sorted by year, then month, then day, and
3794 finally by hour-minute-second field, using @option{-k} to isolate each
3795 field. Except for hour-minute-second there's no need to specify the
3796 end of each key field, since the @samp{n} and @samp{M} modifiers sort
3797 based on leading prefixes that cannot cross field boundaries. The
3798 IPv4 addresses are sorted lexicographically. The second sort uses
3799 @samp{-s} so that ties in the primary key are broken by the secondary
3800 key; the first sort uses @samp{-s} so that the combination of the two
3804 Generate a tags file in case-insensitive sorted order.
3807 find src -type f -print0 | sort -z -f | xargs -0 etags --append
3810 The use of @option{-print0}, @option{-z}, and @option{-0} in this case means
3811 that file names that contain blanks or other special characters are
3813 by the sort operation.
3815 @c This example is a bit contrived and needs more explanation.
3817 @c Sort records separated by an arbitrary string by using a pipe to convert
3818 @c each record delimiter string to @samp{\0}, then using sort's -z option,
3819 @c and converting each @samp{\0} back to the original record delimiter.
3822 @c printf 'c\n\nb\n\na\n'|perl -0pe 's/\n\n/\n\0/g'|sort -z|perl -0pe 's/\0/\n/g'
3826 Shuffle a list of directories, but preserve the order of files within
3827 each directory. For instance, one could use this to generate a music
3828 playlist in which albums are shuffled but the songs of each album are
3832 ls */* | sort -t / -k 1,1R -k 2,2
3838 @node uniq invocation
3839 @section @command{uniq}: Uniquify files
3842 @cindex uniquify files
3844 @command{uniq} writes the unique lines in the given @file{input}, or
3845 standard input if nothing is given or for an @var{input} name of
3849 uniq [@var{option}]@dots{} [@var{input} [@var{output}]]
3852 By default, @command{uniq} prints its input lines, except that
3853 it discards all but the first of adjacent repeated lines, so that
3854 no output lines are repeated. Optionally, it can instead discard
3855 lines that are not repeated, or all repeated lines.
3857 The input need not be sorted, but repeated input lines are detected
3858 only if they are adjacent. If you want to discard non-adjacent
3859 duplicate lines, perhaps you want to use @code{sort -u}.
3860 @xref{sort invocation}.
3863 Comparisons use the character collating sequence specified by the
3864 @env{LC_COLLATE} locale category.
3866 If no @var{output} file is specified, @command{uniq} writes to standard
3869 The program accepts the following options. Also see @ref{Common options}.
3874 @itemx --skip-fields=@var{n}
3876 @opindex --skip-fields
3877 Skip @var{n} fields on each line before checking for uniqueness. Use
3878 a null string for comparison if a line has fewer than @var{n} fields. Fields
3879 are sequences of non-space non-tab characters that are separated from
3880 each other by at least one space or tab.
3882 For compatibility @command{uniq} supports an obsolete option syntax
3883 @option{-@var{n}}. New scripts should use @option{-f @var{n}} instead.
3886 @itemx --skip-chars=@var{n}
3888 @opindex --skip-chars
3889 Skip @var{n} characters before checking for uniqueness. Use a null string
3890 for comparison if a line has fewer than @var{n} characters. If you use both
3891 the field and character skipping options, fields are skipped over first.
3893 @vindex _POSIX2_VERSION
3894 On older systems, @command{uniq} supports an obsolete option syntax
3896 This obsolete behavior can be enabled or disabled with the
3897 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
3898 conformance}), but portable scripts should avoid commands whose
3899 behavior depends on this variable.
3900 For example, use @samp{uniq ./+10} or @samp{uniq -s 10} rather than
3901 the ambiguous @samp{uniq +10}.
3907 Print the number of times each line occurred along with the line.
3910 @itemx --ignore-case
3912 @opindex --ignore-case
3913 Ignore differences in case when comparing lines.
3919 @cindex repeated lines, outputting
3920 Discard lines that are not repeated. When used by itself, this option
3921 causes @command{uniq} to print the first copy of each repeated line,
3925 @itemx --all-repeated[=@var{delimit-method}]
3927 @opindex --all-repeated
3928 @cindex all repeated lines, outputting
3929 Do not discard the second and subsequent repeated input lines,
3930 but discard lines that are not repeated.
3931 This option is useful mainly in conjunction with other options e.g.,
3932 to ignore case or to compare only selected fields.
3933 The optional @var{delimit-method} tells how to delimit
3934 groups of repeated lines, and must be one of the following:
3939 Do not delimit groups of repeated lines.
3940 This is equivalent to @option{--all-repeated} (@option{-D}).
3943 Output a newline before each group of repeated lines.
3946 Separate groups of repeated lines with a single newline.
3947 This is the same as using @samp{prepend}, except that
3948 there is no newline before the first group, and hence
3949 may be better suited for output direct to users.
3952 Note that when groups are delimited and the input stream contains
3953 two or more consecutive blank lines, then the output is ambiguous.
3954 To avoid that, filter the input through @samp{tr -s '\n'} to replace
3955 each sequence of consecutive newlines with a single newline.
3957 This is a @sc{gnu} extension.
3958 @c FIXME: give an example showing *how* it's useful
3964 @cindex unique lines, outputting
3965 Discard the first repeated line. When used by itself, this option
3966 causes @command{uniq} to print unique lines, and nothing else.
3969 @itemx --check-chars=@var{n}
3971 @opindex --check-chars
3972 Compare at most @var{n} characters on each line (after skipping any specified
3973 fields and characters). By default the entire rest of the lines are
3981 @node comm invocation
3982 @section @command{comm}: Compare two sorted files line by line
3985 @cindex line-by-line comparison
3986 @cindex comparing sorted files
3988 @command{comm} writes to standard output lines that are common, and lines
3989 that are unique, to two input files; a file name of @samp{-} means
3990 standard input. Synopsis:
3993 comm [@var{option}]@dots{} @var{file1} @var{file2}
3997 Before @command{comm} can be used, the input files must be sorted using the
3998 collating sequence specified by the @env{LC_COLLATE} locale.
3999 If an input file ends in a non-newline
4000 character, a newline is silently appended. The @command{sort} command with
4001 no options always outputs a file that is suitable input to @command{comm}.
4003 @cindex differing lines
4004 @cindex common lines
4005 With no options, @command{comm} produces three-column output. Column one
4006 contains lines unique to @var{file1}, column two contains lines unique
4007 to @var{file2}, and column three contains lines common to both files.
4008 Columns are separated by a single TAB character.
4009 @c FIXME: when there's an option to supply an alternative separator
4010 @c string, append `by default' to the above sentence.
4015 The options @option{-1}, @option{-2}, and @option{-3} suppress printing of
4016 the corresponding columns. Also see @ref{Common options}.
4018 Unlike some other comparison utilities, @command{comm} has an exit
4019 status that does not depend on the result of the comparison.
4020 Upon normal completion @command{comm} produces an exit code of zero.
4021 If there is an error it exits with nonzero status.
4024 @node tsort invocation
4025 @section @command{tsort}: Topological sort
4028 @cindex topological sort
4030 @command{tsort} performs a topological sort on the given @var{file}, or
4031 standard input if no input file is given or for a @var{file} of
4032 @samp{-}. For more details and some history, see @ref{tsort background}.
4036 tsort [@var{option}] [@var{file}]
4039 @command{tsort} reads its input as pairs of strings, separated by blanks,
4040 indicating a partial ordering. The output is a total ordering that
4041 corresponds to the given partial ordering.
4055 will produce the output
4066 Consider a more realistic example.
4067 You have a large set of functions all in one file, and they may all be
4068 declared static except one. Currently that one (say @code{main}) is the
4069 first function defined in the file, and the ones it calls directly follow
4070 it, followed by those they call, etc. Let's say that you are determined
4071 to take advantage of prototypes, so you have to choose between declaring
4072 all of those functions (which means duplicating a lot of information from
4073 the definitions) and rearranging the functions so that as many as possible
4074 are defined before they are used. One way to automate the latter process
4075 is to get a list for each function of the functions it calls directly.
4076 Many programs can generate such lists. They describe a call graph.
4077 Consider the following list, in which a given line indicates that the
4078 function on the left calls the one on the right directly.
4084 tail_file pretty_name
4085 tail_file write_header
4087 tail_forever recheck
4088 tail_forever pretty_name
4089 tail_forever write_header
4090 tail_forever dump_remainder
4093 tail_lines start_lines
4094 tail_lines dump_remainder
4095 tail_lines file_lines
4096 tail_lines pipe_lines
4098 tail_bytes start_bytes
4099 tail_bytes dump_remainder
4100 tail_bytes pipe_bytes
4101 file_lines dump_remainder
4105 then you can use @command{tsort} to produce an ordering of those
4106 functions that satisfies your requirement.
4109 example$ tsort call-graph | tac
4129 @command{tsort} detects any cycles in the input and writes the first cycle
4130 encountered to standard error.
4132 Note that for a given partial ordering, generally there is no unique
4133 total ordering. In the context of the call graph above, the function
4134 @code{parse_options} may be placed anywhere in the list as long as it
4135 precedes @code{main}.
4137 The only options are @option{--help} and @option{--version}. @xref{Common
4140 @node tsort background
4141 @section @command{tsort}: Background
4143 @command{tsort} exists because very early versions of the Unix linker processed
4144 an archive file exactly once, and in order. As @command{ld} read each object
4145 in the archive, it decided whether it was needed in the program based on
4146 whether it defined any symbols which were undefined at that point in
4149 This meant that dependencies within the archive had to be handled
4150 specially. For example, @code{scanf} probably calls @code{read}. That means
4151 that in a single pass through an archive, it was important for @code{scanf.o}
4152 to appear before read.o, because otherwise a program which calls
4153 @code{scanf} but not @code{read} might end up with an unexpected unresolved
4154 reference to @code{read}.
4156 The way to address this problem was to first generate a set of
4157 dependencies of one object file on another. This was done by a shell
4158 script called @command{lorder}. The GNU tools don't provide a version of
4159 lorder, as far as I know, but you can still find it in BSD
4162 Then you ran @command{tsort} over the @command{lorder} output, and you used the
4163 resulting sort to define the order in which you added objects to the archive.
4165 This whole procedure has been obsolete since about 1980, because
4166 Unix archives now contain a symbol table (traditionally built by
4167 @command{ranlib}, now generally built by @command{ar} itself), and the Unix
4168 linker uses the symbol table to effectively make multiple passes over
4171 Anyhow, that's where tsort came from. To solve an old problem with
4172 the way the linker handled archive files, which has since been solved
4178 @node ptx invocation
4179 @section @command{ptx}: Produce permuted indexes
4183 @command{ptx} reads a text file and essentially produces a permuted index, with
4184 each keyword in its context. The calling sketch is either one of:
4187 ptx [@var{option} @dots{}] [@var{file} @dots{}]
4188 ptx -G [@var{option} @dots{}] [@var{input} [@var{output}]]
4191 The @option{-G} (or its equivalent: @option{--traditional}) option disables
4192 all @sc{gnu} extensions and reverts to traditional mode, thus introducing some
4193 limitations and changing several of the program's default option values.
4194 When @option{-G} is not specified, @sc{gnu} extensions are always enabled.
4195 @sc{gnu} extensions to @command{ptx} are documented wherever appropriate in this
4196 document. For the full list, see @xref{Compatibility in ptx}.
4198 Individual options are explained in the following sections.
4200 When @sc{gnu} extensions are enabled, there may be zero, one or several
4201 @var{file}s after the options. If there is no @var{file}, the program
4202 reads the standard input. If there is one or several @var{file}s, they
4203 give the name of input files which are all read in turn, as if all the
4204 input files were concatenated. However, there is a full contextual
4205 break between each file and, when automatic referencing is requested,
4206 file names and line numbers refer to individual text input files. In
4207 all cases, the program outputs the permuted index to the standard
4210 When @sc{gnu} extensions are @emph{not} enabled, that is, when the program
4211 operates in traditional mode, there may be zero, one or two parameters
4212 besides the options. If there are no parameters, the program reads the
4213 standard input and outputs the permuted index to the standard output.
4214 If there is only one parameter, it names the text @var{input} to be read
4215 instead of the standard input. If two parameters are given, they give
4216 respectively the name of the @var{input} file to read and the name of
4217 the @var{output} file to produce. @emph{Be very careful} to note that,
4218 in this case, the contents of file given by the second parameter is
4219 destroyed. This behavior is dictated by System V @command{ptx}
4220 compatibility; @sc{gnu} Standards normally discourage output parameters not
4221 introduced by an option.
4223 Note that for @emph{any} file named as the value of an option or as an
4224 input text file, a single dash @kbd{-} may be used, in which case
4225 standard input is assumed. However, it would not make sense to use this
4226 convention more than once per program invocation.
4229 * General options in ptx:: Options which affect general program behavior.
4230 * Charset selection in ptx:: Underlying character set considerations.
4231 * Input processing in ptx:: Input fields, contexts, and keyword selection.
4232 * Output formatting in ptx:: Types of output format, and sizing the fields.
4233 * Compatibility in ptx::
4237 @node General options in ptx
4238 @subsection General options
4243 @itemx --traditional
4244 As already explained, this option disables all @sc{gnu} extensions to
4245 @command{ptx} and switches to traditional mode.
4248 Print a short help on standard output, then exit without further
4252 Print the program version on standard output, then exit without further
4260 @node Charset selection in ptx
4261 @subsection Charset selection
4263 @c FIXME: People don't necessarily know what an IBM-PC was these days.
4264 As it is set up now, the program assumes that the input file is coded
4265 using 8-bit @acronym{ISO} 8859-1 code, also known as Latin-1 character set,
4266 @emph{unless} it is compiled for MS-DOS, in which case it uses the
4267 character set of the IBM-PC@. (@sc{gnu} @command{ptx} is not known to work on
4268 smaller MS-DOS machines anymore.) Compared to 7-bit @acronym{ASCII}, the set
4269 of characters which are letters is different; this alters the behavior
4270 of regular expression matching. Thus, the default regular expression
4271 for a keyword allows foreign or diacriticized letters. Keyword sorting,
4272 however, is still crude; it obeys the underlying character set ordering
4278 @itemx --ignore-case
4279 Fold lower case letters to upper case for sorting.
4284 @node Input processing in ptx
4285 @subsection Word selection and input processing
4290 @item --break-file=@var{file}
4292 This option provides an alternative (to @option{-W}) method of describing
4293 which characters make up words. It introduces the name of a
4294 file which contains a list of characters which can@emph{not} be part of
4295 one word; this file is called the @dfn{Break file}. Any character which
4296 is not part of the Break file is a word constituent. If both options
4297 @option{-b} and @option{-W} are specified, then @option{-W} has precedence and
4298 @option{-b} is ignored.
4300 When @sc{gnu} extensions are enabled, the only way to avoid newline as a
4301 break character is to write all the break characters in the file with no
4302 newline at all, not even at the end of the file. When @sc{gnu} extensions
4303 are disabled, spaces, tabs and newlines are always considered as break
4304 characters even if not included in the Break file.
4307 @itemx --ignore-file=@var{file}
4309 The file associated with this option contains a list of words which will
4310 never be taken as keywords in concordance output. It is called the
4311 @dfn{Ignore file}. The file contains exactly one word in each line; the
4312 end of line separation of words is not subject to the value of the
4316 @itemx --only-file=@var{file}
4318 The file associated with this option contains a list of words which will
4319 be retained in concordance output; any word not mentioned in this file
4320 is ignored. The file is called the @dfn{Only file}. The file contains
4321 exactly one word in each line; the end of line separation of words is
4322 not subject to the value of the @option{-S} option.
4324 There is no default for the Only file. When both an Only file and an
4325 Ignore file are specified, a word is considered a keyword only
4326 if it is listed in the Only file and not in the Ignore file.
4331 On each input line, the leading sequence of non-white space characters will be
4332 taken to be a reference that has the purpose of identifying this input
4333 line in the resulting permuted index. For more information about reference
4334 production, see @xref{Output formatting in ptx}.
4335 Using this option changes the default value for option @option{-S}.
4337 Using this option, the program does not try very hard to remove
4338 references from contexts in output, but it succeeds in doing so
4339 @emph{when} the context ends exactly at the newline. If option
4340 @option{-r} is used with @option{-S} default value, or when @sc{gnu} extensions
4341 are disabled, this condition is always met and references are completely
4342 excluded from the output contexts.
4344 @item -S @var{regexp}
4345 @itemx --sentence-regexp=@var{regexp}
4347 This option selects which regular expression will describe the end of a
4348 line or the end of a sentence. In fact, this regular expression is not
4349 the only distinction between end of lines or end of sentences, and input
4350 line boundaries have no special significance outside this option. By
4351 default, when @sc{gnu} extensions are enabled and if @option{-r} option is not
4352 used, end of sentences are used. In this case, this @var{regex} is
4353 imported from @sc{gnu} Emacs:
4356 [.?!][]\"')@}]*\\($\\|\t\\| \\)[ \t\n]*
4359 Whenever @sc{gnu} extensions are disabled or if @option{-r} option is used, end
4360 of lines are used; in this case, the default @var{regexp} is just:
4366 Using an empty @var{regexp} is equivalent to completely disabling end of
4367 line or end of sentence recognition. In this case, the whole file is
4368 considered to be a single big line or sentence. The user might want to
4369 disallow all truncation flag generation as well, through option @option{-F
4370 ""}. @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4373 When the keywords happen to be near the beginning of the input line or
4374 sentence, this often creates an unused area at the beginning of the
4375 output context line; when the keywords happen to be near the end of the
4376 input line or sentence, this often creates an unused area at the end of
4377 the output context line. The program tries to fill those unused areas
4378 by wrapping around context in them; the tail of the input line or
4379 sentence is used to fill the unused area on the left of the output line;
4380 the head of the input line or sentence is used to fill the unused area
4381 on the right of the output line.
4383 As a matter of convenience to the user, many usual backslashed escape
4384 sequences from the C language are recognized and converted to the
4385 corresponding characters by @command{ptx} itself.
4387 @item -W @var{regexp}
4388 @itemx --word-regexp=@var{regexp}
4390 This option selects which regular expression will describe each keyword.
4391 By default, if @sc{gnu} extensions are enabled, a word is a sequence of
4392 letters; the @var{regexp} used is @samp{\w+}. When @sc{gnu} extensions are
4393 disabled, a word is by default anything which ends with a space, a tab
4394 or a newline; the @var{regexp} used is @samp{[^ \t\n]+}.
4396 An empty @var{regexp} is equivalent to not using this option.
4397 @xref{Regexps, , Syntax of Regular Expressions, emacs, The GNU Emacs
4400 As a matter of convenience to the user, many usual backslashed escape
4401 sequences, as found in the C language, are recognized and converted to
4402 the corresponding characters by @command{ptx} itself.
4407 @node Output formatting in ptx
4408 @subsection Output formatting
4410 Output format is mainly controlled by the @option{-O} and @option{-T} options
4411 described in the table below. When neither @option{-O} nor @option{-T} are
4412 selected, and if @sc{gnu} extensions are enabled, the program chooses an
4413 output format suitable for a dumb terminal. Each keyword occurrence is
4414 output to the center of one line, surrounded by its left and right
4415 contexts. Each field is properly justified, so the concordance output
4416 can be readily observed. As a special feature, if automatic
4417 references are selected by option @option{-A} and are output before the
4418 left context, that is, if option @option{-R} is @emph{not} selected, then
4419 a colon is added after the reference; this nicely interfaces with @sc{gnu}
4420 Emacs @code{next-error} processing. In this default output format, each
4421 white space character, like newline and tab, is merely changed to
4422 exactly one space, with no special attempt to compress consecutive
4423 spaces. This might change in the future. Except for those white space
4424 characters, every other character of the underlying set of 256
4425 characters is transmitted verbatim.
4427 Output format is further controlled by the following options.
4431 @item -g @var{number}
4432 @itemx --gap-size=@var{number}
4434 Select the size of the minimum white space gap between the fields on the
4437 @item -w @var{number}
4438 @itemx --width=@var{number}
4440 Select the maximum output width of each final line. If references are
4441 used, they are included or excluded from the maximum output width
4442 depending on the value of option @option{-R}. If this option is not
4443 selected, that is, when references are output before the left context,
4444 the maximum output width takes into account the maximum length of all
4445 references. If this option is selected, that is, when references are
4446 output after the right context, the maximum output width does not take
4447 into account the space taken by references, nor the gap that precedes
4451 @itemx --auto-reference
4453 Select automatic references. Each input line will have an automatic
4454 reference made up of the file name and the line ordinal, with a single
4455 colon between them. However, the file name will be empty when standard
4456 input is being read. If both @option{-A} and @option{-r} are selected, then
4457 the input reference is still read and skipped, but the automatic
4458 reference is used at output time, overriding the input reference.
4461 @itemx --right-side-refs
4463 In the default output format, when option @option{-R} is not used, any
4464 references produced by the effect of options @option{-r} or @option{-A} are
4465 placed to the far right of output lines, after the right context. With
4466 default output format, when the @option{-R} option is specified, references
4467 are rather placed at the beginning of each output line, before the left
4468 context. For any other output format, option @option{-R} is
4469 ignored, with one exception: with @option{-R} the width of references
4470 is @emph{not} taken into account in total output width given by @option{-w}.
4472 This option is automatically selected whenever @sc{gnu} extensions are
4475 @item -F @var{string}
4476 @itemx --flac-truncation=@var{string}
4478 This option will request that any truncation in the output be reported
4479 using the string @var{string}. Most output fields theoretically extend
4480 towards the beginning or the end of the current line, or current
4481 sentence, as selected with option @option{-S}. But there is a maximum
4482 allowed output line width, changeable through option @option{-w}, which is
4483 further divided into space for various output fields. When a field has
4484 to be truncated because it cannot extend beyond the beginning or the end of
4485 the current line to fit in, then a truncation occurs. By default,
4486 the string used is a single slash, as in @option{-F /}.
4488 @var{string} may have more than one character, as in @option{-F ...}.
4489 Also, in the particular case when @var{string} is empty (@option{-F ""}),
4490 truncation flagging is disabled, and no truncation marks are appended in
4493 As a matter of convenience to the user, many usual backslashed escape
4494 sequences, as found in the C language, are recognized and converted to
4495 the corresponding characters by @command{ptx} itself.
4497 @item -M @var{string}
4498 @itemx --macro-name=@var{string}
4500 Select another @var{string} to be used instead of @samp{xx}, while
4501 generating output suitable for @command{nroff}, @command{troff} or @TeX{}.
4504 @itemx --format=roff
4506 Choose an output format suitable for @command{nroff} or @command{troff}
4507 processing. Each output line will look like:
4510 .xx "@var{tail}" "@var{before}" "@var{keyword_and_after}" "@var{head}" "@var{ref}"
4513 so it will be possible to write a @samp{.xx} roff macro to take care of
4514 the output typesetting. This is the default output format when @sc{gnu}
4515 extensions are disabled. Option @option{-M} can be used to change
4516 @samp{xx} to another macro name.
4518 In this output format, each non-graphical character, like newline and
4519 tab, is merely changed to exactly one space, with no special attempt to
4520 compress consecutive spaces. Each quote character: @kbd{"} is doubled
4521 so it will be correctly processed by @command{nroff} or @command{troff}.
4526 Choose an output format suitable for @TeX{} processing. Each output
4527 line will look like:
4530 \xx @{@var{tail}@}@{@var{before}@}@{@var{keyword}@}@{@var{after}@}@{@var{head}@}@{@var{ref}@}
4534 so it will be possible to write a @code{\xx} definition to take care of
4535 the output typesetting. Note that when references are not being
4536 produced, that is, neither option @option{-A} nor option @option{-r} is
4537 selected, the last parameter of each @code{\xx} call is inhibited.
4538 Option @option{-M} can be used to change @samp{xx} to another macro
4541 In this output format, some special characters, like @kbd{$}, @kbd{%},
4542 @kbd{&}, @kbd{#} and @kbd{_} are automatically protected with a
4543 backslash. Curly brackets @kbd{@{}, @kbd{@}} are protected with a
4544 backslash and a pair of dollar signs (to force mathematical mode). The
4545 backslash itself produces the sequence @code{\backslash@{@}}.
4546 Circumflex and tilde diacritical marks produce the sequence @code{^\@{ @}} and
4547 @code{~\@{ @}} respectively. Other diacriticized characters of the
4548 underlying character set produce an appropriate @TeX{} sequence as far
4549 as possible. The other non-graphical characters, like newline and tab,
4550 and all other characters which are not part of @acronym{ASCII}, are merely
4551 changed to exactly one space, with no special attempt to compress
4552 consecutive spaces. Let me know how to improve this special character
4553 processing for @TeX{}.
4558 @node Compatibility in ptx
4559 @subsection The @sc{gnu} extensions to @command{ptx}
4561 This version of @command{ptx} contains a few features which do not exist in
4562 System V @command{ptx}. These extra features are suppressed by using the
4563 @option{-G} command line option, unless overridden by other command line
4564 options. Some @sc{gnu} extensions cannot be recovered by overriding, so the
4565 simple rule is to avoid @option{-G} if you care about @sc{gnu} extensions.
4566 Here are the differences between this program and System V @command{ptx}.
4571 This program can read many input files at once, it always writes the
4572 resulting concordance on standard output. On the other hand, System V
4573 @command{ptx} reads only one file and sends the result to standard output
4574 or, if a second @var{file} parameter is given on the command, to that
4577 Having output parameters not introduced by options is a dangerous
4578 practice which @sc{gnu} avoids as far as possible. So, for using @command{ptx}
4579 portably between @sc{gnu} and System V, you should always use it with a
4580 single input file, and always expect the result on standard output. You
4581 might also want to automatically configure in a @option{-G} option to
4582 @command{ptx} calls in products using @command{ptx}, if the configurator finds
4583 that the installed @command{ptx} accepts @option{-G}.
4586 The only options available in System V @command{ptx} are options @option{-b},
4587 @option{-f}, @option{-g}, @option{-i}, @option{-o}, @option{-r}, @option{-t} and
4588 @option{-w}. All other options are @sc{gnu} extensions and are not repeated in
4589 this enumeration. Moreover, some options have a slightly different
4590 meaning when @sc{gnu} extensions are enabled, as explained below.
4593 By default, concordance output is not formatted for @command{troff} or
4594 @command{nroff}. It is rather formatted for a dumb terminal. @command{troff}
4595 or @command{nroff} output may still be selected through option @option{-O}.
4598 Unless @option{-R} option is used, the maximum reference width is
4599 subtracted from the total output line width. With @sc{gnu} extensions
4600 disabled, width of references is not taken into account in the output
4601 line width computations.
4604 All 256 bytes, even null bytes, are always read and processed from
4605 input file with no adverse effect, even if @sc{gnu} extensions are disabled.
4606 However, System V @command{ptx} does not accept 8-bit characters, a few
4607 control characters are rejected, and the tilde @kbd{~} is also rejected.
4610 Input line length is only limited by available memory, even if @sc{gnu}
4611 extensions are disabled. However, System V @command{ptx} processes only
4612 the first 200 characters in each line.
4615 The break (non-word) characters default to be every character except all
4616 letters of the underlying character set, diacriticized or not. When @sc{gnu}
4617 extensions are disabled, the break characters default to space, tab and
4621 The program makes better use of output line width. If @sc{gnu} extensions
4622 are disabled, the program rather tries to imitate System V @command{ptx},
4623 but still, there are some slight disposition glitches this program does
4624 not completely reproduce.
4627 The user can specify both an Ignore file and an Only file. This is not
4628 allowed with System V @command{ptx}.
4633 @node Operating on fields within a line
4634 @chapter Operating on fields within a line
4637 * cut invocation:: Print selected parts of lines.
4638 * paste invocation:: Merge lines of files.
4639 * join invocation:: Join lines on a common field.
4643 @node cut invocation
4644 @section @command{cut}: Print selected parts of lines
4647 @command{cut} writes to standard output selected parts of each line of each
4648 input file, or standard input if no files are given or for a file name of
4652 cut [@var{option}]@dots{} [@var{file}]@dots{}
4655 In the table which follows, the @var{byte-list}, @var{character-list},
4656 and @var{field-list} are one or more numbers or ranges (two numbers
4657 separated by a dash) separated by commas. Bytes, characters, and
4658 fields are numbered starting at 1. Incomplete ranges may be
4659 given: @option{-@var{m}} means @samp{1-@var{m}}; @samp{@var{n}-} means
4660 @samp{@var{n}} through end of line or last field. The list elements
4661 can be repeated, can overlap, and can be specified in any order; but
4662 the selected input is written in the same order that it is read, and
4663 is written exactly once.
4665 The program accepts the following options. Also see @ref{Common
4670 @item -b @var{byte-list}
4671 @itemx --bytes=@var{byte-list}
4674 Select for printing only the bytes in positions listed in
4675 @var{byte-list}. Tabs and backspaces are treated like any other
4676 character; they take up 1 byte. If an output delimiter is specified,
4677 (see the description of @option{--output-delimiter}), then output that
4678 string between ranges of selected bytes.
4680 @item -c @var{character-list}
4681 @itemx --characters=@var{character-list}
4683 @opindex --characters
4684 Select for printing only the characters in positions listed in
4685 @var{character-list}. The same as @option{-b} for now, but
4686 internationalization will change that. Tabs and backspaces are
4687 treated like any other character; they take up 1 character. If an
4688 output delimiter is specified, (see the description of
4689 @option{--output-delimiter}), then output that string between ranges
4692 @item -f @var{field-list}
4693 @itemx --fields=@var{field-list}
4696 Select for printing only the fields listed in @var{field-list}.
4697 Fields are separated by a TAB character by default. Also print any
4698 line that contains no delimiter character, unless the
4699 @option{--only-delimited} (@option{-s}) option is specified
4701 @item -d @var{input_delim_byte}
4702 @itemx --delimiter=@var{input_delim_byte}
4704 @opindex --delimiter
4705 With @option{-f}, use the first byte of @var{input_delim_byte} as
4706 the input fields separator (default is TAB).
4710 Do not split multi-byte characters (no-op for now).
4713 @itemx --only-delimited
4715 @opindex --only-delimited
4716 For @option{-f}, do not print lines that do not contain the field separator
4717 character. Normally, any line without a field separator is printed verbatim.
4719 @item --output-delimiter=@var{output_delim_string}
4720 @opindex --output-delimiter
4721 With @option{-f}, output fields are separated by @var{output_delim_string}.
4722 The default with @option{-f} is to use the input delimiter.
4723 When using @option{-b} or @option{-c} to select ranges of byte or
4724 character offsets (as opposed to ranges of fields),
4725 output @var{output_delim_string} between non-overlapping
4726 ranges of selected bytes.
4729 @opindex --complement
4730 This option is a @acronym{GNU} extension.
4731 Select for printing the complement of the bytes, characters or fields
4732 selected with the @option{-b}, @option{-c} or @option{-f} options.
4733 In other words, do @emph{not} print the bytes, characters or fields
4734 specified via those options. This option is useful when you have
4735 many fields and want to print all but a few of them.
4742 @node paste invocation
4743 @section @command{paste}: Merge lines of files
4746 @cindex merging files
4748 @command{paste} writes to standard output lines consisting of sequentially
4749 corresponding lines of each given file, separated by a TAB character.
4750 Standard input is used for a file name of @samp{-} or if no input files
4772 paste [@var{option}]@dots{} [@var{file}]@dots{}
4775 The program accepts the following options. Also see @ref{Common options}.
4783 Paste the lines of one file at a time rather than one line from each
4784 file. Using the above example data:
4787 $ paste -s num2 let3
4792 @item -d @var{delim-list}
4793 @itemx --delimiters=@var{delim-list}
4795 @opindex --delimiters
4796 Consecutively use the characters in @var{delim-list} instead of
4797 TAB to separate merged lines. When @var{delim-list} is
4798 exhausted, start again at its beginning. Using the above example data:
4801 $ paste -d '%_' num2 let3 num2
4812 @node join invocation
4813 @section @command{join}: Join lines on a common field
4816 @cindex common field, joining on
4818 @command{join} writes to standard output a line for each pair of input
4819 lines that have identical join fields. Synopsis:
4822 join [@var{option}]@dots{} @var{file1} @var{file2}
4825 Either @var{file1} or @var{file2} (but not both) can be @samp{-},
4826 meaning standard input. @var{file1} and @var{file2} should be
4827 sorted on the join fields.
4830 Normally, the sort order is that of the
4831 collating sequence specified by the @env{LC_COLLATE} locale. Unless
4832 the @option{-t} option is given, the sort comparison ignores blanks at
4833 the start of the join field, as in @code{sort -b}. If the
4834 @option{--ignore-case} option is given, the sort comparison ignores
4835 the case of characters in the join field, as in @code{sort -f}.
4837 The @command{sort} and @command{join} commands should use consistent
4838 locales and options if the output of @command{sort} is fed to
4839 @command{join}. You can use a command like @samp{sort -k 1b,1} to
4840 sort a file on its default join field, but if you select a non-default
4841 locale, join field, separator, or comparison options, then you should
4842 do so consistently between @command{join} and @command{sort}.
4844 As a @acronym{GNU} extension, if the input has no unpairable lines the
4845 sort order can be any order that considers two fields to be equal if and
4846 only if the sort comparison described above considers them to be equal.
4866 @item the join field is the first field in each line;
4867 @item fields in the input are separated by one or more blanks, with leading
4868 blanks on the line ignored;
4869 @item fields in the output are separated by a space;
4870 @item each output line consists of the join field, the remaining
4871 fields from @var{file1}, then the remaining fields from @var{file2}.
4874 The program accepts the following options. Also see @ref{Common options}.
4878 @item -a @var{file-number}
4880 Print a line for each unpairable line in file @var{file-number} (either
4881 @samp{1} or @samp{2}), in addition to the normal output.
4883 @item -e @var{string}
4885 Replace those output fields that are missing in the input with
4889 @itemx --ignore-case
4891 @opindex --ignore-case
4892 Ignore differences in case when comparing keys.
4893 With this option, the lines of the input files must be ordered in the same way.
4894 Use @samp{sort -f} to produce this ordering.
4896 @item -1 @var{field}
4898 Join on field @var{field} (a positive integer) of file 1.
4900 @item -2 @var{field}
4902 Join on field @var{field} (a positive integer) of file 2.
4904 @item -j @var{field}
4905 Equivalent to @option{-1 @var{field} -2 @var{field}}.
4907 @item -o @var{field-list}
4908 Construct each output line according to the format in @var{field-list}.
4909 Each element in @var{field-list} is either the single character @samp{0} or
4910 has the form @var{m.n} where the file number, @var{m}, is @samp{1} or
4911 @samp{2} and @var{n} is a positive field number.
4913 A field specification of @samp{0} denotes the join field.
4914 In most cases, the functionality of the @samp{0} field spec
4915 may be reproduced using the explicit @var{m.n} that corresponds
4916 to the join field. However, when printing unpairable lines
4917 (using either of the @option{-a} or @option{-v} options), there is no way
4918 to specify the join field using @var{m.n} in @var{field-list}
4919 if there are unpairable lines in both files.
4920 To give @command{join} that functionality, @acronym{POSIX} invented the @samp{0}
4921 field specification notation.
4923 The elements in @var{field-list}
4924 are separated by commas or blanks.
4925 Blank separators typically need to be quoted for the shell. For
4926 example, the commands @samp{join -o 1.2,2.2} and @samp{join -o '1.2
4927 2.2'} are equivalent.
4929 All output lines---including those printed because of any -a or -v
4930 option---are subject to the specified @var{field-list}.
4933 Use character @var{char} as the input and output field separator.
4934 Treat as significant each occurrence of @var{char} in the input file.
4935 Use @samp{sort -t @var{char}}, without the @option{-b} option of
4936 @samp{sort}, to produce this ordering.
4938 @item -v @var{file-number}
4939 Print a line for each unpairable line in file @var{file-number}
4940 (either @samp{1} or @samp{2}), instead of the normal output.
4947 @node Operating on characters
4948 @chapter Operating on characters
4950 @cindex operating on characters
4952 This commands operate on individual characters.
4955 * tr invocation:: Translate, squeeze, and/or delete characters.
4956 * expand invocation:: Convert tabs to spaces.
4957 * unexpand invocation:: Convert spaces to tabs.
4962 @section @command{tr}: Translate, squeeze, and/or delete characters
4969 tr [@var{option}]@dots{} @var{set1} [@var{set2}]
4972 @command{tr} copies standard input to standard output, performing
4973 one of the following operations:
4977 translate, and optionally squeeze repeated characters in the result,
4979 squeeze repeated characters,
4983 delete characters, then squeeze repeated characters from the result.
4986 The @var{set1} and (if given) @var{set2} arguments define ordered
4987 sets of characters, referred to below as @var{set1} and @var{set2}. These
4988 sets are the characters of the input that @command{tr} operates on.
4989 The @option{--complement} (@option{-c}, @option{-C}) option replaces
4991 complement (all of the characters that are not in @var{set1}).
4993 Currently @command{tr} fully supports only single-byte characters.
4994 Eventually it will support multibyte characters; when it does, the
4995 @option{-C} option will cause it to complement the set of characters,
4996 whereas @option{-c} will cause it to complement the set of values.
4997 This distinction will matter only when some values are not characters,
4998 and this is possible only in locales using multibyte encodings when
4999 the input contains encoding errors.
5001 The program accepts the @option{--help} and @option{--version}
5002 options. @xref{Common options}. Options must precede operands.
5007 * Character sets:: Specifying sets of characters.
5008 * Translating:: Changing one set of characters to another.
5009 * Squeezing:: Squeezing repeats and deleting.
5013 @node Character sets
5014 @subsection Specifying sets of characters
5016 @cindex specifying sets of characters
5018 The format of the @var{set1} and @var{set2} arguments resembles
5019 the format of regular expressions; however, they are not regular
5020 expressions, only lists of characters. Most characters simply
5021 represent themselves in these strings, but the strings can contain
5022 the shorthands listed below, for convenience. Some of them can be
5023 used only in @var{set1} or @var{set2}, as noted below.
5027 @item Backslash escapes
5028 @cindex backslash escapes
5030 The following backslash escape sequences are recognized:
5048 The character with the value given by @var{ooo}, which is 1 to 3
5054 While a backslash followed by a character not listed above is
5055 interpreted as that character, the backslash also effectively
5056 removes any special significance, so it is useful to escape
5057 @samp{[}, @samp{]}, @samp{*}, and @samp{-}.
5062 The notation @samp{@var{m}-@var{n}} expands to all of the characters
5063 from @var{m} through @var{n}, in ascending order. @var{m} should
5064 collate before @var{n}; if it doesn't, an error results. As an example,
5065 @samp{0-9} is the same as @samp{0123456789}.
5067 @sc{gnu} @command{tr} does not support the System V syntax that uses square
5068 brackets to enclose ranges. Translations specified in that format
5069 sometimes work as expected, since the brackets are often transliterated
5070 to themselves. However, they should be avoided because they sometimes
5071 behave unexpectedly. For example, @samp{tr -d '[0-9]'} deletes brackets
5074 Many historically common and even accepted uses of ranges are not
5075 portable. For example, on @acronym{EBCDIC} hosts using the @samp{A-Z}
5076 range will not do what most would expect because @samp{A} through @samp{Z}
5077 are not contiguous as they are in @acronym{ASCII}.
5078 If you can rely on a @acronym{POSIX} compliant version of @command{tr}, then
5079 the best way to work around this is to use character classes (see below).
5080 Otherwise, it is most portable (and most ugly) to enumerate the members
5083 @item Repeated characters
5084 @cindex repeated characters
5086 The notation @samp{[@var{c}*@var{n}]} in @var{set2} expands to @var{n}
5087 copies of character @var{c}. Thus, @samp{[y*6]} is the same as
5088 @samp{yyyyyy}. The notation @samp{[@var{c}*]} in @var{string2} expands
5089 to as many copies of @var{c} as are needed to make @var{set2} as long as
5090 @var{set1}. If @var{n} begins with @samp{0}, it is interpreted in
5091 octal, otherwise in decimal.
5093 @item Character classes
5094 @cindex character classes
5096 The notation @samp{[:@var{class}:]} expands to all of the characters in
5097 the (predefined) class @var{class}. The characters expand in no
5098 particular order, except for the @code{upper} and @code{lower} classes,
5099 which expand in ascending order. When the @option{--delete} (@option{-d})
5100 and @option{--squeeze-repeats} (@option{-s}) options are both given, any
5101 character class can be used in @var{set2}. Otherwise, only the
5102 character classes @code{lower} and @code{upper} are accepted in
5103 @var{set2}, and then only if the corresponding character class
5104 (@code{upper} and @code{lower}, respectively) is specified in the same
5105 relative position in @var{set1}. Doing this specifies case conversion.
5106 The class names are given below; an error results when an invalid class
5118 Horizontal whitespace.
5127 Printable characters, not including space.
5133 Printable characters, including space.
5136 Punctuation characters.
5139 Horizontal or vertical whitespace.
5148 @item Equivalence classes
5149 @cindex equivalence classes
5151 The syntax @samp{[=@var{c}=]} expands to all of the characters that are
5152 equivalent to @var{c}, in no particular order. Equivalence classes are
5153 a relatively recent invention intended to support non-English alphabets.
5154 But there seems to be no standard way to define them or determine their
5155 contents. Therefore, they are not fully implemented in @sc{gnu} @command{tr};
5156 each character's equivalence class consists only of that character,
5157 which is of no particular use.
5163 @subsection Translating
5165 @cindex translating characters
5167 @command{tr} performs translation when @var{set1} and @var{set2} are
5168 both given and the @option{--delete} (@option{-d}) option is not given.
5169 @command{tr} translates each character of its input that is in @var{set1}
5170 to the corresponding character in @var{set2}. Characters not in
5171 @var{set1} are passed through unchanged. When a character appears more
5172 than once in @var{set1} and the corresponding characters in @var{set2}
5173 are not all the same, only the final one is used. For example, these
5174 two commands are equivalent:
5181 A common use of @command{tr} is to convert lowercase characters to
5182 uppercase. This can be done in many ways. Here are three of them:
5185 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
5187 tr '[:lower:]' '[:upper:]'
5191 But note that using ranges like @code{a-z} above is not portable.
5193 When @command{tr} is performing translation, @var{set1} and @var{set2}
5194 typically have the same length. If @var{set1} is shorter than
5195 @var{set2}, the extra characters at the end of @var{set2} are ignored.
5197 On the other hand, making @var{set1} longer than @var{set2} is not
5198 portable; @acronym{POSIX} says that the result is undefined. In this situation,
5199 BSD @command{tr} pads @var{set2} to the length of @var{set1} by repeating
5200 the last character of @var{set2} as many times as necessary. System V
5201 @command{tr} truncates @var{set1} to the length of @var{set2}.
5203 By default, @sc{gnu} @command{tr} handles this case like BSD @command{tr}.
5204 When the @option{--truncate-set1} (@option{-t}) option is given,
5205 @sc{gnu} @command{tr} handles this case like the System V @command{tr}
5206 instead. This option is ignored for operations other than translation.
5208 Acting like System V @command{tr} in this case breaks the relatively common
5212 tr -cs A-Za-z0-9 '\012'
5216 because it converts only zero bytes (the first element in the
5217 complement of @var{set1}), rather than all non-alphanumerics, to
5221 By the way, the above idiom is not portable because it uses ranges, and
5222 it assumes that the octal code for newline is 012.
5223 Assuming a @acronym{POSIX} compliant @command{tr}, here is a better way to write it:
5226 tr -cs '[:alnum:]' '[\n*]'
5231 @subsection Squeezing repeats and deleting
5233 @cindex squeezing repeat characters
5234 @cindex deleting characters
5236 When given just the @option{--delete} (@option{-d}) option, @command{tr}
5237 removes any input characters that are in @var{set1}.
5239 When given just the @option{--squeeze-repeats} (@option{-s}) option,
5240 @command{tr} replaces each input sequence of a repeated character that
5241 is in @var{set1} with a single occurrence of that character.
5243 When given both @option{--delete} and @option{--squeeze-repeats}, @command{tr}
5244 first performs any deletions using @var{set1}, then squeezes repeats
5245 from any remaining characters using @var{set2}.
5247 The @option{--squeeze-repeats} option may also be used when translating,
5248 in which case @command{tr} first performs translation, then squeezes
5249 repeats from any remaining characters using @var{set2}.
5251 Here are some examples to illustrate various combinations of options:
5256 Remove all zero bytes:
5263 Put all words on lines by themselves. This converts all
5264 non-alphanumeric characters to newlines, then squeezes each string
5265 of repeated newlines into a single newline:
5268 tr -cs '[:alnum:]' '[\n*]'
5272 Convert each sequence of repeated newlines to a single newline:
5279 Find doubled occurrences of words in a document.
5280 @c Separate the following two "the"s, so typo checkers don't complain.
5281 For example, people often write ``the @w{}the'' with the repeated words
5282 separated by a newline. The Bourne shell script below works first
5283 by converting each sequence of punctuation and blank characters to a
5284 single newline. That puts each ``word'' on a line by itself.
5285 Next it maps all uppercase characters to lower case, and finally it
5286 runs @command{uniq} with the @option{-d} option to print out only the words
5292 | tr -s '[:punct:][:blank:]' '[\n*]' \
5293 | tr '[:upper:]' '[:lower:]' \
5298 Deleting a small set of characters is usually straightforward. For example,
5299 to remove all @samp{a}s, @samp{x}s, and @samp{M}s you would do this:
5305 However, when @samp{-} is one of those characters, it can be tricky because
5306 @samp{-} has special meanings. Performing the same task as above but also
5307 removing all @samp{-} characters, we might try @code{tr -d -axM}, but
5308 that would fail because @command{tr} would try to interpret @option{-a} as
5309 a command-line option. Alternatively, we could try putting the hyphen
5310 inside the string, @code{tr -d a-xM}, but that wouldn't work either because
5311 it would make @command{tr} interpret @code{a-x} as the range of characters
5312 @samp{a}@dots{}@samp{x} rather than the three.
5313 One way to solve the problem is to put the hyphen at the end of the list
5320 Or you can use @samp{--} to terminate option processing:
5326 More generally, use the character class notation @code{[=c=]}
5327 with @samp{-} (or any other character) in place of the @samp{c}:
5333 Note how single quotes are used in the above example to protect the
5334 square brackets from interpretation by a shell.
5339 @node expand invocation
5340 @section @command{expand}: Convert tabs to spaces
5343 @cindex tabs to spaces, converting
5344 @cindex converting tabs to spaces
5346 @command{expand} writes the contents of each given @var{file}, or standard
5347 input if none are given or for a @var{file} of @samp{-}, to standard
5348 output, with tab characters converted to the appropriate number of
5352 expand [@var{option}]@dots{} [@var{file}]@dots{}
5355 By default, @command{expand} converts all tabs to spaces. It preserves
5356 backspace characters in the output; they decrement the column count for
5357 tab calculations. The default action is equivalent to @option{-t 8} (set
5358 tabs every 8 columns).
5360 The program accepts the following options. Also see @ref{Common options}.
5364 @item -t @var{tab1}[,@var{tab2}]@dots{}
5365 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5368 @cindex tab stops, setting
5369 If only one tab stop is given, set the tabs @var{tab1} spaces apart
5370 (default is 8). Otherwise, set the tabs at columns @var{tab1},
5371 @var{tab2}, @dots{} (numbered from 0), and replace any tabs beyond the
5372 last tab stop given with single spaces. Tab stops can be separated by
5373 blanks as well as by commas.
5375 For compatibility, GNU @command{expand} also accepts the obsolete
5376 option syntax, @option{-@var{t1}[,@var{t2}]@dots{}}. New scripts
5377 should use @option{-t @var{t1}[,@var{t2}]@dots{}} instead.
5383 @cindex initial tabs, converting
5384 Only convert initial tabs (those that precede all non-space or non-tab
5385 characters) on each line to spaces.
5392 @node unexpand invocation
5393 @section @command{unexpand}: Convert spaces to tabs
5397 @command{unexpand} writes the contents of each given @var{file}, or
5398 standard input if none are given or for a @var{file} of @samp{-}, to
5399 standard output, converting blanks at the beginning of each line into
5400 as many tab characters as needed. In the default @acronym{POSIX}
5401 locale, a @dfn{blank} is a space or a tab; other locales may specify
5402 additional blank characters. Synopsis:
5405 unexpand [@var{option}]@dots{} [@var{file}]@dots{}
5408 By default, @command{unexpand} converts only initial blanks (those
5409 that precede all non-blank characters) on each line. It
5410 preserves backspace characters in the output; they decrement the column
5411 count for tab calculations. By default, tabs are set at every 8th
5414 The program accepts the following options. Also see @ref{Common options}.
5418 @item -t @var{tab1}[,@var{tab2}]@dots{}
5419 @itemx --tabs=@var{tab1}[,@var{tab2}]@dots{}
5422 If only one tab stop is given, set the tabs @var{tab1} columns apart
5423 instead of the default 8. Otherwise, set the tabs at columns
5424 @var{tab1}, @var{tab2}, @dots{} (numbered from 0), and leave blanks
5425 beyond the tab stops given unchanged. Tab stops can be separated by
5426 blanks as well as by commas. This option implies the @option{-a} option.
5428 For compatibility, GNU @command{unexpand} supports the obsolete option syntax,
5429 @option{-@var{tab1}[,@var{tab2}]@dots{}}, where tab stops must be
5430 separated by commas. (Unlike @option{-t}, this obsolete option does
5431 not imply @option{-a}.) New scripts should use @option{--first-only -t
5432 @var{tab1}[,@var{tab2}]@dots{}} instead.
5438 Also convert all sequences of two or more blanks just before a tab stop,
5439 even if they occur after non-blank characters in a line.
5446 @node Directory listing
5447 @chapter Directory listing
5449 This chapter describes the @command{ls} command and its variants @command{dir}
5450 and @command{vdir}, which list information about files.
5453 * ls invocation:: List directory contents.
5454 * dir invocation:: Briefly ls.
5455 * vdir invocation:: Verbosely ls.
5456 * dircolors invocation:: Color setup for ls, etc.
5461 @section @command{ls}: List directory contents
5464 @cindex directory listing
5466 The @command{ls} program lists information about files (of any type,
5467 including directories). Options and file arguments can be intermixed
5468 arbitrarily, as usual.
5470 For non-option command-line arguments that are directories, by default
5471 @command{ls} lists the contents of directories, not recursively, and
5472 omitting files with names beginning with @samp{.}. For other non-option
5473 arguments, by default @command{ls} lists just the file name. If no
5474 non-option argument is specified, @command{ls} operates on the current
5475 directory, acting as if it had been invoked with a single argument of @samp{.}.
5478 By default, the output is sorted alphabetically, according to the locale
5479 settings in effect.@footnote{If you use a non-@acronym{POSIX}
5480 locale (e.g., by setting @env{LC_ALL} to @samp{en_US}), then @command{ls} may
5481 produce output that is sorted differently than you're accustomed to.
5482 In that case, set the @env{LC_ALL} environment variable to @samp{C}.}
5483 If standard output is
5484 a terminal, the output is in columns (sorted vertically) and control
5485 characters are output as question marks; otherwise, the output is listed
5486 one per line and control characters are output as-is.
5488 Because @command{ls} is such a fundamental program, it has accumulated many
5489 options over the years. They are described in the subsections below;
5490 within each section, options are listed alphabetically (ignoring case).
5491 The division of options into the subsections is not absolute, since some
5492 options affect more than one aspect of @command{ls}'s operation.
5494 @cindex exit status of @command{ls}
5499 1 minor problems (e.g., a subdirectory was not found)
5500 2 serious trouble (e.g., memory exhausted)
5503 Also see @ref{Common options}.
5506 * Which files are listed::
5507 * What information is listed::
5508 * Sorting the output::
5509 * More details about version sort::
5510 * General output formatting::
5511 * Formatting file timestamps::
5512 * Formatting the file names::
5516 @node Which files are listed
5517 @subsection Which files are listed
5519 These options determine which files @command{ls} lists information for.
5520 By default, @command{ls} lists files and the contents of any
5521 directories on the command line, except that in directories it ignores
5522 files whose names start with @samp{.}.
5530 In directories, do not ignore file names that start with @samp{.}.
5535 @opindex --almost-all
5536 In directories, do not ignore all file names that start with @samp{.};
5537 ignore only @file{.} and @file{..}. The @option{--all} (@option{-a})
5538 option overrides this option.
5541 @itemx --ignore-backups
5543 @opindex --ignore-backups
5544 @cindex backup files, ignoring
5545 In directories, ignore files that end with @samp{~}. This option is
5546 equivalent to @samp{--ignore='*~' --ignore='.*~'}.
5551 @opindex --directory
5552 List just the names of directories, as with other types of files, rather
5553 than listing their contents.
5554 @c The following sentence is the same as the one for -F.
5555 Do not follow symbolic links listed on the
5556 command line unless the @option{--dereference-command-line} (@option{-H}),
5557 @option{--dereference} (@option{-L}), or
5558 @option{--dereference-command-line-symlink-to-dir} options are specified.
5561 @itemx --dereference-command-line
5563 @opindex --dereference-command-line
5564 @cindex symbolic links, dereferencing
5565 If a command line argument specifies a symbolic link, show information
5566 for the file the link references rather than for the link itself.
5568 @itemx --dereference-command-line-symlink-to-dir
5569 @opindex --dereference-command-line-symlink-to-dir
5570 @cindex symbolic links, dereferencing
5571 Do not dereference symbolic links, with one exception:
5572 if a command line argument specifies a symbolic link that refers to
5573 a directory, show information for that directory rather than for the
5575 This is the default behavior when no other dereferencing-related
5576 option has been specified (@option{--classify} (@option{-F}),
5577 @option{--directory} (@option{-d}),
5579 @option{--dereference} (@option{-L}), or
5580 @option{--dereference-command-line} (@option{-H})).
5582 @item --group-directories-first
5583 @opindex --group-directories-first
5584 Group all the directories before the files and then sort the
5585 directories and the files separately using the selected sort key
5586 (see --sort option).
5587 That is, this option specifies a primary sort key,
5588 and the --sort option specifies a secondary key.
5590 @item --hide=PATTERN
5591 @opindex --hide=@var{pattern}
5592 In directories, ignore files whose names match the shell pattern
5593 @var{pattern}, unless the @option{--all} (@option{-a}) or
5594 @option{--almost-all} (@option{-A}) is also given. This
5595 option acts like @option{--ignore=@var{pattern}} except that it has no
5596 effect if @option{--all} (@option{-a}) or @option{--almost-all}
5597 (@option{-A}) is also given.
5599 This option can be useful in shell aliases. For example, if
5600 @command{lx} is an alias for @samp{ls --hide='*~'} and @command{ly} is
5601 an alias for @samp{ls --ignore='*~'}, then the command @samp{lx -A}
5602 lists the file @file{README~} even though @samp{ly -A} would not.
5604 @item -I @var{pattern}
5605 @itemx --ignore=@var{pattern}
5607 @opindex --ignore=@var{pattern}
5608 In directories, ignore files whose names match the shell pattern
5609 (not regular expression) @var{pattern}. As
5610 in the shell, an initial @samp{.} in a file name does not match a
5611 wildcard at the start of @var{pattern}. Sometimes it is useful
5612 to give this option several times. For example,
5615 $ ls --ignore='.??*' --ignore='.[^.]' --ignore='#*'
5618 The first option ignores names of length 3 or more that start with @samp{.},
5619 the second ignores all two-character names that start with @samp{.}
5620 except @samp{..}, and the third ignores names that start with @samp{#}.
5623 @itemx --dereference
5625 @opindex --dereference
5626 @cindex symbolic links, dereferencing
5627 When showing file information for a symbolic link, show information
5628 for the file the link references rather than the link itself.
5629 However, even with this option, @command{ls} still prints the name
5630 of the link itself, not the name of the file that the link points to.
5635 @opindex --recursive
5636 @cindex recursive directory listing
5637 @cindex directory listing, recursive
5638 List the contents of all directories recursively.
5643 @node What information is listed
5644 @subsection What information is listed
5646 These options affect the information that @command{ls} displays. By
5647 default, only file names are shown.
5653 @cindex hurd, author, printing
5654 List each file's author when producing long format directory listings.
5655 In GNU/Hurd, file authors can differ from their owners, but in other
5656 operating systems the two are the same.
5662 @cindex dired Emacs mode support
5663 With the long listing (@option{-l}) format, print an additional line after
5667 //DIRED// @var{beg1} @var{end1} @var{beg2} @var{end2} @dots{}
5671 The @var{begN} and @var{endN} are unsigned integers that record the
5672 byte position of the beginning and end of each file name in the output.
5673 This makes it easy for Emacs to find the names, even when they contain
5674 unusual characters such as space or newline, without fancy searching.
5676 If directories are being listed recursively (@option{-R}), output a similar
5677 line with offsets for each subdirectory name:
5680 //SUBDIRED// @var{beg1} @var{end1} @dots{}
5683 Finally, output a line of the form:
5686 //DIRED-OPTIONS// --quoting-style=@var{word}
5690 where @var{word} is the quoting style (@pxref{Formatting the file names}).
5692 Here is an actual example:
5695 $ mkdir -p a/sub/deeper a/sub2
5697 $ touch a/sub/deeper/file
5698 $ ls -gloRF --dired a
5701 -rw-r--r-- 1 0 Jun 10 12:27 f1
5702 -rw-r--r-- 1 0 Jun 10 12:27 f2
5703 drwxr-xr-x 3 4096 Jun 10 12:27 sub/
5704 drwxr-xr-x 2 4096 Jun 10 12:27 sub2/
5708 drwxr-xr-x 2 4096 Jun 10 12:27 deeper/
5712 -rw-r--r-- 1 0 Jun 10 12:27 file
5716 //DIRED// 48 50 84 86 120 123 158 162 217 223 282 286
5717 //SUBDIRED// 2 3 167 172 228 240 290 296
5718 //DIRED-OPTIONS// --quoting-style=literal
5721 Note that the pairs of offsets on the @samp{//DIRED//} line above delimit
5722 these names: @file{f1}, @file{f2}, @file{sub}, @file{sub2}, @file{deeper},
5724 The offsets on the @samp{//SUBDIRED//} line delimit the following
5725 directory names: @file{a}, @file{a/sub}, @file{a/sub/deeper}, @file{a/sub2}.
5727 Here is an example of how to extract the fifth entry name, @samp{deeper},
5728 corresponding to the pair of offsets, 222 and 228:
5731 $ ls -gloRF --dired a > out
5732 $ dd bs=1 skip=222 count=6 < out 2>/dev/null; echo
5736 Note that although the listing above includes a trailing slash
5737 for the @samp{deeper} entry, the offsets select the name without
5738 the trailing slash. However, if you invoke @command{ls} with @option{--dired}
5739 along with an option like @option{--escape} (aka @option{-b}) and operate
5740 on a file whose name contains special characters, notice that the backslash
5745 $ ls -blog --dired 'a b'
5746 -rw-r--r-- 1 0 Jun 10 12:28 a\ b
5748 //DIRED-OPTIONS// --quoting-style=escape
5751 If you use a quoting style that adds quote marks
5752 (e.g., @option{--quoting-style=c}), then the offsets include the quote marks.
5753 So beware that the user may select the quoting style via the environment
5754 variable @env{QUOTING_STYLE}. Hence, applications using @option{--dired}
5755 should either specify an explicit @option{--quoting-style=literal} option
5756 (aka @option{-N} or @option{--literal}) on the command line, or else be
5757 prepared to parse the escaped names.
5760 @opindex --full-time
5761 Produce long format directory listings, and list times in full. It is
5762 equivalent to using @option{--format=long} with
5763 @option{--time-style=full-iso} (@pxref{Formatting file timestamps}).
5767 Produce long format directory listings, but don't display owner information.
5773 Inhibit display of group information in a long format directory listing.
5774 (This is the default in some non-@sc{gnu} versions of @command{ls}, so we
5775 provide this option for compatibility.)
5783 @cindex inode number, printing
5784 Print the inode number (also called the file serial number and index
5785 number) of each file to the left of the file name. (This number
5786 uniquely identifies each file within a particular file system.)
5789 @itemx --format=long
5790 @itemx --format=verbose
5793 @opindex long ls @r{format}
5794 @opindex verbose ls @r{format}
5795 In addition to the name of each file, print the file type, file mode bits,
5796 number of hard links, owner name, group name, size, and
5797 timestamp (@pxref{Formatting file timestamps}), normally
5798 the modification time.
5800 Normally the size is printed as a byte count without punctuation, but
5801 this can be overridden (@pxref{Block size}). For example, @option{-h}
5802 prints an abbreviated, human-readable count, and
5803 @samp{--block-size="'1"} prints a byte count with the thousands
5804 separator of the current locale.
5806 For each directory that is listed, preface the files with a line
5807 @samp{total @var{blocks}}, where @var{blocks} is the total disk allocation
5808 for all files in that directory. The block size currently defaults to 1024
5809 bytes, but this can be overridden (@pxref{Block size}).
5810 The @var{blocks} computed counts each hard link separately;
5811 this is arguably a deficiency.
5813 The file type is one of the following characters:
5815 @c The commented-out entries are ones we're not sure about.
5823 character special file
5825 high performance (``contiguous data'') file
5829 door (Solaris 2.5 and up)
5831 @c semaphore, if this is a distinct file type
5835 @c multiplexed file (7th edition Unix; obsolete)
5837 off-line (``migrated'') file (Cray DMF)
5839 network special file (HP-UX)
5843 port (Solaris 10 and up)
5845 @c message queue, if this is a distinct file type
5849 @c shared memory object, if this is a distinct file type
5851 @c typed memory object, if this is a distinct file type
5853 @c whiteout (4.4BSD; not implemented)
5855 some other file type
5858 @cindex permissions, output by @command{ls}
5859 The file mode bits listed are similar to symbolic mode specifications
5860 (@pxref{Symbolic Modes}). But @command{ls} combines multiple bits into the
5861 third character of each set of permissions as follows:
5865 If the set-user-ID or set-group-ID bit and the corresponding executable bit
5869 If the set-user-ID or set-group-ID bit is set but the corresponding
5870 executable bit is not set.
5873 If the restricted deletion flag or sticky bit, and the
5874 other-executable bit, are both set. The restricted deletion flag is
5875 another name for the sticky bit. @xref{Mode Structure}.
5878 If the restricted deletion flag or sticky bit is set but the
5879 other-executable bit is not set.
5882 If the executable bit is set and none of the above apply.
5888 Following the file mode bits is a single character that specifies
5889 whether an alternate access method such as an access control list
5890 applies to the file. When the character following the file mode bits is a
5891 space, there is no alternate access method. When it is a printing
5892 character, then there is such a method.
5894 For a file with an extended access control list, a @samp{+} character is
5895 listed. Basic access control lists are equivalent to the permissions
5896 listed, and are not considered an alternate access method.
5899 @itemx --numeric-uid-gid
5901 @opindex --numeric-uid-gid
5902 @cindex numeric uid and gid
5903 @cindex numeric user and group IDs
5904 Produce long format directory listings, but
5905 display numeric user and group IDs instead of the owner and group names.
5909 Produce long format directory listings, but don't display group information.
5910 It is equivalent to using @option{--format=long} with @option{--no-group} .
5916 @cindex disk allocation
5917 @cindex size of files, reporting
5918 Print the disk allocation of each file to the left of the file name.
5919 This is the amount of disk space used by the file, which is usually a
5920 bit more than the file's size, but it can be less if the file has holes.
5922 Normally the disk allocation is printed in units of
5923 1024 bytes, but this can be overridden (@pxref{Block size}).
5925 @cindex NFS mounts from BSD to HP-UX
5926 For files that are NFS-mounted from an HP-UX system to a BSD system,
5927 this option reports sizes that are half the correct values. On HP-UX
5928 systems, it reports sizes that are twice the correct values for files
5929 that are NFS-mounted from BSD systems. This is due to a flaw in HP-UX;
5930 it also affects the HP-UX @command{ls} program.
5937 @node Sorting the output
5938 @subsection Sorting the output
5940 @cindex sorting @command{ls} output
5941 These options change the order in which @command{ls} sorts the information
5942 it outputs. By default, sorting is done by character code
5943 (e.g., @acronym{ASCII} order).
5949 @itemx --time=status
5952 @opindex ctime@r{, printing or sorting by}
5953 @opindex status time@r{, printing or sorting by}
5954 @opindex use time@r{, printing or sorting files by}
5955 If the long listing format (e.g., @option{-l}, @option{-o}) is being used,
5956 print the status change time (the @samp{ctime} in the inode) instead of
5957 the modification time.
5958 When explicitly sorting by time (@option{--sort=time} or @option{-t})
5959 or when not using a long listing format,
5960 sort according to the status change time.
5964 @cindex unsorted directory listing
5965 @cindex directory order, listing by
5966 Primarily, like @option{-U}---do not sort; list the files in whatever
5967 order they are stored in the directory. But also enable @option{-a} (list
5968 all files) and disable @option{-l}, @option{--color}, and @option{-s} (if they
5969 were specified before the @option{-f}).
5975 @cindex reverse sorting
5976 Reverse whatever the sorting method is---e.g., list files in reverse
5977 alphabetical order, youngest first, smallest first, or whatever.
5983 @opindex size of files@r{, sorting files by}
5984 Sort by file size, largest first.
5990 @opindex modification time@r{, sorting files by}
5991 Sort by modification time (the @samp{mtime} in the inode), newest first.
5995 @itemx --time=access
5999 @opindex use time@r{, printing or sorting files by}
6000 @opindex atime@r{, printing or sorting files by}
6001 @opindex access time@r{, printing or sorting files by}
6002 If the long listing format (e.g., @option{--format=long}) is being used,
6003 print the last access time (the @samp{atime} in the inode).
6004 When explicitly sorting by time (@option{--sort=time} or @option{-t})
6005 or when not using a long listing format, sort according to the access time.
6011 @opindex none@r{, sorting option for @command{ls}}
6012 Do not sort; list the files in whatever order they are
6013 stored in the directory. (Do not do any of the other unrelated things
6014 that @option{-f} does.) This is especially useful when listing very large
6015 directories, since not doing any sorting can be noticeably faster.
6018 @itemx --sort=version
6021 @opindex version@r{, sorting option for @command{ls}}
6022 Sort by version name and number, lowest first. It behaves like a default
6023 sort, except that each sequence of decimal digits is treated numerically
6024 as an index/version number. (@xref{More details about version sort}.)
6027 @itemx --sort=extension
6030 @opindex extension@r{, sorting files by}
6031 Sort directory contents alphabetically by file extension (characters
6032 after the last @samp{.}); files with no extension are sorted first.
6037 @node More details about version sort
6038 @subsection More details about version sort
6040 The version sort takes into account the fact that file names frequently include
6041 indices or version numbers. Standard sorting functions usually do not produce
6042 the ordering that people expect because comparisons are made on a
6043 character-by-character basis. The version
6044 sort addresses this problem, and is especially useful when browsing
6045 directories that contain many files with indices/version numbers in their
6050 foo.zml-1.gz foo.zml-1.gz
6051 foo.zml-100.gz foo.zml-2.gz
6052 foo.zml-12.gz foo.zml-6.gz
6053 foo.zml-13.gz foo.zml-12.gz
6054 foo.zml-2.gz foo.zml-13.gz
6055 foo.zml-25.gz foo.zml-25.gz
6056 foo.zml-6.gz foo.zml-100.gz
6059 Note also that numeric parts with leading zeros are considered as
6064 abc-1.007.tgz abc-1.007.tgz
6065 abc-1.012b.tgz abc-1.01a.tgz
6066 abc-1.01a.tgz abc-1.012b.tgz
6069 This functionality is implemented using the @code{strverscmp} function.
6070 @xref{String/Array Comparison, , , libc, The GNU C Library Reference Manual}.
6071 One result of that implementation decision is that @code{ls -v} does not
6072 use the locale category, @env{LC_COLLATE}. As a result, non-numeric prefixes
6073 are sorted as if @env{LC_COLLATE} were set to @code{C}.
6075 @node General output formatting
6076 @subsection General output formatting
6078 These options affect the appearance of the overall output.
6083 @itemx --format=single-column
6086 @opindex single-column @r{output of files}
6087 List one file per line. This is the default for @command{ls} when standard
6088 output is not a terminal.
6091 @itemx --format=vertical
6094 @opindex vertical @r{sorted files in columns}
6095 List files in columns, sorted vertically. This is the default for
6096 @command{ls} if standard output is a terminal. It is always the default
6097 for the @command{dir} program.
6098 @sc{gnu} @command{ls} uses variable width columns to display as many files as
6099 possible in the fewest lines.
6101 @item --color [=@var{when}]
6103 @cindex color, distinguishing file types with
6104 Specify whether to use color for distinguishing file types. @var{when}
6105 may be omitted, or one of:
6108 @vindex none @r{color option}
6109 - Do not use color at all. This is the default.
6111 @vindex auto @r{color option}
6112 @cindex terminal, using color iff
6113 - Only use color if standard output is a terminal.
6115 @vindex always @r{color option}
6118 Specifying @option{--color} and no @var{when} is equivalent to
6119 @option{--color=always}.
6120 Piping a colorized listing through a pager like @command{more} or
6121 @command{less} usually produces unreadable results. However, using
6122 @code{more -f} does seem to work.
6126 @itemx --indicator-style=classify
6129 @opindex --indicator-style
6130 @cindex file type and executables, marking
6131 @cindex executables and file type, marking
6132 Append a character to each file name indicating the file type. Also,
6133 for regular files that are executable, append @samp{*}. The file type
6134 indicators are @samp{/} for directories, @samp{@@} for symbolic links,
6135 @samp{|} for FIFOs, @samp{=} for sockets, @samp{>} for doors,
6136 and nothing for regular files.
6137 @c The following sentence is the same as the one for -d.
6138 Do not follow symbolic links listed on the
6139 command line unless the @option{--dereference-command-line} (@option{-H}),
6140 @option{--dereference} (@option{-L}), or
6141 @option{--dereference-command-line-symlink-to-dir} options are specified.
6144 @itemx --indicator-style=file-type
6145 @opindex --file-type
6146 @opindex --indicator-style
6147 @cindex file type, marking
6148 Append a character to each file name indicating the file type. This is
6149 like @option{-F}, except that executables are not marked.
6151 @item --indicator-style=@var{word}
6152 @opindex --indicator-style
6153 Append a character indicator with style @var{word} to entry names,
6158 Do not append any character indicator; this is the default.
6160 Append @samp{/} for directories. This is the same as the @option{-p}
6163 Append @samp{/} for directories, @samp{@@} for symbolic links, @samp{|}
6164 for FIFOs, @samp{=} for sockets, and nothing for regular files. This is
6165 the same as the @option{--file-type} option.
6167 Append @samp{*} for executable regular files, otherwise behave as for
6168 @samp{file-type}. This is the same as the @option{-F} or
6169 @option{--classify} option.
6174 Print file sizes in 1024-byte blocks, overriding the default block
6175 size (@pxref{Block size}).
6176 This option is equivalent to @option{--block-size=1K}.
6179 @itemx --format=commas
6182 @opindex commas@r{, outputting between files}
6183 List files horizontally, with as many as will fit on each line,
6184 separated by @samp{, } (a comma and a space).
6187 @itemx --indicator-style=slash
6189 @opindex --indicator-style
6190 @cindex file type, marking
6191 Append a @samp{/} to directory names.
6194 @itemx --format=across
6195 @itemx --format=horizontal
6198 @opindex across@r{, listing files}
6199 @opindex horizontal@r{, listing files}
6200 List the files in columns, sorted horizontally.
6203 @itemx --tabsize=@var{cols}
6206 Assume that each tab stop is @var{cols} columns wide. The default is 8.
6207 @command{ls} uses tabs where possible in the output, for efficiency. If
6208 @var{cols} is zero, do not use tabs at all.
6211 @itemx --width=@var{cols}
6215 Assume the screen is @var{cols} columns wide. The default is taken
6216 from the terminal settings if possible; otherwise the environment
6217 variable @env{COLUMNS} is used if it is set; otherwise the default
6223 @node Formatting file timestamps
6224 @subsection Formatting file timestamps
6226 By default, file timestamps are listed in abbreviated form. Most
6227 locales use a timestamp like @samp{2002-03-30 23:45}. However, the
6228 default @acronym{POSIX} locale uses a date like @samp{Mar 30@ @ 2002}
6229 for non-recent timestamps, and a date-without-year and time like
6230 @samp{Mar 30 23:45} for recent timestamps.
6232 A timestamp is considered to be @dfn{recent} if it is less than six
6233 months old, and is not dated in the future. If a timestamp dated
6234 today is not listed in recent form, the timestamp is in the future,
6235 which means you probably have clock skew problems which may break
6236 programs like @command{make} that rely on file timestamps.
6239 Time stamps are listed according to the time zone rules specified by
6240 the @env{TZ} environment variable, or by the system default rules if
6241 @env{TZ} is not set. @xref{TZ Variable,, Specifying the Time Zone
6242 with @env{TZ}, libc, The GNU C Library}.
6244 The following option changes how file timestamps are printed.
6247 @item --time-style=@var{style}
6248 @opindex --time-style
6250 List timestamps in style @var{style}. The @var{style} should
6251 be one of the following:
6256 List timestamps using @var{format}, where @var{format} is interpreted
6257 like the format argument of @command{date} (@pxref{date invocation}).
6258 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
6259 @command{ls} to list timestamps like @samp{2002-03-30 23:45:56}. As
6260 with @command{date}, @var{format}'s interpretation is affected by the
6261 @env{LC_TIME} locale category.
6263 If @var{format} contains two format strings separated by a newline,
6264 the former is used for non-recent files and the latter for recent
6265 files; if you want output columns to line up, you may need to insert
6266 spaces in one of the two formats.
6269 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
6270 format with nanosecond precision, e.g., @samp{2002-03-30
6271 23:45:56.477817180 -0700}. This style is equivalent to
6272 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
6274 This is useful because the time output includes all the information that
6275 is available from the operating system. For example, this can help
6276 explain @command{make}'s behavior, since @acronym{GNU} @command{make}
6277 uses the full timestamp to determine whether a file is out of date.
6280 List @acronym{ISO} 8601 date and time in minutes, e.g.,
6281 @samp{2002-03-30 23:45}. These timestamps are shorter than
6282 @samp{full-iso} timestamps, and are usually good enough for everyday
6283 work. This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
6286 List @acronym{ISO} 8601 dates for non-recent timestamps (e.g.,
6287 @samp{2002-03-30@ }), and @acronym{ISO} 8601 month, day, hour, and
6288 minute for recent timestamps (e.g., @samp{03-30 23:45}). These
6289 timestamps are uglier than @samp{long-iso} timestamps, but they carry
6290 nearly the same information in a smaller space and their brevity helps
6291 @command{ls} output fit within traditional 80-column output lines.
6292 The following two @command{ls} invocations are equivalent:
6297 ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
6298 ls -l --time-style="iso"
6303 List timestamps in a locale-dependent form. For example, a Finnish
6304 locale might list non-recent timestamps like @samp{maalis 30@ @ 2002}
6305 and recent timestamps like @samp{maalis 30 23:45}. Locale-dependent
6306 timestamps typically consume more space than @samp{iso} timestamps and
6307 are harder for programs to parse because locale conventions vary so
6308 widely, but they are easier for many people to read.
6310 The @env{LC_TIME} locale category specifies the timestamp format. The
6311 default @acronym{POSIX} locale uses timestamps like @samp{Mar 30@
6312 @ 2002} and @samp{Mar 30 23:45}; in this locale, the following two
6313 @command{ls} invocations are equivalent:
6318 ls -l --time-style="+%b %e %Y$newline%b %e %H:%M"
6319 ls -l --time-style="locale"
6322 Other locales behave differently. For example, in a German locale,
6323 @option{--time-style="locale"} might be equivalent to
6324 @option{--time-style="+%e. %b %Y $newline%e. %b %H:%M"}
6325 and might generate timestamps like @samp{30. M@"ar 2002@ } and
6326 @samp{30. M@"ar 23:45}.
6328 @item posix-@var{style}
6330 List @acronym{POSIX}-locale timestamps if the @env{LC_TIME} locale
6331 category is @acronym{POSIX}, @var{style} timestamps otherwise. For
6332 example, the @samp{posix-long-iso} style lists
6333 timestamps like @samp{Mar 30@ @ 2002} and @samp{Mar 30 23:45} when in
6334 the @acronym{POSIX} locale, and like @samp{2002-03-30 23:45} otherwise.
6339 You can specify the default value of the @option{--time-style} option
6340 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
6341 the default style is @samp{locale}. @acronym{GNU} Emacs 21.3 and
6342 later use the @option{--dired} option and therefore can parse any date
6343 format, but if you are using Emacs 21.1 or 21.2 and specify a
6344 non-@acronym{POSIX} locale you may need to set
6345 @samp{TIME_STYLE="posix-long-iso"}.
6347 To avoid certain denial-of-service attacks, timestamps that would be
6348 longer than 1000 bytes may be treated as errors.
6351 @node Formatting the file names
6352 @subsection Formatting the file names
6354 These options change how file names themselves are printed.
6360 @itemx --quoting-style=escape
6363 @opindex --quoting-style
6364 @cindex backslash sequences for file names
6365 Quote nongraphic characters in file names using alphabetic and octal
6366 backslash sequences like those used in C.
6370 @itemx --quoting-style=literal
6373 @opindex --quoting-style
6374 Do not quote file names. However, with @command{ls} nongraphic
6375 characters are still printed as question marks if the output is a
6376 terminal and you do not specify the @option{--show-control-chars}
6380 @itemx --hide-control-chars
6382 @opindex --hide-control-chars
6383 Print question marks instead of nongraphic characters in file names.
6384 This is the default if the output is a terminal and the program is
6389 @itemx --quoting-style=c
6391 @opindex --quote-name
6392 @opindex --quoting-style
6393 Enclose file names in double quotes and quote nongraphic characters as
6396 @item --quoting-style=@var{word}
6397 @opindex --quoting-style
6398 @cindex quoting style
6399 Use style @var{word} to quote file names and other strings that may
6400 contain arbitrary characters. The @var{word} should
6401 be one of the following:
6405 Output strings as-is; this is the same as the @option{-N} or
6406 @option{--literal} option.
6408 Quote strings for the shell if they contain shell metacharacters or would
6409 cause ambiguous output.
6410 The quoting is suitable for @acronym{POSIX}-compatible shells like
6411 @command{bash}, but it does not always work for incompatible shells
6414 Quote strings for the shell, even if they would normally not require quoting.
6416 Quote strings as for C character string literals, including the
6417 surrounding double-quote characters; this is the same as the
6418 @option{-Q} or @option{--quote-name} option.
6420 Quote strings as for C character string literals, except omit the
6421 surrounding double-quote
6422 characters; this is the same as the @option{-b} or @option{--escape} option.
6424 Quote strings as for C character string literals, except use
6425 surrounding quotation marks appropriate for the
6428 @c Use @t instead of @samp to avoid duplicate quoting in some output styles.
6429 Quote strings as for C character string literals, except use
6430 surrounding quotation marks appropriate for the locale, and quote
6431 @t{`like this'} instead of @t{"like
6432 this"} in the default C locale. This looks nicer on many displays.
6435 You can specify the default value of the @option{--quoting-style} option
6436 with the environment variable @env{QUOTING_STYLE}. If that environment
6437 variable is not set, the default value is @samp{literal}, but this
6438 default may change to @samp{shell} in a future version of this package.
6440 @item --show-control-chars
6441 @opindex --show-control-chars
6442 Print nongraphic characters as-is in file names.
6443 This is the default unless the output is a terminal and the program is
6449 @node dir invocation
6450 @section @command{dir}: Briefly list directory contents
6453 @cindex directory listing, brief
6455 @command{dir} is equivalent to @code{ls -C
6456 -b}; that is, by default files are listed in columns, sorted vertically,
6457 and special characters are represented by backslash escape sequences.
6459 @xref{ls invocation, @command{ls}}.
6462 @node vdir invocation
6463 @section @command{vdir}: Verbosely list directory contents
6466 @cindex directory listing, verbose
6468 @command{vdir} is equivalent to @code{ls -l
6469 -b}; that is, by default files are listed in long format and special
6470 characters are represented by backslash escape sequences.
6472 @node dircolors invocation
6473 @section @command{dircolors}: Color setup for @command{ls}
6477 @cindex setup for color
6479 @command{dircolors} outputs a sequence of shell commands to set up the
6480 terminal for color output from @command{ls} (and @command{dir}, etc.).
6484 eval "`dircolors [@var{option}]@dots{} [@var{file}]`"
6487 If @var{file} is specified, @command{dircolors} reads it to determine which
6488 colors to use for which file types and extensions. Otherwise, a
6489 precompiled database is used. For details on the format of these files,
6490 run @samp{dircolors --print-database}.
6493 @vindex SHELL @r{environment variable, and color}
6494 The output is a shell command to set the @env{LS_COLORS} environment
6495 variable. You can specify the shell syntax to use on the command line,
6496 or @command{dircolors} will guess it from the value of the @env{SHELL}
6497 environment variable.
6499 The program accepts the following options. Also see @ref{Common options}.
6504 @itemx --bourne-shell
6507 @opindex --bourne-shell
6508 @cindex Bourne shell syntax for color setup
6509 @cindex @command{sh} syntax for color setup
6510 Output Bourne shell commands. This is the default if the @env{SHELL}
6511 environment variable is set and does not end with @samp{csh} or
6520 @cindex C shell syntax for color setup
6521 @cindex @command{csh} syntax for color setup
6522 Output C shell commands. This is the default if @code{SHELL} ends with
6523 @command{csh} or @command{tcsh}.
6526 @itemx --print-database
6528 @opindex --print-database
6529 @cindex color database, printing
6530 @cindex database for color setup, printing
6531 @cindex printing color database
6532 Print the (compiled-in) default color configuration database. This
6533 output is itself a valid configuration file, and is fairly descriptive
6534 of the possibilities.
6541 @node Basic operations
6542 @chapter Basic operations
6544 @cindex manipulating files
6546 This chapter describes the commands for basic file manipulation:
6547 copying, moving (renaming), and deleting (removing).
6550 * cp invocation:: Copy files.
6551 * dd invocation:: Convert and copy a file.
6552 * install invocation:: Copy files and set attributes.
6553 * mv invocation:: Move (rename) files.
6554 * rm invocation:: Remove files or directories.
6555 * shred invocation:: Remove files more securely.
6560 @section @command{cp}: Copy files and directories
6563 @cindex copying files and directories
6564 @cindex files, copying
6565 @cindex directories, copying
6567 @command{cp} copies files (or, optionally, directories). The copy is
6568 completely independent of the original. You can either copy one file to
6569 another, or copy arbitrarily many files to a destination directory.
6573 cp [@var{option}]@dots{} [-T] @var{source} @var{dest}
6574 cp [@var{option}]@dots{} @var{source}@dots{} @var{directory}
6575 cp [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
6580 If two file names are given, @command{cp} copies the first file to the
6584 If the @option{--target-directory} (@option{-t}) option is given, or
6585 failing that if the last file is a directory and the
6586 @option{--no-target-directory} (@option{-T}) option is not given,
6587 @command{cp} copies each @var{source} file to the specified directory,
6588 using the @var{source}s' names.
6591 Generally, files are written just as they are read. For exceptions,
6592 see the @option{--sparse} option below.
6594 By default, @command{cp} does not copy directories. However, the
6595 @option{-R}, @option{-a}, and @option{-r} options cause @command{cp} to
6596 copy recursively by descending into source directories and copying files
6597 to corresponding destination directories.
6599 By default, @command{cp} follows symbolic links only when not copying
6600 recursively. This default can be overridden with the
6601 @option{--archive} (@option{-a}), @option{-d}, @option{--dereference}
6602 (@option{-L}), @option{--no-dereference} (@option{-P}), and
6603 @option{-H} options. If more than one of these options is specified,
6604 the last one silently overrides the others.
6606 By default, @command{cp} copies the contents of special files only
6607 when not copying recursively. This default can be overridden with the
6608 @option{--copy-contents} option.
6610 @cindex self-backups
6611 @cindex backups, making only
6612 @command{cp} generally refuses to copy a file onto itself, with the
6613 following exception: if @option{--force --backup} is specified with
6614 @var{source} and @var{dest} identical, and referring to a regular file,
6615 @command{cp} will make a backup file, either regular or numbered, as
6616 specified in the usual ways (@pxref{Backup options}). This is useful when
6617 you simply want to make a backup of an existing file before changing it.
6619 The program accepts the following options. Also see @ref{Common options}.
6626 Preserve as much as possible of the structure and attributes of the
6627 original files in the copy (but do not attempt to preserve internal
6628 directory structure; i.e., @samp{ls -U} may list the entries in a copied
6629 directory in a different order).
6630 Equivalent to @option{-dpPR}.
6633 @itemx @w{@kbd{--backup}[=@var{method}]}
6636 @vindex VERSION_CONTROL
6637 @cindex backups, making
6638 @xref{Backup options}.
6639 Make a backup of each file that would otherwise be overwritten or removed.
6640 As a special case, @command{cp} makes a backup of @var{source} when the force
6641 and backup options are given and @var{source} and @var{dest} are the same
6642 name for an existing, regular file. One useful application of this
6643 combination of options is this tiny Bourne shell script:
6647 # Usage: backup FILE...
6648 # Create a @sc{gnu}-style backup of each listed FILE.
6650 cp --backup --force -- "$i" "$i"
6654 @item --copy-contents
6655 @cindex directories, copying recursively
6656 @cindex copying directories recursively
6657 @cindex recursively copying directories
6658 @cindex non-directories, copying as special files
6659 If copying recursively, copy the contents of any special files (e.g.,
6660 FIFOs and device files) as if they were regular files. This means
6661 trying to read the data in each source file and writing it to the
6662 destination. It is usually a mistake to use this option, as it
6663 normally has undesirable effects on special files like FIFOs and the
6664 ones typically found in the @file{/dev} directory. In most cases,
6665 @code{cp -R --copy-contents} will hang indefinitely trying to read
6666 from FIFOs and special files like @file{/dev/console}, and it will
6667 fill up your destination disk if you use it to copy @file{/dev/zero}.
6668 This option has no effect unless copying recursively, and it does not
6669 affect the copying of symbolic links.
6673 @cindex symbolic links, copying
6674 @cindex hard links, preserving
6675 Copy symbolic links as symbolic links rather than copying the files that
6676 they point to, and preserve hard links between source files in the copies.
6677 Equivalent to @option{--no-dereference --preserve=links}.
6683 When copying without this option and an existing destination file cannot
6684 be opened for writing, the copy fails. However, with @option{--force}),
6685 when a destination file cannot be opened, @command{cp} then unlinks it and
6686 tries to open it again. Contrast this behavior with that enabled by
6687 @option{--link} and @option{--symbolic-link}, whereby the destination file
6688 is never opened but rather is unlinked unconditionally. Also see the
6689 description of @option{--remove-destination}.
6693 If a command line argument specifies a symbolic link, then copy the
6694 file it points to rather than the symbolic link itself. However,
6695 copy (preserving its nature) any symbolic link that is encountered
6696 via recursive traversal.
6699 @itemx --interactive
6701 @opindex --interactive
6702 Prompt whether to overwrite existing regular destination files.
6708 Make hard links instead of copies of non-directories.
6711 @itemx --dereference
6713 @opindex --dereference
6714 Always follow symbolic links.
6717 @itemx --no-dereference
6719 @opindex --no-dereference
6720 @cindex symbolic links, copying
6721 Copy symbolic links as symbolic links rather than copying the files that
6725 @itemx @w{@kbd{--preserve}[=@var{attribute_list}]}
6728 @cindex file information, preserving
6729 Preserve the specified attributes of the original files.
6730 If specified, the @var{attribute_list} must be a comma-separated list
6731 of one or more of the following strings:
6735 Preserve the file mode bits and access control lists.
6737 Preserve the owner and group. On most modern systems,
6738 only users with appropriate privileges may change the owner of a file,
6740 may preserve the group ownership of a file only if they happen to be
6741 a member of the desired group.
6743 Preserve the times of last access and last modification.
6745 Preserve in the destination files
6746 any links between corresponding source files.
6747 @c Give examples illustrating how hard links are preserved.
6748 @c Also, show how soft links map to hard links with -L and -H.
6750 Preserve all file attributes.
6751 Equivalent to specifying all of the above.
6754 Using @option{--preserve} with no @var{attribute_list} is equivalent
6755 to @option{--preserve=mode,ownership,timestamps}.
6757 In the absence of this option, each destination file is created with the
6758 mode bits of the corresponding source file, minus the bits set in the
6759 umask and minus the set-user-ID and set-group-ID bits.
6760 @xref{File permissions}.
6762 @itemx @w{@kbd{--no-preserve}=@var{attribute_list}}
6763 @cindex file information, preserving
6764 Do not preserve the specified attributes. The @var{attribute_list}
6765 has the same form as for @option{--preserve}.
6769 @cindex parent directories and @command{cp}
6770 Form the name of each destination file by appending to the target
6771 directory a slash and the specified name of the source file. The last
6772 argument given to @command{cp} must be the name of an existing directory.
6773 For example, the command:
6776 cp --parents a/b/c existing_dir
6780 copies the file @file{a/b/c} to @file{existing_dir/a/b/c}, creating
6781 any missing intermediate directories.
6783 @itemx @w{@kbd{--reply}=@var{how}}
6785 @cindex interactivity
6786 @c FIXME: remove in 2008
6787 @strong{Deprecated: to be removed in 2008.}@*
6788 Using @option{--reply=yes} makes @command{cp} act as if @samp{yes} were
6789 given as a response to every prompt about a destination file. That effectively
6790 cancels any preceding @option{--interactive} or @option{-i} option.
6791 Specify @option{--reply=no} to make @command{cp} act as if @samp{no} were
6792 given as a response to every prompt about a destination file.
6793 Specify @option{--reply=query} to make @command{cp} prompt the user
6794 about each existing destination file.
6801 @opindex --recursive
6802 @cindex directories, copying recursively
6803 @cindex copying directories recursively
6804 @cindex recursively copying directories
6805 @cindex non-directories, copying as special files
6806 Copy directories recursively. Symbolic links are not followed by
6807 default; see the @option{--archive} (@option{-a}), @option{-d},
6808 @option{--dereference} (@option{-L}), @option{--no-dereference}
6809 (@option{-P}), and @option{-H} options. Special files are copied by
6810 creating a destination file of the same type as the source; see the
6811 @option{--copy-contents} option. It is not portable to use
6812 @option{-r} to copy symbolic links or special files. On some
6813 non-@sc{gnu} systems, @option{-r} implies the equivalent of
6814 @option{-L} and @option{--copy-contents} for historical reasons.
6815 Also, it is not portable to use @option{-R} to copy symbolic links
6816 unless you also specify @option{-P}, as @acronym{POSIX} allows
6817 implementations that dereference symbolic links by default.
6819 @item --remove-destination
6820 @opindex --remove-destination
6821 Remove each existing destination file before attempting to open it
6822 (contrast with @option{-f} above).
6824 @item --sparse=@var{when}
6825 @opindex --sparse=@var{when}
6826 @cindex sparse files, copying
6827 @cindex holes, copying files with
6828 @findex read @r{system call, and holes}
6829 A @dfn{sparse file} contains @dfn{holes}---a sequence of zero bytes that
6830 does not occupy any physical disk blocks; the @samp{read} system call
6831 reads these as zeros. This can both save considerable disk space and
6832 increase speed, since many binary files contain lots of consecutive zero
6833 bytes. By default, @command{cp} detects holes in input source files via a crude
6834 heuristic and makes the corresponding output file sparse as well.
6835 Only regular files may be sparse.
6837 The @var{when} value can be one of the following:
6841 The default behavior: if the input file is sparse, attempt to make
6842 the output file sparse, too. However, if an output file exists but
6843 refers to a non-regular file, then do not attempt to make it sparse.
6846 For each sufficiently long sequence of zero bytes in the input file,
6847 attempt to create a corresponding hole in the output file, even if the
6848 input file does not appear to be sparse.
6849 This is useful when the input file resides on a file system
6850 that does not support sparse files
6851 (for example, @samp{efs} file systems in SGI IRIX 5.3 and earlier),
6852 but the output file is on a type of file system that does support them.
6853 Holes may be created only in regular files, so if the destination file
6854 is of some other type, @command{cp} does not even try to make it sparse.
6857 Never make the output file sparse.
6858 This is useful in creating a file for use with the @command{mkswap} command,
6859 since such a file must not have any holes.
6862 @optStripTrailingSlashes
6865 @itemx --symbolic-link
6867 @opindex --symbolic-link
6868 @cindex symbolic links, copying with
6869 Make symbolic links instead of copies of non-directories. All source
6870 file names must be absolute (starting with @samp{/}) unless the
6871 destination files are in the current directory. This option merely
6872 results in an error message on systems that do not support symbolic links.
6878 @optNoTargetDirectory
6884 @cindex newer files, copying only
6885 Do not copy a non-directory that has an existing destination with the
6886 same or newer modification time. If time stamps are being preserved,
6887 the comparison is to the source time stamp truncated to the
6888 resolutions of the destination file system and of the system calls
6889 used to update time stamps; this avoids duplicate work if several
6890 @samp{cp -pu} commands are executed with the same source and
6897 Print the name of each file before copying it.
6900 @itemx --one-file-system
6902 @opindex --one-file-system
6903 @cindex file systems, omitting copying to different
6904 Skip subdirectories that are on different file systems from the one that
6905 the copy started on.
6906 However, mount point directories @emph{are} copied.
6914 @section @command{dd}: Convert and copy a file
6917 @cindex converting while copying a file
6919 @command{dd} copies a file (from standard input to standard output, by
6920 default) with a changeable I/O block size, while optionally performing
6921 conversions on it. Synopses:
6924 dd [@var{operand}]@dots{}
6928 The only options are @option{--help} and @option{--version}.
6929 @xref{Common options}. @command{dd} accepts the following operands.
6935 Read from @var{file} instead of standard input.
6939 Write to @var{file} instead of standard output. Unless
6940 @samp{conv=notrunc} is given, @command{dd} truncates @var{file} to zero
6941 bytes (or the size specified with @samp{seek=}).
6943 @item ibs=@var{bytes}
6945 @cindex block size of input
6946 @cindex input block size
6947 Read @var{bytes} bytes at a time.
6949 @item obs=@var{bytes}
6951 @cindex block size of output
6952 @cindex output block size
6953 Write @var{bytes} bytes at a time.
6955 @item bs=@var{bytes}
6958 Both read and write @var{bytes} bytes at a time. This overrides
6959 @samp{ibs} and @samp{obs}.
6961 @item cbs=@var{bytes}
6963 @cindex block size of conversion
6964 @cindex conversion block size
6965 Convert @var{bytes} bytes at a time.
6967 @item skip=@var{blocks}
6969 Skip @var{blocks} @samp{ibs}-byte blocks in the input file before copying.
6971 @item seek=@var{blocks}
6973 Skip @var{blocks} @samp{obs}-byte blocks in the output file before copying.
6975 @item count=@var{blocks}
6977 Copy @var{blocks} @samp{ibs}-byte blocks from the input file, instead
6978 of everything until the end of the file.
6980 @item conv=@var{conversion}[,@var{conversion}]@dots{}
6982 Convert the file as specified by the @var{conversion} argument(s).
6983 (No spaces around any comma(s).)
6990 @opindex ascii@r{, converting to}
6991 Convert @acronym{EBCDIC} to @acronym{ASCII},
6992 using the conversion table specified by @acronym{POSIX}.
6993 This provides a 1:1 translation for all 256 bytes.
6996 @opindex ebcdic@r{, converting to}
6997 Convert @acronym{ASCII} to @acronym{EBCDIC}.
6998 This is the inverse of the @samp{ascii} conversion.
7001 @opindex alternate ebcdic@r{, converting to}
7002 Convert @acronym{ASCII} to alternate @acronym{EBCDIC},
7003 using the alternate conversion table specified by @acronym{POSIX}.
7004 This is not a 1:1 translation, but reflects common historical practice
7005 for @samp{~}, @samp{[}, and @samp{]}.
7007 The @samp{ascii}, @samp{ebcdic}, and @samp{ibm} conversions are
7011 @opindex block @r{(space-padding)}
7012 For each line in the input, output @samp{cbs} bytes, replacing the
7013 input newline with a space and padding with spaces as necessary.
7017 Replace trailing spaces in each @samp{cbs}-sized input block with a
7020 The @samp{block} and @samp{unblock} conversions are mutually exclusive.
7023 @opindex lcase@r{, converting to}
7024 Change uppercase letters to lowercase.
7027 @opindex ucase@r{, converting to}
7028 Change lowercase letters to uppercase.
7030 The @samp{lcase} and @samp{ucase} conversions are mutually exclusive.
7033 @opindex swab @r{(byte-swapping)}
7034 @cindex byte-swapping
7035 Swap every pair of input bytes. @sc{gnu} @command{dd}, unlike others, works
7036 when an odd number of bytes are read---the last byte is simply copied
7037 (since there is nothing to swap it with).
7041 @cindex read errors, ignoring
7042 Continue after read errors.
7046 @cindex creating output file, avoiding
7047 Do not create the output file; the output file must already exist.
7051 @cindex creating output file, requiring
7052 Fail if the output file already exists; @command{dd} must create the
7055 The @samp{excl} and @samp{nocreat} conversions are mutually exclusive.
7059 @cindex truncating output file, avoiding
7060 Do not truncate the output file.
7063 @opindex sync @r{(padding with nulls)}
7064 Pad every input block to size of @samp{ibs} with trailing zero bytes.
7065 When used with @samp{block} or @samp{unblock}, pad with spaces instead of
7070 @cindex synchronized data writes, before finishing
7071 Synchronize output data just before finishing. This forces a physical
7072 write of output data.
7076 @cindex synchronized data and metadata writes, before finishing
7077 Synchronize output data and metadata just before finishing. This
7078 forces a physical write of output data and metadata.
7082 @item iflag=@var{flag}[,@var{flag}]@dots{}
7084 Access the input file using the flags specified by the @var{flag}
7085 argument(s). (No spaces around any comma(s).)
7087 @item oflag=@var{flag}[,@var{flag}]@dots{}
7089 Access the output file using the flags specified by the @var{flag}
7090 argument(s). (No spaces around any comma(s).)
7092 Here are the flags. Not every flag is supported on every operating
7099 @cindex appending to the output file
7100 Write in append mode, so that even if some other process is writing to
7101 this file, every @command{dd} write will append to the current
7102 contents of the file. This flag makes sense only for output.
7107 Use direct I/O for data, avoiding the buffer cache.
7111 @cindex directory I/O
7113 Fail unless the file is a directory. Most operating systems do not
7114 allow I/O to a directory, so this flag has limited utility.
7118 @cindex synchronized data reads
7119 Use synchronized I/O for data. For the output file, this forces a
7120 physical write of output data on each write. For the input file,
7121 this flag can matter when reading from a remote file that has been
7122 written to synchronously by some other process. Metadata (e.g.,
7123 last-access and last-modified time) is not necessarily synchronized.
7127 @cindex synchronized data and metadata I/O
7128 Use synchronized I/O for both data and metadata.
7132 @cindex nonblocking I/O
7133 Use non-blocking I/O.
7138 Do not update the file's access time.
7142 @cindex controlling terminal
7143 Do not assign the file to be a controlling terminal for @command{dd}.
7144 This has no effect when the file is not a terminal.
7145 On many hosts (e.g., @acronym{GNU}/Linux hosts), this option has no effect
7150 @cindex symbolic links, following
7151 Do not follow symbolic links.
7156 Fail if the file has multiple hard links.
7161 Use binary I/O. This option has an effect only on nonstandard
7162 platforms that distinguish binary from text I/O.
7167 Use text I/O. Like @samp{binary}, this option has no effect on
7172 These flags are not supported on all systems, and @samp{dd} rejects
7173 attempts to use them when they are not supported. When reading from
7174 standard input or writing to standard output, the @samp{nofollow} and
7175 @samp{noctty} flags should not be specified, and the other flags
7176 (e.g., @samp{nonblock}) can affect how other processes behave with the
7177 affected file descriptors, even after @command{dd} exits.
7181 @cindex multipliers after numbers
7182 The numeric-valued strings above (@var{bytes} and @var{blocks}) can be
7183 followed by a multiplier: @samp{b}=512, @samp{c}=1,
7184 @samp{w}=2, @samp{x@var{m}}=@var{m}, or any of the
7185 standard block size suffixes like @samp{k}=1024 (@pxref{Block size}).
7187 Use different @command{dd} invocations to use different block sizes for
7188 skipping and I/O@. For example, the following shell commands copy data
7189 in 512 KiB blocks between a disk and a tape, but do not save or restore a
7190 4 KiB label at the start of the disk:
7193 disk=/dev/rdsk/c0t1d0s2
7196 # Copy all but the label from disk to tape.
7197 (dd bs=4k skip=1 count=0 && dd bs=512k) <$disk >$tape
7199 # Copy from tape back to disk, but leave the disk label alone.
7200 (dd bs=4k seek=1 count=0 && dd bs=512k) <$tape >$disk
7203 Sending an @samp{INFO} signal to a running @command{dd}
7204 process makes it print I/O statistics to standard error
7205 and then resume copying. In the example below,
7206 @command{dd} is run in the background to copy 10 million blocks.
7207 The @command{kill} command makes it output intermediate I/O statistics,
7208 and when @command{dd} completes, it outputs the final statistics.
7211 $ dd if=/dev/zero of=/dev/null count=10MB & pid=$!
7212 $ kill -s INFO $pid; wait $pid
7213 3385223+0 records in
7214 3385223+0 records out
7215 1733234176 bytes (1.7 GB) copied, 6.42173 seconds, 270 MB/s
7216 10000000+0 records in
7217 10000000+0 records out
7218 5120000000 bytes (5.1 GB) copied, 18.913 seconds, 271 MB/s
7221 @vindex POSIXLY_CORRECT
7222 On systems lacking the @samp{INFO} signal @command{dd} responds to the
7223 @samp{USR1} signal instead, unless the @env{POSIXLY_CORRECT}
7224 environment variable is set.
7229 @node install invocation
7230 @section @command{install}: Copy files and set attributes
7233 @cindex copying files and setting attributes
7235 @command{install} copies files while setting their file mode bits and, if
7236 possible, their owner and group. Synopses:
7239 install [@var{option}]@dots{} [-T] @var{source} @var{dest}
7240 install [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7241 install [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7242 install [@var{option}]@dots{} -d @var{directory}@dots{}
7247 If two file names are given, @command{install} copies the first file to the
7251 If the @option{--target-directory} (@option{-t}) option is given, or
7252 failing that if the last file is a directory and the
7253 @option{--no-target-directory} (@option{-T}) option is not given,
7254 @command{install} copies each @var{source} file to the specified
7255 directory, using the @var{source}s' names.
7258 If the @option{--directory} (@option{-d}) option is given,
7259 @command{install} creates each @var{directory} and any missing parent
7260 directories. Parent directories are created with mode
7261 @samp{u=rwx,go=rx} (755), regardless of the @option{-m} option or the
7265 @cindex Makefiles, installing programs in
7266 @command{install} is similar to @command{cp}, but allows you to control the
7267 attributes of destination files. It is typically used in Makefiles to
7268 copy programs into their destination directories. It refuses to copy
7269 files onto themselves.
7271 The program accepts the following options. Also see @ref{Common options}.
7279 Ignored; for compatibility with old Unix versions of @command{install}.
7284 @opindex --directory
7285 @cindex directories, creating with given attributes
7286 @cindex parent directories, creating missing
7287 @cindex leading directories, creating missing
7288 Create any missing parent directories, giving them the default
7289 attributes. Then create each given directory, setting their owner,
7290 group and mode as given on the command line or to the defaults.
7292 @item -g @var{group}
7293 @itemx --group=@var{group}
7296 @cindex group ownership of installed files, setting
7297 Set the group ownership of installed files or directories to
7298 @var{group}. The default is the process's current group. @var{group}
7299 may be either a group name or a numeric group ID.
7302 @itemx --mode=@var{mode}
7305 @cindex permissions of installed files, setting
7306 Set the file mode bits for the installed file or directory to @var{mode},
7307 which can be either an octal number, or a symbolic mode as in
7308 @command{chmod}, with @samp{a=} (no access allowed to anyone) as the
7309 point of departure (@pxref{File permissions}).
7310 The default mode is @samp{u=rwx,go=rx,a-s} (0755)---read, write, and
7311 execute for the owner, read and execute for group and other, and with
7312 set-user-ID and set-group-ID disabled.
7314 @item -o @var{owner}
7315 @itemx --owner=@var{owner}
7318 @cindex ownership of installed files, setting
7319 @cindex appropriate privileges
7320 @vindex root @r{as default owner}
7321 If @command{install} has appropriate privileges (is run as root), set the
7322 ownership of installed files or directories to @var{owner}. The default
7323 is @code{root}. @var{owner} may be either a user name or a numeric user
7327 @itemx --preserve-timestamps
7329 @opindex --preserve-timestamps
7330 @cindex timestamps of installed files, preserving
7331 Set the time of last access and the time of last modification of each
7332 installed file to match those of each corresponding original file.
7333 When a file is installed without this option, its last access and
7334 last modification times are both set to the time of installation.
7335 This option is useful if you want to use the last modification times
7336 of installed files to keep track of when they were last built as opposed
7337 to when they were last installed.
7343 @cindex symbol table information, stripping
7344 @cindex stripping symbol table information
7345 Strip the symbol tables from installed binary executables.
7351 @optNoTargetDirectory
7357 Print the name of each file before copying it.
7365 @section @command{mv}: Move (rename) files
7369 @command{mv} moves or renames files (or directories). Synopses:
7372 mv [@var{option}]@dots{} [-T] @var{source} @var{dest}
7373 mv [@var{option}]@dots{} @var{source}@dots{} @var{directory}
7374 mv [@var{option}]@dots{} -t @var{directory} @var{source}@dots{}
7379 If two file names are given, @command{mv} moves the first file to the
7383 If the @option{--target-directory} (@option{-t}) option is given, or
7384 failing that if the last file is a directory and the
7385 @option{--no-target-directory} (@option{-T}) option is not given,
7386 @command{mv} moves each @var{source} file to the specified
7387 directory, using the @var{source}s' names.
7390 @command{mv} can move any type of file from one file system to another.
7391 Prior to version @code{4.0} of the fileutils,
7392 @command{mv} could move only regular files between file systems.
7393 For example, now @command{mv} can move an entire directory hierarchy
7394 including special device files from one partition to another. It first
7395 uses some of the same code that's used by @code{cp -a} to copy the
7396 requested directories and files, then (assuming the copy succeeded)
7397 it removes the originals. If the copy fails, then the part that was
7398 copied to the destination partition is removed. If you were to copy
7399 three directories from one partition to another and the copy of the first
7400 directory succeeded, but the second didn't, the first would be left on
7401 the destination partition and the second and third would be left on the
7404 @cindex prompting, and @command{mv}
7405 If a destination file exists but is normally unwritable, standard input
7406 is a terminal, and the @option{-f} or @option{--force} option is not given,
7407 @command{mv} prompts the user for whether to replace the file. (You might
7408 own the file, or have write permission on its directory.) If the
7409 response is not affirmative, the file is skipped.
7411 @emph{Warning}: If you try to move a symlink that points to a directory,
7412 and you specify the symlink with a trailing slash, then @command{mv}
7413 doesn't move the symlink but instead moves the directory referenced
7414 by the symlink. @xref{Trailing slashes}.
7416 The program accepts the following options. Also see @ref{Common options}.
7426 @cindex prompts, omitting
7427 Do not prompt the user before removing a destination file.
7430 @itemx --interactive
7432 @opindex --interactive
7433 @cindex prompts, forcing
7434 Prompt whether to overwrite each existing destination file, regardless
7436 If the response is not affirmative, the file is skipped.
7438 @itemx @w{@kbd{--reply}=@var{how}}
7440 @cindex interactivity
7441 @c FIXME: remove in 2008
7442 @strong{Deprecated: to be removed in 2008.}@*
7443 Specifying @option{--reply=yes} is equivalent to using @option{--force}.
7444 Specify @option{--reply=no} to make @command{mv} act as if @samp{no} were
7445 given as a response to every prompt about a destination file.
7446 Specify @option{--reply=query} to make @command{mv} prompt the user
7447 about each existing destination file.
7448 Note that @option{--reply=no} has an effect only when @command{mv} would prompt
7449 without @option{-i} or equivalent, i.e., when a destination file exists and is
7450 not writable, standard input is a terminal, and no @option{-f} (or equivalent)
7451 option is specified.
7457 @cindex newer files, moving only
7458 Do not move a non-directory that has an existing destination with the
7459 same or newer modification time.
7460 If the move is across file system boundaries, the comparison is to the
7461 source time stamp truncated to the resolutions of the destination file
7462 system and of the system calls used to update time stamps; this avoids
7463 duplicate work if several @samp{mv -u} commands are executed with the
7464 same source and destination.
7470 Print the name of each file before moving it.
7472 @optStripTrailingSlashes
7478 @optNoTargetDirectory
7486 @section @command{rm}: Remove files or directories
7489 @cindex removing files or directories
7491 @command{rm} removes each given @var{file}. By default, it does not remove
7492 directories. Synopsis:
7495 rm [@var{option}]@dots{} [@var{file}]@dots{}
7498 @cindex prompting, and @command{rm}
7499 If the @option{-I} or @option{--interactive=once} option is given,
7500 and there are more than three files or the @option{-r}, @option{-R},
7501 or @option{--recursive} are given, then @command{rm} prompts the user
7502 for whether to proceed with the entire operation. If the response is
7503 not affirmative, the entire command is aborted.
7505 Otherwise, if a file is unwritable, standard input is a terminal, and
7506 the @option{-f} or @option{--force} option is not given, or the
7507 @option{-i} or @option{--interactive=always} option @emph{is} given,
7508 @command{rm} prompts the user for whether to remove the file.
7509 If the response is not affirmative, the file is skipped.
7511 @emph{Warning}: If you use @command{rm} to remove a file, it is usually
7512 possible to recover the contents of that file. If you want more assurance
7513 that the contents are truly unrecoverable, consider using @command{shred}.
7515 The program accepts the following options. Also see @ref{Common options}.
7523 Ignore nonexistent files and never prompt the user.
7524 Ignore any previous @option{--interactive} (@option{-i}) option.
7528 Prompt whether to remove each file.
7529 If the response is not affirmative, the file is skipped.
7530 Ignore any previous @option{--force} (@option{-f}) option.
7531 Equivalent to @option{--interactive=always}.
7535 Prompt once whether to proceed with the command, if more than three
7536 files are named or if a recursive removal is requested. Ignore any
7537 previous @option{--force} (@option{-f}) option. Equivalent to
7538 @option{--interactive=once}.
7540 @itemx --interactive [=@var{when}]
7541 @opindex --interactive
7542 Specify when to issue an interactive prompt. @var{when} may be
7546 @vindex never @r{interactive option}
7547 - Do not prompt at all.
7549 @vindex once @r{interactive option}
7550 - Prompt once if more than three files are named or if a recursive
7551 removal is requested. Equivalent to @option{-I}.
7553 @vindex always @r{interactive option}
7554 - Prompt for every file being removed. Equivalent to @option{-i}.
7556 Specifying @option{--interactive} and no @var{when} is equivalent to
7557 @option{--interactive=always}.
7559 @itemx --preserve-root
7560 @opindex --preserve-root
7561 @cindex root directory, disallow recursive destruction
7562 Fail upon any attempt to remove the file system root, @file{/},
7563 when used with the @option{--recursive} option.
7564 Without @option{--recursive}, this option has no effect.
7565 @xref{Treating / specially}.
7567 @itemx --no-preserve-root
7568 @opindex --no-preserve-root
7569 @cindex root directory, allow recursive destruction
7570 Cancel the effect of any preceding @option{--preserve-root} option.
7571 @xref{Treating / specially}.
7578 @opindex --recursive
7579 @cindex directories, removing (recursively)
7580 Remove the listed directories and their contents recursively.
7586 Print the name of each file before removing it.
7590 @cindex files beginning with @samp{-}, removing
7591 @cindex @samp{-}, removing files beginning with
7592 One common question is how to remove files whose names begin with a
7593 @samp{-}. @sc{gnu} @command{rm}, like every program that uses the @code{getopt}
7594 function to parse its arguments, lets you use the @samp{--} option to
7595 indicate that all following arguments are non-options. To remove a file
7596 called @file{-f} in the current directory, you could type either:
7609 @opindex - @r{and Unix @command{rm}}
7610 The Unix @command{rm} program's use of a single @samp{-} for this purpose
7611 predates the development of the getopt standard syntax.
7616 @node shred invocation
7617 @section @command{shred}: Remove files more securely
7620 @cindex data, erasing
7621 @cindex erasing data
7623 @command{shred} overwrites devices or files, to help prevent even
7624 very expensive hardware from recovering the data.
7626 Ordinarily when you remove a file (@pxref{rm invocation}), the data is
7627 not actually destroyed. Only the index listing where the file is
7628 stored is destroyed, and the storage is made available for reuse.
7629 There are undelete utilities that will attempt to reconstruct the index
7630 and can bring the file back if the parts were not reused.
7632 On a busy system with a nearly-full drive, space can get reused in a few
7633 seconds. But there is no way to know for sure. If you have sensitive
7634 data, you may want to be sure that recovery is not possible by actually
7635 overwriting the file with non-sensitive data.
7637 However, even after doing that, it is possible to take the disk back
7638 to a laboratory and use a lot of sensitive (and expensive) equipment
7639 to look for the faint ``echoes'' of the original data underneath the
7640 overwritten data. If the data has only been overwritten once, it's not
7643 The best way to remove something irretrievably is to destroy the media
7644 it's on with acid, melt it down, or the like. For cheap removable media
7645 like floppy disks, this is the preferred method. However, hard drives
7646 are expensive and hard to melt, so the @command{shred} utility tries
7647 to achieve a similar effect non-destructively.
7649 This uses many overwrite passes, with the data patterns chosen to
7650 maximize the damage they do to the old data. While this will work on
7651 floppies, the patterns are designed for best effect on hard drives.
7652 For more details, see the source code and Peter Gutmann's paper
7653 @uref{http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html,
7654 @cite{Secure Deletion of Data from Magnetic and Solid-State Memory}},
7655 from the proceedings of the Sixth @acronym{USENIX} Security Symposium (San Jose,
7656 California, July 22--25, 1996).
7658 @strong{Please note} that @command{shred} relies on a very important assumption:
7659 that the file system overwrites data in place. This is the traditional
7660 way to do things, but many modern file system designs do not satisfy this
7661 assumption. Exceptions include:
7666 Log-structured or journaled file systems, such as those supplied with
7667 AIX and Solaris, and JFS, ReiserFS, XFS, Ext3 (in @code{data=journal} mode),
7668 BFS, NTFS, etc.@: when they are configured to journal @emph{data}.
7671 File systems that write redundant data and carry on even if some writes
7672 fail, such as RAID-based file systems.
7675 File systems that make snapshots, such as Network Appliance's NFS server.
7678 File systems that cache in temporary locations, such as NFS version 3
7682 Compressed file systems.
7685 In the particular case of ext3 file systems, the above disclaimer applies (and
7686 @command{shred} is thus of limited effectiveness) only in @code{data=journal}
7687 mode, which journals file data in addition to just metadata. In both
7688 the @code{data=ordered} (default) and @code{data=writeback} modes,
7689 @command{shred} works as usual. Ext3 journaling modes can be changed
7690 by adding the @code{data=something} option to the mount options for a
7691 particular file system in the @file{/etc/fstab} file, as documented in
7692 the mount man page (man mount).
7694 If you are not sure how your file system operates, then you should assume
7695 that it does not overwrite data in place, which means that shred cannot
7696 reliably operate on regular files in your file system.
7698 Generally speaking, it is more reliable to shred a device than a file,
7699 since this bypasses the problem of file system design mentioned above.
7700 However, even shredding devices is not always completely reliable. For
7701 example, most disks map out bad sectors invisibly to the application; if
7702 the bad sectors contain sensitive data, @command{shred} won't be able to
7705 @command{shred} makes no attempt to detect or report this problem, just as
7706 it makes no attempt to do anything about backups. However, since it is
7707 more reliable to shred devices than files, @command{shred} by default does
7708 not truncate or remove the output file. This default is more suitable
7709 for devices, which typically cannot be truncated and should not be
7712 Finally, consider the risk of backups and mirrors.
7713 File system backups and remote mirrors may contain copies of the
7714 file that cannot be removed, and that will allow a shredded file
7715 to be recovered later. So if you keep any data you may later want
7716 to destroy using @command{shred}, be sure that it is not backed up or mirrored.
7719 shred [@var{option}]@dots{} @var{file}[@dots{}]
7722 The program accepts the following options. Also see @ref{Common options}.
7730 @cindex force deletion
7731 Override file permissions if necessary to allow overwriting.
7734 @itemx -n @var{NUMBER}
7735 @itemx --iterations=@var{NUMBER}
7736 @opindex -n @var{NUMBER}
7737 @opindex --iterations=@var{NUMBER}
7738 @cindex iterations, selecting the number of
7739 By default, @command{shred} uses 25 passes of overwrite. This is enough
7740 for all of the useful overwrite patterns to be used at least once.
7741 You can reduce this to save time, or increase it if you have a lot of
7744 @item -s @var{BYTES}
7745 @itemx --size=@var{BYTES}
7746 @opindex -s @var{BYTES}
7747 @opindex --size=@var{BYTES}
7748 @cindex size of file to shred
7749 Shred the first @var{BYTES} bytes of the file. The default is to shred
7750 the whole file. @var{BYTES} can be followed by a size specification like
7751 @samp{K}, @samp{M}, or @samp{G} to specify a multiple. @xref{Block size}.
7757 @cindex removing files after shredding
7758 After shredding a file, truncate it (if possible) and then remove it.
7759 If a file has multiple links, only the named links will be removed.
7765 Display status updates as sterilization proceeds.
7771 By default, @command{shred} rounds the size of a regular file up to the next
7772 multiple of the file system block size to fully erase the last block of the file.
7773 Use @option{--exact} to suppress that behavior.
7774 Thus, by default if you shred a 10-byte regular file on a system with 512-byte
7775 blocks, the resulting file will be 512 bytes long. With this option,
7776 shred does not increase the apparent size of the file.
7782 Normally, the last pass that @command{shred} writes is made up of
7783 random data. If this would be conspicuous on your hard drive (for
7784 example, because it looks like encrypted data), or you just think
7785 it's tidier, the @option{--zero} option adds an additional overwrite pass with
7786 all zero bits. This is in addition to the number of passes specified
7787 by the @option{--iterations} option.
7791 You might use the following command to erase all trace of the
7792 file system you'd created on the floppy disk in your first drive.
7793 That command takes about 20 minutes to erase a ``1.44MB'' (actually
7797 shred --verbose /dev/fd0
7800 Similarly, to erase all data on a selected partition of
7801 your hard disk, you could give a command like this:
7804 shred --verbose /dev/sda5
7807 A @var{file} of @samp{-} denotes standard output.
7808 The intended use of this is to shred a removed temporary file.
7812 i=`tempfile -m 0600`
7815 echo "Hello, world" >&3
7820 However, the command @samp{shred - >file} does not shred the contents
7821 of @var{file}, since the shell truncates @var{file} before invoking
7822 @command{shred}. Use the command @samp{shred file} or (if using a
7823 Bourne-compatible shell) the command @samp{shred - 1<>file} instead.
7828 @node Special file types
7829 @chapter Special file types
7831 @cindex special file types
7832 @cindex file types, special
7834 This chapter describes commands which create special types of files (and
7835 @command{rmdir}, which removes directories, one special file type).
7837 @cindex special file types
7839 Although Unix-like operating systems have markedly fewer special file
7840 types than others, not @emph{everything} can be treated only as the
7841 undifferentiated byte stream of @dfn{normal files}. For example, when a
7842 file is created or removed, the system must record this information,
7843 which it does in a @dfn{directory}---a special type of file. Although
7844 you can read directories as normal files, if you're curious, in order
7845 for the system to do its job it must impose a structure, a certain
7846 order, on the bytes of the file. Thus it is a ``special'' type of file.
7848 Besides directories, other special file types include named pipes
7849 (FIFOs), symbolic links, sockets, and so-called @dfn{special files}.
7852 * link invocation:: Make a hard link via the link syscall
7853 * ln invocation:: Make links between files.
7854 * mkdir invocation:: Make directories.
7855 * mkfifo invocation:: Make FIFOs (named pipes).
7856 * mknod invocation:: Make block or character special files.
7857 * readlink invocation:: Print the referent of a symbolic link.
7858 * rmdir invocation:: Remove empty directories.
7859 * unlink invocation:: Remove files via the unlink syscall
7863 @node link invocation
7864 @section @command{link}: Make a hard link via the link syscall
7867 @cindex links, creating
7868 @cindex hard links, creating
7869 @cindex creating links (hard only)
7871 @command{link} creates a single hard link at a time.
7872 It is a minimalist interface to the system-provided
7873 @code{link} function. @xref{Hard Links, , , libc,
7874 The GNU C Library Reference Manual}.
7875 It avoids the bells and whistles of the more commonly-used
7876 @command{ln} command (@pxref{ln invocation}).
7880 link @var{filename} @var{linkname}
7883 @var{filename} must specify an existing file, and @var{linkname}
7884 must specify a nonexistent entry in an existing directory.
7885 @command{link} simply calls @code{link (@var{filename}, @var{linkname})}
7888 On a @acronym{GNU} system, this command acts like @samp{ln --directory
7889 --no-target-directory @var{filename} @var{linkname}}. However, the
7890 @option{--directory} and @option{--no-target-directory} options are
7891 not specified by @acronym{POSIX}, and the @command{link} command is
7892 more portable in practice.
7898 @section @command{ln}: Make links between files
7901 @cindex links, creating
7902 @cindex hard links, creating
7903 @cindex symbolic (soft) links, creating
7904 @cindex creating links (hard or soft)
7906 @cindex file systems and hard links
7907 @command{ln} makes links between files. By default, it makes hard links;
7908 with the @option{-s} option, it makes symbolic (or @dfn{soft}) links.
7912 ln [@var{option}]@dots{} [-T] @var{target} @var{linkname}
7913 ln [@var{option}]@dots{} @var{target}
7914 ln [@var{option}]@dots{} @var{target}@dots{} @var{directory}
7915 ln [@var{option}]@dots{} -t @var{directory} @var{target}@dots{}
7921 If two file names are given, @command{ln} creates a link to the first
7922 file from the second.
7925 If one @var{target} is given, @command{ln} creates a link to that file
7926 in the current directory.
7929 If the @option{--target-directory} (@option{-t}) option is given, or
7930 failing that if the last file is a directory and the
7931 @option{--no-target-directory} (@option{-T}) option is not given,
7932 @command{ln} creates a link to each @var{target} file in the specified
7933 directory, using the @var{target}s' names.
7937 Normally @command{ln} does not remove existing files. Use the
7938 @option{--force} (@option{-f}) option to remove them unconditionally,
7939 the @option{--interactive} (@option{-i}) option to remove them
7940 conditionally, and the @option{--backup} (@option{-b}) option to
7943 @cindex hard link, defined
7944 @cindex inode, and hard links
7945 A @dfn{hard link} is another name for an existing file; the link and the
7946 original are indistinguishable. Technically speaking, they share the
7947 same inode, and the inode contains all the information about a
7948 file---indeed, it is not incorrect to say that the inode @emph{is} the
7949 file. On all existing implementations, you cannot make a hard link to
7950 a directory, and hard links cannot cross file system boundaries. (These
7951 restrictions are not mandated by @acronym{POSIX}, however.)
7953 @cindex dereferencing symbolic links
7954 @cindex symbolic link, defined
7955 @dfn{Symbolic links} (@dfn{symlinks} for short), on the other hand, are
7956 a special file type (which not all kernels support: System V release 3
7957 (and older) systems lack symlinks) in which the link file actually
7958 refers to a different file, by name. When most operations (opening,
7959 reading, writing, and so on) are passed the symbolic link file, the
7960 kernel automatically @dfn{dereferences} the link and operates on the
7961 target of the link. But some operations (e.g., removing) work on the
7962 link file itself, rather than on its target. @xref{Symbolic Links,,,
7963 libc, The GNU C Library Reference Manual}.
7965 The program accepts the following options. Also see @ref{Common options}.
7976 @opindex --directory
7977 @cindex hard links to directories
7978 Allow users with appropriate privileges to attempt to make hard links
7980 However, note that this will probably fail due to
7981 system restrictions, even for the super-user.
7987 Remove existing destination files.
7990 @itemx --interactive
7992 @opindex --interactive
7993 @cindex prompting, and @command{ln}
7994 Prompt whether to remove existing destination files.
7997 @itemx --no-dereference
7999 @opindex --no-dereference
8000 Do not treat the last operand specially when it is a symbolic link to
8001 a directory. Instead, treat it as if it were a normal file.
8003 When the destination is an actual directory (not a symlink to one),
8004 there is no ambiguity. The link is created in that directory.
8005 But when the specified destination is a symlink to a directory,
8006 there are two ways to treat the user's request. @command{ln} can
8007 treat the destination just as it would a normal directory and create
8008 the link in it. On the other hand, the destination can be viewed as a
8009 non-directory---as the symlink itself. In that case, @command{ln}
8010 must delete or backup that symlink before creating the new link.
8011 The default is to treat a destination that is a symlink to a directory
8012 just like a directory.
8014 This option is weaker than the @option{--no-target-directory}
8015 (@option{-T}) option, so it has no effect if both options are given.
8021 Make symbolic links instead of hard links. This option merely produces
8022 an error message on systems that do not support symbolic links.
8028 @optNoTargetDirectory
8034 Print the name of each file after linking it successfully.
8045 # Create link ../a pointing to a in that directory.
8046 # Not really useful because it points to itself.
8051 # Change to the target before creating symlinks to avoid being confused.
8057 # Hard coded file names don't move well.
8058 ln -s $(pwd)/a /some/dir/
8062 # Relative file names survive directory moves and also
8063 # work across networked file systems.
8064 ln -s afile anotherfile
8065 ln -s ../adir/afile yetanotherfile
8069 @node mkdir invocation
8070 @section @command{mkdir}: Make directories
8073 @cindex directories, creating
8074 @cindex creating directories
8076 @command{mkdir} creates directories with the specified names. Synopsis:
8079 mkdir [@var{option}]@dots{} @var{name}@dots{}
8082 @command{mkdir} creates each directory @var{name} in the order given.
8083 It reports an error if @var{name} already exists, unless the
8084 @option{-p} option is given and @var{name} is a directory.
8086 The program accepts the following options. Also see @ref{Common options}.
8091 @itemx --mode=@var{mode}
8094 @cindex modes of created directories, setting
8095 Set the file permission bits of created directories to @var{mode},
8096 which uses the same syntax as
8097 in @command{chmod} and uses @samp{a=rwx} (read, write and execute allowed for
8098 everyone) for the point of the departure. @xref{File permissions}.
8100 Normally the directory has the desired file mode bits at the moment it
8101 is created. As a @acronym{GNU} extension, @var{mode} may also mention
8102 special mode bits, but in this case there may be a temporary window
8103 during which the directory exists but its special mode bits are
8110 @cindex parent directories, creating
8111 Make any missing parent directories for each argument. The file permission
8112 bits of parent directories are set to the umask modified by @samp{u+wx}.
8113 Ignore arguments corresponding to existing directories, and do not
8114 change their file mode bits.
8120 Print a message for each created directory. This is most useful with
8127 @node mkfifo invocation
8128 @section @command{mkfifo}: Make FIFOs (named pipes)
8131 @cindex FIFOs, creating
8132 @cindex named pipes, creating
8133 @cindex creating FIFOs (named pipes)
8135 @command{mkfifo} creates FIFOs (also called @dfn{named pipes}) with the
8136 specified names. Synopsis:
8139 mkfifo [@var{option}] @var{name}@dots{}
8142 A @dfn{FIFO} is a special file type that permits independent processes
8143 to communicate. One process opens the FIFO file for writing, and
8144 another for reading, after which data can flow as with the usual
8145 anonymous pipe in shells or elsewhere.
8147 The program accepts the following option. Also see @ref{Common options}.
8152 @itemx --mode=@var{mode}
8155 @cindex modes of created FIFOs, setting
8156 Set the mode of created FIFOs to @var{mode}, which is symbolic as in
8157 @command{chmod} and uses @samp{a=rw} (read and write allowed for everyone)
8158 for the point of departure. @var{mode} should specify only file
8159 permission bits. @xref{File permissions}.
8166 @node mknod invocation
8167 @section @command{mknod}: Make block or character special files
8170 @cindex block special files, creating
8171 @cindex character special files, creating
8173 @command{mknod} creates a FIFO, character special file, or block special
8174 file with the specified name. Synopsis:
8177 mknod [@var{option}]@dots{} @var{name} @var{type} [@var{major} @var{minor}]
8180 @cindex special files
8181 @cindex block special files
8182 @cindex character special files
8183 Unlike the phrase ``special file type'' above, the term @dfn{special
8184 file} has a technical meaning on Unix: something that can generate or
8185 receive data. Usually this corresponds to a physical piece of hardware,
8186 e.g., a printer or a disk. (These files are typically created at
8187 system-configuration time.) The @command{mknod} command is what creates
8188 files of this type. Such devices can be read either a character at a
8189 time or a ``block'' (many characters) at a time, hence we say there are
8190 @dfn{block special} files and @dfn{character special} files.
8192 The arguments after @var{name} specify the type of file to make:
8197 @opindex p @r{for FIFO file}
8201 @opindex b @r{for block special file}
8202 for a block special file
8205 @c Don't document the `u' option -- it's just a synonym for `c'.
8206 @c Do *any* versions of mknod still use it?
8208 @opindex c @r{for character special file}
8209 @c @opindex u @r{for character special file}
8210 for a character special file
8214 When making a block or character special file, the major and minor
8215 device numbers must be given after the file type.
8216 If a major or minor device number begins with @samp{0x} or @samp{0X},
8217 it is interpreted as hexadecimal; otherwise, if it begins with @samp{0},
8218 as octal; otherwise, as decimal.
8220 The program accepts the following option. Also see @ref{Common options}.
8225 @itemx --mode=@var{mode}
8228 Set the mode of created files to @var{mode}, which is symbolic as in
8229 @command{chmod} and uses @samp{a=rw} as the point of departure.
8230 @var{mode} should specify only file permission bits.
8231 @xref{File permissions}.
8238 @node readlink invocation
8239 @section @command{readlink}: Print the referent of a symbolic link
8242 @cindex displaying value of a symbolic link
8244 @command{readlink} may work in one of two supported modes:
8250 @command{readlink} outputs the value of the given symbolic link.
8251 If @command{readlink} is invoked with an argument other than the name
8252 of a symbolic link, it produces no output and exits with a nonzero exit code.
8254 @item Canonicalize mode
8256 @command{readlink} outputs the absolute name of the given file which contains
8257 no @file{.}, @file{..} components nor any repeated separators
8258 (@file{/}) or symbolic links.
8263 readlink [@var{option}] @var{file}
8266 By default, @command{readlink} operates in readlink mode.
8268 The program accepts the following options. Also see @ref{Common options}.
8273 @itemx --canonicalize
8275 @opindex --canonicalize
8276 Activate canonicalize mode.
8277 If any component of the file name except the last one is missing or unavailable,
8278 @command{readlink} produces no output and exits with a nonzero exit code.
8281 @itemx --canonicalize-existing
8283 @opindex --canonicalize-existing
8284 Activate canonicalize mode.
8285 If any component is missing or unavailable, @command{readlink} produces
8286 no output and exits with a nonzero exit code.
8289 @itemx --canonicalize-missing
8291 @opindex --canonicalize-missing
8292 Activate canonicalize mode.
8293 If any component is missing or unavailable, @command{readlink} treats it
8299 @opindex --no-newline
8300 Do not output the trailing newline.
8310 Suppress most error messages.
8316 Report error messages.
8320 The @command{readlink} utility first appeared in OpenBSD 2.1.
8325 @node rmdir invocation
8326 @section @command{rmdir}: Remove empty directories
8329 @cindex removing empty directories
8330 @cindex directories, removing empty
8332 @command{rmdir} removes empty directories. Synopsis:
8335 rmdir [@var{option}]@dots{} @var{directory}@dots{}
8338 If any @var{directory} argument does not refer to an existing empty
8339 directory, it is an error.
8341 The program accepts the following option. Also see @ref{Common options}.
8345 @item --ignore-fail-on-non-empty
8346 @opindex --ignore-fail-on-non-empty
8347 @cindex directory deletion, ignoring failures
8348 Ignore each failure to remove a directory that is solely because
8349 the directory is non-empty.
8355 @cindex parent directories, removing
8356 Remove @var{directory}, then try to remove each component of @var{directory}.
8357 So, for example, @samp{rmdir -p a/b/c} is similar to @samp{rmdir a/b/c a/b a}.
8358 As such, it fails if any of those directories turns out not to be empty.
8359 Use the @option{--ignore-fail-on-non-empty} option to make it so such
8360 a failure does not evoke a diagnostic and does not cause @command{rmdir} to
8361 exit unsuccessfully.
8367 @cindex directory deletion, reporting
8368 Give a diagnostic for each successful removal.
8369 @var{directory} is removed.
8373 @xref{rm invocation}, for how to remove non-empty directories (recursively).
8378 @node unlink invocation
8379 @section @command{unlink}: Remove files via the unlink syscall
8382 @cindex removing files or directories (via the unlink syscall)
8384 @command{unlink} deletes a single specified file name.
8385 It is a minimalist interface to the system-provided
8386 @code{unlink} function. @xref{Deleting Files, , , libc,
8387 The GNU C Library Reference Manual}. Synopsis:
8388 It avoids the bells and whistles of the more commonly-used
8389 @command{rm} command (@pxref{rm invocation}).
8392 unlink @var{filename}
8395 On some systems @code{unlink} can be used to delete the name of a
8396 directory. On others, it can be used that way only by a privileged user.
8397 In the GNU system @code{unlink} can never delete the name of a directory.
8399 The @command{unlink} command honors the @option{--help} and
8400 @option{--version} options. To remove a file whose name begins with
8401 @samp{-}, prefix the name with @samp{./}, e.g., @samp{unlink ./--help}.
8406 @node Changing file attributes
8407 @chapter Changing file attributes
8409 @cindex changing file attributes
8410 @cindex file attributes, changing
8411 @cindex attributes, file
8413 A file is not merely its contents, a name, and a file type
8414 (@pxref{Special file types}). A file also has an owner (a user ID), a
8415 group (a group ID), permissions (what the owner can do with the file,
8416 what people in the group can do, and what everyone else can do), various
8417 timestamps, and other information. Collectively, we call these a file's
8420 These commands change file attributes.
8423 * chgrp invocation:: Change file groups.
8424 * chmod invocation:: Change access permissions.
8425 * chown invocation:: Change file owners and groups.
8426 * touch invocation:: Change file timestamps.
8430 @node chown invocation
8431 @section @command{chown}: Change file owner and group
8434 @cindex file ownership, changing
8435 @cindex group ownership, changing
8436 @cindex changing file ownership
8437 @cindex changing group ownership
8439 @command{chown} changes the user and/or group ownership of each given @var{file}
8440 to @var{new-owner} or to the user and group of an existing reference file.
8444 chown [@var{option}]@dots{} @{@var{new-owner} | --reference=@var{ref_file}@} @var{file}@dots{}
8447 If used, @var{new-owner} specifies the new owner and/or group as follows
8448 (with no embedded white space):
8451 [@var{owner}] [ : [@var{group}] ]
8458 If only an @var{owner} (a user name or numeric user ID) is given, that
8459 user is made the owner of each given file, and the files' group is not
8462 @item owner@samp{:}group
8463 If the @var{owner} is followed by a colon and a @var{group} (a
8464 group name or numeric group ID), with no spaces between them, the group
8465 ownership of the files is changed as well (to @var{group}).
8468 If a colon but no group name follows @var{owner}, that user is
8469 made the owner of the files and the group of the files is changed to
8470 @var{owner}'s login group.
8473 If the colon and following @var{group} are given, but the owner
8474 is omitted, only the group of the files is changed; in this case,
8475 @command{chown} performs the same function as @command{chgrp}.
8478 If only a colon is given, or if @var{new-owner} is empty, neither the
8479 owner nor the group is changed.
8483 Some older scripts may still use @samp{.} in place of the @samp{:} separator.
8484 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) does not
8485 require support for that, but for backward compatibility @acronym{GNU}
8486 @command{chown} supports @samp{.} so long as no ambiguity results.
8487 New scripts should avoid the use of @samp{.} because it is not
8488 portable, and because it has undesirable results if the entire
8489 @var{owner@samp{.}group} happens to identify a user whose name
8492 The @command{chown} command sometimes clears the set-user-ID or
8493 set-group-ID permission bits. This behavior depends on the policy and
8494 functionality of the underlying @code{chown} system call, which may
8495 make system-dependent file mode modifications outside the control of
8496 the @command{chown} command. For example, the @command{chown} command
8497 might not affect those bits when invoked by a user with appropriate
8498 privileges, or when the
8499 bits signify some function other than executable permission (e.g.,
8501 When in doubt, check the underlying system behavior.
8503 The program accepts the following options. Also see @ref{Common options}.
8511 @cindex changed owners, verbosely describing
8512 Verbosely describe the action for each @var{file} whose ownership
8521 @cindex error messages, omitting
8522 Do not print error messages about files whose ownership cannot be
8525 @itemx @w{@kbd{--from}=@var{old-owner}}
8527 @cindex symbolic links, changing owner
8528 Change a @var{file}'s ownership only if it has current attributes specified
8529 by @var{old-owner}. @var{old-owner} has the same form as @var{new-owner}
8531 This option is useful primarily from a security standpoint in that
8532 it narrows considerably the window of potential abuse.
8533 For example, to reflect a user ID numbering change for one user's files
8534 without an option like this, @code{root} might run
8537 find / -owner OLDUSER -print0 | xargs -0 chown -h NEWUSER
8540 But that is dangerous because the interval between when the @command{find}
8541 tests the existing file's owner and when the @command{chown} is actually run
8543 One way to narrow the gap would be to invoke chown for each file
8547 find / -owner OLDUSER -exec chown -h NEWUSER @{@} \;
8550 But that is very slow if there are many affected files.
8551 With this option, it is safer (the gap is narrower still)
8552 though still not perfect:
8555 chown -h -R --from=OLDUSER NEWUSER /
8559 @opindex --dereference
8560 @cindex symbolic links, changing owner
8562 Do not act on symbolic links themselves but rather on what they point to.
8563 This is the default.
8566 @itemx --no-dereference
8568 @opindex --no-dereference
8569 @cindex symbolic links, changing owner
8571 Act on symbolic links themselves instead of what they point to.
8572 This mode relies on the @code{lchown} system call.
8573 On systems that do not provide the @code{lchown} system call,
8574 @command{chown} fails when a file specified on the command line
8576 By default, no diagnostic is issued for symbolic links encountered
8577 during a recursive traversal, but see @option{--verbose}.
8579 @itemx --preserve-root
8580 @opindex --preserve-root
8581 @cindex root directory, disallow recursive modification
8582 Fail upon any attempt to recursively change the file system root, @file{/}.
8583 Without @option{--recursive}, this option has no effect.
8584 @xref{Treating / specially}.
8586 @itemx --no-preserve-root
8587 @opindex --no-preserve-root
8588 @cindex root directory, allow recursive modification
8589 Cancel the effect of any preceding @option{--preserve-root} option.
8590 @xref{Treating / specially}.
8592 @item --reference=@var{ref_file}
8593 @opindex --reference
8594 Change the user and group of each @var{file} to be the same as those of
8595 @var{ref_file}. If @var{ref_file} is a symbolic link, do not use the
8596 user and group of the symbolic link, but rather those of the file it
8603 Output a diagnostic for every file processed.
8604 If a symbolic link is encountered during a recursive traversal
8605 on a system without the @code{lchown} system call, and @option{--no-dereference}
8606 is in effect, then issue a diagnostic saying neither the symbolic link nor
8607 its referent is being changed.
8612 @opindex --recursive
8613 @cindex recursively changing file ownership
8614 Recursively change ownership of directories and their contents.
8617 @xref{Traversing symlinks}.
8620 @xref{Traversing symlinks}.
8623 @xref{Traversing symlinks}.
8632 # Change the owner of /u to "root".
8635 # Likewise, but also change its group to "staff".
8638 # Change the owner of /u and subfiles to "root".
8643 @node chgrp invocation
8644 @section @command{chgrp}: Change group ownership
8647 @cindex group ownership, changing
8648 @cindex changing group ownership
8650 @command{chgrp} changes the group ownership of each given @var{file}
8651 to @var{group} (which can be either a group name or a numeric group ID)
8652 or to the group of an existing reference file. Synopsis:
8655 chgrp [@var{option}]@dots{} @{@var{group} | --reference=@var{ref_file}@} @var{file}@dots{}
8658 The program accepts the following options. Also see @ref{Common options}.
8666 @cindex changed files, verbosely describing
8667 Verbosely describe the action for each @var{file} whose group actually
8676 @cindex error messages, omitting
8677 Do not print error messages about files whose group cannot be
8681 @opindex --dereference
8682 @cindex symbolic links, changing owner
8684 Do not act on symbolic links themselves but rather on what they point to.
8685 This is the default.
8688 @itemx --no-dereference
8690 @opindex --no-dereference
8691 @cindex symbolic links, changing group
8693 Act on symbolic links themselves instead of what they point to.
8694 This mode relies on the @code{lchown} system call.
8695 On systems that do not provide the @code{lchown} system call,
8696 @command{chgrp} fails when a file specified on the command line
8698 By default, no diagnostic is issued for symbolic links encountered
8699 during a recursive traversal, but see @option{--verbose}.
8701 @itemx --preserve-root
8702 @opindex --preserve-root
8703 @cindex root directory, disallow recursive modification
8704 Fail upon any attempt to recursively change the file system root, @file{/}.
8705 Without @option{--recursive}, this option has no effect.
8706 @xref{Treating / specially}.
8708 @itemx --no-preserve-root
8709 @opindex --no-preserve-root
8710 @cindex root directory, allow recursive modification
8711 Cancel the effect of any preceding @option{--preserve-root} option.
8712 @xref{Treating / specially}.
8714 @item --reference=@var{ref_file}
8715 @opindex --reference
8716 Change the group of each @var{file} to be the same as that of
8717 @var{ref_file}. If @var{ref_file} is a symbolic link, do not use the
8718 group of the symbolic link, but rather that of the file it refers to.
8724 Output a diagnostic for every file processed.
8725 If a symbolic link is encountered during a recursive traversal
8726 on a system without the @code{lchown} system call, and @option{--no-dereference}
8727 is in effect, then issue a diagnostic saying neither the symbolic link nor
8728 its referent is being changed.
8733 @opindex --recursive
8734 @cindex recursively changing group ownership
8735 Recursively change the group ownership of directories and their contents.
8738 @xref{Traversing symlinks}.
8741 @xref{Traversing symlinks}.
8744 @xref{Traversing symlinks}.
8753 # Change the group of /u to "staff".
8756 # Change the group of /u and subfiles to "staff".
8761 @node chmod invocation
8762 @section @command{chmod}: Change access permissions
8765 @cindex changing access permissions
8766 @cindex access permissions, changing
8767 @cindex permissions, changing access
8769 @command{chmod} changes the access permissions of the named files. Synopsis:
8772 chmod [@var{option}]@dots{} @{@var{mode} | --reference=@var{ref_file}@} @var{file}@dots{}
8775 @cindex symbolic links, permissions of
8776 @command{chmod} never changes the permissions of symbolic links, since
8777 the @command{chmod} system call cannot change their permissions.
8778 This is not a problem since the permissions of symbolic links are
8779 never used. However, for each symbolic link listed on the command
8780 line, @command{chmod} changes the permissions of the pointed-to file.
8781 In contrast, @command{chmod} ignores symbolic links encountered during
8782 recursive directory traversals.
8784 A successful use of @command{chmod} clears the set-group-ID bit of a
8785 regular file if the file's group ID does not match the user's
8786 effective group ID or one of the user's supplementary group IDs,
8787 unless the user has appropriate privileges. Additional restrictions
8788 may cause the set-user-ID and set-group-ID bits of @var{mode} or
8789 @var{ref_file} to be ignored. This behavior depends on the policy and
8790 functionality of the underlying @code{chmod} system call. When in
8791 doubt, check the underlying system behavior.
8793 If used, @var{mode} specifies the new file mode bits.
8794 For details, see the section on @ref{File permissions}.
8795 If you really want @var{mode} to have a leading @samp{-}, you should
8796 use @option{--} first, e.g., @samp{chmod -- -w file}. Typically,
8797 though, @samp{chmod a-w file} is preferable, and @command{chmod -w
8798 file} (without the @option{--}) complains if it behaves differently
8799 from what @samp{chmod a-w file} would do.
8801 The program accepts the following options. Also see @ref{Common options}.
8809 Verbosely describe the action for each @var{file} whose permissions
8818 @cindex error messages, omitting
8819 Do not print error messages about files whose permissions cannot be
8822 @itemx --preserve-root
8823 @opindex --preserve-root
8824 @cindex root directory, disallow recursive modification
8825 Fail upon any attempt to recursively change the file system root, @file{/}.
8826 Without @option{--recursive}, this option has no effect.
8827 @xref{Treating / specially}.
8829 @itemx --no-preserve-root
8830 @opindex --no-preserve-root
8831 @cindex root directory, allow recursive modification
8832 Cancel the effect of any preceding @option{--preserve-root} option.
8833 @xref{Treating / specially}.
8839 Verbosely describe the action or non-action taken for every @var{file}.
8841 @item --reference=@var{ref_file}
8842 @opindex --reference
8843 Change the mode of each @var{file} to be the same as that of @var{ref_file}.
8844 @xref{File permissions}.
8845 If @var{ref_file} is a symbolic link, do not use the mode
8846 of the symbolic link, but rather that of the file it refers to.
8851 @opindex --recursive
8852 @cindex recursively changing access permissions
8853 Recursively change permissions of directories and their contents.
8860 @node touch invocation
8861 @section @command{touch}: Change file timestamps
8864 @cindex changing file timestamps
8865 @cindex file timestamps, changing
8866 @cindex timestamps, changing file
8868 @command{touch} changes the access and/or modification times of the
8869 specified files. Synopsis:
8872 touch [@var{option}]@dots{} @var{file}@dots{}
8875 @cindex empty files, creating
8876 Any @var{file} that does not exist is created empty.
8878 A @var{file} of @samp{-} causes @command{touch} to change the
8879 times of the file associated with standard output.
8881 @cindex permissions, for changing file timestamps
8882 If changing both the access and modification times to the current
8883 time, @command{touch} can change the timestamps for files that the user
8884 running it does not own but has write permission for. Otherwise, the
8885 user must own the files.
8887 Although @command{touch} provides options for changing two of the times---the
8888 times of last access and modification---of a file, there is actually
8889 a third one as well: the inode change time. This is often referred to
8890 as a file's @code{ctime}.
8891 The inode change time represents the time when the file's meta-information
8892 last changed. One common example of this is when the permissions of a
8893 file change. Changing the permissions doesn't access the file, so
8894 the atime doesn't change, nor does it modify the file, so the mtime
8895 doesn't change. Yet, something about the file itself has changed,
8896 and this must be noted somewhere. This is the job of the ctime field.
8897 This is necessary, so that, for example, a backup program can make a
8898 fresh copy of the file, including the new permissions value.
8899 Another operation that modifies a file's ctime without affecting
8900 the others is renaming. In any case, it is not possible, in normal
8901 operations, for a user to change the ctime field to a user-specified value.
8904 Time stamps assume the time zone rules specified by the @env{TZ}
8905 environment variable, or by the system default rules if @env{TZ} is
8906 not set. @xref{TZ Variable,, Specifying the Time Zone with @env{TZ},
8907 libc, The GNU C Library}. You can avoid avoid ambiguities during
8908 daylight saving transitions by using @sc{utc} time stamps.
8910 The program accepts the following options. Also see @ref{Common options}.
8916 @itemx --time=access
8920 @opindex atime@r{, changing}
8921 @opindex access @r{time, changing}
8922 @opindex use @r{time, changing}
8923 Change the access time only.
8928 @opindex --no-create
8929 Do not create files that do not exist.
8932 @itemx --date=@var{time}
8936 Use @var{time} instead of the current time. It can contain month names,
8937 time zones, @samp{am} and @samp{pm}, @samp{yesterday}, etc. For
8938 example, @option{--date="2004-02-27 14:19:13.489392193 +0530"}
8939 specifies the instant of time that is 489,392,193 nanoseconds after
8940 February 27, 2004 at 2:19:13 PM in a time zone that is 5 hours and 30
8941 minutes east of @acronym{UTC}. @xref{Date input formats}.
8942 File systems that do not support high-resolution time stamps
8943 silently ignore any excess precision here.
8947 @cindex BSD @command{touch} compatibility
8948 Ignored; for compatibility with BSD versions of @command{touch}.
8952 @itemx --time=modify
8955 @opindex mtime@r{, changing}
8956 @opindex modify @r{time, changing}
8957 Change the modification time only.
8960 @itemx --reference=@var{file}
8962 @opindex --reference
8963 Use the times of the reference @var{file} instead of the current time.
8964 If this option is combined with the @option{--date=@var{time}}
8965 (@option{-d @var{time}}) option, the reference @var{file}'s time is
8966 the origin for any relative @var{time}s given, but is otherwise ignored.
8967 For example, @samp{-r foo -d '-5 seconds'} specifies a time stamp
8968 equal to five seconds before the corresponding time stamp for @file{foo}.
8970 @item -t [[@var{CC}]@var{YY}]@var{MMDDhhmm}[.@var{ss}]
8971 Use the argument (optional four-digit or two-digit years, months,
8972 days, hours, minutes, optional seconds) instead of the current time.
8973 If the year is specified with only two digits, then @var{CC}
8974 is 20 for years in the range 0 @dots{} 68, and 19 for years in
8975 69 @dots{} 99. If no digits of the year are specified,
8976 the argument is interpreted as a date in the current year.
8980 @vindex _POSIX2_VERSION
8981 On older systems, @command{touch} supports an obsolete syntax, as follows.
8982 If no timestamp is given with any of the @option{-d}, @option{-r}, or
8983 @option{-t} options, and if there are two or more @var{file}s and the
8984 first @var{file} is of the form @samp{@var{MMDDhhmm}[@var{YY}]} and this
8985 would be a valid argument to the @option{-t} option (if the @var{YY}, if
8986 any, were moved to the front), and if the represented year
8987 is in the range 1969--1999, that argument is interpreted as the time
8988 for the other files instead of as a file name.
8989 This obsolete behavior can be enabled or disabled with the
8990 @env{_POSIX2_VERSION} environment variable (@pxref{Standards
8991 conformance}), but portable scripts should avoid commands whose
8992 behavior depends on this variable.
8993 For example, use @samp{touch ./12312359 main.c} or @samp{touch -t
8994 12312359 main.c} rather than the ambiguous @samp{touch 12312359 main.c}.
9004 No disk can hold an infinite amount of data. These commands report on
9005 how much disk storage is in use or available. (This has nothing much to
9006 do with how much @emph{main memory}, i.e., RAM, a program is using when
9007 it runs; for that, you want @command{ps} or @command{pstat} or @command{swap}
9008 or some such command.)
9011 * df invocation:: Report file system disk space usage.
9012 * du invocation:: Estimate file space usage.
9013 * stat invocation:: Report file or file system status.
9014 * sync invocation:: Synchronize memory and disk.
9019 @section @command{df}: Report file system disk space usage
9022 @cindex file system disk usage
9023 @cindex disk usage by file system
9025 @command{df} reports the amount of disk space used and available on
9026 file systems. Synopsis:
9029 df [@var{option}]@dots{} [@var{file}]@dots{}
9032 With no arguments, @command{df} reports the space used and available on all
9033 currently mounted file systems (of all types). Otherwise, @command{df}
9034 reports on the file system containing each argument @var{file}.
9036 Normally the disk space is printed in units of
9037 1024 bytes, but this can be overridden (@pxref{Block size}).
9038 Non-integer quantities are rounded up to the next higher unit.
9040 @cindex disk device file
9041 @cindex device file, disk
9042 If an argument @var{file} is a disk device file containing a mounted
9043 file system, @command{df} shows the space available on that file system
9044 rather than on the file system containing the device node (i.e., the root
9045 file system). @sc{gnu} @command{df} does not attempt to determine the disk usage
9046 on unmounted file systems, because on most kinds of systems doing so
9047 requires extremely nonportable intimate knowledge of file system
9050 The program accepts the following options. Also see @ref{Common options}.
9058 @cindex automounter file systems
9059 @cindex ignore file systems
9060 Include in the listing dummy file systems, which
9061 are omitted by default. Such file systems are typically special-purpose
9062 pseudo-file-systems, such as automounter entries.
9065 @itemx --block-size=@var{size}
9067 @opindex --block-size
9068 @cindex file system sizes
9069 Scale sizes by @var{size} before printing them (@pxref{Block size}).
9070 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
9076 Equivalent to @option{--si}.
9083 List inode usage information instead of block usage. An inode (short
9084 for index node) contains information about a file such as its owner,
9085 permissions, timestamps, and location on the disk.
9089 @cindex kibibytes for file system sizes
9090 Print sizes in 1024-byte blocks, overriding the default block size
9091 (@pxref{Block size}).
9092 This option is equivalent to @option{--block-size=1K}.
9098 @cindex file system types, limiting output to certain
9099 Limit the listing to local file systems. By default, remote file systems
9104 @cindex file system space, retrieving old data more quickly
9105 Do not invoke the @code{sync} system call before getting any usage data.
9106 This may make @command{df} run significantly faster on systems with many
9107 disks, but on some systems (notably SunOS) the results may be slightly
9108 out of date. This is the default.
9111 @itemx --portability
9113 @opindex --portability
9114 @cindex one-line output format
9115 @cindex @acronym{POSIX} output format
9116 @cindex portable output format
9117 @cindex output format, portable
9118 Use the @acronym{POSIX} output format. This is like the default format except
9123 The information about each file system is always printed on exactly
9124 one line; a mount device is never put on a line by itself. This means
9125 that if the mount device name is more than 20 characters long (e.g., for
9126 some network mounts), the columns are misaligned.
9129 The labels in the header output line are changed to conform to @acronym{POSIX}.
9136 @cindex file system space, retrieving current data more slowly
9137 Invoke the @code{sync} system call before getting any usage data. On
9138 some systems (notably SunOS), doing this yields more up to date results,
9139 but in general this option makes @command{df} much slower, especially when
9140 there are many or very busy file systems.
9142 @item -t @var{fstype}
9143 @itemx --type=@var{fstype}
9146 @cindex file system types, limiting output to certain
9147 Limit the listing to file systems of type @var{fstype}. Multiple
9148 file system types can be specified by giving multiple @option{-t} options.
9149 By default, nothing is omitted.
9154 @opindex --print-type
9155 @cindex file system types, printing
9156 Print each file system's type. The types printed here are the same ones
9157 you can include or exclude with @option{-t} and @option{-x}. The particular
9158 types printed are whatever is supported by the system. Here are some of
9159 the common names (this list is certainly not exhaustive):
9164 @cindex @acronym{NFS} file system type
9165 An @acronym{NFS} file system, i.e., one mounted over a network from another
9166 machine. This is the one type name which seems to be used uniformly by
9169 @item 4.2@r{, }ufs@r{, }efs@dots{}
9170 @cindex Linux file system types
9171 @cindex local file system types
9172 @opindex 4.2 @r{file system type}
9173 @opindex ufs @r{file system type}
9174 @opindex efs @r{file system type}
9175 A file system on a locally-mounted hard disk. (The system might even
9176 support more than one type here; Linux does.)
9178 @item hsfs@r{, }cdfs
9179 @cindex CD-ROM file system type
9180 @cindex High Sierra file system
9181 @opindex hsfs @r{file system type}
9182 @opindex cdfs @r{file system type}
9183 A file system on a CD-ROM drive. HP-UX uses @samp{cdfs}, most other
9184 systems use @samp{hsfs} (@samp{hs} for ``High Sierra'').
9187 @cindex PC file system
9188 @cindex DOS file system
9189 @cindex MS-DOS file system
9190 @cindex diskette file system
9192 An MS-DOS file system, usually on a diskette.
9196 @item -x @var{fstype}
9197 @itemx --exclude-type=@var{fstype}
9199 @opindex --exclude-type
9200 Limit the listing to file systems not of type @var{fstype}.
9201 Multiple file system types can be eliminated by giving multiple
9202 @option{-x} options. By default, no file system types are omitted.
9205 Ignored; for compatibility with System V versions of @command{df}.
9213 @section @command{du}: Estimate file space usage
9216 @cindex file space usage
9217 @cindex disk usage for files
9219 @command{du} reports the amount of disk space used by the specified files
9220 and for each subdirectory (of directory arguments). Synopsis:
9223 du [@var{option}]@dots{} [@var{file}]@dots{}
9226 With no arguments, @command{du} reports the disk space for the current
9227 directory. Normally the disk space is printed in units of
9228 1024 bytes, but this can be overridden (@pxref{Block size}).
9229 Non-integer quantities are rounded up to the next higher unit.
9231 The program accepts the following options. Also see @ref{Common options}.
9239 Show counts for all files, not just directories.
9241 @itemx --apparent-size
9242 @opindex --apparent-size
9243 Print apparent sizes, rather than disk usage. The apparent size of a
9244 file is the number of bytes reported by @code{wc -c} on regular files,
9245 or more generally, @code{ls -l --block-size=1} or @code{stat --format=%s}.
9246 For example, a file containing the word @samp{zoo} with no newline would,
9247 of course, have an apparent size of 3. Such a small file may require
9248 anywhere from 0 to 16 KiB or more of disk space, depending on
9249 the type and configuration of the file system on which the file resides.
9250 However, a sparse file created with this command:
9253 dd bs=1 seek=2GiB if=/dev/null of=big
9257 has an apparent size of 2 GiB, yet on most modern
9258 systems, it actually uses almost no disk space.
9264 Equivalent to @code{--apparent-size --block-size=1}.
9267 @itemx --block-size=@var{size}
9269 @opindex --block-size
9271 Scale sizes by @var{size} before printing them (@pxref{Block size}).
9272 For example, @option{-BG} prints sizes in units of 1,073,741,824 bytes.
9278 @cindex grand total of disk space
9279 Print a grand total of all arguments after all arguments have
9280 been processed. This can be used to find out the total disk usage of
9281 a given set of files or directories.
9284 @itemx --dereference-args
9286 @opindex --dereference-args
9287 Dereference symbolic links that are command line arguments.
9288 Does not affect other symbolic links. This is helpful for finding
9289 out the disk usage of directories, such as @file{/usr/tmp}, which
9290 are often symbolic links.
9292 @itemx --files0-from=@var{FILE}
9293 @opindex --files0-from=@var{FILE}
9294 @cindex including files from @command{du}
9295 Rather than processing files named on the command line, process those
9296 named in file @var{FILE}; each name is terminated by a null byte.
9297 This is useful with the @option{--total} (@option{-c}) option when
9298 the list of file names is so long that it may exceed a command line
9300 In such cases, running @command{du} via @command{xargs} is undesirable
9301 because it splits the list into pieces and makes @command{du} print a
9302 total for each sublist rather than for the entire list.
9303 One way to produce a list of null-byte-terminated file names is with @sc{gnu}
9304 @command{find}, using its @option{-print0} predicate.
9305 Do not specify any @var{FILE} on the command line when using this option.
9311 Currently, @option{-H} is the same as @option{--si},
9312 except that @option{-H} evokes a warning.
9313 This option will be changed to be equivalent to
9314 @option{--dereference-args} (@option{-D}).
9318 @cindex kibibytes for file sizes
9319 Print sizes in 1024-byte blocks, overriding the default block size
9320 (@pxref{Block size}).
9321 This option is equivalent to @option{--block-size=1K}.
9324 @itemx --count-links
9326 @opindex --count-links
9327 @cindex hard links, counting in @command{du}
9328 Count the size of all files, even if they have appeared already (as a
9332 @itemx --dereference
9334 @opindex --dereference
9335 @cindex symbolic links, dereferencing in @command{du}
9336 Dereference symbolic links (show the disk space used by the file
9337 or directory that the link points to instead of the space used by
9342 @cindex mebibytes for file sizes
9343 Print sizes in 1,048,576-byte blocks, overriding the default block size
9344 (@pxref{Block size}).
9345 This option is equivalent to @option{--block-size=1M}.
9348 @itemx --no-dereference
9350 @opindex --no-dereference
9351 @cindex symbolic links, dereferencing in @command{du}
9352 For each symbolic links encountered by @command{du},
9353 consider the disk space used by the symbolic link.
9355 @item --max-depth=@var{DEPTH}
9356 @opindex --max-depth=@var{DEPTH}
9357 @cindex limiting output of @command{du}
9358 Show the total for each directory (and file if --all) that is at
9359 most MAX_DEPTH levels down from the root of the hierarchy. The root
9360 is at level 0, so @code{du --max-depth=0} is equivalent to @code{du -s}.
9366 @cindex output null-byte-terminated lines
9367 Output a null byte at the end of each line, rather than a newline.
9368 This option enables other programs to parse the output of @command{du}
9369 even when that output would contain file names with embedded newlines.
9374 Append an SI-style abbreviation to each size, such as @samp{MB} for
9375 megabytes. Powers of 1000 are used, not 1024; @samp{MB} stands for
9376 1,000,000 bytes. Use the @option{-h} or @option{--human-readable} option if
9377 you prefer powers of 1024.
9382 @opindex --summarize
9383 Display only a total for each argument.
9386 @itemx --separate-dirs
9388 @opindex --separate-dirs
9389 Report the size of each directory separately, not including the sizes
9394 @cindex last modified dates, displaying in @command{du}
9395 Show time of the most recent modification of any file in the directory,
9396 or any of its subdirectories.
9399 @itemx --time=status
9402 @opindex ctime@r{, show the most recent}
9403 @opindex status time@r{, show the most recent}
9404 @opindex use time@r{, show the most recent}
9405 Show the most recent status change time (the @samp{ctime} in the inode) of
9406 any file in the directory, instead of the modification time.
9409 @itemx --time=access
9411 @opindex atime@r{, show the most recent}
9412 @opindex access time@r{, show the most recent}
9413 Show the most recent access time (the @samp{atime} in the inode) of
9414 any file in the directory, instead of the modification time.
9416 @item --time-style=@var{style}
9417 @opindex --time-style
9419 List timestamps in style @var{style}. This option has an effect only if
9420 the @option{--time} option is also specified. The @var{style} should
9421 be one of the following:
9426 List timestamps using @var{format}, where @var{format} is interpreted
9427 like the format argument of @command{date} (@pxref{date invocation}).
9428 For example, @option{--time-style="+%Y-%m-%d %H:%M:%S"} causes
9429 @command{du} to list timestamps like @samp{2002-03-30 23:45:56}. As
9430 with @command{date}, @var{format}'s interpretation is affected by the
9431 @env{LC_TIME} locale category.
9434 List timestamps in full using @acronym{ISO} 8601 date, time, and time zone
9435 format with nanosecond precision, e.g., @samp{2002-03-30
9436 23:45:56.477817180 -0700}. This style is equivalent to
9437 @samp{+%Y-%m-%d %H:%M:%S.%N %z}.
9440 List @acronym{ISO} 8601 date and time in minutes, e.g.,
9441 @samp{2002-03-30 23:45}. These timestamps are shorter than
9442 @samp{full-iso} timestamps, and are usually good enough for everyday
9443 work. This style is equivalent to @samp{+%Y-%m-%d %H:%M}.
9446 List @acronym{ISO} 8601 dates for timestamps, e.g., @samp{2002-03-30}.
9447 This style is equivalent to @samp{+%Y-%m-%d}.
9451 You can specify the default value of the @option{--time-style} option
9452 with the environment variable @env{TIME_STYLE}; if @env{TIME_STYLE} is not set
9453 the default style is @samp{long-iso}. For compatibility with @command{ls},
9454 if @env{TIME_STYLE} begins with @samp{+} and contains a newline,
9455 the newline and any later characters are ignored; if @env{TIME_STYLE}
9456 begins with @samp{posix-} the @samp{posix-} is ignored; and if
9457 @env{TIME_STYLE} is @samp{locale} it is ignored.
9460 @itemx --one-file-system
9462 @opindex --one-file-system
9463 @cindex one file system, restricting @command{du} to
9464 Skip directories that are on different file systems from the one that
9465 the argument being processed is on.
9467 @item --exclude=@var{PATTERN}
9468 @opindex --exclude=@var{PATTERN}
9469 @cindex excluding files from @command{du}
9470 When recursing, skip subdirectories or files matching @var{PATTERN}.
9471 For example, @code{du --exclude='*.o'} excludes files whose names
9475 @itemx --exclude-from=@var{FILE}
9476 @opindex -X @var{FILE}
9477 @opindex --exclude-from=@var{FILE}
9478 @cindex excluding files from @command{du}
9479 Like @option{--exclude}, except take the patterns to exclude from @var{FILE},
9480 one per line. If @var{FILE} is @samp{-}, take the patterns from standard
9485 @cindex NFS mounts from BSD to HP-UX
9486 On BSD systems, @command{du} reports sizes that are half the correct
9487 values for files that are NFS-mounted from HP-UX systems. On HP-UX
9488 systems, it reports sizes that are twice the correct values for
9489 files that are NFS-mounted from BSD systems. This is due to a flaw
9490 in HP-UX; it also affects the HP-UX @command{du} program.
9495 @node stat invocation
9496 @section @command{stat}: Report file or file system status
9500 @cindex file system status
9502 @command{stat} displays information about the specified file(s). Synopsis:
9505 stat [@var{option}]@dots{} [@var{file}]@dots{}
9508 With no option, @command{stat} reports all information about the given files.
9509 But it also can be used to report the information of the file systems the
9510 given files are located on. If the files are links, @command{stat} can
9511 also give information about the files the links point to.
9517 @itemx --dereference
9519 @opindex --dereference
9520 @cindex symbolic links, dereferencing in @command{stat}
9521 Change how @command{stat} treats symbolic links.
9522 With this option, @command{stat} acts on the file referenced
9523 by each symbolic link argument.
9524 Without it, @command{stat} acts on any symbolic link argument directly.
9527 @itemx --file-system
9529 @opindex --file-system
9530 @cindex file systems
9531 Report information about the file systems where the given files are located
9532 instead of information about the files themselves.
9535 @itemx --format=@var{format}
9537 @opindex --format=@var{format}
9538 @cindex output format
9539 Use @var{format} rather than the default format.
9540 @var{format} is automatically newline-terminated, so
9541 running a command like the following with two or more @var{file}
9542 operands produces a line of output for each operand:
9544 $ stat --format=%d:%i / /usr
9549 @itemx --printf=@var{format}
9550 @opindex --printf=@var{format}
9551 @cindex output format
9552 Use @var{format} rather than the default format.
9553 Like like @option{--format}, but interpret backslash escapes,
9554 and do not output a mandatory trailing newline.
9555 If you want a newline, include @samp{\n} in the @var{format}.
9556 Here's how you would use @option{--printf} to print the device
9557 and inode numbers of @file{/} and @file{/usr}:
9559 $ stat --printf='%d:%i\n' / /usr
9568 @cindex terse output
9569 Print the information in terse form, suitable for parsing by other programs.
9571 The valid format sequences for files are:
9574 @item %a - Access rights in octal
9575 @item %A - Access rights in human readable form
9576 @item %b - Number of blocks allocated (see @samp{%B})
9577 @item %B - The size in bytes of each block reported by @samp{%b}
9578 @item %d - Device number in decimal
9579 @item %D - Device number in hex
9580 @item %f - Raw mode in hex
9581 @item %F - File type
9582 @item %g - Group ID of owner
9583 @item %G - Group name of owner
9584 @item %h - Number of hard links
9585 @item %i - Inode number
9586 @item %n - File name
9587 @item %N - Quoted file name with dereference if symbolic link
9588 @item %o - I/O block size
9589 @item %s - Total size, in bytes
9590 @item %t - Major device type in hex
9591 @item %T - Minor device type in hex
9592 @item %u - User ID of owner
9593 @item %U - User name of owner
9594 @item %x - Time of last access
9595 @item %X - Time of last access as seconds since Epoch
9596 @item %y - Time of last modification
9597 @item %Y - Time of last modification as seconds since Epoch
9598 @item %z - Time of last change
9599 @item %Z - Time of last change as seconds since Epoch
9602 The valid format sequences for file systems are:
9605 @item %a - Free blocks available to non-super-user
9606 @item %b - Total data blocks in file system
9607 @item %c - Total file nodes in file system
9608 @item %d - Free file nodes in file system
9609 @item %f - Free blocks in file system
9610 @item %i - File System ID in hex
9611 @item %l - Maximum length of file names
9612 @item %n - File name
9613 @item %s - Block size (for faster transfers)
9614 @item %S - Fundamental block size (for block counts)
9615 @item %t - Type in hex
9616 @item %T - Type in human readable form
9620 Time stamps are listed according to the time zone rules specified by
9621 the @env{TZ} environment variable, or by the system default rules if
9622 @env{TZ} is not set. @xref{TZ Variable,, Specifying the Time Zone
9623 with @env{TZ}, libc, The GNU C Library}.
9629 @node sync invocation
9630 @section @command{sync}: Synchronize data on disk with memory
9633 @cindex synchronize disk and memory
9635 @cindex superblock, writing
9636 @cindex inodes, written buffered
9637 @command{sync} writes any data buffered in memory out to disk. This can
9638 include (but is not limited to) modified superblocks, modified inodes,
9639 and delayed reads and writes. This must be implemented by the kernel;
9640 The @command{sync} program does nothing but exercise the @code{sync} system
9643 @cindex crashes and corruption
9644 The kernel keeps data in memory to avoid doing (relatively slow) disk
9645 reads and writes. This improves performance, but if the computer
9646 crashes, data may be lost or the file system corrupted as a
9647 result. The @command{sync} command ensures everything in memory
9650 Any arguments are ignored, except for a lone @option{--help} or
9651 @option{--version} (@pxref{Common options}).
9657 @chapter Printing text
9659 @cindex printing text, commands for
9660 @cindex commands for printing text
9662 This section describes commands that display text strings.
9665 * echo invocation:: Print a line of text.
9666 * printf invocation:: Format and print data.
9667 * yes invocation:: Print a string until interrupted.
9671 @node echo invocation
9672 @section @command{echo}: Print a line of text
9675 @cindex displaying text
9676 @cindex printing text
9677 @cindex text, displaying
9678 @cindex arbitrary text, displaying
9680 @command{echo} writes each given @var{string} to standard output, with a
9681 space between each and a newline after the last one. Synopsis:
9684 echo [@var{option}]@dots{} [@var{string}]@dots{}
9687 The program accepts the following options. Also see @ref{Common options}.
9688 Options must precede operands, and the normally-special argument
9689 @samp{--} has no special meaning and is treated like any other
9695 Do not output the trailing newline.
9699 @cindex backslash escapes
9700 Enable interpretation of the following backslash-escaped characters in
9709 suppress trailing newline
9723 the eight-bit value that is the octal number @var{nnn}
9724 (zero to three octal digits)
9726 the eight-bit value that is the octal number @var{nnn}
9727 (one to three octal digits)
9729 the eight-bit value that is the hexadecimal number @var{hh}
9730 (one or two hexadecimal digits)
9735 @cindex backslash escapes
9736 Disable interpretation of backslash escapes in each @var{string}.
9737 This is the default. If @option{-e} and @option{-E} are both
9738 specified, the last one given takes effect.
9742 @vindex POSIXLY_CORRECT
9743 If the @env{POSIXLY_CORRECT} environment variable is set, then when
9744 @command{echo}'s first argument is not @option{-n} it outputs
9745 option-like arguments instead of treating them as options. For
9746 example, @code{echo -ne hello} outputs @samp{-ne hello} instead of
9749 @acronym{POSIX} does not require support for any options, and says
9750 that the behavior of @command{echo} is implementation-defined if any
9751 @var{string} contains a backslash or if the first argument is
9752 @option{-n}. Portable programs can use the @command{printf} command
9753 if they need to omit trailing newlines or output control characters or
9754 backslashes. @xref{printf invocation}.
9759 @node printf invocation
9760 @section @command{printf}: Format and print data
9763 @command{printf} does formatted printing of text. Synopsis:
9766 printf @var{format} [@var{argument}]@dots{}
9769 @command{printf} prints the @var{format} string, interpreting @samp{%}
9770 directives and @samp{\} escapes to format numeric and string arguments
9771 in a way that is mostly similar to the C @samp{printf} function. The
9772 differences are as follows:
9777 The @var{format} argument is reused as necessary to convert all the
9778 given @var{argument}s. For example, the command @samp{printf %s a b}
9782 Missing @var{argument}s are treated as null strings or as zeros,
9783 depending on whether the context expects a string or a number. For
9784 example, the command @samp{printf %sx%d} prints @samp{x0}.
9788 An additional escape, @samp{\c}, causes @command{printf} to produce no
9789 further output. For example, the command @samp{printf 'A%sC\cD%sF' B
9790 E} prints @samp{ABC}.
9793 The hexadecimal escape sequence @samp{\x@var{hh}} has at most two
9794 digits, as opposed to C where it can have an unlimited number of
9795 digits. For example, the command @samp{printf '\x07e'} prints two
9796 bytes, whereas the C statement @samp{printf ("\x07e")} prints just
9801 @command{printf} has an additional directive, @samp{%b}, which prints its
9802 argument string with @samp{\} escapes interpreted in the same way as in
9803 the @var{format} string, except that octal escapes are of the form
9804 @samp{\0@var{ooo}} where @var{ooo} is 0 to 3 octal digits.
9805 If a precision is also given, it limits the number of bytes printed
9806 from the converted string.
9809 Numeric arguments must be single C constants, possibly with leading
9810 @samp{+} or @samp{-}. For example, @samp{printf %.4d -3} outputs
9814 @vindex POSIXLY_CORRECT
9815 If the leading character of a numeric argument is @samp{"} or @samp{'}
9816 then its value is the numeric value of the immediately following
9817 character. Any remaining characters are silently ignored if the
9818 @env{POSIXLY_CORRECT} environment variable is set; otherwise, a
9819 warning is printed. For example, @samp{printf "%d" "'a"} outputs
9820 @samp{97} on hosts that use the @acronym{ASCII} character set, since
9821 @samp{a} has the numeric value 97 in @acronym{ASCII}.
9826 A floating-point argument must use a period before any fractional
9827 digits, but is printed according to the @env{LC_NUMERIC} category of the
9828 current locale. For example, in a locale whose radix character is a
9829 comma, the command @samp{printf %g 3.14} outputs @samp{3,14} whereas
9830 the command @samp{printf %g 3,14} is an error.
9834 @command{printf} interprets @samp{\@var{ooo}} in @var{format} as an octal number
9835 (if @var{ooo} is 1 to 3 octal digits) specifying a character to print,
9836 and @samp{\x@var{hh}} as a hexadecimal number (if @var{hh} is 1 to 2 hex
9837 digits) specifying a character to print.
9842 @cindex ISO/IEC 10646
9844 @command{printf} interprets two character syntaxes introduced in
9846 @samp{\u} for 16-bit Unicode (@acronym{ISO}/@acronym{IEC} 10646)
9847 characters, specified as
9848 four hexadecimal digits @var{hhhh}, and @samp{\U} for 32-bit Unicode
9849 characters, specified as eight hexadecimal digits @var{hhhhhhhh}.
9850 @command{printf} outputs the Unicode characters
9851 according to the @env{LC_CTYPE} locale.
9853 The processing of @samp{\u} and @samp{\U} requires a full-featured
9854 @code{iconv} facility. It is activated on systems with glibc 2.2 (or newer),
9855 or when @code{libiconv} is installed prior to this package. Otherwise
9856 @samp{\u} and @samp{\U} will print as-is.
9858 The only options are a lone @option{--help} or
9859 @option{--version}. @xref{Common options}.
9860 Options must precede operands.
9862 The Unicode character syntaxes are useful for writing strings in a locale
9863 independent way. For example, a string containing the Euro currency symbol
9866 $ /usr/local/bin/printf '\u20AC 14.95'
9870 will be output correctly in all locales supporting the Euro symbol
9871 (@acronym{ISO}-8859-15, UTF-8, and others). Similarly, a Chinese string
9874 $ /usr/local/bin/printf '\u4e2d\u6587'
9878 will be output correctly in all Chinese locales (GB2312, BIG5, UTF-8, etc).
9880 Note that in these examples, the full name of @command{printf} has been
9881 given, to distinguish it from the GNU @code{bash} built-in function
9884 For larger strings, you don't need to look up the hexadecimal code
9885 values of each character one by one. @acronym{ASCII} characters mixed with \u
9886 escape sequences is also known as the JAVA source file encoding. You can
9887 use GNU recode 3.5c (or newer) to convert strings to this encoding. Here
9888 is how to convert a piece of text into a shell script which will output
9889 this text in a locale-independent way:
9892 $ LC_CTYPE=zh_CN.big5 /usr/local/bin/printf \
9893 '\u4e2d\u6587\n' > sample.txt
9894 $ recode BIG5..JAVA < sample.txt \
9895 | sed -e "s|^|/usr/local/bin/printf '|" -e "s|$|\\\\n'|" \
9902 @node yes invocation
9903 @section @command{yes}: Print a string until interrupted
9906 @cindex repeated output of a string
9908 @command{yes} prints the command line arguments, separated by spaces and
9909 followed by a newline, forever until it is killed. If no arguments are
9910 given, it prints @samp{y} followed by a newline forever until killed.
9912 Upon a write error, @command{yes} exits with status @samp{1}.
9914 The only options are a lone @option{--help} or @option{--version}.
9915 To output an argument that begins with
9916 @samp{-}, precede it with @option{--}, e.g., @samp{yes -- --help}.
9917 @xref{Common options}.
9924 @cindex commands for exit status
9925 @cindex exit status commands
9927 This section describes commands that are primarily useful for their exit
9928 status, rather than their output. Thus, they are often used as the
9929 condition of shell @code{if} statements, or as the last command in a
9933 * false invocation:: Do nothing, unsuccessfully.
9934 * true invocation:: Do nothing, successfully.
9935 * test invocation:: Check file types and compare values.
9936 * expr invocation:: Evaluate expressions.
9940 @node false invocation
9941 @section @command{false}: Do nothing, unsuccessfully
9944 @cindex do nothing, unsuccessfully
9945 @cindex failure exit status
9946 @cindex exit status of @command{false}
9948 @command{false} does nothing except return an exit status of 1, meaning
9949 @dfn{failure}. It can be used as a place holder in shell scripts
9950 where an unsuccessful command is needed.
9951 In most modern shells, @command{false} is a built-in command, so when
9952 you use @samp{false} in a script, you're probably using the built-in
9953 command, not the one documented here.
9955 @command{false} honors the @option{--help} and @option{--version} options.
9957 This version of @command{false} is implemented as a C program, and is thus
9958 more secure and faster than a shell script implementation, and may safely
9959 be used as a dummy shell for the purpose of disabling accounts.
9961 Note that @command{false} (unlike all other programs documented herein)
9962 exits unsuccessfully, even when invoked with
9963 @option{--help} or @option{--version}.
9965 Portable programs should not assume that the exit status of
9966 @command{false} is 1, as it is greater than 1 on some
9967 non-@acronym{GNU} hosts.
9970 @node true invocation
9971 @section @command{true}: Do nothing, successfully
9974 @cindex do nothing, successfully
9976 @cindex successful exit
9977 @cindex exit status of @command{true}
9979 @command{true} does nothing except return an exit status of 0, meaning
9980 @dfn{success}. It can be used as a place holder in shell scripts
9981 where a successful command is needed, although the shell built-in
9982 command @code{:} (colon) may do the same thing faster.
9983 In most modern shells, @command{true} is a built-in command, so when
9984 you use @samp{true} in a script, you're probably using the built-in
9985 command, not the one documented here.
9987 @command{true} honors the @option{--help} and @option{--version} options.
9989 Note, however, that it is possible to cause @command{true}
9990 to exit with nonzero status: with the @option{--help} or @option{--version}
9991 option, and with standard
9992 output already closed or redirected to a file that evokes an I/O error.
9993 For example, using a Bourne-compatible shell:
9996 $ ./true --version >&-
9997 ./true: write error: Bad file number
9998 $ ./true --version > /dev/full
9999 ./true: write error: No space left on device
10002 This version of @command{true} is implemented as a C program, and is thus
10003 more secure and faster than a shell script implementation, and may safely
10004 be used as a dummy shell for the purpose of disabling accounts.
10006 @node test invocation
10007 @section @command{test}: Check file types and compare values
10010 @cindex check file types
10011 @cindex compare values
10012 @cindex expression evaluation
10014 @command{test} returns a status of 0 (true) or 1 (false) depending on the
10015 evaluation of the conditional expression @var{expr}. Each part of the
10016 expression must be a separate argument.
10018 @command{test} has file status checks, string operators, and numeric
10019 comparison operators.
10021 @command{test} has an alternate form that uses opening and closing
10022 square brackets instead a leading @samp{test}. For example, instead
10023 of @samp{test -d /}, you can write @samp{[ -d / ]}. The square
10024 brackets must be separate arguments; for example, @samp{[-d /]} does
10025 not have the desired effect. Since @samp{test @var{expr}} and @samp{[
10026 @var{expr} ]} have the same meaning, only the former form is discussed
10032 test @var{expression}
10034 [ @var{expression} ]
10039 @cindex conflicts with shell built-ins
10040 @cindex built-in shell commands, conflicts with
10041 Because most shells have a built-in @command{test} command, using an
10042 unadorned @command{test} in a script or interactively may get you
10043 different functionality than that described here.
10045 If @var{expression} is omitted, @command{test} returns false.
10046 If @var{expression} is a single argument,
10047 @command{test} returns false if the argument is null and true otherwise. The argument
10048 can be any string, including strings like @samp{-d}, @samp{-1},
10049 @samp{--}, @samp{--help}, and @samp{--version} that most other
10050 programs would treat as options. To get help and version information,
10051 invoke the commands @samp{[ --help} and @samp{[ --version}, without
10052 the usual closing brackets. @xref{Common options}.
10054 @cindex exit status of @command{test}
10058 0 if the expression is true,
10059 1 if the expression is false,
10060 2 if an error occurred.
10064 * File type tests:: -[bcdfhLpSt]
10065 * Access permission tests:: -[gkruwxOG]
10066 * File characteristic tests:: -e -s -nt -ot -ef
10067 * String tests:: -z -n = !=
10068 * Numeric tests:: -eq -ne -lt -le -gt -ge
10069 * Connectives for test:: ! -a -o
10073 @node File type tests
10074 @subsection File type tests
10076 @cindex file type tests
10078 These options test for particular types of files. (Everything's a file,
10079 but not all files are the same!)
10083 @item -b @var{file}
10085 @cindex block special check
10086 True if @var{file} exists and is a block special device.
10088 @item -c @var{file}
10090 @cindex character special check
10091 True if @var{file} exists and is a character special device.
10093 @item -d @var{file}
10095 @cindex directory check
10096 True if @var{file} exists and is a directory.
10098 @item -f @var{file}
10100 @cindex regular file check
10101 True if @var{file} exists and is a regular file.
10103 @item -h @var{file}
10104 @itemx -L @var{file}
10107 @cindex symbolic link check
10108 True if @var{file} exists and is a symbolic link.
10109 Unlike all other file-related tests, this test does not dereference
10110 @var{file} if it is a symbolic link.
10112 @item -p @var{file}
10114 @cindex named pipe check
10115 True if @var{file} exists and is a named pipe.
10117 @item -S @var{file}
10119 @cindex socket check
10120 True if @var{file} exists and is a socket.
10124 @cindex terminal check
10125 True if @var{fd} is a file descriptor that is associated with a
10131 @node Access permission tests
10132 @subsection Access permission tests
10134 @cindex access permission tests
10135 @cindex permission tests
10137 These options test for particular access permissions.
10141 @item -g @var{file}
10143 @cindex set-group-ID check
10144 True if @var{file} exists and has its set-group-ID bit set.
10146 @item -k @var{file}
10148 @cindex sticky bit check
10149 True if @var{file} exists and has its @dfn{sticky} bit set.
10151 @item -r @var{file}
10153 @cindex readable file check
10154 True if @var{file} exists and read permission is granted.
10156 @item -u @var{file}
10158 @cindex set-user-ID check
10159 True if @var{file} exists and has its set-user-ID bit set.
10161 @item -w @var{file}
10163 @cindex writable file check
10164 True if @var{file} exists and write permission is granted.
10166 @item -x @var{file}
10168 @cindex executable file check
10169 True if @var{file} exists and execute permission is granted
10170 (or search permission, if it is a directory).
10172 @item -O @var{file}
10174 @cindex owned by effective user ID check
10175 True if @var{file} exists and is owned by the current effective user ID.
10177 @item -G @var{file}
10179 @cindex owned by effective group ID check
10180 True if @var{file} exists and is owned by the current effective group ID.
10184 @node File characteristic tests
10185 @subsection File characteristic tests
10187 @cindex file characteristic tests
10189 These options test other file characteristics.
10193 @item -e @var{file}
10195 @cindex existence-of-file check
10196 True if @var{file} exists.
10198 @item -s @var{file}
10200 @cindex nonempty file check
10201 True if @var{file} exists and has a size greater than zero.
10203 @item @var{file1} -nt @var{file2}
10205 @cindex newer-than file check
10206 True if @var{file1} is newer (according to modification date) than
10207 @var{file2}, or if @var{file1} exists and @var{file2} does not.
10209 @item @var{file1} -ot @var{file2}
10211 @cindex older-than file check
10212 True if @var{file1} is older (according to modification date) than
10213 @var{file2}, or if @var{file2} exists and @var{file1} does not.
10215 @item @var{file1} -ef @var{file2}
10217 @cindex same file check
10218 @cindex hard link check
10219 True if @var{file1} and @var{file2} have the same device and inode
10220 numbers, i.e., if they are hard links to each other.
10226 @subsection String tests
10228 @cindex string tests
10230 These options test string characteristics. You may need to quote
10231 @var{string} arguments for the shell. For example:
10237 The quotes here prevent the wrong arguments from being passed to
10238 @command{test} if @samp{$V} is empty or contains special characters.
10242 @item -z @var{string}
10244 @cindex zero-length string check
10245 True if the length of @var{string} is zero.
10247 @item -n @var{string}
10248 @itemx @var{string}
10250 @cindex nonzero-length string check
10251 True if the length of @var{string} is nonzero.
10253 @item @var{string1} = @var{string2}
10255 @cindex equal string check
10256 True if the strings are equal.
10258 @item @var{string1} != @var{string2}
10260 @cindex not-equal string check
10261 True if the strings are not equal.
10266 @node Numeric tests
10267 @subsection Numeric tests
10269 @cindex numeric tests
10270 @cindex arithmetic tests
10272 Numeric relationals. The arguments must be entirely numeric (possibly
10273 negative), or the special expression @w{@code{-l @var{string}}}, which
10274 evaluates to the length of @var{string}.
10278 @item @var{arg1} -eq @var{arg2}
10279 @itemx @var{arg1} -ne @var{arg2}
10280 @itemx @var{arg1} -lt @var{arg2}
10281 @itemx @var{arg1} -le @var{arg2}
10282 @itemx @var{arg1} -gt @var{arg2}
10283 @itemx @var{arg1} -ge @var{arg2}
10290 These arithmetic binary operators return true if @var{arg1} is equal,
10291 not-equal, less-than, less-than-or-equal, greater-than, or
10292 greater-than-or-equal than @var{arg2}, respectively.
10299 test -1 -gt -2 && echo yes
10301 test -l abc -gt 1 && echo yes
10304 @error{} test: integer expression expected before -eq
10308 @node Connectives for test
10309 @subsection Connectives for @command{test}
10311 @cindex logical connectives
10312 @cindex connectives, logical
10314 The usual logical connectives.
10320 True if @var{expr} is false.
10322 @item @var{expr1} -a @var{expr2}
10324 @cindex logical and operator
10325 @cindex and operator
10326 True if both @var{expr1} and @var{expr2} are true.
10328 @item @var{expr1} -o @var{expr2}
10330 @cindex logical or operator
10331 @cindex or operator
10332 True if either @var{expr1} or @var{expr2} is true.
10337 @node expr invocation
10338 @section @command{expr}: Evaluate expressions
10341 @cindex expression evaluation
10342 @cindex evaluation of expressions
10344 @command{expr} evaluates an expression and writes the result on standard
10345 output. Each token of the expression must be a separate argument.
10347 Operands are either integers or strings. Integers consist of one or
10348 more decimal digits, with an optional leading @samp{-}.
10349 @command{expr} converts
10350 anything appearing in an operand position to an integer or a string
10351 depending on the operation being applied to it.
10353 Strings are not quoted for @command{expr} itself, though you may need to
10354 quote them to protect characters with special meaning to the shell,
10355 e.g., spaces. However, regardless of whether it is quoted, a string
10356 operand should not be a parenthesis or any of @command{expr}'s
10357 operators like @code{+}, so you cannot safely pass an arbitrary string
10358 @code{$str} to expr merely by quoting it to the shell. One way to
10359 work around this is to use the @sc{gnu} extension @code{+},
10360 (e.g., @code{+ "$str" = foo}); a more portable way is to use
10361 @code{@w{" $str"}} and to adjust the rest of the expression to take
10362 the leading space into account (e.g., @code{@w{" $str" = " foo"}}).
10364 You should not pass a negative integer or a string with leading
10365 @samp{-} as @command{expr}'s first argument, as it might be
10366 misinterpreted as an option; this can be avoided by parenthesization.
10367 Also, portable scripts should not use a string operand that happens to
10368 take the form of an integer; this can be worked around by inserting
10369 leading spaces as mentioned above.
10371 @cindex parentheses for grouping
10372 Operators may be given as infix symbols or prefix keywords. Parentheses
10373 may be used for grouping in the usual manner. You must quote
10374 parentheses and many operators to avoid the shell evaluating them,
10377 The only options are @option{--help} and @option{--version}. @xref{Common
10378 options}. Options must precede operands.
10380 @cindex exit status of @command{expr}
10384 0 if the expression is neither null nor 0,
10385 1 if the expression is null or 0,
10386 2 if the expression is invalid,
10387 3 if an internal error occurred (e.g., arithmetic overflow).
10391 * String expressions:: + : match substr index length
10392 * Numeric expressions:: + - * / %
10393 * Relations for expr:: | & < <= = == != >= >
10394 * Examples of expr:: Examples.
10398 @node String expressions
10399 @subsection String expressions
10401 @cindex string expressions
10402 @cindex expressions, string
10404 @command{expr} supports pattern matching and other string operators. These
10405 have lower precedence than both the numeric and relational operators (in
10406 the next sections).
10410 @item @var{string} : @var{regex}
10411 @cindex pattern matching
10412 @cindex regular expression matching
10413 @cindex matching patterns
10414 Perform pattern matching. The arguments are converted to strings and the
10415 second is considered to be a (basic, a la GNU @code{grep}) regular
10416 expression, with a @code{^} implicitly prepended. The first argument is
10417 then matched against this regular expression.
10419 If the match succeeds and @var{regex} uses @samp{\(} and @samp{\)}, the
10420 @code{:} expression returns the part of @var{string} that matched the
10421 subexpression; otherwise, it returns the number of characters matched.
10423 If the match fails, the @code{:} operator returns the null string if
10424 @samp{\(} and @samp{\)} are used in @var{regex}, otherwise 0.
10426 @kindex \( @r{regexp operator}
10427 Only the first @samp{\( @dots{} \)} pair is relevant to the return
10428 value; additional pairs are meaningful only for grouping the regular
10429 expression operators.
10431 @kindex \+ @r{regexp operator}
10432 @kindex \? @r{regexp operator}
10433 @kindex \| @r{regexp operator}
10434 In the regular expression, @code{\+}, @code{\?}, and @code{\|} are
10435 operators which respectively match one or more, zero or one, or separate
10436 alternatives. SunOS and other @command{expr}'s treat these as regular
10437 characters. (@acronym{POSIX} allows either behavior.)
10438 @xref{Top, , Regular Expression Library, regex, Regex}, for details of
10439 regular expression syntax. Some examples are in @ref{Examples of expr}.
10441 @item match @var{string} @var{regex}
10443 An alternative way to do pattern matching. This is the same as
10444 @w{@samp{@var{string} : @var{regex}}}.
10446 @item substr @var{string} @var{position} @var{length}
10448 Returns the substring of @var{string} beginning at @var{position}
10449 with length at most @var{length}. If either @var{position} or
10450 @var{length} is negative, zero, or non-numeric, returns the null string.
10452 @item index @var{string} @var{charset}
10454 Returns the first position in @var{string} where the first character in
10455 @var{charset} was found. If no character in @var{charset} is found in
10456 @var{string}, return 0.
10458 @item length @var{string}
10460 Returns the length of @var{string}.
10462 @item + @var{token}
10464 Interpret @var{token} as a string, even if it is a keyword like @var{match}
10465 or an operator like @code{/}.
10466 This makes it possible to test @code{expr length + "$x"} or
10467 @code{expr + "$x" : '.*/\(.\)'} and have it do the right thing even if
10468 the value of @var{$x} happens to be (for example) @code{/} or @code{index}.
10469 This operator is a @acronym{GNU} extension. Portable shell scripts should use
10470 @code{@w{" $token"} : @w{' \(.*\)'}} instead of @code{+ "$token"}.
10474 To make @command{expr} interpret keywords as strings, you must use the
10475 @code{quote} operator.
10478 @node Numeric expressions
10479 @subsection Numeric expressions
10481 @cindex numeric expressions
10482 @cindex expressions, numeric
10484 @command{expr} supports the usual numeric operators, in order of increasing
10485 precedence. The string operators (previous section) have lower precedence,
10486 the connectives (next section) have higher.
10494 @cindex subtraction
10495 Addition and subtraction. Both arguments are converted to integers;
10496 an error occurs if this cannot be done.
10502 @cindex multiplication
10505 Multiplication, division, remainder. Both arguments are converted to
10506 integers; an error occurs if this cannot be done.
10511 @node Relations for expr
10512 @subsection Relations for @command{expr}
10514 @cindex connectives, logical
10515 @cindex logical connectives
10516 @cindex relations, numeric or string
10518 @command{expr} supports the usual logical connectives and relations. These
10519 are higher precedence than either the string or numeric operators
10520 (previous sections). Here is the list, lowest-precedence operator first.
10526 @cindex logical or operator
10527 @cindex or operator
10528 Returns its first argument if that is neither null nor zero, otherwise
10529 its second argument if it is neither null nor zero, otherwise 0. It
10530 does not evaluate its second argument if its first argument is neither
10535 @cindex logical and operator
10536 @cindex and operator
10537 Return its first argument if neither argument is null or zero, otherwise
10538 0. It does not evaluate its second argument if its first argument is
10541 @item < <= = == != >= >
10548 @cindex comparison operators
10550 Compare the arguments and return 1 if the relation is true, 0 otherwise.
10551 @code{==} is a synonym for @code{=}. @command{expr} first tries to convert
10552 both arguments to integers and do a numeric comparison; if either
10553 conversion fails, it does a lexicographic comparison using the character
10554 collating sequence specified by the @env{LC_COLLATE} locale.
10559 @node Examples of expr
10560 @subsection Examples of using @command{expr}
10562 @cindex examples of @command{expr}
10563 Here are a few examples, including quoting for shell metacharacters.
10565 To add 1 to the shell variable @code{foo}, in Bourne-compatible shells:
10568 foo=`expr $foo + 1`
10571 To print the non-directory part of the file name stored in
10572 @code{$fname}, which need not contain a @code{/}:
10575 expr $fname : '.*/\(.*\)' '|' $fname
10578 An example showing that @code{\+} is an operator:
10586 expr abc : 'a\(.\)c'
10588 expr index abcdef cz
10591 @error{} expr: syntax error
10592 expr index quote index a
10598 @chapter Redirection
10600 @cindex redirection
10601 @cindex commands for redirection
10603 Unix shells commonly provide several forms of @dfn{redirection}---ways
10604 to change the input source or output destination of a command. But one
10605 useful redirection is performed by a separate command, not by the shell;
10606 it's described here.
10609 * tee invocation:: Redirect output to multiple files.
10613 @node tee invocation
10614 @section @command{tee}: Redirect output to multiple files
10617 @cindex pipe fitting
10618 @cindex destinations, multiple output
10619 @cindex read from stdin and write to stdout and files
10621 The @command{tee} command copies standard input to standard output and also
10622 to any files given as arguments. This is useful when you want not only
10623 to send some data down a pipe, but also to save a copy. Synopsis:
10626 tee [@var{option}]@dots{} [@var{file}]@dots{}
10629 If a file being written to does not already exist, it is created. If a
10630 file being written to already exists, the data it previously contained
10631 is overwritten unless the @option{-a} option is used.
10633 A @var{file} of @samp{-} causes @command{tee} to send another copy of
10634 input to standard output, but this is typically not that useful as the
10635 copies are interleaved.
10637 The program accepts the following options. Also see @ref{Common options}.
10644 Append standard input to the given files rather than overwriting
10648 @itemx --ignore-interrupts
10650 @opindex --ignore-interrupts
10651 Ignore interrupt signals.
10658 @node File name manipulation
10659 @chapter File name manipulation
10661 @cindex file name manipulation
10662 @cindex manipulation of file names
10663 @cindex commands for file name manipulation
10665 This section describes commands that manipulate file names.
10668 * basename invocation:: Strip directory and suffix from a file name.
10669 * dirname invocation:: Strip non-directory suffix from a file name.
10670 * pathchk invocation:: Check file name portability.
10674 @node basename invocation
10675 @section @command{basename}: Strip directory and suffix from a file name
10678 @cindex strip directory and suffix from file names
10679 @cindex directory, stripping from file names
10680 @cindex suffix, stripping from file names
10681 @cindex file names, stripping directory and suffix
10682 @cindex leading directory components, stripping
10684 @command{basename} removes any leading directory components from
10685 @var{name}. Synopsis:
10688 basename @var{name} [@var{suffix}]
10691 If @var{suffix} is specified and is identical to the end of @var{name},
10692 it is removed from @var{name} as well. Note that since trailing slashes
10693 are removed prior to suffix matching, @var{suffix} will do nothing if it
10694 contains slashes. @command{basename} prints the result on standard
10697 @c This test is used both here and in the section on dirname.
10698 @macro basenameAndDirname
10699 Together, @command{basename} and @command{dirname} are designed such
10700 that if @samp{ls "$name"} succeeds, then the command sequence @samp{cd
10701 "$(dirname "$name")"; ls "$(basename "$name")"} will, too. This works
10702 for everything except file names containing a trailing newline.
10704 @basenameAndDirname
10706 @acronym{POSIX} allows the implementation to define the results if
10707 @var{name} is empty or @samp{//}. In the former case, @acronym{GNU}
10708 @command{basename} returns the empty string. In the latter case, the
10709 result is @samp{//} on platforms where @var{//} is distinct from
10710 @var{/}, and @samp{/} on platforms where there is no difference.
10712 The only options are @option{--help} and @option{--version}. @xref{Common
10713 options}. Options must precede operands.
10721 basename /usr/bin/sort
10724 basename include/stdio.h .h
10728 @node dirname invocation
10729 @section @command{dirname}: Strip non-directory suffix from a file name
10732 @cindex directory components, printing
10733 @cindex stripping non-directory suffix
10734 @cindex non-directory suffix, stripping
10736 @command{dirname} prints all but the final slash-delimited component of
10737 a string (presumably a file name). Synopsis:
10743 If @var{name} is a single component, @command{dirname} prints @samp{.}
10744 (meaning the current directory).
10746 @basenameAndDirname
10748 @acronym{POSIX} allows the implementation to define the results if
10749 @var{name} is @samp{//}. With @acronym{GNU} @command{dirname}, the
10750 result is @samp{//} on platforms where @var{//} is distinct from
10751 @var{/}, and @samp{/} on platforms where there is no difference.
10753 The only options are @option{--help} and @option{--version}. @xref{Common
10761 # Output "/usr/bin".
10762 dirname /usr/bin/sort
10769 @node pathchk invocation
10770 @section @command{pathchk}: Check file name portability
10773 @cindex file names, checking validity and portability
10774 @cindex valid file names, checking for
10775 @cindex portable file names, checking for
10777 @command{pathchk} checks portability of file names. Synopsis:
10780 pathchk [@var{option}]@dots{} @var{name}@dots{}
10783 For each @var{name}, @command{pathchk} prints a message if any of
10784 these conditions is true:
10788 One of the existing directories in @var{name} does not have search
10789 (execute) permission,
10791 The length of @var{name} is larger than the maximum supported by the
10794 The length of one component of @var{name} is longer than
10795 its file system's maximum.
10798 A nonexistent @var{name} is not an error, so long a file with that
10799 name could be created under the above conditions.
10801 The program accepts the following options. Also see @ref{Common options}.
10802 Options must precede operands.
10808 Instead of performing checks based on the underlying file system,
10809 print a message if any of these conditions is true:
10813 A file name is empty.
10816 The length of a file name or one of its components exceeds the
10817 @acronym{POSIX} minimum limits for portability.
10820 A file name contains a character outside the portable file name
10821 character set, namely, the ASCII letters and digits, @samp{-},
10822 @samp{.}, @samp{/}, and @samp{_}.
10827 Print a message if a file name is empty, or if it contains a component
10828 that begins with @samp{-}.
10830 @item --portability
10831 @opindex --portability
10832 Print a message if a file name is not portable to all @acronym{POSIX}
10833 hosts. This option is equivalent to @samp{-p -P}.
10837 @cindex exit status of @command{pathchk}
10841 0 if all specified file names passed all checks,
10846 @node Working context
10847 @chapter Working context
10849 @cindex working context
10850 @cindex commands for printing the working context
10852 This section describes commands that display or alter the context in
10853 which you are working: the current directory, the terminal settings, and
10854 so forth. See also the user-related commands in the next section.
10857 * pwd invocation:: Print working directory.
10858 * stty invocation:: Print or change terminal characteristics.
10859 * printenv invocation:: Print environment variables.
10860 * tty invocation:: Print file name of terminal on standard input.
10864 @node pwd invocation
10865 @section @command{pwd}: Print working directory
10868 @cindex print name of current directory
10869 @cindex current working directory, printing
10870 @cindex working directory, printing
10872 @cindex symbolic links and @command{pwd}
10873 @command{pwd} prints the fully resolved name of the current directory.
10874 That is, all components of the printed name will be actual directory
10875 names---none will be symbolic links.
10877 @cindex conflicts with shell built-ins
10878 @cindex built-in shell commands, conflicts with
10879 Because most shells have a built-in @command{pwd} command, using an
10880 unadorned @command{pwd} in a script or interactively may get you
10881 different functionality than that described here.
10883 The only options are a lone @option{--help} or
10884 @option{--version}. @xref{Common options}.
10889 @node stty invocation
10890 @section @command{stty}: Print or change terminal characteristics
10893 @cindex change or print terminal settings
10894 @cindex terminal settings
10895 @cindex line settings of terminal
10897 @command{stty} prints or changes terminal characteristics, such as baud rate.
10901 stty [@var{option}] [@var{setting}]@dots{}
10902 stty [@var{option}]
10905 If given no line settings, @command{stty} prints the baud rate, line
10906 discipline number (on systems that support it), and line settings
10907 that have been changed from the values set by @samp{stty sane}.
10908 By default, mode reading and setting are performed on the tty line
10909 connected to standard input, although this can be modified by the
10910 @option{--file} option.
10912 @command{stty} accepts many non-option arguments that change aspects of
10913 the terminal line operation, as described below.
10915 The program accepts the following options. Also see @ref{Common options}.
10922 Print all current settings in human-readable form. This option may not
10923 be used in combination with any line settings.
10925 @item -F @var{device}
10926 @itemx --file=@var{device}
10929 Set the line opened by the file name specified in @var{device} instead of
10930 the tty line connected to standard input. This option is necessary
10931 because opening a @acronym{POSIX} tty requires use of the @code{O_NONDELAY} flag to
10932 prevent a @acronym{POSIX} tty from blocking until the carrier detect line is high if
10933 the @code{clocal} flag is not set. Hence, it is not always possible
10934 to allow the shell to open the device in the traditional manner.
10940 @cindex machine-readable @command{stty} output
10941 Print all current settings in a form that can be used as an argument to
10942 another @command{stty} command to restore the current settings. This option
10943 may not be used in combination with any line settings.
10947 Many settings can be turned off by preceding them with a @samp{-}.
10948 Such arguments are marked below with ``May be negated'' in their
10949 description. The descriptions themselves refer to the positive
10950 case, that is, when @emph{not} negated (unless stated otherwise,
10953 Some settings are not available on all @acronym{POSIX} systems, since they use
10954 extensions. Such arguments are marked below with ``Non-@acronym{POSIX}'' in their
10955 description. On non-@acronym{POSIX} systems, those or other settings also may not
10956 be available, but it's not feasible to document all the variations: just
10962 * Control:: Control settings
10963 * Input:: Input settings
10964 * Output:: Output settings
10965 * Local:: Local settings
10966 * Combination:: Combination settings
10967 * Characters:: Special characters
10968 * Special:: Special settings
10973 @subsection Control settings
10975 @cindex control settings
10981 @cindex two-way parity
10982 Generate parity bit in output and expect parity bit in input.
10988 @cindex even parity
10989 Set odd parity (even if negated). May be negated.
10996 @cindex character size
10997 @cindex eight-bit characters
10998 Set character size to 5, 6, 7, or 8 bits.
11003 Send a hangup signal when the last process closes the tty. May be
11009 Use two stop bits per character (one if negated). May be negated.
11013 Allow input to be received. May be negated.
11017 @cindex modem control
11018 Disable modem control signals. May be negated.
11022 @cindex hardware flow control
11023 @cindex flow control, hardware
11024 @cindex RTS/CTS flow control
11025 Enable RTS/CTS flow control. Non-@acronym{POSIX}. May be negated.
11030 @subsection Input settings
11032 @cindex input settings
11037 @cindex breaks, ignoring
11038 Ignore break characters. May be negated.
11042 @cindex breaks, cause interrupts
11043 Make breaks cause an interrupt signal. May be negated.
11047 @cindex parity, ignoring
11048 Ignore characters with parity errors. May be negated.
11052 @cindex parity errors, marking
11053 Mark parity errors (with a 255-0-character sequence). May be negated.
11057 Enable input parity checking. May be negated.
11061 @cindex eight-bit input
11062 Clear high (8th) bit of input characters. May be negated.
11066 @cindex newline, translating to return
11067 Translate newline to carriage return. May be negated.
11071 @cindex return, ignoring
11072 Ignore carriage return. May be negated.
11076 @cindex return, translating to newline
11077 Translate carriage return to newline. May be negated.
11081 @cindex input encoding, UTF-8
11082 Assume input characters are UTF-8 encoded. May be negated.
11086 @kindex C-s/C-q flow control
11087 @cindex XON/XOFF flow control
11088 Enable XON/XOFF flow control (that is, @kbd{CTRL-S}/@kbd{CTRL-Q}). May
11095 @cindex software flow control
11096 @cindex flow control, software
11097 Enable sending of @code{stop} character when the system input buffer
11098 is almost full, and @code{start} character when it becomes almost
11099 empty again. May be negated.
11103 @cindex uppercase, translating to lowercase
11104 Translate uppercase characters to lowercase. Non-@acronym{POSIX}. May be
11109 Allow any character to restart output (only the start character
11110 if negated). Non-@acronym{POSIX}. May be negated.
11114 @cindex beeping at input buffer full
11115 Enable beeping and not flushing input buffer if a character arrives
11116 when the input buffer is full. Non-@acronym{POSIX}. May be negated.
11121 @subsection Output settings
11123 @cindex output settings
11124 These arguments specify output-related operations.
11129 Postprocess output. May be negated.
11133 @cindex lowercase, translating to output
11134 Translate lowercase characters to uppercase. Non-@acronym{POSIX}. May be
11139 @cindex return, translating to newline
11140 Translate carriage return to newline. Non-@acronym{POSIX}. May be negated.
11144 @cindex newline, translating to crlf
11145 Translate newline to carriage return-newline. Non-@acronym{POSIX}. May be
11150 Do not print carriage returns in the first column. Non-@acronym{POSIX}.
11155 Newline performs a carriage return. Non-@acronym{POSIX}. May be negated.
11159 @cindex pad instead of timing for delaying
11160 Use fill (padding) characters instead of timing for delays. Non-@acronym{POSIX}.
11165 @cindex pad character
11166 Use delete characters for fill instead of null characters. Non-@acronym{POSIX}.
11172 Newline delay style. Non-@acronym{POSIX}.
11179 Carriage return delay style. Non-@acronym{POSIX}.
11185 @opindex tab@var{n}
11186 Horizontal tab delay style. Non-@acronym{POSIX}.
11191 Backspace delay style. Non-@acronym{POSIX}.
11196 Vertical tab delay style. Non-@acronym{POSIX}.
11201 Form feed delay style. Non-@acronym{POSIX}.
11206 @subsection Local settings
11208 @cindex local settings
11213 Enable @code{interrupt}, @code{quit}, and @code{suspend} special
11214 characters. May be negated.
11218 Enable @code{erase}, @code{kill}, @code{werase}, and @code{rprnt}
11219 special characters. May be negated.
11223 Enable non-@acronym{POSIX} special characters. May be negated.
11227 Echo input characters. May be negated.
11233 Echo @code{erase} characters as backspace-space-backspace. May be
11238 @cindex newline echoing after @code{kill}
11239 Echo a newline after a @code{kill} character. May be negated.
11243 @cindex newline, echoing
11244 Echo newline even if not echoing other characters. May be negated.
11248 @cindex flushing, disabling
11249 Disable flushing after @code{interrupt} and @code{quit} special
11250 characters. May be negated.
11254 @cindex case translation
11255 Enable input and output of uppercase characters by preceding their
11256 lowercase equivalents with @samp{\}, when @code{icanon} is set.
11257 Non-@acronym{POSIX}. May be negated.
11261 @cindex background jobs, stopping at terminal write
11262 Stop background jobs that try to write to the terminal. Non-@acronym{POSIX}.
11269 Echo erased characters backward, between @samp{\} and @samp{/}.
11270 Non-@acronym{POSIX}. May be negated.
11276 @cindex control characters, using @samp{^@var{c}}
11277 @cindex hat notation for control characters
11278 Echo control characters in hat notation (@samp{^@var{c}}) instead
11279 of literally. Non-@acronym{POSIX}. May be negated.
11285 Echo the @code{kill} special character by erasing each character on
11286 the line as indicated by the @code{echoprt} and @code{echoe} settings,
11287 instead of by the @code{echoctl} and @code{echok} settings. Non-@acronym{POSIX}.
11293 @subsection Combination settings
11295 @cindex combination settings
11296 Combination settings:
11303 Same as @code{parenb -parodd cs7}. May be negated. If negated, same
11304 as @code{-parenb cs8}.
11308 Same as @code{parenb parodd cs7}. May be negated. If negated, same
11309 as @code{-parenb cs8}.
11313 Same as @code{-icrnl -onlcr}. May be negated. If negated, same as
11314 @code{icrnl -inlcr -igncr onlcr -ocrnl -onlret}.
11318 Reset the @code{erase} and @code{kill} special characters to their default
11325 @c This is too long to write inline.
11327 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff
11328 -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr
11329 -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0
11330 ff0 isig icanon iexten echo echoe echok -echonl
11331 -noflsh -xcase -tostop -echoprt echoctl echoke
11335 and also sets all special characters to their default values.
11339 Same as @code{brkint ignpar istrip icrnl ixon opost isig icanon}, plus
11340 sets the @code{eof} and @code{eol} characters to their default values
11341 if they are the same as the @code{min} and @code{time} characters.
11342 May be negated. If negated, same as @code{raw}.
11349 -ignbrk -brkint -ignpar -parmrk -inpck -istrip
11350 -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
11351 -imaxbel -opost -isig -icanon -xcase min 1 time 0
11355 May be negated. If negated, same as @code{cooked}.
11359 Same as @option{-icanon}. May be negated. If negated, same as
11364 @cindex eight-bit characters
11365 Same as @code{-parenb -istrip cs8}. May be negated. If negated,
11366 same as @code{parenb istrip cs7}.
11370 Same as @option{-parenb -istrip -opost cs8}. May be negated.
11371 If negated, same as @code{parenb istrip opost cs7}.
11375 Same as @option{-ixany}. Non-@acronym{POSIX}. May be negated.
11379 Same as @code{tab0}. Non-@acronym{POSIX}. May be negated. If negated, same
11386 Same as @code{xcase iuclc olcuc}. Non-@acronym{POSIX}. May be negated.
11390 Same as @code{echoe echoctl echoke}.
11394 Same as @code{echoe echoctl echoke -ixany intr ^C erase ^? kill C-u}.
11399 @subsection Special characters
11401 @cindex special characters
11402 @cindex characters, special
11404 The special characters' default values vary from system to system.
11405 They are set with the syntax @samp{name value}, where the names are
11406 listed below and the value can be given either literally, in hat
11407 notation (@samp{^@var{c}}), or as an integer which may start with
11408 @samp{0x} to indicate hexadecimal, @samp{0} to indicate octal, or
11409 any other digit to indicate decimal.
11411 @cindex disabling special characters
11412 @kindex u@r{, and disabling special characters}
11413 For GNU stty, giving a value of @code{^-} or @code{undef} disables that
11414 special character. (This is incompatible with Ultrix @command{stty},
11415 which uses a value of @samp{u} to disable a special character. GNU
11416 @command{stty} treats a value @samp{u} like any other, namely to set that
11417 special character to @key{U}.)
11423 Send an interrupt signal.
11427 Send a quit signal.
11431 Erase the last character typed.
11435 Erase the current line.
11439 Send an end of file (terminate the input).
11447 Alternate character to end the line. Non-@acronym{POSIX}.
11451 Switch to a different shell layer. Non-@acronym{POSIX}.
11455 Restart the output after stopping it.
11463 Send a terminal stop signal.
11467 Send a terminal stop signal after flushing the input. Non-@acronym{POSIX}.
11471 Redraw the current line. Non-@acronym{POSIX}.
11475 Erase the last word typed. Non-@acronym{POSIX}.
11479 Enter the next character typed literally, even if it is a special
11480 character. Non-@acronym{POSIX}.
11485 @subsection Special settings
11487 @cindex special settings
11492 Set the minimum number of characters that will satisfy a read until
11493 the time value has expired, when @option{-icanon} is set.
11497 Set the number of tenths of a second before reads time out if the minimum
11498 number of characters have not been read, when @option{-icanon} is set.
11500 @item ispeed @var{n}
11502 Set the input speed to @var{n}.
11504 @item ospeed @var{n}
11506 Set the output speed to @var{n}.
11510 Tell the tty kernel driver that the terminal has @var{n} rows. Non-@acronym{POSIX}.
11513 @itemx columns @var{n}
11516 Tell the kernel that the terminal has @var{n} columns. Non-@acronym{POSIX}.
11522 Print the number of rows and columns that the kernel thinks the
11523 terminal has. (Systems that don't support rows and columns in the kernel
11524 typically use the environment variables @env{LINES} and @env{COLUMNS}
11525 instead; however, GNU @command{stty} does not know anything about them.)
11526 Non-@acronym{POSIX}.
11530 Use line discipline @var{n}. Non-@acronym{POSIX}.
11534 Print the terminal speed.
11537 @cindex baud rate, setting
11538 @c FIXME: Is this still true that the baud rate can't be set
11539 @c higher than 38400?
11540 Set the input and output speeds to @var{n}. @var{n} can be one
11541 of: 0 50 75 110 134 134.5 150 200 300 600 1200 1800 2400 4800 9600
11542 19200 38400 @code{exta} @code{extb}. @code{exta} is the same as
11543 19200; @code{extb} is the same as 38400. 0 hangs up the line if
11544 @option{-clocal} is set.
11548 @node printenv invocation
11549 @section @command{printenv}: Print all or some environment variables
11552 @cindex printing all or some environment variables
11553 @cindex environment variables, printing
11555 @command{printenv} prints environment variable values. Synopsis:
11558 printenv [@var{option}] [@var{variable}]@dots{}
11561 If no @var{variable}s are specified, @command{printenv} prints the value of
11562 every environment variable. Otherwise, it prints the value of each
11563 @var{variable} that is set, and nothing for those that are not set.
11565 The only options are a lone @option{--help} or @option{--version}.
11566 @xref{Common options}.
11568 @cindex exit status of @command{printenv}
11572 0 if all variables specified were found
11573 1 if at least one specified variable was not found
11574 2 if a write error occurred
11578 @node tty invocation
11579 @section @command{tty}: Print file name of terminal on standard input
11582 @cindex print terminal file name
11583 @cindex terminal file name, printing
11585 @command{tty} prints the file name of the terminal connected to its standard
11586 input. It prints @samp{not a tty} if standard input is not a terminal.
11590 tty [@var{option}]@dots{}
11593 The program accepts the following option. Also see @ref{Common options}.
11603 Print nothing; only return an exit status.
11607 @cindex exit status of @command{tty}
11611 0 if standard input is a terminal
11612 1 if standard input is not a terminal
11613 2 if given incorrect arguments
11614 3 if a write error occurs
11618 @node User information
11619 @chapter User information
11621 @cindex user information, commands for
11622 @cindex commands for printing user information
11624 This section describes commands that print user-related information:
11625 logins, groups, and so forth.
11628 * id invocation:: Print user identity.
11629 * logname invocation:: Print current login name.
11630 * whoami invocation:: Print effective user ID.
11631 * groups invocation:: Print group names a user is in.
11632 * users invocation:: Print login names of users currently logged in.
11633 * who invocation:: Print who is currently logged in.
11637 @node id invocation
11638 @section @command{id}: Print user identity
11641 @cindex real user and group IDs, printing
11642 @cindex effective user and group IDs, printing
11643 @cindex printing real and effective user and group IDs
11645 @command{id} prints information about the given user, or the process
11646 running it if no user is specified. Synopsis:
11649 id [@var{option}]@dots{} [@var{username}]
11652 By default, it prints the real user ID, real group ID, effective user ID
11653 if different from the real user ID, effective group ID if different from
11654 the real group ID, and supplemental group IDs.
11656 Each of these numeric values is preceded by an identifying string and
11657 followed by the corresponding user or group name in parentheses.
11659 The options cause @command{id} to print only part of the above information.
11660 Also see @ref{Common options}.
11667 Print only the group ID.
11673 Print only the group ID and the supplementary groups.
11679 Print the user or group name instead of the ID number. Requires
11680 @option{-u}, @option{-g}, or @option{-G}.
11686 Print the real, instead of effective, user or group ID. Requires
11687 @option{-u}, @option{-g}, or @option{-G}.
11693 Print only the user ID.
11700 @node logname invocation
11701 @section @command{logname}: Print current login name
11704 @cindex printing user's login name
11705 @cindex login name, printing
11706 @cindex user name, printing
11709 @command{logname} prints the calling user's name, as found in a
11710 system-maintained file (often @file{/var/run/utmp} or
11711 @file{/etc/utmp}), and exits with a status of 0. If there is no entry
11712 for the calling process, @command{logname} prints
11713 an error message and exits with a status of 1.
11715 The only options are @option{--help} and @option{--version}. @xref{Common
11721 @node whoami invocation
11722 @section @command{whoami}: Print effective user ID
11725 @cindex effective user ID, printing
11726 @cindex printing the effective user ID
11728 @command{whoami} prints the user name associated with the current
11729 effective user ID. It is equivalent to the command @samp{id -un}.
11731 The only options are @option{--help} and @option{--version}. @xref{Common
11737 @node groups invocation
11738 @section @command{groups}: Print group names a user is in
11741 @cindex printing groups a user is in
11742 @cindex supplementary groups, printing
11744 @command{groups} prints the names of the primary and any supplementary
11745 groups for each given @var{username}, or the current process if no names
11746 are given. If names are given, the name of each user is printed before
11747 the list of that user's groups. Synopsis:
11750 groups [@var{username}]@dots{}
11753 The group lists are equivalent to the output of the command @samp{id -Gn}.
11755 The only options are @option{--help} and @option{--version}. @xref{Common
11761 @node users invocation
11762 @section @command{users}: Print login names of users currently logged in
11765 @cindex printing current usernames
11766 @cindex usernames, printing current
11768 @cindex login sessions, printing users with
11769 @command{users} prints on a single line a blank-separated list of user
11770 names of users currently logged in to the current host. Each user name
11771 corresponds to a login session, so if a user has more than one login
11772 session, that user's name will appear the same number of times in the
11781 With no @var{file} argument, @command{users} extracts its information from
11782 a system-maintained file (often @file{/var/run/utmp} or
11783 @file{/etc/utmp}). If a file argument is given, @command{users} uses
11784 that file instead. A common choice is @file{/var/log/wtmp}.
11786 The only options are @option{--help} and @option{--version}. @xref{Common
11792 @node who invocation
11793 @section @command{who}: Print who is currently logged in
11796 @cindex printing current user information
11797 @cindex information, about current users
11799 @command{who} prints information about users who are currently logged on.
11803 @command{who} [@var{option}] [@var{file}] [am i]
11806 @cindex terminal lines, currently used
11808 @cindex remote hostname
11809 If given no non-option arguments, @command{who} prints the following
11810 information for each user currently logged on: login name, terminal
11811 line, login time, and remote hostname or X display.
11815 If given one non-option argument, @command{who} uses that instead of
11816 a default system-maintained file (often @file{/var/run/utmp} or
11817 @file{/etc/utmp}) as the name of the file containing the record of
11818 users logged on. @file{/var/log/wtmp} is commonly given as an argument
11819 to @command{who} to look at who has previously logged on.
11823 If given two non-option arguments, @command{who} prints only the entry
11824 for the user running it (determined from its standard input), preceded
11825 by the hostname. Traditionally, the two arguments given are @samp{am
11826 i}, as in @samp{who am i}.
11829 Time stamps are listed according to the time zone rules specified by
11830 the @env{TZ} environment variable, or by the system default rules if
11831 @env{TZ} is not set. @xref{TZ Variable,, Specifying the Time Zone
11832 with @env{TZ}, libc, The GNU C Library}.
11834 The program accepts the following options. Also see @ref{Common options}.
11842 Same as @samp{-b -d --login -p -r -t -T -u}.
11848 Print the date and time of last system boot.
11854 Print information corresponding to dead processes.
11860 Print column headings.
11864 Same as @samp{who am i}.
11870 Print only the login names and the number of users logged on.
11871 Overrides all other options.
11875 Ignored; for compatibility with other versions of @command{who}.
11880 After the login time, print the number of hours and minutes that the
11881 user has been idle. @samp{.} means the user was active in the last minute.
11882 @samp{old} means the user has been idle for more than 24 hours.
11888 List only the entries that correspond to processes via which the
11889 system is waiting for a user to login. The user name is always @samp{LOGIN}.
11893 Attempt to canonicalize hostnames found in utmp through a DNS lookup. This
11894 is not the default because it can cause significant delays on systems with
11895 automatic dial-up internet access.
11901 Print a line of column headings.
11912 @opindex --writable
11913 @cindex message status
11914 @pindex write@r{, allowed}
11915 After each login name print a character indicating the user's message status:
11918 @samp{+} allowing @code{write} messages
11919 @samp{-} disallowing @code{write} messages
11920 @samp{?} cannot find terminal device
11928 @node System context
11929 @chapter System context
11931 @cindex system context
11932 @cindex context, system
11933 @cindex commands for system context
11935 This section describes commands that print or change system-wide
11939 * date invocation:: Print or set system date and time.
11940 * uname invocation:: Print system information.
11941 * hostname invocation:: Print or set system name.
11942 * hostid invocation:: Print numeric host identifier.
11946 @node date invocation
11947 @section @command{date}: Print or set system date and time
11950 @cindex time, printing or setting
11951 @cindex printing the current time
11956 date [@var{option}]@dots{} [+@var{format}]
11957 date [-u|--utc|--universal] @c this avoids a newline in the output
11958 [ MMDDhhmm[[CC]YY][.ss] ]
11962 Invoking @command{date} with no @var{format} argument is equivalent to invoking
11963 it with a default format that depends on the @env{LC_TIME} locale category.
11964 In the default C locale, this format is @samp{'+%a %b %e %H:%M:%S %Z %Y'},
11965 so the output looks like @samp{Thu Mar @ 3 13:47:51 PST 2005}.
11968 Normally, @command{date} uses the time zone rules indicated by the
11969 @env{TZ} environment variable, or the system default rules if @env{TZ}
11970 is not set. @xref{TZ Variable,, Specifying the Time Zone with
11971 @env{TZ}, libc, The GNU C Library}.
11973 @findex strftime @r{and @command{date}}
11974 @cindex time formats
11975 @cindex formatting times
11976 If given an argument that starts with a @samp{+}, @command{date} prints the
11977 current date and time (or the date and time specified by the
11978 @option{--date} option, see below) in the format defined by that argument,
11979 which is similar to that of the @code{strftime} function. Except for
11980 conversion specifiers, which start with @samp{%}, characters in the
11981 format string are printed unchanged. The conversion specifiers are
11987 * Time conversion specifiers:: %[HIklMNpPrRsSTXzZ]
11988 * Date conversion specifiers:: %[aAbBcCdDeFgGhjmuUVwWxyY]
11989 * Literal conversion specifiers:: %[%nt]
11990 * Padding and other flags:: Pad with zeros, spaces, etc.
11991 * Setting the time:: Changing the system clock.
11992 * Options for date:: Instead of the current time.
11993 * Examples of date:: Examples.
11996 @node Time conversion specifiers
11997 @subsection Time conversion specifiers
11999 @cindex time conversion specifiers
12000 @cindex conversion specifiers, time
12002 @command{date} conversion specifiers related to times.
12006 hour (@samp{00}@dots{}@samp{23})
12008 hour (@samp{01}@dots{}@samp{12})
12010 hour (@samp{ 0}@dots{}@samp{23}).
12011 This is a @acronym{GNU} extension.
12013 hour (@samp{ 1}@dots{}@samp{12}).
12014 This is a @acronym{GNU} extension.
12016 minute (@samp{00}@dots{}@samp{59})
12018 nanoseconds (@samp{000000000}@dots{}@samp{999999999}).
12019 This is a @acronym{GNU} extension.
12021 locale's equivalent of either @samp{AM} or @samp{PM};
12022 blank in many locales.
12023 Noon is treated as @samp{PM} and midnight as @samp{AM}.
12025 like @samp{%p}, except lower case.
12026 This is a @acronym{GNU} extension.
12028 locale's 12-hour clock time (e.g., @samp{11:11:04 PM})
12030 24-hour hour and minute. Same as @samp{%H:%M}.
12031 This is a @acronym{GNU} extension.
12033 @cindex epoch, seconds since
12034 @cindex seconds since the epoch
12035 @cindex beginning of time
12036 seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC.
12037 Leap seconds are not counted unless leap second support is available.
12038 @xref{%s-examples}, for examples.
12039 This is a @acronym{GNU} extension.
12041 second (@samp{00}@dots{}@samp{60}).
12042 This may be @samp{60} if leap seconds are supported.
12044 24-hour hour, minute, and second. Same as @samp{%H:%M:%S}.
12046 locale's time representation (e.g., @samp{23:13:48})
12048 @w{@acronym{RFC} 2822/@acronym{ISO} 8601} style numeric time zone
12049 (e.g., @samp{-0600} or @samp{+0530}), or nothing if no
12050 time zone is determinable. This value reflects the numeric time zone
12051 appropriate for the current time, using the time zone rules specified
12052 by the @env{TZ} environment variable.
12053 The time (and optionally, the time zone rules) can be overridden
12054 by the @option{--date} option.
12055 This is a @acronym{GNU} extension.
12057 @w{@acronym{RFC} 3339/@acronym{ISO} 8601} style numeric time zone with
12058 @samp{:} (e.g., @samp{-06:00} or @samp{+05:30}), or nothing if no time
12059 zone is determinable.
12060 This is a @acronym{GNU} extension.
12062 Numeric time zone to the nearest second with @samp{:} (e.g.,
12063 @samp{-06:00:00} or @samp{+05:30:00}), or nothing if no time zone is
12065 This is a @acronym{GNU} extension.
12067 Numeric time zone with @samp{:} using the minimum necessary precision
12068 (e.g., @samp{-06}, @samp{+05:30}, or @samp{-04:56:02}), or nothing if
12069 no time zone is determinable.
12070 This is a @acronym{GNU} extension.
12072 alphabetic time zone abbreviation (e.g., @samp{EDT}), or nothing if no
12073 time zone is determinable. See @samp{%z} for how it is determined.
12077 @node Date conversion specifiers
12078 @subsection Date conversion specifiers
12080 @cindex date conversion specifiers
12081 @cindex conversion specifiers, date
12083 @command{date} conversion specifiers related to dates.
12087 locale's abbreviated weekday name (e.g., @samp{Sun})
12089 locale's full weekday name, variable length (e.g., @samp{Sunday})
12091 locale's abbreviated month name (e.g., @samp{Jan})
12093 locale's full month name, variable length (e.g., @samp{January})
12095 locale's date and time (e.g., @samp{Thu Mar @ 3 23:05:25 2005})
12097 century. This is like @samp{%Y}, except the last two digits are omitted.
12098 For example, it is @samp{20} if @samp{%Y} is @samp{2000},
12099 and is @samp{-0} if @samp{%Y} is @samp{-001}.
12100 It is normally at least two characters, but it may be more.
12102 day of month (e.g., @samp{01})
12104 date; same as @samp{%m/%d/%y}
12106 day of month, space padded; same as @samp{%_d}
12108 full date in @acronym{ISO} 8601 format; same as @samp{%Y-%m-%d}.
12109 This is a good choice for a date format, as it is standard and
12110 is easy to sort in the usual case where years are in the range
12112 This is a @acronym{GNU} extension.
12114 year corresponding to the @acronym{ISO} week number, but without the century
12115 (range @samp{00} through @samp{99}). This has the same format and value
12116 as @samp{%y}, except that if the @acronym{ISO} week number (see
12118 to the previous or next year, that year is used instead.
12119 This is a @acronym{GNU} extension.
12121 year corresponding to the @acronym{ISO} week number. This has the
12122 same format and value as @samp{%Y}, except that if the @acronym{ISO}
12124 @samp{%V}) belongs to the previous or next year, that year is used
12126 It is normally useful only if @samp{%V} is also used;
12127 for example, the format @samp{%G-%m-%d} is probably a mistake,
12128 since it combines the ISO week number year with the conventional month and day.
12129 This is a @acronym{GNU} extension.
12133 day of year (@samp{001}@dots{}@samp{366})
12135 month (@samp{01}@dots{}@samp{12})
12137 day of week (@samp{1}@dots{}@samp{7}) with @samp{1} corresponding to Monday
12139 week number of year, with Sunday as the first day of the week
12140 (@samp{00}@dots{}@samp{53}).
12141 Days in a new year preceding the first Sunday are in week zero.
12143 @acronym{ISO} week number, that is, the
12144 week number of year, with Monday as the first day of the week
12145 (@samp{01}@dots{}@samp{53}).
12146 If the week containing January 1 has four or more days in
12147 the new year, then it is considered week 1; otherwise, it is week 53 of
12148 the previous year, and the next week is week 1. (See the @acronym{ISO} 8601
12151 day of week (@samp{0}@dots{}@samp{6}) with 0 corresponding to Sunday
12153 week number of year, with Monday as first day of week
12154 (@samp{00}@dots{}@samp{53}).
12155 Days in a new year preceding the first Monday are in week zero.
12157 locale's date representation (e.g., @samp{12/31/99})
12159 last two digits of year (@samp{00}@dots{}@samp{99})
12161 year. This is normally at least four characters, but it may be more.
12162 Year @samp{0000} precedes year @samp{0001}, and year @samp{-001}
12163 precedes year @samp{0000}.
12167 @node Literal conversion specifiers
12168 @subsection Literal conversion specifiers
12170 @cindex literal conversion specifiers
12171 @cindex conversion specifiers, literal
12173 @command{date} conversion specifiers that produce literal strings.
12185 @node Padding and other flags
12186 @subsection Padding and other flags
12188 @cindex numeric field padding
12189 @cindex padding of numeric fields
12190 @cindex fields, padding numeric
12192 Unless otherwise specified, @command{date} normally pads numeric fields
12193 with zeros, so that, for
12194 example, numeric months are always output as two digits.
12195 Seconds since the epoch are not padded, though,
12196 since there is no natural width for them.
12198 As a @acronym{GNU} extension, @command{date} recognizes any of the
12199 following optional flags after the @samp{%}:
12203 (hyphen) Do not pad the field; useful if the output is intended for
12206 (underscore) Pad with spaces; useful if you need a fixed
12207 number of characters in the output, but zeros are too distracting.
12209 (zero) Pad with zeros even if the conversion specifier
12210 would normally pad with spaces.
12212 Use upper case characters if possible.
12214 Use opposite case characters if possible.
12215 A field that is normally upper case becomes lower case, and vice versa.
12219 Here are some examples of padding:
12222 date +%d/%m -d "Feb 1"
12224 date +%-d/%-m -d "Feb 1"
12226 date +%_d/%_m -d "Feb 1"
12230 As a @acronym{GNU} extension, you can specify the field width
12231 (after any flag, if present) as a decimal number. If the natural size of the
12232 output is of the field has less than the specified number of characters,
12233 the result is written right adjusted and padded to the given
12234 size. For example, @samp{%9B} prints the right adjusted month name in
12235 a field of width 9.
12237 An optional modifier can follow the optional flag and width
12238 specification. The modifiers are:
12242 Use the locale's alternate representation for date and time. This
12243 modifier applies to the @samp{%c}, @samp{%C}, @samp{%x}, @samp{%X},
12244 @samp{%y} and @samp{%Y} conversion specifiers. In a Japanese locale, for
12245 example, @samp{%Ex} might yield a date format based on the Japanese
12249 Use the locale's alternate numeric symbols for numbers. This modifier
12250 applies only to numeric conversion specifiers.
12253 If the format supports the modifier but no alternate representation
12254 is available, it is ignored.
12257 @node Setting the time
12258 @subsection Setting the time
12260 @cindex setting the time
12261 @cindex time setting
12262 @cindex appropriate privileges
12264 If given an argument that does not start with @samp{+}, @command{date} sets
12265 the system clock to the date and time specified by that argument (as
12266 described below). You must have appropriate privileges to set the
12267 system clock. The @option{--date} and @option{--set} options may not be
12268 used with such an argument. The @option{--universal} option may be used
12269 with such an argument to indicate that the specified date and time are
12270 relative to Coordinated Universal Time rather than to the local time
12273 The argument must consist entirely of digits, which have the following
12286 first two digits of year (optional)
12288 last two digits of year (optional)
12293 The @option{--set} option also sets the system clock; see the next section.
12296 @node Options for date
12297 @subsection Options for @command{date}
12299 @cindex @command{date} options
12300 @cindex options for @command{date}
12302 The program accepts the following options. Also see @ref{Common options}.
12306 @item -d @var{datestr}
12307 @itemx --date=@var{datestr}
12310 @cindex parsing date strings
12311 @cindex date strings, parsing
12312 @cindex arbitrary date strings, parsing
12315 @opindex next @var{day}
12316 @opindex last @var{day}
12317 Display the date and time specified in @var{datestr} instead of the
12318 current date and time. @var{datestr} can be in almost any common
12319 format. It can contain month names, time zones, @samp{am} and @samp{pm},
12320 @samp{yesterday}, etc. For example, @option{--date="2004-02-27
12321 14:19:13.489392193 +0530"} specifies the instant of time that is
12322 489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
12323 time zone that is 5 hours and 30 minutes east of @acronym{UTC}.
12324 @xref{Date input formats}.
12326 @item -f @var{datefile}
12327 @itemx --file=@var{datefile}
12330 Parse each line in @var{datefile} as with @option{-d} and display the
12331 resulting date and time. If @var{datefile} is @samp{-}, use standard
12332 input. This is useful when you have many dates to process, because the
12333 system overhead of starting up the @command{date} executable many times can
12336 @item -r @var{file}
12337 @itemx --reference=@var{file}
12339 @opindex --reference
12340 Display the date and time of the last modification of @var{file},
12341 instead of the current date and time.
12348 @opindex --rfc-2822
12349 Display the date and time using the format @samp{%a, %d %b %Y %H:%M:%S
12350 %z}, evaluated in the C locale so abbreviations are always in English.
12354 Fri, 09 Sep 2005 13:51:39 -0700
12357 This format conforms to
12358 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc2822.txt, Internet
12359 @acronym{RFCs} 2822} and
12360 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc822.txt, 822}, the
12361 current and previous standards for Internet email.
12363 @item --rfc-3339=@var{timespec}
12364 @opindex --rfc-3339=@var{timespec}
12365 Display the date using a format specified by
12366 @uref{ftp://ftp.rfc-editor.org/in-notes/rfc3339.txt, Internet
12367 @acronym{RFC} 3339}. This is a subset of the @acronym{ISO} 8601
12368 format, except that it also permits applications to use a space rather
12369 than a @samp{T} to separate dates from times. Unlike the other
12370 standard formats, @acronym{RFC} 3339 format is always suitable as
12371 input for the @option{--date} (@option{-d}) and @option{--file}
12372 (@option{-f}) options, regardless of the current locale.
12374 The argument @var{timespec} specifies how much of the time to include.
12375 It can be one of the following:
12379 Print just the full-date, e.g., @samp{2005-09-14}.
12380 This is equivalent to the format @samp{%Y-%m-%d}.
12383 Print the full-date and full-time separated by a space, e.g.,
12384 @samp{2005-09-14 00:56:06+05:30}. The output ends with a numeric
12385 time-offset; here the @samp{+05:30} means that local time is five
12386 hours and thirty minutes east of @acronym{UTC}. This is equivalent to
12387 the format @samp{%Y-%m-%d %H:%M:%S%:z}.
12390 Like @samp{seconds}, but also print nanoseconds, e.g.,
12391 @samp{2005-09-14 00:56:06.998458565+05:30}.
12392 This is equivalent to the format @samp{%Y-%m-%d %H:%M:%S.%N%:z}.
12396 @item -s @var{datestr}
12397 @itemx --set=@var{datestr}
12400 Set the date and time to @var{datestr}. See @option{-d} above.
12407 @opindex --universal
12408 @cindex Coordinated Universal Time
12410 @cindex Greenwich Mean Time
12413 Use Coordinated Universal Time (@acronym{UTC}) by operating as if the
12414 @env{TZ} environment variable were set to the string @samp{UTC0}.
12416 Universal Time is often called ``Greenwich Mean Time'' (@sc{gmt}) for
12417 historical reasons.
12421 @node Examples of date
12422 @subsection Examples of @command{date}
12424 @cindex examples of @command{date}
12426 Here are a few examples. Also see the documentation for the @option{-d}
12427 option in the previous section.
12432 To print the date of the day before yesterday:
12435 date --date='2 days ago'
12439 To print the date of the day three months and one day hence:
12442 date --date='3 months 1 day'
12446 To print the day of year of Christmas in the current year:
12449 date --date='25 Dec' +%j
12453 To print the current full month name and the day of the month:
12459 But this may not be what you want because for the first nine days of
12460 the month, the @samp{%d} expands to a zero-padded two-digit field,
12461 for example @samp{date -d 1may '+%B %d'} will print @samp{May 01}.
12464 To print a date without the leading zero for one-digit days
12465 of the month, you can use the (@acronym{GNU} extension)
12466 @samp{-} flag to suppress
12467 the padding altogether:
12470 date -d 1may '+%B %-d
12474 To print the current date and time in the format required by many
12475 non-@acronym{GNU} versions of @command{date} when setting the system clock:
12478 date +%m%d%H%M%Y.%S
12482 To set the system clock forward by two minutes:
12485 date --set='+2 minutes'
12489 To print the date in @acronym{RFC} 2822 format,
12490 use @samp{date --rfc-2822}. Here is some example output:
12493 Fri, 09 Sep 2005 13:51:39 -0700
12496 @anchor{%s-examples}
12498 To convert a date string to the number of seconds since the epoch
12499 (which is 1970-01-01 00:00:00 UTC), use the @option{--date} option with
12500 the @samp{%s} format. That can be useful in sorting and/or graphing
12501 and/or comparing data by date. The following command outputs the
12502 number of the seconds since the epoch for the time two minutes after the
12506 date --date='1970-01-01 00:02:00 +0000' +%s
12510 If you do not specify time zone information in the date string,
12511 @command{date} uses your computer's idea of the time zone when
12512 interpreting the string. For example, if your computer's time zone is
12513 that of Cambridge, Massachusetts, which was then 5 hours (i.e., 18,000
12514 seconds) behind UTC:
12517 # local time zone used
12518 date --date='1970-01-01 00:02:00' +%s
12523 If you're sorting or graphing dated data, your raw date values may be
12524 represented as seconds since the epoch. But few people can look at
12525 the date @samp{946684800} and casually note ``Oh, that's the first second
12526 of the year 2000 in Greenwich, England.''
12529 date --date='2000-01-01 UTC' +%s
12533 An alternative is to use the @option{--utc} (@option{-u}) option.
12534 Then you may omit @samp{UTC} from the date string. Although this
12535 produces the same result for @samp{%s} and many other format sequences,
12536 with a time zone offset different from zero, it would give a different
12537 result for zone-dependent formats like @samp{%z}.
12540 date -u --date=2000-01-01 +%s
12544 To convert such an unwieldy number of seconds back to
12545 a more readable form, use a command like this:
12548 # local time zone used
12549 date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
12550 1999-12-31 19:00:00 -0500
12553 Or if you do not mind depending on the @samp{@@} feature present since
12554 coreutils 5.3.0, you could shorten this to:
12557 date -d @@946684800 +"%F %T %z"
12558 1999-12-31 19:00:00 -0500
12561 Often it is better to output UTC-relative date and time:
12564 date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
12565 2000-01-01 00:00:00 +0000
12571 @node uname invocation
12572 @section @command{uname}: Print system information
12575 @cindex print system information
12576 @cindex system information, printing
12578 @command{uname} prints information about the machine and operating system
12579 it is run on. If no options are given, @command{uname} acts as if the
12580 @option{-s} option were given. Synopsis:
12583 uname [@var{option}]@dots{}
12586 If multiple options or @option{-a} are given, the selected information is
12587 printed in this order:
12590 @var{kernel-name} @var{nodename} @var{kernel-release} @var{kernel-version}
12591 @var{machine} @var{processor} @var{hardware-platform} @var{operating-system}
12594 The information may contain internal spaces, so such output cannot be
12595 parsed reliably. In the following example, @var{release} is
12596 @samp{2.2.18ss.e820-bda652a #4 SMP Tue Jun 5 11:24:08 PDT 2001}:
12600 @result{} Linux dum 2.2.18 #4 SMP Tue Jun 5 11:24:08 PDT 2001 i686 unknown unknown GNU/Linux
12604 The program accepts the following options. Also see @ref{Common options}.
12612 Print all of the below information, except omit the processor type
12613 and the hardware platform name if they are unknown.
12616 @itemx --hardware-platform
12618 @opindex --hardware-platform
12619 @cindex implementation, hardware
12620 @cindex hardware platform
12621 @cindex platform, hardware
12622 Print the hardware platform name
12623 (sometimes called the hardware implementation).
12624 Print @samp{unknown} if the kernel does not make this information
12625 easily available, as is the case with Linux kernels.
12631 @cindex machine type
12632 @cindex hardware class
12633 @cindex hardware type
12634 Print the machine hardware name (sometimes called the hardware class
12640 @opindex --nodename
12643 @cindex network node name
12644 Print the network node hostname.
12649 @opindex --processor
12650 @cindex host processor type
12651 Print the processor type (sometimes called the instruction set
12652 architecture or ISA).
12653 Print @samp{unknown} if the kernel does not make this information
12654 easily available, as is the case with Linux kernels.
12657 @itemx --operating-system
12659 @opindex --operating-system
12660 @cindex operating system name
12661 Print the name of the operating system.
12664 @itemx --kernel-release
12666 @opindex --kernel-release
12667 @cindex kernel release
12668 @cindex release of kernel
12669 Print the kernel release.
12672 @itemx --kernel-name
12674 @opindex --kernel-name
12675 @cindex kernel name
12676 @cindex name of kernel
12677 Print the kernel name.
12678 @acronym{POSIX} 1003.1-2001 (@pxref{Standards conformance}) calls this
12679 ``the implementation of the operating system'', because the
12680 @acronym{POSIX} specification itself has no notion of ``kernel''.
12681 The kernel name might be the same as the operating system name printed
12682 by the @option{-o} or @option{--operating-system} option, but it might
12683 differ. Some operating systems (e.g., FreeBSD, HP-UX) have the same
12684 name as their underlying kernels; others (e.g., GNU/Linux, Solaris)
12688 @itemx --kernel-version
12690 @opindex --kernel-version
12691 @cindex kernel version
12692 @cindex version of kernel
12693 Print the kernel version.
12700 @node hostname invocation
12701 @section @command{hostname}: Print or set system name
12704 @cindex setting the hostname
12705 @cindex printing the hostname
12706 @cindex system name, printing
12707 @cindex appropriate privileges
12709 With no arguments, @command{hostname} prints the name of the current host
12710 system. With one argument, it sets the current host name to the
12711 specified string. You must have appropriate privileges to set the host
12715 hostname [@var{name}]
12718 The only options are @option{--help} and @option{--version}. @xref{Common
12724 @node hostid invocation
12725 @section @command{hostid}: Print numeric host identifier.
12728 @cindex printing the host identifier
12730 @command{hostid} prints the numeric identifier of the current host
12731 in hexadecimal. This command accepts no arguments.
12732 The only options are @option{--help} and @option{--version}.
12733 @xref{Common options}.
12735 For example, here's what it prints on one system I use:
12742 On that system, the 32-bit quantity happens to be closely
12743 related to the system's Internet address, but that isn't always
12749 @node Modified command invocation
12750 @chapter Modified command invocation
12752 @cindex modified command invocation
12753 @cindex invocation of commands, modified
12754 @cindex commands for invoking other commands
12756 This section describes commands that run other commands in some context
12757 different than the current one: a modified environment, as a different
12761 * chroot invocation:: Modify the root directory.
12762 * env invocation:: Modify environment variables.
12763 * nice invocation:: Modify niceness.
12764 * nohup invocation:: Immunize to hangups.
12765 * su invocation:: Modify user and group ID.
12769 @node chroot invocation
12770 @section @command{chroot}: Run a command with a different root directory
12773 @cindex running a program in a specified root directory
12774 @cindex root directory, running a program in a specified
12776 @command{chroot} runs a command with a specified root directory.
12777 On many systems, only the super-user can do this.
12781 chroot @var{newroot} [@var{command} [@var{args}]@dots{}]
12782 chroot @var{option}
12785 Ordinarily, file names are looked up starting at the root of the
12786 directory structure, i.e., @file{/}. @command{chroot} changes the root to
12787 the directory @var{newroot} (which must exist) and then runs
12788 @var{command} with optional @var{args}. If @var{command} is not
12789 specified, the default is the value of the @env{SHELL} environment
12790 variable or @command{/bin/sh} if not set, invoked with the @option{-i} option.
12791 @var{command} must not be a special built-in utility
12792 (@pxref{Special built-in utilities}).
12794 The only options are @option{--help} and @option{--version}. @xref{Common
12795 options}. Options must precede operands.
12797 Here are a few tips to help avoid common problems in using chroot.
12798 To start with a simple example, make @var{command} refer to a statically
12799 linked binary. If you were to use a dynamically linked executable, then
12800 you'd have to arrange to have the shared libraries in the right place under
12801 your new root directory.
12803 For example, if you create a statically linked @command{ls} executable,
12804 and put it in @file{/tmp/empty}, you can run this command as root:
12807 $ chroot /tmp/empty /ls -Rl /
12810 Then you'll see output like this:
12815 -rwxr-xr-x 1 0 0 1041745 Aug 16 11:17 ls
12818 If you want to use a dynamically linked executable, say @command{bash},
12819 then first run @samp{ldd bash} to see what shared objects it needs.
12820 Then, in addition to copying the actual binary, also copy the listed
12821 files to the required positions under your intended new root directory.
12822 Finally, if the executable requires any other files (e.g., data, state,
12823 device files), copy them into place, too.
12825 @cindex exit status of @command{chroot}
12829 1 if @command{chroot} itself fails
12830 126 if @var{command} is found but cannot be invoked
12831 127 if @var{command} cannot be found
12832 the exit status of @var{command} otherwise
12836 @node env invocation
12837 @section @command{env}: Run a command in a modified environment
12840 @cindex environment, running a program in a modified
12841 @cindex modified environment, running a program in a
12842 @cindex running a program in a modified environment
12844 @command{env} runs a command with a modified environment. Synopses:
12847 env [@var{option}]@dots{} [@var{name}=@var{value}]@dots{} @c
12848 [@var{command} [@var{args}]@dots{}]
12852 Operands of the form @samp{@var{variable}=@var{value}} set
12853 the environment variable @var{variable} to value @var{value}.
12854 @var{value} may be empty (@samp{@var{variable}=}). Setting a variable
12855 to an empty value is different from unsetting it.
12856 These operands are evaluated left-to-right, so if two operands
12857 mention the same variable the earlier is ignored.
12859 Environment variable names can be empty, and can contain any
12860 characters other than @samp{=} and the null character (@acronym{ASCII}
12861 @sc{nul}). However, it is wise to limit yourself to names that
12862 consist solely of underscores, digits, and @acronym{ASCII} letters,
12863 and that begin with a non-digit, as applications like the shell do not
12864 work well with other names.
12867 The first operand that does not contain the character @samp{=}
12868 specifies the program to invoke; it is
12869 searched for according to the @env{PATH} environment variable. Any
12870 remaining arguments are passed as arguments to that program.
12871 The program should not be a special built-in utility
12872 (@pxref{Special built-in utilities}).
12874 @cindex environment, printing
12876 If no command name is specified following the environment
12877 specifications, the resulting environment is printed. This is like
12878 specifying the @command{printenv} program.
12880 The program accepts the following options. Also see @ref{Common options}.
12881 Options must precede operands.
12885 @item -u @var{name}
12886 @itemx --unset=@var{name}
12889 Remove variable @var{name} from the environment, if it was in the
12894 @itemx --ignore-environment
12897 @opindex --ignore-environment
12898 Start with an empty environment, ignoring the inherited environment.
12902 @cindex exit status of @command{env}
12906 0 if no @var{command} is specified and the environment is output
12907 1 if @command{env} itself fails
12908 126 if @var{command} is found but cannot be invoked
12909 127 if @var{command} cannot be found
12910 the exit status of @var{command} otherwise
12914 @node nice invocation
12915 @section @command{nice}: Run a command with modified niceness
12919 @cindex scheduling, affecting
12920 @cindex appropriate privileges
12922 @command{nice} prints or modifies a process's @dfn{niceness},
12923 a parameter that affects whether the process is scheduled favorably.
12927 nice [@var{option}]@dots{} [@var{command} [@var{arg}]@dots{}]
12930 If no arguments are given, @command{nice} prints the current niceness.
12931 Otherwise, @command{nice} runs the given @var{command} with its
12932 niceness adjusted. By default, its niceness is incremented by 10.
12934 Nicenesses range at least from @minus{}20 (resulting in the most
12935 favorable scheduling) through 19 (the least favorable). Some systems
12936 may have a wider range of nicenesses; conversely, other systems may
12937 enforce more restrictive limits. An attempt to set the niceness
12938 outside the supported range is treated as an attempt to use the
12939 minimum or maximum supported value.
12941 A niceness should not be confused with a scheduling priority, which
12942 lets applications determine the order in which threads are scheduled
12943 to run. Unlike a priority, a niceness is merely advice to the
12944 scheduler, which the scheduler is free to ignore. Also, as a point of
12945 terminology, @acronym{POSIX} defines the behavior of @command{nice} in
12946 terms of a @dfn{nice value}, which is the nonnegative difference
12947 between a niceness and the minimum niceness. Though @command{nice}
12948 conforms to @acronym{POSIX}, its documentation and diagnostics use the
12949 term ``niceness'' for compatibility with historical practice.
12951 @var{command} must not be a special built-in utility (@pxref{Special
12952 built-in utilities}).
12954 @cindex conflicts with shell built-ins
12955 @cindex built-in shell commands, conflicts with
12956 Because many shells have a built-in @command{nice} command, using an
12957 unadorned @command{nice} in a script or interactively may get you
12958 different functionality than that described here.
12960 The program accepts the following option. Also see @ref{Common options}.
12961 Options must precede operands.
12964 @item -n @var{adjustment}
12965 @itemx --adjustment=@var{adjustment}
12967 @opindex --adjustment
12968 Add @var{adjustment} instead of 10 to the command's niceness. If
12969 @var{adjustment} is negative and you lack appropriate privileges,
12970 @command{nice} issues a warning but otherwise acts as if you specified
12973 For compatibility @command{nice} also supports an obsolete
12974 option syntax @option{-@var{adjustment}}. New scripts should use
12975 @option{-n @var{adjustment}} instead.
12979 @cindex exit status of @command{nice}
12983 0 if no @var{command} is specified and the niceness is output
12984 1 if @command{nice} itself fails
12985 126 if @var{command} is found but cannot be invoked
12986 127 if @var{command} cannot be found
12987 the exit status of @var{command} otherwise
12990 It is sometimes useful to run a non-interactive program with reduced niceness.
12993 $ nice factor 4611686018427387903
12996 Since @command{nice} prints the current niceness,
12997 you can invoke it through itself to demonstrate how it works.
12999 The default behavior is to increase the niceness by @samp{10}:
13010 The @var{adjustment} is relative to the current niceness. In the
13011 next example, the first @command{nice} invocation runs the second one
13012 with niceness 10, and it in turn runs the final one with a niceness
13016 $ nice nice -n 3 nice
13020 Specifying a niceness larger than the supported range
13021 is the same as specifying the maximum supported value:
13024 $ nice -n 10000000000 nice
13028 Only a privileged user may run a process with lower niceness:
13032 nice: cannot set niceness: Permission denied
13034 $ sudo nice -n -1 nice
13039 @node nohup invocation
13040 @section @command{nohup}: Run a command immune to hangups
13043 @cindex hangups, immunity to
13044 @cindex immunity to hangups
13045 @cindex logging out and continuing to run
13048 @command{nohup} runs the given @var{command} with hangup signals ignored,
13049 so that the command can continue running in the background after you log
13053 nohup @var{command} [@var{arg}]@dots{}
13056 If standard input is a terminal, it is redirected from
13057 @file{/dev/null} so that terminal sessions do not mistakenly consider
13058 the terminal to be used by the command. This is a @acronym{GNU}
13059 extension; programs intended to be portable to non-@acronym{GNU} hosts
13060 should use @samp{nohup @var{command} [@var{arg}]@dots{} </dev/null}
13064 If standard output is a terminal, the command's standard output is appended
13065 to the file @file{nohup.out}; if that cannot be written to, it is appended
13066 to the file @file{$HOME/nohup.out}; and if that cannot be written to, the
13067 command is not run.
13068 Any @file{nohup.out} or @file{$HOME/nohup.out} file created by
13069 @command{nohup} is made readable and writable only to the user,
13070 regardless of the current umask settings.
13072 If standard error is a terminal, it is normally redirected to the same file
13073 descriptor as the (possibly-redirected) standard output.
13074 However, if standard output is closed, standard error terminal output
13075 is instead appended to the file @file{nohup.out} or
13076 @file{$HOME/nohup.out} as above.
13078 @command{nohup} does not automatically put the command it runs in the
13079 background; you must do that explicitly, by ending the command line
13080 with an @samp{&}. Also, @command{nohup} does not alter the
13081 niceness of @var{command}; use @command{nice} for that,
13082 e.g., @samp{nohup nice @var{command}}.
13084 @var{command} must not be a special built-in utility (@pxref{Special
13085 built-in utilities}).
13087 The only options are @option{--help} and @option{--version}. @xref{Common
13088 options}. Options must precede operands.
13090 @cindex exit status of @command{nohup}
13094 126 if @var{command} is found but cannot be invoked
13095 127 if @command{nohup} itself fails or if @var{command} cannot be found
13096 the exit status of @var{command} otherwise
13100 @node su invocation
13101 @section @command{su}: Run a command with substitute user and group ID
13104 @cindex substitute user and group IDs
13105 @cindex user ID, switching
13106 @cindex super-user, becoming
13107 @cindex root, becoming
13109 @command{su} allows one user to temporarily become another user. It runs a
13110 command (often an interactive shell) with the real and effective user
13111 ID, group ID, and supplemental groups of a given @var{user}. Synopsis:
13114 su [@var{option}]@dots{} [@var{user} [@var{arg}]@dots{}]
13117 @cindex passwd entry, and @command{su} shell
13119 @flindex /etc/passwd
13120 If no @var{user} is given, the default is @code{root}, the super-user.
13121 The shell to use is taken from @var{user}'s @code{passwd} entry, or
13122 @file{/bin/sh} if none is specified there. If @var{user} has a
13123 password, @command{su} prompts for the password unless run by a user with
13124 effective user ID of zero (the super-user).
13130 @cindex login shell
13131 By default, @command{su} does not change the current directory.
13132 It sets the environment variables @env{HOME} and @env{SHELL}
13133 from the password entry for @var{user}, and if @var{user} is not
13134 the super-user, sets @env{USER} and @env{LOGNAME} to @var{user}.
13135 By default, the shell is not a login shell.
13137 Any additional @var{arg}s are passed as additional arguments to the
13140 @cindex @option{-su}
13141 GNU @command{su} does not treat @file{/bin/sh} or any other shells specially
13142 (e.g., by setting @code{argv[0]} to @option{-su}, passing @option{-c} only
13143 to certain shells, etc.).
13146 @command{su} can optionally be compiled to use @code{syslog} to report
13147 failed, and optionally successful, @command{su} attempts. (If the system
13148 supports @code{syslog}.) However, GNU @command{su} does not check if the
13149 user is a member of the @code{wheel} group; see below.
13151 The program accepts the following options. Also see @ref{Common options}.
13154 @item -c @var{command}
13155 @itemx --command=@var{command}
13158 Pass @var{command}, a single command line to run, to the shell with
13159 a @option{-c} option instead of starting an interactive shell.
13166 @cindex file name pattern expansion, disabled
13167 @cindex globbing, disabled
13168 Pass the @option{-f} option to the shell. This probably only makes sense
13169 if the shell run is @command{csh} or @command{tcsh}, for which the @option{-f}
13170 option prevents reading the startup file (@file{.cshrc}). With
13171 Bourne-like shells, the @option{-f} option disables file name pattern
13172 expansion (globbing), which is not likely to be useful.
13180 @c other variables already indexed above
13183 @cindex login shell, creating
13184 Make the shell a login shell. This means the following. Unset all
13185 environment variables except @env{TERM}, @env{HOME}, and @env{SHELL}
13186 (which are set as described above), and @env{USER} and @env{LOGNAME}
13187 (which are set, even for the super-user, as described above), and set
13188 @env{PATH} to a compiled-in default value. Change to @var{user}'s home
13189 directory. Prepend @samp{-} to the shell's name, intended to make it
13190 read its login startup file(s).
13194 @itemx --preserve-environment
13197 @opindex --preserve-environment
13198 @cindex environment, preserving
13199 @flindex /etc/shells
13200 @cindex restricted shell
13201 Do not change the environment variables @env{HOME}, @env{USER},
13202 @env{LOGNAME}, or @env{SHELL}. Run the shell given in the environment
13203 variable @env{SHELL} instead of the shell from @var{user}'s passwd
13204 entry, unless the user running @command{su} is not the super-user and
13205 @var{user}'s shell is restricted. A @dfn{restricted shell} is one that
13206 is not listed in the file @file{/etc/shells}, or in a compiled-in list
13207 if that file does not exist. Parts of what this option does can be
13208 overridden by @option{--login} and @option{--shell}.
13210 @item -s @var{shell}
13211 @itemx --shell=@var{shell}
13214 Run @var{shell} instead of the shell from @var{user}'s passwd entry,
13215 unless the user running @command{su} is not the super-user and @var{user}'s
13216 shell is restricted (see @option{-m} just above).
13220 @cindex exit status of @command{su}
13224 1 if @command{su} itself fails
13225 126 if subshell is found but cannot be invoked
13226 127 if subshell cannot be found
13227 the exit status of the subshell otherwise
13230 @cindex wheel group, not supported
13231 @cindex group wheel, not supported
13233 @subsection Why GNU @command{su} does not support the @samp{wheel} group
13235 (This section is by Richard Stallman.)
13239 Sometimes a few of the users try to hold total power over all the
13240 rest. For example, in 1984, a few users at the MIT AI lab decided to
13241 seize power by changing the operator password on the Twenex system and
13242 keeping it secret from everyone else. (I was able to thwart this coup
13243 and give power back to the users by patching the kernel, but I
13244 wouldn't know how to do that in Unix.)
13246 However, occasionally the rulers do tell someone. Under the usual
13247 @command{su} mechanism, once someone learns the root password who
13248 sympathizes with the ordinary users, he or she can tell the rest. The
13249 ``wheel group'' feature would make this impossible, and thus cement the
13250 power of the rulers.
13252 I'm on the side of the masses, not that of the rulers. If you are
13253 used to supporting the bosses and sysadmins in whatever they do, you
13254 might find this idea strange at first.
13257 @node Process control
13258 @chapter Process control
13260 @cindex processes, commands for controlling
13261 @cindex commands for controlling processes
13264 * kill invocation:: Sending a signal to processes.
13268 @node kill invocation
13269 @section @command{kill}: Send a signal to processes
13272 @cindex send a signal to processes
13274 The @command{kill} command sends a signal to processes, causing them
13275 to terminate or otherwise act upon receiving the signal in some way.
13276 Alternatively, it lists information about signals. Synopses:
13279 kill [-s @var{signal} | --signal @var{signal} | -@var{signal}] @var{pid}@dots{}
13280 kill [-l | --list | -t | --table] [@var{signal}]@dots{}
13283 The first form of the @command{kill} command sends a signal to all
13284 @var{pid} arguments. The default signal to send if none is specified
13285 is @samp{TERM}. The special signal number @samp{0} does not denote a
13286 valid signal, but can be used to test whether the @var{pid} arguments
13287 specify processes to which a signal could be sent.
13289 If @var{pid} is positive, the signal is sent to the process with the
13290 process ID @var{pid}. If @var{pid} is zero, the signal is sent to all
13291 processes in the process group of the current process. If @var{pid}
13292 is @minus{}1, the signal is sent to all processes for which the user has
13293 permission to send a signal. If @var{pid} is less than @minus{}1, the signal
13294 is sent to all processes in the process group that equals the absolute
13295 value of @var{pid}.
13297 If @var{pid} is not positive, a system-dependent set of system
13298 processes is excluded from the list of processes to which the signal
13301 If a negative @var{PID} argument is desired as the first one, it
13302 should be preceded by @option{--}. However, as a common extension to
13303 @acronym{POSIX}, @option{--} is not required with @samp{kill
13304 -@var{signal} -@var{pid}}. The following commands are equivalent:
13313 The first form of the @command{kill} command succeeds if every @var{pid}
13314 argument specifies at least one process that the signal was sent to.
13316 The second form of the @command{kill} command lists signal information.
13317 Either the @option{-l} or @option{--list} option, or the @option{-t}
13318 or @option{--table} option must be specified. Without any
13319 @var{signal} argument, all supported signals are listed. The output
13320 of @option{-l} or @option{--list} is a list of the signal names, one
13321 per line; if @var{signal} is already a name, the signal number is
13322 printed instead. The output of @option{-t} or @option{--table} is a
13323 table of signal numbers, names, and descriptions. This form of the
13324 @command{kill} command succeeds if all @var{signal} arguments are valid
13325 and if there is no output error.
13327 The @command{kill} command also supports the @option{--help} and
13328 @option{--version} options. @xref{Common options}.
13330 A @var{signal} may be a signal name like @samp{HUP}, or a signal
13331 number like @samp{1}, or an exit status of a process terminated by the
13332 signal. A signal name can be given in canonical form or prefixed by
13333 @samp{SIG}. The case of the letters is ignored, except for the
13334 @option{-@var{signal}} option which must use upper case to avoid
13335 ambiguity with lower case option letters. The following signal names
13336 and numbers are supported on all @acronym{POSIX} compliant systems:
13342 2. Terminal interrupt.
13348 9. Kill (cannot be caught or ignored).
13356 Other supported signal names have system-dependent corresponding
13357 numbers. All systems conforming to @acronym{POSIX} 1003.1-2001 also
13358 support the following signals:
13362 Access to an undefined portion of a memory object.
13364 Child process terminated, stopped, or continued.
13366 Continue executing, if stopped.
13368 Erroneous arithmetic operation.
13370 Illegal Instruction.
13372 Write on a pipe with no one to read it.
13374 Invalid memory reference.
13376 Stop executing (cannot be caught or ignored).
13380 Background process attempting read.
13382 Background process attempting write.
13384 High bandwidth data is available at a socket.
13386 User-defined signal 1.
13388 User-defined signal 2.
13392 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XSI} extension
13393 also support the following signals:
13399 Profiling timer expired.
13403 Trace/breakpoint trap.
13405 Virtual timer expired.
13407 CPU time limit exceeded.
13409 File size limit exceeded.
13413 @acronym{POSIX} 1003.1-2001 systems that support the @acronym{XRT} extension
13414 also support at least eight real-time signals called @samp{RTMIN},
13415 @samp{RTMIN+1}, @dots{}, @samp{RTMAX-1}, @samp{RTMAX}.
13421 @cindex delaying commands
13422 @cindex commands for delaying
13424 @c Perhaps @command{wait} or other commands should be described here also?
13427 * sleep invocation:: Delay for a specified time.
13431 @node sleep invocation
13432 @section @command{sleep}: Delay for a specified time
13435 @cindex delay for a specified time
13437 @command{sleep} pauses for an amount of time specified by the sum of
13438 the values of the command line arguments.
13442 sleep @var{number}[smhd]@dots{}
13446 Each argument is a number followed by an optional unit; the default
13447 is seconds. The units are:
13460 Historical implementations of @command{sleep} have required that
13461 @var{number} be an integer, and only accepted a single argument
13462 without a suffix. However, GNU @command{sleep} accepts
13463 arbitrary floating point numbers (using a period before any fractional
13466 The only options are @option{--help} and @option{--version}. @xref{Common
13472 @node Numeric operations
13473 @chapter Numeric operations
13475 @cindex numeric operations
13476 These programs do numerically-related operations.
13479 * factor invocation:: Show factors of numbers.
13480 * seq invocation:: Print sequences of numbers.
13484 @node factor invocation
13485 @section @command{factor}: Print prime factors
13488 @cindex prime factors
13490 @command{factor} prints prime factors. Synopses:
13493 factor [@var{number}]@dots{}
13494 factor @var{option}
13497 If no @var{number} is specified on the command line, @command{factor} reads
13498 numbers from standard input, delimited by newlines, tabs, or spaces.
13500 The only options are @option{--help} and @option{--version}. @xref{Common
13503 The algorithm it uses is not very sophisticated, so for some inputs
13504 @command{factor} runs for a long time. The hardest numbers to factor are
13505 the products of large primes. Factoring the product of the two largest 32-bit
13506 prime numbers takes about 80 seconds of CPU time on a 1.6 GHz Athlon.
13509 $ p=`echo '4294967279 * 4294967291'|bc`
13511 18446743979220271189: 4294967279 4294967291
13514 Similarly, it takes about 80 seconds for GNU factor (from coreutils-5.1.2)
13515 to ``factor'' the largest 64-bit prime:
13518 $ factor 18446744073709551557
13519 18446744073709551557: 18446744073709551557
13522 In contrast, @command{factor} factors the largest 64-bit number in just
13523 over a tenth of a second:
13526 $ factor `echo '2^64-1'|bc`
13527 18446744073709551615: 3 5 17 257 641 65537 6700417
13533 @node seq invocation
13534 @section @command{seq}: Print numeric sequences
13537 @cindex numeric sequences
13538 @cindex sequence of numbers
13540 @command{seq} prints a sequence of numbers to standard output. Synopses:
13543 seq [@var{option}]@dots{} @var{last}
13544 seq [@var{option}]@dots{} @var{first} @var{last}
13545 seq [@var{option}]@dots{} @var{first} @var{increment} @var{last}
13548 @command{seq} prints the numbers from @var{first} to @var{last} by
13549 @var{increment}. By default, each number is printed on a separate line.
13550 When @var{increment} is not specified, it defaults to @samp{1},
13551 even when @var{first} is larger than @var{last}.
13552 @var{first} also defaults to @samp{1}. So @code{seq 1} prints
13553 @samp{1}, but @code{seq 0} and @code{seq 10 5} produce no output.
13554 Floating-point numbers
13555 may be specified (using a period before any fractional digits).
13557 The program accepts the following options. Also see @ref{Common options}.
13558 Options must precede operands.
13561 @item -f @var{format}
13562 @itemx --format=@var{format}
13563 @opindex -f @var{format}
13564 @opindex --format=@var{format}
13565 @cindex formatting of numbers in @command{seq}
13566 Print all numbers using @var{format}.
13567 @var{format} must contain exactly one of the @samp{printf}-style
13568 floating point conversion specifications @samp{%a}, @samp{%e},
13569 @samp{%f}, @samp{%g}, @samp{%A}, @samp{%E}, @samp{%F}, @samp{%G}.
13571 The default format is derived from @var{first}, @var{step}, and
13572 @var{last}. If these all use a fixed point decimal representation,
13573 the default format is @samp{%.@var{p}f}, where @var{p} is the minimum
13574 precision that can represent the output numbers exactly. Otherwise,
13575 the default format is @samp{%g}.
13577 @item -s @var{string}
13578 @itemx --separator=@var{string}
13579 @cindex separator for numbers in @command{seq}
13580 Separate numbers with @var{string}; default is a newline.
13581 The output always terminates with a newline.
13584 @itemx --equal-width
13585 Print all numbers with the same width, by padding with leading zeros.
13586 @var{first}, @var{step}, and @var{last} should all use a fixed point
13587 decimal representation.
13588 (To have other kinds of padding, use @option{--format}).
13592 If you want hexadecimal integer output, you can use @command{printf}
13593 to perform the conversion:
13596 $ printf '%x\n' `seq 1048575 1024 1050623`
13602 For very long lists of numbers, use xargs to avoid
13603 system limitations on the length of an argument list:
13606 $ seq 1000000 | xargs printf '%x\n' | tail -n 3
13612 To generate octal output, use the printf @code{%o} format instead
13615 On most systems, seq can produce whole-number output for values up to
13616 at least @code{2^53}. Larger integers are approximated. The details
13617 differ depending on your floating-point implementation, but a common
13618 case is that @command{seq} works with integers through @code{2^64},
13619 and larger integers may not be numerically correct:
13622 $ seq 18446744073709551616 1 18446744073709551618
13623 18446744073709551616
13624 18446744073709551616
13625 18446744073709551618
13628 Be careful when using @command{seq} with a fractional @var{increment};
13629 otherwise you may see surprising results. Most people would expect to
13630 see @code{0.000003} printed as the last number in this example:
13633 $ seq -s ' ' 0 0.000001 0.000003
13634 0.000000 0.000001 0.000002
13637 But that doesn't happen on many systems because @command{seq} is
13638 implemented using binary floating point arithmetic (via the C
13639 @code{long double} type)---which means decimal fractions like @code{0.000001}
13640 cannot be represented exactly. That in turn means some nonintuitive
13641 conditions like @w{@code{0.000001 * 3 > 0.000003}} will end up being true.
13643 To work around that in the above example, use a slightly larger number as
13644 the @var{last} value:
13647 $ seq -s ' ' 0 0.000001 0.0000031
13648 0.000000 0.000001 0.000002 0.000003
13651 In general, when using an @var{increment} with a fractional part, where
13652 (@var{last} - @var{first}) / @var{increment} is (mathematically) a whole
13653 number, specify a slightly larger (or smaller, if @var{increment} is negative)
13654 value for @var{last} to ensure that @var{last} is the final value printed
13660 @node File permissions
13661 @chapter File permissions
13664 @include getdate.texi
13668 @node Opening the software toolbox
13669 @chapter Opening the Software Toolbox
13671 An earlier version of this chapter appeared in
13672 @uref{http://www.linuxjournal.com/article.php?sid=2762, the
13673 @cite{What's GNU?} column of @cite{Linux Journal}, 2 (June, 1994)}.
13674 It was written by Arnold Robbins.
13677 * Toolbox introduction:: Toolbox introduction
13678 * I/O redirection:: I/O redirection
13679 * The who command:: The @command{who} command
13680 * The cut command:: The @command{cut} command
13681 * The sort command:: The @command{sort} command
13682 * The uniq command:: The @command{uniq} command
13683 * Putting the tools together:: Putting the tools together
13687 @node Toolbox introduction
13688 @unnumberedsec Toolbox Introduction
13690 This month's column is only peripherally related to the GNU Project, in
13691 that it describes a number of the GNU tools on your GNU/Linux system and how they
13692 might be used. What it's really about is the ``Software Tools'' philosophy
13693 of program development and usage.
13695 The software tools philosophy was an important and integral concept
13696 in the initial design and development of Unix (of which Linux and GNU are
13697 essentially clones). Unfortunately, in the modern day press of
13698 Internetworking and flashy GUIs, it seems to have fallen by the
13699 wayside. This is a shame, since it provides a powerful mental model
13700 for solving many kinds of problems.
13702 Many people carry a Swiss Army knife around in their pants pockets (or
13703 purse). A Swiss Army knife is a handy tool to have: it has several knife
13704 blades, a screwdriver, tweezers, toothpick, nail file, corkscrew, and perhaps
13705 a number of other things on it. For the everyday, small miscellaneous jobs
13706 where you need a simple, general purpose tool, it's just the thing.
13708 On the other hand, an experienced carpenter doesn't build a house using
13709 a Swiss Army knife. Instead, he has a toolbox chock full of specialized
13710 tools---a saw, a hammer, a screwdriver, a plane, and so on. And he knows
13711 exactly when and where to use each tool; you won't catch him hammering nails
13712 with the handle of his screwdriver.
13714 The Unix developers at Bell Labs were all professional programmers and trained
13715 computer scientists. They had found that while a one-size-fits-all program
13716 might appeal to a user because there's only one program to use, in practice
13721 difficult to write,
13724 difficult to maintain and
13728 difficult to extend to meet new situations.
13731 Instead, they felt that programs should be specialized tools. In short, each
13732 program ``should do one thing well.'' No more and no less. Such programs are
13733 simpler to design, write, and get right---they only do one thing.
13735 Furthermore, they found that with the right machinery for hooking programs
13736 together, that the whole was greater than the sum of the parts. By combining
13737 several special purpose programs, you could accomplish a specific task
13738 that none of the programs was designed for, and accomplish it much more
13739 quickly and easily than if you had to write a special purpose program.
13740 We will see some (classic) examples of this further on in the column.
13741 (An important additional point was that, if necessary, take a detour
13742 and build any software tools you may need first, if you don't already
13743 have something appropriate in the toolbox.)
13745 @node I/O redirection
13746 @unnumberedsec I/O Redirection
13748 Hopefully, you are familiar with the basics of I/O redirection in the
13749 shell, in particular the concepts of ``standard input,'' ``standard output,''
13750 and ``standard error''. Briefly, ``standard input'' is a data source, where
13751 data comes from. A program should not need to either know or care if the
13752 data source is a disk file, a keyboard, a magnetic tape, or even a punched
13753 card reader. Similarly, ``standard output'' is a data sink, where data goes
13754 to. The program should neither know nor care where this might be.
13755 Programs that only read their standard input, do something to the data,
13756 and then send it on, are called @dfn{filters}, by analogy to filters in a
13759 With the Unix shell, it's very easy to set up data pipelines:
13762 program_to_create_data | filter1 | ... | filterN > final.pretty.data
13765 We start out by creating the raw data; each filter applies some successive
13766 transformation to the data, until by the time it comes out of the pipeline,
13767 it is in the desired form.
13769 This is fine and good for standard input and standard output. Where does the
13770 standard error come in to play? Well, think about @command{filter1} in
13771 the pipeline above. What happens if it encounters an error in the data it
13772 sees? If it writes an error message to standard output, it will just
13773 disappear down the pipeline into @command{filter2}'s input, and the
13774 user will probably never see it. So programs need a place where they can send
13775 error messages so that the user will notice them. This is standard error,
13776 and it is usually connected to your console or window, even if you have
13777 redirected standard output of your program away from your screen.
13779 For filter programs to work together, the format of the data has to be
13780 agreed upon. The most straightforward and easiest format to use is simply
13781 lines of text. Unix data files are generally just streams of bytes, with
13782 lines delimited by the @acronym{ASCII} @sc{lf} (Line Feed) character,
13783 conventionally called a ``newline'' in the Unix literature. (This is
13784 @code{'\n'} if you're a C programmer.) This is the format used by all
13785 the traditional filtering programs. (Many earlier operating systems
13786 had elaborate facilities and special purpose programs for managing
13787 binary data. Unix has always shied away from such things, under the
13788 philosophy that it's easiest to simply be able to view and edit your
13789 data with a text editor.)
13791 OK, enough introduction. Let's take a look at some of the tools, and then
13792 we'll see how to hook them together in interesting ways. In the following
13793 discussion, we will only present those command line options that interest
13794 us. As you should always do, double check your system documentation
13795 for the full story.
13797 @node The who command
13798 @unnumberedsec The @command{who} Command
13800 The first program is the @command{who} command. By itself, it generates a
13801 list of the users who are currently logged in. Although I'm writing
13802 this on a single-user system, we'll pretend that several people are
13807 @print{} arnold console Jan 22 19:57
13808 @print{} miriam ttyp0 Jan 23 14:19(:0.0)
13809 @print{} bill ttyp1 Jan 21 09:32(:0.0)
13810 @print{} arnold ttyp2 Jan 23 20:48(:0.0)
13813 Here, the @samp{$} is the usual shell prompt, at which I typed @samp{who}.
13814 There are three people logged in, and I am logged in twice. On traditional
13815 Unix systems, user names are never more than eight characters long. This
13816 little bit of trivia will be useful later. The output of @command{who} is nice,
13817 but the data is not all that exciting.
13819 @node The cut command
13820 @unnumberedsec The @command{cut} Command
13822 The next program we'll look at is the @command{cut} command. This program
13823 cuts out columns or fields of input data. For example, we can tell it
13824 to print just the login name and full name from the @file{/etc/passwd}
13825 file. The @file{/etc/passwd} file has seven fields, separated by
13829 arnold:xyzzy:2076:10:Arnold D. Robbins:/home/arnold:/bin/bash
13832 To get the first and fifth fields, we would use @command{cut} like this:
13835 $ cut -d: -f1,5 /etc/passwd
13836 @print{} root:Operator
13838 @print{} arnold:Arnold D. Robbins
13839 @print{} miriam:Miriam A. Robbins
13843 With the @option{-c} option, @command{cut} will cut out specific characters
13844 (i.e., columns) in the input lines. This is useful for input data
13845 that has fixed width fields, and does not have a field separator. For
13846 example, list the Monday dates for the current month:
13848 @c Is using cal ok? Looked at gcal, but I don't like it.
13859 @node The sort command
13860 @unnumberedsec The @command{sort} Command
13862 Next we'll look at the @command{sort} command. This is one of the most
13863 powerful commands on a Unix-style system; one that you will often find
13864 yourself using when setting up fancy data plumbing.
13867 command reads and sorts each file named on the command line. It then
13868 merges the sorted data and writes it to standard output. It will read
13869 standard input if no files are given on the command line (thus
13870 making it into a filter). The sort is based on the character collating
13871 sequence or based on user-supplied ordering criteria.
13874 @node The uniq command
13875 @unnumberedsec The @command{uniq} Command
13877 Finally (at least for now), we'll look at the @command{uniq} program. When
13878 sorting data, you will often end up with duplicate lines, lines that
13879 are identical. Usually, all you need is one instance of each line.
13880 This is where @command{uniq} comes in. The @command{uniq} program reads its
13881 standard input. It prints only one
13882 copy of each repeated line. It does have several options. Later on,
13883 we'll use the @option{-c} option, which prints each unique line, preceded
13884 by a count of the number of times that line occurred in the input.
13887 @node Putting the tools together
13888 @unnumberedsec Putting the Tools Together
13890 Now, let's suppose this is a large ISP server system with dozens of users
13891 logged in. The management wants the system administrator to write a program that will
13892 generate a sorted list of logged in users. Furthermore, even if a user
13893 is logged in multiple times, his or her name should only show up in the
13896 The administrator could sit down with the system documentation and write a C
13897 program that did this. It would take perhaps a couple of hundred lines
13898 of code and about two hours to write it, test it, and debug it.
13899 However, knowing the software toolbox, the administrator can instead start out
13900 by generating just a list of logged on users:
13910 Next, sort the list:
13913 $ who | cut -c1-8 | sort
13920 Finally, run the sorted list through @command{uniq}, to weed out duplicates:
13923 $ who | cut -c1-8 | sort | uniq
13929 The @command{sort} command actually has a @option{-u} option that does what
13930 @command{uniq} does. However, @command{uniq} has other uses for which one
13931 cannot substitute @samp{sort -u}.
13933 The administrator puts this pipeline into a shell script, and makes it available for
13934 all the users on the system (@samp{#} is the system administrator,
13935 or @code{root}, prompt):
13938 # cat > /usr/local/bin/listusers
13939 who | cut -c1-8 | sort | uniq
13941 # chmod +x /usr/local/bin/listusers
13944 There are four major points to note here. First, with just four
13945 programs, on one command line, the administrator was able to save about two
13946 hours worth of work. Furthermore, the shell pipeline is just about as
13947 efficient as the C program would be, and it is much more efficient in
13948 terms of programmer time. People time is much more expensive than
13949 computer time, and in our modern ``there's never enough time to do
13950 everything'' society, saving two hours of programmer time is no mean
13953 Second, it is also important to emphasize that with the
13954 @emph{combination} of the tools, it is possible to do a special
13955 purpose job never imagined by the authors of the individual programs.
13957 Third, it is also valuable to build up your pipeline in stages, as we did here.
13958 This allows you to view the data at each stage in the pipeline, which helps
13959 you acquire the confidence that you are indeed using these tools correctly.
13961 Finally, by bundling the pipeline in a shell script, other users can use
13962 your command, without having to remember the fancy plumbing you set up for
13963 them. In terms of how you run them, shell scripts and compiled programs are
13966 After the previous warm-up exercise, we'll look at two additional, more
13967 complicated pipelines. For them, we need to introduce two more tools.
13969 The first is the @command{tr} command, which stands for ``transliterate.''
13970 The @command{tr} command works on a character-by-character basis, changing
13971 characters. Normally it is used for things like mapping upper case to
13975 $ echo ThIs ExAmPlE HaS MIXED case! | tr '[:upper:]' '[:lower:]'
13976 @print{} this example has mixed case!
13979 There are several options of interest:
13983 work on the complement of the listed characters, i.e.,
13984 operations apply to characters not in the given set
13987 delete characters in the first set from the output
13990 squeeze repeated characters in the output into just one character.
13993 We will be using all three options in a moment.
13995 The other command we'll look at is @command{comm}. The @command{comm}
13996 command takes two sorted input files as input data, and prints out the
13997 files' lines in three columns. The output columns are the data lines
13998 unique to the first file, the data lines unique to the second file, and
13999 the data lines that are common to both. The @option{-1}, @option{-2}, and
14000 @option{-3} command line options @emph{omit} the respective columns. (This is
14001 non-intuitive and takes a little getting used to.) For example:
14023 The file name @file{-} tells @command{comm} to read standard input
14024 instead of a regular file.
14026 Now we're ready to build a fancy pipeline. The first application is a word
14027 frequency counter. This helps an author determine if he or she is over-using
14030 The first step is to change the case of all the letters in our input file
14031 to one case. ``The'' and ``the'' are the same word when doing counting.
14034 $ tr '[:upper:]' '[:lower:]' < whats.gnu | ...
14037 The next step is to get rid of punctuation. Quoted words and unquoted words
14038 should be treated identically; it's easiest to just get the punctuation out of
14042 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' | ...
14045 The second @command{tr} command operates on the complement of the listed
14046 characters, which are all the letters, the digits, the underscore, and
14047 the blank. The @samp{\n} represents the newline character; it has to
14048 be left alone. (The @acronym{ASCII} tab character should also be included for
14049 good measure in a production script.)
14051 At this point, we have data consisting of words separated by blank space.
14052 The words only contain alphanumeric characters (and the underscore). The
14053 next step is break the data apart so that we have one word per line. This
14054 makes the counting operation much easier, as we will see shortly.
14057 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14058 > tr -s ' ' '\n' | ...
14061 This command turns blanks into newlines. The @option{-s} option squeezes
14062 multiple newline characters in the output into just one. This helps us
14063 avoid blank lines. (The @samp{>} is the shell's ``secondary prompt.''
14064 This is what the shell prints when it notices you haven't finished
14065 typing in all of a command.)
14067 We now have data consisting of one word per line, no punctuation, all one
14068 case. We're ready to count each word:
14071 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14072 > tr -s ' ' '\n' | sort | uniq -c | ...
14075 At this point, the data might look something like this:
14088 The output is sorted by word, not by count! What we want is the most
14089 frequently used words first. Fortunately, this is easy to accomplish,
14090 with the help of two more @command{sort} options:
14094 do a numeric sort, not a textual one
14097 reverse the order of the sort
14100 The final pipeline looks like this:
14103 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14104 > tr -s ' ' '\n' | sort | uniq -c | sort -n -r
14113 Whew! That's a lot to digest. Yet, the same principles apply. With six
14114 commands, on two lines (really one long one split for convenience), we've
14115 created a program that does something interesting and useful, in much
14116 less time than we could have written a C program to do the same thing.
14118 A minor modification to the above pipeline can give us a simple spelling
14119 checker! To determine if you've spelled a word correctly, all you have to
14120 do is look it up in a dictionary. If it is not there, then chances are
14121 that your spelling is incorrect. So, we need a dictionary.
14122 The conventional location for a dictionary is @file{/usr/dict/words}.
14123 On my GNU/Linux system,@footnote{Redhat Linux 6.1, for the November 2000
14124 revision of this article.}
14125 this is a is a sorted, 45,402 word dictionary.
14127 Now, how to compare our file with the dictionary? As before, we generate
14128 a sorted list of words, one per line:
14131 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14132 > tr -s ' ' '\n' | sort -u | ...
14135 Now, all we need is a list of words that are @emph{not} in the
14136 dictionary. Here is where the @command{comm} command comes in.
14139 $ tr '[:upper:]' '[:lower:]' < whats.gnu | tr -cd '[:alnum:]_ \n' |
14140 > tr -s ' ' '\n' | sort -u |
14141 > comm -23 - /usr/dict/words
14144 The @option{-2} and @option{-3} options eliminate lines that are only in the
14145 dictionary (the second file), and lines that are in both files. Lines
14146 only in the first file (standard input, our stream of words), are
14147 words that are not in the dictionary. These are likely candidates for
14148 spelling errors. This pipeline was the first cut at a production
14149 spelling checker on Unix.
14151 There are some other tools that deserve brief mention.
14155 search files for text that matches a regular expression
14158 count lines, words, characters
14161 a T-fitting for data pipes, copies data to files and to standard output
14164 the stream editor, an advanced tool
14167 a data manipulation language, another advanced tool
14170 The software tools philosophy also espoused the following bit of
14171 advice: ``Let someone else do the hard part.'' This means, take
14172 something that gives you most of what you need, and then massage it the
14173 rest of the way until it's in the form that you want.
14179 Each program should do one thing well. No more, no less.
14182 Combining programs with appropriate plumbing leads to results where
14183 the whole is greater than the sum of the parts. It also leads to novel
14184 uses of programs that the authors might never have imagined.
14187 Programs should never print extraneous header or trailer data, since these
14188 could get sent on down a pipeline. (A point we didn't mention earlier.)
14191 Let someone else do the hard part.
14194 Know your toolbox! Use each program appropriately. If you don't have an
14195 appropriate tool, build one.
14198 As of this writing, all the programs we've discussed are available via
14199 anonymous @command{ftp} from: @*
14200 @uref{ftp://gnudist.gnu.org/textutils/textutils-1.22.tar.gz}. (There may
14201 be more recent versions available now.)
14203 None of what I have presented in this column is new. The Software Tools
14204 philosophy was first introduced in the book @cite{Software Tools}, by
14205 Brian Kernighan and P.J. Plauger (Addison-Wesley, ISBN 0-201-03669-X).
14206 This book showed how to write and use software tools. It was written in
14207 1976, using a preprocessor for FORTRAN named @command{ratfor} (RATional
14208 FORtran). At the time, C was not as ubiquitous as it is now; FORTRAN
14209 was. The last chapter presented a @command{ratfor} to FORTRAN
14210 processor, written in @command{ratfor}. @command{ratfor} looks an awful
14211 lot like C; if you know C, you won't have any problem following the
14214 In 1981, the book was updated and made available as @cite{Software Tools
14215 in Pascal} (Addison-Wesley, ISBN 0-201-10342-7). Both books are
14216 still in print and are well worth
14217 reading if you're a programmer. They certainly made a major change in
14218 how I view programming.
14220 The programs in both books are available from
14221 @uref{http://cm.bell-labs.com/who/bwk, Brian Kernighan's home page}.
14222 For a number of years, there was an active
14223 Software Tools Users Group, whose members had ported the original
14224 @command{ratfor} programs to essentially every computer system with a
14225 FORTRAN compiler. The popularity of the group waned in the middle 1980s
14226 as Unix began to spread beyond universities.
14228 With the current proliferation of GNU code and other clones of Unix programs,
14229 these programs now receive little attention; modern C versions are
14230 much more efficient and do more than these programs do. Nevertheless, as
14231 exposition of good programming style, and evangelism for a still-valuable
14232 philosophy, these books are unparalleled, and I recommend them highly.
14234 Acknowledgment: I would like to express my gratitude to Brian Kernighan
14235 of Bell Labs, the original Software Toolsmith, for reviewing this column.
14237 @include doclicense.texi
14248 @c Local variables:
14249 @c texinfo-column-for-description: 32