File contents not deleted in linux centos - bash

I have the program running using this command
command 2> sample.txt
now that file is growing continuously and command will exit in 5-6 days and i beleive that file size won't go in GB
I tried this
echo "" > sample.txt but thats not making any differnce to it and filesize is growing.
i was thinking of setting up cron job after 1 hour to empty its contents
How can i empty the contents of file

Try the following command, this will write the console output to a file. (Your console will also get the messages printed).
command | tee -a file.log
and you can empty the contents by
> file.log

Related

How to write to file that support tail -f

Is it possible to write to file in one bash process and read it with tail in another (same way you can read system generated logs with tail -f.
I would like to open and continuously write something to file
vi /tmp/myfile
And in other terminal prints what was written to that file
tail -f /tmp/myfile
I've tried this, but tail doesn't print anything after I save changes in vi (only initial lines, before save).
Motivation:
In my toy project. I would like to build shared clipboard using pipeto.me service. Where I would write to my file continuously and all changes captured by tail would be piped to curl. Something like watch log example from pipeto.me
tail -f logfile | curl -T- -s https://pipeto.me/2xrGcZtQ.
But instead of logfile it will watch my file, where I would write in vi
But apart from solving my problem, I'm looking for general answer if something like this is possible with vi and tail.
You can use cat command, by changing its output stream as /tmp/file that is whatever you type will be added to myfile,
cat > /tmp/myfile;
#input-> add text(standard input by default is set as keyboard)
#typing...
And to print the file with tail command with -F as argument,
tail -F /tmp/file; #-F -> output appended data as the file grows and with retry
#output-> input given to file
#typing....
Writing text to file with vim,
vi /tmp/file;
#typing...
#:w -> write text to file
tail -F /tmp/file;
#
#typing...
When you write to your file using vim, it doesn't write(save) it instantly as you type, instead when you exit the insert mode and save the file explicitly(:w), it is then the output of tail will be updated.
Hence you can use a plugin like Autosaveplugin which could help to save automatically, to display logs synchronously.

How to view live overwritten file in terminal?

I have an output file which is constantly overwritten. There is a command in terminal tail -f filename which is helpful when the results are appended in the output file. However, I want to observe the first 10 or 20 lines of the constantly overwritten output file. Is there any such command?
It refreshes every 1 second and prints first 100 lines of filename
watch -n 1 head -100 filename
If using FreeBSD you would use wait_on, for example:
#!/bin/sh
while :; do
tail file
wait_on -w file
done
You may need to install it by using pkg install wait_on
In Linux you could use inotifywait something like:
#!/bin/sh
inotifywait --quiet --monitor --event modify file | while read; do
tail file;
done
Based on your distro you may need to install the inotify-tools package, for example in CentOS: yum install inotify-tools
For more options check the answer to this quesion: https://superuser.com/q/181517/284722
Suggestion using php.
In the command line. This won't load the whole file in memory.
Output first 8192bytes of seed.txt.
php -r "echo fread(fopen('seed.txt','r'), 8192);"
fread() reads up to length bytes from the file pointer referenced by
handle. Reading stops as soon as one of the following conditions is
met http://php.net/manual/en/function.fread.php
See https://stackoverflow.com/a/15025877/2494754

How to save terminal output automatically in shell script

I was wondering if there's any possible way to save the output of a shell script while to a file while it's running in terminal. for example, say I'm compiling a java program with the command javac foo.java. How would I save all the output of that particular command (errors, etc.) to a file for future reference without having to hit command- s and select save and replace after every time I run the command?
Use javac foo.java > output.txt to capture the output of your command to the file output.txt.
This will however hide all output from you while it is compiling your module.
If you would like to see the output of your build in the terminal and at the same time capture the output into file output.txt you can use tee:
javac foo.java | tee output.txt
The tee program reads from stdin and writes everything into the specified file and also to stdout again.
Is that what you want ?
javac foo.java > output.txt
All error will go in the output.txt and nothing will print on the shell.
I use tee
javac foo.java | tee output.log
similar to this thread.
Dump terminal session to file?

How to print the latest appended content of a text file?

My program takes a text file instead of stdout as its output. It's constantly appending new lines to the file. I can tail the file every time I want to get the latest lines of content appended. But now I want the appended content to show on my terminal simultaneously, as if my program was taking stdout as its output.
I've found out an ugly solution: print the new appended content every five seconds by backing up the content of the text file five seconds earlier and diff the current content with it, like bellow:
#!/bin/sh
# show the appended text of a file every 5 seconds
echo `pwd`;
while true
do
cp $1 $1.earlier;
sleep 5;
echo `date`;
diff $1 $1.earlier;
done
I think what you want is:
$ tail -f file
From man tail:
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor
are equivalent
If you want more than just the last few lines of output, as well as following, you can invoke:
less +F $file
(or press Shift-F while viewing the file in less).
While following the file in less, press Ctrl-C to stop following but keep the file open, then Shift-F to follow again.

Bash: How to redirect the output of set of commands piped together to a file?

perf record | perf inject -b | perf report > tempfile 2>&1
I am running the above set of commands and trying to capture the ouput to temfile, but sometimes the outputs doesn't get fully appended in the tempfile (output of each command). To be more precise I am running this command from a script and I tried putting them in small brackets like
(perf record | perf inject -b | perf report) > tempfile 2>&1
but this also didn't work.
Pipe redirects output of one program to another. To log the output to a file and redirect to another program use tee command:
http://en.wikipedia.org/wiki/Tee_(command)

Resources