LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-27-2003, 03:51 AM   #1
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Rep: Reputation: 30
pipe output to append to a text file


I would like to run a command that appends it's output to an existing text file. If I try:

command > file.text

it will overwrite file.text - how do I get it to append this file rather than overwrite?

Dave
 
Old 08-27-2003, 03:55 AM   #2
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
>>

command >> file.txt

Cool
 
1 members found this post helpful.
Old 08-27-2003, 04:53 AM   #3
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
Thanks again...!

Dave
 
Old 08-27-2003, 05:03 AM   #4
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
You're welcome

Cool
 
Old 03-13-2016, 07:24 PM   #5
Lunar
Member
 
Registered: Feb 2005
Location: Texas, USA
Distribution: opensuse
Posts: 106
Blog Entries: 1

Rep: Reputation: 8
the command(s) I like this to work for, doesn't

Quote:
Originally Posted by MasterC View Post
>>

command >> file.txt
I wanted to pipe or redirect output of 'head' and 'tail' run on the /var/log/messages file, but >> overwrites the 'head' portion of the output file.

Code:
# head /var/log/messages > messages-01.txt
# tail /var/log/messages >> messages-01.txt
# cat messages-01.txt
file 'messages-01.txt contains Only the output from 'tail'.
output from 'head' is being overwritten. It is not being appended.

any ideas why?

alternatively, is there a number of lines for cat (or another display util) to output, like tail has?
Code:
 # tail -n 100 /var/log/messages > text.txt
so i could just use cat and say 'cat output first 5 lines >> cat output last 50 lines to file text.txt?

Thanks, Landis.
 
Old 03-22-2016, 07:44 PM   #6
Tim Abracadabra
Member
 
Registered: May 2014
Location: USA, Wherever I may Roam
Distribution: debian 9.8 w/GNOME and KDE dual boot w/Win 10.| debian 7.11 w/Xfce, LFS 7.9, + Multi-boot w/Windows7
Posts: 122

Rep: Reputation: Disabled
Quote:
Originally Posted by Lunar View Post
I wanted to pipe or redirect output of 'head' and 'tail' run on the /var/log/messages file, but >> overwrites the 'head' portion of the output file.

Code:
# head /var/log/messages > messages-01.txt
# tail /var/log/messages >> messages-01.txt
# cat messages-01.txt
file 'messages-01.txt contains Only the output from 'tail'.
output from 'head' is being overwritten. It is not being appended.

any ideas why?
Using your exact commands on Debian 7.9 using the bash shell works fine. The tail with the >> does not
overwrite the content added previously with the head > command.

Unsure why that would not work for you. What distro are you using and what shell?
Code:
echo $SHELL
That might give a clue that may click with someone

Maybe try switching to another shell will help but I can't say I've ever see >> not append.

Quote:
alternatively, is there a number of lines for cat (or another display util) to output, like tail has?
Code:
 # tail -n 100 /var/log/messages > text.txt
so i could just use cat and say 'cat output first 5 lines >> cat output last 50 lines to file text.txt?

Thanks, Landis.
cat does not have an option for outputting specific lines. See:
Code:
man cat
I know you can use sed for printing a range of line numbers but getting the last 50 lines is ...
quite ugly and I suspect less efficient than tail. In any case you would still be using >> to append, correct?

These commands meet your requirements when I tested them (Again using the bash shell)

Code:
head -n 5 /var/log/messages > text.txt
tail -n 50 /var/log/messages >> text.txt
Also, are you sure you didn't just use the up arrow to repeat the previous command, then edit it
to use tail instead of head and then maybe forget to add the second > character??

Easy to do

Hope that helps,
Tim
 
  


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
Possible to pipe output and error to a file while displaying them on monitor? Akhran Linux - Newbie 2 03-29-2006 05:04 AM
pipe xine output to file sohmc Linux - Software 0 01-01-2005 10:03 PM
how to enumerate first line of text file to standard output? zero79 Linux - General 1 07-07-2004 06:37 PM
Pipe telnet session output to text file joshlamerritt Linux - Software 3 02-10-2004 08:42 PM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:21 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