+5 votes
252 views
How to create text file with command in Linux

in Linux / Unix by (551k points)
reopened | 252 views

1 Answer

+3 votes
Best answer

1. Create a file using the cat command in Linux
2. Create a text file using the Touch command in Linux
3. Create a text file using the redirect symbol in Linux
4. Create a text file with command results in Linux
5. How to rename files and directories in Linux

As we know, Linux distributions have been initially designed to be managed through a console or command line, so it is practical and necessary to know some ways to manage elementary tasks through it. Linux is a system that is basically based on the command line and many users prefer a terminal environment to a graphic one..

Thanks to the multiple possibilities that we find within the world of commands, we can manage practically any element of our equipment through the terminal, being the files and directories a fundamental part of them. Imagine creating an entire root of directories and files with the extension and name you want with two commands. Interesting, right? Well, this is what we are going to see next.

One of these tasks is to create text files with some kind of useful information and one of the best options to achieve this is through the cat command and TechnoWikis today will analyze how to achieve it in a simple way. In addition to creating a file as such, you can also see how to create one with information from a command or how to rename them and change the extension quickly..


1. Create a file using the cat command in Linux


This method gives us the possibility to create text files with the cat command and it is practical if we want to immediately add text to the new file to be created.
Step 1

For this, we will enter the following command in the terminal:
 cat> file.txt 
Step 2

After pressing Enter, it will not return to the indicator of the terminal of the selected distribution, but the cursor will be located on the next line and there we can start entering text directly into this file. image
Step 3

There we will enter the lines of text we want to enter, pressing Enter after each line. When finished, press the key combination Ctrl + D to exit the file and return to the created message.

+ D Ctrl + D

image
Step 4

To verify that the file was created, we can use the ls command to display a directory list for the current file:
 ls -l file.txt 
image
Step 5

There we will see details such as:
  • File creation date and time
  • Assigned permissions
Step 6

The cat command will also be useful to see the contents of the created file, just run the following command.
 cat file.txt 
image

2. Create a text file using the Touch command in Linux


Another of the options that Linux offers us to create text files is using the touch command, and one of the differences between using the touch command and the cat command is that when we use the cat command it will be possible to enter the text in the new file of immediately while with the touch command this will not be possible.

One of the advantages of the touch command is that it allows us to create multiple new files with a single command simultaneously.

Step 1

To create a new file using touch we will execute the following:
 touch file.txt 
image
Step 2

To confirm that our file has been created correctly, we will execute the following:
 ls -l file.txt 
image
Step 3

As we have indicated, the touch command gives us the opportunity to create multiple new files at once, for this it is enough to add all the additional file names (separated by spaces) as necessary:
 touch File.txt File.txt File.txt 
image
Step 4

To add content to this file, we can use some text editor like nano or vi:
 nano TechnoWikis.txt 
image

3. Create a text file using the redirect symbol in Linux


Another alternative to create a text file on Linux is using the standard redirection symbol which is usually used to redirect the output of a command to a new file.
Step 1

To create a new file we will execute the following:
 > file.txt 
image
Step 2

To see that our file has been created, we will execute the following:
 ls -l file.txt 
image

4. Create a text file with command results in Linux


As we are seeing the use of commands is also applicable to the creation and management of files in Linux environments. Being able to manage our information from the terminal allows us to have flexibility when choosing from where we want to perform the tasks and that is that from the same console we can perform many actions. A text file is a basic piece in the construction of our database and being able to create it with a command makes things much faster.

One of the options we have when creating a text file, is that one is created that contains information from a command directly, that is, the information it throws. With this method we can create a text file that saves error information, that does not display anything and until it shows it and also saves it. When we use commands that, for example, display the information in a directory listing, it is optimal to know that we can save all this information at once in a text file..


5. How to rename files and directories in Linux

Once we have created the files that we need we have been able to realize that we have created them with the names we did not want and if we are supporters of the command console, we will really be lazy to change to the graphic mode and make the changes. In addition we also have the possibility to make changes in batches, that is, with multiple files.

Before making such changes, you must ensure that you have the necessary permissions to make changes to the data structure. In the following tutorial you will see two useful options to be able to rename a Linux file or directory using only commands. In addition you can also change the extension of the files quickly using only commands, something that will allow you to really manage your information from the terminal.

Thus, we have seen various ways to create our text files on Linux in a practical way, with security and ease to enter all the information we consider necessary for such files.


by (3.5m points)
edited

Related questions

+5 votes
1 answer
asked Nov 15, 2019 in Linux / Unix by backtothefuture (551k points) | 620 views
+5 votes
1 answer
asked Oct 6, 2019 in Linux / Unix by backtothefuture (551k points) | 209 views
+4 votes
1 answer
asked Sep 29, 2019 in Linux / Unix by backtothefuture (551k points) | 316 views
+5 votes
1 answer
asked Nov 10, 2019 in Linux / Unix by backtothefuture (551k points) | 492 views
+4 votes
1 answer
asked Jun 22, 2019 in Linux / Unix by backtothefuture (551k points) | 230 views
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users