
264 Managing Files and Directories
I/O Redirection and Pipes
And don’t forget about using pipes and output redirection when you want to store and/or print
information to read at a later time.
You can, for example, use grep to search for particular contents of a file, then have those results
either saved as a file or sent to a printer.
To print the information about references to “coffee” in sneakers.txt, for example, we just type:
grep coffee sneakers.txt | lpr
This command behaves similar to the command ls -al /etc | more, which you may have used
in Chapter 16 on page 217 to list the contents of the /etc directory then send the results through the
more command for viewing on the screen.
Tip: Remember the distinction of using > and >>:using> will overwrite a file, while
>> appends the information to a file. Usually, unless you’re certain you want to, it’s
safer to use >>,becauseyouwon’t lose potentially valuable information (though you
may have to edit the file if you didn’t want to append information to it).
Wildcards and Regular Expressions
What if you forget the name of the file you’re looking for? You can’t say to your computer, “Find a
file called ’sneak’ or ’sneak-something’.”
Well, yes you can, in a way. Using wildcards or regular expressions, you can perform actions on a file
or files without knowing the complete filename. Just fill out what you know, then substitute the
remainder with a wildcard.
Tip: To read more about wildcards and regular expressions, take a look at the bash
man page (man bash). Remember that you can save the file to a text file by typing man
bash | col -b > bash.txt. Then, you can open and read the file with less or
pico (pico bash.txt). If you want to print the file, be prepared: It’squitelong.
We know the file’s called “sneak-something.txt”,sojusttype:
ls sneak*.txt
and there’s the name of the file:
Komentarze do niniejszej Instrukcji