2 CPS393 ContinuingLinux
2 CPS393 ContinuingLinux
https://www.cs.ryerson.ca/dwoit/courses/cps393/cmf393.html
Continuing Linux:
Glob constructs
Metacharacters
Other special characters
Find, head, tail commands
Globbing
? * [ ]
© Alex Ufkes, 2022 5
Glob Constructs
Globbing
? * [ ]
© Alex Ufkes, 2022 6
Glob Constructs: Basic Substitutions
ls lab?.txt
? lab1.txt lab2.txt lab3.txt lab4.txt
ls lab?.???
lab1.txt lab2.txt lab3.txt lab4.cpp
lab4.txt
ls lab?.???
*
-
* prog1.c
top.c
ls lab?.*
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
prog2.c
a.out
to the Galaxy: 42
*
What is the ASCII
symbol for 42?
* 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
* prog1.c
top.c
ls *.*t*
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
# What prints?
prog2.c
a.out
* prog1.c
top.c
lab2.txt
lab4.txt
lab3.txt
lab4.cpp
prog2.c
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
[ ] 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
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.
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:
[ ]
No labY.txt, no labZ.txt
© Alex Ufkes, 2022 27
Glob Constructs: Character Classes
[:upper:] VS [A-Z] ?
And so on.
[ ]
A couple others, less common, but still useful:
https://www.gnu.org/software/grep/manual/
html_node/Character-Classes-and-Bracket-
Expressions.html
What if we want to echo the glob construct, and not expand it?
Use quotes:
Can also
use \
What if we want to echo the glob construct, and not expand it?
man bash
Search using /
Keep doing
/Pattern Matching
to find next occurrence
Do / then UP to speed
things up.
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 ] *
Are not java or text files (do not end in .jav or .txt) ls !(O( jar *
-
14*+11
Oh no!
Why is permission denied?
I created the script file.
Much better!
Common comment
structure at the top of
a Bash program
No args passed? $1
and $2 are empty
grep string
File
Line contents
name:
-i Ignore case
-v Print lines not matching search string
-x search string must match entire line
.
*
[...] Like glob, any single character inside brackets
.
*
\{m\} Exactly m repetitions of previous character
any sequentially
.
* \< Beginning of word
^ \> End of word
$
[...]
[^...]
\{m\}
\{m,\}
Lines start with lowercase a
\{m,n\}
✗ is end of a word it matches
.
* 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
| Logical OR:
grep - dog|cat|bird fname
Lines with at least 1 of dog, cat, or bird
z
EA Za 2
-
-
-
E
grep -
\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
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.
Find
courses directory
exclude ,
-o acts as logical OR
Link together options
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
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)
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
Shortcut:
tail -n-3 same as tail -3
head -n+2 same as head -2
© Alex Ufkes, 2022 96
Week 2
Continuing Linux:
Glob constructs
Metacharacters
Other special characters
Find, head, tail commands