ult/Using_Linux_Tools.rst
changeset 55 6aa9568b5874
parent 44 6a9f6526b4f4
child 73 d32a19958ad9
equal deleted inserted replaced
54:bed3fe467e21 55:6aa9568b5874
   195 Logging out is done by entering the logout command, followed by Enter. You are successfully disconnected from the system when you see the login screen again.Don't power−off the computer after logging out. It is not meant to be shut off without application of the proper procedures for halting the system. Powering it off without going through the halting process might cause severe damage!
   195 Logging out is done by entering the logout command, followed by Enter. You are successfully disconnected from the system when you see the login screen again.Don't power−off the computer after logging out. It is not meant to be shut off without application of the proper procedures for halting the system. Powering it off without going through the halting process might cause severe damage!
   196 
   196 
   197 Basic Commands
   197 Basic Commands
   198 ===============
   198 ===============
   199 
   199 
   200 ls
   200 1.ls
   201 ---
   201 ----
   202 
   202 
   203 When invoked without any arguments, *ls* lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed. The name *ls* is derived from *list segments* which was used in earlier systems.
   203 When invoked without any arguments, *ls* lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed. The name *ls* is derived from *list segments* which was used in earlier systems.
   204 
   204 
   205 Files whose names start with "." are not listed, unless the *-a* flag is specified or the files are specified explicitly.
   205 Files whose names start with "." are not listed, unless the *-a* flag is specified or the files are specified explicitly.
   206 
   206 
   222 	drwxr--r--   1 plum  editors   4096  jeeves
   222 	drwxr--r--   1 plum  editors   4096  jeeves
   223 	-rw-r--r--   1 plum  editors  30405  psmith
   223 	-rw-r--r--   1 plum  editors  30405  psmith
   224 	-r-xr-xr-x   1 plum  plum      8460  blandings
   224 	-r-xr-xr-x   1 plum  plum      8460  blandings
   225 
   225 
   226 
   226 
   227 date
   227 2.date
   228 ------
   228 -------
   229 
   229 
   230 The Unix date command displays the time and date. The super-user can use it to set the system clock.
   230 The Unix date command displays the time and date. The super-user can use it to set the system clock.
   231 
   231 
   232 With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the timezone name, and the year. For example::
   232 With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the timezone name, and the year. For example::
   233 
   233 
   268 
   268 
   269 Other Unix and Unix-like systems may set different options or date formats for date, for example, on some systems to set the current date and time to September 8, 2004 01:22 you type::
   269 Other Unix and Unix-like systems may set different options or date formats for date, for example, on some systems to set the current date and time to September 8, 2004 01:22 you type::
   270 
   270 
   271 	$date --set="20040908 01:22"
   271 	$date --set="20040908 01:22"
   272 
   272 
   273 cd
   273 3.cd
   274 ----
   274 -----
   275 
   275 
   276 Change directory. Use “ cd ..” to go up one directory.
   276 Change directory. Use “ cd ..” to go up one directory.
   277 
   277 
   278 One dot '.' represents the current directory while two dots '..' represent the parent directory.
   278 One dot '.' represents the current directory while two dots '..' represent the parent directory.
   279 
   279 
   280 “ cd -” will return you to the previous directory (a bit like an “undo”).
   280 “ cd -” will return you to the previous directory (a bit like an “undo”).
   281 
   281 
   282 You can also use cd absolute path or cd relative path (see below):
   282 You can also use cd absolute path or cd relative path (see below):
   283 
   283 
   284 Absolute paths
   284 Absolute paths:
   285 
   285 
   286     An “ absolute path” is easily recognised from the leading forward slash, /. The / means that you start at the top level directory and continue down.
   286     An “ absolute path” is easily recognised from the leading forward slash, /. The / means that you start at the top level directory and continue down.
   287 
   287 
   288 For example to get to /boot/grub you would type::
   288 For example to get to /boot/grub you would type::
   289 
   289 
   290 	$cd /boot/grub
   290 	$cd /boot/grub
   291 
   291 
   292 This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command).
   292 This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command).
   293 
   293 
   294 Relative paths
   294 Relative paths:
   295 
   295 
   296     A “ relative path” doesn't have a preceding slash. Use a relative path when you start from a directory below the top level directory structure. This is dependent on where you are in the filesystem.
   296     A “ relative path” doesn't have a preceding slash. Use a relative path when you start from a directory below the top level directory structure. This is dependent on where you are in the filesystem.
   297 
   297 
   298     For example if you are in root's home directory and want to get to /root/music, you type::
   298     For example if you are in root's home directory and want to get to /root/music, you type::
   299 
   299 
   300 	$ cd music
   300 	$ cd music
   301 
   301 
   302 Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward.
   302 Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward.
       
   303 
       
   304 4.who
       
   305 -----
       
   306 
       
   307 The standard Unix command *who* displays a list of users who are currently logged into a computer.
       
   308 
       
   309 The *who* command is related to the command *w*, which provides the same information but also displays additional data and statistics.::
       
   310 
       
   311 	$who
       
   312 	beeblebrox tty7         2009-09-08 10:50 (:0)
       
   313 	beeblebrox pts/0        2009-09-08 11:25 (:0.0)
       
   314 	dumbledore pts/1        2009-09-08 18:11 (potter.xyz.in)
       
   315 	beeblebrox pts/2        2009-09-08 18:53 (:0.0)
       
   316 
       
   317 
       
   318 The command can be invoked with the arguments am i or am I (so it is invoked as who am i or am I), showing information about the current terminal only (see the -m option below, of which this invocation is equivalent).
       
   319 
       
   320 The Single Unix Specification (SUS) without extensions only specifies the following -m, -T, and -u options, all other options are specified in the XSI extension.
       
   321 
       
   322     -a, process the system database used for user information with the -b, -d, -l, -p, -r, -t, -T and -u.
       
   323     -b, show time when system was last rebooted
       
   324     -d, show zombie processes and details
       
   325     -H, show column headers
       
   326     -l, show terminals where a user can log in
       
   327     -m, show information about the current terminal only
       
   328     -p, show active processes
       
   329     -q, quick format, show only names and the number of all users logged on, disables all other options; equivalent to users command line utility
       
   330     -r, show runlevel of the init process.
       
   331     -s, (default) show only name, terminal, and time details
       
   332     -t, show when system clock was last changed
       
   333     -T, show details of each terminal in a standard format 
       
   334     -u, show idle time; XSI shows users logged in and displays information whether the terminal has been used recently or not 
       
   335 
       
   336 5.mkdir
       
   337 --------
       
   338 
       
   339 Normal usage is as straightforward as follows::
       
   340 
       
   341 	$mkdir name_of_directory
       
   342 
       
   343 Where *name_of_directory* is the name of the directory one wants to create. When typed as above (ie. normal usage), the new directory would be created within the current directory. On Unix, multiple directories can be specified, and *mkdir* will try to create all of them.
       
   344 
       
   345 Options
       
   346 ~~~~~~~
       
   347 
       
   348 On Unix-like operating systems, *mkdir* takes options. Three of the most common options are:
       
   349 
       
   350     * *-p*: will also create all directories leading up to the given directory that do not exist already. If the given directory already exists, ignore the error.
       
   351     * *-v*: display each directory that mkdir creates. Most often used with -p.
       
   352     * *-m*: specify the octal permissions of directories created by mkdir.
       
   353 
       
   354 *-p* is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts.
       
   355 
       
   356 Examples
       
   357 ~~~~~~~~
       
   358 
       
   359 An example of *-p* in action is::
       
   360 
       
   361 	$mkdir -p /tmp/a/b/c
       
   362 
       
   363 If */tmp/a* exists but */tmp/a/b* does not, mkdir will create */tmp/a/b* before creating */tmp/a/b/c*.
       
   364 
       
   365 And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself)::
       
   366 
       
   367 	$mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags}
       
   368 
       
   369 This will create:
       
   370 
       
   371 tmpdir 	- branches
       
   372 	- tag
       
   373 	- trunk	- sources - includes
       
   374 			  - docs
       
   375 
       
   376 Getting Help
       
   377 ============
       
   378 
       
   379 1. apropos and whatis
       
   380 ----------------------
       
   381 
       
   382 This is a command to search the manual pages files in Unix and Unix-like operating systems. ::
       
   383 
       
   384 	$ apropos grep
       
   385 	egrep       egrep (1)       Search a file for a pattern using full 		regular expressions
       
   386 	fgrep       fgrep (1)       Search a file for a fixed-character 	string
       
   387 	fmlgrep     fmlgrep (1)     Search a file for a pattern
       
   388 	grep        grep (1)        Search a file for a pattern
       
   389 	gzgrep      gzgrep (1)      Search a possibly compressed file for a 		regular expression
       
   390 	nisgrep     nismatch (1)    Utilities for searching NIS+ tables
       
   391 	pgrep       pgrep (1)       Find or signal a process by name or 	other attribute
       
   392 	zgrep       zgrep (1)       Search a possibly compressed file for a 		regular expression
       
   393 
       
   394 In this example, the user uses *apropos* to search for the string "grep", and apropos returns the indicated *man* pages that include the term "grep".
       
   395 
       
   396 A short index of explanations for commands is available using the *whatis* command, like in the examples below::
       
   397 
       
   398 	[your_prompt] whatis ls
       
   399 	ls                   (1)  - list directory contents
       
   400 
       
   401 This displays short information about a command, and the first section in the collection of man pages that contains an appropriate page.
       
   402 
       
   403 If you don't know where to get started and which man page to read, *apropos* gives more information. Say that you don't know how to start a browser, then you could enter the following command::
       
   404 
       
   405 	another prompt> apropos browser
       
   406 	gmusicbrowser (1)    - Jukebox for large collections of audio files
       
   407 	infobrowser (1)      - read Info documents
       
   408 	libsmbclient (7)     - An extension library for browsers and that 		can be used...
       
   409 	opera (1)            - a standards-compliant graphical Web browser
       
   410 	sensible-browser (1) - sensible editing, paging, and web browsing
       
   411 	smbtree (1)          - A text based smb network browser
       
   412 	tvtk_doc (1)         - A GUI based TVTK documentation search browser.
       
   413 	viewres (1)          - graphical class browser for Xt
       
   414 	w3m (1)              - a text based Web browser and pager
       
   415 	www-browser (1)      - a text based Web browser and pager
       
   416 
       
   417 
       
   418 2. man
       
   419 -------
       
   420 
       
   421 Man pages (short for "manual pages") are the extensive documentation that comes preinstalled with almost all substantial Unix and Unix-like operating systems. The Unix command used to display them is *man*. Each page is a self-contained document.
       
   422 
       
   423 To read a manual page for a Unix command, one can use::
       
   424 
       
   425 	$ man <command_name>
       
   426 
       
   427 at a shell prompt; for example, "man ftp". In order to simplify navigation through the output, man generally uses the less terminal pager.
       
   428 
       
   429 Pages are traditionally referred to using the notation "name(section)"; for example, ftp(1). The same page name may appear in more than one section of the manual, this can occur when the names of system calls, user commands, or macro packages coincide. Two examples are *man(1)* and *man(7)*, or *exit(2)* and *exit(3)*. The syntax for accessing the non-default manual section varies between different man implementations. On Linux and *BSD, for example, the syntax for reading *printf(3)* is::
       
   430 
       
   431 	$ man 3 printf
       
   432 
       
   433 Another example::
       
   434 
       
   435 	yourname@yourcomp ~> man man
       
   436 
       
   437 
       
   438 Layout
       
   439 ~~~~~~
       
   440 
       
   441 All man pages follow a common layout that is optimized for presentation on a simple ASCII text display, possibly without any form of highlighting or font control. Sections present may include:
       
   442 
       
   443 NAME
       
   444     The name of the command or function, followed by a one-line description of what it does.
       
   445 SYNOPSIS
       
   446     In the case of a command, you get a formal description of how to run it and what command line options it takes. For program functions, a list of the parameters the function takes and which header file contains its definition. For experienced users, this may be all the documentation they need.
       
   447 DESCRIPTION
       
   448     A textual description of the functioning of the command or function.
       
   449 EXAMPLES
       
   450     Some examples of common usage.
       
   451 SEE ALSO
       
   452     A list of related commands or functions.
       
   453 
       
   454 Other sections may be present, but these are not well standardized across man pages. Common examples include: OPTIONS, EXIT STATUS, ENVIRONMENT, KNOWN BUGS, FILES, AUTHOR, REPORTING BUGS, HISTORY and COPYRIGHT.
       
   455 
       
   456 History
       
   457 ~~~~~~~
       
   458 
       
   459 The UNIX Programmer's Manual was first published on November 3, 1971. The first actual man pages were written by Dennis Ritchie and Ken Thompson at the insistence of Doug McIlroy in 1971. The *troff* macros used for man pages (-mm) were the general-purpose ones written by Ted Dolotta (later to be the first manager of USG and the principal author of the System III manual), with additions for the manuals. At the time, the availability of online documentation through the manual page system was regarded as a great advance. To this day, virtually every Unix command line application comes with its man page, and many Unix users perceive a lack of man pages as a sign of low quality; indeed, some projects, such as Debian, go out of their way to write man pages for programs lacking one. Few alternatives to man have enjoyed much popularity, with the possible exception of the GNU project's "info" system, an early and simple hypertext system.
       
   460 
       
   461 However, the format of a single page for each application, the lack of classification within the sections and the relatively unsophisticated formatting facilities have motivated the development of alternative documentation systems, such as the previously mentioned info system.
       
   462 
       
   463 Most Unix GUI applications (particularly those built using the GNOME and KDE development environments) now provide end-user documentation in HTML and include embedded HTML viewers such as yelp for reading the help within the application.
       
   464 
       
   465 Usually the man pages are written in English. Translations into other languages can be also available on the system.
       
   466 
       
   467 The default format of the man pages is troff, with either the macro package man (appearance oriented) or on some systems mdoc (semantic oriented). This makes it possible to typeset a man page to PostScript, PDF and various other formats for viewing or printing.
       
   468 
       
   469 3. info
       
   470 --------
       
   471 
       
   472 *info* is a software utility which forms a hypertextual, multipage documentation and help viewer working on a command line interface, useful when there is no GUI available.
       
   473 
       
   474 The syntax is ::
       
   475 	
       
   476 	$ info <command_name>
       
   477 
       
   478 *info* processes info files, which are Texinfo formatted files, and presents the documentation as a tree, with simple commands to traverse the tree and to follow cross references. For instance
       
   479 
       
   480     - *n* goes to the next page.
       
   481     - *p* goes to the previous page.
       
   482     - *u* goes to the upper page.
       
   483     - *l* goes to the last(visited) node
       
   484     - To follow a cross reference, the cursor can be moved over a link (a word preceded by a `*`) and enter pressed.
       
   485 
       
   486 info was initially written for use with GNU/Linux and then ported to other Unix-like operating systems.
       
   487 
       
   488 4. --help
       
   489 ----------
       
   490 
       
   491 Most GNU commands support the --help, which gives a short explanation about how to use the command and a list of available options. Below is the output of this option with the *cat* command::
       
   492 
       
   493 	$ userprompt@host: cat --help
       
   494 	Usage: cat [OPTION] [FILE]...
       
   495 	Concatenate FILE(s), or standard input, to standard output.
       
   496 
       
   497 	  -A, --show-all           equivalent to -vET
       
   498 	  -b, --number-nonblank    number nonempty output lines
       
   499 	  -e                       equivalent to -vE
       
   500 	  -E, --show-ends          display $ at end of each line
       
   501 	  -n, --number             number all output lines
       
   502 	  -s, --squeeze-blank      suppress repeated empty output lines
       
   503 	  -t                       equivalent to -vT
       
   504 	  -T, --show-tabs          display TAB characters as ^I
       
   505 	  -u                       (ignored)
       
   506 	  -v, --show-nonprinting   use ^ and M- notation, except for LFD and 		  TAB
       
   507 	  --help     display this help and exit
       
   508       	  --version  output version information and exit
       
   509 
       
   510 	With no FILE, or when FILE is -, read standard input.
       
   511 
       
   512 	Examples:
       
   513 	  cat f - g  Output f's contents, then standard input, then g's 	  contents.
       
   514 	  cat        Copy standard input to standard output.
       
   515 
       
   516 	Report bugs to <bug-coreutils@gnu.org>.
       
   517 
       
   518 
       
   519 Basic file handling
       
   520 ===================
       
   521 
       
   522 1. cp
       
   523 ------
       
   524 
       
   525 *cp* is the command entered in a Unix shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name.
       
   526 
       
   527 Usage
       
   528 ~~~~~
       
   529 
       
   530 To copy a file to another file::
       
   531 
       
   532 	$ cp [ -f ] [ -H ] [ -i ] [ -p ][ -- ] SourceFile TargetFile
       
   533 
       
   534 To copy a file to a directory::
       
   535 
       
   536 	$ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... 		TargetDirectory
       
   537 
       
   538 To copy a directory to a directory::
       
   539 
       
   540 	$ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -- ] { -r | -R } 
       
   541 	SourceDirectory ... TargetDirectory
       
   542 
       
   543 Flags
       
   544 ~~~~~
       
   545 
       
   546 *-f* (force) – specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command.
       
   547 
       
   548 *-P* – makes the cp command copy symbolic links. The default is to follow symbolic links, that is, to copy files to which symbolic links point.
       
   549 
       
   550 *-i* (interactive) – prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file.
       
   551 
       
   552 *-p* (preserve) – duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory:
       
   553 
       
   554     * The time of the last data modification and the time of the last access.
       
   555     * The user ID and group ID (only if it has permissions to do this)
       
   556     * The file permission bits and the SUID and SGID bits.
       
   557 
       
   558 *-R* (recursive) – copy directories (recursively copying all the contents)
       
   559 
       
   560 Examples
       
   561 ~~~~~~~~
       
   562 
       
   563 To make a copy of a file in the current directory, enter::
       
   564 
       
   565     $ cp prog.c prog.bak
       
   566 
       
   567 This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces it with a copy of the prog.c file.
       
   568 
       
   569 To copy a file in your current directory into another directory, enter::
       
   570 
       
   571     $ cp zaphod /home/books/hhgg
       
   572 
       
   573 This copies the jones file to /home/books/hhgg/zaphod.
       
   574 
       
   575 To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter::
       
   576 
       
   577     $ cp -p martin_luther_king martin_luther_king.jr
       
   578 
       
   579 This copies the *martin_luther_king* file to the *martin_luther_king.jr* file. Instead of creating the file with the current date and time stamp, the system gives the *martin_luther_king.jr* file the same date and time as the *martin_luther_king* file. The *martin_luther_king.jr* file also inherits the *martin_luther_king* file's access control protection.
       
   580 
       
   581 To copy all the files in a directory to a new directory, enter::
       
   582 
       
   583     $ cp /home/galactica/clients/* /home/hhgg/customers
       
   584 
       
   585 This copies only the files in the clients directory to the customers directory.
       
   586 
       
   587 To copy a directory, including all its files and subdirectories, to another directory, enter:
       
   588 
       
   589     $ cp -R /home/hhgg/clients /home/hhgg/customers
       
   590 
       
   591 This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory.
       
   592 
       
   593 To copy a specific set of files to another directory, enter::
       
   594 
       
   595     $ cp zaphod arthur ford /home/hhgg/clients
       
   596 
       
   597 This copies the *zaphod*, *arthur*, and *ford* files in your current working directory to the /home/hhgg/clients directory.
       
   598 
       
   599 To use pattern-matching characters to copy files, enter::
       
   600 
       
   601     $ cp programs/*.py .
       
   602 
       
   603 This copies the files in the programs directory that end with *.py* to the current directory, signified by the single . (dot). You must type a space between the *py* and the final dot.
       
   604 
       
   605 2. mv
       
   606 -----
       
   607 
       
   608 *mv* (short for move) is a Unix command that moves one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. If possible (i.e. when the original and new files are on the same file system), *mv* will rename the file instead. Write permission is required on all directories being modified.
       
   609 
       
   610 Conflicting existing file
       
   611 ~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   612 
       
   613 In all cases, when a file is moved to have the name of an existing file (in the same directory), the existing file is deleted. If the existing file is not writable but is in a directory that is writable, then the mv command asks for confirmation if possible (i.e. if run from a terminal) before proceeding, unless the -f (force) option is used.
       
   614 
       
   615 Differences with copy and delete
       
   616 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   617 
       
   618 Note that, usually, when moving files within the same volume, moving (and/or renaming) is not the same as simply copying and then deleting the original. When moving a file, the link is simply removed from the old parent directory and added to the new parent directory. However, the file itself is untouched (i.e. it has the same inodes and resides at the same place on the disk). For example, you cannot copy a file you cannot read, but you can move (and/or rename) it (provided you have write permission to its old and new parent directories). Also, suppose there is a non-empty directory you do not have write permission to. You cannot delete this directory (since you cannot delete its contents); but you can move (and/or rename) it. Also, since moving between filenames on a single volume does not involve copying, it is faster and does not place strain of lots of reads and writes on the disk. Moving files across different volumes, however, does necessitate copying and deleting.
       
   619 
       
   620 Examples
       
   621 ~~~~~~~~
       
   622 ::
       
   623 
       
   624 	$ mv myfile mynewfilename    renames a file
       
   625 	$ mv myfile otherfilename    renames a file and deletes the existing 		file "myfile"
       
   626 	$ mv myfile /myfile          moves 'myfile' from the current 		directory to the root directory
       
   627 	$ mv myfile dir/myfile       moves 'myfile' to 'dir/myfile' relative 		to the current directory
       
   628 	$ mv myfile dir              same as the previous command (the 		filename is implied to be the same)
       
   629 	$ mv myfile dir/myfile2      moves 'myfile' to dir and renames it to 		'myfile2'
       
   630 	$ mv foo bar baz dir         moves multiple files to directory dir
       
   631 	$ mv --help                  shows a very concise help about the 		syntax of the command
       
   632 	$ man mv                     prints an extensive user manual for 		'mv' in the terminal
       
   633 
       
   634 In all cases, the file or files being moved or renamed can be a directory.
       
   635 
       
   636 Note that when the command is called with two arguments (as *mv name1 name2* or *mv name1 /dir/name2*), it can have three different effects, depending on whether *name2* does not exist, is an existing file, or is an existing directory. If the user intends to refer to an existing directory, */.* (or in some Unix versions */* is sufficient) may be appended to the name to force the system to check this. To move a file to a new directory, the directory must be created first.
       
   637 
       
   638 3. rm
       
   639 ------
       
   640 
       
   641 *rm* (short for remove) is one of several basic Unix command lines that operates on files. It is used to delete files from a filesystem. The data is not actually destroyed. Only the index listing where the file is stored is destroyed, and the storage is made available for reuse. There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused.
       
   642 
       
   643 Here's example to remove a file named "foo" from a directory, here shown with the -i option::
       
   644 
       
   645   	$ rm -i foo
       
   646     	remove foo? y
       
   647 
       
   648 Options
       
   649 ~~~~~~~
       
   650 
       
   651 Common options that rm accepts include:
       
   652 
       
   653     * *-r*, which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive")
       
   654     * *-i*, which asks for every deletion to be confirmed ("interactive")
       
   655     * *-f*, which ignores non-existent files and overrides any confirmation prompts ("force")
       
   656     * *-v*, which shows what is being removed as it happens ("verbose")
       
   657 
       
   658 *rm* is often aliased to "rm -i" so as to avoid accidental deletion of files. If a user still wishes to delete a large number of files without confirmation, they can manually cancel out the -i argument by adding the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence).
       
   659 
       
   660 *rm -rf* (variously, rm -rf /, rm -rf `*`, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf variant of the command, if run by a superuser on the root directory, would cause the contents of every writable mounted filesystem on the computer to be deleted.
       
   661 
       
   662 *rm* is often used in conjunction with xargs to supply a list of files to delete::
       
   663 
       
   664 	xargs rm < filelist
       
   665 
       
   666 When *rm* is used on a symbolic link, it deletes the link, but does not affect the target of the link.
       
   667 
       
   668 Permissions
       
   669 ~~~~~~~~~~~
       
   670 
       
   671 Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.)
       
   672 
       
   673 To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any). The read permissions are needed to list the contents of the directory in order to delete them. This sometimes leads to an odd situation where a non-empty directory cannot be deleted because one doesn't have write permission to it and so cannot delete its contents; but if the same directory were empty, one would be able to delete it.
       
   674 
       
   675 If a file resides in a directory with the sticky bit set, then deleting the file requires one to be the owner of the file.
       
   676 
       
   677 
       
   678 Command Line Arguments
       
   679 =======================
       
   680 
       
   681 In computer command line interfaces, a command line argument is an argument sent to a program being called. In general, a program can take any number of command line arguments, which may be necessary for the program to run, or may even be ignored, depending on the function of that program.
       
   682 
       
   683 For example, in Unix and Unix-like environments, an example of a command-line argument is::
       
   684 
       
   685 	rm file.s
       
   686 
       
   687 "file.s" is a command line argument which tells the program rm to remove the file "file.s".
       
   688 
       
   689 Programming languages such as C, C++ and Java allow a program to interpret the command line arguments by handling them as string parameters in the main function.
       
   690 
       
   691 A command line option or simply option (also known as a command line parameter, flag, or a switch) is an indication by a user that a computer program should change its default output.
       
   692 
       
   693 Long options are introduced via --, and are typically whole words. For example, *ls --long --classify --all*. Arguments to long options are provided with =, as *ls --block-size=1024*. Some Unix programs use long options with single dashes, for example MPlayer as in *mplayer -nosound*.
       
   694 
       
   695 Linux also uses -- to terminate option lists. For example, an attempt to delete a file called *-file1* by using *rm -file1* may produce an error, since rm may interpret *-file1* as a command line switch. Using *rm -- -file1* removes ambiguity.
       
   696 
       
   697 Basic Text Processing
       
   698 ======================
       
   699 
       
   700 1. head
       
   701 --------
       
   702 
       
   703 *head* is a program on Unix and Unix-like systems used to display the first few lines of a text file or piped data. The command syntax is::
       
   704 
       
   705 	$ head [options] <file_name>
       
   706 
       
   707 By default, *head* will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option. The following example shows the first 20 lines of filename::
       
   708 
       
   709 	$ head -n 20 filename
       
   710 
       
   711 This displays the first 5 lines of all files starting with *foo*::
       
   712 
       
   713 	$ head -n 5 foo*
       
   714 
       
   715 Some versions omit the n and just let you say -5.
       
   716 
       
   717 Flags
       
   718 ~~~~~
       
   719 ::
       
   720 
       
   721 	-c <x number of bytes> Copy first x number of bytes.
       
   722 
       
   723 Other options: *sed*
       
   724 
       
   725 Many early versions of Unix did not have this command, and so documentation and books had *sed* do this job::
       
   726 
       
   727 	sed 5q foo
       
   728 
       
   729 This says to print every line (implicit), and quit after the fifth.
       
   730 
       
   731 
       
   732 2. tail
       
   733 -------- 
       
   734 
       
   735 *tail* is a program on Unix and Unix-like systems used to display the last few lines of a text file or piped data.
       
   736 
       
   737 The command-syntax is::
       
   738 
       
   739 	$ tail [options] <file_name>
       
   740 
       
   741 By default, *tail* will print the last 10 lines of its input to the standard output. With command line options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The following example shows the last 20 lines of filename::
       
   742 
       
   743 	$ tail -n 20 filename
       
   744 
       
   745 This example shows the last 15 bytes of all files starting with *foo*::
       
   746 
       
   747 	$ tail -c 15 foo*
       
   748 
       
   749 This example shows all lines of filename from the second line onwards::
       
   750 
       
   751 	$ tail -n +2 filename
       
   752 
       
   753 Using an older syntax (still used in Sun Solaris as the -n option is not supported), the last 20 lines and the last 50 bytes of filename can be shown with the following command::
       
   754 
       
   755 	$ tail -20 filename
       
   756 	$ tail -50c filename
       
   757 
       
   758 However this syntax is now obsolete and does not conform with the POSIX 1003.1-2001 standard. Even if still supported in current versions, when used with other options (like -f, see below), these switches could not work at all.
       
   759 
       
   760 File monitoring
       
   761 ~~~~~~~~~~~~~~~
       
   762 
       
   763 *tail* has a special command line option *-f* (follow) that allows a file to be monitored. Instead of displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages::
       
   764 
       
   765 	$ tail -f /var/adm/messages
       
   766 
       
   767 To interrupt tail while it is monitoring, break-in with *Ctrl+C*. This command can be run "in the background" with &, see job control.
       
   768 
       
   769 If you have a command's result to monitor, you can use the *watch* command.
       
   770 
       
   771 
       
   772 3. cut
       
   773 -------
       
   774 
       
   775 In computing, *cut* is a Unix command line utility which is used to extract sections from each line of input — usually from a file.
       
   776 
       
   777 Extraction of line segments can typically be done by *bytes (-b), characters (-c)*, or *fields (-f)* separated by a *delimiter (-d — the tab character by default)*. A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1.
       
   778 
       
   779 Assuming a file named file containing the lines::
       
   780 
       
   781 	foo:bar:baz:qux:quux
       
   782 	one:two:three:four:five:six:seven
       
   783 	alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu
       
   784 
       
   785 To output the fourth through tenth characters of each line::
       
   786 
       
   787 	$ cut -c 4-10 file
       
   788 
       
   789 This gives the output::
       
   790 
       
   791 	:bar:ba
       
   792 	:two:th
       
   793 	ha:beta
       
   794 
       
   795 To output the fifth field through the end of the line of each line using the colon character as the field delimiter::
       
   796 
       
   797 	$ cut -d : -f 5- file
       
   798 
       
   799 This gives the output::
       
   800 
       
   801 	quux
       
   802 	five:six:seven
       
   803 	epsilon:zeta:eta:teta:iota:kappa:lambda:mu
       
   804 
       
   805 4. paste
       
   806 ---------
       
   807 
       
   808 *paste* is a Unix command line utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output. It is effectively the horizontal equivalent to the utility *cat* command which operates on the vertical plane of two or more files.
       
   809 
       
   810 To paste several columns of data together into the file *www* from files *who*, *where*, and *when*::
       
   811 
       
   812 	$ paste who where when > www
       
   813 
       
   814 If the files contain:
       
   815 
       
   816 +-----------+------------+------------+
       
   817 |   who     |   where    |    when    |
       
   818 +===========+============+============+
       
   819 |  Sam      |  Detroit   | January 3  |
       
   820 +-----------+------------+------------+	
       
   821 |  Dave     | Edgewood   | February 4 |
       
   822 +-----------+------------+------------+
       
   823 |  Sue      |  Tampa     |  March 19  |
       
   824 +-----------+------------+------------+	
       
   825 
       
   826 This creates the file named *www* containing::
       
   827 
       
   828 	Sam            Detroit         January 3
       
   829 	Dave           Edgewood        February 4
       
   830 	Sue            Tampa           March 19
       
   831 
       
   832 Shell Meta Characters
       
   833 ======================
       
   834 
       
   835 Unix recognizes certain special characters, called "meta characters," as command directives. The shell meta characters are recognized anywhere they appear in the command line, even if they are not surrounded by blank space. For that reason, it is safest to only use the characters A-Z, a-z, 0-9, and the period, dash, and underscore characters when naming files and directories on Unix. If your file or directory has a shell meta character in the name, you will find it difficult to use the name in a shell command.
       
   836 
       
   837 The shell meta characters include:
       
   838 
       
   839 \ / < > ! $ % ^ & * | { } [ ] " ' ` ~ ; 
       
   840 
       
   841 Different shells may differ in the meta characters recognized.
       
   842 
       
   843 As an example,
       
   844 ::
       
   845 
       
   846 	$ ls file.*
       
   847 
       
   848 run on a directory containing the files file, file.c, file.lst, and myfile would list the files file.c and file.lst. However,::
       
   849 
       
   850 	$ ls file.?
       
   851 
       
   852 run on the same directory would only list file.c because the ? only matches one character, no more, no less. This can save you a great deal of typing time. For example, if there is a file called california_cornish_hens_with_wild_rice and no other files whose names begin with 'c', you could view the file without typing the whole name by typing this::
       
   853 
       
   854 	$ more c*
       
   855 
       
   856 because the c* matches that long file name.
       
   857 
       
   858 Filenames containing metacharacters can pose many problems and should never be intentionally created. If you do find that you've created a file with metacharacters, and you would like to remove it, you have three options. You may use wildcards to match metacharacter, use the \  to directly enter the filename, or put the command in double quotes (except in the case of double quotes within the file name, these must be captured with one of the first two methods). For example, deleting a file named `"``*`|more`"` can be accomplished with::
       
   859 
       
   860 	$ rm ??more
       
   861 
       
   862 or::
       
   863 
       
   864 	$ rm $\backslash$*$\backslash$|more
       
   865 
       
   866 or::
       
   867 
       
   868 	$ rm ''*|more'' 
       
   869 
       
   870 
       
   871 Looking At Files
       
   872 ================
       
   873 
       
   874 1. cat
       
   875 -------
       
   876 
       
   877 The *cat* command is a standard Unix program used to concatenate and display files. The name is from "catenate", a synonym of *concatenate*.
       
   878 
       
   879 The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read.
       
   880 
       
   881 If the filename is specified as -, then *cat* will read from standard input at that point in the sequence. If no files are specified, *cat* will read from standard input entered.
       
   882 
       
   883 Extensions
       
   884 ~~~~~~~~~~
       
   885 
       
   886 Both the BSD versions of *cat* (as per the OpenBSD manpage) and the GNU coreutils version of *cat* specify the following options:
       
   887 
       
   888     `*` -b (GNU only: --number-nonblank), number non-blank output lines
       
   889 
       
   890     `*` -n (GNU only: --number), number all output lines
       
   891 
       
   892     `*` -s (GNU only: --squeeze-blank), squeeze multiple adjacent blank lines
       
   893 
       
   894     `*` -v (GNU only: --show-nonprinting), displays nonprinting characters 		as if they were visible, except for tabs and the end of line 		character
       
   895 
       
   896     `*` -t on BSD, -T on GNU, implies -v but also display tabs as ^I
       
   897 
       
   898     `*` -e on BSD, -E on GNU, implies -v but also display end-of-line 		characters as $
       
   899 
       
   900 Jargon File Definition
       
   901 ~~~~~~~~~~~~~~~~~~~~~~
       
   902 
       
   903 The Jargon File version 4.4.7 lists this as the definition of *cat*::
       
   904 
       
   905    1. To spew an entire file to the screen or some other output sink without
       
   906  	pause (syn. blast).
       
   907 
       
   908    2. By extension, to dump large amounts of data at an unprepared target or
       
   909  	with no intention of browsing it carefully. Usage: considered silly.
       
   910  	Rare outside Unix sites. See also dd, BLT.
       
   911 
       
   912 	Among Unix fans, *cat(1)* is considered an excellent example of
       
   913  	user-interface design, because it delivers the file contents without 
       
   914 	such verbosity as spacing or headers between the files, and because 
       
   915 	it does not require the files to consist of lines of text, but works 
       
   916 	with any sort of data.
       
   917 
       
   918 	Among Unix critics, *cat(1)* is considered the canonical example of 
       
   919 	bad user-interface design, because of its woefully unobvious name. 
       
   920 	It is far more often used to blast a single file to standard output 
       
   921 	than to concatenate two or more files. The name cat for the former 
       
   922 	operation is just as unintuitive as, say, LISP's cdr.
       
   923 
       
   924 	Of such oppositions are holy wars made...
       
   925 
       
   926 Useless Use of 'cat'
       
   927 ~~~~~~~~~~~~~~~~~~~~
       
   928 
       
   929 UUOC (from comp.unix.shell on Usenet) stands for “Useless Use of cat”. As received wisdom on *comp.unix.shell* observes, “The purpose of cat is to concatenate (or 'catenate') files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process.”
       
   930 
       
   931 Nevertheless one sees people doing::
       
   932 
       
   933 	$ cat file | some_command and its args ...
       
   934 
       
   935 instead of the equivalent and cheaper::
       
   936 
       
   937 	<file some_command and its args ...
       
   938 
       
   939 or (equivalently and more classically)::
       
   940 
       
   941 	some_command and its args ... <file
       
   942 
       
   943 Since 1995, occasional awards for UUOC have been given out, usually by Perl luminary Randal L. Schwartz. There is a web page devoted to this and other similar awards. In British hackerdom the activity of fixing instances of UUOC is sometimes called 'demoggification'.
       
   944 
       
   945 Amongst many, it is still considered safer to use *cat* for such cases given that the < and > keys are next to each other in many popular keyboard mappings. While the risk might be low, the impact of using > instead of < can be high and prohibitive.
       
   946 
       
   947 zcat
       
   948 ~~~~~
       
   949 
       
   950 *zcat* is a Unix program similar to *cat*, that decompresses individual files and concatenates them to standard output. Traditionally *zcat* operated on files compressed by compress but today it is usually able to operate on *gzip* or even *bzip2* archives. On such systems, it is equivalent to *gunzip -c*
       
   951 
       
   952 2. more
       
   953 --------
       
   954 
       
   955 In computing, *more* is a command to view (but not modify) the contents of a text file one screen at a time (terminal pager). It is available on Unix and Unix-like systems, DOS, OS/2 and Microsoft Windows. Programs of this sort are called pagers.
       
   956 
       
   957 History
       
   958 ~~~~~~~
       
   959 
       
   960 The *more* command was originally written by Daniel Halbert, a graduate student at the University of California, Berkeley, in 1978. It was first included in 3.0 BSD, and has since become a standard program in all Unix systems. *less*, a similar command with the extended capability of allowing both forward and backward navigation through the file was written by Mark Nudelman during 1983-85 and is now included in most Unix and Unix-like systems.
       
   961 
       
   962 Usage
       
   963 ~~~~~
       
   964 
       
   965 The command-syntax is::
       
   966 
       
   967 	$ more [options] [file_name]
       
   968 
       
   969 If no file name is provided, *more* looks for input from stdin.
       
   970 
       
   971 Once *more* has obtained input, it displays as much as can fit on the current screen and waits for user input to advance, with the exception that a form feed (^L) will also cause *more* to wait at that line, regardless of the amount of text on the screen. In the lower-left corner of the screen is displayed the text "--More--" and a percentage, representing the percent of the file that *more* has paged through. (This percentage includes the text displayed on the current screen.) When *more* reaches the end of a file (100%) it exits. The most common methods of navigating through a file are *Enter*, which advances the output by one line, and *Space*, which advances the output by one screen.
       
   972 
       
   973 There are also other commands that can be used while navigating through the document; consult *more*'s *man* page for more details.
       
   974 
       
   975 Options
       
   976 ~~~~~~~
       
   977 
       
   978 Options are typically entered before the file name, but can also be entered in the environment variable *$MORE*. Options entered in the actual command line will override those entered in the *$MORE* environment variable. Available options may vary between Unix systems, but a typical set of options is as follows:
       
   979 
       
   980     * -num: This option specifies an integer which is the screen size (in 	lines).
       
   981 
       
   982     * -d: more will prompt the user with the message "[Press space to 		  continue, 'q' to quit.]" and will display "[Press 'h' for 		  instructions.]" instead of ringing the bell when an illegal key is 		  pressed.
       
   983 
       
   984     * -l: more usually treats ^L (form feed) as a special character, and 	   will pause after any line that contains a form feed. The -l option 		  will prevent this behavior.
       
   985 
       
   986     * -f: Causes more to count logical, rather than screen lines (i.e., long 		  lines are not folded).
       
   987 
       
   988     * -p: Do not scroll. Instead, clear the whole screen and then display 		  the text.
       
   989 
       
   990     * -c: Do not scroll. Instead, paint each screen from the top, clearing 		  the remainder of each line as it is displayed.
       
   991 
       
   992     * -s: Squeeze multiple blank lines into one.
       
   993 
       
   994     * -u: Suppress underlining.
       
   995 
       
   996     * +/: The +/ option specifies a string that will be searched for before 		  each file is displayed. (Ex.: more +/Preamble gpl.txt)
       
   997 
       
   998     * +num: Start at line number num.
       
   999 
       
  1000 
       
  1001 3. less
       
  1002 --------
       
  1003 
       
  1004 *less* is a terminal pager program on Unix, Windows and Unix-like systems used to view (but not change) the contents of a text file one screen at a time. It is similar to *more*, but has the extended capability of allowing both forward and backward navigation through the file. Unlike most Unix text editors/viewers, *less* does not need to read the entire file before starting, resulting in faster load times with large files.
       
  1005 
       
  1006 History
       
  1007 ~~~~~~~~
       
  1008 
       
  1009 *less* was initially written by Mark Nudelman during 1983-85, in the need of a version of more able to do backward scrolling of the displayed text. The name came from the joke of doing "backwards more." *less* is now part of the GNU project and it is included in most Unix systems.
       
  1010 
       
  1011 Usage
       
  1012 ~~~~~~
       
  1013 
       
  1014 *less* can be invoked with options to change its behaviour, for example, the number of lines to display on the screen. A few options vary depending on the operating system. While *less* is displaying the file, various commands can be used to navigate through the file. These commands are based on those used by both *more* and *vi*. It is also possible to search for character patterns in the file.
       
  1015 
       
  1016 By default, *less* displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat.
       
  1017 
       
  1018 The command-syntax is::
       
  1019 
       
  1020 	$ less [options] file_name
       
  1021 
       
  1022 Frequently Used Options
       
  1023 ~~~~~~~~~~~~~~~~~~~~~~~
       
  1024 
       
  1025     * -g: Highlights just the current match of any searched string.
       
  1026 
       
  1027     * -I: Case-insensitive searches.
       
  1028 
       
  1029     * -M: Shows more detailed prompt, including file position.
       
  1030 
       
  1031     * -N: Shows line numbers (useful for source code viewing).
       
  1032 
       
  1033     * -S: Disables line wrap ("chop long lines"). Long lines can be seen by 		  side scrolling.
       
  1034 
       
  1035     * -?: Shows help.
       
  1036 
       
  1037 Frequently Used Commands
       
  1038 ~~~~~~~~~~~~~~~~~~~~~~~~
       
  1039 
       
  1040     * [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation.
       
  1041 
       
  1042     * [Space bar]: Next page.
       
  1043 
       
  1044     * b: Previous page.
       
  1045 
       
  1046     * ng: Jump to line number n. Default is the start of the file.
       
  1047 
       
  1048     * nG: Jump to line number n. Default is the end of the file.
       
  1049 
       
  1050     * /pattern: Search for pattern. Regular expressions can be used.
       
  1051 
       
  1052     * n: Go to next match (after a successful search).
       
  1053 
       
  1054     * N: Go to previous match.
       
  1055 
       
  1056     * mletter: Mark the current position with letter.
       
  1057 
       
  1058     * 'letter: Return to position letter. [' = single quote]
       
  1059 
       
  1060     * '^ or g: Go to start of file.
       
  1061 
       
  1062     * '$ or G: Go to end of file.
       
  1063 
       
  1064     * s: Save current content (got from another program like grep) in a file.
       
  1065 
       
  1066     * =: File information.
       
  1067 
       
  1068     * F: continually read information from file and follow its end. Useful 	    for logs watching. Use Ctrl+C to exit this mode.
       
  1069 
       
  1070     * -option: Toggle command-line option -option.
       
  1071 
       
  1072     * h: Help.
       
  1073 
       
  1074     * q: Quit.
       
  1075 
       
  1076 Examples 
       
  1077 ~~~~~~~~~
       
  1078 ::
       
  1079 
       
  1080 	$ less -M readme.txt                     #Read "readme.txt."
       
  1081 	$ less +F /var/log/mail.log              #Follow mode for log
       
  1082 	$ file * | less                          #Easier file analysis.
       
  1083 	$ grep -i void *.c | less -I -p void     #Case insensitive search 		                                          for "void" in all .c files
       
  1084 
       
  1085 Directory Structure
       
  1086 ====================
       
  1087 
       
  1088 In the File Hierarchy Standard (FHS) all files and directories appear under the root directory "/", even if they are stored on different physical devices. Note however that some of these directories may or may not be present on a Unix system depending on whether certain subsystems, such as the X Window System, are installed.
       
  1089 
       
  1090 The majority of these directories exist in all UNIX operating systems and are generally used in much the same way; however, the descriptions here are those used specifically for the FHS, and are not considered authoritative for platforms other than Linux.
       
  1091 
       
  1092 +---------------+------------------------------------------------+
       
  1093 |   Directory   |             Description                        |
       
  1094 +===============+================================================+
       
  1095 | /             | Primary hierarchy root and root directory of   |
       
  1096 |               | the entire file system hierarchy.              |
       
  1097 +---------------+------------------------------------------------+
       
  1098 | /bin/         | Essential command binaries that need to be     |
       
  1099 |               | available in single user mode; for all users,  |
       
  1100 |               | e.g., *cat*, *ls*, *cp*.                       |
       
  1101 +---------------+------------------------------------------------+
       
  1102 | /boot/        | Boot loader files, e.g., *kernels*, *initrd*;  |
       
  1103 |               | often a separate partition.                    |
       
  1104 +---------------+------------------------------------------------+
       
  1105 | /dev/         | Essential devices, e.g., /dev/null             |
       
  1106 +---------------+------------------------------------------------+
       
  1107 | /etc/         | Host-specific system-wide configuration files  |
       
  1108 |               | (the name comes from *et cetera*)              |
       
  1109 +---------------+------------------------------------------------+
       
  1110 | /home/        | User's home directories, containing saved      |
       
  1111 |               | files, personal settings, etc.; often a        |
       
  1112 |               | separate partition.                            |
       
  1113 +---------------+------------------------------------------------+
       
  1114 | /lib/         | Libraries essential for the binaries in        |
       
  1115 |               | */bin/* and */sbin/*                           |
       
  1116 +---------------+------------------------------------------------+
       
  1117 | /media/       | Mount points for removable media such as       |
       
  1118 |               | CD-ROMs, external hard disks, USB sticks, etc. |
       
  1119 +---------------+------------------------------------------------+
       
  1120 | /mnt/         | Temporarily mounted file systems               |
       
  1121 +---------------+------------------------------------------------+
       
  1122 | /opt/         | Optional application software packages         |
       
  1123 +---------------+------------------------------------------------+
       
  1124 | /proc/        | Virtual filesystem documenting kernel and      |
       
  1125 |               | process status as text files; e.g., uptime,    |
       
  1126 |               | network. In Linux, corresponds to a *Procfs*   |
       
  1127 |               | mount.                                         |
       
  1128 +---------------+------------------------------------------------+
       
  1129 | /root/        | Home directory for the root user               |
       
  1130 +---------------+------------------------------------------------+
       
  1131 | /sbin/        | Essential system binaries; e.g., *init*,       |
       
  1132 |               | *route*, *mount*.                              |
       
  1133 +---------------+------------------------------------------------+
       
  1134 | /srv/         | Site-specific data which is served by the      |
       
  1135 |               | system.                                        |
       
  1136 +---------------+------------------------------------------------+
       
  1137 | /tmp/         | Temporary files. Often not preserved between   |
       
  1138 |               | system reboots.                                |
       
  1139 +---------------+------------------------------------------------+
       
  1140 | /usr/         | Secondary hierarchy for read-only user data;   |
       
  1141 |               | contains the majority of (multi-)user          |
       
  1142 |               | utilities and applications.                    |
       
  1143 +---------------+------------------------------------------------+
       
  1144 | /var/         | Variable files - files whose content is        |
       
  1145 |               | expected to continually change during normal   |
       
  1146 |               | operation of the system - such as logs, spool  |
       
  1147 |               | files, and temporary e-mail files.             |
       
  1148 |               | Sometimes a separate partition.                |
       
  1149 +---------------+------------------------------------------------+
       
  1150 
       
  1151 
       
  1152 1. man hier
       
  1153 ------------
       
  1154 
       
  1155 This is the manual page on the UNIX filesystem. The syntax for this is::
       
  1156 
       
  1157 	$ man hier
       
  1158 
       
  1159 2. ls -l
       
  1160 ---------
       
  1161 
       
  1162 Shows you huge amounts of information (permissions, owners, size, and when last modified) for folders and files. The syntax is ::
       
  1163 
       
  1164 	$ ls -l
       
  1165 
       
  1166 This can be done after entering the required directory. 
       
  1167 
       
  1168 Permissions and Ownership
       
  1169 =========================
       
  1170 
       
  1171 1. chmod
       
  1172 ---------
       
  1173 
       
  1174 The *chmod* command (abbreviated from 'change mode') is a shell command and C language function in Unix and Unix-like environments. When executed, it can change file system modes of files and directories. The modes include permissions and special modes.A chmod command first appeared in AT&T Unix version 1, and is still used today on Unix-like machines.
       
  1175 
       
  1176 Usage
       
  1177 ~~~~~
       
  1178 
       
  1179 The *chmod* command options are specified like this:
       
  1180 ::
       
  1181 
       
  1182 	$ chmod [options] mode[,mode] file1 [file2 ...]
       
  1183 
       
  1184 To view what the permissions currently are, type:
       
  1185 ::
       
  1186 
       
  1187 	$ ls -l file
       
  1188 
       
  1189 Command line options
       
  1190 ~~~~~~~~~~~~~~~~~~~~
       
  1191 
       
  1192 The *chmod* command has a number of command line options that affect its behavior. The most common options are:
       
  1193 
       
  1194     * -R: Changes the modes of directories and files recursively
       
  1195 
       
  1196     * -v: Verbose mode; lists all files as they are being processed
       
  1197 
       
  1198 Symbolic modes
       
  1199 +++++++++++++++
       
  1200 
       
  1201 To the *chmod* utility, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text:
       
  1202 ::
       
  1203 
       
  1204 	$ chmod [references][operator][modes] file1 ...
       
  1205 
       
  1206 The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to “all” but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters:
       
  1207 
       
  1208 +--------------+--------+---------------------------------------------+
       
  1209 | Reference    | Class  |                Description                  |
       
  1210 +==============+========+=============================================+
       
  1211 |      u       | user   | the owner of the file                       |
       
  1212 +--------------+--------+---------------------------------------------+
       
  1213 |      g       | group  | users who are members of the file's group   |
       
  1214 +--------------+--------+---------------------------------------------+
       
  1215 |      o       | others | users who are not hte owner of the file or  |
       
  1216 |              |        | members of the group                        |
       
  1217 +--------------+--------+---------------------------------------------+
       
  1218 |      a       | all    | all three of the above; is the same as *ugo*|
       
  1219 +--------------+--------+---------------------------------------------+
       
  1220 
       
  1221 The *chmod* program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted:
       
  1222 
       
  1223 +--------------+------------------------------------------------------+
       
  1224 | Operator     |                      Description                     |
       
  1225 +==============+======================================================+
       
  1226 | +            | adds the specified modes to the specified classes    |
       
  1227 +--------------+------------------------------------------------------+
       
  1228 | -            | removes the specified modes from the specified       |
       
  1229 |              | classes                                              |
       
  1230 +--------------+------------------------------------------------------+
       
  1231 | =            | the modes specified are to be made the exact modes   |
       
  1232 |              | for the specified classes                            |
       
  1233 +--------------+------------------------------------------------------+
       
  1234 
       
  1235 The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions:
       
  1236 
       
  1237 +-----+--------------+------------------------------------------------+
       
  1238 |Mode |    Name      |                 Description                    |
       
  1239 +=====+==============+================================================+
       
  1240 | r   | read         | read a file or list a directory's contents     |
       
  1241 +-----+--------------+------------------------------------------------+
       
  1242 | w   | write        | write to a file or directory                   |   
       
  1243 +-----+--------------+------------------------------------------------+
       
  1244 | x   | execute      | execute a file or recurse a directory tree     |
       
  1245 +-----+--------------+------------------------------------------------+
       
  1246 | X   | special      | which is not a permission in itself but rather |
       
  1247 |     | execute      | can be used instead of 'x'. It applies execute |
       
  1248 |     |              | permissions to directories regardless of their |
       
  1249 |     |              | current permissions and applies execute        |
       
  1250 |     |              | permissions to a file which already has at     |
       
  1251 |     |              | least 1 execute permission bit already set     |
       
  1252 |     |              | (either user, group or other). It is only      |
       
  1253 |     |              | really useful when used with '+' and usually   |
       
  1254 |     |              | in combination with the *-R* option for giving |
       
  1255 |     |              | group or other access to a big directory tree  |
       
  1256 |     |              | without setting execute permission on normal   |
       
  1257 |     |              | files (such as text files), which would        |
       
  1258 |     |              | normally happen if one just used 'chmod -R     |
       
  1259 |     |              | a+rx .', whereas with 'X' one can do 'chmod -R |
       
  1260 |     |              | a+rX .' instead.                               |
       
  1261 +-----+--------------+------------------------------------------------+
       
  1262 | s   | setuid/gid   | are Unix access rights flags that allow users  |
       
  1263 |     |              | to run an executable with the permissions of   |
       
  1264 |     |              | the executable's owner or group.They are often |
       
  1265 |     |              | used to allow users on a computer system to run| 
       
  1266 |     |              | programs with temporarily elevated privileges  | 
       
  1267 |     |              | in order to perform a specific task. While the |
       
  1268 |     |              | assumed user id or group id privileges provided|
       
  1269 |     |              | are not always elevated, at a minimum they are | 
       
  1270 |     |              | specific.They are needed for tasks that require|
       
  1271 |     |              | higher privileges than those which a common    |
       
  1272 |     |              | user has, such as changing his or her login    |  
       
  1273 |     |              | password.                                      |
       
  1274 +-----+--------------+------------------------------------------------+
       
  1275 | t   | sticky       | The most common use of the sticky bit today is |
       
  1276 |     |              | on directories, where, when set, items inside  |
       
  1277 |     |              | the directory can be renamed or deleted only by|
       
  1278 |     |              | the item's owner, the directory's owner, or the| 
       
  1279 |     |              | superuser; without the sticky bit set, any user|
       
  1280 |     |              | with write and execute permissions for the     |
       
  1281 |     |              | directory can rename or delete contained files,| 
       
  1282 |     |              | regardless of owner.                           |
       
  1283 +-----+--------------+------------------------------------------------+
       
  1284 
       
  1285 The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas.
       
  1286 
       
  1287 Symbolic examples
       
  1288 +++++++++++++++++
       
  1289 
       
  1290 Add the 'read' and 'write' permissions to the 'user' and 'group' classes of a directory:
       
  1291 ::
       
  1292 
       
  1293 	$ chmod ug+rw mydir
       
  1294 	$ ls -ld mydir
       
  1295 	drw-rw----   2 starwars  yoda  96 Dec 8 12:53 mydir
       
  1296 
       
  1297 For a file, remove *write* permissions for all classes:
       
  1298 ::
       
  1299 
       
  1300 	$ chmod a-w myfile
       
  1301 	$ ls -l myfile
       
  1302 	-r-xr-xr-x   2 starwars  yoda 96 Dec 8 12:53 myfile
       
  1303 
       
  1304 Set the permissions for the *u*ser and the *g*roup to read and execute only (no write permission) on *mydir*.
       
  1305 ::
       
  1306 
       
  1307 	$ chmod ug=rx mydir
       
  1308 	$ ls -ld mydir
       
  1309 	dr-xr-x---   2 starwars  yoda 96 Dec 8 12:53 mydir
       
  1310 
       
  1311 Octal numbers
       
  1312 +++++++++++++
       
  1313 
       
  1314 The *chmod* command also accepts three and four-digit octal numbers representing modes. Using a three-digit octal number to set the modes of a file named myfile :
       
  1315 ::
       
  1316 
       
  1317 	$ chmod 664 myfile
       
  1318 	$ ls -l myfile
       
  1319 	-rw-rw-r--  1   57 Jul  3 10:13  myfile
       
  1320 
       
  1321 Since the *setuid*, *setgid* and *sticky* bits are not set, this is equivalent to:
       
  1322 ::
       
  1323 
       
  1324 	$ chmod 0664 myfile
       
  1325 
       
  1326 Special modes
       
  1327 +++++++++++++
       
  1328 
       
  1329 The *chmod* command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use **s** to represent the *setuid* and *setgid* modes, and **t** to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified.
       
  1330 
       
  1331 Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used.
       
  1332 
       
  1333 Redirection and Piping
       
  1334 =======================
       
  1335 
       
  1336 In computing, *redirection* is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.
       
  1337 
       
  1338 Programs do redirection with the *dup2(2)* system call, or its less-flexible but higher-level stdio analogues, *freopen(3)* and *popen(3)*.
       
  1339 
       
  1340 Redirecting standard input and standard output
       
  1341 -----------------------------------------------
       
  1342 
       
  1343 Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows::
       
  1344 
       
  1345 	$ command1 > file1
       
  1346 
       
  1347 executes *command1*, placing the output in file1. Note that this will truncate any existing data in *file1*. To append output to the end of the file, use the >> operator.::
       
  1348 
       
  1349 	$ command1 < file1
       
  1350 
       
  1351 executes *command1*, using *file1* as the source of input (as opposed to the keyboard).::
       
  1352 
       
  1353 	$ command1 < infile > outfile
       
  1354 
       
  1355 combines the two capabilities: *command1* reads from *infile* and writes to *outfile*
       
  1356 
       
  1357 Piping
       
  1358 -------
       
  1359 
       
  1360 Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file:
       
  1361 A pipeline of three programs run on a text terminal::
       
  1362 
       
  1363 	$ command1 | command2
       
  1364 
       
  1365 executes *command1*, using its output as the input for *command2* (commonly called piping, since the "|" character is known as a "pipe").
       
  1366 
       
  1367 This is equivalent to using two redirects and a temporary file::
       
  1368 
       
  1369 	$ command1 > tempfile
       
  1370 	$ command2 < tempfile
       
  1371 	$ rm tempfile
       
  1372 
       
  1373 A good example for command piping is combining *echo* with another command to achieve something interactive in a non-interactive shell, e.g.::
       
  1374 
       
  1375 	$ echo -e "user\npass" | ftp localhost
       
  1376 
       
  1377 This runs the ftp client with input user, press return, then pass.
       
  1378 
       
  1379 Redirecting to and from the standard file handles
       
  1380 --------------------------------------------------
       
  1381 
       
  1382 In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are:
       
  1383 
       
  1384 +------------+-------------+------------------------+
       
  1385 |   Handle   |    Name     |      Description       |
       
  1386 +============+=============+========================+
       
  1387 | 0          |   stdin     |    Standard input      |
       
  1388 +------------+-------------+------------------------+
       
  1389 | 1          |   stdout    |    Standard output     |
       
  1390 +------------+-------------+------------------------+
       
  1391 | 2          |   stderr    |    Standard error      |
       
  1392 +------------+-------------+------------------------+
       
  1393 
       
  1394 For example:
       
  1395 ::
       
  1396 
       
  1397 	$ command1 2> file1
       
  1398 
       
  1399 executes *command1*, directing the standard error stream to *file1*.
       
  1400 
       
  1401 In shells derived from *csh* (the C shell), the syntax instead appends the & character to the redirect characters, thus achieving a similar result.
       
  1402 
       
  1403 Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge standard error into standard output so error messages can be processed together with (or alternately to) the usual output. Example:
       
  1404 ::
       
  1405 
       
  1406 	$ find / -name .profile > results 2>&1
       
  1407 
       
  1408 will try to find all files named *.profile*. Executed without redirection, it will output hits to *stdout* and errors (e.g. for lack of privilege to traverse protected directories) to *stderr*. If standard output is directed to file results, error messages appear on the console. To see both hits and error messages in file results, merge *stderr* (handle 2) into *stdout* (handle 1) using 2>&1 .
       
  1409 
       
  1410 It's possible use 2>&1 before ">" but it doesn't work. In fact, when the interpreter reads 2>&1, it doesn't know yet where standard output is redirected and then standard error isn't merged.
       
  1411 
       
  1412 If the merged output is to be piped into another program, the file merge sequence 2>&1 must precede the pipe symbol, thus:
       
  1413 ::
       
  1414 
       
  1415 	$ find / -name .profile 2>&1 | less
       
  1416 
       
  1417 A simplified form of the command:
       
  1418 ::
       
  1419 
       
  1420 	$ command > file 2>&1
       
  1421 
       
  1422 is:
       
  1423 ::
       
  1424 
       
  1425 	$ command &>file
       
  1426 
       
  1427 or:
       
  1428 ::
       
  1429 
       
  1430 	$command >&file
       
  1431 
       
  1432 Chained pipelines
       
  1433 ------------------
       
  1434 
       
  1435 The redirection and piping tokens can be chained together to create complex commands. For example:
       
  1436 ::
       
  1437 
       
  1438 	$ ls | grep '\.sh' | sort > shlist
       
  1439 
       
  1440 lists the contents of the current directory, where this output is filtered to only contain lines which contain *.sh*, sort this resultant output lexicographically, and place the final output in *shlist*. This type of construction is used very commonly in shell scripts and batch files.
       
  1441 
       
  1442 Redirect to multiple outputs
       
  1443 -----------------------------
       
  1444 
       
  1445 The standard command *tee* can redirect output from a command to several destinations.
       
  1446 ::
       
  1447 
       
  1448 	$ ls -lrt | tee xyz
       
  1449 
       
  1450 This directs the file list output to both standard output as well as to the file *xyz*.
       
  1451 
       
  1452 More Text Processing
       
  1453 ====================
       
  1454 
       
  1455 1. grep
       
  1456 --------
       
  1457 
       
  1458 *grep* is a command line text search utility originally written for Unix. The name is taken from the first letters in *global / regular expression / print*, a series of instructions for the *ed* text editor. The *grep* command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
       
  1459 
       
  1460 Usage
       
  1461 ~~~~~~
       
  1462 
       
  1463 This is an example of a common *grep* usage:
       
  1464 ::
       
  1465 
       
  1466 	$ grep apple fruitlist.txt
       
  1467 
       
  1468 In this case, *grep* prints all lines containing 'apple' from the file *fruitlist.txt*, regardless of word boundaries; therefore lines containing 'pineapple' or 'apples' are also printed. The *grep* command is case sensitive by default, so this example's output does not include lines containing 'Apple' (with a capital A) unless they also contain 'apple'.
       
  1469 
       
  1470 Like most Unix commands, *grep* accepts command line arguments to change this and many other behaviors. For example:
       
  1471 ::
       
  1472 
       
  1473 	$ grep -i apple fruitlist.txt
       
  1474 
       
  1475 This prints all lines containing 'apple' regardless of capitalization. The '-i' argument tells *grep* to be case insensitive, or to ignore case.
       
  1476 
       
  1477 To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match):
       
  1478 ::
       
  1479 
       
  1480 	$ grep -w apple fruitlist.txt
       
  1481 
       
  1482 Regular expressions can be used to match more complicated queries.
       
  1483 
       
  1484 Variations
       
  1485 +++++++++++
       
  1486 
       
  1487 There are countless implementations and derivatives of *grep* available for many operating systems. Early variants of *grep* included *egrep* and *fgrep*. The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern *grep* implementations as command-line switches (and standardized as -E and -F in POSIX). In such combined implementations, *grep* may also behave differently depending on the name by which it is invoked, allowing *fgrep*, *egrep*, and *grep* to be links to the same program.
       
  1488 
       
  1489 *pcregrep* is an implementation of *grep* that uses Perl regular expression syntax.
       
  1490 
       
  1491 Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The *pgrep* utility, for instance, displays the processes whose names match a given regular expression.
       
  1492 
       
  1493 In Perl, *grep* is a built-in function that finds elements in a list. In functional programming languages, this higher-order function is typically named "filter" instead.
       
  1494 
       
  1495 The DOS, OS/2 and Microsoft Windows platforms provide the find command for simple string searches. Windows includes the "findstr" command which approximates much of the functionality of “grep”. Ports of grep (Cygwin and GnuWin32, for example) are also available for Windows.
       
  1496 
       
  1497 Adobe added support for grep in the CS4 version of InDesign. Their support allow for finding and changing the formatting of text or the text itself in a document using *grep*.
       
  1498 
       
  1499 2. tr
       
  1500 ------
       
  1501 
       
  1502 *tr* (abbreviated from *translate* or *transliterate*) is a command in Unix-like operating systems.
       
  1503 
       
  1504 When executed, the program reads from the standard input and writes to the standard output. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set. For example,
       
  1505 ::
       
  1506 
       
  1507 	$ tr 'abcd' 'jkmn' 
       
  1508 
       
  1509 maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'.
       
  1510 
       
  1511 Sets of characters may be abbreviated by using character ranges. The previous example could be written:
       
  1512 ::
       
  1513 
       
  1514 	$ tr 'a-d' 'jkmn'
       
  1515 
       
  1516 In POSIX compliant versions of *tr* the set represented by a character range depends on the locale's collating order, so it is safer to avoid character ranges in scripts that might be executed in a locale different from that in which they were written. Ranges can often be replaced with POSIX character sets such as [:alpha:].
       
  1517 
       
  1518 The *-c* flag complements the first set of characters.
       
  1519 ::
       
  1520 
       
  1521 	$ tr -cd '[:alnum:]' 
       
  1522 
       
  1523 therefore removes all non-alphanumeric characters.
       
  1524 
       
  1525 The *-s* flag causes tr to compress sequences of identical adjacent characters in its output to a single token. For example,
       
  1526 ::
       
  1527 
       
  1528 	$ tr -s '\n' '\n'
       
  1529 
       
  1530 replaces sequences of one or more newline characters with a single newline.
       
  1531 
       
  1532 The *-d* flag causes tr to delete all tokens of the specified set of characters from its input. In this case, only a single character set argument is used. The following command removes carriage return characters, thereby converting a file in DOS/Windows format to one in Unix format.
       
  1533 ::
       
  1534 
       
  1535 	$ tr -d '\r'
       
  1536 
       
  1537 Most versions of *tr*, including GNU *tr* and classic Unix *tr*, operate on single byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support.
       
  1538 
       
  1539 Ruby and Perl also have an internal *tr* operator, which operates analogously. Tcl's *string map* command is more general in that it maps strings to strings while *tr* maps characters to characters.
       
  1540 
       
  1541 Elementary Regex
       
  1542 =================
       
  1543 
       
  1544 In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. A regular expression (often shortened to regex or regexp) is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification.
       
  1545 
       
  1546 Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. For example, Perl, Ruby and Tcl have a powerful regular expression engine built directly into their syntax. Several utilities provided by Unix distributions—including the editor *ed* and the filter *grep* — were the first to popularize the concept of regular expressions.
       
  1547 
       
  1548 Traditional Unix regular expression syntax followed common conventions but often differed from tool to tool. The IEEE POSIX Basic Regular Expressions (BRE) standard (released alongside an alternative flavor called Extended Regular Expressions or ERE) was designed mostly for backward compatibility with the traditional (Simple Regular Expression) syntax but provided a common standard which has since been adopted as the default syntax of many Unix regular expression tools, though there is often some variation or additional features. Many such tools also provide support for ERE syntax with command line arguments.
       
  1549 
       
  1550 In the BRE syntax, most characters are treated as literals — they match only themselves (i.e., a matches "a"). The exceptions, listed below, are called metacharacters or metasequences.
       
  1551 
       
  1552 +-------------+------------------------------------------------------------+
       
  1553 |Metacharacter|                            Description                     |
       
  1554 +=============+============================================================+
       
  1555 | .           | Matches any single character (many applications exclude    | 
       
  1556 |             | newlines, and exactly which characters are considered      | 
       
  1557 |             | newlines is flavor, character encoding, and platform       |
       
  1558 |             | specific, but it is safe to assume that the line feed      |
       
  1559 |             | character is included). Within POSIX bracket expressions,  |
       
  1560 |             | the dot character matches a literal dot. For example, a.c  |
       
  1561 |             | matches abc, etc., but [a.c] matches only a, ., or         |
       
  1562 |             | c.                                                         |
       
  1563 +-------------+------------------------------------------------------------+
       
  1564 | [ ]         | A bracket expression. Matches a single character that is   | 
       
  1565 |             | contained within the brackets. For example, [abc] matches  |
       
  1566 |             | a, b, or c. [a-z] specifies a range which matches any      |
       
  1567 |             | lowercase letter from a to z. These forms can be mixed:    |
       
  1568 |             | [abcx-z] matches a, b, c, x, y, or z, as does              |
       
  1569 |             | [a-cx-z]. The - character is treated as a literal character|
       
  1570 |             | if it is the last or the first character within the        |
       
  1571 |             | brackets, or if it is escaped with a backslash: [abc-],    |
       
  1572 |             | [-abc], or [a\-bc].                                        |
       
  1573 +-------------+------------------------------------------------------------+
       
  1574 | [^ ]        | Matches a single character that is not contained within the|
       
  1575 |             | brackets. For example, [^abc] matches any character other  |
       
  1576 |             | than a, b, or c. [^a-z] matches any single character       |
       
  1577 |             | that is not a lowercase letter from a to z. As above,      |
       
  1578 |             | literal characters and ranges can be mixed.                |
       
  1579 +-------------+------------------------------------------------------------+
       
  1580 | ^           | Matches the starting position within the string. In        |
       
  1581 |             | line-based tools, it matches the starting position of any  |
       
  1582 |             | line.                                                      |
       
  1583 +-------------+------------------------------------------------------------+
       
  1584 | $           | Matches the ending position of the string or the position  |
       
  1585 |             | just before a string-ending newline. In line-based tools,  |
       
  1586 |             | it matches the ending position of any line.                |
       
  1587 +-------------+------------------------------------------------------------+
       
  1588 | `*`         | Matches the preceding element zero or more times. For      |
       
  1589 |             | example, ab*c matches "ac", "abc", "abbbc", etc. [xyz]*    |
       
  1590 |             | matches "", "x", "y", "z", "zx", "zyx", "xyzzy", and so on.|
       
  1591 |             | \(ab\)* matches "", "ab", "abab", "ababab", and so on.     |
       
  1592 +-------------+------------------------------------------------------------+
       
  1593 | ?           | Matches the preceding element zero or one time. For        |
       
  1594 |             | example, ba? matches "b" or "ba".                          |
       
  1595 +-------------+------------------------------------------------------------+
       
  1596 | `+`         | Matches the preceding element one or more times. For       |
       
  1597 |             | example, ba+ matches "ba", "baa", "baaa", and so on.       |
       
  1598 +-------------+------------------------------------------------------------+
       
  1599 | `|`         | The choice (aka alternation or set union) operator matches |
       
  1600 |             | either the expression before or the expression after the   |
       
  1601 |             | operator. For example, abc|def matches "abc" or "def".     |
       
  1602 +-------------+------------------------------------------------------------+
       
  1603 
       
  1604 Lazy quantification
       
  1605 --------------------
       
  1606 
       
  1607 The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex. For example, someone new to regexes wishing to find the first instance of an item between < and > symbols in this example:
       
  1608 ::
       
  1609 
       
  1610 	Another whale explosion occurred on <January 26>, <2004>.
       
  1611 
       
  1612 ...would likely come up with the pattern <.*>, or similar. However, this pattern will actually return "<January 26>, <2004>" instead of the "<January 26>" which might be expected, because the `*` quantifier is greedy — it will consume as many characters as possible from the input, and "January 26>, <2004" has more characters than "January 26".
       
  1613 
       
  1614 Though this problem can be avoided in a number of ways (e.g., by specifying the text that is not to be matched: <[^>]*>), modern regular expression tools allow a quantifier to be specified as lazy (also known as non-greedy, reluctant, minimal, or ungreedy) by putting a question mark after the quantifier (e.g., <.*?>), or by using a modifier which reverses the greediness of quantifiers (though changing the meaning of the standard quantifiers can be confusing). By using a lazy quantifier, the expression tries the minimal match first. Though in the previous example lazy matching is used to select one of many matching results, in some cases it can also be used to improve performance when greedy matching would require more backtracking.
       
  1615 
       
  1616 One Liners
       
  1617 ===========
       
  1618 
       
  1619 A *one-liner* is textual input to the command-line of an operating system shell that performs some function in just one line of input.
       
  1620 
       
  1621 The one liner can be
       
  1622 
       
  1623    1. An expression written in the language of the shell.
       
  1624    2. The invocation of an interpreter together with program source for the  	   interpreter to run.
       
  1625    3. The invocation of a compiler together with source to compile and 	  
       
  1626       instructions for executing the compiled program.
       
  1627 
       
  1628 Certain dynamic scripting languages such as AWK, sed, and perl have traditionally been adept at expressing one-liners. Specialist shell interpreters such as these Unix shells or the Windows PowerShell, allow for the construction of powerful one-liners.
       
  1629 
       
  1630 The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.
       
  1631 
       
  1632 The word *One-liner* has two references in the index of the book *The AWK Programming Language* (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix operating system. The authors explain the birth of the One-liner paradigm with their daily work on early Unix machines:
       
  1633 ::
       
  1634 
       
  1635     “The 1977 version had only a few built-in variables and predefined functions. It was designed for writing short programs [...] Our model was that an invocation would be one or two lines long, typed in and used immediately. Defaults were chosen to match this style [...] We, being the authors, knew how the language was supposed to be used, and so we only wrote one-liners.”
       
  1636 
       
  1637 Notice that this original definition of a One-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a One-liner. But this strict understanding of a One-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C, which is a compiled language.
       
  1638 
       
  1639 The TAPL book contains 20 examples of One-liners (A Handful of Useful awk One-Liners) at the end of the book's first chapter.
       
  1640 
       
  1641 Here are the very first of them:
       
  1642 
       
  1643    1. Print the total number of input lines:
       
  1644 
       
  1645       END { print NR }
       
  1646 
       
  1647    2. Print the tenth input line:
       
  1648 
       
  1649       NR == 10
       
  1650 
       
  1651    3. Print the last field of every input line:
       
  1652 
       
  1653       { print $NF }
       
  1654 
       
  1655 Here are examples in J:
       
  1656 
       
  1657    1. A function avg to return the average of a list of numbers:
       
  1658 
       
  1659       avg=: +/ % #
       
  1660 
       
  1661    2. Quicksort:
       
  1662 
       
  1663       quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)
       
  1664 
       
  1665 
       
  1666 Many one-liners are practical. For example, the following Perl one-liner will reverse all the bytes in a file:
       
  1667 
       
  1668 	perl -0777e 'print scalar reverse <>' filename
       
  1669 
       
  1670 One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner.
       
  1671 
       
  1672 The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC, here split to two lines for presentation).::
       
  1673 	
       
  1674 	main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return
       
  1675 	*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}
       
  1676 
       
  1677 This one-liner program is a glob pattern matcher. It understands the glob characters '*' meaning 'zero or more characters' and '?' meaning exactly one character, just like most Unix shells.
       
  1678 
       
  1679 Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples::
       
  1680 
       
  1681 	$ prog foo 'f??'; echo $?
       
  1682 
       
  1683 	$ prog 'best short program' '??st*o**p?*'; echo $?
       
  1684 
       
  1685 Here is a one line shell script to show directories:
       
  1686 
       
  1687 ::
       
  1688 
       
  1689 	$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/' 
       
  1690 
       
  1691 
       
  1692 One liners in functional programming
       
  1693 -------------------------------------
       
  1694 
       
  1695 The following Haskell program is a one-liner: it sorts its input lines asciibetically.
       
  1696 ::
       
  1697 
       
  1698 	 main = (mapM_ putStrLn . List.sort . lines) =<< getContents 
       
  1699 
       
  1700 -- In ghci a qualified name like List.sort will work, although as a standalone executable you'd need to import List.
       
  1701 
       
  1702 An even shorter version.
       
  1703 ::
       
  1704  
       
  1705 	main = interact (unlines . List.sort . lines) -- Ditto.
       
  1706 
       
  1707 Python is well suited for writing one liners using lambda functions without yielding obfuscated code. Here is a one liner in Python that multiplies two matrices (represented as a list of lists)
       
  1708 ::
       
  1709 
       
  1710 	z = lambda x, y : [[ sum([x[i] [k]*y[k] [j] for k in 
       
  1711 	range(len(x[0]))]) for j in range(len(y[0]))] for i in range(len(x))]
       
  1712 
       
  1713 Another, that prints all primes within the specified range [2, n] :
       
  1714 ::
       
  1715 
       
  1716 	z = lambda n : [x for x in range(2, n + 1) if len([i for i in 
       
  1717 	range(2, x) if x%i == 0]) == 0]
       
  1718 
       
  1719 This performs a Discrete Time Convolution on two input lists and yields a new list
       
  1720 ::
       
  1721 
       
  1722 	z = lambda x, h : [sum((x[i - j] if i - j >= 0 and i - j < len(x) 	  else 0)*h[j]
       
  1723  	 for j in range(len(h)))
       
  1724  	 for i in range(len(x) + len(h) - 1)]
       
  1725 
       
  1726