Empty the contents of a file











up vote
200
down vote

favorite
65












I am aware of three methods to delete all entries from a file.



They are




  • >filename

  • touch filename

  • filename < /dev/null


Of these three I abuse >filename the most as that requires the least number of keystrokes.



However, I would like to know which is the most efficient of the three (if there are any more efficient methods) with respect to large log files and small files.



Also, how does the three codes operate and delete the contents?










share|improve this question


















  • 22




    What about truncate -s 0 filename?
    – Martin Thoma
    Jun 29 '14 at 16:18










  • Very similar to Difference between cat and '>' to zero out a file where you'll find more information.
    – Stéphane Chazelas
    Oct 26 '15 at 15:27










  • The first will work only if called from bash command line, but won't work if executed in a .sh file
    – Marco Marsala
    Nov 24 '15 at 11:04






  • 6




    touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
    – hbogert
    Sep 28 '16 at 10:12

















up vote
200
down vote

favorite
65












I am aware of three methods to delete all entries from a file.



They are




  • >filename

  • touch filename

  • filename < /dev/null


Of these three I abuse >filename the most as that requires the least number of keystrokes.



However, I would like to know which is the most efficient of the three (if there are any more efficient methods) with respect to large log files and small files.



Also, how does the three codes operate and delete the contents?










share|improve this question


















  • 22




    What about truncate -s 0 filename?
    – Martin Thoma
    Jun 29 '14 at 16:18










  • Very similar to Difference between cat and '>' to zero out a file where you'll find more information.
    – Stéphane Chazelas
    Oct 26 '15 at 15:27










  • The first will work only if called from bash command line, but won't work if executed in a .sh file
    – Marco Marsala
    Nov 24 '15 at 11:04






  • 6




    touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
    – hbogert
    Sep 28 '16 at 10:12















up vote
200
down vote

favorite
65









up vote
200
down vote

favorite
65






65





I am aware of three methods to delete all entries from a file.



They are




  • >filename

  • touch filename

  • filename < /dev/null


Of these three I abuse >filename the most as that requires the least number of keystrokes.



However, I would like to know which is the most efficient of the three (if there are any more efficient methods) with respect to large log files and small files.



Also, how does the three codes operate and delete the contents?










share|improve this question













I am aware of three methods to delete all entries from a file.



They are




  • >filename

  • touch filename

  • filename < /dev/null


Of these three I abuse >filename the most as that requires the least number of keystrokes.



However, I would like to know which is the most efficient of the three (if there are any more efficient methods) with respect to large log files and small files.



Also, how does the three codes operate and delete the contents?







shell-script files






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 30 '13 at 5:13









debal

1,59451118




1,59451118








  • 22




    What about truncate -s 0 filename?
    – Martin Thoma
    Jun 29 '14 at 16:18










  • Very similar to Difference between cat and '>' to zero out a file where you'll find more information.
    – Stéphane Chazelas
    Oct 26 '15 at 15:27










  • The first will work only if called from bash command line, but won't work if executed in a .sh file
    – Marco Marsala
    Nov 24 '15 at 11:04






  • 6




    touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
    – hbogert
    Sep 28 '16 at 10:12
















  • 22




    What about truncate -s 0 filename?
    – Martin Thoma
    Jun 29 '14 at 16:18










  • Very similar to Difference between cat and '>' to zero out a file where you'll find more information.
    – Stéphane Chazelas
    Oct 26 '15 at 15:27










  • The first will work only if called from bash command line, but won't work if executed in a .sh file
    – Marco Marsala
    Nov 24 '15 at 11:04






  • 6




    touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
    – hbogert
    Sep 28 '16 at 10:12










22




22




What about truncate -s 0 filename?
– Martin Thoma
Jun 29 '14 at 16:18




What about truncate -s 0 filename?
– Martin Thoma
Jun 29 '14 at 16:18












Very similar to Difference between cat and '>' to zero out a file where you'll find more information.
– Stéphane Chazelas
Oct 26 '15 at 15:27




Very similar to Difference between cat and '>' to zero out a file where you'll find more information.
– Stéphane Chazelas
Oct 26 '15 at 15:27












The first will work only if called from bash command line, but won't work if executed in a .sh file
– Marco Marsala
Nov 24 '15 at 11:04




The first will work only if called from bash command line, but won't work if executed in a .sh file
– Marco Marsala
Nov 24 '15 at 11:04




6




6




touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
– hbogert
Sep 28 '16 at 10:12






