How to create a new file in Linux ?

Published: September 02, 2022

Updated: December 09, 2022

Tags: Linux;

DMCA.com Protection Status

Examples of how to create a new file in Linux:

Create a new file using touch

To create a new file in linux a straightforward solution is to use the command touch:

touch new_file.txt

touch accept options such as:

-c or –no-create: Not creating the file if it doesn’t exist.
-m: Changes the modification time.

Using linux > output operator

Another solution is to use the operator >:

linux_cmd > filename

for example:

ls > results.txt

will create a file called results.txt with the results of the ls command.

Using a text editor

Another solution by using a text editor such as vi or nano:

vi my_file.txt

it will open vi text editor. To enter some text just press touch "i" (as insert).
Then press the touch "Esc" and :wq to write and save the file.