Ch. 7 Linux
text
A core philosophy of UNIX and Linux is that commands should read _____ as input and write ____ as output.
10
By default the head command will display the first ____ lines of a file's contents.
1,000
By default, the split command will break apart a file into chunks of how many lines?
wc
Command that can be used to analyze a text file. By default, it counts the number of lines, words, and byte counts in a passage and outputs this information in the following format: lines words bytes filename
tr
Command that can be used to translate from one set of characters to another.
od
Command that can output data in several different formats. It produces an octal dump by default. One use of this command is to display the contents of a file when it contains non-printable characters, such as control characters. It can also be used for file recovery, or to open a file that crashes other programs.
cat
Command that can output the contents of a text file.
split
Command that can split a file into multiple files (opposite of the cat command).
cut
Command that extracts fields of information from a text file.
sort
Command used to display a file sorted on a specific field of data.
sort
Command useful for working with data organized in columns. Used to display a file sorted on a specific field of data.
prefix: x suffix: aa, ab, etc.
Default files created by the split command will be created by a prefix of _ and an alphabetical suffix of ____.
sed '/localhost/d' /etc/hosts
Delete the lines in the /etc/hosts file that contain 'localhost'
cat /etc/hosts | tr 'a-z' 'A-Z'
Display the /etc/hosts file with all of the letters capitalized.
man ls | head -n 20 (or man ls | head -20)
Display the 1st 20 lines of the output of the man file for ls.
tail -n +3 /etc/hosts
Display the contents of the /etc/hosts file starting from 3rd line.
head -15 /usr/share/dict/words (or head -n 15 /usr/share/dict/words)
Display the first 15 lines of the /usr/share/dict/words file.
tail -5 /usr/share/dict/words
Display the last 5 lines of the /usr/share/dict/words file.
ls -ltr | tail -5 (or ls -ltr | tail -n 5)
Display the last 5 lines of the output of the ls -ltr command.
nl -ba newhome.txt
Display the newhome.txt file contents with all lines (including blank lines) numbered.
sort -u output
Display the sorted results of the output file but with no duplicates.
cut -d: -f1,3,4 /etc/passwd | head -n 4
Extract the 1st,3rd,and 4th fields from the /etc/passwd file (which uses a colon delimiter) and display the first 4 lines.
ls -l /etc | head | cut -c31-43
Extract the first 10 lines of the ls -l /etc command, extracting only the contents from columns 31 through 43.
less alpha-first.txt
How would you display the alpha-first.txt file using the less pager?
cat alpha-first.txt | tr -d 'AEIOUaeiou'
How would you display the contents of alpha-first.txt with all of the vowels deleted?
tail -n +20 alpha.txt
How would you display the contents of the alpha.txt file from line 20 to the end?
paste numbers.txt letters.txt
How would you display the contents of the numbers.txt and then letters.txt files right next to each other, line by line?
tail -3 alpha.txt (or tail -n3 alpha.txt)
How would you display the last 3 lines of the alpha.txt file?
tail -3 alpha.txt | nl
How would you display the last 3 lines of the file alpha.txt with numbered lines?
cat alpha-first.txt | tr 'a-z' 'A-Z'
How would you display the output of the alpha-first.txt file using all uppercase letters?
wc *
How would you display the wc information for all the files in the current directory?
head -1 /etc/passwd | cut -d: -f1,5,6,7 (or head -1 /etc/passwd | cut -d: -f1,5-7)
How would you extract just fields 1, 5, 6, and 7 from the first line of the /etc/passwd file? The /etc/passwd first line is separated by colons.
ls | head -5
How would you list just the first 5 files in the current directory?
cat -n
How would you number every line using the cat command?
paste -d : head tail > total
How would you paste the head and tail files together using a colon delimiter, storing the result in a file called total?
sed 's/Animal/Apple/' alpha-first.txt
How would you replace the word 'Animal' with 'Apple in alpha-first.txt?
sort -t',' -k1n os.csv
How would you sort by the 1st field of os.csv (which is delimited by commas) by numerical order rather than ASCII ordering?
sort people.csv
How would you sort the lines of people.csv displayed in ASCII order.
paste -d ','
How would you specify a comma be the delimiter for the paste command?
join -1 2 -2 3 -t',' people.csv os.csv
How would you specify for the join command that you want to join the 2nd field of the 1st file with the 3rd field of the 2nd file, with a comma delimiter in each file rather than a space? The two file names are people.csv and os.csv.
split longfile.txt file.
How would you split a file named longfile.txt with a prefix of file.
split words -d result ls result*
How would you split up the words file into other files that look like this: result00, result01, result02, etc., then display the resulting files?
The 1st 10 lines (omitting the last 90 lines)
If a file was 100 lines long and you input the head -n -90 command, what would be shown?
sed '/1 retriever/c\1 cat' animals.txt (note that the whole line is changed, not just the search term)
If a line of animals.txt contains '1 retriever', how would you change any such lines to say '1 cat'?
sed '/127.0.0.1/i\Report' /etc/hosts
Insert the word Report in the /etc/hosts file preceding the line containing 127.0.0.1
sed '/allrouters/a\End of report' /etc/hosts
Insert the words 'End of report' in the /etc/hosts file after the line containing 'allrouters'
split
One use of this command is to take a file that is too large to fit on some kind of removable media and break the file apart so that each piece could be stored on separate media. Another use is to break a large file into smaller pieces so it can be transferred across a slow network with connection issues. The cat command can be used to reassemble the pieces into a single file again.
head -5 words > head tail -5 words > tail paste head tail > total cat total
Put the 1st 5 lines of the words file into a file called head, the last 5 lines of the words file into a file called tail, then display the output head and tail side by side in a file called total.
sed 's/host/NAME/g' /etc/hosts
Replace all instances of the string 'host' with the string 'NAME' in /etc/hosts
sort -k2 adjectives.txt
Sort the adjectives.txt column based on the 2nd field (column).
sort -t: -k1r /etc/passwd | head -n 4
Sort the etc/passwd output using the colon as the delimiter, sorting based on the 1st field. Only display the 1st 4 lines and display the results in descending (reverse) order.
sort -t: -k3n /etc/passwd | head -n 4
Sort the etc/passwd output using the colon delimiter, sorting based on the third field, which is a number. Sort numerically instead of using ASCII. Display only the 1st 4 lines.
sort output | uniq -c
Sort the results of the output file and display no duplicates. However, each line should display the number of duplicates of that line.
split words result -l 500
Split the words file into chunks of 500 lines each (instead of the default 1,000 line file) with prefix of result and suffix aa, ab, etc.
sed
Stream editor command that is a non-interactive editor that can be used to modify text.
-l, -b
The ___ split command option can be used to specify the number of lines to split upon while the __ option can be used to specify the maximum number of bytes to use per file.
sed 's/oo/00/g' food.txt (add /g, the global modifier after the final slash of the search/replace)
The file food.txt contains the line: Food is good. How would you replace all of the oo pattern in this file with 00 (rather than just the first instance of oo)?
sort -t',' -k2 os.csv
The os.csv file contains text delimited by commas. How would you sort this file based on the 2nd field in each line? Example: a line might look like: 1991,Linux,Torvalds
head
The purpose of this command is to view the beginning of a file or output.
more, less
There are two pager commands, allowing us to display only one page at a time. What are they?
join
This command combines two fields based on a common field between the files, similar to how it's done in SQL.
tail
This command displays contents from the end of the file, rather than the beginning.
paste
This command will merge the lines of one or more files, line by line, separating them with a tab as a delimiter by default.
less
This is the pager generally used for man pages.
cat /etc/hosts /etc/hostname > result
concatenate the /etc/hosts /etc/hostname and store the result in a new file named result
-ba (--body-numbering=a)
nl command option to number every line in a text file (including empty lines).
file offsets
non-negative integers that specify a point within a file that is a specific number of bytes from a known position.
-c
od option that displays ASCII characters or backslash escapes
-s
od option that displays decimal 2-byte units
-i
od option that displays decimal integers
-l
od option that displays decimal longs
-f
od option that displays floats
-x
od option that displays hexadecimal 2-byte units
-a
od option that displays named characters, ignoring high bit
-o
od option that displays octal 2-byte units
-b
od option that displays octal bytes
-d
od option that displays unsigned decimal 2-byte units
-Ad
od option that outputs file offsets using decimal addresses
-Ax
od option that outputs file offsets using hexadecimal addresses
-An
od option that prevents the od command from outputting the offset address to the left of the data
-j
od option that specifies how many bytes to skip from the input
-N
od option that specifies the total number of bytes the od command will read from the file
-w
od option that specifies the width in bytes to output next to the offset address. By default, the width will be sixteen bytes.
-e (e.g., sed -e '/3/c\three' -e '/a/d')
sed option that is used with each expression when you want to parse using more than one expression at a time
-i
sed option that will modify the output in the original file. This option has an optional argument that is an extension to the original file name (e.g., alpha-first.txt.original instead of alpha-first.txt could be the result of the optional argument)
cat -n result
show the contents of the file named result along with line numbers
-F
tail command option that is used when following a log file that may have been rotated or archived, and a new empty file of the same name replaces it.
-s
tr option that can be used to squeeze out repeats of characters
-L
wc option that displays the maximum line length in the file
-c
wc option to display only number of bytes and the filename
-m
wc option to display only number of characters and the filename
-l
wc option to display only number of lines and the filename
-w
wc option to display only number of words and the filename
cat alpha-first.txt | tr 'aeiou' '@&1*^'
How would you display all the lowercase vowels in alpha-first.txt as the symbols @&1*^ (which replace aeiou, respectively)?
head -3 alpha.txt. (or head -n3 alpha.txt)
How would you display just the first 3 lines of the alpha.txt file?
10
How many lines does the tail command display by default?
cat alpha-first.txt alpha-second.txt
How to output the contents of alpha-first.txt followed by alpha-second.txt
less /etc/passwd -> /sbin -> Enter
How to search for sbin within /etc/passwd
sed '/is/i\Hello' food.txt
How would you add the word 'Hello' as a line before a line that contains the word 'is' in food.txt?
sed '/is/a\Hello' food.txt
How would you append the word 'Hello' as the next line after a line that contains the word 'is' in food.txt?
cp /usr/share/dict/words . split words output ls output*
How would you copy the /usr/share/dict/words file into the current directory and then split the file up into smaller files named outputaa, outputab, etc.. Then display the newly created files.
sed '/a/d' animals.txt
How would you delete all the lines in animals.txt that contain the letter 'a'?
The pipe character (|)
What character can be used to send the output of one command to another.
Space or tab
What is the default field separator for the cut command?
Q
What key do you press to exit the less command?
Spacebar
What keyboard key do you use to advance one page using the less pager?
n (moves forward), N (moves backward)
What keys, respectively, do you use within a less command search result to move forward and backward ?
The field that is used in the joining process must be sorted in both files first.
What must happen before using the join command?
sort -u (the unique option)
What option would you use with the sort command to remove duplicate lines from the output?
-r (e.g., sort -t',' -k1nr os.csv)
What sort command option do you add to the field specification option (-k) to reverse the order of sorting?
H key or Shift + H
When viewing a file using the less command, how do you display a help screen with a summary of the less commands?
nl
Which command will display the output of a file with numbered lines?
uniq
Which command will remove duplicate commands that are currently consecutive?
cut -c1-15
Which option could you use with the cut command to display only the first 15 characters of each field?
uniq -c
Which option of the uniq command will output the number of duplicates that were counted?
-d
Which split command option will allow you to have a numeric suffix instead of the default alphabetical suffix?
-f
Which tail command option will "follow" a file? First, the tail command will display the number of lines specified. Then it follows any changes in the file and displays new content as it is added at the end of the file. One of the main applications for this follow feature is for administrators to watch log files change while they are troubleshooting problems.