LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 05-16-2005, 08:10 PM   #1
LinuxPadawan
Member
 
Registered: Mar 2005
Location: USA
Distribution: Fedora Core 3
Posts: 114

Rep: Reputation: 15
what is output redirection?


I am reading a UNIX book and in one of the chapters it mentioned using echo and output redirection, could you explain what output redirection is?

It was similar to this:
echo "sample text" >> file

This is probably very simple but I'm not familiar with it.

Last edited by LinuxPadawan; 05-16-2005 at 08:26 PM.
 
Old 05-16-2005, 08:25 PM   #2
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Rep: Reputation: 47
That could be, for instance, when you run, say the 'ls' command.
Say you have a directory with a lot of .conf files. You can run 'ls -l *.conf' to see them. Then, if you wanted to, you could redirect the output of 'ls' to another text file, like 'ls -l *.conf > conf_list.txt', such that the conf_list.txt file is created from the output of ls.

Example:
Code:
bs@bsubuntu:/etc$ ls -l *.conf
-rw-r--r--  1 root root  2188 2005-03-31 07:13 adduser.conf
-rw-r--r--  1 root root  2673 2005-03-01 16:08 debconf.conf
-rw-r--r--  1 root root   392 2004-10-26 09:52 deluser.conf
-rw-r--r--  1 root root   689 2005-02-28 08:19 discover.conf
-rw-r--r--  1 root root  6414 2005-03-19 00:11 evms.conf
-rw-r--r--  1 root root   354 2004-08-12 21:23 fdmount.conf
-rw-r--r--  1 root root  1100 2005-03-29 08:38 gksu.conf
-rw-r--r--  1 root root  3688 2005-04-02 13:33 hdparm.conf
-rw-r--r--  1 root root    26 1995-09-25 23:20 host.conf
-rw-r--r--  1 root root  1270 2005-03-31 07:13 inetd.conf
-rw-r--r--  1 root root  6947 2005-03-25 17:37 irssi.conf
-rw-r--r--  1 root root   179 2005-03-31 07:18 kernel-img.conf
-rw-r--r--  1 root root    15 2005-03-31 13:23 ld.so.conf
-rw-r--r--  1 root root  3243 2004-12-21 21:41 lftp.conf
-rw-r--r--  1 root root    19 2005-02-23 06:39 libao.conf
-rw-r--r--  1 root root   599 2004-08-12 19:14 logrotate.conf
-rw-r--r--  1 root root 10815 2004-11-09 06:25 ltrace.conf
-rw-r--r--  1 root root   268 2005-04-04 09:42 mplayerplug-in.conf
-rw-r--r--  1 root root   465 1999-03-11 10:48 nsswitch.conf
-rw-r--r--  1 root root  1741 2005-04-18 14:07 ntp.conf
-rw-r--r--  1 root root   534 2004-10-26 09:39 pam.conf
-rw-r--r--  1 root root  7649 2005-03-31 13:26 pnm2ppa.conf
-rw-r--r--  1 root root   328 2005-03-31 07:13 popularity-contest.conf
-rw-r--r--  1 root root  2554 2005-01-25 15:03 reportbug.conf
-rw-r--r--  1 root root    70 2005-05-16 17:10 resolv.conf
-rw-r--r--  1 root root    23 2004-10-26 11:36 scrollkeeper.conf
-rw-r--r--  1 root root 65682 2005-04-06 17:02 sensors.conf
-rw-r--r--  1 root root   424 2004-12-08 04:36 sysctl.conf
-rw-r--r--  1 root root  1664 2005-03-21 05:06 syslog.conf
-rw-r--r--  1 root root  1260 2004-10-28 13:50 ucf.conf
-rw-r--r--  1 root root   645 2005-02-01 11:10 updatedb.conf
-rw-r--r--  1 root root  4622 2004-10-28 18:57 vnc.conf
-rw-r--r--  1 root root  2690 2005-04-04 18:33 xcdroast.conf
bs@bsubuntu:/etc$ ls -l *.conf > /home/bs/conf_list.txt
bs@bsubuntu:/etc$ cat /home/bs/conf_list.txt
-rw-r--r--  1 root root  2188 2005-03-31 07:13 adduser.conf
-rw-r--r--  1 root root  2673 2005-03-01 16:08 debconf.conf
-rw-r--r--  1 root root   392 2004-10-26 09:52 deluser.conf
-rw-r--r--  1 root root   689 2005-02-28 08:19 discover.conf
-rw-r--r--  1 root root  6414 2005-03-19 00:11 evms.conf
-rw-r--r--  1 root root   354 2004-08-12 21:23 fdmount.conf
-rw-r--r--  1 root root  1100 2005-03-29 08:38 gksu.conf
-rw-r--r--  1 root root  3688 2005-04-02 13:33 hdparm.conf
-rw-r--r--  1 root root    26 1995-09-25 23:20 host.conf
-rw-r--r--  1 root root  1270 2005-03-31 07:13 inetd.conf
-rw-r--r--  1 root root  6947 2005-03-25 17:37 irssi.conf
-rw-r--r--  1 root root   179 2005-03-31 07:18 kernel-img.conf
-rw-r--r--  1 root root    15 2005-03-31 13:23 ld.so.conf
-rw-r--r--  1 root root  3243 2004-12-21 21:41 lftp.conf
-rw-r--r--  1 root root    19 2005-02-23 06:39 libao.conf
-rw-r--r--  1 root root   599 2004-08-12 19:14 logrotate.conf
-rw-r--r--  1 root root 10815 2004-11-09 06:25 ltrace.conf
-rw-r--r--  1 root root   268 2005-04-04 09:42 mplayerplug-in.conf
-rw-r--r--  1 root root   465 1999-03-11 10:48 nsswitch.conf
-rw-r--r--  1 root root  1741 2005-04-18 14:07 ntp.conf
-rw-r--r--  1 root root   534 2004-10-26 09:39 pam.conf
-rw-r--r--  1 root root  7649 2005-03-31 13:26 pnm2ppa.conf
-rw-r--r--  1 root root   328 2005-03-31 07:13 popularity-contest.conf
-rw-r--r--  1 root root  2554 2005-01-25 15:03 reportbug.conf
-rw-r--r--  1 root root    70 2005-05-16 17:10 resolv.conf
-rw-r--r--  1 root root    23 2004-10-26 11:36 scrollkeeper.conf
-rw-r--r--  1 root root 65682 2005-04-06 17:02 sensors.conf
-rw-r--r--  1 root root   424 2004-12-08 04:36 sysctl.conf
-rw-r--r--  1 root root  1664 2005-03-21 05:06 syslog.conf
-rw-r--r--  1 root root  1260 2004-10-28 13:50 ucf.conf
-rw-r--r--  1 root root   645 2005-02-01 11:10 updatedb.conf
-rw-r--r--  1 root root  4622 2004-10-28 18:57 vnc.conf
-rw-r--r--  1 root root  2690 2005-04-04 18:33 xcdroast.conf
bs@bsubuntu:/etc$

