How To Create Files in Linux From a Bash Shell Prompt

I‘m a new to CentOS Linux user. How do I create a file from bash prompt without using GUI tools?

Linux / UNIX like operating system offers many command line tools and text editors for creating text files. You can use vi or joe text editor. It is a terminal-based text editor for Linux/Unix like systems, 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. cat command
  2. echo or printf command
  3. Nano text editor
  4. vi text editor
  5. joe text editor
  6. Any other console based text editor

Create a Text File using cat command

To create a text file called foo.txt, enter:
$ cat > foo.txt
Output:

This is a test.
Hello world!
press CTRL+D to save file

To display file contents, type:
$ cat foo.txt

Create a text file using echo or printf command

To create a file called foo.txt, enter:

echo 'This is a test' > foo.txt

OR

printf 'This is a test\n' > foo.txt

OR

printf 'This is a test\n Another line' > foo.txt

To display file contents, type:
$ cat foo.txt

Create a Text File using nano text editor

Nano is a text editor from GNU project for Linux or Unix-like system. To create a file named foo.txt, run:
$ nano foo.txt
Sample outputs:

GNU nano is a simple terminal-based text editor for Linux/Unix

You can start typing on screen. To quit or exit nano, press the Ctrl-X key combination and select Yes followed by the enter key.

Create a Text File using joe text editor

JOE is text editor. To create a file called foo.txt, type:
$ joe -help foo.txt
You will see help menu on screen. Next type something. To save the file and leave joe, by typing ^KX (press CTRL+K+X).

Create a Text File using vi / vim text editor

vi / vim is another text editor. To create a file called bar.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).

Further readings
  • Learn more about joe here : Introduction to the Joe Text Editor
  • Learn more about vi here : Download mastering the VI editor pdf version.
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
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
44 comments… add one
  • Suresh Aug 11, 2010 @ 16:40

    What is the main difference between touch & vi commnd?

  • vr4sk Dec 7, 2010 @ 9:58

    Could someone help me understand what this means?

    Save the information that you have read from the keyboard into a file

  • superking Feb 23, 2011 @ 8:37

    good job !!!

  • vijay Mar 21, 2011 @ 6:33

    how to search the empty file in linux …
    For that which command use ???
    please tell me …. help me …..

  • Ryan Jul 26, 2011 @ 18:39

    Perhaps a subliminal mistake, looks like you used ‘cat foot.txt’ instead of ‘cat foo.txt’ in your example.

  • siddharth Dec 5, 2011 @ 16:54

    hi guyz,

    mu /dev/hdc had 2.7 G and its now 100%. Need to clear it.

    Please help.

  • Phearret Jun 10, 2012 @ 0:41

    I am trying to maintain a list of “ROMS” be it NDS, or MAME, no matter.
    Instead of accessing each page on a website, is it possible to use cat to look for say; title” … ” within .aspx on the website and copy it to local txt file ? I have been using ..
    cat wordlist/* | tr -cs A-Za-z ’12’ | tr A-Z a-z | sort | uniq > wordlist/masterlist.txt to merge numerous txt files into one, have them sort, and no repeats. something like this to be able to input the website start & end page and where to save the roms list.. there doesn’t really seem to be any 1 site that has a complete & accurate list. I an not trying to circumvent anything to get to the roms, I merely wish to be able to goto a webpage, enter in the variables, run a script to gather the list from the site, then after I have done this to a few sites run the above quoted, to merge the different sites into 1 ( hopefully ) accurate list so I know the names & numbers of the roms for each list.

  • LAsitha Jun 14, 2012 @ 16:10

    Nice job.. thanxxx… :D

  • tot Jul 4, 2012 @ 12:22

    hey guys, how can I concatenate an echo command & DATE to only one line ..

    like:
    echo “The date is: ” && date >> file.txt

    but this command will display text “The date is: ” and insert the date into the file.txt

    the output I want is

    “The date is 07-12-12.”

    to the file.txt

    help pls..

    • shivdas Dec 19, 2013 @ 6:32

      echo `echo The date is: && date` >> file.txt

  • Dipti Das Jul 27, 2012 @ 12:11

    Very helpful steps for a fresher in C/C++

  • riten Jan 8, 2013 @ 11:11

    pls send me how to create log file(tape wirrten is normal file, normal file format only not tar file format)

  • masoud Jan 26, 2013 @ 18:46

    i want create “example.com” file in this dir:
    etc/apache2/
    plz help me…
    tnx

  • Salem Jan 27, 2013 @ 21:18

    How do you edit the file you created ?

    • 🐧 nixCraft Oct 6, 2013 @ 8:45

      Use a text editor such as vi/emacs/joe and more.

  • Ray Sep 19, 2013 @ 20:11

    Great, but I found a typo:

    To display file contents, type
    $ cat foot.txt

    It should be foo.txt not foot.txt.

  • Sabit Mubarik Mar 5, 2015 @ 13:12

    I am doing with my BS Computer Science, I am provided with a project to make a

    A_primitive linux shell using terminal of Ubuntu or GNU (GCC) compiler and in it i have to set up a shell environment to use the following commands
    1 cd
    2 ls
    3 pwd
    4 pop
    5 push

    B_ using any technique within this file system to make a single file password protected.

    Noted: Can’t use ‘system’ or ‘exe’ command but can use libC and its assorted libraries also can use dirent.h,ctype.h.
    please please please help me with this having said that i have to submit it on monday 9th March,2015

  • M.kabir rahimi May 4, 2015 @ 15:30

    how we can create html file automatically in linux

  • Ngeshlew Oct 28, 2015 @ 14:50

    Hi Vivek,

    Thank you for the post. It keeps me coming back because it is easy and very direct.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.