GeneralThis forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.