Originally Posted by Lunar
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.
# 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?
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.
alternatively, is there a number of lines for cat (or another display util) to output, like tail has?
so i could just use cat and say 'cat output first 5 lines >> cat output last 50 lines to file text.txt?
# tail -n 100 /var/log/messages > text.txt
cat does not have an option for outputting specific lines. See:
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)
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,