1 of 2

Name:

PID: (page 1)

Example directory structure, file contents in parentheses

some-files/

|- a.txt ("hello\n")

|- more-files/

|- b.txt ("hi\n")

|- c.java ("psvm\n")

|- even-more-files/

|- d.java ("junit\ntest")

|- a.txt ("nested file\n")

find «path»: Recursively traverse the given path and list all files in that directory and subdirectories

wc «file»: Print the number of lines, words, and characters in a file or files

grep «string» «files»: Search a file or files for the given string, print matching lines

«command» > «file» Save the output of the command in the given file. Overwrites the file!

* (asterisk, star) Used to create patterns, which expands to all matching paths.�Examples: lib/*.jar, *.txt

echo «arguments» Print the arguments to the terminal

Which command or commands (do you think) produces the output on the right, and why? Make a guess!

$ wc some-files/a.txt

$ wc some-files/even-more-files/a.txt

1 2 12 Joe hid a little bit of output here

$ grep "e" some-files/a.txt

$ grep "e" some-files/even-more-files/a.txt

hello

$ grep "e" */a.txt

$ grep "e" */*/a.txt */a.txt

$ grep "e" */*/a.txt

some-files/even-more-files/a.txt:nested

some-files/a.txt:hello

$ find some-files > files.txt

$ grep ".txt" some-files

$ find some-files > files.txt

$ grep ".txt" files.txt

some-files/even-more-files/a.txt

some-files/more-files/b.txt

some-files/a.txt

Which command or commands (do you think) produces the output on the right, and why? Make a guess!

2 of 2

Name:

PID: (page 2)

$ find some-files -name "*.txt"

$ find some-files -name *.txt

some-files/even-more-files/a.txt

some-files/more-files/b.txt

some-files/a.txt

Which command or commands (do you think) produces the output on the right, and why? Make a guess!

xargs «command» < «file»: perform «command» and add on all the contents of «file» as command-line arguments

Example:

1 2 12 some-files/even-more-files/a.txt

2 2 11 some-files/even-more-files/d.java

3 4 23 total

Write a command or commands that produces the output on the right. There's more than one way to do it!

$ find some-files -type f > files.txt

$ xargs wc < files.txt

11 some-files/even-more-files/d.java

12 some-files/even-more-files/a.txt

7 some-files/more-files/c.java

3 some-files/more-files/b.txt

6 some-files/a.txt

39 total

What's a command to count the lines/words/characters of all the .java files? Can you do it without listing the java files individually?

# in a file called all-java.sh

JAVAS=`find some-files -name "*.java"`

wc $JAVAS

$ bash all-java.sh

2 2 11 some-files/even-more-files/d.java

1 1 7 some-files/more-files/c.java

3 3 18 total

Another way using bash variables

What if we did not add -type f in the first command? How would the above output change?