How to Create a File in Linux Using Text Editors and the Terminal

Learn how to create files in Linux using the terminal and text editors. A step-by-step guide to file creation in Linux using a variety of methods. Boost your productivity today!

Updated: 14 Jul, 23 by Antoniy Yushkevych 14 Min

List of content you will read in this article:

Anyone using the Linux operating system daily should be familiar with creating a text file in Linux, or Linux creates file commands. A new file may be created using either the command line or the desktop file manager. You should have been granted writing permissions on the parent directory to generate a new file. If you don't, you'll get a permission denied mistake. This tutorial will provide brief information about creating a file in Linux. 

Linux recognizes different types of files based on their characteristics and intended usage. Here are some common file types you may encounter in a Linux system:

  1. Regular Files: Regular files, denoted by the letter " - " in file listings, are the most common type of files. They can contain text, binary data, or a combination of both. Regular files can be documents, scripts, programs, configuration files, or any other file used to store data.
  2. Directories are special files that organize other files and directories in a hierarchical structure. They are represented by the letter "d" in file listings. Directories provide a way to organize files into a logical structure, making it easier to locate and access them.
  3. Symbolic Links: Symbolic links, also known as symlinks or soft links, are files that point to another file or directory. They act as shortcuts or aliases, allowing you to access files or directories elsewhere on your system. Symbolic links are denoted by the letter "l" in file listings.
  4. Device Files: Device files represent physical or virtual devices connected to the system. They allow applications to communicate with hard drives, keyboards, printers, and network interfaces. Device files are classified into two categories: character devices (represented by the letter "c") and block devices (represented by the letter "b").
  5. Named Pipes: Named pipes, also known as FIFOs (First-In-First-Out), provide a mechanism for interprocess communication. They allow data to be passed between processes in a way similar to a real pipe. Named pipes are denoted by the letter "p" in file listings.
  6. Sockets: Sockets enable communication between processes on the same system or across different network systems. They facilitate data exchange between applications, such as messages or file transfers. Sockets are represented by the letter "s" in file listings.

In Linux, it is important to identify the file type to understand its purpose, handle it appropriately, and determine which applications can open or process it. Linux provides several methods to identify the type of a file, allowing users to quickly determine whether a file is a regular, directory, symbolic link, or another type of file. This section will explore different techniques to identify the file type in a Linux system.

 File Extensions

One common way to identify file types in Linux is by examining their extensions. File extensions are the suffixes attached to the end of filenames, such as ".txt" for text files or ".jpg" for image files. While file extensions can provide a clue about the file type, they are not foolproof, as they can be misleading or absent. Also, Linux does not rely heavily on file extensions to determine the file type.

 File Command

The 'file' command is a powerful utility in Linux that examines the content of a file to determine its type. The 'file' command provides detailed information about the file type by analysing the file's data and metadata. To use the 'file' command, open a terminal and run the following syntax:

file <filename>

For example, running 'file myfile.txt' will display the file type 'myfile.txt' and additional details such as the file's encoding and any known file formats it matches.

Stat Command

The 'stat' command is another useful tool to identify file types in Linux. It provides detailed file information, including their type, size, access permissions, and timestamps. To use the 'stat' command, open a terminal and enter the following command:

 stat <filename>

The output will include various file attributes, including the file type. For example, if you run 'stat mydirectory' will display information about the directory named 'mydirectory'.

 ls Command

The 'ls' command is commonly used to list files and directories in Linux. When used with certain options, such as the '-l' (long format) or '-F' (classify) options, the 'ls' command can provide hints about the file types. The '-l' option displays detailed information, including the file type indicated by a symbol before the filename (e.g., "d" for directories, "-" for regular files). The '-F' option adds specific symbols to filenames, such as "/" for directories or "@" for symbolic links.

Now we will explain various methods to create a file in Linux easily:

How to Create a File in Linux from Terminal Window

You can use various commands and techniques to create a file in Linux from a terminal window. Here are a few commonly used methods:

Using the Touch Command to Create a File

We may use the touch command to change the timestamps of current files and folders and create new, empty files. The touch command is the simplest and most unforgettable way to generate new, empty folders.

To make a new file, use the touch command and the name of the file you want to make:

touch filename.txt

 If filename.txt does not exist, the command above will generate it; otherwise, the timestamps will be changed. To make several files simultaneously, split the file names with space as listed in the below Linux command.

touch filename1.txt filename2.txt filename3.txt

Using the Echo Command to Create a File

The echo command outputs the strings passed as arguments to standard output, which can be forwarded to a register. For creating a new file in linux, type echo followed by the text you want to print, and then use the redirection operator > to write the output to the new file.

echo "Some line" > filename.txt

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

echo > filename.txt

Using the Redirection Operator to Make a File

You may use redirection to take a command's output and use it as input for another command or file. There are two ways to assign the output to a file. The >> operator appends the output to a current file, while the > operator overwrites it. In Linux, this is the quickest way to make a new file. You should not delete a significant existing file when using redirection to create a file. To make an empty zero-length buffer, specifically indicate the name of the file you want to construct after the redirection operator:

> filename.txt

Using the Cat Command to Create Text File

The cat command is mostly used to read and concatenate files but can create new ones. To make a new file, use the cat button, followed by the redirection operator > and the new file's name. Click Enter to type your email, and then press the CTRL +D keys to save the files.

cat > filename.txt

Using Heredoc to Create a File

Heredoc is a form of redirection that lets you give a command multiple lines of input. This approach is typically used when generating a file from a shell script containing several text lines. To build a new file named filename.txt, for example, use the following code:

