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

last updated in Categories , , , ,

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.

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 terminal window quickly.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

4 comment

  1. In my shell script I put

    cat > logfile<<EOF
    Something went wrong at $(date) on $(hostname)
    blah
    blha
    EOF

    Have a question? Post it on our forum!