How to create a new file in linux?

To create a new file in Linux, you can use several text editors or command-line utilities. I’ll provide you with examples using both methods:

Using Command-Line Text Editors:

1. touch <newfilename.txt>

2. vi <newfilename.txt>

3. vim <newfilename.txt>

4. nano <newfilename.txt>

5. cat > <newfilename.txt>

Press Ctrl + D to save the file when creating a new file using cat command.

Note:
In some scenarios, based on the user’s umask value, the file permissions will set while creating a new file. The default umask value for user is 0002 and for root user is 0022.

Below is the example when a new file is created by user1 using touch command.

Below is the example when a new file is created by the root user using touch command.

Following is the example when a new file is created by user1 using cat command.

When a new file is created by root user using cat command, the file owner and files’s group owner will be root.

You can change the file’s last modified time and last accessed time using touch command.

touch –t 202307281200 file2

2023 – Year

07 – Month

28 – Day

12:00 – Time in 24 hrs format.

To change only last modified time of the file only, use option –m with touch command.

To change only last accessed time of the file only, use option –a with touch command.

Using GUI Text Editors:

Using Gedit: If you are using a GUI-based desktop environment, you can open the text editor Gedit and create a new file by going to the terminal and typing:

gedit filename.txt

This opens the Gedit text editor with a new file named filename.txt, where you can edit and save it through the graphical interface.

Remember to replace filename.txt or newfilename.txt with the desired name of your new file. You can choose any text editor that suits your preference and is available on your Red Hat Linux system.

Click Here!! to learn more about editing created files using VIM Editor.

How do you feel about this post? Drop your comments below..