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
This is the simplest way to create a new file.
2. Creating a File using the
echo command can be used to create a file and write data to it.
echo "This is some text" > newfile.txt
3. Creating a File using the
cat command allows you to create a new file and enter the content in one go.
cat > newfile.txt
4. Creating a File using the
printf command can also be used to create a file.
printf "This is some text" > newfile.txt
5. Creating a File using a Text Editor
You can use a text editor like
emacs to create a file.