Globbing
- Linux shell supports globbing patterns as a means of matching filenames - globbing patterns support several wildcard characters/syntax to create patterns that are then checked against filenames
- ? - match any one character
- e.g.
?.txt
- match file name with any one character that is then followed by .txt
- to match two any two characters, you can use two ? symbo;s
- e.g.
CS???.txt
- matches any filenames beginning with CS followed by any 3 characters followed by .txt
-
-
- match 0+ characters
- e.g.
*.txt
- match any file name ending with .txt
- [] - specify one character from a set of characters
- e.g. [aeiou].txt - matches any filename that has a filename of a.txt, e.txt, etc, followed by .txt
- character ranges can also be specified, e.g. [1-9], [a-z]
- [!] - match any one character aside from those specified in set
- e.g.
[!aeiou].txt
- match any single character file name that is not vowel
- {} - technically not a globbing pattern, but means of saying “or”
- e.g.
*.{cc, cpp}
- match all .cc, .ccp files
- To suppress globbing capability, use single/double quotes around string
- one application is using
find
- can use globbing pattern to search for files
- use quotes for example, in
find /usr/share/dict -name "w*"
- direct shell to not use globbing, forward globbing pattern to find
itself
Pipes
- Linux shell supports ability to redirect output generated by one program to the output of another program - achieved with a pipe
- Linux pipe used to connect standard output stream of a program to the standard input stream of a program that executes right after the termination of the first program
- e.g. get first 20 lines of a file, find word count
- one solution: run
head -n 20 sample.txt > first20.txt
and wc -w < first20.txt
- using pipe:
head -n 20 sample.txt | wc -w
, or cat sample.txt | head -n 20 | wc -w
- results from sample.txt are piped into standard input stream of head
- e.g. Print duplicate-free list of words occuring in any files
words*.txt
cat words*.txt | sort | uniq
Embedded Commands
- We can also embed the output of a command in a string - provide output of command as an argument to another program/command
- e.g.
echo "It is currently $(date) and I am $(whoami)"
- When double quotes are used, a single command-line argument is provided to echo, otherwise are are supplying multiple whitespace-separated arguments
- To suppress the execution of embedded commands, we can place text in single quotes (e.g.
echo 'It is currently $(date)'
, or we could also do backticks surrounding date (date
)
egrep
- It is often to search for the contents within text files, so Linux shell provides
egrep
utility
- Tool searches given files and outputs lines that match 1+ patterns
- Patterns provided as (extended) regular expressions - not the same as globbing patterns
- e.g.
egrep CS136 sample.txt
- run egrep
with regular expression CS136 as first argument and filename sample.txt as second argument
- could also write command as
ca sample.txt | egrep CS136
- egrep accepts text as input on standard input stream along with command line argument filenames
egrep
will independently try to match the regular expression on each line of the provided file/text
- Line - sequence of 1+ characters where last character in sequence is \n