How to Create a File Using the Terminal in Linux

Suraj Joshi Feb 02, 2024
  1. Create a File With touch Command
  2. Create a File With the Redirection Operator
  3. Create a File With the cat Command
  4. Create a File With the echo Command
  5. Create a File With Heredoc
  6. Create Large Files With the dd Command
  7. Create Large Files With the fallocate Command
How to Create a File Using the Terminal in Linux

We can create files with touch, echo, cat, dd and fallocate commands using Linux Terminal. Similarly, we can also create files from the terminal using the Redirection Operator and the Heredoc.

Create a File With touch Command

The touch command is the standard UNIX/Linux command to create, change, and modify a file’s timestamps. It can only create empty files. If the file already exists, the command will change the timestamps of the file.

To create a new file using the touch command, simply use the touch command followed by the file’s name to be created.

touch main.py

It will simply create an empty file main.py in the current working directory.

To create multiple files, use the touch command followed by the files’ names to be created separated by space.

touch main.py abc.py xyz.txt

It will create three empty files - main.py, abc.py, and xyz.txt in the current working directory. If any of the files already exists, it will just update the timestamp of the file.

Create a File With the Redirection Operator

We can also create new files using the redirection operator. Redirection operation allows us to save the output of a command to a file. The > operator will overwrite the existing file with the output while >> will append output to the file.

To create an empty file, specify the file’s name to be created after the redirection operator.

> abc.txt

It will create an empty file named abc.txt in the current working directory.

Create a File With the cat Command

The cat command primarily used to create and concatenate files can also create new files. We can also add content to the files while creating the files with the cat command.

To create files with the cat command, use cat followed by the redirection operator > and then the file’s name. Then hit Enter and add contents to the file. After adding the contents, use CTRL+D to save the file.

cat > main.py

It creates main.py file, and we can add content to the files after creating it and then save with CTRL+D.

Create a File With the echo Command

echo command is used to print the text passed as an argument to the command.

The arguments passed to the echo command are saved as the newly created file’s content using redirection operator >.

echo "print('Hello World!!')" > main.py

It creates the file main.py in the current working directory with content print('Hello World!!').

To create an empty file using the echo command, use redirection operator > with echo and then specify the file’s name to be created.

echo>main.py

It creates an empty file main.py in the current working directory.

Create a File With Heredoc

Heredoc is a special class of redirection that allows us to add multiple content lines in the file.

<< eof > test.txt
Line-1
Line-2
eof

It will create a new file test.txt in the current working directory.

Create Large Files With the dd Command

The primary purpose of the dd command is to convert and copy files.

To create files with the dd command, use the command in the terminal.

dd if=/dev/zero of=large.test seek=3G

It creates a file large.test with the size of 3GB.

Create Large Files With the fallocate Command

The primary purpose of the dd command is to allocate real disk space for files.

To create files with the fallocate command, use the command in the terminal.

fallocate -l 3G large.test

It creates a file large.test with the size of 3GB.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux File