Tree
Tree

Reputation: 10342

How to find the size of file

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 ?

Upvotes: 0

Views: 252

Answers (2)

paxdiablo
paxdiablo

Reputation: 881303

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.

Upvotes: 1

Borealid
Borealid

Reputation: 98469

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.

Upvotes: 1

Related Questions