103 GNU and Unix Commands KT Flashcards
103.1 Work on the command line
The following is a partial list of the used files, terms and utilities:
bash echo env export pwd set unset type which man uname history .bash_history Quoting
bash
Command: Bash (Bourne Again SHell) is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh).
bash [options] [command_string | file]
echo
Command: Echo the STRING(s) to standard output.
echo [SHORT-OPTION]… [STRING]…
echo LONG-OPTION
env
Command: Set each NAME to VALUE in the environment and run COMMAND. Run a program in a modified environment.
env [OPTION]… [-] [NAME=VALUE]… [COMMAND [ARG]…]
export
Command: Set export attribute for shell variables. Marks each NAME for automatic export to the environment of subsequently executed commands. If VALUE is supplied, assign VALUE before exporting.
export [-fn] [name[=value] …]
export -p
pwd
Command: Print the full filename of the current working directory.
pwd [OPTION]…
set
Command: Set or unset values of shell options and positional parameters. Change the value of shell attributes and positional parameters, or display the names and values of shell variables.
set [-abefhkmnptuvxBCHP] [-o option-name] [–] [arg …]
unset
Command: Unset values and attributes of shell variables and functions. For each NAME, remove the corresponding variable or function.
unset [-f] [-v] [-n] [name …]
type
Command: Display information about command type. For each NAME, indicate how it would be interpreted if used as a command name.
type [-afptP] name [name …]
which
Command: which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell. It
does this by searching the PATH for executable files matching the names of the arguments. It does not canonicalize path names.
which [-a] filename …
man
Command: man is the system’s manual pager. Each page argument given to man is normally the name of a program, utility or function. The manual page associated with each of these arguments is then found and displayed.
man [OPTION…] [SECTION] PAGE…
uname
Command: Print certain system information. With no OPTION, same as -s.
-s, –kernel-name
print the kernel name
uname [OPTION]…
history
Command: The GNU History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones.
history [-c] [-d offset] [n] or history -anrw [filename]
history -ps arg [arg…]
.bash_history
File that stores a history of commands executed by a user.
It can generally be found in the users home directory:
~/home/user/.bash_history
Quoting
“
‘
\
Unix Shell provides various metacharacters which have special meaning while using them in any Shell Script and causes termination of a word unless quoted.
The double quote ( “quote” ) protects everything enclosed between two double quote marks except $, ‘, “ and .Use the double quotes when you want only variables and command substitution.
- Variable - Yes
- Wildcards - No
- Command substitution - yes
The single quote ( ‘quote’ ) protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters.
- Variable - No
- Wildcards - No
- Command substitution - No
Use backslash to change the special meaning of the characters or to escape special characters within the text such as quotation marks.
103.2 Process text streams using filters
The following is a partial list of the used files, terms and utilities:
bzcat cat cut head less md5sum nl od paste sed sha256sum sha512sum sort split tail tr uniq wc xzcat zcat
bzcat
Command: bzcat (or bzip2 -dc) decompresses all specified files to the standard output.
- d –decompress
- c –stdout
bzcat [flags and input files in any order]
cat
Command: Concatenate FILE(s) to standard output.
cat [OPTION]… [FILE]…
cut
Command: Print selected parts of lines from each FILE to standard output.
cut OPTION… [FILE]…
head
Command: Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
head [OPTION]… [FILE]…
less
Command: less is a filter for paging through text one screenful at a time. Less is a program similar to more (1), but it has many more features. Less does not have to read the entire input file before starting, so with large input files it starts up faster than other text editors.
less [filename]…
md5sum
Command: Print or check MD5 (128-bit) checksums.
md5sum [OPTION]… [FILE]…
nl
Command: Write each FILE to standard output, with line numbers added.
nl [OPTION]… [FILE]…
od
Command: Write an unambiguous representation, octal bytes by default, of FILE to standard output. With more than one FILE argument, concatenate them in the listed order to form the input.
od [OPTION]… [FILE]…
od [-abcdfilosx]… [FILE] [[+]OFFSET[.][b]]
paste
Command: Write lines consisting of the sequentially corresponding lines from each FILE, separated by TABs, to standard output.
paste [OPTION]… [FILE]…
sed
Command: Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
sed [OPTION]… {script-only-if-no-other-script} [input-file]…
sha256sum
Command: Print or check SHA256 (256-bit) checksums.
sha256sum [OPTION]… [FILE]…
sha512sum
Command: Print or check SHA512 (512-bit) checksums.
sha512sum [OPTION]… [FILE]…
sort
Command: Write sorted concatenation of all FILE(s) to standard output.
sort [OPTION]… [FILE]…
sort [OPTION]… –files0-from=F
split
Command: Output pieces of FILE to PREFIXaa, PREFIXab, …; default size is 1000 lines, and default PREFIX is ‘x’.
split [OPTION]… [FILE [PREFIX]]
tail
Command: Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
tail [OPTION]… [FILE]…
tr
Command: Translate, squeeze, and/or delete characters from standard input, writing to standard output.
tr [OPTION]… SET1 [SET2]
uniq
Command: Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).
uniq [OPTION]… [INPUT [OUTPUT]]
wc
Command: Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. A word is a non-zero-length sequence of characters delimited by white space.
wc [OPTION]… [FILE]…
wc [OPTION]… –files0-from=F
xzcat
Command: xzcat is an alias of the xz utility. It is a basic way of doing the following commands:
xz –decompress –stdout.
xz -dc
xzcat [OPTION]… [FILE]…
zcat
Command: Uncompress FILEs to standard output. zcat is identical to gunzip -c. (On some systems, zcat may be installed as gzcat to preserve the original link to compress.)
/usr/bin/zcat [OPTION]… [FILE]…
zcat [ -fhLV ] [ name … ]
103.3 Perform basic file management
The following is a partial list of the used files, terms and utilities:
cp find mkdir mv ls rm rmdir touch tar cpio dd file gzip gunzip bzip2 bunzip2 xz unxz file globbing
cp
Command: Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
find
Command: GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence until the outcome is known, at which point find moves on to the next file name.
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point…] [expression]
mkdir
Command: Create the DIRECTORY(ies), if they do not already exist.
mkdir [OPTION]… DIRECTORY…
mv
Command: Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…
ls
Command: List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor –sort is specified.
ls [OPTION]… [FILE]…
rm
Command: rm removes each specified file. By default, it does not remove directories.
rm [OPTION]… [FILE]…
rmdir
Command: Remove the DIRECTORY(ies), if they are empty.
rmdir [OPTION]… DIRECTORY…
touch
Command: Update the access and modification times of each FILE to the current time. A FILE argument that does not exist is created empty, unless -c or -h is supplied.
touch [OPTION]… FILE…
tar
Command: GNU tar is an archiving program designed to store multiple files in a single file (an archive), and to manipulate such archives.
tar {A|c|d|r|t|u|x}[GnSkUWOmpsMBiajJzZhPlRvwo] [ARG…]
tar -A [OPTIONS] ARCHIVE ARCHIVE
tar –create [–file ARCHIVE] [OPTIONS] [FILE…]
cpio
Command: GNU cpio copies files between archives and directories.
cpio -o < name-list [> archive]
cpio -p destination-directory < name-list
dd
Command: Copy a file, converting and formatting according to the operands.
dd [OPERAND]…
dd OPTION
file
Command: file tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests. The first test that succeeds causes the file type to be printed.
file [OPTION…] [FILE…]
gzip
Command: Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times.
gzip [OPTION]… [FILE]…
gzip [ -acdfhklLnNrtvV19 ] [-S suffix] [ name … ]
gunzip
Command: gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack. The detection of the input format is automatic.
gunzip [ -acfhklLnNrtvV ] [-S suffix] [ name … ]
/usr/bin/gunzip [OPTION]… [FILE]…
bzip2
Command: bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. Compression is generally considerably better than that achieved by more conventional LZ77/LZ78-based compressors, and approaches the performance of the PPM family of statistical compressors.
bzip2 [ -cdfkqstvzVL123456789 ] [ filenames … ]
bunzip2
Command: bunzip2 (or bzip2 -d) decompresses all specified files. Files which were not created by bzip2 will be detected and ignored, and a warning issued.
bunzip2 [ -fkvsVL ] [ filenames … ]
xz
Command: xz is a general-purpose data compression tool with command line syntax similar to gzip(1) and bzip2(1).
The native file format is the .xz format, but the legacy .lzma format used by LZMA Utils and raw compressed streams with no container format headers are also supported.
xz [option…] [file…]
unxz
Command: Alias used to decompress FILEs in the .xz format. unxz stems from the xz command.
unxz is equivalent to xz –decompress.
unxz [OPTION]… [FILE]…
file globbing
File globbing is the operation that recognises wildcard patterns and does the job of file path expansion. Here is an example of some wildcards it accepts:
* - represents zero or more characters ? - represents a single character [] - represents a range of characters
103.4 Use streams, pipes and redirects
The following is a partial list of the used files, terms and utilities:
tee
xargs
tee
Command: Copy standard input to each FILE, and also to standard output.
tee [OPTION]… [FILE]…
xargs
Command: xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.
xargs [options] [command [initial-arguments]]
103.5 Create, monitor and kill processes
The following is a partial list of the used files, terms and utilities:
& bg fg jobs kill nohup ps top free uptime pgrep pkill killall watch screen tmux
&
Runs a job in the background when added at the end of a command line.
sleep 100 &
bg
Command: Move jobs to the background.
bg [job_spec …]
fg
Command: Move job to the foreground.
fg [job_spec …]
jobs
Command: Lists the active jobs. JOBSPEC restricts output to that job. Without options, the status of all active jobs is displayed.
jobs [-lnprs] [jobspec …]
jobs -x command [args]
kill
Command: Send the processes identified by PID or JOBSPEC the signal named by SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then SIGTERM is assumed.
kill [options] […]
nohup
Command: Run COMMAND, ignoring hangup signals.
nohup COMMAND [ARG]…
nohup OPTION
ps
Command: ps displays information about a selection of the active processes.
ps [options]
top
Command: top provides a dynamic real-time view of a running system.
It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel.
top -hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]
free
Command: free displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel.
free [options]
uptime
Command: uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.
uptime [options]
pgrep
Command: pgrep looks through the currently running processes and lists the process IDs which match the selection criteria to stdout. All the criteria have to match.
pgrep [options] pattern
pkill
Command: pkill looks through the currently running processes and will send the specified signal (by default SIGTERM) to each process that matches the criteria. It will not list them on stdout.
pkill [options] pattern
killall
Command: killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.
killall [ -Z CONTEXT ] [ -u USER ] [ -y TIME ] [ -o TIME ] [ -eIgiqrvw ] [ -s SIGNAL | -SIGNAL ] NAME…
watch
Command: watch runs a command repeatedly, displaying its output and errors (the first screenfull).
watch [options] command
screen
Command: Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).
screen [ -options ] [ cmd [ args ] ]
tmux
Command: tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen.
tmux [-28lquvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]]
103.6 Modify process execution priorities
The following is a partial list of the used files, terms and utilities:
nice
ps
renice
top
nice
Command: Run COMMAND with an adjusted niceness, which affects process scheduling. With no COMMAND, print the current niceness.
Niceness values range from -20 (most favorable to the process) to 19 (least favorable to the process).
nice [OPTION] [COMMAND [ARG]…]
ps
Command: ps displays information about a selection of the active processes.
ps [options]
renice
Command: renice alters the scheduling priority of one or more running processes.
renice [-n] [-p|–pid] …
renice [-n] -g|–pgrp …
renice [-n] -u|–user …
top
Command: top provides a dynamic real-time view of a running system.
It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel.
top -hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]
103.7 Search text files using regular expressions
The following is a partial list of the used files, terms and utilities:
grep egrep fgrep sed regex(7)
grep
Command: grep searches for PATTERNS in each FILE. PATTERNS is one or patterns separated by newline characters, and grep prints each line that matches a pattern.
grep [OPTION…] PATTERNS [FILE…]
grep [OPTION…] -e PATTERNS … [FILE…]
grep [OPTION…] -f PATTERN_FILE … [FILE…]
egrep
Command: egrep interprets PATTERNS as extended regular expressions and is a variant of grep. egrep is the same as grep -E. The -E option is as follows:
-E, –extended-regexp
egrep [OPTION…] PATTERNS [FILE…]
fgrep
Command: fgreg interprets PATTERNS as fixed strings, not regular expressions. and is a variant of grep. fgrep is the same as grep -F. The -F option is as follows:
-F, –fixed-strings
fgrep [OPTION…] PATTERNS [FILE…]
sed
Command: Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
sed [OPTION]… {script-only-if-no-other-script} [input-file]…
regex(7)
Regular expressions (“RE”s), come in two forms: modern REs and obsolete REs.
A (modern) RE is one(!) or more nonempty(!) branches, separated by ‘|’. It matches anything that matches one of the branches.
A branch is one(!) or more pieces, concatenated. It matches a match for the first, followed by a match for the second, and so on.
A piece is an atom possibly followed by a single(!) ‘’, ‘+’, ‘?’, or bound. An atom followed by ‘’ matches a sequence of 0 or more matches of the atom.
A bound is ‘{‘ followed by an unsigned decimal integer, possibly followed by ‘,’ possibly followed by another unsigned decimal integer, always followed by ‘}’.
Obsolete (“basic”) regular expressions differ in several respects. ‘|’, ‘+’, and ‘?’ are ordinary characters and there is no equivalent for their functionality.
For more: man regex.7
103.8 Basic file editing
Terms and Utilities:
vi /, ? h,j,k,l i, o, a d, p, y, dd, yy ZZ, :w!, :q! EDITOR
vi
Editor launched from the command line: vi (visual) utility is a screen-oriented text editor.
vi [−rR] [−c command] [−t tagstring] [−w size] [file…]
vi editor commands
/, ?
/ = Repeat previous search forward ? = Repeat previous search backward
vi editor commands
h,j,k,l
h = Left j = Down k = Up l = Right
vi editor commands
i, o, a
i = Insert text before cursor o = Open new line for text below cursor a = Insert text after cursor
vi editor commands
d, p, y, dd, yy
d = Deletes characters and lines in the direction of choice. Choices are h, j, k, l. p = Paste buffer contents. y = Copies(Yank) a single character after the cursor into the buffer. dd = Deletes a current line. yy = Copies a current line into the buffer.
vi editor commands
ZZ, :w!, :q!
ZZ = Write the file and quit. \:w! = Write the file overriding protection. \:q! = Quit the file overriding protection.
EDITOR
Enables the editing of files. Some examples are: vi vim nano emacs