Jump to content
daredevil

linux truncate - empty(clear) file in linux

Recommended Posts

To clear / truncate / empty file in linux u can use below command from bash:

 

cat /dev/null >| filename

 

Does anyone know any other commands to achieve the same thing in linux?

Share this post


Link to post
Share on other sites

echo "" > filename

 

:> filename

 

>filename

 

rm filename && touch filename

 

cat /dev/null > filename

 

truncate -s0 filename

  • Like 1

Share this post


Link to post
Share on other sites

try below

 

cp /dev/null filename

 

while the next one below will give you a visually empty file but the size of the file would still be 1 since

""

is a character #13 or #10 which is invincible to human eyes. :P

echo "" > filename

 

also, the below code should be executed under the actual ownership of the file, so it doesnt change file ownership and access.

rm filename && touch filename

 

If you are root and going to do the last command with file(s) owned by postfix or named, the last command above is critical and may create another issue for you I think so.

Edited by PiNoY

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×