[SOLVED] Why does cat > sample.txt create a new file ?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
The shell interprets the '>' character as a specifier for redirection of standard output. In the example cited, it refers to the standard output of 'cat'. In the example cited, cat is simply passing it's own standard input to it's standard output. The standard input of cat in the cited example is the console, so it will pass all of it's keyboard input to it's standard output. This is common idiom for creating a text file by typing at the commandline.
In other news, redirections are parsed and set up before anything else in the line, so the file is created/cleared well before the command executes.
It also means that the position of the redirection on the line doesn't matter.
> sample.txt cat
...is the same as...
cat > sample.txt
In either case, since cat has been given no input, it will just sit there listening to stdin for something to work on. If you start typing something into the terminal, everything you write will end up in the file. (You can use ctrl+D to send an end-of-file signal to tell cat you're finished, BTW).