350 likes | 578 Views
Introduction to UNIX. A User’s Perspective: Day 3: Advanced Commands & vi. What We Will Cover Today. Shell customizations File operation commands The UNIX search utility vi Shell script basics. Clean Up: Standard Error. Redirecting standard error 2> = redirect standard error
E N D
Introduction to UNIX A User’s Perspective: Day 3: Advanced Commands & vi
What We Will Cover Today • Shell customizations • File operation commands • The UNIX search utility • vi • Shell script basics
Clean Up: Standard Error • Redirecting standard error • 2> = redirect standard error • Let’s redo the link example seen yesterday • cat file 2> errors • cat index.html 2> errors
BASH! • User friendly & Free • Want to switch • $ bash • Easy to navigate and edit at command-line • Let’s see…
Bash Tip 1 • History recall • The up/down arrow • Korn Shell equivalent • $ set –o vi • Let’s you use vi control keys at the command-line
Bash Tip 2 • Tab completion • Hitting the tab key will attempt to complete file name from the current directory • Example • $ cd • $ cd pu (hit the tab key)
Bash Tip 3 • Remembering your history • Attempts to auto complete commands previously entered • Start with CTRL-R • Start typing • Try ls -al
Bash Customization Ideas • Make you life easier • Examples: • $ alias procs=‘echo “Number of processes are: ”;ps | wc –l’ • Issue the command ‘procs’ • $ procs • $ alias ll=‘ls –al’ • $alias l=‘ls’ • These can be placed in your .profile so they are always available
Understanding Your Environment • env • Values that follow you when you change shells • set • Values set when you enter a shell • Lost when leave that shell
File Operations • join • grep • diff • cmp • dircmp • split • sed
join • cd • cp –p /u/ux101is1/jointest* . • sort –k 1 jointest1 > jtest1s • sort –k 1 jointest2 > jtest2s • join jtest1s jtest2s • What are the issues? • What are possible resolutions?
grep • cd • grep your_user_name /etc/passwd • What did you find? • grep –i Filter * • What did you find? • What is the problem?
diff • cd • diff jtest1s jtest2s
cmp & dircmp • cmp file1 file2 • cmp jtest1s jtest2s • dircmp directory1 directory2 • dircmp -ds public_html public_html/test > dircmp.txt
split • Example • $ split -l 1 jtest1s student • $ ls –al • $ cat student*
sed • Example: • cd • cp -p jtest1s sedtest • $ cat sedtest • $ sed -e 's/mark/mike/g' sedtest > sedtest1 • $ cat sedtest1
Subshells • No change to your current shell state • ( some commands ) • Example 1: • $ ( date; who ) > whowhen.txt • Example 2: • $ cd • $ pwd • $ ( cd pub*; pwd ) • $ pwd • Implications?
The UNIX Search Tool • find • Very versatile • Built in command handling • Extremely extensible • Can be used to search by any file characteristic
Advanced find • Example 1: • find /u/msaba/public_html -type f -print | grep refs
Advanced find • Example 2: • find /u/msaba/public_html -type f -print | grep refs 2> errors
Advanced find • Example 3: • find /u/msaba/public_html -type f -print 2> errors | grep refs
Advanced find • Example 4: • find . -name "*" -exec grep -i "ocks" {} /dev/null \;
find: Directory Tree Mapping • Show the directory hierarchy of /tmp • $ find /tmp –type f –o –print • $ find /tmp –type d –print
Making find More Efficient • A better way of invoking grep large number of files is: • $ find . -type f -print | xargs grep ocks • $ find . -type f -print 2> errors | xargs grep ocks 2>> errors • This results in far fewer invocations of grep • Runs faster • Less system load
vi – The Only Editor You Need • Why learn vi? • You don’t always have a GUI interface • It’s on every UNIX system • It’s powerful • Try it, you’ll like it!
Let’s Edit Something • To create a new file • # vi new_file_name • # vi • To open a file for editing • # vi existing_file_name
Additional Modes for Starting vi • vi –r file • Recover the last saved version after a crash (rare) • vi –R file • Read-Only • vi +n file • Open with cursor at line number n • vi file1 file2 file3 • Open multiple files, move between using :n
A Cool Feature of vi • :r • Reads into the current file the contents of a file • :r file • OR • The output of a command • :r !command
Shell Scripts • The first line: • #!/usr/bash • The rest of the file: • Standard command sequences
A shell Script Example • cd public_html • vi bash.script • #!/usr/bash • cp –p /u/ux101fa*/jointest* /u/ux101fa*/public_html • sort -k 1 jointest1 > autojtest1s • sort -k 1 jointest2 > autojtest2s • join autojtest1s autojtest2s • $ . bash.script
What Should You Know? • How to Log on and off of a UNIX system • Be familiar with UNIX shells • Be familiar with the UNIX file system • UNIX Paths, ownership and permissions • Use UNIX commands • Use vi • Write a basic shell script
Question? • Anyone… anyone?
THANK YOU FOR ATTENDING Please fill out the Evaluation Form before leaving.