0% found this document useful (0 votes)
16 views99 pages

2 CPS393 ContinuingLinux

Uploaded by

Amirali Eslami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views99 pages

2 CPS393 ContinuingLinux

Uploaded by

Amirali Eslami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 99

C/CPS 393

Introduction to UNIX, C and C++


Prof. Alex Ufkes

Topic 2: Continuing Linux


Notice!

Obligatory copyright notice in the age of digital


delivery and online classrooms:

The copyright to this original work is held by Alex Ufkes. Students


registered in course C/CPS 393 can use this material for the purposes
of this course but no other use is permitted, and there can be no sale
or transfer or use of the work for any other purpose without explicit
permission of Alex Ufkes.

© Alex Ufkes, 2022 2


Course Administration

Announcements and grades will be posted on D2L.


I (Prof. Ufkes) will post my slides on D2L.
Dr. Woit is the course coordinator, all other course
resources can be found on her website:

https://www.cs.ryerson.ca/dwoit/courses/cps393/cmf393.html

© Alex Ufkes, 2022 3


This Week

Continuing Linux:
Glob constructs
Metacharacters
Other special characters
Find, head, tail commands

© Alex Ufkes, 2022 4


Glob Constructs

Globbing

Bash version of Regular Expression matching


Match over files, directory contents
Using glob constructs saves time and encourages
good naming convention.

? * [ ]
© Alex Ufkes, 2022 5
Glob Constructs

Globbing

When the shell encounters a glob construct:


1. Expand the construct
2. Execute the resulting command

? * [ ]
© Alex Ufkes, 2022 6
Glob Constructs: Basic Substitutions

This can be used to match any single character

? Multiple ? can match multiple characters

For example: The working directory contains the following:

lab1.txt lb2.txt new.txt pie.c


prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out
the ? will be replaced
ls lab?.txt it will match with anything follows
this
structure .

lab1.txt lab2.txt lab3.txt lab4.txt

© Alex Ufkes, 2022 7


Glob Constructs: Basic Substitutions

ls lab?.txt
? lab1.txt lab2.txt lab3.txt lab4.txt

© Alex Ufkes, 2022 8


Glob Constructs: Basic Substitutions

Using glob constructs saves time and encourages

? good naming convention.


What if the file names were like this instead?
lab1.txt lb2.txt new.txt pie.c
prog1.c Lab2.txt lab_3.txt prog2.c
top.c lab04.txt lab4.cpp a.out
How can we filter for lab descriptions?
Not so easy anymore!
Following a predictable and consistent naming
pattern saves time in the long run.
© Alex Ufkes, 2022 9
Glob Constructs: Basic Substitutions

? Substitute for multiple characters? Use multiple ?

lab1.txt lb2.txt new.txt pie.c


prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out

ls lab?.???
lab1.txt lab2.txt lab3.txt lab4.cpp
lab4.txt

© Alex Ufkes, 2022 10


Glob Constructs: Basic Substitutions

? Substitute for multiple characters? Use multiple ?

ls lab?.???

© Alex Ufkes, 2022 11


Glob Constructs: Basic Substitutions

The * symbol matches any sequence of characters

*
-

Includes the null string!

lab1.txt lb2.txt new.txt pie.c


prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out
matches with of characters
any sequence
ls lab?.*
.

lab1.txt lab2.txt lab3.txt lab4.cpp


lab4.txt

© Alex Ufkes, 2022 12


Glob Constructs: Basic Substitutions

lab1.txt lb2.txt new.txt pie.c

* prog1.c
top.c
ls lab?.*
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
prog2.c
a.out

© Alex Ufkes, 2022 13


The Meaning of Life

What is the answer to life, the universe, everything?

to the Galaxy: 42
*
What is the ASCII
symbol for 42?

Draw your own philosophical conclusions

© Alex Ufkes, 2022 14


Glob Constructs: Basic Substitutions

lab1.txt lb2.txt new.txt pie.c

* prog1.c
top.c
ls *.c*
lab2.txt
lab4.txt
# What prints?
lab3.txt
lab4.cpp
prog2.c
a.out

© Alex Ufkes, 2022 Very common for filtering based on file extension 15
Glob Constructs: Basic Substitutions

lab1.txt lb2.txt new.txt pie.c

* prog1.c
top.c
ls *.*t*
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
# What prints?
prog2.c
a.out

© Alex Ufkes, 2022 16


Glob Constructs: Basic Substitutions

lab1.txt lb2.txt new.txt pie.c

