That's what echo does:
echo "Some text here." > myfile.txt
Answer from Carl Norum on Stack OverflowVideos
cat's primary purpose is to concatenate files. cat file1 file2 ... will show the contents of file, file2 and the others one after the other, as if the contents were in a single file. See the manpage:
NAME
cat - concatenate and print files
It is meant for usage where either:
a target command cannot read from files and you need to pass multiple files to it. An example is the
trutility. Ordinarily, with one file, you'd do:tr < fileBut with multiple files, redirection can't be used, so you have to do:
cat file1 file2 ... | tr- a target command can read from multiple files, but its behaviour may change when it's given multiple files. An example is
wc, which prints the counts for each file, along with the filenames, where you might have wanted just the total, without a filename.
Remember that most commands you encounter (grep, sed, awk, sort, ...) can read files perfectly fine.
If you want to view the contents of a file, use a pager - less and more are both eminently capable of presenting files for viewing, and are far more convenient to use.
cat is one of the most frequently used commands on Unix-like operating systems. It has three related functions with regard to text files:
- displaying them
- combining copies of them
- creating new ones.
- Copy files
cat's general syntax is:
cat [options] [filenames] [-] [filenames]
Reading Files
The most common use of cat is to read the contents of files, and cat is often the most convenient program for this purpose. All that is necessary to open a text file for viewing on the display monitor is to type the word cat followed by a space and the name of the file and then press the ENTER key. For example, the following will display the contents of a file named file1:
cat file1
Concatenation
The second role of cat is concatenation. (This is the source of cat's curious name.) There is no effect on the original files.
For example, the following command will concatenate copies of the contents of the three files file1, file2 and file3:
cat file1 file2 file3
The contents of each file will be displayed on the monitor screen. This output could just as easily be redirected using the output redirection operator to another file, such as file4, using the following:
cat file1 file2 file3 > file4
File Creation
Thanks to @muru comment : cat is capable of create new files depending on the shell redirection feature and not itself
For small files this is often easier than using vi, gedit or other text editors. It is accomplished by typing cat followed by the output redirection operator and the name of the file to be created, then pressing ENTER and finally simultaneously pressing the Ctrl & d keys.
For example, a new file named file1 can be created by typing
cat > file1
then press ENTER and simultaneously press the Ctrl & d keys.
PS1: If a file named file1 already exists, it will be overwritten
PS2: you can append to exited file using append operator >> example cat >> file1
Copy Files
The cat command can also be used (depending on shell redirection feature) to create a new file and transfer to it the data from an existing file. Example: make a copy of file oldfile.txt:
cat oldfile.txt > newfile.txt
References:
- Linux and Unix cat command
- The cat Command
- cat (Unix)
- HowTo: Use cat Command In Linux / UNIX
Yes, it's expected.
We say that Ctrl-D makes cat see "end of file" in the input, and it then stops reading and exits, but that's not really true. Since that's on the terminal, there's no actual "end", and in fact it's not really "end of file" that's ever detected, but any read() of zero bytes.
Usually, the read() system call doesn't return zero bytes except when it's known there's no more available, like at the end of a file. When reading from a network socket where there's no data available, it's expected that new data will arrive at some point, so instead of that zero-byte read, the system call will either block and wait for some data to arrive, or return an error saying that it would block. If the connection was shut down, then it would return zero bytes, though.
Then again, even on a file, reading at (or past) the end is not an interminably final end as another process could write something to the file to make it longer, after which a new attempt to read would return more data. (That's what a simple implementation of tail -f would do.)
For a lot of use-cases treating "zero bytes read" as "end of file detected" happens to work well enough that they're considered effectively the same thing in practice.
What the Ctrl-D does here, is to tell the terminal driver to pass along everything it was given this far, even if it's not a full line yet. At the start of a line, that's all of zero bytes, which is detected as an EOF. But after the letter b, the first Ctrl-D sends the b, and then the next one sends the zero bytes entered after the b, and that now gets detected as the EOF.
You can also see what happens if you just run cat without a redirection. It'll look something like this, the parts in italics are what I typed:
$ cat fooCtrl-Dfoo
When Ctrl-D is pressed, cat gets the input foo, prints it back and continues waiting for input. The line will look like foofoo, and there's no newline after that, so the cursor stays there at the end.
A few words about.
As far as I can tell from a quick glance at the source code, cat uses a buffer to optimize its workflow.
Referring to the behavior of the simple cat invocation, from standard input and without special command line options:
- when the buffer is empty a single Ctrl+D it is enough to exit;
- when not, the first Ctrl-D forces the buffer dump (which becomes empty), and the second is interpreted as the command to quit.
This means that if you run your cat> test and simply enter Ctrl-D you will create an empty file (named test) in the current directory, or you will empty the file if it already exists, without the need for a second Ctrl-D .
Beyond the scope of this question, but not so remote: it should be noted that if you are sending characters to a terminal/shell instance an unnecessary second Ctrl-D could cause an unwanted exit (mainly connected to the example above).