touch does not delete contents, but does change access time on the file. It does create an empty file if none existed.
– hbogert
Sep 28 '16 at 10:12












3 Answers
3






active

oldest

votes

















up vote
272
down vote



accepted










Actually, the second form touch filename doesn't delete anything from the file - it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.



And the third filename < /dev/null tries to run filename with /dev/null as input.



cp /dev/null filename works.



As for efficient, the most efficient would be truncate -s 0 filename; see here: http://linux.die.net/man/1/truncate.



Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null, so that makes it marginally slower.



On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and the run it.






share|improve this answer

















  • 7




    So why do you say that truncate is the most efficient?
    – Stéphane Chazelas
    Aug 30 '13 at 6:24






  • 6




    The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
    – ash
    Aug 30 '13 at 6:26






  • 3




    Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
    – Stéphane Chazelas
    Aug 30 '13 at 8:01












  • If we use touch filename, will the inode remain same (provided there was a file before)?
    – pMan
    Aug 30 '13 at 8:30






  • 1




    @pMan yes, you can try it and check with ls -i
    – terdon
    Aug 30 '13 at 13:02


















up vote
36
down vote













Other option could be:



echo -n > filename


From the man page of echo:




-n Do not print the trailing newline character.







share|improve this answer





















  • How can I set the size? Say if I want 30000 null characters?
    – User
    Nov 16 '16 at 23:52




















up vote
0
down vote













There is a builtin command ":", which is available in sh,csh,bash and others maybe, which can be easily used with the redirecting output operator > truncate a file:



#!/usr/bin/env bash
:> filename


What I like on this is, that it does not need any external commands like "echo" etc.



One big advantage of truncating files instead of deleate/recreate them is, that running applications which works with this file (e.g. someone makes an tail -f filename or a monitoring software, ...) don't have to reopen it. They just can continue using the filedescriptor and gets all the new data.






share|improve this answer























  • man bash describes the : shell builtin as having no effect.
    – Haxiel
    Dec 3 at 10:06










  • Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
    – Mirko Steiner
    Dec 3 at 10:10






  • 1




    Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
    – terdon
    Dec 3 at 11:12










  • sometimes, > filename won't work. for example, in zsh. but : > filename works still.
    – CS Pei
    yesterday










protected by Anthon Jun 20 '17 at 10:06



Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?














3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
272
down vote



accepted










Actually, the second form touch filename doesn't delete anything from the file - it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.



And the third filename < /dev/null tries to run filename with /dev/null as input.



cp /dev/null filename works.



As for efficient, the most efficient would be truncate -s 0 filename; see here: http://linux.die.net/man/1/truncate.



Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null, so that makes it marginally slower.



On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and the run it.






share|improve this answer

















  • 7




    So why do you say that truncate is the most efficient?
    – Stéphane Chazelas
    Aug 30 '13 at 6:24






  • 6




    The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
    – ash
    Aug 30 '13 at 6:26






  • 3




    Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
    – Stéphane Chazelas
    Aug 30 '13 at 8:01












  • If we use touch filename, will the inode remain same (provided there was a file before)?
    – pMan
    Aug 30 '13 at 8:30






  • 1




    @pMan yes, you can try it and check with ls -i
    – terdon
    Aug 30 '13 at 13:02















up vote
272
down vote



accepted










Actually, the second form touch filename doesn't delete anything from the file - it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.



And the third filename < /dev/null tries to run filename with /dev/null as input.



cp /dev/null filename works.



As for efficient, the most efficient would be truncate -s 0 filename; see here: http://linux.die.net/man/1/truncate.



Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null, so that makes it marginally slower.



On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and the run it.






share|improve this answer

















  • 7




    So why do you say that truncate is the most efficient?
    – Stéphane Chazelas
    Aug 30 '13 at 6:24






  • 6




    The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
    – ash
    Aug 30 '13 at 6:26






  • 3




    Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
    – Stéphane Chazelas
    Aug 30 '13 at 8:01












  • If we use touch filename, will the inode remain same (provided there was a file before)?
    – pMan
    Aug 30 '13 at 8:30






  • 1




    @pMan yes, you can try it and check with ls -i
    – terdon
    Aug 30 '13 at 13:02













up vote
272
down vote



accepted







up vote
272
down vote



accepted






Actually, the second form touch filename doesn't delete anything from the file - it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.



And the third filename < /dev/null tries to run filename with /dev/null as input.



cp /dev/null filename works.



As for efficient, the most efficient would be truncate -s 0 filename; see here: http://linux.die.net/man/1/truncate.



Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null, so that makes it marginally slower.



