tags:

views:

44

answers:

2
FILE=a.txt 
FILE_SIZE = `stat -c %s $FILE`

if [ $FILESIZE >= 1000  ]; then 

   cp $FILE /tmp/

   # How to empty file ? 

then 

I am trying to get size of file and if the size is over limited and cp those file and empty the same file

How to acheive this ?

+1  A: 
echo > $FILE

But be aware that if something is still writing to the file, you'll lose the output that happens between the cp and the echo.

Borealid
+1  A: 

You'd probably be better off doing something like:

mv ${FILE} ${FILE}.staging

since that'll be near-atomic in duration.

Then mv it to the tmp filesystem after that while processes will recreate a new ${FILE}.

Keep in mind that, if you have a long lived process writing to ${FILE}, it will continue to write to ${FILE}.staging after the first move.

But, if you just have a lot of processes which open/write/close your file, the atomic move is probably the safest way to do it.

paxdiablo