How to create a file in Linux from terminal window?
What's the easiest way to create a file in Linux terminal?
Depending on what you want the file to contain:
touch /path/to/filefor an empty file
somecommand > /path/to/filefor a file containing the output of some command.
eg: grep --help > randomtext.txt echo "This is some text" > randomtext.txt
any other editor emacs,gedit etc)
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn't exist
Read more... Read less...
Create the file using
$ cat > myfile.txt
Now, just type whatever you want in the file:
CTRL-D to save and exit
There are several possible solutions:
Create an empty file
touch file >file echo -n > file printf '' > file
echo version will work only if your version of
echo supports the
-n switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.
Create a file containing a newline and nothing else
echo '' > file printf '\n' > file
This is a valid "text file" because it ends in a newline.
Write text into a file
"$EDITOR" file echo 'text' > file cat > file <<END \ text END printf 'text\n' > file
These are equivalent. The
$EDITOR command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The
cat version presumes a literal newline after the
\ and after each other line. Other than that these will all work in a POSIX shell.
Of course there are many other methods of writing and creating files, too.
Also, create an empty file:
How to create a text file on Linux:
touchto create a text file:
$ touch NewFile.txt
catto create a new file:
$ cat NewFile.txt
The file is created, but it's empty and still waiting for the input from the user. You can type any text into the terminal, and once done CTRL-D will close it, or CTRL-C will escape you out.
- Simply using
>to create a text file:
$ > NewFile.txt
- Lastly, we can use any text editor name and then create the file, such as:
nano MyNewFile vi MyNewFile NameOfTheEditor NewFileName