Unix
what does ! do?
! recalls commands
Write a script that takes two command line arguments between 0 and 5. Verify the user has entered two arguments. Print error message and exit if no arguments were entered or if less than two were entered.
#!/bin/bash if [ $# -lt 1 ] then echo Two integers are required, please provide two. exit 1 elif [ $# -lt 2 ] then echo Please provide two integers between o and 5 exit 2 fi
write a simple script that accepts a pattern and a filename as arguments and then counts the number of occurrences of the pattern in the file.
#!/bin/bash echo "enter a filename to read throught:" read filename echo what string do you want to find? read str grep -o $str $filename | wc -l echo done
Devise wild-card patterns to match all filenames that begin with a dot and end with .swp
.*.swp
in VI how would you replace has with have in the current line?
:s/has/have/g
what is the difference between > and >>?
> kills file and >> appends file
how do these differ? [0-9]* and [0-9][0-9]*
First pattern just requires that it starts with 1 number, while the second requires it starts with 2 numbers
how do these differ? ^[^^] and ^^^
The ^[^^] found lines that started with either / or //. So :%s/^[^^]/HH/g replaced / or // with HH The [^^^] matched any line starting with / So :%s/[^^^]/HH/g replaced any line staring with / or // with all H's.
how is it possible to have two files with the same name in your account?
Two different absolute paths to each file
if you have a hard linked file called BOB which is linked to /home/stuart and you remove /home/stuart, what happens when you type more BOB?
You will have the contents of the file stuart as BOB has a copy of the contents
if you have a soft linked file called KING to the file /home/bob, what happens when you remove the file /home/bob and type more KING?
You will receive an error message as the file is gone. The soft link relies on the original file.
3 ways of exiting VI
ZZ q q! wq
Devise wild-card patterns to match the following filenames: foo1, foo2, foo5, Foo1,Foo2,and Foo5
[Ff]oo[1,2,5]
Devise wild-card patterns to match the following filenames: watch.htm, watch.HTML and Watch.html
[wW]atch.[hH][tT][mM][!a-kmz]
*[!0-9] what does this wildcard pattern match?
anything that does not end with a number
*[!s][!h] what does this wildcard pattern match?
anything with a 2 character extension, the first character not an s and the second not an h
if foo does not exist what will the command who>>foo do?
append the results of the who command onto the end of foo
A in VI
appends to end of line
what does VI command I mean
beginning of line insert
[A-z]????* what does this wildcard pattern match?
capital or lowercase letter, then any 4 characters, then anything
how would i create a file GREAT with "I am a file. LOve Me. " in it?
cat > GREAT I am a file. Love me. ^D
How would you concatenate two files foo1 and foo2 but also insert text after foo1 and before foo2 from the terminal.
cat foo1 - foo2 >filename
in VI how would you change the current line completely?
cc or R making sure you"clean up" or d$ and insert new text
if you are in a directory 7 levels below root. How would you change your current directory to one that is two levels above where you are now?
cd ../..
Change your working directory to root, do it with an absolute path. Return to home directory using the tilde way. Then Change to root using a relative path. Now return to your working directory using only a two letter command. Verify that you are in your home directory.
cd / cd ~ cd ../.. cd pwd
in directory called /home/users/student ; how would you change to your home directory (list two ways) to home/users/student/new/files
cd/home/users/student/new/files or cd ../new/files
change the protection on file X so that it has the following permissions: You have only execute permission, your group members have read and execute permission and all others have no permission.
chmod u=x, g=rx, o= X
what does cmp file1 file 2 do?
cmp command will compare file 1 and file 2
what does cp do?
copy
yy in VI
copy
how do you recursively copy one directory to an existing directory
cp -R dir1 dir2
how would I copy files 1, 2, and 3 from my current directory into /home/town? (give two options)
cp 1 2 3 /home/town or cp 1 2 3 ../town
how do you copy a file to a directory?
cp file dir
copy foo to a subdirectory two and bar to a subdirectory three
cp foo one/two cp bar one/two/three
copy a file named null to foo
cp null foo
in VI how do you delete text from current line to beginning of the file
d1G
x in VI
delete a character
what does the command rm* do?
delete all files except for hidden and subdirectories
#x in VI
deletes that # of times
what does bc do?
does calculations
How would I find all the files in and under root that are called errno.h?
find / -name errno.h
what is the format to use the find command?
find / -name filename > newfile 2> /dev/null the dev/null pulls out errors and sends to null folder
what is the command to use find and grep to locate all C programs in the home directory tree that contains the words "void main("
find /home -name "*.cpp" 2>/dev/null |xargs grep "void main("
how would you locate all files in /mydir and its subdirectories that contain a file named foo.bar?
find /mydir -name foo.bar
what is the difference between echo "$SHELL" and echo '$SHELL'
first shows the shell you are using (/bin/bash). the second displays the $PATH
Devise wild-card patterns to match the following filenames: foo1, foo2, and foo5
foo[1,2,5]
How would I find all the files that have the words "I did it" in them?
grep "I did it" *
how do I grep a file?
grep "words searching for"*
if all the names of my files in my home directory were changed and I really needed to find a file with the words "you owe me" , how would I find the file?
grep "you owe me" *
more .profile
hidden file in environment
Write a script that confirms the two numbers entered are within a specified range of 0-5. If not print out message and exit.
if [ $1 -lt 0 ] || [ $1 -gt 5 ] then echo Your first number needs to be between 0 and 5. exit 1 fi if [ $2 -lt 0 ] || [ $2 -gt 5 ] then echo Your first number needs to be between 0 and 5. exit 2 fi
what is an if statement form?
if condition is true do something else do something else fi
what does rm -i* do
inquires before you delete everything
J in VI
join two lines
what does ctrl c do?
kills whats in the background
what does ls do?
lists
what does ls -l do?
lists all but hidden files
what does ls -R do?
lists all files in current directory and all subdirectories
what does ls -a do?
lists all files including hidden files
how do you create a soft link called KING to the file /home/Bob
ln -s /home/Bob KING
how do you create a hard link called BOB to the file /home/Stuart
ln /home/Stuart BOB
Create a hard link called FOO to the file foo and a soft link BAR to bar. Check the inodes of FOO and BAR. Delete foo and bar. Do an ls on FOO and BAR.
ln foo FOO ln -s bar BAR ls -i FOO BAR rm foo bar ls FOO BAR
what does cat do?
look at files, creates files as well if not already created
what does grep "^\*" look for?
looking for a line that starts with the asterisk
I want to create a file called FOO that contains a list of all the files in my directory. I want one per line. How would I do this?
ls -1 > FOO
list all files in directory one and it's subdirectories
ls -R one
how would you list all the files in and below your current directory that have the second letter W, the 5th letter Z?
ls -Ra ?W??Z*
how would you list all files in your current directory
ls -a
list all files in your current directory and all its subdirectories
ls -aR
how would you list all the files in and below your current directory that have the second letter W, the 5th letter Z, and end in a 7?
ls -aR ?W??Z*7
to show the full list of attributes of files null and NULL
ls -l null NULL
append the contents of the ls command onto the end of file NAME
ls >>NAME
how would you find out what the split command does?
man split
*[0-9]* what does this wildcard pattern match?
match at least one number
how would you create the directory A/B/C within your current directory?
mkdir -p A/B/C
Create a directory called fruit/twigs/nuts. From you current directory put grapes under fruit, stick under twigs and almonds under nuts.
mkdir -p fruit/twigs/nuts cp grapes fruit cp stick fruit/twigs cp almonds fruit/twigs/nuts
create a subdirectory called one and then a subdirectory of one called two, and then a subdirectory of two called three (complete in one step)
mkdir -p one/two/three
create a subdirectory called one and then a subdirectory of one called two, and then a subdirectory of two called three (complete in steps)
mkdir one cd one mkdir two cd two mkdir three
what is %
modulus- tells what the remainder is, and therefore tells if number is odd or even, all odd =1
what does more do?
more with a filename will show what is in a file
W in VI
moves to next words
how do you recursively move one directory to an existing directory
mv -R dir1 dir2
move file NULL to file bar
mv NULL bar
how would you rename the file called test.txt to new.dat
mv test.txt new.dat
o in VI
new line after cursor
O in VI
new line before cursor
if you start at /home/romeo and use the command mkdir ..bin
nothing as don't have privileges beyond home
if you start at /home/romeo and use the command rmdir?
nothing as the directory is not empty as you are in that directory
write a script where you print the sum of two numbers entered and the remainder of dividing the sum by 3
num=$(( $1 + $2 )) num2=$(( $num % 3 )) echo The sum of $1 and $2 is $num echo The remainder of $num divided by three is $num2
P in VI
paste Puts before line
p in VI
pastes puts after
in VI how will you add /* at the beginning of a line and */ at the end?
put in the /* before each line: :%?^?/*?g Put in */ after each line: :%?$?*/?g
How do you find out what directory you are in?
pwd
Devise wild-card patterns to match the following filenames: quit.c, quiot.o, and quit.g
quit.[cog]
write a script that asks the user to enter a filename. If the username is null, yell at the user and exit, if the filename does not exist create any empty file with that name. If the filename exists but is a directory, yell at the user and exit with a value.
read -p "please enter a filename: " filename if [ -z "$filename" ] then echo A filename needs to be entered. exit 5 fi if [ ! -f "${filename}" ] then touch $filename fi if [ -d "${filename}" ] then echo $filename is a directory. Please enter a new filename. exit 6 fi
what does !t do?
recalls last line with letter t
what does rm - rf* do?
recursively deltes the files in your directory except hidden and includes protected with u-w
If foo contains data what will the command cat>foo do?
remove the contents of foo
#dd in VI
removes # of lines
d^ in VI
removes beginning of line before cursor
d$ in VI
removes end of line after cursor
dd in VI
removes entire line
what will mv dir 1 dir2 do when dir2 does not exist
renames dir1 to dir2
what will mv dir 1 dir2 do when dir2 exists?
renames dir1 to dir2
How would you removed: /home/dir/mary (if mary is a directory)
rm -R /home/dir/mary
delete the fruit/twigs/nuts directory with one command
rm -R fruit/twigs/nuts
Delete a directory named one and everything below it
rm -R one
How would you remove: /home/mike (if mike is a file)
rm /home/mike
removal all files that have yz as sthe 3rd and 4th letters and end in a 9
rm ??yz*9
remove a file name bozo
rm bozo
delete file null
rm null
what is the difference between rmdir and rm -R?
rmdir works to delete an empty directory (after you have all ready deleted the files that were in the directory) rm -R deletes all files in the directory and all sub directories below it
#w
skips # of words
for statement: add the number from 2 to 34
sum = 0 for I = 2 to 34 sum = sum + 1
what does the finger command do?
tells you who is on the system
move the file IDEAS from current directory to a subdirectory named WORK and you enter the command mv IDEAS WORK when you examine the work directory the file is not listed. What happened?
the file has been renamed to WORK within your current directory
what happens if you cat foo>foo
this command fails because a file cannot overwrite itself
In VI how would you insert a line above the current line and below it?
to insert above O to insert below o
what is grep used for?
to search for words within a file
create empty files called null and NULL
touch null NULL
u in VI
undo
in VI, five contiguous lines contain only lowercase letters. How would you combine them into one line and then convert the entire line to uppercase?
use J 4 times J to join the lines, type ^ to put the cursor in column 1 and then 400 ~ from column 1 to change lowercase to upper
write name
used to send message
How would I get a word count of the file FOO?
wc FOO
write a script where the user inputs two integers, if either integer is a 0 then add 1 to both numbers entered, and then use a while loop to print out the integer values from -3 to the product of the two numbers entered.
while [ $1 -eq 0 ] || [ $2 -eq 0 ] do let a=$1+1 let b=$2+1 let c="$(( $a * $b )) " start=-3 end=$c s=$start while [[ $s -le $end ]] do echo "$s" ((s=s + 1 )) done echo the product of $a and $b is $c break done
while statement
while something is true do stuff end while is no longer true
if I take over a session from someone, how do I know who I am?
whoami
:r filename in VI
will insert filename into file working on
what does cat file > file2 do?
will rename file to file 2
grep [Hh]is filename
will search filename for His and his
to send files
write person name < filename
in VI how do you copy 10 characters?
yank copy 10 characters with 10yl (el) Letter I moves a space to the right note, y6l (y 6 el) also works
in VI how do you copy 10 words?
yank copy 10 words with 10yw
how do you copy a directory to a file?
you cannot copy a directory to a file
if you start at /home/romeo and use the command cd ../.. what will happen?
you will move up two levels