CSE 391
Shell commands
Streams, Redirection
Slides created by Josh Ervin and Hunter Schafer. �Based off slides made by Marty Stepp, Jessica Miller, Ruth Anderson, Brett Wortzman, and Zorah Fung
AGENDA
Logistics
FILE EXAMINATION
Command | description |
cat | Print contents of a file |
less | Output file contents, one page |
more | Output file contents, one page |
head | Output number of lines of start of file |
tail | Output number of lines of end of ile |
wc | Count words, characters, lines in a file |
SEARCHING AND SORTING
Command | description |
grep | Search given file for pattern |
sort | Sort input or file, line based |
uniq | Strip duplicate adjacent lines |
find | Search filesystem |
cut | Remove section from each line of file |
You’re working on a project and have been leaving comments with the text “TODO” near all the things you still need to finish. Your project is stored in the project directory in your current directory.
Write a command to find and print all of the lines that have a TODO on them in the project folder.
Hint: Recall for HW1 we taught you how to use the “wildcard” to select all files as part of a path.
$ grep “TODO” project/*
JAVA AND THE COMMAND LINE
Command | description |
javac ClassName.java | Compile ClassName |
java ClassName | Run ClassName |
python, ruby, perl, gcc, go, etc | Run or compile other files in different languages! |
What is the command to compile all Java files in the current directory?
Hint: How can you use wildcards to find all Java files?
$ javac *.java
STANDARD STREAMS
STANDARD STREAMS
Process
stdin
stdout
stderr
A program such as ls, cd, or grep
These default to the console (terminal)
int | stream | Java |
0 | stdin | System.in |
1 | stdout | System.out |
2 | stderr | System.err |
STDIN VS PARAMETERS
OUTPUT REDIRECTION
command > filename
INPUT REDIRECTION
command < filename
Write a command to store all of the lines in fruits.txt that contain the letter a into a file called a.txt
$ grep “a” fruits.txt > a.txt
STDIN VS PARAMETERS: JAVA
// Read and print input from stdin
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
while (console.hasNext()) {
System.out.println(console.next());
}
}
// Read and print the parameters
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(args[i]);
}
}
STDERR REDIRECTION
command 2> filename
command 2>&1
command > filename 2>&1
PIPES
command1 | command2
command1 > filename
command2 < filename
rm filename
Suppose we have a file berries.txt where each line is the name of a different berry. Write a command that outputs how many berries have names that contain both the letter a and the letter e.
$ grep “a” berries.txt | grep “e” | wc -l
COMBINING COMMANDS
command1 ; command2
command1 && command2
LOGS
$ tail -f /cse/web/courses/logs/common_log | grep “391”