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.
LVL 4
prashant_n_mhatreAsked:
Who is Participating?
 
griesshConnect With a Mentor Commented:
prashant_n_mhatre

Since you are using the 'rm/touch' already, how about creating a function:

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

You can put this function in your login script (I hope you can use functions ...) and then just call it with:

empty file

Better so?
======
Werner
0
 
griesshCommented:
COuld you please explain a bit more? You delete the content of a file with "rm <filename>"

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

rm <filename>
touch <filename>

(touch will create an empty file for you)
======
Werner
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
prashant_n_mhatreAuthor Commented:
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.
0
 
prashant_n_mhatreAuthor Commented:
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.
0
 
interiotCommented:
    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.
0
 
interiotCommented:
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.
0
 
chris_calabreseCommented:
This is a very simple thing to do:
  > filename
0
 
interiotCommented:
   echo > filename.txt

This preserves the file's permissions.
0
 
prashant_n_mhatreAuthor Commented:
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.
0
 
chris_calabreseCommented:
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.
0
 
prashant_n_mhatreAuthor Commented:
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...
0
 
interiotCommented:
'> filename' works only in sh/bash/ksh.

So...  for csh or tcsh, 'echo -n >! filename' might be preferable?
0
 
prashant_n_mhatreAuthor Commented:
I am using korn shell
0
 
griesshCommented:
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
0
 
chris_calabreseCommented:
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).
0
 
prashant_n_mhatreAuthor Commented:
Yeah...I agree with griessh. His function should work fine everywhere.
0
 
prashant_n_mhatreAuthor Commented:
chris_calabrese

where does ex come into picture in griessh's function?
0
 
chris_calabreseCommented:
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.
0
 
griesshCommented:
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
0
 
griesshCommented:
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
0
 
interiotCommented:
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.
0
 
chris_calabreseCommented:
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 ;-)
0
 
griesshCommented:
chris_calabrese
BTW: if you have "set -o noclobber" in your .profile, then "> filename" will fail!

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

From novice to tech pro — start learning today.