Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty a file in linux bash [duplicate]

which one is the best way to clean or empty a file in Linux? I have to zip ( tar ) a file to an archive and then clean/empty it; this is what I do and it works correctly:

tar -zcvf /mnt/file.tar.gz /mnt/file.txt > /dev/null 2>&1

echo "" > /mnt/file.txt

I'm doing it with echo, probably there is a better way ?

Thanks

like image 294
DDBE Avatar asked Apr 15 '26 09:04

DDBE


1 Answers

There are multiple ways to do that:

We presume that our file is called access.log and it's in the current directory:

1.

: > access.log

2.

true > access.log

3.

cat /dev/null > access.log

4.

cp /dev/null access.log

5.

dd if=/dev/null of=access.log

6.

echo -n "" > access.log

7.

echo -n > access.log
like image 104
Dan Ionescu Avatar answered Apr 16 '26 23:04

Dan Ionescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!