* prog1.c
top.c
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
prog2.c
a.out

ls *.* # What prints?


ls * # What prints?
ls *** # What prints?
ls ********* # What prints?
In all four cases, everything in the folder.
Remember that * can be the empty string.
When would the first example not print everything?
© Alex Ufkes, 2022 17
Glob Constructs: Basic Substitutions

In square brackets we can specify a sequence of characters.

[ ] If one matches, the substitution will be successful.


lab1.txt lb2.txt new.txt pie.c
prog1.c lab2.txt lab3.txt prog2.c
top.c lab4.txt lab4.cpp a.out

ls lab[123].txt
lab1.txt lab2.txt lab3.txt

ls lab[1-3].txt # Variation
lab1.txt lab2.txt lab3.txt
© Alex Ufkes, 2022 18
Glob Constructs: Basic Substitutions

lab1.txt lb2.txt new.txt pie.c

[ ] prog1.c
top.c
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
prog2.c
a.out

ls lab[1-3].txt # Variation
lab1.txt lab2.txt lab3.txt
Can use a dash to specify a range of values.
[1-3], [A-Z], [a-z], etc.
Can also do [A-Za-z]
[A-z] is based on ASCII ordering.
Careful!
© Alex Ufkes, 2022 19
From Dr. Notes

Optional: If you want to change the collate order in the shell to


ASCII you can execute:

shopt -s globasciiranges

Once you do that, then the ordering is A-Za-z (see man ASCII for
order), and a command such as ls [A-Z]* will list only those files
starting with an upper-case letter, and ls [a-z] only those starting
with lower-case letters. In F21, globasciiranges defaults to off, so
you DO need to set it, if you want it.

It appears to be set for me in F2022, but YMMV


© Alex Ufkes, 2022 20
[A-z] without ASCII ordering is: A,a,B,b,C,c,D,d,E,e, etc.
[A-z] with ASCII ordering is: a,b,c x,y,z

[A-Z] without ASCII ordering captures all letters,

[A-Z] with ASCII ordering captures all capital letters


and no lower case

ASCII ordering is enabled on moon

© Alex Ufkes, 2022 21


ASCII ordering seems to be set out of the box this year.

© Alex Ufkes, 2022 22


Glob Constructs: Basic Substitutions

More examples:

[ ] [1-58]
[!ABC]
[!0-9]
1, 2, 3, 4, 5, 8. NOT 1 to fifty-eight!
Matches any character except A,B,C
Matches anything except digits
What happens if nothing matches? Cannot access:

© Alex Ufkes, 2022 23


Glob Constructs: Basic Substitutions

[ ]

© Alex Ufkes, 2022 24


Glob Constructs: Character Classes

[:alnum:] Alphanumeric (A-Z, a-z, 0-9)


[:alpha:] Alphabetic (A-Z, a-z)
[ ] [:upper:]
[:lower:]
Uppercase letters
Lowercase letters

© Alex Ufkes, 2022 25


Glob Constructs: Character Classes

Notice the double brackets: [[, ]]


They can also be used as part of an outer bracket expression,
[ ] meaning we can combine multiple classes. E.g
ls lab[[:digit:][:lower:]].*

© Alex Ufkes, 2022 26


Glob Constructs: Character Classes

Notice the double brackets: [[, ]]


They can also be used as part of an outer bracket expression,
[ ] meaning we can combine multiple classes. E.g
ls lab[[:digit:][:lower:]].*

No labY.txt, no labZ.txt
© Alex Ufkes, 2022 27
Glob Constructs: Character Classes

[:upper:] VS [A-Z] ?

[ ] always give all upper case.


[A-Z] can include lower case, depending on system ordering.

© Alex Ufkes, 2022 28


Glob Constructs: Character Classes

[ ] Observe that, with ASCII ordering:


[[:alnum:]] == [a-zA-Z0-9]
[[:alpha:]] == [a-zA-Z]
[[:alpha:]] == [[:upper:][:lower:]]
[[:alnum:]] == [[:upper:][:lower:][:digit:]]

And so on.

© Alex Ufkes, 2022 29


Glob Constructs: Character Classes

[ ]
A couple others, less common, but still useful:

[:blank:] Blank characters (space and tab)


[:punct:] Punctuation characters, (ASCII only):
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
[:graph:] Graphical characters:
[[:alnum:][:punct:]]
[:print:] Printable characters:
[[:alnum:][:punct:]] and space
[:xdigit:] Hexadecimal digits: [0-9a-f]

