How to quickly create an empty and non-empty text file in Linux via a terminal?
1 answer
The general answer is, you can correct it if you see fit.
There is an N-th number of ways to create text files, we give two main ones, which, in our opinion, are the fastest from the point of view of keyboard input, and several others.
The phrase “when entering” means that you need to enter a command into the terminal and press the
Enterkey.
Creating empty files
> a When you enter this command, an empty file will be created in the current directory with the name a .
You can enter without a space:
>a You can create as many files as you want:
> b > c > d > e > f > g Although it is more convenient (about touch will be further):
touch bcdefg Creating text files
echo blablabla > h When you enter this command, a file in the current directory will be created with the name h , containing the text blablabla and one newline .
You can enter without a space around the operator > :
echo blablabla>i The displayed context can be enclosed in quotation marks, but you can also not enclose it, even if it contains spaces:
echo bla bla bla > j echo 'bla bla bla' > k echo "bla bla bla" > l All three of the above commands give the same result (except for file names, of course).
You can also crank such a thing:
echo 123 > m > n > o When you enter this command, two empty files will be created in the current directory: m and n ; and an o file containing the text 123 and one line feed .
In other words, the result of all the commands that output something can be pushed into a file ...
man man > p Man by man 'u ... When you enter this command in the current directory, a file will be created named p containing the manual for the man command.
cal 2000 > 2000 Calendar for the year 2000 ...
Other ways to create files
Creating an empty file with touch
touch q When you enter this command in the current directory, an empty file will be created with the name q .
To be precise, touch is a command whose main purpose is to change the time of the last change or last file access, if the file does not exist, then it creates it. Quote .
Creating a text file with cat
cat > r When you enter this command in the current directory, an empty file will be created with the name r and the terminal will switch to the concatenation mode of the input strings by the end of the contents of this file. That is, we can immediately begin to fill the file with text. Saving the typed text will be done line by line by pressing the Enter key. In other words, by pressing the Enter key, concatenation will be performed.
You can enter without a space:
cat>s Example
- Enter
cat>s- an empty file is created in the current directory with the names. - We type
123- this text will not be in the file yet. - Press
Enter- the text123entered the file and the cursor, both in the terminal and in the file, moved to a new line.
It is impossible to return to the line above.
You can exit concatenation mode by using Ctrl+D (EOF - End Of File) at the beginning of a line. If you have already started typing a line, Ctrl+D will not finish entering the file, but will write the typed part of the line without the end of line character. So you can write strings in parts. To exit with an incomplete line, you can press Ctrl+D twice, then the last line in the file will not have a line end character (EOL - End Of Line).
Creating a file using the editor.
Obviously, we can use an editor like nano , vi , vim , etc to create a file.
Example
- Enter
nano t- opens thenanoeditor in terminal mode. - Enter
123and pressCtrl + O(not a zero, but a letter), and thenEnter- in the current directory created a file namedt, containing the text123and one line break . - To exit the editor and press
Ctrl + X(at the bottom of the tooltip editor).