≡ Menu

HowTo: 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
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m
  1. cat command
  2. echo or printf command
  3. vi text editor
  4. joe text editor
  5. 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

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 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.
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 43 comments… add one }

  • Pafcio October 18, 2007, 9:58 am

    You can also use touch command to create empty files:
    touch foobar.txt

    • amit October 12, 2013, 7:47 am

      basically i m beginner could please give me any links or book to understand linux architecture, well i m also confused in using linux commands since there r so many option is there any web which explains how to use linux commands
      thanks in advance.

  • nixCraft October 18, 2007, 8:09 pm

    Pafcio,

    thanks for pointing out touch command!

  • Mohan October 18, 2007, 11:46 pm

    echo ‘ This is a multiline

    text file created

    from the bash shell

    using echo command’ > multiline.txt

    • shankar August 26, 2011, 9:39 am

      i want create a text file. pls could you help me…

      • maxwell September 22, 2011, 2:24 pm

        $ cat foot.txt

  • ganni October 29, 2007, 8:30 am

    Thanks a lot! It helps me.

    Thank you very much! all of you

  • Kaka November 16, 2007, 5:42 pm

    Excellent, cause I just used it at work, and it worked and helped me.

    Thanks

  • Nitish December 4, 2007, 3:48 pm

    Detailed information but would appreciate if someone can upload the solution for cat command as in executing or running the vi program. I am writing a finction in the vi and want to execute it outside the vi on the bash shell.

    Thx- Nitish Anand

  • amit nehra December 6, 2007, 2:12 am

    thanks guys. i do try cat, it works perfectly.
    but in vi mode i’m not able to save & exit the file. i had to exit using ctrl+z & the file was not saved.
    please help me. mail on nehra13@gmail.com

  • Nitish Anand December 23, 2007, 12:36 pm

    Have u tried using:-

    !wq for save and exit VI
    !w to just exit.

  • chakjoy February 24, 2008, 3:35 am

    have two files, ‘file1’ and ‘file2’.
    file1 has:
    1234
    4567
    6789

    file2 has:
    abcd
    efgh
    ijkl

    would like to combine into one like:
    1234 | abcd
    4567 | efgh
    6789 | ijkl

    Please give direction.

    Thanks

    • musteah October 5, 2010, 1:19 pm

      Hi,
      @chakjoy

      here is an solution. Using arrays:

      #!/bin/bash
      declare -a ARRAY1
      declare -a ARRAY2
      let count=0
      let count1=0
      while read LINE
      do
      ARRAY1[$count]=$LINE
      ((count++))
      done < file1.txt
      while read LINE
      do
      ARRAY2[$count1]=$LINE
      ((count1++))
      done < file2.txt
      for((i=0; i<${#ARRAY1[@]};i++));do
      echo "${ARRAY1[${i}]} | ${ARRAY2[${i}]}"
      done

      I hope this help.

  • Nitish Anand March 27, 2008, 12:07 pm

    Hi chakjoy,

    Did u manage to resolve your problem? Try the folling link if thats what you meant:-

    http://www.linuxsa.org.au/tips/command-pipelines.html

    OR

    cd /var/log && ls -al
    this switches to the /var/log directory if the directory change was sucessfuly it will then list the contents of that directory.

    You can also list the contents of a directory without leaving your current location..

    ls -al /var/log
    Would list the contents of /var/log no matter your current location in the directory tree.

    You can also use | to send the output of one command to the input of another..

    mount | column -t
    Show mount points in a column format

    ls -al | grep *.jpg
    List the contents of a directory buit only show files ending in .jpg

    ps aux | grep X11
    List processes but only show processes containing X11 > and list.txt
    Will list the contents of the /var/log directory and save it in the file list.txt

    hope this helps

    ====================

    You can chain copmmands together using &&

    cd /var/log && ls -al
    this swithces to the /var/log directory if the directory change was sucessfuly it will then list the contents o fhte directory.

    just to add to that, you can likewise chain commands together with the || operator (OR operator, not to be confused with a pipe | )

    Code:
    $ cd /mnt || cd /varthis would attempt to change to the /mnt directory. If it could not it would then switch to the /var directory. If the cd /mnt command was successfull then it would stop. Similarly,

    Code:
    $ cd /blah || cd /homeit’s probably safe to say there is no /blah directory on your computer so typing this command would attempt to change to a non-existant directory, fail, and consequently change to /home
    have fun!

  • Jaime June 10, 2008, 6:21 am

    There is so much to learn when you get inside of linux, for example, VI is not the easier text editor but it rocks xD

  • jakemanse June 16, 2009, 7:25 am

    That’s good I am new Linuxer , now I am studying it .
    Thanks.

  • Mithat Perkoz September 3, 2009, 2:16 pm

    Thx a lot guys.
    I am also a newbie to Linux and it helps a lot!

  • pratheesh November 10, 2009, 12:36 pm

    how to chkdsk in linux cmd mode

  • pratheesh November 10, 2009, 12:39 pm

    how to check in all disk drive in linux command mode

  • pratheesh November 10, 2009, 12:41 pm

    how to install other software in linux command mode

  • srividya July 1, 2010, 4:56 am

    please i am not able to understand to do program with echo command. please tell mr steps from starting….

  • srividya July 1, 2010, 4:58 am

    how to save lines in a file? how to execute ?

  • srividya July 1, 2010, 5:00 am

    how to save lines in a file at unix? how to execute?

  • Vidar 75 August 1, 2010, 8:25 pm

    Just $ > filename will do the same, manily creating a empty file named “filename”

    Go ahead type “> test” and see for your self

  • Suresh August 11, 2010, 4:40 pm

    What is the main difference between touch & vi commnd?

  • vr4sk December 7, 2010, 9:58 am

    Could someone help me understand what this means?

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

  • superking February 23, 2011, 8:37 am

    good job !!!

  • vijay March 21, 2011, 6:33 am

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

  • Ryan July 26, 2011, 6:39 pm

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

  • siddharth December 5, 2011, 4:54 pm

    hi guyz,

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

    Please help.

  • Phearret June 10, 2012, 12:41 am

    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 June 14, 2012, 4:10 pm

    Nice job.. thanxxx… :D

  • tot July 4, 2012, 12:22 pm

    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 December 19, 2013, 6:32 am

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

  • Dipti Das July 27, 2012, 12:11 pm

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

  • riten January 8, 2013, 11:11 am

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

  • masoud January 26, 2013, 6:46 pm

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

  • Salem January 27, 2013, 9:18 pm

    How do you edit the file you created ?

    • nixCraft October 6, 2013, 8:45 am

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

  • Ray September 19, 2013, 8:11 pm

    Great, but I found a typo:

    To display file contents, type
    $ cat foot.txt

    It should be foo.txt not foot.txt.

  • Sabit Mubarik March 5, 2015, 1:12 pm

    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, 3:30 pm

    how we can create html file automatically in linux

Leave a Comment