LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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!

Notices


Reply
  Search this Thread
Old 05-29-2013, 09:11 AM   #1
Grtyop
Member
 
Registered: May 2013
Location: Over the Rainbow..
Distribution: LinuxMint , RedHat
Posts: 31

Rep: Reputation: Disabled
Why does cat > sample.txt create a new file ?


Code:
cat sample.txt
reads the contents of the file and outputs it to display.

I don't understand the process behind
Code:
cat > sample.txt
. It redirects the cat input (?) to the 'sample.txt' file ? What input is present ?

Thanks.
 
Old 05-29-2013, 09:14 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
see man cat. input is now the stdin and nothing is sent, so sample.txt will be written/created without any content.
see man bash about redirection (the meaning of > )
 
Old 05-29-2013, 09:28 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Actually, it doesn't matter which command precedes the > operator, the file to which the output is redirected will be created by the shell anyways before any command is run.
 
1 members found this post helpful.
Old 05-29-2013, 10:00 AM   #4
Grtyop
Member
 
Registered: May 2013
Location: Over the Rainbow..
Distribution: LinuxMint , RedHat
Posts: 31

Original Poster
Rep: Reputation: Disabled
Thanks pan64 and tobiSGD
 
Old 05-29-2013, 11:25 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.

Last edited by theNbomr; 05-29-2013 at 11:26 AM.
 
2 members found this post helpful.
Old 05-29-2013, 11:27 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Also, just in case not obvious from the above, should the file already exist, anything previously in the file will now be removed (for want of a better word).
 
Old 05-29-2013, 12:24 PM   #7
Grtyop
Member
 
Registered: May 2013
Location: Over the Rainbow..
Distribution: LinuxMint , RedHat
Posts: 31

Original Poster
Rep: Reputation: Disabled
@theNbomr: Great explanation, thanks =)
 
Old 05-30-2013, 05:41 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
Code:
> sample.txt cat
...is the same as...
Code:
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).

See here for a really good, detailed explanation:
redirections and file descriptors explained

Last edited by David the H.; 05-30-2013 at 05:43 PM.
 
1 members found this post helpful.
Old 05-31-2013, 12:52 PM   #9
rpeter
LQ Newbie
 
Registered: Jan 2008
Location: Hungary
Distribution: Slackware
Posts: 7

Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
See here for a really good, detailed explanation:
redirections and file descriptors explained
Thanks for the link. I was always interested in file descriptors (especially 3,4,5,...)

Last edited by rpeter; 05-31-2013 at 01:09 PM.
 
Old 05-31-2013, 01:40 PM   #10
vasthan
LQ Newbie
 
Registered: May 2011
Posts: 4
Blog Entries: 2

Rep: Reputation: 0
cat > sample.txt
it Function is open the file by name sample.txt and > it will allow to write lines in the file , The function simultaneous do 2 opertions opening and Writing in the file Sample.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to create txt file in driver code? ligang Linux - Embedded & Single-board computer 1 06-08-2010 04:11 PM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 07:38 AM
Create 1 csv file from multiple txt files richmur Programming 10 09-03-2008 02:28 PM
Simple question for i in `cat move.txt` Noto Programming 13 05-25-2008 07:32 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM


All times are GMT -5. The time now is 04:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration