How to create a new file in Linux ?

Created September 02, 2022 | Viewed 354 | by Benjamin Edit


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.

References