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?
`>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.
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.
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.