How to extend a file in linux or how to increase a file size by command line method ‘truncate’

Here is a small tip.

People used to ask me, how can we increase a file size in linux ? Mostly it is in conjuction with ‘virtual image file’ .. It is possible to increase/decrease the size of the file using 2 ways (those are atleast known to me)

One method is using ‘dd‘ command. I am sure it is pretty much common to you . If not, let me know, so that I can expand it.

Other being ‘truncate’ command. I will give small demo on how to use ‘truncate’ command.

 truncate – shrink or extend the size of a file to the specified size

truncate OPTION  FILE…

A FILE argument that does not exist is created.

If  a  FILE is larger than the specified size, the extra data is lost.  If a FILE is shorter, it is extended and the extended part (hole)
reads as zero bytes.

For more information please refer #man truncate

Here is an example from my system:

[root@humbles-lap ~]# rpm -qf which truncate
[root@humbles-lap ~]#

Note the size of the file called ‘cscope.out’ , this can be a virtual image file as well:

[root@humbles-lap~]# ls -lh cscope.out
-rw-r–r– 1 root root 197M Feb 15 12:36 cscope.out

Use ‘truncate’ to extend ‘100M’

[root@humbles-lap ~]# truncate –size=+100M cscope.out

Note new size of the file

[root@humbles-lap ~]# ls -lh cscope.out
-rw-r–r– 1 root root 297M Feb 15 12:38 cscope.out

Leave a Reply

Your email address will not be published. Required fields are marked *