LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2007, 01:36 PM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
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
 
Old 04-16-2007, 02:02 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

From a terminal: > file

Hope this helps.
 
Old 04-16-2007, 02:51 PM   #3
Zention
Member
 
Registered: Mar 2007
Posts: 119

Rep: Reputation: 16
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.
 
Old 04-17-2007, 01:12 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 04-17-2007, 01:54 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if i understand you, you are refering to this?
Code:
# cat /dev/null > file #file is file to be emptied.
 
Old 04-17-2007, 02:45 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 04-17-2007, 03:44 AM   #7
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
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.

Last edited by omnio; 04-17-2007 at 03:46 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
delete file that match the content packets Programming 5 04-03-2007 02:47 PM
Delete text content syseeker Programming 5 02-07-2006 07:32 AM
delete content form a file bru Programming 1 04-30-2004 07:41 AM
How does one delete directories with content as the SU? Ferrell Ramey Linux - General 2 08-22-2002 07:16 PM
How to delete a dir and all it's content? ReSync Linux - Newbie 2 06-29-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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