• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Create a File in Linux

Written by Admin, Updated On May 21, 2020
bash, terminal
How to Create a File in Linux

Creating a file in Linux is basic requirement for users. In Linux, we can create a new file using the command line or from the desktop file manager. This tutorial explains how to create a new file in Linux using the command line.

Prerequisite#

Make sure your logged in user have write permission on parent directory.

Before going to creating a new file, you can check the existing files and directories list using ls command.

Create File with Touch Command#

The touch command will create a new empty file and update the timestamps. It will work same for existing files as well. Simply run the following touch command to create a new file:

touch test.txt

In above command replace text.txt with your real filename. If the file is exists in directory it will just update the timestamps other it will create and update timestamps.

You also can create multiple file in a single command, give file names separated by space:

touch test1.txt test2.txt test3.txt

Creating a File with cat Command#

Generally, the cat command is used to read but it also can be used for creating new files. To create a new file run the cat command followed by the redirection operator > and the name of the file you want to create. Press Enter type the text and once you are done press the CRTL+D to save the files.

cat > test1.txt

Create a File with echo Command#

The echo command prints the strings that are passing as arguments to the standard output, which can be redirect to a file.

To create a new file run the echo command followed by the text you want to print and use the redirection operator > to write the output to the file you want to create.

echo "Test line" > test.txt

For empty file just run:

echo > test1.txt

Creating a File with the Redirection Operator#

You can create a file using redirect symbol, which is usually used to redirect the output of a command to a new file or to another command. The > operator will overwrite an existing file, while the >> operator will append the output to the file.

For example, to create a empty new file, simply run the following command in your terminal:

> test1.txt

Conclusion#

You learned how to create a new file in Linux using the command line with different methods.

If you have any question or feedback, leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Secure Apache with Let’s Encrypt on Ubuntu 20.04
Next Article   How to Check MySQL Version

Related Posts

  • How to Use sed to Find and Replace String in Files

    How to Use sed to Find and Replace String in Files

    December 18, 2020
  • Linux Head Command

    Linux Head Command

    December 16, 2020
  • Rename Files and Directories in Linux

    How to Rename Files and Directories in Linux

    December 15, 2020

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook