Clearing file contents | Unix Linux Forums | Filesystems, Disks and Memory

  Go Back    


Filesystems, Disks and Memory Discuss NAS, SAN, RAID, Robotic Libraries, backup devices, RAM, DRAM, SCSI, IDE, EIDE topics here.

Clearing file contents

Filesystems, Disks and Memory


Tags
linux, unix commands

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-18-2006
raghukayyar raghukayyar is offline
Registered User
 
Join Date: May 2006
Last Activity: 24 May 2006, 12:51 AM EDT
Location: Bangalore, India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Clearing file contents

Hi,

We need to clear the contents of a file. For this we tried using the commands like - '>filename' and 'cat filename > /dev/null'.

If the file size is large, the above commands will clear the file contents but the size of the file remains the same (checked using the command 'ls -l filename').

Are there any other UNIX commands which will empty the file contents?

Regards,
Raghavendra
Sponsored Links
    #2  
Old 05-18-2006
dkaplowitz dkaplowitz is offline
Registered User
 
Join Date: Jun 2004
Last Activity: 22 July 2009, 8:31 AM EDT
Location: ~Philadelphia
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
`>filename` works for me on Solaris and Gentoo Linux (and I'm assuming every other Linux). Makes an empty, 0-byte file. The `cat filename > /dev/null` doesn't change the file since you are only catenating its contents and sending that output to /dev/null.
Sponsored Links
    #3  
Old 05-19-2006
raghukayyar raghukayyar is offline
Registered User
 
Join Date: May 2006
Last Activity: 24 May 2006, 12:51 AM EDT
Location: Bangalore, India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Clearing file contents

Hi,

Thanks for the reply...

But, as I mentioned earlier if the filesize is very large, say 500MB, then if I use the command ">filename", it will clear the file contents, but, when I do the "ls -l filename" it will will show the file size as 500MB itself, not as 0 bytes.

If the filesize is less, it will work without any problem.

It will be of great help, to get any other UNIX command which will clear the file contents.

Regards,
Raghu
    #4  
Old 05-22-2006
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
>filename should work very well.

See this


Code:
[/tmp]$ dd if=/dev/zero of=/tmp/bigfile bs=1000 count=500000
500000+0 records in
500000+0 records out
[/tmp]$ ls -l /tmp/bigfile
-rw-r--r--    1 xxxxxxxx g900     500000000 May 22 00:11 /tmp/bigfile
[/tmp]$ >/tmp/bigfile
[/tmp]$ ls -l /tmp/bigfile
-rw-r--r--    1 xxxxxxxx g900            0 May 22 00:11 /tmp/bigfile
[/tmp]$

Since you notice the file size is still the same, it could be that some process is still using the file (?)
Sponsored Links
    #5  
Old 05-22-2006
kduffin's Avatar
kduffin kduffin is offline Forum Advisor  
UN1X
 
Join Date: Nov 2003
Last Activity: 21 January 2014, 4:03 PM EST
Location: Belize
Posts: 484
Thanks: 0
Thanked 3 Times in 2 Posts
Use 'lsof' or 'fuser' to see what process is holding the file handler open.
Sponsored Links
    #6  
Old 05-23-2006
raghukayyar raghukayyar is offline
Registered User
 
Join Date: May 2006
Last Activity: 24 May 2006, 12:51 AM EDT
Location: Bangalore, India
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Clearing file contents

Are there any other UNIX commands (apart from '>filename) which can be used to clear the file contents?
Sponsored Links
    #7  
Old 05-27-2006
shesatmine shesatmine is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 6 March 2007, 7:44 AM EST
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by raghukayyar
Are there any other UNIX commands (apart from '>filename) which can be used to clear the file contents?
There is some command called shred which overrides file's contents, but it's not effective with many filesystems suchs as JFS, XFS, ReiserFS... and those which doesn't overwrite data in place.

Salut
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace partial contents of file with contents read from other file seeki Shell Programming and Scripting 2 03-12-2012 06:54 AM
I want to delete the contents of a file which are matching with contents of other file ranasheel2000 Shell Programming and Scripting 3 09-02-2011 08:15 AM
compare 2 file contents , if same delete 2nd file contents krishnampkkm UNIX for Dummies Questions & Answers 5 03-08-2010 03:18 AM
Clearing file contents raghukayyar Solaris 2 05-22-2006 07:12 AM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 09:25 AM



All times are GMT -4. The time now is 07:45 PM.