How To Create File In Linux?

Files are used for different purposes in Linux. The purpose can be storing data or setting some configuration or creating code or script for execution. There are different ways to create a file in Linux. In this tutorial, we will learn different ways to create files via command line or GUI for Linux distributions.

List Files

Before learning how to create a file we should learn how to list files after creating them. Even there are different ways to list files the most popular and easy way is using the ls command to list files and folders.

ls

Create File with touch Command

The touch command is the most popular and defacto command used to create files. The touch command creates an empty file with the specified name. If the specified file does not exist an empty file is created. If a file exists the exiting file modification timestamps are updated. In the following example, we create a file named “myfile.txt”.

touch myfile.txt

Alternatively we can create multiple files with a single touch command. All file names are specified after the touch command.

touch myfile.txt yourfile.txt allfile.txt

Create File with Redirection Operator

The Linux bash provides the redirection operator in order to redirect some output into the specified device or file or command. If the specified redirection is a file the file is created if it does not exist. The redirection operator can be also used to create a new file. We redirect nothing to the file and create an empty file with the redirection operator.

> myfile.txt

Create File with cat Command

The cat command is used to print content of the specified file to the standard output or into a file. This means the cat command can be used to create a file by putting empty content into a file which is not exist. In the following example we create a file named “myfile.txt”. This cat command starts the input mode and this input mode can be stopped and content is saved with the CTRL+D command like below.

cat > myfile.txt

Create File with echo Command

The echo command prints provided string argument into the standard output or into a file. So we can use the echo command in order to create a file. In the following example we create an empty file named “myfile.txt”.

echo "" > myfile.txt

Alternatively we can create the new file with some content. In the following example we set the new file content.

echo "I am a content" > myfile.txt

Create File with dd Command

The dd command is a command used to read and write some data into files and devices. Even the dd command is create in order to create backups or mirror data. In the following example we create a file named myfile.txt without a content.

dd if=/dev/zero of=mytext.txt bs=1 count=0 

Create File with fallocate Command

The fallocate command is used to manipulate the allocated disk space for a file. If the specified file is not created it is created automatically. In order to create a new file with the fallocate the file size should be specified. In the following example we create a file with the 1 byte in size.

fallocate -l 1 myfile.txt

Create File with vim Command

The vi or vim is very popular command line based text editor. The vi/vim can be used to create a file. Just type the following command where the file name is “myfile.txt”.

vim myfile.txt

Now a file is create and opened we should save and closed the file with the “:wq” like below.

See also  Bash Shell Script Comment

Leave a Comment