On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and the run it.






share|improve this answer












Actually, the second form touch filename doesn't delete anything from the file - it only creates an empty file if one did not exist, or updates the last-modified date of an existing file.



And the third filename < /dev/null tries to run filename with /dev/null as input.



cp /dev/null filename works.



As for efficient, the most efficient would be truncate -s 0 filename; see here: http://linux.die.net/man/1/truncate.



Otherwise, cp /dev/null filename or > filename are both fine. They both open and then close the file, using the truncate-on-open setting. cp also opens /dev/null, so that makes it marginally slower.



On the other hand, truncate would likely be slower than > filename when run from a script since running the truncate command requires the system to open the executable, load it, and the run it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 30 '13 at 5:19









ash

4,57011311




4,57011311








  • 7




    So why do you say that truncate is the most efficient?
    – Stéphane Chazelas
    Aug 30 '13 at 6:24






  • 6




    The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
    – ash
    Aug 30 '13 at 6:26






  • 3




    Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
    – Stéphane Chazelas
    Aug 30 '13 at 8:01












  • If we use touch filename, will the inode remain same (provided there was a file before)?
    – pMan
    Aug 30 '13 at 8:30






  • 1




    @pMan yes, you can try it and check with ls -i
    – terdon
    Aug 30 '13 at 13:02














  • 7




    So why do you say that truncate is the most efficient?
    – Stéphane Chazelas
    Aug 30 '13 at 6:24






  • 6




    The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
    – ash
    Aug 30 '13 at 6:26






  • 3




    Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
    – Stéphane Chazelas
    Aug 30 '13 at 8:01












  • If we use touch filename, will the inode remain same (provided there was a file before)?
    – pMan
    Aug 30 '13 at 8:30






  • 1




    @pMan yes, you can try it and check with ls -i
    – terdon
    Aug 30 '13 at 13:02








7




7




So why do you say that truncate is the most efficient?
– Stéphane Chazelas
Aug 30 '13 at 6:24




So why do you say that truncate is the most efficient?
– Stéphane Chazelas
Aug 30 '13 at 6:24




6




6




The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
– ash
Aug 30 '13 at 6:26




The truncate operation uses the ftruncate() or truncate() system call which does not bother to open the file. It also avoids the close() system call that cp and > filename methods need to call.
– ash
Aug 30 '13 at 6:26




3




3




Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
– Stéphane Chazelas
Aug 30 '13 at 8:01






Actually, it (at least the GNU one) does an open+ftruncate+close (in addition to the many system calls it does to load and initialise itself), as anyway, it would have to create the file if it didn't exist and truncate(2) doesn't do that.
– Stéphane Chazelas
Aug 30 '13 at 8:01














If we use touch filename, will the inode remain same (provided there was a file before)?
– pMan
Aug 30 '13 at 8:30




If we use touch filename, will the inode remain same (provided there was a file before)?
– pMan
Aug 30 '13 at 8:30




1




1




@pMan yes, you can try it and check with ls -i
– terdon
Aug 30 '13 at 13:02




@pMan yes, you can try it and check with ls -i
– terdon
Aug 30 '13 at 13:02












up vote
36
down vote













Other option could be:



echo -n > filename


From the man page of echo:




-n Do not print the trailing newline character.







share|improve this answer





















  • How can I set the size? Say if I want 30000 null characters?
    – User
    Nov 16 '16 at 23:52

















up vote
36
down vote













Other option could be:



echo -n > filename


From the man page of echo:




-n Do not print the trailing newline character.







share|improve this answer





















  • How can I set the size? Say if I want 30000 null characters?
    – User
    Nov 16 '16 at 23:52















up vote
36
down vote










up vote
36
down vote









Other option could be:



echo -n > filename


From the man page of echo:




-n Do not print the trailing newline character.







share|improve this answer












Other option could be:



echo -n > filename


From the man page of echo:




-n Do not print the trailing newline character.








share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 23 '16 at 20:35









Arturo Herrero

95121117




95121117












  • How can I set the size? Say if I want 30000 null characters?
    – User
    Nov 16 '16 at 23:52




















  • How can I set the size? Say if I want 30000 null characters?
    – User
    Nov 16 '16 at 23:52


















How can I set the size? Say if I want 30000 null characters?
– User
Nov 16 '16 at 23:52






How can I set the size? Say if I want 30000 null characters?
– User
Nov 16 '16 at 23:52












up vote
0
down vote













