Bash Advanced Exercises#
1 Cleaning up#
As we have gone through this chapter, we have created several files that we won’t need again. We can clean them up with the following commands; briefly explain what each line does.
$ cd ~/zipf
$ for file in $(find . -name "*.bak")
> do
> rm $file
> done
$ rm bin/summarize_all_books.sh
$ rm -r results
2 Variables in shell scripts#
Imagine you have a shell script called script.sh
that contains:
head -n $2 $1
tail -n $3 $1
With this script in your data
directory, you type the following command:
$ bash script.sh '*.txt' 1 1
Which of the following outputs would you expect to see?
All of the lines between the first and the last lines of each file ending in
.txt
in thedata
directoryThe first and the last line of each file ending in
.txt
in thedata
directoryThe first and the last line of each file in the
data
directoryAn error because of the quotes around
*.txt
3 Find the longest file with a given extension#
Write a shell script called longest.sh
that takes the name of a directory and a filename extension as its arguments, and prints out the name of the file with the most lines in that directory with that extension. For example:
$ bash longest.sh data/ txt
would print the name of the .txt
file in data
that has the most lines.
4 Script reading comprehension#
For this question, consider your data
directory once again.
Explain what each of the following three scripts would do when run as bash script1.sh *.txt
, bash script2.sh *.txt
, and bash script3.sh *.txt
respectively.
# script1.sh
echo *.*
# script2.sh
for filename in $1 $2 $3
do
cat $filename
done
# script3.sh
echo $@.txt
(You may need to search online to find the meaning of $@
.)
5 Using grep
#
Assume the following text from The Adventures of Sherlock Holmes is contained in a file called excerpt.txt
:
To Sherlock Holmes she is always THE woman. I have seldom heard
him mention her under any other name. In his eyes she eclipses
and predominates the whole of her sex. It was not that he felt
any emotion akin to love for Irene Adler.
Which of the following commands would provide the following output:
and predominates the whole of her sex. It was not that he felt
grep "he" excerpt.txt
grep -E "he" excerpt.txt
grep -w "he" excerpt.txt
grep -i "he" excerpt.txt
6 Tracking publication years#
In Exercise bash - pipe commands you examined code that extracted the publication year from a list of book titles.
Write a shell script called year.sh
that takes any number of filenames as command-line arguments, and uses a variation of the code you used earlier to print a list of the unique publication years appearing in each of those files separately.
7 Counting names#
You and your friend have just finished reading Sense and Sensibility and are now having an argument.
Your friend thinks that the elder of the two Dashwood sisters, Elinor, was mentioned more frequently in the book, but you are certain it was the younger sister, Marianne.
Luckily, sense_and_sensibility.txt
contains the full text of the novel.
Using a for
loop, how would you tabulate the number of times each of the sisters is mentioned?
Hint: one solution might employ the commands grep
and wc
and a |
, while another might utilize grep
options.
There is often more than one way to solve a problem with the shell; people choose solutions based on readability, speed, and what commands they are most familiar with.
8 Matching and subtracting#
Assume you are in the root directory of the zipf
project.
Which of the following commands will find all files in data
whose names end in e.txt
, but do not contain the word machine
?
find data -name '*e.txt' | grep -v machine
find data -name *e.txt | grep -v machine
grep -v "machine" $(find data -name '*e.txt')
None of the above.
9 find
pipeline reading comprehension#
Write a short explanatory comment for the following shell script:
wc -l $(find . -name '*.dat') | sort -n
10 Finding files with different properties#
The find
command can be given criteria called “tests” to locate files with specific attributes, such as creation time, size, or ownership.
Use man find
to explore these, then write a single command using -type
, -mtime
, and -user
to find all files in or below your Desktop directory that are owned by you and were modified in the last 24 hours.
Explain why the value for -mtime
needs to be negative.