- 6 Methods to create files of specific size in Linux
- How To Create A File In Specific Size In Linux Using fallocate Command?
- How To Create A File In Specific Size In Linux Using truncate Command?
- How To Create A File In Specific Size In Linux Using dd Command?
- How To Create A File In Specific Size In Linux Using head Command?
- How To Create A File In Specific Size In Linux Using xfs_mkfile Command?
- How To Create A File In Specific Size In Linux Using perl Command?
- How to create a file with ANY given size in Linux?
- 4 Answers 4
- Sparse file
- Non-sparse (opaque) file:
- How to create a file with a given size in linux?
- Method 1: Using the «dd» command
- Method 2: Using the «fallocate» command
- Method 3: Using the «truncate» command
- Method 4: Using the «touch» command
- Method 5: Using the «echo» command
- Conclusion
- How can I create a file with a specific size from a command line?
- 7 Answers 7
6 Methods to create files of specific size in Linux
If you want to create a specific size of a file in Linux to test your upload or download speed on your file server. Or you want to create a certain size of a file in Linux to extend your swap partition then you can use one of the below method to create it.
Navigate to the following URL, if you want to know about multiple ways to create a file in Linux.
As we already know few commands to perform this but i have included all the possible options in this article.
You can check all the commands which is available in this article and choose the preferred one for you.
I’m going to create a 10M file using all commands to test this.
It can be done using the following 6 methods.
- fallocate: fallocate is used to preallocate or deallocate space to a file.
- truncate: truncate is used to shrink or extend the size of a file to the specified size.
- dd: Copy a file, converting and formatting according to the operands.
- head: head is used to print the first part of files.
- xfs_mkfile: xfs_mkfile command allow us to create a specific size of a file in Linux.
- perl: Perl is a programming language specially designed for text editing.
How To Create A File In Specific Size In Linux Using fallocate Command?
fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For filesystems which support the fallocate system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks. This is much faster than creating a file by filling it with zeroes.
$ fallocate -l 10M daygeek.txt
Use the ls command to check the given file size.
$ ls -lh daygeek.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:43 daygeek.txt
How To Create A File In Specific Size In Linux Using truncate Command?
truncate is used to shrink or extend the size of a file to the specified size.
$ truncate -s 10M daygeek1.txt
Use the ls command to check the given file size.
$ ls -lh daygeek1.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:44 daygeek1.txt
How To Create A File In Specific Size In Linux Using dd Command?
The dd command stands for data duplicator. It is used for copying and converting data (from standard input to standard output, by default).
Also, dd command allow us to create a Bootable USB Disk in Linux.
$ dd if=/dev/zero of=daygeek2.txt bs=10M count=1 or $ dd if=/dev/zero of=daygeek3.txt bs=1M count=10 1+0 records in 1+0 records out 10485760 bytes (10 MB, 10 MiB) copied, 0.03385 s, 310 MB/s
Use the ls command to check the given file size.
$ ls -lh daygeek2.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:44 daygeek2.txt
How To Create A File In Specific Size In Linux Using head Command?
The head command reads the first few lines of any text given to it as an input and writes them to standard output (which, by default, is the display screen).
$ head -c 10MB /dev/zero > daygeek4.txt
Use the ls command to check the given file size.
$ ls -lh daygeek4.txt -rw-rw-r-- 1 daygeek daygeek 9.6M Feb 3 13:45 daygeek4.txt
How To Create A File In Specific Size In Linux Using xfs_mkfile Command?
xfs_mkfile creates one or more files. The file is padded with zeroes by default. The default size is in bytes, but it can be flagged as kilobytes, blocks, megabytes, or gigabytes with the k, b, m or g suffixes, respectively.
$ xfs_mkfile 10M daygeek5.txt
Use the ls command to check the given file size.
$ 1 ls -lh daygeek5.txt -rw------- 1 daygeek daygeek 10M Feb 3 13:45 daygeek5.txt
How To Create A File In Specific Size In Linux Using perl Command?
Perl stands in for “Practical Extraction and Reporting Language”. Perl is a programming language specially designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, web development, etc.
$ perl -e 'print "a" x 10485760' > daygeek6.txt
Use the ls command to check the given file size.
$ ls -lh daygeek6.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:47 daygeek6.txt
All together in the single output.
$ ls -lh daygeek* -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:44 daygeek1.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:44 daygeek2.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:44 daygeek3.txt -rw-rw-r-- 1 daygeek daygeek 9.6M Feb 3 13:45 daygeek4.txt -rw------- 1 daygeek daygeek 10M Feb 3 13:45 daygeek5.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:47 daygeek6.txt -rw-rw-r-- 1 daygeek daygeek 10M Feb 3 13:43 daygeek.txt
How to create a file with ANY given size in Linux?
I have read this question: How to create a file with a given size in Linux? But I havent got answer to my question. I want to create a file of 372.07 MB, I tried the following commands in Ubuntu 10.08:
dd if=/dev/zero of=output.dat bs=390143672 count=1 dd: memory exhausted
390143672=372.07*1024*1024 Is there any other methods? Thanks a lot! Edit: How to view a file’s size on Linux command line with decimal. I mean, the command line ls -hl just says: ‘373M’ but the file is actually «372.07M».
4 Answers 4
Sparse file
dd of=output.dat bs=1 seek=390143672 count=0
This has the added benefit of creating the file sparse if the underlying filesystem supports that. This means, no space is wasted if some of the pages (_blocks) ever get written to and the file creation is extremely quick.
Non-sparse (opaque) file:
Edit since people have, rightly pointed out that sparse files have characteristics that could be disadvantageous in some scenarios, here is the sweet point:
You could use fallocate (in Debian present due to util-linux ) instead:
fallocate -l 390143672 output.dat
This still has the benefit of not needing to actually write the blocks, so it is pretty much as quick as creating the sparse file, but it is not sparse. Best Of Both Worlds.
How to create a file with a given size in linux?
Linux: how to create a file with a given size in Linux?
There are several ways to create a file with a specific size in Linux. Here are a few methods:
Method 1: Using the «dd» command
- Step 1 — Open a terminal window.
- Step 2 — Type the following command — «dd if=/dev/zero of=filename bs=1M count=size», where «filename» is the name of the file you want to create and «size» is the size of the file in megabytes
For example, «dd if=/dev/zero of=largefile bs=1M count=1024» will create a file named «largefile» with a size of 1024MB.
The file will be created in the current directory.
- «dd» is a command-line utility for copying and converting data.
- «if» specifies the input file. In this case, we are using «/dev/zero», which is a special file that provides an endless stream of null characters.
- «of» specifies the output file.
- «bs» specifies the block size. In this case, we are using 1MB.
- «count» specifies the number of blocks to copy.
Method 2: Using the «fallocate» command
- Step 1 — Open a terminal window.
- Step 2 — Type the following command — «fallocate -l size filename», where «size» is the size of the file in bytes and «filename» is the name of the file you want to create
For example, «fallocate -l 1G largefile» will create a file named «largefile» with a size of 1GB.
The file will be created in the current directory.
- «fallocate» is a command-line utility for allocating space to a file.
- «-l» specifies the size of the file to be created in bytes.
Method 3: Using the «truncate» command
- Step 1 — Open a terminal window.
- Step 2 — Type the following command — «truncate -s size filename», where «size» is the size of the file in bytes and «filename» is the name of the file you want to create
For example, «truncate -s 1G largefile» will create a file named «largefile» with a size of 1GB.
The file will be created in the current directory.
- «truncate» is a command-line utility for adjusting the size of a file to the specified size.
- «-s» specifies the size of the file in bytes.
Please note that these commands will create a file with only zeroes in it, if you want to add some data to the file you have to use other command like echo to write some data to the file.
Method 4: Using the «touch» command
- Step 1 — Open a terminal window.
- Step 2 — Type the following command — «touch filename», where «filename» is the name of the file you want to create
For example, «touch largefile» will create a file named «largefile» with a size of 0 bytes in the current directory.
The file will be created in the current directory.
- «touch» is a command-line utility for creating or updating the timestamp of a file.
- This command creates an empty file if the file does not exist, or updates the timestamp of the file if it already exists.
Method 5: Using the «echo» command
- Step 1 — Open a terminal window.
- Step 2 — Type the following command — «echo «text» > filename», where «text» is the text you want to write to the file and «filename» is the name of the file you want to create
For example, «echo «Hello World» > largefile» will create a file named «largefile» with the text «Hello World» in the current directory.
The file will be created in the current directory.
- «echo» is a command-line utility for displaying a line of text.
- «>» is the output redirection operator, which redirects the output of the «echo» command to the specified file.
All the above-mentioned methods are useful when you want to create a file with a specific size. You can choose the one that best suits your needs. Remember that you can always check the size of a file using the «ls -l» command, which will display the file size in bytes.
Conclusion
In conclusion, there are several ways to create a file with a specific size in Linux using different command-line utilities such as «dd», «fallocate», «truncate», «touch» and «echo». Each method has its own advantages and use cases, and you can choose the one that best fits your needs. Always remember to check the size of the created file using the «ls -l» command and also you can use other commands like echo to write some data to the file. As a new developer, it’s always a good idea to practice and experiment with different commands and utilities to gain a better understanding of how they work and when to use them.
How can I create a file with a specific size from a command line?
How can I create a file (e.g. using the touch command or another similar way), but with a specific size (e.g. 14 MB)? The use case is to test FTP with various file sizes. If we can create a file with a specific size, it will be useful to verify FTP transfer etc.
7 Answers 7
DESCRIPTION Shrink or extend the size of each FILE to the specified size [. ] -s, --size=SIZE set or adjust the file size by SIZE
Note: truncate may not be available on your system, e.g. on Mac OS X it’s not installed by default (but you can easily install it, using macports for example). In these cases you may need to use dd or head instead.
Very nice indeed. The touch is not needed though, you can simply run truncate -s 14M filename it will create filename directly.
This seems to create a sparse file, whereas the below answer (that uses «dd») doesn’t. I don’t know if the difference is important to you, but I figured I’d point it out
EDIT: The simplest way is probably the truncate of Ashutosh Vishwa Bandhu’s answer, but as pointed out by @offby1 that creates sparse files which may not be what you want. The solutions below create normal, full files.
The following commands create a 14MB file called foo :
- fallocate (thanks to @Breakthrough who suggested it in the comments and vote up Ahmed Masud’s answer below which also mentions it.)
This command is particularly impressive since it is as fast as truncate (instantaneous) irrespective of the desired file size (unlike the other solutions which will be slow for large files) and yet creates normal files, not sparse ones :
$ truncate -s 14MB foo1.txt $ fallocate -l 14000000 foo2.txt $ ls -ls foo?.txt 0 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:54 foo1.txt 13672 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:55 foo2.txt
dd if=/dev/urandom of=foo bs=14MB count=1
head -c 14MB /dev/urandom > foo
dd if=/dev/zero of=foo.txt bs=14MB count=1
In all of the above examples, the file will be 14*1000*1000 if you want 14*1024*1024 , replace MB with M . For example:
dd if=/dev/urandom of=foo bs=14M count=1 head -c 14M /dev/zero > foo
fallocate only deals in bytes, so you’d have to do (14*1024*1024=14680064)