cat << EOF > filename.txt
Some lines
Some other lines
EOF

The heredoc's body will use variables, special characters, and instructions.

Creating a Large File

You may occasionally want to generate a big data file for testing purposes. When you want to measure the write speed of your drive or the download speed of your link, this is handy.

 Making use of the dd command

The dd command is most often used to copy and transfer files. To make a 2GB file called 2G.test, execute the following commands:

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

 The fallocate function is a command-line feature that allows you to assign actual disc space to data. The command below will generate a new file called 2G.test with a 2GB size:

fallocate -l 2G 2G.test

 Creating a File using the printf Command

Follow the steps below to create a file using the 'printf' command in Linux:

  1. Open a Terminal
  2. Navigate to the Desired Directory
  3. Use the 'cd' command to navigate to the directory where you want to create the file. For example, if you want to create the file in your home directory, use the following command:

cd ~

  1. Create the file using the 'printf' command. Use the following syntax:

printf 'file content' > filename.extension

Replace 'file content' with the desired content you want to write to the file. Replace the filename.extension with your file's desired name and extension.

  1. Verify the file creation by using the 'ls' command. Execute the following command:

ls

This will display a list of files and directories in the current directory. Confirm that your newly created file appears in the list.

  1. View the file content by using the 'cat' command. Use the following command:

cat example.txt

The content of the file will be printed to the terminal.

Using Text Editors to Create a Linux File

Text editors are crucial in creating and editing files in a Linux environment. They provide a user-friendly interface to write, modify, and save text-based files. This section will explore how to create files in Linux using three popular text editors: Joe, Nano, and Vi/Vim.

How to Create a File in Linux Using Joe Text Editor

Joe is a user-friendly and feature-rich text editor for Linux systems. Follow the steps below to create a file using Joe: 

  1. Open a terminal by launching the Terminal application or using a keyboard shortcut (e.g., Ctrl+Alt+T).
  2. Navigate to the desired directory where you want to create the file using the 'cd' command. For example:

cd /path/to/directory

  1. Enter the following command to open Joe text editor and create a new file:

joe filename.extension

   Replace 'filename.extension' with your file's desired name and extension.

  1. The Joe text editor will open, providing a blank canvas to enter your content. Start typing the desired content for your file.
  2. Once you have finished entering the content, press Ctrl+K followed by X to save and exit the editor.
  3. You have now successfully created a file using Joe's text editor.

How to Create a File in Linux Using Nano Text Editor

Nano is a lightweight and beginner-friendly text editor available on most Linux distributions. Follow the steps below to create a file using Nano:

  1. Launch a terminal window.
  2. Navigate to the directory where you want to create the file using the 'cd' command.
  3. Enter the following command to open the Nano text editor and create a new file:

nano filename.extension

Replace 'filename.extension' with your file's desired name and extension.

  1. The nano text editor will open with a blank screen. Start typing the content for your file.
  2. Once you have finished entering the content, press Ctrl+O to save the file.
  3. When prompted, confirm the filename and press Enter.
  4. Press Ctrl+X to exit the Nano text editor.
  5. Congratulations! You have successfully created a file using the Nano text editor.

How to Create a Text File in Linux Using Vi / Vim Text Editor

Vi and Vim are powerful and widely used text editors in Linux. Although they have a steep learning curve, they offer extensive features and flexibility. Follow the steps below to create a file using Vi/Vim:

  1. Open a terminal window.
  2. Navigate to the directory where you want to create the file using the 'cd' command.
  3. Enter the following command to open the file in Vim:

vi filename.extension

Replace 'filename.extension' with your file's desired name and extension.

  1. The vim text editor will open in command mode. Press 'i' to enter insert mode, which allows you to start typing the content for your file.
  2. Begin typing the desired content for your file.
  3. Once you have finished entering the content, press the Esc key to exit insert mode and return to command mode.
  4. To save the file and exit Vim, type ':wq' and press Enter.
  5. Congratulations! You have successfully created a text file using Vi/Vim text editor.

 How to Create a ZIP File and Ignore the Directory Structure in Linux

To create a zip file in Linux while ignoring the directory structure, you can use the 'zip' command with the '-j' option. Here's how you can do it: 

  1. Open a terminal window.
  2. Navigate to the parent directory that contains the files and directories you want to include in the zip file. For example, if you want to zip the contents of the "my_directory" directory located in your home directory, use the following command:

 cd ~/my_directory

  1. Now, to create the zip file while ignoring the directory structure, use the 'zip' command with the '-j' option followed by the name of the zip file and the files or directories you want to include. For example, to create a zip file named "my_files.zip" containing all the files and directories in the current directory without preserving the directory structure, use the following command:

   zip -j my_files.zip *

The '*' wildcard symbol includes all files and directories in the current directory.

  1. The 'zip' command will create the zip file "my_files.zip" in the current directory, including all the files and directories without maintaining the original directory structure.

 Using the '-j' option, you instruct the 'zip' command to store only individual files without directory paths. This allows you to create a zip file that includes all the files from different directories without replicating the original directory structure.

Using different commands and redirection, we have learned how to create a file in Linux/Linux create file command using the command line functions. We hope our described information can help you create a new file in Linux without trouble. If you have any other suggestions for creating a new file, comment via the below comment section.

Are you a website owner or looking to create a website but do not know where to host it? You can use our Linux VPS to host your website without errors. You will also have a 24/7 support team who will always help you.

Antoniy Yushkevych

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com