Solved

unix command to delete the content of the file

Posted on 2001-07-02
25
222 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:prashant_n_mhatre
  • 8
  • 7
  • 5
  • +1
25 Comments
 
LVL 11

Expert Comment

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

======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 6245753
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6245755
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6245765
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
 
LVL 3

Expert Comment

by:interiot
ID: 6245825
    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
 
LVL 3

Expert Comment

by:interiot
ID: 6245840
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
 
LVL 11

Accepted Solution

by:
griessh earned 25 total points
ID: 6245842
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6245851
This is a very simple thing to do:
  > filename
0
 
LVL 3

Expert Comment

by:interiot
ID: 6245854
   echo > filename.txt

This preserves the file's permissions.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6245877
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6245890
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6245895
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:interiot
ID: 6245929
'> filename' works only in sh/bash/ksh.

So...  for csh or tcsh, 'echo -n >! filename' might be preferable?
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6245934
I am using korn shell
0
 
LVL 11

Expert Comment

by:griessh
ID: 6245976
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6245996
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
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6245997
Yeah...I agree with griessh. His function should work fine everywhere.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6246000
chris_calabrese

where does ex come into picture in griessh's function?
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6246014
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
 
LVL 11

Expert Comment

by:griessh
ID: 6246019
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
 
LVL 11

Expert Comment

by:griessh
ID: 6246023
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
 
LVL 3

Expert Comment

by:interiot
ID: 6246032
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6246040
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
 
LVL 11

Expert Comment

by:griessh
ID: 6246043
chris_calabrese
BTW: if you have "set -o noclobber" in your .profile, then "> filename" will fail!

======
Werner
0
 
LVL 11

Expert Comment

by:griessh
ID: 6246050
OK, I am giving up in this discussion ... the whole thing it's getting to perfect now :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now