1 of 2

Name:

PID: (page 1)

$ 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?

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!

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

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

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")

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 can refer to multiple files.�Examples: lib/*.jar, *.txt

echo «arguments» Print the arguments to the terminal

2 of 2

Name:

PID: (page 2)

import java.util.Scanner;

class Read {

public static void main(String[] args) {

System.out.print("What's your name? ");

Scanner in;

in = new Scanner(System.in);

String s = in.nextLine();

System.out.println("Hello " + s);

}

}

$ java Read

What's your name? JoeTyped

Hello JoeTyped

$ cat name.txt

JoeFromFile

$ java Read < name.txt

Read.java

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

xargs reads the command-line arguments from standard input!

Example:

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

$ xargs wc < files.txt

«command» < «file» Input redirection – use contents of «file» as the standard input to the command. That's what you would type!

What's a command (or commands) that would count the lines/words/characters of all the .java files?

«command» | «command» Pipe – Take the output of the first command and use it as the input to the second command.

$ find -type f some-files | xargs wc

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 would wc < files.txt do?

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

bash-3.2$ wc /usr/share/dict/words

235976 235976 2493885 /usr/share/dict/words

bash-3.2$ head -n 10 /usr/share/dict/words

A

a

aa

aal

aalii

aam

Aani

aardvark

aardwolf

Aaron

bash-3.2$ tail -n 10 /usr/share/dict/words

zymotoxic

zymurgy

Zyrenian

Zyrian

Zyryan

zythem

Zythia

zythum

Zyzomys

Zyzzogeton