• 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 December 10, 2022
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 Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 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