Last edited by itsjustme; 05-16-2005 at 08:34 PM.
 
Old 05-16-2005, 08:32 PM   #3
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Rep: Reputation: 47
Or it could be like this:

You can run ps -A and get output on the screen. Or you could run ps -A and redirect the ouput to grep:

Code:
bs@bsubuntu:/etc$ ps -A
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    2 ?        00:00:00 ksoftirqd/0
    3 ?        00:00:00 events/0
    4 ?        00:00:00 khelper
   22 ?        00:00:00 kacpid
   94 ?        00:00:00 kblockd/0
  132 ?        00:00:00 pdflush
  133 ?        00:00:00 pdflush
  135 ?        00:00:00 aio/0
  134 ?        00:00:00 kswapd0
  722 ?        00:00:00 kseriod
 1096 ?        00:00:00 kjournald
 1124 ?        00:00:00 udevd
 2249 ?        00:00:00 khpsbpkt
 4650 ?        00:00:00 knodemgrd_0
 4783 ?        00:00:00 khubd
 6030 ?        00:00:00 dhclient3
 6386 ?        00:00:00 syslogd
 6401 ?        00:00:00 dd
 6403 ?        00:00:00 klogd
 6436 ?        00:00:00 gdm
 6447 ?        00:00:00 gdm
 6484 ?        00:02:34 Xorg
 6487 ?        00:00:02 cupsd
 7005 ?        00:00:00 acpid
 7071 ?        00:00:00 dbus-daemon-1
 7086 ?        00:00:07 hald
 7099 ?        00:00:00 inetd
 7237 ?        00:00:00 master
 7266 ?        00:00:00 qmgr
 7431 ?        00:00:00 sshd
 7448 ?        00:00:00 ntpd
 7475 ?        00:00:00 atd
 7486 ?        00:00:00 cron
 7508 tty1     00:00:00 getty
 7509 tty2     00:00:00 getty
 7510 tty3     00:00:00 getty
 7511 tty4     00:00:00 getty
 7512 tty5     00:00:00 getty
 7513 tty6     00:00:00 getty
 7610 ?        00:00:00 x-session-manag
 7655 ?        00:00:00 ssh-agent
 7658 ?        00:00:00 dbus-launch
 7659 ?        00:00:00 dbus-daemon-1
 7661 ?        00:00:01 gconfd-2
 7664 ?        00:00:00 gnome-keyring-d
 7666 ?        00:00:00 esd
 7668 ?        00:00:00 bonobo-activati
 7670 ?        00:00:01 gnome-settings-
 7673 ?        00:00:00 gam_server
 7681 ?        00:00:00 xscreensaver
 7705 ?        00:00:00 gnome-smproxy
 7707 ?        00:00:06 metacity
 7709 ?        00:00:00 gnome-volume-ma
 7711 ?        00:00:04 nautilus
 7713 ?        00:00:02 gnome-panel
 7715 ?        00:00:02 gnome-cups-icon
 7717 ?        00:00:03 gnome-terminal
 7719 ?        00:01:04 gkrellm
 7721 ?        00:00:02 gaim
 7723 ?        00:00:00 update-notifier
 7727 ?        00:00:00 evolution-alarm
 7733 ?        00:00:00 gnome-pty-helpe
 7734 pts/0    00:00:00 bash
 7746 ?        00:00:03 wnck-applet
 7752 ?        00:00:00 evolution-excha
 7756 ?        00:00:00 gnome-vfs-daemo
 7763 ?        00:00:00 evolution-data-
 7765 ?        00:00:00 trashapplet
 7775 ?        00:00:00 mapping-daemon
 7798 ?        00:00:00 notification-ar
 7800 ?        00:00:00 mixer_applet2
 9044 ?        00:00:02 xchat
