Do you need an ultra fast way to create a large number of files in Linux? Perhaps, you are doing some load testing for an application and you need to create 1000 or even 1,000,000 files in the matter of seconds. Well, here is how to do it!
There are two parts to creating these files. First, is creating a single master file that contains the data that the thousands/millions files are based on. Second, splitting this master file into the total number of files that you require.
Table of Contents
Linux File Creation – 10,000 Files
Create the master file
- Determine the number of files and the size of each file that you require
- Multiply the total number of files times the size (in bytes). For example: If you want to create 10000 files that are 10 bytes each, do 10000 * 10 = 1,000,000. This represents the size of the master file that is needed.
- To create this master file, run the command:
dd if=/dev/zero of=masterfile bs=1 count=1000000
The above command will create a 1 MB file called ‘masterfile’. This file contains all 0’s. If you would like it to contain random binary data, use /dev/urandom
Split the master file into thousands of pieces
- Now that the master file is created, you can now use this to generate the desired 10,000 files that are 10 bytes each.
- Run the command:
split -b 10 -a 10 masterfile
The -b option specifies the size in bytes of each file. The -a option defines the length of the filename of the new files +1 (-a 10 means create a 11 character long filename)
5 Ways to Create a File in Linux
1. Creating a File using the touch
Command
This is the simplest way to create a new file.
Command:
touch newfile.txt
2. Creating a File using the echo
Command
The echo
command can be used to create a file and write data to it.
Command:
echo "This is some text" > newfile.txt
3. Creating a File using the cat
Command
The cat
command allows you to create a new file and enter the content in one go.
Command:
cat > newfile.txt
4. Creating a File using the printf
Command
Similar to echo
, the printf
command can also be used to create a file.
Command:
printf "This is some text" > newfile.txt
5. Creating a File using a Text Editor
You can use a text editor like nano
, vi
, or emacs
to create a file.
Command:
nano newfile.txt
interesting!
but
10000 * 10 != 1,000,000
there is something wrong in your dd
it should be
bs=1000000 count=1
not the other way around.
Thanks this is awesome thanks!
Thanks very much!
Useful!
Thanks a lot. It really helped.
Just a little correction i.e. (100,000 * 10 = 1,000,000 ) instead of (10,000*10 = 1,000,000)