delete file-content
hi
is there an easy way to delete the content of the file but leaving an empty files in the directory? oliver |
Hi,
From a terminal: > file Hope this helps. |
Noclobber might be set by the shell though.
So perhaps: rm file && touch file you could make a little script: #!/bin/bash rm $1 && touch $1 call it rmkeep and change PATH or stick it in an executable directory. |
Hi,
Although Zention's solution is not wrong it could have a unwanted side effect. If the file you want to empty is a live file (/var/log/messages to name just one) and Zention's way of emptying is used, nothing will be written in the new file afterwards, only way to solve that is to restart the program that writes to that file (syslogd in the messages example). The > file solution does not remove the file (no need to re-create it), it just empties the file. Hope this clears things up. |
if i understand you, you are refering to this?
Code:
# cat /dev/null > file #file is file to be emptied. |
Hi,
Nope (probably). Your example does empty the file, but the 'cat /dev/null' part isn't needed/wanted. If you like to have something before the > to make it more readable, use echo "" echo is a bash internal and faster then cat /dev/null BTW: This is the order that bash uses to look for 'commands': - keyword - alias - built-in - function - PATH I gave a heads-up for the following situation (below is a bit more detailed then the previous post): syslogd has several open files it writes to, /var/log/messages being one of those. If you need to empty /var/log/massage you should do it using: > /var/log/messages. This leaves the pipe that syslogd has to /var/log/messages intact and syslogd can still write to this file. If you do this: rm /var/log/message ; touch /var/log/messages you create a completely new file. The pipe syslogd had to the original file is broken and messages cannot be written to the new file. The only way to set up the pipe again is to restart syslogd. Syslogd is not the only program that has open files, so be careful when you empty a file. If you do it the way I posted before, you have nothing to worry about. Hope this clears things up. |
I agree with druuna. For example, logrotate has the copytruncate option to prevent the move/deletion of open log files:
Quote:
|
All times are GMT -5. The time now is 08:46 AM. |