12261 ?        00:01:34 firefox-bin
12389 ?        00:00:01 java_vm
13088 ?        00:00:00 pickup
13108 pts/0    00:00:00 ps
bs@bsubuntu:/etc$
bs@bsubuntu:/etc$ ps -A | grep gnome
 7664 ?        00:00:00 gnome-keyring-d
 7670 ?        00:00:01 gnome-settings-
 7705 ?        00:00:00 gnome-smproxy
 7709 ?        00:00:00 gnome-volume-ma
 7713 ?        00:00:02 gnome-panel
 7715 ?        00:00:02 gnome-cups-icon
 7717 ?        00:00:04 gnome-terminal
 7733 ?        00:00:00 gnome-pty-helpe
 7756 ?        00:00:00 gnome-vfs-daemo
bs@bsubuntu:/etc$
 
Old 05-16-2005, 10:10 PM   #4
JunctaJuvant
Member
 
Registered: May 2003
Location: Wageningen, the Netherlands
Distribution: OS X & Linux Mint
Posts: 488

Rep: Reputation: 31
Be carefull with redirecting output to printers though. A fellow student of mine mindlessly executed something like "cat book.pdf > printer".
By the time he'd realized his stupid mistake and stopped the printer, he'd allready wasted a great deal of his printing quota for that month.
 
Old 05-17-2005, 05:20 PM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
I am taking your question to literally mean that you are asking for an understanding about what redirection is and does. If I am incorrect and you already know this information, then please accept my apology.

In UNIX, Linux, etc the shell (does not matter which one) automatically opens three data streams for you. The first stream, stdin (standard in) accepts data from the keyboard and passes it to a program. The second stream stdout (standard out) takes text data from a program and displays it in a screen window. The third stream stderr (standard error) also takes text data from a program and also displays it in a screen window. These are the default behaviors for any program running from a shell.

The operating system allows you to "redirect" any one, or all, of the data streams to either obtain data from something other than the keyboard, or to output text to something other than the screen window. This is accomplished by giving the shell special instructions when you execute a command. "<" causes the program's stdin to be redirected to the new source instead of the keyboard. ">" causes the program's stdout to be redirected to the new target instead of the screen window. "2>" causes the program's stderr to be redirected like the stdout redirection. ">>" causes the program's stdout to also be redirected to a new target, but causes the new information to be a continuation of whatever was originally in the target output.

Your example "echo "sample text" >> file" simply means using the program "echo" (a utility to simply echo the contents of the first argument passed to it out to stdout) the text "sample text" is sent to a file called "file", and the output text is concatenated onto the end of the file if it preexisted. If it did not preexist, then a new file called "file" will be created with the contents of a single text line of "sample text".
 
Old 05-18-2005, 12:53 PM   #6
mohit dhawan
Member
 
Registered: Mar 2005
Posts: 92

Rep: Reputation: 15
note the difference btw
>
and
>>
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Output redirection rmartine Linux - Newbie 2 01-25-2005 06:31 PM
Output & input redirection serotonincy Programming 3 04-12-2004 08:28 AM
Minor output redirection problem.. Tyir Linux - General 0 01-27-2004 09:13 PM
output redirection to another directory spyghost Linux - Software 3 09-21-2003 09:28 AM
Output redirection legtester Linux - General 4 07-07-2003 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 01:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration