How to Create a Text File in Linux Terminal [5 Best Ways]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To create a text file in Linux Terminal, you can try this method:

  1. Open the Terminal.
  2. Navigate to your target directory with cd directory-name.
  3. Create an empty text file by typing touch filename.txt.
  4. Verify the file creation using ls.

Learn more about how to create a text file in Linux Terminal with five proven methods and some best practices to get the most out of it.

Whether you are a developer, sysadmin, or simply a Linux enthusiast, knowing how to create text files directly in the Terminal can save you time, increase your productivity, and streamline your workflow. In this article, I will explain five ways to create text files in the Linux Terminal, providing step-by-step instructions and practical examples. Apart from these methods, you’ll also get to learn the five best practices to follow when creating a text file in Linux Terminal.

Why Use the Terminal for Creating Text Files?

Using the terminal to create text files in Linux offers several key advantages:

  • Efficiency and Speed: The terminal allows you to create files quickly with simple commands, avoiding the slower process of opening a graphical text editor. This speed is crucial for developers and system administrators who need to perform tasks rapidly.
  • Remote Management: When managing remote servers, the terminal is often the only option. You can easily create and manage files on a server through SSH, making it an essential tool for remote server management. This is especially important for cloud services and remote working environments.
  • Automation: The terminal makes it easy to automate file creation with scripts. You can schedule tasks to run at specific times, creating files automatically. This capability is invaluable for maintaining regular backups, updating logs, or performing routine maintenance tasks without manual intervention.
  • Resource Efficiency: Terminal commands use fewer system resources compared to graphical editors, which is important on older or low-powered machines. This efficiency helps in maintaining system performance and is especially beneficial for environments with limited resources.
  • Flexibility: The terminal provides powerful tools for creating, editing, and managing files. You can combine commands to perform complex tasks quickly, enhancing productivity and enabling advanced file operations that are difficult or impossible with graphical interfaces.

How to Create a Text File in Linux Terminal

To create a text file in the Linux terminal, you can use several methods. First, use the touch command, like touch filename.txt, to create an empty file. Second, use the cat command with cat > filename.txt, type your content, then press Ctrl+D to save. Third, use the echo command, such as echo "Your text" > filename.txt, to create a file with specific content.

Fourth, use text editors like Nano or Vim by typing nano filename.txt or vim filename.txt, then enter your content, and save it with the appropriate commands (Ctrl+O and Ctrl+X for Nano, :wq for Vim). Lastly, use the printf command, like printf "Your text\n" > filename.txt, to create a file with formatted content.

Now, let’s have a closer look at each of these methods here:

1. touch Command

The touch command is a simple and widely used method among Linux users to create an empty text file in the Terminal. Follow these steps to create a text file using this command:

  1. In the Terminal window, use the cd command to navigate to the directory where you want to create the text file.
cd directory-name
  1. Type the following command and replace filename.txt with the desired name of your text file:
touch filename.txt
  1. Press Enter to execute the touch command.
press enter to execute the touch command
  1. Then, use the ls command to confirm if the text file is created in the directory.
ls command to confirm if the text file is created in the directory

You can specify permissions for the file by using the -m option followed by the desired permissions.

2. cat Command

The cat command allows you to create a text file using the > operator followed by the filename. Moreover, you can immediately add content to the file when created using this command. Here are the detailed steps to create a text file in Linux Terminal with this command:

  1. Head to the directory where you want to create a text file via Linux Terminal.
head to the directory where you want to create the text file
  1. Execute the following cat command while replacing filename.txt with the desired name of your text file:
cat > filename.txt
  1. Once you press Enter to execute the command, it’ll open the text editor within the Terminal interface. Type some content in this text file. Then, press Enter to add new lines.
text editor within the terminal interface
  1. Once you have finished adding content, press Ctrl + D to save the file.
once you have finished adding content
  1. Then, run the ls and cat commands to see if the file is created with the typed content.
run the ls and cat commands create a text file in linux terminal

3. echo Command

Another way to create a text file in Linux is to use the echo command. This method lets you create a text file with specific content. Here’s how you can use this method:

  1. In the Terminal, run the following command and replace filename.txt with the desired name of your text file:
echo "Your text here" > filename.txt
  1. Press Enter to execute the echo command.
press enter to execute the echo command
  1. To confirm the successful creation of the text file, run the ls command.
confirm the successful creation of the text file

4. printf Command

The printf command in Linux is a versatile tool that allows users to create and format text files with precision. Unlike other methods, printf offers extensive formatting capabilities, making it ideal for generating well-structured and complex text outputs.

  1. Begin by opening your terminal. 
  2. In the terminal, type the printf command followed by your desired text and redirection operator to create a new file:
