Topics to review.
Useful command line control characters.
Control | Action |
d |
signals end of file. Used when program taking input from command line. |
c |
Interrupt - kill -2 or kill -INT |
Used to interrupt a current foreground process. This signal can be trapped.
|
\ |
Interrupt - kill -2 or kill -QUIT |
Used to interrupt a current foreground process. This signal can be trapped.
Like [ctrl] C, but will provide a core dump for debugging in certain cases.
|
u |
Clears the current command line. |
w |
Clears the previous word entered on the command line. |
v |
Used to enter another control character on the command line as a literal.
|
h |
Deletes the preceding character on the command line. If not at end of
the line, it will drag the suceeding chacacters with it. |
z |
Suspends the current foreground process. Puts it in the background in
a sleeping condition. |
s |
Suspends the current process's output to the screen. It does not
suspend the execution of the process. At current CPU speeds, this has very
limited use. Piping output to less is much more useful. |
q |
Resumes dislaying suspended output, [ctrl]s, to the screen. |
m |
Carridge return. Equivalent of hitting enter. |
l |
Clears the screen. |
|
|
Filename wild-cards.
* # any match of any length.
[charlist] # lists alternative single character values for one position in
file-name. Supports [:...:] meta ranges when used correctly.
ls -d A[[:digit:]]* # lists any files starting with A followed by a digit
and possibly other characters. -d prevents matching sub-directories being listed.
? # question mark lists one character of any value.
Permissions
chmod perm-list target-file[s] # changes permission on target file[s].
Review chmod and be able to set permissions using both the octal and
symbolic formats.
umask mask # sets maximum default permissions. Won't affect existing files and
won't turn on permissions on new file, only blocks.
Command line delimiter.
&&, &, ||, ;
Redirection.
> redirect out
>> append out
< redirect in
<< (here) take in-stream input.
Aliases
Be able to define an alias similar to those in the assignment, strictly
using && and || for conditional execution.
Inodes and linking
Be able to create hard and soft links.
Be able to list 4-5 pieces of data stored in an Inode entry.
permissions
file size
owner's id
group id
time data last modified
time data last accessed
time inode last modified
pointers to data on drive
link count (number of filenames)
File types :
regular - file containing data, could be text, html, or binary executable.
directory - specially tagged file containing filenames and their associated
indode number. Usually only accessible by special purpose commands.
symbolic - file designed to hold the path to another file of interest.
Created with ln -s . May "link" to any other type of file.
named pipe - special purpose file designed connect to processes together.
Acts like the command line pipe, |, execpt it can be addressed by name.
Addressible from the command line.
character special - unbuffered interface to a device driver. Psuedo-terminals
(network connections) used by ssh are usually character special.
block special - buffer interface to a device driver. Commonly used to mount
hard drives or other storage devices to the Unix filesystem.
socket - similar but more flexible that a named pipe. Connects two processes
together to provide 2-way communication. Provide a variety of connections,
including network connections.
The bash configuration files :
/etc/profile # system default shell login configuration.
/etc/bash.bashrc # system default non-login shell configuration.
$HOME/.bash_profile # User configurable default shell login configuration.
$HOME/.bashrc # user configurable non-login shell configuration.
# The /etc/bash.bashrc and $HOME/.bashrc normally are not read when
# logging in. However, /etc/profile and $HOME/.bash_profile often
# source .bash_profile and .bashrc anyways.
List the 2 login configuration files in the right order.
List the 2 non-login configuration files in the right order.
Important Commands and their options
Note : All commands listed under
http://faculty.cs.niu.edu/~berezin/330/Q/cmds-q.html
are acceptable for simple questions such has "Which command does ...."
Look these up and understand what they do. Don't worry about options unless
I mention it elsewhere in the study guide. But, where possible, run the
command so you know what the output looks like.
aspell bg cal cat comm cp cut date df diff du egrep expr
fg fgrep file find finger ftp grep head id jobs kill killall less
ls ln make mkdir more mv od pgrep pkill pr ps quota rm
rmdir scp ssh sort sftp split stat telnet tail time top touch tr
w whereis who whoami |
Commands for which you should know more useful options.
cp # copy - duplicates contents of file. Copies file to new name, to new
directory, copies multiple files into a directory.
-r, -R # recursive. Copies or duplicates a directory. Only way to duplicate
a directory and its contents.
-p # preserves original owner ID, permissions, and time-stamps.
-l target-dir # link, creates a set of links rather than duplicating data.
ln works on a single file, cp -l can work with a list or filename wildcards.
Target has to be a directory on same file-system. And only regular files and
named pipes will be copied.
mv
ln # links a new name to a filename.
[no link option] # hard link, file being linked to must be an existing regular
file on the same file-system as the new name.
-s # symbolic. Creates a new file in which it stores the target argument. Does
not check for existence or accessibility of target.
ps # lists processes running.
-u # specify user ID of processes to list.
-f # full, lists additional info such has parent process ID., when process was
started, Mount of CPU time consumed, command name.
-e # list every process running.
jobs # lists processes(jobs) running or stopped in the current login
environment.
-l # include process ID in listing.
bg [%job-id] # restart in background last job accessed or specified
job [%job-id]. bg is built-in cmd of bash.
fg [%job-id] # restart in foreground last job accessed or specified
job [%job-id]. bg is built-in cmd of bash.
kill PID # send semaphore signal to process Id'ed with PID. You must
own process.
Signal will usually specify termination of process, but other signals types
available. Use man -s 7 signal to see list.
kill exists as both a built-in and a separate external program.
-[#], -s [#], -SIGID>, -SIGID # specifies type of signal to send.
-l # built-in, lists signal names and number.
-L # /bin/kill, lists signal names and number.
-l # /bin/kill, lists signals' short names.
Most useful signals :
-1, -INT, -SIGINT # signals hangup. On receiving HUP, many processes are
designed to re-read their configuration files.
-9, -KILL, -SIGKILL # this is an unblockable signal. It will terminate a
process you own. But open files may be left in open state.
-15, -TERM, -SIGTERM # default of kill with no signal id. Programs will
generally attempt to close files before dieing.
-2, -INT, -SIGINT # equivalent of [ctr]c. Interrupt. Trappable.
-3, -QUIT, -SIGQUIT # equivalent of [ctr]\. Will do a core dump
if program is binary executable.
-17, -STOP, -SIGSTOP # issues a stop signal. equivalent of [ctrl]z.
-19, -TSTP, -SIGTSTP # issues a terminal stop signal.
pgrep # allows for targeted search for processes based on name,
partial name, number, or other search pattern. It can also observe
light-weight threads.
pkill # when pgrep invoked under the name pkill, provides all
the features of pgrep and will attempt to signal (terminate) matched
processes.
You must own processes to kill, and you may be required to specify a signal.
sort # sort file contents.
-o # specify output file, can be same as input file.
-r # sort in reverse order.
-R # random - scrambles file.
-n # treat number strings as numeric, so 10 is greater than 5
-t # specify a specific field separator, default is white space. Most often
used with the key field modifier.
-k # specify one or more key fields(columns) to sort on first. If more than
one field to be used, specify both start and stop fields to be a particular
key.
#list all processes in full detail, sort on parent process ID in reverse
# order, then on user id in forward order.
ps -ef | sort -n -k3,3r -k1,1 | less
find # searches all directories under a stated starting point for files
that match a specified criteria. Once found, it can take various actions
on found files. You must have appropriate access to any directories encountered
to view their contents.
Test may have questions similar to these :
Write a find that :
locates files that are greater than 100k.
locates files that are symbolic link files and less than a day old.
locates .cpp (c+ files) and provides a long listing.
locates files that are less than a day old.
locates files that have a .o or .obj extension
and deletes them after prompting user for OK.
Write a find that starts search in home directory (absolute path) and
finds any files that have a .cpp extension and list them. But, don't
look in the Backup directory (prune).
Write a find that starts search in home directory and It will find any
files with a .o extension OR is file with a .gch extension. If found,
do an rm after prompting user for permission.
Write a find that finds all files that are not directories, regular
files, or symbolic links. Do a long listing. Start search in root.
Write a find that finds any files greater than 200k OR is an object file
(filename has a .o extension). Give the long listing of what you found.
Start search in your home directory.
Be aware, because the OR breaks the find into two alternative actions,
you will have to use parentheses around the search criteria to restrict its
scope or repeat the action twice.
find . \( -name "*.o" -o -name "*.obj" \) -ok rm {} \;
find . -name "*.o" -ok rm -o -name "*.obj" \) -ok rm {} \;
locates c+ files (*.cpp), and displays the 1st 5 lines (head).
start point
find # with no start point, will start search in current directory
using relative path and simply lists all found files with path information.
find dir-name # if dir-name is a relative location, listed files will be
shown with relative paths.
If dir-name is an absolute path, files will be listed with their absolute path.
search criteria
-name "somename" # will list location of any file of the same
name. Will take filename wild-cards, but must be quoted to pass wild-cards
into find. This is probably the most common usage.
-mtime [+-]# # find files created/modified # days ago.
+ specifies more than # days and - specifies less than # days.
-mmin [+-]# # same search except uses minutes for measurement.
find Notes -mtime -2 -ls
# look in the Notes directory and locate all files created/modified within
# the last 2 days. Do a long listing so you can see the timestamp.
-ctime [+-]# # find files whose inode created/modified # days ago.
+ specifies more than # days and - specifies less than # days.
Modification occurs with file creation, linking or removing hard linked names,
moving/renaming file, etc.
-cmin [+-]# # same search except uses minutes for measurement.
-atime # locate files that were accessed # days ago.
+ specifies more than # days and - specifies less than # days.
-min # does same matches except uses minutes.
-links [+-]# # finds files with # links,
+ specify file with more than # links, and - fewer.
-inum #, -samefile filename # finds files with
specified inode number or inode number of named file.
find . -samefile restore.sh
# locates all filenames linked to restore.sh file. Search told to look only
# in current directory and its sub-directories.
-size [+-]#[bckMG] # files files that match specified size. +/-
specify greater or less than, [bckMG] scale, so 4k is 4098 bytes.
find $HOME -size +400k
# locates all files greater than 409600. Symbolic size ranges use binary based.
-uid #, -name user-id # finds files belonging to
specified user. uid is user numeric identifier, uname is login name.
actions
-print # display found file with path info. If no action specified,
find will assume print, but some more complex finds may require you
to specify the print action.
-ls # does an ls -il on the found files.
-exec cmd-sequence{} \; # performs command sequence on
matched files. Note {} required to pass found file info to cmd-sequence and
the \; (space, backslash, semi-colon) required to specify end of cmd-sequence.
find $HOME -name "*.txt" -exec grep -l "network.cfg" {} \;
# starting in home directory, locate all files with a .txt extension. For
# those files, grep for the string network.cfg. If found, print print the
# name of the file.
-ok cmd-sequence{} \; # performs command sequence on
matched files. Note {} required to pass found file info to cmd-sequence and
the \; (space, backslash, semi-colon) required to specify end of cmd-sequence.
The -ok will prompt user for an OK before applying cmd-sequence to a
located file.
find pics -name "*.png" -ok rm {} \;
# Look in the pics directory and find any png files, delete them but only
# if user gives permissions. Note that it is the find that is asking
# permission to run the rm command.
-prune dir # prunes or skips specified directory and its
sub-directories. Used with the -o (OR) statement.
find . -prune 2015 -o prune 2016 -o -name "makefile"
# looks for a makefile anywhere under the current directory except in the
# 2015 and 2016 directories.
! # the NOT or invert. Negates the test condition. Useful when you
know what you don't want.
find public_html ! -name "*.html" -ls
# locates any files under public_html that are not web-pages (*.html) and
# gives a long listing.