LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2011, 03:46 PM   #1
takayama
Member
 
Registered: Sep 2009
Posts: 97

Rep: Reputation: 0
pipe output of ls to file


Hello
I want to pipe the output of ls in a folder to a file (lets call it test.txt) but when i do so, but when i do ls > test.txt in test.txt there is also test.txt (logical, sense it there, is there anyway to avoid this)
 
Old 05-03-2011, 03:50 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This is the normal behavior: when you redirect the output of a command to a file, first the file (empty) is created then the command is executed and the file populated with its output. To avoid that, just create the file in another location, e.g. in the /tmp directory:
Code:
ls > /tmp/test.txt && mv /tmp/test.txt .
 
Old 05-03-2011, 03:55 PM   #3
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Well it is working as per design and it is not showing you the false information as when you do ls -l > test.txt the file do exist there.

If you want to avoid that you can do other way round. Support you want to do ls -l /root and save the output into a file then do the following:

ls -l /root > /test.txt

This will put the output of ls -l ran on /root to test.txt file located under / provided test.txt file exist there on / else you have to use double redirection to create it

ls -l /root >> /test.txt
 
Old 05-03-2011, 04:08 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by T3RM1NVT0R View Post
This will put the output of ls -l ran on /root to test.txt file located under / provided test.txt file exist there on / else you have to use double redirection to create it

ls -l /root >> /test.txt
Wrong. Both redirectors will create the file if it doesn't exist. The difference is that the >-redirector will delete the contents of the file when it already exists, while the >>-redirector will append the new text to the existing file without deleting previous content.
 
1 members found this post helpful.
Old 05-03-2011, 05:13 PM   #5
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Yup thats true. I dont know why I said that even after knowing that it apends after the last line but still it was wrong. You always correct me... :-) Thank you

Last edited by T3RM1NVT0R; 05-03-2011 at 05:16 PM.
 
  


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
pipe output to append to a text file davee Linux - Newbie 5 03-22-2016 07:44 PM
[SOLVED] perl script output cannot pipe to file hattori.hanzo Programming 3 12-15-2010 02:26 AM
how to pass pipe output to file command nickleus Linux - General 6 06-19-2008 06:18 AM
pipe xine output to file sohmc Linux - Software 0 01-01-2005 10:03 PM
Pipe telnet session output to text file joshlamerritt Linux - Software 3 02-10-2004 08:42 PM

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

All times are GMT -5. The time now is 01:16 AM.

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