There is a builtin command ":", which is available in sh,csh,bash and others maybe, which can be easily used with the redirecting output operator > truncate a file:



#!/usr/bin/env bash
:> filename


What I like on this is, that it does not need any external commands like "echo" etc.



One big advantage of truncating files instead of deleate/recreate them is, that running applications which works with this file (e.g. someone makes an tail -f filename or a monitoring software, ...) don't have to reopen it. They just can continue using the filedescriptor and gets all the new data.






share|improve this answer























  • man bash describes the : shell builtin as having no effect.
    – Haxiel
    Dec 3 at 10:06










  • Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
    – Mirko Steiner
    Dec 3 at 10:10






  • 1




    Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
    – terdon
    Dec 3 at 11:12










  • sometimes, > filename won't work. for example, in zsh. but : > filename works still.
    – CS Pei
    yesterday















up vote
0
down vote













There is a builtin command ":", which is available in sh,csh,bash and others maybe, which can be easily used with the redirecting output operator > truncate a file:



#!/usr/bin/env bash
:> filename


What I like on this is, that it does not need any external commands like "echo" etc.



One big advantage of truncating files instead of deleate/recreate them is, that running applications which works with this file (e.g. someone makes an tail -f filename or a monitoring software, ...) don't have to reopen it. They just can continue using the filedescriptor and gets all the new data.






share|improve this answer























  • man bash describes the : shell builtin as having no effect.
    – Haxiel
    Dec 3 at 10:06










  • Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
    – Mirko Steiner
    Dec 3 at 10:10






  • 1




    Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
    – terdon
    Dec 3 at 11:12










  • sometimes, > filename won't work. for example, in zsh. but : > filename works still.
    – CS Pei
    yesterday













up vote
0
down vote










up vote
0
down vote









There is a builtin command ":", which is available in sh,csh,bash and others maybe, which can be easily used with the redirecting output operator > truncate a file:



#!/usr/bin/env bash
:> filename


What I like on this is, that it does not need any external commands like "echo" etc.



One big advantage of truncating files instead of deleate/recreate them is, that running applications which works with this file (e.g. someone makes an tail -f filename or a monitoring software, ...) don't have to reopen it. They just can continue using the filedescriptor and gets all the new data.






share|improve this answer














There is a builtin command ":", which is available in sh,csh,bash and others maybe, which can be easily used with the redirecting output operator > truncate a file:



#!/usr/bin/env bash
:> filename


What I like on this is, that it does not need any external commands like "echo" etc.



One big advantage of truncating files instead of deleate/recreate them is, that running applications which works with this file (e.g. someone makes an tail -f filename or a monitoring software, ...) don't have to reopen it. They just can continue using the filedescriptor and gets all the new data.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 3 at 10:12

























answered Dec 3 at 9:59









Mirko Steiner

292




292












  • man bash describes the : shell builtin as having no effect.
    – Haxiel
    Dec 3 at 10:06










  • Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
    – Mirko Steiner
    Dec 3 at 10:10






  • 1




    Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
    – terdon
    Dec 3 at 11:12










  • sometimes, > filename won't work. for example, in zsh. but : > filename works still.
    – CS Pei
    yesterday


















  • man bash describes the : shell builtin as having no effect.
    – Haxiel
    Dec 3 at 10:06










  • Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
    – Mirko Steiner
    Dec 3 at 10:10






  • 1




    Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
    – terdon
    Dec 3 at 11:12










  • sometimes, > filename won't work. for example, in zsh. but : > filename works still.
    – CS Pei
    yesterday
















man bash describes the : shell builtin as having no effect.
– Haxiel
Dec 3 at 10:06




man bash describes the : shell builtin as having no effect.
– Haxiel
Dec 3 at 10:06












Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
– Mirko Steiner
Dec 3 at 10:10




Yes, and you redirect this with > in to the file, which creates the file if it does not exists, and if it exists you truncate it to zero. Better said: you use the : to do nothing, and use > to redirect nothing to a file, and truncate it.
– Mirko Steiner
Dec 3 at 10:10




1




1




Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
– terdon
Dec 3 at 11:12




Why would you do that? > file is enough to truncate a file. You don't need any command, just the redirection operator.
– terdon
Dec 3 at 11:12












sometimes, > filename won't work. for example, in zsh. but : > filename works still.
– CS Pei
yesterday




sometimes, > filename won't work. for example, in zsh. but : > filename works still.
– CS Pei
yesterday





protected by Anthon Jun 20 '17 at 10:06



Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



Would you like to answer one of these unanswered questions instead?



Popular posts from this blog

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt