Chapter 1: Exploring Linux Command-Line Tools Flashcards
shell
A program that interprets text commands and provides an interface to the system, e.g. bash in terminal.
Environment Variables
Placeholders for for data that may be useful to many programs
bash
Most common default shell
sh
The original Bourne shell which bash is based off of, and not often used
tcsh
Based on the C shell (csh), fairly popular
ksh
The Korn shell, designed to combine the best features of sh and csh
zsh
Tries to extend ksh
Default Interactive Shell
The shell a user uses to enter commands, run run programs, run shell scripts, etc., from command line.
Default System Shell
Used to run system shell scripts, typically at startup
/bin/sh
A file that points to the system’s default system shell (normally bin/bash)
uname -a
Shows the operating system being run, along with hostname and kernel version
Built-In Commands
Commands built into the shell program, AKA internal commands
time
Command that tells you how long commands take to execute, e.g. time pwd tells you how long pwd takes to execute
set
Displays options relating to bash shell operation.
Can also change options if specified.
exit and logout
exit terminates any shell, and logout terminates only login shells
type ____
Checks if a command is internal or external
type -a ____
Same as type, but checks to see if there are both internal and external versions of the command
path
A list of directories in which commands can be found
How to Search for a Previously Done Command
Ctrl+R then type what you’re looking for.
Ctrl+S to search forward if you pass what you were looking for
If Ctrl+S not working when searching for a command, what command should you use to fix it?
stty -ixon
How to exit searching for a command?
Ctrl+G
How to move to beginning of line?
Ctrl+A
How to move to end of line?
Ctrl+E
How to move through a line one word at a time?
Ctrl+Left or Right Arrow
Backspace vs. Delete
Backspace deletes the character to the left of the cursor, while Delete deletes the character under the cursor
How to delete all text from cursor to the end of the line?
Ctrl+K
How to delete all text from cursor to beginning of the line?
Ctrl+X then Backspace
How to transpose letters or words?
Ctrl+T
How to change a word to uppercase? lowercase?
Esc then U
Esc then L
How to change a single letter to uppercase?
Esc then C
How to bring up an editor of the command on the line?
Ctrl+X then Ctrl+E
How to retrieve the last command and execute it?
!!
How to clear the command history?
history -c
How to execute a command by number?
! followed by the number
What is the name of the file where command history is stored?
.bash_history
What does an environment variable look like?
$VAR
How to view all environment variables?
env
How to delete an environment variable?
unset
How to search man pages?
man -k along with your keyword in quotes
whatis database
Contains a short description of each man page
How to create or update the whatis database?
makewhatis
man section 1
Executable programs and shells
man section 2
System calls provided by the kernel
man section 3
Library calls provided by program libraries
man section 4
Device Files
man section 5
File formats
man section 6
Games
man section 7
Miscellaneous
man section 8
System administration commands
man section 9
Kernel routines
An alternative to man
info
What are the pages about the built in commands called?
help pages
Stream
A data entity that can be manipulated
>
Creates a new file containing STDOUT
> >
Appends STDOUT to an existing file
2>
Creates a new file containing STDERR
2»
Appends STDERR to the existing file
&>
Creates a new file containing STDOUT and STDERR
Sends the contents of the specified file to be used at STDIN
Accepts text on the following lines as STDIN
Causes the specified file to be used as both STDIN and STDOUT
What is the file you redirect output to if you want to get rid of it?
/dev/null
here document
Takes text from subsequent lines as standard input
How do you indicate that you are done with typing input when you use the
EOF
(pipe)
Redirects the first program’s standard output to the second programs standard input
tee
Splits STDIN so that it’s displayed on STDOUT and in as many files as you specify.
Often used with pipes
xargs
Builds a command from it’s standard input.
xargs [options] [command [initial arguments]]
What do you surround a command with if you want it to be treated as a separate command from the rest of the line?
Either ` or $()
cat
Links together an arbitrary number of files end to end and sends the result to STDOUT
How to show end of lines when using cat?
cat -E
How to number the lines when using cat?
cat -n numbers all lines
cat -b numbers only the lines that have text
How to compress blank lines when using cat?
cat -s
Makes multiple blank lines into a single blank line
cat -T
Displays tab characters as ^I
cat -v
Displays special characters using carat (^) and M- notations
tac
Like cat, but reverses the order of the lines
join
Combines two files by matching the contents of specified fields.
Fields are typically space-separated
How to specify a different separator for the fields when using join?
join -t [char you want]
How to make join case-insensitive?
join -i
How to specify the fields you want to join?
join -1 [num] -2 [num]
paste
Merges files line by line, separating the lines from each file by a tab
What is the command that converts tabs to spaces?
expand
od
Displays a file in octal
sort
Used to sort files
sort -f
Causes sort to ignore case
sort -M
Sorts by three letter month abbreviations
sort -n
Sort by number
sort -r
Sort in reverse
How do you sort by a specific field with sort?
sort -k [number of the field you want to use]
split
Splits a file into two or more files
split -b [size]
Splits a file into the specified size of bytes
split -l [num]
splits the file by the number of lines specified
How do you signal EOF with a keystroke?
Ctrl+D
tr [options] [set1] [set2]
each character in set 1 is replaced by the corresponding element of set 2
tr -d
Deletes the characters specified in set1, and set 2 is omitted
tr -t
Makes set 1 the same size as set 2 if they are different sizes
unexpand
Converts multiple spaces to tabs
uniq
Removes duplicate lines.
fmt
Tries to format a file well.
Makes lines no longer than 75 characters by default
fmt -w [num]
Allows you to set the desired length of the lines
nl
Command used to number lines
nl -b [style]
Specifies the body numbering style
nl -h [style]
Specifies the header numbering style
nl -f [style]
Specifies the footer numbering style
nl -d=[code]
Tells it that a new page starts wherever it sees what you specify as the code
nl -p
Tells it to not start renumbering when it reaches a new page
nl -n [format]
Specifies the numbering format:
1) ln (left justified, no leading zeroes)
2) rn (right justified, no leading zeroes)
3) rz (right justified, with leading zeroes)
Styles if numbering for nl
1) t: number the lines that aren’t empty only
2) a: number all lines
3) n: omits all line numbers
4) p[REGEX]: only lines that match the REGEX are numbered
pr [file]
- Puts the file into a format for printing.
- 80 character line lengths
- Includes the original text with headers, which list the current date and time, the original filename, and the page number.
How to make pr use a certain number of columns?
pr -[num]
or
pr –columns=[num]
How to make pr use double spacing?
pr -d
or
pr –double-space
How to make pr form-feed between pages?
pr -f or pr -F or pr --form-feed
How to set the page length for pr?
pr -l [num]
or
pr –length=[num]
How to set the text to be displayed in the header using pr?
pr -h [text]
or
pr –header=[text]
How to change the left margin for pr?
pr -o [num]
or
pr –indent=[num]
How to set the page width for pr? Also, what is the default width?
pr -w [num]
or
pr -width [num]
72
head [file(s)]
Shows you the first 10 lines of one or more files
How to specify the number of bytes you want to view for head?
head -c [num]
or
head –bytes=[num]
How to specify the number of lines you want to view for head?
head -n [num]
or
head –lines=[num]
tail [file(s)]
Displays the last 10 lines of one or more files.
Options are the same as head for specifying bytes or lines desired.
How to keep a file open and see when lines are added to it for tail?
tail -f
or
tail –follow
How to tell tail to stop tracking once a certain process ends?
tail -pid=[process ID]
How to move forward a screen when using less?
spacebar
How to move backward a screen when using less?
Esc then V
How to search forward in a file while using less?
/ followed by the search term
How to move to the next occurrence of a searched term in less? previous?
n
N
How to search backward in a file while using less?
? then the search term
How to move to a specific line while using less?
g followed by the number of the line
How to view help while in less?
h
cut
Extracts portions of input lines and displays them on STDOUT
How to specify the list of bytes to cut using cut?
cut -b [list]
or
cut –bytes=[list]
How to specify characters to be cut using cut?
cut -c [list]
or
cut –characters=[list]
How to use cut by field?
cut -f [list]
or
cut –fields=[list]
How to set the delimiting character when using cut?
cut -d [char] or cut --delim=[char] or --delimiter=[char]
wc [file]
Produces a word count of a file, as well aslines and bytes
How to limit the output of wc to line count? word count? byte count? character count? max line-length?
–lines or -l
–words or -w
–bytes or -c
–chars or -m
–max-line-length or -L
Regular Expression
A tool for describing or matching patterns in text
What would be matched using the regular expression b[eia]g
beg, big, and bag
What would be matched using the regular expression a[2-4]z
a2z, a3z, a4z
What would be matched using the regular expression a.z
Any three character string beginning with a and ending with z
Which symbol represents the beginning of a line in regular expressions? the end of a line?
$
What does the * repetition operator indicate?
Zero or more occurrences
What does the + repetition operator indicate?
One or more occurrences
What does the ? repetition operator indicate?
Zero or one occurrence
What would be matched to the regular expression car|truck
car or truck
What are parentheses used for in regular expressions?
Used to group subexpressions
grep [options] [regex] [files]
Searches for files that contain a specified string
How to count the number of lines that match for grep?
grep -c
or
grep –count
How to specify a pattern input file for grep?
grep -f [file]
or
grep –file=[file]
How to make grep case-insensitive?
grep -i
or
grep –ignore-case
How to search in specified directory and all subdirectories with grep?
grep -r or grep --recursive or rgrep
How to make grep not recognize characters as regular expression characters and instead take them literally?
grep -F or grep --fixed-strings or fgrep
How to use extended regular expressions with grep?
grep -e or grep --extended-regexp or egrep
sed [options] -f [script-file] [input-file]
or
sed [options] [script-text] [input-file]
- Modifies input-file using a set of commands specified in script-text or script-file, and sends it to STDOUT.
- Typically you should surround script-text in single quotes.
sed command =
Displays the current line number
sed command a"text”
Append “text” to the file
sed command i"text”
Insert “text” into the file
sed command r “filename”
Append text from “filename” into the file
sed command c"text”
Replace the selected range of lines with the provided text
sed command s/”regexp”/”replacement”
Replace text that matches “regexp” with “replacement”
sed command w “filename”
Write the current pattern space to the specified file
sed command q
Immediately quit the script, but print the current pattern space
sed command Q
Immediately quit the script.
What is a special purpose program that can convert a DOS-style file to a Unix-style file?
dos2unix