Oct 11, 2023 5 min read

How to Create a File on Linux

Create a File on Linux with our step-by-step tutorial. It is a popular operating system renowned for its versatility, security, and stability.

Create a File on Linux
Table of Contents

Introduction

Before we begin talking about how to create a file on Linux, let's briefly understand – What is Linux ?

Linux is a popular operating system renowned for its versatility, security, and stability. Anyone who uses Linux on a regular basis should be familiar with how to create a new file. A new file can be created using either the command line or the desktop file manager.

In this tutorial, we'll show you how to use the command line to easily create a new file on Linux. We will also address a few FAQs on how to create a file on Linux.

Advantages of Linux

  1. Versatility: Linux runs on a wide range of devices, from smartphones to servers, enabling seamless integration across platforms.
  2. Security: Linux's robust architecture and strong community support make it more resistant to malware and cyber threats.
  3. Stability: With its efficient design and reliability, Linux systems can operate continuously for extended periods without performance issues.
  4. Cost-effective: Linux is free to use and offers a vast repository of free and open-source software, saving money on licensing fees.
  5. Customization: Linux allows users to personalize their computing experience, enabling them to tailor the system to their specific needs and preferences.

Before you Begin

You must have written permission on the parent directory in order to create a new file. If you don't, you'll get a permission denied error.

The ls command is used to view the contents of a directory.

Creating a File with touch Command

We can use the touch command to change the timestamps of existing files and directories, as well as create new, empty files.

The touch command is the simplest and most memorable way to create new, empty files.

Use the touch command and the name of the file you wish to create a new file:

touch file1.txt

If file1.txt does not exist, the command above will create it; otherwise, the timestamps will be changed.

To make many files at the same time, separate the file names by a space:

touch file1.txt file2.txt file3.txt

Creating a File with the Redirection Operator

You can use redirection to take a command's output and use it as input for another command or file. You can redirect output to a file in two ways. The >> operator appends the output to an existing file, whereas the > operator overwrites it.

Simply specify the name of the file you want to create after the redirection operator to create an empty zero-length file:

> file1.txt

In Linux, this is the shortest way to create a new file.

When using redirection to create a file, take care not to overwrite a critical existing file.

Creating a File with cat Command

Cat is mostly used to read and concatenate files, but it can also be used to create new ones.

To make a new file, use the cat command, followed by the redirection operator > and the name of the new file. Press Enter to input your text, and then press CRTL+D to save the files.

cat > file1.txt

Creating a File with echo Command

The echo command outputs the strings supplied as arguments to standard output, which can be redirected to a file.

To make a new file, type echo followed by the text you wish to print, and then use the redirection operator > to write the output to the new file.

echo "Some line" > file1.txt

If you want to create an empty file, just type:

echo > file1.txt

Creating a File using Heredoc

A Here document, often known as Heredoc, is a sort of redirection that lets you pass multiple lines of input to a command.

This method is typically used when you need to produce a file from a shell script that has numerous lines of text.

To create a new file named file1.txt, for example, use the following code:

cat << EOF > file1.txt
Some line
Some other line
EOF

Variables, special characters, and commands can all be found in the heredoc's body.

Creating a Large File

You may want to construct a huge data file for testing purposes on occasion. When you want to test the write speed of your disc or the download speed of your connection, this is beneficial.

Using dd command

The dd command is most commonly used to copy and convert files.

To create a 1GB file named 1G.test, use the following commands:

dd if=/dev/zero of=1G.test bs=1 count=0 seek=1G

Using fallocate command

fallocate is a command-line tool that allows you to allocate real disc space to files.

The command below will create a new file named 1G.test with a 1GB size:

fallocate -l 1G 1G.test

FAQs to Create a File on Linux

Can I create a file with specific content? 

Yes, you can use the echo command to create a file with specific content. For example, type echo "Hello, world!" > filename.txt to create the file "filename.txt" with the text "Hello, world!".

How can I create a file in a specific directory? 

To create a file in a specific directory, navigate to that directory using the cd command and then use the techniques mentioned above to create a file.

What if I want to create a file without an extension? 

You can create a file without an extension simply by omitting the "extension" part when using the touch command. For instance, type touch filename to create a file without an extension.

Can I create multiple files simultaneously? 

Yes, you can create multiple files at once. Use the touch command followed by the names of the files you want to create, separated by spaces. For example, touch file1.txt file2.txt file3.txt.

What if I want to create a file with a specific permission? 

To create a file with specific permissions, you can use the chmod command after creating the file. For example, chmod 644 filename.txt will set the file's permissions to read and write for the owner, and read-only for others.

How do I create a file with a specific size? 

To create a file with a specific size, you can use the truncate command. For instance, type truncate -s 1M filename.txt to create a file named "filename.txt" with a size of 1 megabyte.

What if I encounter permission issues while creating a file? 

If you encounter permission issues, ensure that you have the necessary permissions to create a file in the specified directory. You may need to use the sudo command to gain superuser privileges or change the permissions of the target directory using the chmod command.

Conclusion

Using various commands and redirection, you learned how to create a new file on Linux from the command line in this tutorial.

If the command line isn't your style, the File Manager's right-click menu may simply create a blank text file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.