Solved

Clear contents of a file

Posted on 2007-03-28
7
1,660 Views
Last Modified: 2013-12-27
Let's say that I have a Java program running, with its standard output directed to a log file:

java MyJavaProgram > stdout.log

This program will keep on running. To ensure that the log file does not grow very big, I am trying to write a script to remove the contents of the file. I tried two ways:

echo "" > stdout.log

cp /dev/null stdout.log

But in both cases, they are writting a lot of null characters into the log file. The file does not shrink at all. The contents are merely replaced with null characters.

How can I achive what I want? Thanks.
0
Comment
Question by:integrosys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:mdrichards
ID: 18814032
You could remove the file then "touch" it

rm -f stdout.log
touch stdout.log

This is probably not the most ideal scenario as you "may" need to reset user / group permissions.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18814153
You can only reliably truncate a file when it's not being written to.

Do you have any control over the java program, ie: can you restart it?

Easiest way to truncate a file is

>stdout.log
0
 

Author Comment

by:integrosys
ID: 18814216
Thanks guys. Actually I found that these two lines:

echo "" > stdout.log
cp /dev/null stdout.log

will work if the Java program is started this way instead:

java MyJavaProgram >> stdout.log
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Accepted Solution

by:
blu earned 50 total points
ID: 18844166
The problem you were seeing in the first instance is that the java program's file pointer continues to point to the same block that it is writing to even after you have truncated the file. Thus when it writes again, it writes to the same block it would have written to before you truncated. This actually does not fill in the file with nulls, it only appears to. It actually transforms the file into a sparse file. It will not actually allocate storage blocks for the intervening null blocks. If space is your only concern, then this may be enough.

You can see why converting to >> work though. The >> says to append to the file, which means that all blocks are written to the end of the file. This means that once the file is truncated, the end of the file becomes the beginning as it were.
0
 
LVL 9

Expert Comment

by:yongsing
ID: 18844354
Hi blu, what is a sparse file? You said that it does not allocate storage blocks. But when I use the ls command, I notice that the file size never decreases.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18847201
If you use

du -sh file

You'll see the real size of the file.
0
 
LVL 22

Expert Comment

by:blu
ID: 18852087
Or use "ls -ls"
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

734 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