Link to home
Start Free TrialLog in
Avatar of prashant_n_mhatre
prashant_n_mhatre

asked on

unix command to delete the content of the file

Is there any command/switch to do this?
I don't want a script with lot of options.

It should keep the file deleting the content.
Avatar of griessh
griessh
Flag of United States of America image

COuld you please explain a bit more? You delete the content of a file with "rm <filename>"

======
Werner
Sorry, now I understand ... Can't you just recreate the file?

rm <filename>
touch <filename>

(touch will create an empty file for you)
======
Werner
Avatar of prashant_n_mhatre
prashant_n_mhatre

ASKER

let us say I have a file log.txt containing some text. I would like to remove all its content from command line.

Empty file log.txt will still exist.
Thanks griessh but
The way you suggested..I already know that and using the same.

I'm sure there is some command with a switch I used long back to do the same job.
    ex -s -c '0,$d' -c 'x' filename

-s tells it to run in "silent" batch mode.

'0,$d' tells it to delete lines 0 to EOF.

'x' tells it to write the file and exit.


I don't know if the -b (binary mode) flag is necessary, it seemed to work without it for me.
I looked through the files in /bin/ and /usr/bin/ for programs that call truncate(2) or ftruncate(2), and got the following list:

ci crontab ctags elm emacs emacs-20.7 emacs-nox ex hoststat lha Mail mailq make_printerdef make_smbcodepage make_unicodemap mc mcedit mutt newaliases nmblookup perl perl5.00503 pico pilot pine pl2pm
procmail purgestat python python1.5 rcp rpcclient screen smbclient smbmnt smbmount smbspool smbumount strace testparm testprns vi vim xchat

Of course, there are other ways to make a file's size 0, but you'd think this would be the best way to do it.
ASKER CERTIFIED SOLUTION
Avatar of griessh
griessh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is a very simple thing to do:
  > filename
   echo > filename.txt

This preserves the file's permissions.
Thank you all for your suggestions.

echo > filename

is the simplesh way but it still doesn't empty the file.
On my system (DYNIX/PTX) it shows the size of file = 1.

griessh's script works fine and the file size is also 0 as I expected.

Thanks again.
Actually griessh's script does not work fine because it assumes the file contains only ascii text, has relatively short lines, and is a file small enough to fit in /tmp.  It will barf otherwise.  Not to mention that it will be dog slow on non-trivially sized files.

Similarly 'echo > filename' fails because it places a newline character into the file, and it is therefore not empty.

'> filename' is the proper way to to d this.  It's always fast.  It always works.  The file is always empty afterwards.
Sorry chris_calabrese:

Your way works fine and in fact that is what I was looking for. Since I have already accepted the answer..I have posted your points.

Thanks...
'> filename' works only in sh/bash/ksh.

So...  for csh or tcsh, 'echo -n >! filename' might be preferable?
I am using korn shell
chris_calabrese

What are you telling about my function?

> Actually griessh's script does not work fine because it assumes the file contains only

I think you are talking about somebody else. The one problem my function has is that is doesn't check if the file is really there. It doesn't use /tmp and doesn't care about types ...

=====
Werner
Your solution calls ex.  Ex assumes that all files contain only ASCII characters, have lines under 4k, and are small enough to fit in /tmp (it uses /tmp for 'undo' buffering).
Yeah...I agree with griessh. His function should work fine everywhere.
chris_calabrese

where does ex come into picture in griessh's function?
Oops, never mind, I'm looking at interiot's original suggestion.

Yes, the function you've written will empty the file but...

1.  There's a race condition between deleting the file and recreating it (i.e., some other process may try to access the file when it' doesn't exist).
2.  This solution does not preserve file permissions.
3.  It's possible that you have permission to write/truncate the file, but not to delete/recreate it.
chris_calabrese

Could you please explain to a stupid AIX person where 'ex' and ASCII only comes into the game with this function?

function empty
{
  rm -f $1
  touch $1
}

======
Werner
OK, that sounds better now ...

Since prashant_n_mhatre  seems to be doing it manually until now anyway, I didn't see any harm ...

======
Werner
The 'ex' solution works on binary files, or at least in the environment that I'd tested it on.

The 'echo' solution also works, but only for csh and tcsh (similar to how your solution works only in sh, bash, and ksh).  tcsh's *builtin* echo doesn't print a "\n" when the output is going to a pipe or a file.
The '> filename' should work on *csh too.  But if you want a truely sh agnostic solution, how about

  dd if=/dev/null of=filename


I love a good flame war ;-)
chris_calabrese
BTW: if you have "set -o noclobber" in your .profile, then "> filename" will fail!

======
Werner
OK, I am giving up in this discussion ... the whole thing it's getting to perfect now :-)