How to use the `cat` command to create a new file?

Published on Aug. 22, 2023, 12:18 p.m.

To create a new file using the cat command in Linux, you can use the output redirection operator (> ) to redirect the output of the cat command to a file instead of displaying it in the terminal. Here’s an example:

cat > example.txt

This will create a new file called example.txt in the current directory and open it for editing in the terminal. You can then type in the contents of the file manually, press CTRL + D when you are done to save the file and exit the editor.

Alternatively, you can use the echo command to add some text to the file while creating it, like this:

echo "This is some text" > example.txt

This will create a new file called example.txt with the text “This is some text” in it. If the file already exists, this will overwrite its contents with the new text.

Note that using cat to create a file in this way is not commonly used and is only recommended for creating small text files. For larger files or more complex formatting, it is recommended to use a text editor like nano or vim.

Tags:

cat