How to create a file in Linux using the bash shell terminal

I am a new Linux system user. How do I create a file in Linux using the bash shell terminal? What is the fastest and easiest way to create a file in a Linux terminal?

Introduction – A file is nothing but a container in a Linux based system for storing information. For example, music stored in a file named foo.mp4. Similarly, my cat’s picture stored in kitten.jpg and so on. This page shows various methods to create a file in Linux using the terminal window.

ADVERTISEMENTS

How to create a file in Linux from terminal window?

  1. Create an empty text file named foo.txt:
    touch foo.bar
    OR
    > foo.bar
  2. Make a text file on Linux:
    cat > filename.txt
  3. Add data and press CTRL+D to save the filename.txt when using cat on Linux
  4. Run shell command:
    echo 'This is a test' > data.txt

Let us see some examples for creating a text files on Linux operating systems.

How to create a text file using the cat command

To create a text file named sales.txt, type the following command and then press [Enter] key:
cat > sales.txt
Now type your lines of text. For example:

Month, Profit
01/Apr/2004, $1500
01/May/2004, $1600
01/Jun/2004, $1450
01/Jul/2004, $1950
01/Aug/2004, $3950
01/Sep/2004, $2950
01/Oct/2004, $1750
01/Nov/2004, $1950
01/Dec/2004, $3250

When done and you need to save and exit, press Ctrl+D to return to the bash shell prompt. To view file use cat or more command/less command:
cat sales.txt
more sales.txt

How to create a file in Linux from terminal window

How to create a file in Linux from terminal window

See also: Redirection of standard output

How to create an empty text file using the touch command

Simply type any one of the following command:
> data.txt
OR
touch test.txt
Verify that empty files are created with the help of ls command:
ls -l data.txt test.txt
How to Quickly Create a Text File Using the Command Line in Linux

Creating a file in Linux using the echo or printf

Let us create a file called quote1.txt using echo command, enter:
echo "While I thought that I was learning how to live, I have been learning how to die." > quote1.txt
OR use the printf command printf 'Study nature, love nature, stay close to nature. It will never fail you.\n' > quote2.txt

How to create a file in Linux using joe text editor

JOE is text editor. To create a file called delta.txt, type:
joe -help delta.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).

How to create a text file in Linux using vi / vim text editor

The vi / vim is another text editor. To create a file called purchase.txt, type:
vi purchase.txt
OR
vim purchase.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).

Conclusion

You learned various methods that allow you to create text files in a Linux or Unix/macOS terminal window quickly.

🐧 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

ADVERTISEMENTS
8 comments… add one
  • Martin Apr 1, 2004 @ 22:16

    Bash create a file in Linux:
    touch lock.file

  • George Mar 13, 2005 @ 12:11

    I needed to create a file containing a newline:
    echo '' > foo
    printf '\n' > bar

  • Tim Aug 2, 2008 @ 3:11

    In my shell script I put

    cat > logfile<<EOF
    Something went wrong at $(date) on $(hostname)
    blah
    blha
    EOF
  • nobody Oct 2, 2013 @ 7:44

    EMACS or GTFO
    emacs file
    You kids get off my lawn!

  • Iridium Flare Sep 19, 2020 @ 12:50

    How to do this (creating non-empty file) from executable .sh file? Since we don’t have the option to press Ctrl+D while running executable .sh file.

    • 🐧 Vivek Gite Sep 19, 2020 @ 15:02

      In your script you put something as follows:

      #!/bin/sh
      file="/tmp/bar.txt"
      echo "Creating non-empty file called /tmp/foo.txt ..."
      echo "This is a test" > /tmp/foo.txt
      echo "Creating another file called ${file} ..."
      echo "Thursday" > "${file}"
      ## append ##
      echo "Friday" >> "${file}"
      

      Use the cat command to view those two files.
      cat /tmp/foot.txt /tmp/bar.txt

      • Leo Oct 17, 2020 @ 20:53

        @Vivek-
        i am new to shell scripting side and learning
        How to create a file using shell scripting ? Like format should be name_fol, i need to specify the name like Kenny or Jenny when it prompts and the script needs to create a file Kenny_fol.txt or Jenny_fol.txt ..

        please help

        • 🐧 Vivek Gite Oct 21, 2020 @ 1:17

          Try the following:

          #!/bin/bash
          file=""
          read -p "May I know your file name please? " filename
          file="${filename}_fol.txt"
          echo "Creating $file ..."
          echo "$(date)" >"$file"

Leave a Reply

Your email address will not be published.

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