In this article I will show you 7 different methods to create a new file in Linux using terminal.

1. Using touch command

The touch command is used for changing the timestamp of a file. But it can also be used to create a new empty file using the following command:

touch yourfilename

touch.png

If yourfilename already exist in your current directory, touch will update its timestamp, but if the file doesn’t exist, it will create a new empty file.

2. Using cat command

cat is usually used to display the contents of one or more files in the terminal.

But cat can also be used to create a new file using the following command:

cat > filename

cat-file.png

After typing the command and pressing enter, you can type the contents of the file if you want to, after typing you can use Ctrl + D to save it.

If filename doesn’t exist in the current working directory, it will create that file. If the filename exists, it will override that file with the new content which you have typed

Alternatively, you can use >> instead of > to append the text to the end of the already existing file.

You can use the command cat filename to display the contents of the file in the terminal.

3. Using the redirect operator

Redirection operator (>) is used to change the destination of the results displayed on the terminal to another place

You can use the redirect operator followed by the filename to create a file

> filename.txt

After typing the command, you can type the contents of the file (optional) and press Ctrl + D to save.

redirect-operator.png

4. Using echo command

The echo command will output back to you whatever you give as input to it.

echo command along with the redirection operator can be used to create a file with the content you specify.

echo 'Text inside file' > file.txt

echo.png

You can use \n inside echo command to create a new line inside the file. Example:

echo 'First line\nSecondline' > file.txt

5. Using printf command

The printf command works like the echo command.

You can use the following command to create a new file using prinf

printf 'Hello world!' > file.txt

printf.png

6. Using nano text editor

nano.gif

If your Linux system has nano installed, you can use it to create or edit files.

You can use the following command to create a new file using nano: nano file_name

If file_name doesn’t exist in the current directory, it will create that file and opens the editor in terminal. If the file_name already exists, it will open that file in the editor to edit

After typing your text inside the file, you can use Ctrl + X and then type Y (to save), and press enter to exit the editor and go back to terminal.

7. Using vim text editor

vim.gif

You can also use the vim text editor to create a file and also for writing to the file

Typing the command vim filename will create a new file if the file doesn’t exist and then opens the editor

You can press i to go into insert mode and write to the file. After editing the file, you can press esc key and type :wq to save and quit.

🌐 Connect with me: tenocijam.in