© Alex Ufkes, 2022 30


Glob Constructs: Character Classes

[ ] good description of bracket expressions in general:

https://www.gnu.org/software/grep/manual/
html_node/Character-Classes-and-Bracket-
Expressions.html

© Alex Ufkes, 2022 31


Other Special Characters

~ Refers to your home directory

~usr Home directory of usr. E.g. cd ~dwoit

~- Previous working directory. E.g. cd ~-


In Bash, this is the same as cd -

~+ Current working directory.

© Alex Ufkes, 2022 32


Useful Usage

cd - Use this repeatedly to toggle between directories:

© Alex Ufkes, 2022 33


New Command: echo

echo Prints argument to stdout:


Can use special characters, can use glob constructs!
Can redirect to a file if we want!

© Alex Ufkes, 2022 34


© Alex Ufkes, 2022 35
Echo contents of
working directory

© Alex Ufkes, 2022 36


anything, simply echo the construct itself

© Alex Ufkes, 2022 37


Ignoring Glob Constructs

What if we want to echo the glob construct, and not expand it?

Use quotes:

Can also
use \

© Alex Ufkes, 2022 38


Ignoring Glob Constructs

What if we want to echo the glob construct, and not expand it?

© Alex Ufkes, 2022 39


Ignoring Glob Constructs

Short and sweet summary:


Glob constructs are not expanded if

The \ character, before a special


