How to quickly create an empty and non-empty text file in Linux via terminal?

Question:

How to quickly create an empty and non-empty text file in Linux via terminal?

Answer:

The answer is general, you can correct it if you see fit.

There are N number of ways to create text files, we will give two main ones, which, in our opinion, are the fastest in terms of keyboard input, and several others.

The phrase "while typing" means that you need to enter a command into the terminal and press the Enter key.


Creating empty files

> a

When you enter this command, an empty file named a will be created in the current directory.

You can enter without a space:

>a

A more readable option, it helps not to make a mistake and not accidentally write the result of the previous command to a file:

: > file

(NOP> file).

You can create as many files as you want at once:

> b > c > d > e > f > g

Although this is more convenient ( touch will be touch later):

touch b c d e f g

Creating files with text

echo blablabla > h

When you enter this command, a file named h will be created in the current directory containing the text blablabla and one line feed .

You can enter without a space around the > operator:

echo blablabla>i

The output context may or may not be quoted, 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 filenames, of course).


You can also do the following:

echo 123 > m > n > o

When you enter this command, two empty files will be created in the current directory: m and n ; and file o containing text 123 and one line feed .


In other words, the result of all commands that output something can be stuffed into a file …

man man > p

Man on man … When you enter this command, a file named p will be created in the current directory 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

Entering this command will create an empty file named q in the current directory.

To be precise, touch is a command, the main purpose of which is to change the time of the last modification or last access of the file, if the file does not exist, then it creates it. Quote .

Create a "text" file with cat

cat > r

When you enter this command, an empty file named r will be created in the current directory and the terminal will switch to the mode of concatenating input lines to the end of the contents of this file. That is, we can immediately start filling the file with text. The typed text will be saved line by line by pressing the Enter key. In other words, by pressing the Enter key, the concatenation will be performed.

You can enter without a space:

cat>s

Example

  • Enter cat>s – an empty file named s created in the current directory.
  • We type 123 – this text will not be in the file yet.
  • Press Enter – the text 123 written to the file and the cursor, both in the terminal and in the file, moved to a new line.

You cannot go back one line above.

To exit concatenation mode, Ctrl+D (EOF – End Of File) at the beginning of the 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. This is how you can write strings in chunks. To exit with an unfinished line, you can press Ctrl+D twice, then the last line in the file will not have an end of line character (EOL – End Of Line).


Creating a file using an editor.

Obviously we can use an editor like nano , vi , vim , etc to create the file.

Example

  1. Enter nano t – the nano editor opens in terminal mode.
  2. Enter 123 and press Ctrl + O (not zero, but a letter), and then Enter – a file named t created in the current directory, containing the text 123 and one line feed .
  3. To exit and the editor, press Ctrl + X (at the bottom of the tooltip editor).

Creating a data file via dd

It is sometimes useful to create a file of a certain size with zeros

dd if=/dev/zero of=./file bs=10M count=100

or random bytes

dd if=/dev/urandom of=./file bs=10M count=100

A file is created from 100 blocks of 10 megabytes – 1 GB.

Allocating space for a file using the file system

Such commands work faster than dd because the data itself is not written, but simply a disk area is allocated

fallocate -l 10M ./file

or

truncate -s 10M ./file

fallocate reserves space for the file on disk, and truncate truncates the file or appends to the desired size, reserving disk space.

When files are created in this way, they may contain chunks of deleted files on some systems.

Scroll to Top