What is Redirection and How Does it Work?
Another useful bash feature is its ability to redirect the input and output of Linux commands. You can save the results of a command in a file instead of displaying the results on the screen, or you can feed data from a file to a program instead of entering data from the keyboard. Let's look at redirection first. Imagine a fictitious command called nocats that prompts the user for a number and then waits for that many lines of text to be entered before processing them. (The program looks at each input line and pr ints only the ones that do not contain the word cat.)
You could feed the program by entering the data from the console (bold text is your typed input, normal text is console output):
$ nocats
3
Dogs are much better than those other household animals.
A cat would never beg for jerky
treats.
Dogs are pretty stupid, but at least they stick around.
Dogs are much better than those other household animals.
Dogs are pretty stupid, but at least they stick around.
Or using a text editor, you could put all the input data in a file called stuff and feed the nocats program like this:
% nocats < stuff
Dogs are much better than those other household animals.
Dogs are pretty stupid, but at least they stick around.
The less-than (<) symbol causes the program to get input from the stuff file instead of waiting for keyboard input. The greater-than (>) symbol, on the other hand, redirects output to a file instead of to the console. Thus, the co mmand
% nocats < stuff > bother
will cause the nocats program to read its input from one file (stuff) and write it to another (bother), without the keyboard or console entering the picture. Note that the nocats progra m doesn't know or care about all this redirection. It still thinks it is reading data from the keyboard and writing to the console--but the shell has temporarily reassigned the input and output to files instead of physical devices.
To append to an existing file instead of creating a new one, use two greater-than symbols (>>), as in this example:
zippity > somefile
doodah >> somefile
The zippity command runs first, and the output is placed in a new file called somefile. Then doodah runs, and its output is added (appended) to the somefile file.
Note: It's important to remember that piping with a single > symbol will wipe out existing data if the output file already exists.
Comments - most recent first
(Please feel free to answer questions posted by others!)
rpm -ivh *.rpm 2>&1 |tee output.txt
Works great.
Thanks, Dr. Bob!
I'm trying to redirect output from a multi-rpm install in order to grep for errors. However, the redirect never kicks in if there are failed dependencies.
As in: rpm -ivh *.rpm | tee output.txt
or rpm -ivh *.rpm > output.txt
Both leave me with a 0 byte output.txt when I immediately get this message on the screen:
error: Failed dependencies:
I'm trying to trap <all> output to look for the error message, but that output never hits the .txt file.
I'm sure this is a side effect from rpm install, and not redirect, but I'm not sure how to fix.
Any ideas?
Thanks.
Your explanation about Redirection is indeed helpful to me.
THIS SITE IS QUITE EDUCATIVE ON LINUX.
I think the % is just another prompt symbol. It doesn't do anything, but it just lets you know that you can start typing to bash. The reason you're getting the error is because Bob is using a fictional command. Nocats doesn't exist, but bash thinks it does and is searching your system for a command called Nocats and can't find it. I'm new to this myself but think that you would need to read the shell scripting section to create your own Nocats command.
you are very good teacher.....but my question is same whats % for ??
firstly,i would like to thank u for such a nice work..
secondly,i would like to know that how to make a program like in the above example nocats,doodah and zippity are..?
$ - prompt -user
# - prompt - root user
where did the % come from, what does it mean ... this is first mention of it and its not explained. Ive googled it and am half way through this site and havent come across it again.
Thanks if you can help.
And to Dr Bob tyvm for this site, you are truly a star :)
I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But
don't ask me to send what you already have. :-) NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Copyright © by -
All rights reserved - Redistribution is allowed only with permission.

