LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   delete file-content (https://www.linuxquestions.org/questions/programming-9/delete-file-content-546530/)

ovince 04-16-2007 01:36 PM

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

druuna 04-16-2007 02:02 PM

Hi,

From a terminal: > file

Hope this helps.

Zention 04-16-2007 02:51 PM

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.

druuna 04-17-2007 01:12 AM

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.

ghostdog74 04-17-2007 01:54 AM

if i understand you, you are refering to this?
Code:

# cat /dev/null > file #file is file to be emptied.

druuna 04-17-2007 02:45 AM

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.

omnio 04-17-2007 03:44 AM

I agree with druuna. For example, logrotate has the copytruncate option to prevent the move/deletion of open log files:

Quote:

copytruncate
Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever.


All times are GMT -5. The time now is 08:46 AM.