How ToLinux

How to Create a File in Linux Terminal [7 Easy Ways]

Creating a file on Linux is a bit tricky but you can create it easily once knowing it.

If you’re an owner of a Windows computer or laptop, you will know that creating a file is like a piece of cake. However, things look different in the Linux environment. All the functions are accessible only through the command line. So, how to create a new file in Linux?

How to Create a File in Linux?

Here are the top 7 and easiest ways to create a file on Linux:

  • using touch command
  • using redirection operator
  • using cat command
  • using echo command
  • using printf command
  • using fallocate
  • using text editors

Note: Here, we have used techowns as a file name for the demo purpose. Replace techowns with the desired file name that you want to create. All the following commands have to be run in the terminal window. Launch it by pressing Ctrl+Alt+F2 or Ctrl+Alt+T.

Create File in Linux

Method 1: Create a file with touch command

Using touch command is the easiest option to create a new and empty file in the Linux machines. Open the terminal and run the following command

touch techowns.txt

It will create a new file named techowns.txt. You can preview it by entering

ls

To create a word file, run

touch techowns.docx

To create multiple files at once, enter the file names with a space between them.

touch techowns.docx techowns1.docx techowns2.docx

If you have created a file but named it wrongly. You can then rename the file on Linux.

Method 2: Create a File with the redirection operator

Redirection operator is yet another easiest way to create files on Linux. Here, you have to use the redirection operator > followed by the file name. It will redirect the output of a command to the new file.

> techowns.txt

To create a docx file,

> techowns.docx

This is the easiest command to create a file.

Method 3: Create a file with cat command

Generally, cat command is used to read and concatenate the files. Apart from that, you can also use this command to create a file on Linux.

To create an empty file named techowns, run

cat > techowns.txt

Press the Enter key and type the text that you want. After entering the ext, press CRTL+D to save the file.

Method 4: Create a file with the echo command

The echo command will print whatever the text you type on the command and put them into a file.

To create an empty file,

echo > techowns.txt

To create a file with the text,

echo "some text" > techowns.txt

Method 5: Create a file with printf command

Like the echo command, printf command will help you to create a file with the text line. With printf command, you can add two lines of text to the file.

To create a file with a single of text,

printf 'first line of text\n' techowns.txt

To create a file with two lines of text,

printf 'first line of text\n second line of text' techowns.txt

Method 6: Create a large file on Linux using fallocate command

fallocate command can be used to create large files on your Linux. To create a file with the size of 1GB:

fallocate -l 1G techowns.test

Note: On the above command, .test represents the file extension.

Method 7: Create files on Linux using Text editors

Instead of using the commands, you can also use text editors like vi, nano and vim. We will show you the steps for all the three editors.

Vi editor

Vi is one of the oldest editors created alongside Linux OS. You can use this editor to create and edit files. To create a new file

vi techowns.txt

Now, the editor will open. Press the ‘i‘ letter and add some few words. To save and exit, type ESC :X and then hit the Enter key.

Nano editor

To create a file using nano editor, type the following command

nano techowns.txt

The editor mode will open automatically. Type some word and press Ctrl+O to save the file. If you want to exit the editor, press Ctrl+X.

Vim editor

To create a file using vim editor, type

vim techowns.txt

Press the ‘i‘ letter to enter insert mode. Type some words and type Esc:wq Enter to save and exit the file.

Tips: If you are not able to find the location, you can use a set of command lines to find a file on Linux.

Now, you will know the seven different ways to create a file on Linux from the terminal. If you have questions, feel free to use the comments section. Also, follow our social media profiles Facebook and Twitter for more tutorials and latest news related to Linux.

Was this article helpful?
YesNo

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button