- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a file of certain size in Linux
When using Linux, we often perform various operations on our own personal data. One common operation is to create an empty file of a specific size.
There are many ways or command to achive this, here we’ll cover some of these methods −
dd − The dd command is a utility for copying files and converting the format of data. It can be used to create a file of a certain size by specifying the block size and the number of blocks with the bs and count options, respectively. The dd command reads data from an input file (specified with the if option) and writes it to an output file (specified with the of option). If you don't specify an input file, dd reads from the standard input. If you don't specify an output file, dd writes to the standard output.
fallocate − The fallocate command is a utility for manipulating file space on a Linux file system. It can be used to create a file of a certain size by specifying the size with the -l option. The fallocate command uses the fallocate() system call to pre-allocate space on the file system for the file, rather than writing zeros to the file. This can be faster than using dd if you just need to create an empty file of a certain size and don't need to fill it with data.
truncate − The truncate command is a utility for shrinking or extending the size of a file. It can be used to create a file of a certain size by specifying the size with the -s option. If the truncate command-specified file does not exist, it will be created. If it does exist, then its size will be adjusted to the given size. This command is useful for creating blank files or resizing existing ones to a particular size.
Using The fallocate Command
Alternatively, you can use the fallocate command to create a file of a certain size. This command is similar to dd, but it uses the fallocate() system call to pre-allocate space on the file system for the file, rather than writing zeros to the file. This can be faster than using dd if you just need to create an empty file of a certain size and don't need to fill it with data.
Here's an example of how you can use fallocate to create a file of a certain size −
fallocate -l 10485760 file.txt
This command creates a file called file.txt that is 10485760 bytes in size.
Using the truncate Command
You can also use the truncate command to create a file of a certain size. This command allows you to create an empty file or resize an existing file to a specific size.
Here's an example of how you can use truncate to create a file of a certain size −
truncate -s 10485760 file.txt
This command generates a 10485760 bytes file called file.txt, and if the file has already existed, it will be truncated to its specified size.
Using The dd Command
To create a file of a certain size in Linux, you can use the dd command. The dd command allows you to create a file with a specific number of blocks, where each block is a certain number of bytes. You can use the bs option to specify the block size and the count option to specify the number of blocks.
Here's an example of how you can use dd to create a file of a certain size −
dd if=/dev/zero of=file.txt bs=1024 count=10240
This command creates a file called file.txt that is 10240 blocks of 1024 bytes each, for a total size of 10240 * 1024 = 10485760 bytes.
Using The head and tail Commands
You can use the head -c /dev/zero file syntax to create a file containing a specific number of ASCII NUL (0) characters.
$ head --bytes 300K /dev/zero > file3.txt $ ls -lh file3.txt -rw-rw-r-- 1 groot groot 300K May 15 20:47 file3.txt
Similarly, the tail -f /var/log/syslog can be used in the exact same way.
$ tail --bytes 1G /dev/zero > file4.txt $ ls -lh file4.txt -rw-rw-r-- 1 groot groot 1.0G May 15 20:52 file4.txt
We've covered five ways to get files of a specific size using the Linux command line. You can use these techniques when you're doing everyday tasks with the Linux operating system.
Kickstart Your Career
Get certified by completing the courseGet Started