How to create file with size in Linux.

 To create file with size you can use following commands on different platform.
On Solaris, you can use this command:
$ mkfile file-size
$ mkfile 10m

On RHEL Linux, you can use the dd command:
dd if=/dev/zero
of=output.dat  bs=24M  count=1
dd if=/dev/zero
of=output.dat  bs=1M  count=24
[root@kernal ~]# dd if=/dev/zero of=file.example
bs=10M count=1
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0559974 s,
187 MB/s
For Check size of the file:
[root@kernal ~]# ls -hl file.example
-rw-r–r– 1 root root 10M Jul 26 19:47 file.example
Under non-embedded Linux or Cygwin (or any
system with GNU coreutils) and FreeBSD:
# truncate -s 10G
[root@kernal ~]# truncate -s 20m file
[root@kernal ~]#
[root@kernal ~]# ls -hl file
-rw-r–r– 1 root root 20M Jul 26 19:57 file
This creates a file
full of null bytes. If the file already exists and is smaller, it is extended
to the requested size with null bytes. If the file already exists and is
larger, is is truncated to the requested size.
The null bytes do not
consume any disk space, the file is a sparse file.
# fallocate -l 5G
~]#  fallocate  -l  25M
~]# ls  –hl  file1
1 root root 25M Jul 26 19:57 file1
can do it using Program:

main() {
    int fd = creat(“/tmp/foo.txt”,
    ftruncate(fd, SIZE_IN_BYTES);
    return 0;
This  is
useful to subsequently mmap the file into memory.
use the following command to check  file size:
du -B1 –apparent-size /tmp/foo.txt

du  /tmp/foo.txt

It will print 0 because it is allocated as Sparse
file if supported by your filesystem.

Linux Interview Questions And Answers

Leave a Reply

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