Table of Contents

7 ways to create a file in Linux

In Linux, there are a few ways to create a file. In this blog post, we will discuss 8 of the most common methods. Each method has its own advantages and disadvantages, so it is important to understand them all before choosing which one to use. Let’s get started!

create a file with touch command in Linux

To create a file in Linux, we can use the “touch” command. This command is used to create a new, empty file. The touch command is very simple to use and only requires a single argument: the name of the file you want to create. For example, if we wanted to create a new file called “test.txt”, we would use the following command: touch test.txt

The touch command is also used to change the timestamps on existing files. Every file in Linux is associated with timestamps, which specifies the last access time, last modification time and last change time.

Whenever we create a new file, or modify an existing file or its attributes, these timestamps will be updated automatically. Touch command can used to change these timestamps (access time, modification time, and change time of a file).

create a file with cat command in Linux

The second method is to use the “cat” command. The cat command can be used for a lot of things, but one of its most common uses is to create a new file. To do this, you simply need to pass the cat command the name of the file you want to create, and then provide the contents of the file on stdin (standard input). For example, if we wanted to create a new file called “test.txt” , we would use the following command: cat > test.txt

This would create a new file called “test.txt”. We can then add the contents of the file on stdin. To do this, we simply type the contents of the file and press CTRL+D when we are finished.

create a file with cp command in Linux

The third method is to use the “cp” command. The cp command is used to copy files and directories from one place to another. It can also be used to create a new file by copying an existing file.

To do this, you simply need to pass the cp command the name of the existing file and the name of the new file you want to create. For example, if we wanted to copy the file “test.txt” to a new file called “new_test.txt”, we would use the following command: cp test.txt new_test.txt

create a file with mv command in Linux

The fourth method is to use the “mv” command. The mv command is used to move files and directories from one place to another. It can also be used to rename files. To do this, you simply need to pass the mv command the name of the existing file and the name of the new file you want to create.

For example, if we wanted to rename the file “test.txt” to “new_test.txt”, we would use the following command: mv test.txt new_test.txt

create a file with truncate command in Linux

To create a specific size file, for example 5 MB, we can use truncate command, run: $ truncate -s 5M howtouselinux.txt

The above command will create a file called howtouselinux.txt with size exactly 5MB. For more details about this command, refer truncate man pages. $ man truncate

create a file with head command in Linux

we can use head command to create a file of certain size too.To create a file with 5 MB in size using head command, run: $ head -c 5MB /dev/urandom > howtouselinux.txt

The above command will create 5MB size file filled with random data. You can also create the file with 0s as shown below. $ head -c 5MB /dev/zero > howtouselinux.txt

Refer man pages for further details about head command. $ man head

create a file with dd command in Linux

We already knew we can convert and copy a file using dd command. We also use dd command to create a bootable disk. However, we can use this command to create files of certain size as well.

To create a file with size 5MB, run: $ dd if=/dev/urandom of=howtouselinux.txt bs=5MB count=1

Sample output:
1+0 records in
1+0 records out
5000000 bytes (5.0 MB, 4.8 MiB) copied, 0.0402477 s, 124 MB/s

The command will create howtouselinux.txt file of size 5MB filed with some random data.

You might also like