LINUX CLASSES - LINUX BASICS
What is Redirection and How Does it Work?
Redirecting the Input or Output of Linux Commands
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.
Previous Lesson: Aliases
Next Lesson: Pipelines
[ RETURN TO INDEX ]
Comments - most recent first (Please feel free to answer questions posted by others!)
John ( 21 Feb 2012, 18:26)
Never mind. Answered my own question. The solution is to simply redirect
err to stdout. I thought I had already tried this, but must have missed
it:
rpm -ivh *.rpm 2>&1 |tee output.txt
Works great.
Thanks, Dr. Bob!
John ( 21 Feb 2012, 17:45)
Hey, I've got one for you.
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.
Bhatt Bipin ( 13 Jan 2012, 04:03)
Your way of Teaching is really interesting.
Your explanation about Redirection is indeed helpful to me.
anekwe lawrence ( 19 Oct 2011, 10:29)
HI,
THIS SITE IS QUITE EDUCATIVE ON LINUX.
Musa Ali ( 14 Oct 2011, 13:08)
u need to learn c OR C++ language to make programs like nocats
Luke ( 28 Sep 2011, 20:13)
What about the << command, I see this used in shell scripts to input
multiple lines to a command but can not find any information on it because
google ignores the "<" symbol
Jack ( 14 Jul 2011, 00:47)
Hassan,
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.
Hassan ( 08 Mar 2011, 13:18)
Also it is giving error command not found.....any idea
Hassan ( 08 Mar 2011, 13:12)
Hi
you are very good teacher.....but my question is same whats % for ??
Mukund ( 30 Jan 2011, 14:07)
Thanks for enhancing my knowledge
akash ( 22 Dec 2010, 14:32)
hello sir,
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..?
sachin bhandari ( 21 Oct 2010, 03:21)
u r a nice teacher
Alok Ranjan ( 31 Aug 2010, 08:54)
% - prompt - user
$ - prompt -user
# - prompt - root user
Simon ( 19 Jun 2010, 19:01)
Hi..
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 Bob Rankin
- Privacy Policy
All rights reserved - Redistribution is allowed only with permission.