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.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash shell/terminal on Linux
Est. reading time 3 minutes

How to create a file in Linux from terminal window?

  1. Create an empty text file named foo.txt:
  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
  5. Append text to existing file in Linux:
    echo 'yet another line' >> 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

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
touch test.txt
Verify that empty files are created with the help of ls command:
ls -l data.txt test.txt

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

Appending data

Use the the >> instead of > to append data to existing file and to avoid overwriting files. The syntax is:

echo "There is no path to happiness. Happiness is the path." >> quote1.txt
printf 'It is ridiculous to think that somebody else can make you happy or unhappy.\n'  >> quote2.txt
printf 'Happiness does not depend on what you have or who you are. -- Buddha\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
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).


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 Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
10 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)
  • nobody Oct 2, 2013 @ 7:44

    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:

      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

        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:

          read -p "May I know your file name please? " filename
          echo "Creating $file ..."
          echo "$(date)" >"$file"
  • Victor Dec 19, 2020 @ 12:25

    cat &gt filename.txt
    Small missprint, you meant
    cat > filename.txt

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum