Unix Create a File Command

I‘m a new Unix user. How do I create a file from bash prompt without using GUI tools? How to create a file in Unix from terminal window? What’s the easiest and best way to create a file in Unix?

UNIX operating system provides many command line tools and text editors for creating a text file. You can use vi (emacs or joe), a terminal-based text editor for Unix, available under the GPL. It is designed to be easy to use. In short, you can use any one of the following tool:
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 5m
  1. Unix cat command
  2. echo or printf command
  3. vi text editor
  4. emacs text editor
  5. joe text editor
  6. Any other console based text editor

Method #1: Creating a file using echo command

Open the Terminal and then type the following command to create a file called demo.txt, enter:

echo 'The only winning move is not to play.' > demo.txt


printf 'The only winning move is not to play.\n' > demo.txt

OR create a file with two lines in demo-1.txt file:

printf 'The only winning move is not to play.\n Source: WarGames movie\n' > demo-1.txt

To examine the contents of the file you have just created is to type the following command at shell prompt:
cat demo.txt
cat demo-1.txt

Sample outputs:

Fig.01: Examine the contents of the file you’ve just created with cat command.

Method #2: Creating a file using cat command

In this example, create a file named quotes.txt and enter one line of text in it, type the following at the shell prompt:

cat > quotes.txt

You need to press the [Enter] or [Return] key. Finally type the text:

There are some who question the relevance of space activities in a developing nation. To us, there is no ambiguity of purpose. 

Again, press the [Enter] or [Return] key. To stop and save file entry by typing Control+d on a line by itself i.e Press and hold down the Control key and type d. On your screen you will see as follows:
cat > quotes.txt
There are some who question the relevance of space activities in a developing nation.
To us, there is no ambiguity of purpose.


Animated gif 01: Creating a file with cat command

To see the contents of the file you’ve just created, enter:
cat quotes.txt

Method #3: Create a text file using vi / vim text editor

vi / vim is a text editor. To create a file called text.txt, type:
$ vi bar.txt
Press i to insert new text. To save the file and leave vi, type ESC+:+x (press ESC key, type : followed by x and [enter] key).

How do I edit a file just created?

You need to use a text editor such as vi or emacs:
vi filename
Grab mastering the VI editor pdf file for more information on how to use a text editor (see below for link).

Further readings
  • Learn more about vi here : Download mastering the VI editor pdf version.
  • See man pages: ksh(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • deepak Mar 17, 2014 @ 10:23

    hey..very helpfull !!

  • ramesh Dec 25, 2014 @ 3:53

    i want to learn basic unix commands

  • Magnus Lien Jan 14, 2015 @ 12:37

    If you just want an empty file you can do : > filename

  • Chris Mar 25, 2015 @ 16:43

    You guys rock. This is a life saver. I’ve tacking a UNIX/Linux class on line and i feel lost. Just came across your website and in 10 minutes learned the first week of the online class.

    the tutorial on how to is what i think people really need. It’s one thing to show the commands but its great to see exactly how its typed. I always get confused on when and where spaces go.

    Now I’m 48 and been around computer for 20+ years but have been tainted when windows.


  • Hanisha Jan 15, 2016 @ 13:06

    Very helpful commands

  • rabia Feb 3, 2016 @ 5:19

    can u plz tell me how can i create a text file in unix?

    • Rock Mar 5, 2016 @ 19:36

      Just type in Putty:
      cat > filename.txt
      This is the easiest way. :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz