Flash cards UNIX
If you want to extract columns of text, then the cut command provides two simple techniques: WHAT ARE THEY
By delimiter, where whitespace is the default. The -d option can let you specify other delimiters and -f is used to indicate which fields to extract. By character position, using the -c option with the range of the column to extract.
Useful options: -group -group payroll
Find any files owned bye the group specified. (used with the find command) finds only files owned by the payroll group
What will the option -maxdepth do with the find command?
Search specified directory and its immediate sub directories
Three Input/Output (I/O)
Standard Input (STDIN) is normally provided by the user via the keyboard. Standard Output (STDOUT) is the output produced by the command when operating correctly. STDOUT normally appears in the same window as where command executed. Standard Error (STERR) is the is the output produced by the command when an error has occurred. STDOUT normally appears in the same window as where command executed.
less command
a pager command designed to display only one page of data at a time. allow the user to move back and forth with movement commands to view one page at a time.
head -n
allows for the number of lines to be displayed to be specified. (used with the head command)
Useful options: -inmae -iname hosts
case insensitive search by filename (used with the find command)
head command
displays the first ten lines of a file by default
tail command
displays the last ten lines of a file by default
Useful options: -user
find any file owned by the user specified (used with the find command) ex. -user bob = finds files owned by the user bob
Useful options: -type
find only regular files (used with the find command) ex. -type f
Useful options: -mmin
finds any files in the interval input (used with the find command) ex. -mmin -10 = finds any files that have be modified in the last ten mintes
more command
is another pager command that has less features than the less command allow the user to move back and forth with movement commands to view one page at a time
tail -f
monitors changes to a file and prints them out as they occur by using this option. (must be terminated by using CTRL-C)
STDIN - 0
normally is provided by the keyboard but can be redirected with the < symbol.
tail -n
option of the tail command that allows for the number of lines to be displayed to be specified
wc command
outputs up to three statistics for each file it is given as an argument.
The Null Device ex. /dev/null
represented by the /dev/null file. (Otherwise known as the "Bit Bucket") This file is very useful in redirection of input and output. This file serves two purposes: any output redirected to /dev/null is discarded. /dev/null can be used for input to provide a stream of null values.
-ls /etc | head - pipe char (l) -
used between two commands to send the output of the first as input to the second:
Search by file size: -size
used with the find command to search by file size. (used with the find command) Units can be specified as K,M, G, ect. use +1M means more that one megabyte use -1M means less then one megabyte
The sort command
will rearrange its output lines according to one or more fields you specify for sorting.
STDERR - 2
• Standard Error (STDERR) is the output of a command after an error has occurred. • It is normally sent to the console/terminal where the command is executed. • ls /fake is a command that will cause an error to be output to STDERR because the /fake file does not exist.
STDOUT - 1
• Standard Out (STDOUT) is the output from the command when operating correctly. • It is usually displayed in the same window where the command is executed. • The echo command is used to print messages to STDOUT. • It can be used to demonstrate how STDOUT can be redirected, as shown on the following slide.
BRE: Combining ^ and $
•Combining both the ^ and $ characters allows for two special matches: -'^$' is a blank line match. -'^pattern$ matches if the whole line contains only the "pattern". -'^pattern$ matches if the whole line contains only the "pattern".
Searching by file name
•Consider the following command: find /etc/pki -name "*.crt" •Begins searching recursively from the /etc/pki directory •Output any file names that match "*.crt" (anything that ends in ".crt").
BRE: the $ example
•The $ (dollar sign), when appearing at the end of the pattern, means that pattern must appear at the end of the line. •The $ not at the beginning of a pattern matches itself.
BRE: the * example
•The * (asterisk) character will match zero or more of the previous character. •Matching "a*" is not very useful because it might match zero a's (matches every line). •Matching "abcd*" would be more useful, since you would need an "abc" followed by zero or more d's.
ERE: the + example
•The + (plus) character will match one or more of the previous character. •Matching "a+" is useful because it can match one or more a's, ensuring only lines that have at least one "a" are matched.
ERE: the ? example
•The ? (question mark) character will optionally match one of the previous character. •The ? character is useful for matching characters that only occasionally appear in a word. The following example illustrates this:
BRE: the [ ] example
•The [ ] (brackets) characters are used to match exactly one character. •The characters can be listed or given as a range. •If the first character listed is ^ (caret), then it means not the characters bracketed.
BRE: the ^ example
•The ^ (caret) character, when appearing at the beginning of the pattern, means that pattern must appear at the beginning of the line. •The ^ not at the beginning of a pattern matches itself.
Searching with find command
•The filesystem has hundreds of directories with thousands of files making finding files challenging. •The find command is a powerful tool to be able to search for files in different ways including: -name -size -date -oqnership
Syntax of find command
•The find command has the following syntax: -find [start_dir] [search_op] [criteria] [result] •If the starting directory (start_dir) is not specified, then the current directory is assumed. •The search option (search_op) is how the search will be done. For example, use the -name option to search by name.
STDERR and STDOUT Example
•The following example demonstrates the find command searching recursively the /etc/pki directory for any files matching "*.crt".
Syntax of find command (cont'd)
•The search criteria (criteria) is the data to be used with the search option. So, if the search option was -name, then the search criteria would be the name of the file to find. •The result option (result) defaults to -print, which will output the names of the files that are found. Other result options can perform actions on the files that are found.
Extended Regular Expressions
•The use of Extended Regular Expressions (ERE) requires the -E option when using the grep command. •Extended Regular Expressions can be combined with Basic Regular Expressions. •The following are ERE characters: ?, +, and |
The xargs command
•The xargs command helps complex piped command sets execute more efficiently •It attempts to build the longest command line possible with as many arguments as possible •It tries to prevent executing the command each time for every argument
ERE: the | example
•The | (vertical bar) character will act like an "or" operator between two regular expressions. •This alternation operator is useful to be able to match multiple patterns: