How to create a file on the command line#

Sometimes you need to create a file on the command line. This can be done using the touch command which is available on most Unix-like operating systems. There are also other ways to create a file and some of them are listed below.

Using the touch command#
$ touch filename.txt
$ ls -l filename.txt
-rw-r--r-- 1 user user 0 Nov 12 12:00 filename.txt

Other ways to create a file are shown below and can be used to create a file with content, but creating a file with large and a specific size is not a real option.

Other ways to create a file#
$ echo "" > filename.txt
$ cat > filename.txt
^D
$ printf "" > filename.txt
$ ls -l filename.txt
-rw-r--r-- 1 user user 0 Nov 12 12:00 filename.txt

The touch command is the most common way to create a file on the command line. It is used to update the access and modification times of a file, but if the file does not exist, it will be created. This methods only create an empty file and other methods can be used to create a file with content. However in soms cases you may need a file with content for testing purposes.

Using the dd command#

The dd command can be used to create a file with a specific size. The following command will create a file with a size of 1MB.

Using the dd command#
$ dd if=/dev/zero of=filename.txt bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.001 s, 1.0 GB/s
$ ls -l filename.txt
-rw-r--r-- 1 user user 1048576 Nov 12 12:00 filename.txt

Using the dd command can be daunting for beginners, but it is a powerful tool that can be used to create a file with a specific size. The if option is used to specify the input file, the of option is used to specify the output file, the bs option is used to specify the block size and the count option is used to specify the number of blocks to copy.

Using the truncate command#

The truncate command can be used to create a file with a specific size. The following command will create a file with a size of 1MB.

Using the truncate command#
$ truncate -s 1M filename.txt
$ ls -l filename.txt
-rw-r--r-- 1 user user 1048576 Nov 12 12:00 filename.txt

With the truncate command, the -s option is used to specify the size of the file. The size can be specified in bytes, kilobytes, megabytes, gigabytes, etc. The following command will create a file with a size of 1MB. Many more things can be done with the truncate command like resizing an existing file, but for just creating a file it is less daunting than the dd command.

Using the fallocate command#

The fallocate command can be used to create a file with a specific size. The following command will create a file with a size of 1MB.

Using the fallocate command#
$ fallocate -l 1M filename.txt
$ ls -l filename.txt
-rw-r--r-- 1 user user 1048576 Nov 12 12:00 filename.txt

The fallocate command is similar to the truncate command, but it is less common. The -l option is used to specify the size of the file. The size can be specified in bytes, kilobytes, megabytes, gigabytes, etc. The fallocate command is less common than the truncate command, but it is a powerful tool that can be used to create a file with a specific size and utilizes more of the underlying file system and Linux specific features.

Closing remarks about creating files#

There are many ways to create a file on the command line. The touch command is the most common way to create an empty file, but other methods can be used to create a file with content. The dd, truncate and fallocate commands can be used to create a file with a specific size or to resize an existing file.

The dd command is the most powerful tool to create a file with a specific size, but it is also the most daunting for beginners. It allows you to specify the input so you can create a file with specific content like random data or zeros. The other commands don’t allow you to specify the content of the file.

With this knowledge you can create files on the command line and use them for testing purposes like in a pipeline or to create files with a specific size for debugging purposes.