YouTube Icon

Code Playground.

How to Truncate (Empty) Files in Linux


How to Truncate (Empty) Files in Linux

In certain circumstances, you should shorten (void) a current record to a zero-length. In basic words, shortening a record implies eliminating the document substance without erasing the record. 

Shortening a document is a lot quicker and simpler than erasing the record , reproducing it, and setting the right consents and proprietorship . Additionally, if the record is opened by a cycle, eliminating the document may cause the program that utilizes it to breakdown. 

This instructional exercise discloses how to shorten documents to zero size in Linux frameworks utilizing shell redirection and the shorten order. 

Shell Redirection

The simplest and most utilized strategy to shorten documents is to utilize the > shell redirection administrator. 

The overall organization for shortening records utilizing redirection is: 

: > filename

We should separate the order: 

The : colon implies valid and creates no yield. 

The redirection administrator > divert the yield of the previous order to the given document. 

filename, the record you need to shorten. 

On the off chance that the document exists , it will be shortened to zero. Something else, the record will be made. 

Rather than : can likewise utilize another order that delivers no yield. 

Here is a case of utilizing the feline order to yield the substance of the/dev/invalid gadget, which returns just a finish of-document character: 

cat /dev/null > filename

Another order that can be utilized is reverberation . The - n choice advises reverberation not to attach a newline: 

echo -n > filename

On most present day shells, for example, Bash or Zsh you can exclude the order before the redirection image and use: 

> filename

To have the option to shorten a document, you have to have compose authorizations on the record. For the most part, you would utilize sudo for this, however the raised root advantages don't make a difference to the redirection. Here is a model: 

sudo : > /var/log/syslog
bash: /var/log/syslog: Permission denied

There are a few arrangements that permit diverting with sudo. The primary choice can run another shell with sudo and execute an order inside that shell utilizing the - c banner: 

sudo sh -c '> filename'

Another alternative is to pipe the yield to the tee order, hoist the tee advantages with sudo, and compose the vacant yield to a given document: 

: | sudo tee filename

truncate Command

shorten is an order line utility that permits you to shrivel or expand the size of a document to a given size. 

The overall language structure for shortening records to zero size with the shorten order, is as per the following: 

truncate -s 0 filename

The - s 0 alternative sets the document size to zero. 

For instance, to discharge the Nginx access log you would utilize: 

sudo truncate -s 0 /var/log/nginx/access.log

Empty All Log Files

Over the long run, your circle drive may get jumbled with a ton of huge log documents taking up a lot of plate space. 

The accompanying order will discharge records finishing with ".log" under the/var/log registry: 

sudo truncate -s 0 /var/log/**/*.log 

A superior alternative is pivot, pack, and eliminate the logs records with the logrotate apparatus. 


To shorten a record in Linux utilize the redirection administrator > followed by the document name. 

In the event that you have any inquiries or comments, if it's not too much trouble leave a remark beneath.