printf "Your formatted text here" > filename.txt
creating a new file and entering text in it
  1. After typing the command, press Enter. 

This will execute the command and create a new file named filename.txt containing the specified text.

result of the command

5. Text Editors

Text editors like Nano and Vim provide more advanced features for creating and editing text files. Generally, these tools are installed on your Linux machine by default. However, if you need to install them, run sudo apt-get install nano or sudo apt-get install vim in the Linux command prompt. Once you are done with the installation of the text editor, use the text editor to create a text file in Linux Terminal:

  1. Launch the Linux command line interface and navigate to the directory where you want to create the text file using the cd command.
create the text file using the cd command
  1. Run the command nano filename.txt or vim filename.txt, while replacing the filename.txt with the desired name of your text file.
run the command nano filename
  1. The Nano editor will open within the Terminal interface.
nano editor will open within the terminal interface
  1. Start typing the content in your text file inside the Nano editor. Use the available commands in the text editor to navigate and edit your file.
start typing the content in your text file inside the nano editor
  1. Once you have finished editing, press Ctrl + O to save the file and Ctrl + X to exit the text editor.
once you have finished editing create a text file in linux terminal

5 Things to Consider When Creating a Text File in Linux Terminal

By considering these key factors when creating text files in the Linux Terminal, you can enhance file security, improve organization, streamline access, and ensure the integrity of your important data. Here are five key things to keep in mind:

  • 🔒 File Permissions: Set the right permissions to control access to your files. Use the chmod command to modify permissions. Balance necessary access with security and regularly review and update permissions to match your security needs.
  • 📄 File Extensions: Adding appropriate file extensions helps with compatibility and easy identification. Common extensions include .txt, .conf, .log, .cfg, and .sh. Choose extensions that reflect the file’s content and purpose for better recognition.
  • 📝 File Naming Conventions: Follow consistent naming conventions to locate and manage files efficiently. Use meaningful names with relevant details like dates or project names. Use dashes, underscores, or capitalize each word for readability.
  • 📂 File Location: Organize files logically by creating directories with clear names. Use commands like cd to navigate, mkdir to create directories, ls to list files, and mv to move files. Regularly review your file structure to keep it organized and accessible.
  • 💾 Backup and Version Control: Prevent data loss and track changes with reliable backup and version control practices. Regularly back up files using tools like rsync, tar, or cloud storage. Use version control systems like Git to track changes, revert to previous versions, and collaborate efficiently. Develop a strategy that suits your workflow to ensure file safety and availability.

Wrapping Up

In this article, I’ve shown you how to create text files using the Linux terminal with methods like touch, cat, and echo commands, and text editors like Nano and Vim. I have also covered best practices for file permissions, naming conventions, and file organization.

If you find this article helpful, check out my other articles on:

Frequently Asked Questions

What is the difference between the touch and cat commands?

The touch command is used to create an empty text file. It simply creates a file without any content. On the other hand, the cat command not only creates a text file but also allows you to add content to it immediately. So, you can use the cat command to create a file and append text to it in a single operation.

How can I change the permissions of a text file?

To change the permissions of a text file, you can use the chmod command. It allows you to modify the permissions for the owner, group, and other users. You can specify the desired permissions using either numerical notation (e.g., 644) or symbolic notation (e.g., u+rwx, g-w, o+r). By adjusting the permissions, you can control who can read, write, or execute the file. Ensure you have the necessary permissions to modify the file’s permissions or use the appropriate sudo command to execute chmod with root privileges if required.

What is the best way to create a text file?

The best way to create a text file depends on your specific requirements and preferences. Each method outlined in this article has its advantages. If you need a quick way to create an empty text file, the touch command is suitable. But if you want to create a text file with content, the cat command or using a text editor like Nano or Vim provides more flexibility and convenience. Consider your needs, familiarity with different commands, and the desired content of the text file to choose the best method for you.

How do I list all the text files in a directory?

To list all the text files in a directory, you can use the ls command along with suitable options. For example, to list all files with a .txt extension, you can use the command: ls *.txt. The asterisk (*) is a wildcard character that matches any characters before the .txt extension. This command will display the names of all text files in the current directory that end with .txt. You can further customize the ls command using various options to modify the output format or include additional details about the files.

What are the permissions of a newly created text file?

When you create a new text file, its permissions are determined by the default permissions of its parent directory. The exact permissions of the newly created file may vary based on your system’s configuration and the permissions set for the parent directory. Typically, the file owner has read and write permissions, while other users may have limited or no permissions initially. You can modify these permissions using the chmod command to meet your specific requirements.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Show Hidden Files in Linux: 4 Best Ways

Next Post

How to Adjust Tmux Pane Size [3 Effective Methods]

Read next