Creating a text file in Linux

In Unix, everything is a file. In this particular case though we will be demonstrating how to create a text file. Of course, these are some of the many ways of creating a file


Just touch it! – touch command followed by some file name and a file will magically appear.

$ ls linuxfreelancer
ls: cannot access 'linuxfreelancer': No such file or directory

$ touch linuxfreelancer

$ $ ls -l linuxfreelancer 
-rw-rw-r-- 1 daniel daniel 0 Feb  8 16:53 linuxfreelancer


vi or any text editor. In fact, any process which writes to a file.

$ vi linuxfreelancer

Save with “:wq” in vi to save the file.


Write multi-line text with “Here Document” syntax in bash.

$ cat <<EOF>linuxfreelancer
my blog is at

$ cat linuxfreelancer 
my blog is at


The echo command with some redirection –

$ echo 'My blog is at' > linuxfreelancer

$ cat linuxfreelancer 
My blog is at


You can redirect the output of any command to a new file

$ ps > ps.output
$ cat ps.output 
  PID TTY          TIME CMD
 2703 pts/0    00:00:00 bash
 3290 pts/0    00:00:00 ps


$ tee linuxfreelancer
Writing for my blog
Writing for my blog

$ cat linuxfreelancer 
Writing for my blog


Why does the tee command repeat what I typed? that is what it does – it reads from standard input and write to standard output and file at the same time.