Title: find Command
1find Command
- Characteristics
- Locate files descending from multiple starting
points - Employs regular expressions
- Examples
- On entire system gtfind / -name "cs"
- Two starting points gtfind /usr /var/html name
"cs" - Case insensitive gtfind /usr iname "htm"
- More than 3 days ago gtfind /usr -mtime 3 name
"htm" - Within the last 3 days gtfind /usr mtime -3
name "htm"
2Customizing your prompt
- To set set PS1 variable with a prompt string.
- Additional prompt string capabilities\d
todays date\H host name\T current time\w
working directory\! history number of this
command - Example export PS1"gt"
- Example export PS1"\! \w gt"
3Disk Usage Commands
- Free Blocks gtdf
- Disk usage
- Size of each directory (recursive) gtdu
- Summary of each directory (not recursive)gtdu s
- Size of a single directory gtdu s ltfileNamegt
- Sort usage, numeric reverse, page at a timegtdu
sort nr more
4Managing Processes
- Display processes and pid number
- gtps displays all of your processes
- gtps a removes "only yourself" restriction
- gtps x remove "must have a terminal" restriction
- gtps u display in a user friendly format
- gtps aux combines the above options
- gtps l displays your processes in long format
- Kill a process
- gtkill -9 ltpidgt force terminate the pid specified
using signal 9
Note the above ps options should not be preceded
with a dash (-)
5Foreground, Background, Suspend
- Concepts
- A single foreground process can run within the
shell - Many background processes can run outside the
shell - A suspended process stops running till it is
restarted - Commands
- suspend process and move to background ltctrlgtz
- track background processes gtjobs
- resume last suspended process in background gtbg
- resume nth background process gtbg n
- move last suspended process to foreground gtfg
- move nth background process to foreground fg n
- run a command in the background gtsort file
6Bash Command Processing
- Read a command
- Transform the command
- Process history substitutions
- Process alias substitutions
- Process special character (break sequences)
- Process variable substitutions
- Process command substitutions
- Process pathname expansions
- Execute the Command
- Back to step 1 to get next command
Note transformations occur in the order stated
above
7Bash History
- Turn on History command export HISTSIZE20
- Clear history list history -c
- History list gthistory
- Execute nth history command gthistory gt!3
- Execute last command gtgrep Bush grades gt!!
awk 'print 4' - Use arguments from the last command echo !
- Use last argument of last command gtcp prog.c
new.c gtls ! - Use the first argument of last command gtls
l gtecho ! - Navigate around history commands use arrow keys
Note argument is anything following the command
name
8Aliases
Replace a word by a string when starting a command
- Definition Replace a string by a word at the
start of a command - Use Shorten what needs to be typed by aliasing a
command with its arguments - Use Rename a command to something more familiar
- Examples
- Display all of the aliases in use gtalias
- Example (dos user directory list) gtalias dir'ls
l' - To remove an alias gtunalias dir
9Creating Script Variables
- create a variable ltvargt"value"
- Note do not use spaces
- Example gtTEAM"Yanks" gtecho TEAM
- Variables are case sensitive
- You can use variables anywhere in
commnads gtbindirbin gtecho bindir - To make variables accessible to all sub shells,
use export gtexport TEAM"yanks"
10Command Substitution
The output of one command becomes part of another
- Setting variables ltcmdgt or (ltcmdgt)
- Examples
- gtdirpwdgtecho dir
- files(ls)echo files
- echo there are grep ci bush grades bush\'s
- From command line ltcmdgtecho dategrep "abc"
cat file.txt
Note The back quote is found on the top left of
most keyboards
11Pathname Expansions
- Any string of 0 or more length
- Any single character ?
- Any single character specified abc
- Examples
- ls cs367/.c
- ls cs367c
- ls newfile???
- ls out12
Notes Filenames must not have an initial period
or slashes
12Change File Mode (chmod)
- Change permissions of a file
- Examples
- write for user and group, read for worldgtchmod
ugw,or trak - all permissions for user, read and execute for
group and world gtchmod urwx,gorx file.cgi - Remove execute permission to user, read, and
world gtchmod a-x fileName
13File Permissions
- Ls l format -rw-r--r-- 1 owner group 213 Aug
26 1631 README - The line contains
- - for a regular file or d for a directory
- permission string (user, group, and other
(world)) - Number of links to file or directory
- the file size in decimal right-justified in a
13-byte field - modification data
- file name
- Permission groups uuser, ggroup, oother
(world), aall - Permission types read r (4), write/delete w
(2), execute x (1)
14PATH Environment Variable
- Most unix commands are C programs
- When you execute a command
- PATH contains a series of path names
- The shell searches all variables in PATH
- To add program/bin to the PATH variableexport
PATH PATHprogram/bin - To add the current directory to PATHexport PATH
PATH.
15Shell Scripts
See Bash's Beginners guide See examples on the
class web site
- Put multiple commands in a file and then execute
them by typing the file name - They execute the statements one by one
- Creating a script
- The first line should be !/bin/bash
- Other lines starting with are comments
- Make executable ux scriptName
- Execute ./scriptName unless working directory
is in the search path - Use script capabilities to
- create loops
- create decisions
- write functions
16Arguments to a Shell Script
- Arguments to a script
- 0 is the name of the shell script (argument 0)
- 1 is the first argument
- 2 is the second argument
- k is the kth argument
- is the number of arguments
- _at_ is all of the arguments
- Example scriptFile x y z
- 0 scriptFile
- 2 y
- is 3
- _at_ is x y z
17Shell Script Decisions
- if condition then ltstatementsgt fi
- if condition then ltstatementsgt else fi
- if 1 3
- then
- echo "arg equals 3"
- else
- echo "arg not 3"
- fi
Note space after and before Comparators !
-lt le gt ge f -r where -f is file r for
readable file
18while and do Loops
- while condition do
- command1 command2 command3
- done
- i0
- while i lt 4 do echo "Welcome i"
ii1 done - or
- i0
- do until i 4 do echo "Welcome i"
ii1 done - or
- i0
- while i lt 4
- do
- echo "Welcome i"
- ii1
- done
Semicolons separate parts of the loop on the same
line Boolean variables true or false are legal
19for Loop
- for ltvarNamegt in ltlist of wordsgtdo
ltstatementsgt done - Example for i in cat list do cp "i"
"i.bak" done - Example for i in PATHNAME/ do rm PATHNAME
done - Example gtfor x in do echo x done
- Note the last example is directly from the
command line
20Calculate the average in a script
- !/bin/bash
- value0
- count0
- for i in _at_
- do
- valuevaluei
- countcount1
- done
- averagevalue/count
- echo average
21Script Functions
- ltfuncNamegt() ltcmdListgt
- Example ll ll() ls l more
- ll
- Note Function must be declared before it can be
used. - Note Spaces required before and after braces
- Note A common error is forgetting the required
spaces
22Functions with arguments
- Arguments
- _at_ means all arguments
- 1 first argument
- 2 second argument
- number of arguments
- Example ll() ls lR _at_ more ll /
- Example mycd() cd _at_ pwd
23Shell Startups
- For bash, edit /.bash_profile
- In this file, include
- aliases
- variables
- functions
- setting the search path (PATH)
- Good idea
- Make the script in a test file first
Note .bash_profile executes on log in, .bashrc
executes starting sub shells