character (e.g. \[) operates like quotes.
Useful when echoing, useful in many
other situations as well.

© Alex Ufkes, 2022 40


*
Patterns & Regular Expressions

*(exp) 0 or more occurrences of exp

+(exp) 1 or more occurrences of exp

?(exp) 0 or 1 occurrences of exp

!(exp) Anything that does not match exp

© Alex Ufkes, 2022 41


What Prints?

A Ax Axxx Axxxx Ay X x X.bak xx xxxx

ls A*(x) A Ax Axxx Axxxx


ls A*(xx) A Axxxx
ls A+(x) Ax Axxx Axxxx
ls A?(x) A Ax
ls X?(bak) X X.bak
ls @(*xx|*ak) Axxx Axxxx X.bak xx xxxx
ls !(@(*xx|*ak)) A Ax Ay X x

© Alex Ufkes, 2022 42


© Alex Ufkes, 2022 43
Check the Manual!

man bash

© Alex Ufkes, 2022 44


Check the Manual!

Search using /

Keep doing
/Pattern Matching
to find next occurrence

Do / then UP to speed
things up.

© Alex Ufkes, 2022 45


© Alex Ufkes, 2022 46
to the man pages
on your tests.
Practice using them
instead of Google.

© Alex Ufkes, 2022 47


Practice Exercise: From Dr. Notes

Give a command to list all filenames that:


Contain the letter "e" ls * e *

Have a total of 7 letters and no extension ls [ 1.) [ ][ ][ ] [ 1.) [ ][ ] i. 1.1 .


1.1 .
1.1 .
i. 1.1 .
1.1 .

Are 7 characters long ls ? ? ? ? ? ? ?


Have 3 letter extensions ls [ ] [ ][ * •
! ! 11 .
1.1-3

Have 3 letter alpha-numeric extensions ls [ ][A ][A * .


A -20-9 -20-9 -20-9 ]

Contain the word "tst" *somewhere* in the name ls * tsttx

Start with the letter "A" and contained a dash (-) ls A- * _


*

Are at least 2 characters long and do not have the following letters
anywhere in the first 2 characters: a,b,c,f,h,x,y,z ls [ _hxyz][!a -hxyz a- of
-

of ] *

End in .ex with an optional c at the end ls ?¢ ) * .


ex

Are not java or text files (do not end in .jav or .txt) ls !(O( jar *
-
14*+11

End in a : followed by a digit from 2-8 followed zero or more digits.


ls * :[ 2-8 ] * ( [ 0-931

© Alex Ufkes, 2022 48


Interactive Shell Use

Typing in commands and executing them one at a time


Handy for performing small, one-off tasks quickly
Not so good for complicated things that may need to
be repeated

© Alex Ufkes, 2022 49


Interactive Shell Use

You may be familiar with the interactive shell in Python,


depending on which IDE you prefer:

Fine for simple calculations and tests


Not so fine when our program needs to
do more than one thing at a time.
script
Same idea with the Bash shell
Time to move on to writing Bash scripts

© Alex Ufkes, 2022 50


Bash Scripting

We can write and execute a Bash script.

In the simplest case, a bash script is a sequence of individual


shell commands that execute in order, top to bottom.

© Alex Ufkes, 2022 51


Hello, Bash!

Script name is hellobash


Contains a handful of commands,

How do we execute it?


Use ./hellobash

© Alex Ufkes, 2022 52


Hello, Bash!

Oh no!
Why is permission denied?
I created the script file.

© Alex Ufkes, 2022 53


Hello, Bash!

Default user permission is rw-

How can I give the user execute permissions?

chmod u+x hellobash


A few ways,
chmod u=rwx hellobash
using chmod: chmod 700 hellobash

© Alex Ufkes, 2022 54


Hello, Bash!

Much better!

© Alex Ufkes, 2022 55


Shell Script Arguments

Arguments can be passed into a script when executing


args to other commands
Inside our script, we access args using the $ prefix

$1 $2 $3 etc. First, second, third, arg


$0 Name of shell script
$# The number of args passed
$@ Lists args as one string: "arg1 arg2 ..."
$* Lists args as separate: "arg1" "arg2" ...

Inside a script, we can use echo and print each of these

© Alex Ufkes, 2022 56


Shell Script Arguments

Common comment
structure at the top of
a Bash program

Bash program will


terminate when it ends,
but can do so explicitly
as well with exit

© Alex Ufkes, 2022 57


Shell Script Arguments

No args passed? $1
and $2 are empty

© Alex Ufkes, 2022 58


Shell Script Arguments

Lots more to say about shell scripting

as we learn more Linux commands.

© Alex Ufkes, 2022 59


grep
Big font for a big topic!

© Alex Ufkes, 2022 60


grep

Global Regular Expression Print


(Globally search for a regular expression and print it)

grep string filename(s)


String The string to search for
Filename(s) One or more files to search in
Result? Displays the lines of the file containing the string

grep string

Without a filename, grep will read from stdin until EOF.

© Alex Ufkes, 2022 61


Example: grep

© Alex Ufkes, 2022 62


Multiple Files

File
Line contents
name:

© Alex Ufkes, 2022 63


grep: Options

-i Ignore case
-v Print lines not matching search string
-x search string must match entire line

© Alex Ufkes, 2022 64


grep: Regular Expressions

The grep search string can contain regular expressions, as


well as many glob-

work the same way as part of a grep string.

© Alex Ufkes, 2022 65


grep: Metacharacters

. Substitutes for any character, except \n


Like ? In glob
0 or more repetitions of previous character
* Very much NOT like glob!

grep A*xx gfile.txt Axxx, Axxxx, xx, xxxx


grep .xx gfile.txt Axxx, Axxxx, xxxx
grep ..... gfile.txt Axxxx, X.bak
grep Ay* gfile.txt A, Ax, Axxx, Axxxx, Ay

© Alex Ufkes, 2022 66


grep: Metacharacters

. Beginning of line: pattern must


*
^ be at the start of the line
End of line: Pattern must be at
$ the end of a line

© Alex Ufkes, 2022 67


grep: Metacharacters

.
*
[...] Like glob, any single character inside brackets

^ [^...] Like glob, any single character not inside brackets


Note that ^ works in glob, can use ! or ^ in glob
$ ! May or may not work in grep regex

© Alex Ufkes, 2022 68


grep: Metacharacters

.
*
\{m\} Exactly m repetitions of previous character

^ \{m,\} At least m repetitions of previous character


$ Between m and n repetitions of previous
[...] \{m,n\} character, inclusive
[^...]
Notice: In these expressions, the \ is acting
as an escape character.

© Alex Ufkes, 2022 69


grep: Metacharacters

. \{m\} Exactly m repetitions of previous character


* \{m,\} At least m repetitions of previous character
\{m,n\} Between m and n repetitions of previous character
^
$
[...]
[^...]
}
the line
anywhere × 's
in
sequentially 3×4
sub string
containing 3

any sequentially

© Alex Ufkes, 2022 70


grep: Metacharacters

. \{m\} Exactly m repetitions of previous character


* \{m,\} At least m repetitions of previous character
\{m,n\} Between m and n repetitions of previous character
^
$
[...]
If exactly three xxx are found, then at
[^...] least three xxx are also found.
If at least three xxx are found, then
between three and n xxx will be found.
When would these have different uses?

© Alex Ufkes, 2022 71


grep: Metacharacters

. \{m\} Exactly m repetitions of previous character


* \{m,\} At least m repetitions of previous character
\{m,n\} Between m and n repetitions of previous character
^
$ Consider additional flanking characters/expressions:
Capitan and followed by capital A
[...] 2*5
,
gowning

[^...] \{2\ AxxA


\{2,4\ AxxA, AxxxxA
\{1,\ Axa, AxxA, AxxxxA

© Alex Ufkes, 2022 72


grep: Metacharacters

. \{m\} Exactly m repetitions of previous character


* \{m,\} At least m repetitions of previous character
\{m,n\} Between m and n repetitions of previous character
^
$
[...]
[^...]

© Alex Ufkes, 2022 73


grep: Metacharacters

.
* \< Beginning of word
^ \> End of word
$
[...]
[^...]
\{m\}
\{m,\}
Lines start with lowercase a

\{m,n\}
✗ is end of a word it matches

© Alex Ufkes, 2022 74


grep: Metacharacters

.
* Final note:
^ Mentioned earlier, \ acts as an escape character.
$ To match a special character, must use \
[...]
[^...]
\{m\} \$\{2\}\ myfile
\{m,\}
\{m,n\} What is this command going to search for?
\< Lines with exactly two occurrences of $ at
\> the end of a word

© Alex Ufkes, 2022 75


grep: More Examples

From Dr. Notes:


grep '^Assignment' fname # lines starting with Assignment
grep -v '^Assignment' fname # lines not starting with Assignment
grep 'Assignment$' fname # lines ending with Assignment
grep 'd.g' fname # lines containing dag, dbg, dcg, d0g, d1g, etc
grep 'su*m' fname # lines containing sm, sum, suum, suuum, etc
grep 'suu*m' fname # lines containing sum, suum, suuum, etc
grep '\<so\>' fname # the word so (vs. social, absolute)
grep '[A-Za-z][A-Za-z]*' fname # lines containing ANY non-empty alpha string
grep '^[A-Za-z][A-Za-z]*$' fname # lines containing ONLY alpha characters
grep 'xyz\.w\{2,3\}X' # lines containing xyz.wwX or xyz.wwwX
grep 'A(bc)\{2,3\}D' # lines containing AbcbcD or AbcbcbcD
grep -x "abc" # lines that contain exactly and only abc

© Alex Ufkes, 2022 76


Extended Regular Expressions

grep -E -or- egrep

Adds a few additional metacharacters


Adds slightly different usage to some
existing metacharacters

Perhaps the most useful QoL feature? Curly braces { }


\

© Alex Ufkes, 2022 77


Extended Regular Expressions

A couple other features: Line match

| Logical OR:
grep - dog|cat|bird fname
Lines with at least 1 of dog, cat, or bird

+ 1 or more repetitions of previous character


grep - -Za- fname
Lines with at least one alpha char
(Recall * is zero or more)
"
*
]
" ] [ A za
-

z
EA Za 2
-
-
-

E
grep -

© Alex Ufkes, 2022 78


Extended Regular Expressions

A couple other features:

() Performs substring grouping:


grep - fname
Two or three consecutive substrings ab
Lines containing AababB or AabababB

\n Backreferencing
\1 replaced with first substring, \2
replaced with 2nd substring, etc.
grep - aei]+)B\ myfile
Here, \1 gets replaced with ([aei]+)
© Alex Ufkes, 2022 79
More Practice Problems
"

dog
"
+ name
grep
1. List all lines in fname containing the string "dog". " "

K dog I

2. List all lines in fname containing the word "dog". grep


>
frame

3. List all lines in fname containing the string "dog" at the beginning of a line. grep "
^

dog
"

+
name

4. List all lines in fname containing the word "dog" at the beginning of a line. grep " ^
I <
dog I >
"

1- name

5. List all lines in fname containing exactly 6 "a"s in a row. grep "
aaaaaa
"

frame

6. List all lines in fname containing one or more words that start with 93 and
end in a sequence of 1 or more W. gnep "

7. Give 2 different grep commands that will list lines of fname that end in a
lower-case vowel.
8. Use the man page to find the option of grep that shows the matched string
in a different color. Try it.

© Alex Ufkes, 2022 80


searches file system

Find

© Alex Ufkes, 2022 81


Find: By Type

grep searches files, find searches the filesystem

find <path> -options <additional args>


files

find . -type f List all files rooted in current directory



directory
find . -type d List all folders rooted in current directory

Recursively finds in subdirectories as well

© Alex Ufkes, 2022 82


Find: By Type

find /usr/courses -type d

filesystem has much more

© Alex Ufkes, 2022 83


vi.% Find: By Type

find /usr/courses -type d

Command taking too long?


Use CTRL-C to terminate it:

True for most commands.


Shell seems frozen, not
responding? Try CTRL-C
CTRL-C sends SIGINT to
foreground program
Usually causes termination

© Alex Ufkes, 2022 84


Find: By Name
anything not c in * he directory
p

find /usr/[!c]* -name tali 2>/dev/null

Glob construct! -name Find by name, tali


Expands first. /usr/[!c]* Search /usr/*, but exclude
folders starting with c
2>/dev/null redirect stderr to /dev/null

Why redirect stderr?

Permission denied results in message to stderr, can clog terminal.

© Alex Ufkes, 2022 85


Find: By Name

find /usr/[!c]* -name tali

Searching the filesystem close to


the root can take a long time.
Your terminal might appear

© Alex Ufkes, 2022 86


Find: By Name

Can also exclude using prune option:

courses directory
exclude ,

find /usr -name courses -prune -o -name tali

Does not recurse down Find by name,


dir /usr/courses tali

-o acts as logical OR
Link together options

© Alex Ufkes, 2022 87


Notice!
usr/courses/
Still prints directory name though!
Y

Stop this by adding print option


Only those directories that were
searched get printed

© Alex Ufkes, 2022 88


© Alex Ufkes, 2022 89
Find: By Name

Can also exclude using prune option:


cd /usr/courses/cps393/dwoit/courseNotes/Programs/c
find . - # Try it. Note results from ./c1 ./c2 ./c3 and ./c4

© Alex Ufkes, 2022 90


Find: By Name

Can also exclude using prune option:


cd /usr/courses/cps393/dwoit/courseNotes/Programs/c

find . -name c2 -prune -o -name "m*.c" -print

Does not recurse -print says print from


down dir ./c2 directories that recursed.
Otherwise, ./c2 itself is
still printed

© Alex Ufkes, 2022 91


© Alex Ufkes, 2022 92
Find: By Permission

find . -type d -perm -g+r,u+r # all dirs rooted in current dir that are readable by
BOTH owner and group
find . -type d -perm /g+r,u+r # are readable by EITHER or BOTH owner and group
find . -type d -perm /a+w # have at least one write bit set
find . -type d ! -perm /a+x # are not executable for anybody
find . -type d ! -perm -a+x # are not executable for at least one of u,g,o

LOTS of ways to specify perms - see man find

© Alex Ufkes, 2022 93


Find: Practice Exercise

This is a great exercise! Combines many previous concepts:

1. Use the "find" command to list all files in directory /bin that start with the letter "m".
2. What would happen if we did not put a name such as "*.jav" in quotes in the find
command? Make sure you have some .jav files in your directory and try it out.
3. Look at the man pages for "find" to determine what the option "-mtime" does in the find
command. Then...Figure out a sequence of commands (involving a find, temp file and a
grep) to display those files in the current directory whose contents were changed within
the last 24 hours and whose names contain the string "tst". Hint: Output of find becomes
input to grep. How can you do this?
4. Make a shell program to do question 3 above. Call your program tstRecent. Your program
should delete any temporary files it creates. Test it to make sure your program works.
5. -maxdepth option works. Use -maxdepth 1 to
list all files in the given directory only (not the whole filetree under it)

© Alex Ufkes, 2022 94


Head and Tail

Not too exciting:

head sends to stdout the first 10 lines of stdin


tail sends to stdout the last 10 lines of stdin

Redirect to use with files (can also pass file as arg):

head <myfile
# Displays first 10 lines of myfile on screen to
brandnew file
10 lines

Prints last
tail <myfile >end.of.myfile
# Copies of last 10 lines of myfile into file called end.of.myfile

© Alex Ufkes, 2022 95


Head and Tail

Specify number of lines with options:


head -20 f1 # First 20 lines of file f1
tail -3 # Last 3 lines of stdin
option -n-X for head and -n+X for tail:

tail -n+3 f1 # all lines of f1 starting at line 3


head -n-2 f1 # all but the last 2 lines of f1

Shortcut:
tail -n-3 same as tail -3
head -n+2 same as head -2
© Alex Ufkes, 2022 96
Week 2

© Alex Ufkes, 2022 97


In Summary

Continuing Linux:
Glob constructs
Metacharacters
Other special characters
Find, head, tail commands

© Alex Ufkes, 2022 98


© Alex Ufkes, 2022 99

You might also like