How to create a file in Linux from terminal window?

LinuxBashFileCommand Line-Interface

Linux Problem Overview


What's the easiest way to create a file in Linux terminal?

Linux Solutions


Solution 1 - Linux

Depending on what you want the file to contain:

  • touch /path/to/file for an empty file

  • somecommand > /path/to/file for a file containing the output of some command.

       eg: grep --help > randomtext.txt
           echo "This is some text" > randomtext.txt
    
  • nano /path/to/file or vi /path/to/file (or 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

Solution 2 - Linux

Use touch

touch filename

Solution 3 - Linux

Create the file using cat

$ cat > myfile.txt

Now, just type whatever you want in the file:

Hello World!

CTRL-D to save and exit

Solution 4 - Linux

There are several possible solutions:

Create an empty file
touch file

>file

echo -n > file

printf '' > file

The 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.

Solution 5 - Linux

Also, create an empty file:

touch myfile.txt

Solution 6 - Linux

You can use touch command, as the others said:

touch filename

To write on file on command line, you can use echo or printf:

echo "Foo" > filename
printf "Foo" > filename

Maybe you can have problems with permissions. If you are getting the following error: bash: filename: Permission denied, you need to use sudo bash -c 'echo "Foo" > filename', as described here: https://askubuntu.com/questions/103643/cannot-echo-hello-x-txt-even-with-sudo

Solution 7 - Linux

How to create a text file on Linux:

  • Using touch to create a text file: $ touch NewFile.txt
  • Using cat to 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

Solution 8 - Linux

haha! it's easy! try this:

$ touch filename

Solution 9 - Linux

This will create an empty file with the current timestamp

touch filename

Solution 10 - Linux

1st method

echo -n > filename.txt

2nd method

> filename.txt

3rd method

touch filename.txt

To view the file contents

vi filename.txt

Solution 11 - Linux

touch filename

for permission denied error use sudo command as:

sudo touch filename

Solution 12 - Linux

Simple as that :

> filename

Solution 13 - Linux

You can use the touch command to create a new empty file.

http://linux.about.com/library/cmd/blcmdl_touch.htm

Solution 14 - Linux

I like the nano command-line editor (text):

nano filename

Solution 15 - Linux

In case you guys are trying to create a new file, but it says: 'File does not exist', it's simply because you are also accessing a directory, which does not exist yet. You have to create all non existent directories first, using the mkdir /path/to/dir command.

Solution 16 - Linux

To create a blank file with ownership and permissions using install.

sudo install -v -oUSER -gGROUP -m640 /dev/null newFile.txt

Solution 17 - Linux

One of the easiest way and quick

$ vim filename

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionraffianView Question on Stackoverflow
Solution 1 - LinuxEugen RieckView Answer on Stackoverflow
Solution 2 - LinuxMariusz JamroView Answer on Stackoverflow
Solution 3 - LinuxraffianView Answer on Stackoverflow
Solution 4 - LinuxsorpigalView Answer on Stackoverflow
Solution 5 - LinuxandreapierView Answer on Stackoverflow
Solution 6 - Linuxuser3753202View Answer on Stackoverflow
Solution 7 - LinuxDATA SCIENCEView Answer on Stackoverflow
Solution 8 - LinuxRoman NewazaView Answer on Stackoverflow
Solution 9 - LinuxChristy GeorgeView Answer on Stackoverflow
Solution 10 - Linuxshashwat guptaView Answer on Stackoverflow
Solution 11 - LinuxPradeep KumarView Answer on Stackoverflow
Solution 12 - LinuxIvanMView Answer on Stackoverflow
Solution 13 - LinuxAndy ArismendiView Answer on Stackoverflow
Solution 14 - LinuxSam PerryView Answer on Stackoverflow
Solution 15 - LinuxSebastianView Answer on Stackoverflow
Solution 16 - LinuxLinuxGuruView Answer on Stackoverflow
Solution 17 - Linuxuser8902801View Answer on Stackoverflow