· To find files that have not been accessed for over 30 days and print the first five lines of each:
find . -atime +30 -exec head -5 {} \;
· To find out if a process named netscape is running:
ps | grep netscape
· To print only the second and third lines of a file:
head -3 some.file | tail -2
Note that the usage changes slightly when a command is in the second or subsequent stages of a pipeline. No input file is specified, because the previous stage feeds the command.
At the beginning of this section, I said that it would be no problem to search within a bunch of files, pull out all lines that contain a certain keyword, sort those lines, eliminate duplicates, and then print just the third column of each line. Here's proof that you can do it all on one line:
grep 'stuff' *.data | sort +1 -2 | uniq | cut -f3
Seems almost too easy, doesn't it? Beats the heck out of writing a program several hundred lines long if you want to run it only once! Now let's use the rest of the commands from this section in another pipeline. Start by creating the file odds.ends containing the lines shown here:
Ford Cat 47
IBM Lion 152
Xerox Slug 31
Zenith Bear 26
Intel Cat 133
Hershey Lynx 28
Apple Panda 74
Then execute the following command. (The backslash at the end of a line tells the shell that you are continuing a command on the next line.) Can you figure out what the output will be?
head -5 odds.ends | sed s/Cat/Tigger/g | \
awk /Tigger/'{print "Buy",$1,"from",$2,"at",$3}' | \
tail -1
The correct answer is "Buy Intel from Tigger at 133"--can you prove it?
Previous Lesson: Finding Files
Next Lesson: Linux Shell Scripts
Comments - most recent first
|
|
||