Through the method of creating Text files via the Terminal on Linux, you can generate any text file with a .txt extension similar to that on Windows operating systems. However, on Linux, you'll need to utilize command lines on the Terminal to create this file.
Creating Text Files via Terminal on Linux
Important Note: Throughout the article, commands are enclosed in double quotes; however, when entering commands for execution, remember to omit the quotes.
Refer to the guide on accessing Terminal in Linux here
The initial method to create a text file is by using the 'cat' command. Enter the command below into the Terminal window and press Enter. Remember to replace 'sample.txt' with the desired file name.
cat > sample.txt
After pressing Enter, the cursor will move to the next line, allowing you to input content into the text file. Enter your text there and press Enter after each segment. Once finished, press Ctrl + D
To confirm the creation of the file, enter the command below into the Terminal window and press Enter:
ls -l sample.txt
Note:
The command starts with the letter l in lowercase, and the option (-l) is also in lowercase l.
At this point, you will see a list of files displayed on the screen as shown below:
Additionally, the 'cat' command is used to view the contents of files. To do this, enter the command below into the Terminal window and press Enter:
cat sample.txt
The contents of the files will be printed on the screen, and you will be brought back to the Terminal window.
Furthermore, to create an empty text file, you use the right arrow (>). Enter (>), then input the file name and press Enter as shown below:
Note that you will be returned to the Terminal window without any indication that the file has been created. To check if the file exists or not, use the 'ls' command mentioned by Mytour above.
When using the 'cat' command to view the contents of the file, you will see that it is an empty file, containing no content whatsoever.
To add text to the file, you can use Vi. Enter the command below into the Terminal window to open the text file in Vi:
vi sample.txt
Vi opens in command mode. To input text into the text editor, type 'i' to enter insert mode. Enter your text and then press Esc to return to command mode. To close Vi and save the file, type ':wq' and press Enter.
To view the contents of the file, enter the command below into the Terminal window and press Enter:
cat sample.txt
Similar to the right arrow (>), use the 'touch' command to create an empty text file. Enter 'touch', followed by the file name as shown below:
The 'touch' command creates an empty text file. Use Vi or another text editor to add text to the file. This concludes the process of creating a text file via Terminal on Linux successfully.
Here, Mytour has guided you on how to create a Text file using Terminal on Linux. The commands and steps are not too difficult. Additionally, you can explore 3 best code editors on Linux and Mac if you're looking for suitable code editors for both Linux and Mac. Wishing you success.