Wave Layer Blog

Crypto Friendly Dedicated Servers Located In 50+ Countries!
World Class 24×7 Support:

How to Create a File in Linux Terminal

Files offer a mechanism to arrange and store data, be it program code, configuration settings, or text documents. Files are used to store records such as error logs and system logs. Administrators can monitor system health, track events, and diagnose problems with the aid of these files.

In Linux, using the command line to create files is the fastest way possible. It is a basic skill for all Linux users, especially system administrators, who often need to write scripts and modify configuration files.

In this tutorial, we’ll show you various ways to create files from your Linux terminal.

Prerequisites

  • A command line environment
  • Sudo or root privileges

Creating Files using the touch Command

Using the touch command to create files is the easiest method. Open your terminal and run:

				
					 touch wavelayer.txt
				
			

This command creates an empty file. To confirm if your file has been created, you can run the following command:

				
					 ls
				
			

You can also create multiple files by inputting the file names separately with spaces after the touch command.

				
					 touch wavelayer.txt1 wavelayer.txt2 wavelayer.txt3
				
			

Creating Files Using the cat Command

The cat command is a versatile tool that can be used for several purposes, including creating new files. To create a file using the cat command, run:

				
					 cat > ourtext.txt
				
			

Replace ourtext.txt with the desired name for your file. Press Enter, and the command will wait for you to enter text. Once you’re done, save and exit the file by pressing Ctrl+D.

To view the contents of the file, run:

				
					 cat ourtext.txt
				
			

You can also use the cat command to concatenate (combine) multiple files. For instance:

				
					 cat wavelayer.txt1 wavelayer.txt2 wavelayer.txt3 > combined_wavelayer.txt
				
			

This command combines the contents of wavelayer.txt1, wavelayer.txt2, and wavelayer.txt3 into a new file called combined_wavelayer.txt.

Creating Files using the echo Command

The echo command is versatile tool that allows you to create and manipulate files. The echo command duplicates whatever you specify in the command, and puts the copy into a file.

You can use the echo command to add text to file, For instance:

				
					 echo "hello world" > sample.txt
				
			

This command creates a file called sample.txt with the specified text.

You can also use the echo command to append text to a file.  To add more text to an existing file (without overwriting), use the double redirection operator as shown:

				
					 echo "additional content" >> sample.txt
				
			

If you want to view the contents of the file you created, you can use the cat command to print the contents of sample.txt:

				
					 cat sample.txt
				
			

Using Text Editors to Create Files on Linux

Each Linux distribution comes with its own built-in command-line text editor. In addition, you can also install various command-line text editors to leverage their unique features. You can check out the following text editors to see how you can use them to create files.

1. Vi Text Editor (vi)

Vi is a powerful text editor commonly found on most Linux systems. To create a file using the vi text editor, run:

				
					 vi myfile.txt
				
			

The screen will change to show that you are in the text editor. Press the i key to enter the insert mode, and then enter some text. Next, type :wq and press Enter to save the file and exit Vi mode.

2. Vim Text Editor (vim)

Vim is a robust, free, and open-source text editor. It is designed for efficient text editing. It’s an improved version of the original vi editor found on most UNIX systems.

Vim is often known as a “programmer’s editor.” This is due to its versatility and usefulness for coding tasks.

To create a file using Vim, run:

				
					 vim testfile.txt
				
			

Vim’s interface resembles Vi; you use Vi commands since they share functionality and commands.

3. Nano Text Editor

Nano is a  simple text editor that runs in the terminal. To use this text editor, launch your terminal and run:

				
					 nano test.txt
				
			

By default, Nano launches in edit mode and conveniently shows a list of helpful commands at the bottom of the screen for quick access. Press Ctrl + O to save changes (write out) and press Ctrl + X to exit Nano.

Conclusion

Now you have multiple options you can use to create new files in Linux from the command line. I hope you find this tutorial helpful.

Share the Post:

Related Posts

How to install Kryptex Miner on Ubuntu

/*! elementor – v3.19.0 – 07-02-2024 */ .elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px} Kryptex Miner Kryptex Miner is a

Read More