This often happens when you have huge log files and How would you do that? One not so clean way would be to remove the file and then create a new file. But this is not a good idea. Instead of creating a new empty file, you can delete its content. So, how do you empty a file in Linux?
How to clear a file from all of its content without deleting the actual file? There are several ways you can empty a file without actually deleting the file. Let me show you some of these methods. When you use -s 0, it means you adjusted the file size to 0 bytes. The simplest way to empty a file is to use the command below. If the file is not in use, it will work in Bash:. Another way to empty a file is via echo command in Linux :.
This applies to the cp copy as well. Steven Sim Kok Leong. So nothing is redirected and not null like expected. Hope this helps Regards Stefan No Mouse found. System halted. Press Mousebutton to continue. Sorry - I missed the part about just wanting to zero the file The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise.
By using this site, you accept the Terms of Use and Rules of Participation. End of content United States. Viewed 11k times. Improve this question. ShellFish 4, 1 1 gold badge 18 18 silver badges 32 32 bronze badges. Made some edits..
Samuel u r right. Are you saying you want to overwrite the file with nothing, setting the length to 0? Show 1 more comment. Active Oldest Votes. Hard coded solutions tee Echo nothing and simply send it to multiple files using the tee command.
Structural file names solution If all files have a structure on their names for instance java files and location you could use the find command. Set this to 1 to not descend or n to descend n times. List of files in separate file solution The easiest thing to do might be to store the files to erase in a file, line by line.
Say the files are stored in a file called erasure. Improve this answer. ShellFish ShellFish 4, 1 1 gold badge 18 18 silver badges 32 32 bronze badges. Second code snippet suffices right? Am I missing something — Srujan Kumar Gulla. Don't forget to accept if the answer helped and upvote if you deem it a quality answer.
Now that's what we call a comprehensive answer. Great stuff. Show 4 more comments. Truncate can be used to empty a file as well.
0コメント