How to create and edit text file in linux by using terminal

In this Linux beginner series, you’ll learn various methods to create a text file in Linux terminal.

If you have used the desktop oriented operating system such as Windows, creating file is a piece of cake. You right click in the file explorer and you would find the option of creating new file.

Things won’t look the same when you are in a command line environment. There is no right click option here. So how do you create a file in Linux then? Let me show you that.

Create file in Linux command line

There are various ways of creating a new file in Linux terminal. I’ll show you the commands one by one. I am using Ubuntu here but creating files in Ubuntu terminal is the same as any other Linux distribution.

1. Create an empty file using touch command

One of the biggest usages of the touch command in Linux is to create a new empty file. The syntax is super simple.

If the file doesn’t exist already, it will create a new empty file. If a file with the same name exists already, it will update the timestamps of the file.

2. Create files using cat command

Another popular way of creating new file is by using the cat command in Linux. The cat command is mostly used for viewing the content of a file but you can use it to create new file as well.

You can write some new text at this time if you want but that’s not necessary. To save and exit, use Ctrl+D terminal shortcut.

If the file with that name already exists and you write new text in it using the cat command, the new lines will be appended at the end of the file.

3. Create new file using echo command

The main use of the echo command is to simply repeat (echo) what you type on the screen. But if you use the redirection with echo, you can create a new file.

To create a new empty file using echo you can use something like this:

The newly created filename.txt file will have the following text: This is a sample text. You can view the file in Linux using cat or other viewing commands.

You are not obliged to put a sample text with echo. You can create an (almost) empty file using the echo command like this:

This will create a new file with just one empty line. You can check the number of lines with wc command.

4. Create a new file using a text editor like Nano or Vim

The last method in this series is the use of a text editor. A terminal-based text editor such as Emacs, Vim or Nano can surely be used for creating a new file in Linux.

Before you use these text editors, you should make sure that you know the basics such as saving an existing from the editor. Unlike the GUI tools, using Ctrl+S in the terminal won’t save the file. It could, in fact, send your terminal into a seemingly frozen state from which you recover using Ctrl+Q.

Let’s say you are going to use Vim editor. Make sure that you are aware of the basic vim commands, and then open a new file with it like this:

What’s your favorite command?

So, I just shared 4 different ways of creating a file in Linux. Personally, I prefer using touch for creating empty file and Vim if I have to edit the file. On a related note, you may want to learn about the file command in Linux that is helpful in determining the actual type of the file.

Which command do you prefer here? Please share your views in the comment section below.

Home » SysAdmin » How to Create a File in Linux Using Terminal/Command Line

Creating a new file in Linux is straightforward, but there are also some surprising and clever techniques.

In this tutorial learn how to to create a file from a Linux terminal.

How to create and edit text file in linux by using terminal

  • Access to a command line/terminal window (CtrlAltF2 or CtrlAltT)
  • A user account with sudo privileges (optional for some files/directories)

Creating New Linux Files from Command Line

Linux is designed to create any file you specify, even if it doesn’t already exist. One smart feature is that you can create a file directly, without needing to open an application first.

Here are a few commands for creating a file directly from the command line.

Create a File with Touch Command

The easiest way to create a new file in Linux is by using the touch command.

In a terminal window, enter the following:

This creates a new empty file named test.txt. You can see it by entering:

The ls command lists the contents of the current directory. Since no other directory was specified, the touch command created the file in the current directory.

How to create and edit text file in linux by using terminal

If there’s already a file with the name you chose, the touch command will update the timestamp.

Create a New File With the Redirect Operator

A redirection operator is a name for a character that changes the destination where the results are displayed.

Right angle bracket >

This symbol tells the system to output results into whatever you specify next. The target is usually a filename. You can use this symbol by itself to create a new file:

This creates a new empty file.
Use the ls command to list the contents of the current directory and find the file test2.txt.

How to create and edit text file in linux by using terminal

Create File with cat Command

The cat command is short for concatenate. It can be used to output the contents of several files, one file, or even part of a file. If the file doesn’t exist, the Linux cat command will create it.

To create an empty file using cat , enter the following:

Note the redirection operator. Typically, the command displays the contents of test2.txt on the screen. The redirection operator > tells the system to place it in the test2.txt file.

Verify that the file was created:

The system should now have test.txt, test2.txt, and test3.txt in the list.

How to create and edit text file in linux by using terminal

Create File with echo Command

The echo command will duplicate whatever you specify in the command, and put the copy into a file.

Enter the following:

Verify that the file was created:

How to create and edit text file in linux by using terminal

You should see the test4.txt file added to the list. Use the cat command to display the contents of the new file:

The system should display Random sample text (or whatever you entered with the echo command.)

How to create and edit text file in linux by using terminal

Create File with printf Command

The printf command works like the echo command, and it adds some formatting functionality. To add a single line of text, enter:

To add two lines of text, separate each line with the \n option:

You can use the cat command on either of these files to display their contents.

Note: To use several terminal instances in a single window manager, consider using Linux screen. It enables additional features and an enhanced command line for working with Linux files.

Using Text Editors to Create a Linux File

All Linux distributions have at least one text editor. Some have multiple editors. Each editor has different strengths and features. This will show you three of the most popular.

Vi Text Editor

Vi is the oldest text editor in Linux. It was created alongside the Linux operating system for directly editing text files. Since it’s unlikely you’ll see a Linux distribution without it, it’s a safe editor to know.

To create a file using Vi, enter the following:

Your screen will change. Now you’re in the text editor. Press the letter i to switch to insert mode, then type a few words to try it out.

To save and exit press Esc 😡 and hit Enter .

How to create and edit text file in linux by using terminal

Vim Text Editor

You may have noticed that the Vi editor wasn’t very user-friendly. Vim is a newer version, which stands for Vi editor, Modified.

Use vim to create a new text file:

How to create and edit text file in linux by using terminal

This screen will look similar to the Vi editor screen. Press i to insert text, and type a few words. Save file and exit by entering:

(Escape, colon wq, then Enter.)

Nano Text Editor

Nano is a newer and much easier text editor to navigate.

Create a new file by entering the command:

By default, Nano puts you directly into editing mode. It also displays a helpful list of commands at the bottom of the screen.

How to create and edit text file in linux by using terminal

Enter some text, then press Ctrl+O to save the changes.

Press Ctrl+X to exit the editor.

Note: Learn all you need about Nano in the Install and Use Nano in Linux article.

Now you have several options to create new files in Linux from the command line. Next, learn how to copy files and directories in Linux to manage your files more efficiently.

In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files.

  1. Using the touch command
  2. Using the cat command
  3. Using redirection operator
  4. Using the echo command
  5. Using the heredoc
  6. Using the dd command

1. Create a file in the Linux/Unix system using the touch command.

The touch command is used to create file/files without any content and update the access date or modification date of a file or directory in the Linux system. This is the simplest way to create a file in Linux/Unix using the terminal.

Syntax:

The general syntax of the touch command is as follows:

A brief description of options available in the touch command.

Option Description
-a Change the access time of a file
-c, –no-create Check file is available or not, if not available then prevent creating a file
-f ignored
-m Change the modification time of a file
-t STAMP Use specified time instead of the current time
–help Display help and exit
–version Display the version information and exit.

Create a file using the touch command in Linux/Unix system.

In this example, using the touch command we can create a file in the Linux system. Before executing the touch command, we will check that how many files available in our current directory using the below command.

After using the below command a new file created newfile.txt in the current directory.

Example :

How to create and edit text file in linux by using terminal

To ensure that the file is created or not we will again execute the ls command to list the directory contents.

2. Create a File in the Linux/Unix system using the cat command.

The cat (concatenate) command is used to create, view, concatenate files in the Linux operating system. The touch command is also used to create a file in a Linux system without content whereas the cat creates files with some content. The cat command reads the content of a file and prompts it.

Syntax

The general syntax of the cat command is as follows:

A brief description of options available in the cat command.

Option Description
-A, –show-all Show all content of a file
-b, –number-nonblank Display number of non-empty lines overrides -n
-n, –number Display number of all output lines
-T, –show-tabs Display this help and exit
–help Display this help and exit
–version Display version information and exit

Create a file with some content using the cat command in Linux/Unix system.

To create a file with some content, we use the cat command and file name after that write some content and press CTRL + C when writing is complete as shown below.

Example :

How to create and edit text file in linux by using terminal

Display contents of the files using the cat command in the Linux system.

The cat command is also used to view the contents of the file. After using the cat command along with the file name contents of the file will be prompt as shown below.

How to create and edit text file in linux by using terminal

3. Create a file in the Linux/Unix system using a redirection operator.

In the Linux/Unix system a redirection operator is also used to create a file.

Example :

How to create and edit text file in linux by using terminal

4. Create a file in the Linux/Unix system using the echo command.

The echo command is also used to create a new file in the Linux system.

Create a new file without contents in the Linux system using the echo command.

To create a file without contents, we use the echo command with a redirection operator followed by the file name as shown below.

Example :

How to create and edit text file in linux by using terminal

Create a new file with some contents in the Linux system using the echo command.

To create a file with some contents, we use the echo command followed by the text, a redirection operator, and the file name as shown below.

How to create and edit text file in linux by using terminal

5. Create a file in the Linux/Unix system using heredoc.

heredoc stands for here document. The heredoc delimiter is a type of redirection. It allows passing multiple lines of input to a command.

The general syntax of heredoc. Important

Create a file with multiple lines of contents using a heredoc delimiter in the Linux system.

To create a file using heredoc, we use the cat command with heredoc delimiter in the Linux system as shown below.

Example :

How to create and edit text file in linux by using terminal

6. Create a file in the Linux/Unix system using the dd command.

The dd command is mainly used to converts and copy files. To check more details about the dd command. We can also create a large file using the dd command.

Create a large file in the Linux system using the dd command.

To create a large file, we use the dd command as shown below.

Walter Glenn is a former Editorial Director for How-To Geek and its sister sites. He has more than 30 years of experience in the computer industry and over 20 years as a technical writer and editor. He’s written hundreds of articles for How-To Geek and edited thousands. He’s authored or co-authored over 30 computer-related books in more than a dozen languages for publishers like Microsoft Press, O’Reilly, and Osborne/McGraw-Hill. He’s also written hundreds of white papers, articles, user manuals, and courseware over the years. Read more.

How to create and edit text file in linux by using terminal

If you’re a keyboard person, you can accomplish a lot of things just using the Linux command line. For example, there are a few easy-to-use methods for creating text files, should you need to do so.

Create a Text File Using the Cat Command

Our first method for creating text files uses the cat command. It’s useful if you want to immediately add some text to your new file.

Just type the following command at the terminal prompt (replacing “sample.txt” with whatever you want to name your file), and then press Enter:

How to create and edit text file in linux by using terminal

After pressing Enter, you are not returned to the terminal prompt. Instead, the cursor is placed on the next line, and you can start entering text directly into your file. Type your lines of text, pressing Enter after each line. When you are done, press Ctrl+D to exit the file and return to the prompt.

How to create and edit text file in linux by using terminal

To verify your file was created, you can use the ls command to show a directory listing for the file:

How to create and edit text file in linux by using terminal

You can also use the cat command to view the contents of your file. Just type the following command at the prompt, and then press Enter:

How to create and edit text file in linux by using terminal

Create a Text File Using the Touch Command

You can also create a text file using the touch command. One difference between using this command and the cat command we covered in the last section is that, while the cat command lets you enter text into your file immediately, using the touch command does not. Another big difference is that the touch command lets you create multiple new files with a single command.

The touch command is handy for quickly creating files you intend to use later.

To create a new file, type the following command at the terminal prompt (replacing “sample.txt” with whatever file name you want to use), and then press Enter:

How to create and edit text file in linux by using terminal

Notice that you are given no indication that the file was created; you’re just returned to the prompt. You can use the ls command to verify the existence of your new file:

How to create and edit text file in linux by using terminal

You can also create multiple new files at once with the touch command. Just add as many extra file names (separated by spaces) as you want to the end of the command:

touch sample1.txt sample2.txt sample3.txt

How to create and edit text file in linux by using terminal

Again, you’re shown no indication that the file was created, but issuing a simple ls command shows that the files are indeed there:

How to create and edit text file in linux by using terminal

And when you’re ready to add text to your new files, you can just use a text editor like Vi.

Create a Text File Using the Standard Redirect Symbol (>)

You can also create a text file using the standard redirect symbol, which is usually used to redirect the output of a command to a new file. If you use it without a preceding command, the redirect symbol just creates a new file. Like the touch command, creating a file this way does not let you enter text into the file right away. Unlike the touch command, though, creating a file using the redirect symbol only lets you create one file at a time. We’re including it for completeness, and also because if you’re just creating a single file, it does offer the least typing.

To create a new file, type the following command at the terminal prompt (replacing “sample.txt” with whatever file name you want to use), and then press Enter:

How to create and edit text file in linux by using terminal

You are given no indication that the file was created, but you can use the ls command to verify the existence of your new file:

How to create and edit text file in linux by using terminal

These three methods should allow you to quickly create text files at the Linux terminal, whether you need to enter text into them immediately or not.

There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article.

1) touch command

This is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The commands are as follows:

As simple as that, just type the word touch followed by the name of the file you like to give it, and Voila! you have created an empty text file inside of a terminal. You can add the file names of the file you would like to create at once with space in between each filename. The command below creates three empty files at once using the touch command and you can create as many files as you like.

How to create and edit text file in linux by using terminal

How to create and edit text file in linux by using terminal

touch command for creating multiple files

2) Standard Redirect Symbol(>)

It is also quite easy to understand the command to create a text file in the terminal with the minimum effort. This works really very well for creating a single text file quickly, but for creating several text files at once it becomes a bit tedious. The command is simply using the standard redirect symbol (>) spacebar followed by the file name.

If you want to create several text files at once, then you can add the redirect symbol after the previous filename and chain the command repeatedly to create multiple empty files.

The above command creates three empty text files. The redirect symbol is quite time-saving if you just want to create a single text file. It gets quite longer than the touch command to create multiple empty text files.

How to create and edit text file in linux by using terminal

Using the redirect symbol for creating files.

3) CAT Command

Now, this method is also quite simple and easy to use. Simply type in CAT with two redirect symbols (>>) and the file name( It is not mandatory to use >> symbols, a user can also use > symbol, but if the user types a pre-existing file by mistake, the existing content in the text file will be overwritten using a single > symbol). This method is a kind of combination of touch and the redirect symbol commands. This method is a bit quirky, so you only prefer using the above two commands if you want to create an empty never edited file. If you want to create and type in the text file straight away, by far this is quite a brilliant method. This will save you time to open up an editor and the command is also quite easy.

The below command creates an empty yet edited file as it prompts the user to create a text file and type in the file at the same time. So, if you do not want to edit the file, simply press CTRL+C and it will simply exit and create an empty file.

But, if you would like to add some text to the file, you can type in after this, like this:

To stop editing and saving in the file, simply type CTRL+C, it will create, save and exit the file. So, this method is quite a time-saving method if you want to edit the text file very quickly. The following command will append the text to the pre-existing file. On the other hand, if you use a single redirect symbol(>) it will overwrite the content of the file, so you only prefer using double redirect symbols for safety reasons.

How to create and edit text file in linux by using terminal

Using cat command to create the file.

How to create and edit text file in linux by using terminal

Using cat command to create and write a file.

4) Using echo / printf

This is also similar to cat command, but it is very flexible comparatively. The following command is usually used for printing text on the terminal, but we can also use it to write to a file or make an empty file. The echo command is used along with the double redirect symbols (single > will also work) followed by the filename.

If you want to create multiple files at a time, you can chain up the command as in previous methods.

We can also add functionality to the echo command to quickly create and write to the text file just like cat command.

echo -e ‘This will be the text in the file \n this is the new line’ >> file.txt

The above command can be highly customizable as it uses the properties of the echo command to make it quite versatile to write the text in the file, but using a new line character every time can be annoying as well.

How to create and edit text file in linux by using terminal

using the echo command to create files.

How to create and edit text file in linux by using terminal

Using echo command to create and write to a file.

Similar to the echo command, we have the printf command as well. The print command does the same thing as the echo command but in a C style rather than shell-style editing.

printf “This is some text here \n The second line \n The third line” >> file.txt

The print command does some pretty C-like things, such as the newline character and the variable names can be used as well, but that is not for a simple text file. But still, the printf command can be useful in a lot of cases to edit files on the go.

How to create and edit text file in linux by using terminal

Using printf command to create files.

How to create and edit text file in linux by using terminal

Using printf to create and write to files.

5) Any command-line text editor(Vim, nano)

This is the most time-consuming method and not the fastest, yet the method can be useful for Linux beginners. If you want to heavily edit a text file, you can use command-line text-editors such as Vim, nano, and there are other options as well. But most people use nano as it is simple to use and quick to go. Vim can also be used but most beginners find it difficult to use, so we’ll stick with nano for this example.

We are now in the nano editor(or vim). You can type in the stuff you require and simply type CTRL+S to save and CTRL+X to exit. In Vim it is a bit different. We won’t make a vim guide here, so you can check out the ‘Nano text editor in Linux‘ or ‘Getting started with vim‘ article from geeks for geeks.

How to create and edit text file in linux by using terminal

Using Nano to create and write files.

So that wraps up the methods for quickly creating a text file or writing to the file. Each method can be used differently depending on the situation and the case used. Not every method will be the fastest, yet these were some of the fastest ways to create a Text File Using the Command Line in Linux.

How to create and edit text file in linux by using terminal

One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover a few of the many text editors available for Linux.

  • GUI text editors.
  • Terminal-based text editors.
  • Redirecting command output into a text file.

GUI text editors

This section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.

If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML or CSS.

NEdit

How to create and edit text file in linux by using terminal

NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.

The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:

For more information, see our NEdit information page.

Geany

How to create and edit text file in linux by using terminal

Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.

The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:

Gedit

How to create and edit text file in linux by using terminal

Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.

The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:

Terminal-based text editors

If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some of the most popular:

How to create and edit text file in linux by using terminal

pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. (“pico” is a scientific prefix for very small things.)

The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.

On Debian and Ubuntu Linux systems, you can install pico using the command:

How to create and edit text file in linux by using terminal

nano is the GNU version of pico and is essentially the same program under a different name.

On Debian and Ubuntu Linux systems, nano can be installed with the command:

How to create and edit text file in linux by using terminal

vim, which stands for “vi improved,” is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.

On Debian and Ubuntu Linux systems, vim can be installed using the command:

emacs

How to create and edit text file in linux by using terminal

emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.

On Debian and Ubuntu Linux systems, emacs can be installed using the command:

Redirecting command output into a text file

When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.

Creating an empty file with the touch command

To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the contents of the file had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.

For instance, the command:

The above command creates a new, empty file called myfile.txt if that file does not already exist.

Redirecting text into a file

Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.

For instance, the echo command is used to “echo” text as output. By default, this goes to the standard output — the screen. So the command:

The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:

The above command puts the text “Example text” into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.

Be careful when redirecting output to a file using >. It will overwrite the previous contents of the file if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.

Here’s an example using another command:

The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.

Redirecting to the end of a file

The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:

Being a Terminal-savvy person, you may always be looking for ways to ditch the mouse. Creating a text file is one task for which you can depend only on your keyboard on an Ubuntu system. Three commands from the Linux command line are at your service for creating text files. These include:

  • The cat command
  • The touch command
  • The standard redirect symbol

Let us explore these commands in this article to create some sample text files. The commands and procedures mentioned in this article have been run on an Ubuntu 20.04 LTS system. Since we will be creating the text files using Ubuntu command line-the Terminal; you can open it either through the system Dash or the Ctrl+Alt+T shortcut.

The cat Command

The cat command is very helpful when dealing with text files in Linux. It helps you in achieving three basic purposes:

  • Creating a text file
  • Printing contents of a text file in your Terminal
  • Printing contents of a text file to another text file

Here, we will explore the first use of the cat command; creating a text file through the command line.

Enter the following command in your Terminal:

After entering this command, the next prompt will not appear; rather the cursor will display for you to enter the text for the file you just created.

In this example, I have created a text file through the following command and then entered some sample text:

Once you have entered all the text, hit enter to move to the next line and then use the Ctrl+D control to tell the system that you are done with entering the text. The usual command prompt will then appear for you to move on with further operations.

You can then use the ls command to see that your newly created text file will be there in the system.

Through the cat command, you can then view the contents of the file as follows:

You can see that the cat command shows the text I wrote while creating my sample file:

The touch command

Another way of quickly creating a text file through the Terminal is by using the touch command. The touch command, however, does not let you enter text in the file at the time of creation. After creating the file, you can enter the text through your favorite text editor. You might prefer the touch command over the cat command in one scenario; when you want to create multiple files at once through one command.

Let us first see how to create a single file first through the Linux touch command:

Use the ls command to see if the recently created file now exists on your system.

Create multiple files at once through the touch command

As mentioned above, the touch command takes the lead on the cat command on the basis that you can create multiple files simultaneously through the former. Use the following syntax to do so:

$ touch filename1.txt filename2.txt filename2.txt ….

For example, in the following command, I have created three files at once through the touch command:

I also checked the presence of the three files through the ls command in the above example.

If you want to edit any of the files you created through the touch command, you can use any of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I created. I used the following command to open the file through the Nano editor.

I then entered the text and saved it by pressing Ctrl+X and then by hitting Enter.

The touch command can also be used to change the access and modification time of a file.

Change the access time of a file:

Set the modification time of a file:

touch -m samplefile.txt

You can view the access and modification time of files with the stat command:

Using the Standard Redirect Symbol

The standard redirect symbol is usually used when redirecting the output of a command to a file. However, it can also be used to create a single text file. The only difference is that while creating a new file we do not specify any command before the redirect symbol.

The difference between using the standard redirect symbol for creating a text file is that, unlike the cat command, you can not enter text this way. Also, unlike the touch command, you can only create one file at a time through the redirect symbol.

Use the following syntax in order to create a text file through this symbol:

to create file” width=319 height=45 data-ezsrcset=”https://vitux.com/wp-content/uploads/2018/10/word-image-129.png 319w,https://vitux.com/wp-content/uploads/2018/10/word-image-129-300×42.png 300w” sizes=”(max-width: 319px) 100vw, 319px” ezimgfmt=”rs rscb10 src ng ngcb10 srcset” data-ezsrc=https://vitux.com/wp-content/uploads/2018/10/word-image-129.png>

You can then use the ls command to see if the newly created text file now exists on your system.

You can enter text in the file through your favorite text editor. In the following example, I am using the Vim editor to edit the file through the following command:

When you save and exit the file, your text file will have those contents saved.

Through this article, we have learned three basic ways to create text files quickly through the Linux command line. You can now avoid the mouse and use only the keyboard in order to perform the simple task of creating a text file in Ubuntu.

  • ← 4 Ways to Log Out of a Ubuntu Desktop Session
  • Uninstall or remove a package using the terminal on CentOS →

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn

The vi editor (visual editor) is the default text editor in many UNIX and UNIX like Operating Systems including Linux. vi editor is an advanced text editor with many features.

vi editor and has two modes of operation:

• Command mode: In command mode, commands can be issued to the vi editor which cause action to be taken on the file.

• Insert mode: In insert mode the file can be edited.

File opening, closing and exit commands

• To open a file – vi filename (If the file is not existing, new file can be created)

• To recover the file from a crash run vi -r filename

•To go to insert mode, use the command “i” or “a” (the characters typed in will be inserted after the current cursor position).

• To go back from insert mode to command mode in vi editor type “Esc”.

• To Exit vi editor (from the command mode of the vi editor run the following commands)

Command

Description

quit vi editor, with writing out modified file to disk.

quit vi editor, with writing out modified file to disk.

quit (or exit) vi editor

quit vi editor even though latest changes have not been saved

vi editor will write file as file, leaving original untouched

Moving the cursor in vi editor

Command

Description

move cursor down one line in vi editor

move cursor up one line in vi editor

move cursor left one character in vi editor

l or right-arrow

move cursor right one character in vi editor

move cursor to start of current line in vi editor

move cursor to end of current line in vi editor

move cursor to beginning of next word in vi editor

move cursor back to beginning of preceding word in vi editor

move cursor to first line in file in vi editor

move cursor to line n in vi editor

move cursor to last line in file in vi editor

Screen Movement Commands in vi editor

Command

Description

move current line to top of screen in vi editor

move current line to the middle of screen in vi editor

move current line to the bottom of screen in vi editor

move forward one screen in vi editor

move backward one screen in vi editor

move down (forward) one half screen in vi editor

move up (back) one half screen in vi editor

redraws the screen in vi editor

Cut, Copy and Paste operation commands in vi editor

Command

Description

delete single character; nx deletes n characters (n – any number) in vi editor

deletes the character to the left of your cursor in vi editor.

delete word; ndw deletes n words (n – any number) in vi editor

delete line; ndd deletes n lines (n – any number) in vi editor

delete word and start in insert mode (i.e. change word) in vi editor

delete line and start insert mode (change line) in vi editor

delete character and start insert mode (change character) in vi editor

delete the remainder of the line, starting with current cursor position in vi editor

copy (yank, cut) the current line into the buffer; nyy copy (yank, cut) the next n lines (n – any number) in vi editor

Copy a word; nyw copies n words (n – any number) in vi editor

Other Useful commands in vi editor

Command

Description

Convert complete line to Lower-case in vi editor

Convert complete line to Upper-case in vi editor

Undo last change in vi editor

redo in vi editor

Search for text (forwards) in vi editor

Repeat forward search in vi editor

Search for text (backwards) in vi editor

Repeat previous search backwards in vi editor

returns line number of current line at bottom of screen in vi editor

returns the total number of lines at bottom of screen in vi editor

How to create and edit text file in linux by using terminal

In this tutorial, I will show you how to create a file from a Linux terminal. There are many text editors like (vim, nano, vi) and many commands like (cat, echo, printf, touch) to create a file in the Linux operating system via command line. Here will explain the following linux tools.

1) Create a file with touch command

We will use touch command with any extension to create file, this command will create an empty file touch.txt in your current directory as an example below.

To see the file type command below.

2) Create a file with cat command

We will use cat command to create file, this command will create an empty file cat.txt in your current directory as an example below, but you must add text in the file.

Add the text below.

To save the file hit Ctrl + d , and to see the file type command below.

To open the file, we will use cat command to open it.

3) Create a file with echo command

We will use echo command to create file, this command will create a file echo.txt in your current directory as an example below, but you should add text in the line command.

To see the file,type command below.

To open the file, we will use cat command to open it.

4) Create a file with printf command

We will use printf command to create file, this command will create a file printf.txt in your current directory as an example below, but you should add text in the line command.

To see the file type command below.

To open the file, we will use cat command to open it.

5) Create a file with nano text editor

To create a file using nano text editor, first install it, after that type command below and the text editor will be opened to adding text.

Add the text below.

To save the file type Ctrl + x and type y , to see the file type command below.

To open the file, We will use nano command to open it.

6) Create a file with vi text editor

To create a file using vi text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.

Add the text below.

To save the file and exit hit Esc after that :wq , To see the file type command below.

To open the file, we will use vi command to open it.

7) Create a file with vim text editor

To create a file using vim text editor, type command below and the text editor will open the file, but you can’t add any text before converting it to insert mode by typing i character.

Add the text below.

To save the file and exit hit Esc after that :wq , to see the file type command below.

To open the file, we will use vim command to open it.

Conclusion

In this tutorial, we learned the different ways to create a file from Linux terminal. Hope you enjoyed reading and please leave your comments in the below comment section.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 7 years ago .

I’m just trying to review basic terminal commands. Having said that, how do I create a text file using the terminal only?

How to create and edit text file in linux by using terminal

5 Answers 5

You can’t use a terminal to create a file. You can use an application running in a terminal. Just invoke any non-GUI editor ( emacs -nw , joe , nano , vi , vim , …).

If you meant using the command line, then you are asking how to create a file using the shell. See What is the exact difference between a ‘terminal’, a ‘shell’, a ‘tty’ and a ‘console’?

The basic way to create a file with the shell is with output redirection. For example, the following command creates a file called foo.txt containing the line Hello, world.

If you want to write multiple lines, here are a few possibilities. You can use printf .

You can use a string literal containing newlines.

Another possibility is to group commands.

On the command line, you can do this more directly with cat . Redirect its output to the file and type the input line by line on cat ‘s standard input. Press Ctrl + D at the beginning of the line to indicate the end of the input.

In a script you would use a here document to achieve the same effect:

If you just want to create an empty file, you can use the touch command: it creates the file if it doesn’t exist, and just updates its last-modified date if it exists.

i.e. open foo.txt for appending, but write 0 bytes to it — this creates the file but doesn’t modify it. Unlike touch , this doesn’t update the file’s last-modified date if it already existed.

To create an empty file, and remove the file’s content if the file already existed, you can use

I need to create a text file (unless it already exists) and write a new line to the file all using bash.

I’m sure it’s simple, but could anyone explain this to me?

How to create and edit text file in linux by using terminal

6 Answers 6

You can now choose to sort by Trending, which boosts votes that have happened recently, helping to surface more up-to-date answers.

Trending is based off of the highest score sort and falls back to it if no posts are trending.

Creating a text file in unix can be done through a text editor (vim, emacs, gedit, etc). But what you want might be something like this

That will put the text ‘insert text here’ into a file myfile.txt. To verify that this worked use the command ‘cat’.

If you want to append to a file use this

If you’re wanting this as a script, the following Bash script should do what you want (plus tell you when the file already exists):

If you don’t want the “already exists” message, you can use:

Edit about using:

Save whichever version with a name you like, let’s say “create_file” (quotes mine, you don’t want them in the file name). Then, to make the file executatble, at a command prompt do:

The $1 is a special shell variable which takes the first argument on the command line after the program name; i.e. $1 will pick up NAME_OF_NEW_FILE in the above usage example.

Assuming you mean UNIX shell commands, just run

echo prints a newline, and the >> tells the shell to append that newline to the file, creating if it doesn’t already exist.

In order to properly answer the question, though, I’d need to know what you would want to happen if the file already does exist. If you wanted to replace its current contents with the newline, for example, you would use

EDIT: and in response to Justin’s comment, if you want to add the newline only if the file didn’t already exist, you can do

At least that works in Bash, I’m not sure if it also does in other shells.

Guides, tutorials, reviews and news for System Administrators.

Create and edit text files – RHEL 8 RHCSA

Being able to create and edit text files in Red Hat Enterprise Linux (RHEL) 8 is a simple yet important task.

In these examples we will cover the Red Hat Certified System Administrator (RHCSA) objective “Create and edit text files”, both through the graphical user interface and command line.

Graphical User Interface

If your installation of Red Hat Enterprise Linux (RHEL) 8 comes with the graphical user interface (GUI) installed, the default, you can open the text editor by following these steps:

  1. Click “Activities” towards the top left hand corner of the screen.
  2. Select the icon towards the bottom from the bar on the left, allowing you to see more applications.
  3. Click on “Text Editor”

How to create and edit text file in linux by using terminal

Note that this process may differ depending on what display manager your installation is using, this example was done with a fresh default installation of RHEL 8.

Once opened, you’ll have access to a standard text editor where you can write content and then choose where to save the file.

How to create and edit text file in linux by using terminal

Command Line

The GUI may not always be available, for instance if you’re editing a text file through SSH, so it is important to know how to create and edit text files through the command line interface (CLI).

In this example we’ll be using the ‘vi’ text editor, as it is always available by default within RHEL – perfect for the RHCSA exam. It’s not very beginner friendly unfortunately, so we’ll only be covering the basics of creating and editing text files. Here’s a great game which will help you learn and get better with Vim (vi-improved, basically a newer more modern version of vi).

To edit an existing file, simply enter ‘vi ’ which will open up the file in the vi text editor. If ‘filename’ does not exist, once you save your changes the new file will be created.

In the example below we are creating a new file called ‘testing’ in the current working directory (the home directory of ‘user’, as identified by the ‘

’ symbol at the end of the bash shell).

How to create and edit text file in linux by using terminal

This is what the vi text editor looks like by default.

How to create and edit text file in linux by using terminal

At this point vi is in command mode, you cannot yet start typing. To enter text, press ‘i’ to enter insert mode. Insert mode is identified by the – – INSERT – – text down the bottom left corner.

How to create and edit text file in linux by using terminal

With insert mode activated, we can now edit the text file by typing normally.

How to create and edit text file in linux by using terminal

Once complete, press the escape key to leave insert mode and go back to command mode. Next we want to save the file and exit vi. To do this, enter ‘:wq’ as shown down the bottom left corner of the screen. The ‘w’ means write the file, so save it, and the ‘q’ means quit vi.

How to create and edit text file in linux by using terminal

You can of course just use ‘w’ only to save the file without exiting vi. You could also use ‘:q!’ to quit without saving the changes. Combining these into a ‘:wq!’ will save the changes, overwrite the files even if it’s read only if the user account has the privileges and exit vi.

Finally we can check that our new file exists and contains the contents expected.

How to create and edit text file in linux by using terminal

That’s it, you should now have a basic understanding of how to create and edit text files in RHEL 8 for the RHCSA exam. Make sure you practice these examples yourself to get a full understanding on how the commands work, new Linux users often have trouble escaping vi!

This post is part of our Red Hat Certified System Administrator (RHCSA) exam study guide series for Red Hat Enterprise Linux (RHEL) 8. For more RHCSA related posts and information, see our full RHCSA 8 study guide.

Chris Hoffman is Editor-in-Chief of How-To Geek. He’s written about technology for over a decade and was a PCWorld columnist for two years. Chris has written for The New York Times and Reader’s Digest, been interviewed as a technology expert on TV stations like Miami’s NBC 6, and had his work covered by news outlets like the BBC. Since 2011, Chris has written over 2,000 articles that have been read nearly one billion times—and that’s just here at How-To Geek. Read more.

How to create and edit text file in linux by using terminal

Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you’ll have to edit a text file on a system that doesn’t include a friendlier text editor, so knowing Vi is essential.

Unlike Nano, an easy-to-use terminal text editor, Vi doesn’t hold your hand and provide a list of keyboard shortcuts on the screen. It’s a modal text editor, and it has both an insert and command mode.

Getting Started

Vi is a terminal application, so you’ll have to start it from a terminal window. Use the vi /path/to/file command to open an existing file with Vi. The vi /path/to/file command also works if the file doesn’t exist yet; Vi will create a new file and write it to the specified location when you save.

How to create and edit text file in linux by using terminal

Remember to use sudo if you want to edit a system file. So, for example, you’d type sudo vi /etc/fstab if you wanted to edit your fstab file. Use the su command instead if you’re using a non-Ubuntu version of Linux that doesn’t use sudo.

Command Mode

This is what you’ll see when you open a file in vi. It looks like you can just start typing, but you can’t. Vi is a modal text editor, and it opens in command mode. Trying to type at this screen will result in unexpected behavior.

How to create and edit text file in linux by using terminal

While in command mode, you can move the cursor around with the arrow keys. Press the x key to delete the character under the cursor. There are a variety of other delete commands — for example, typing dd (press the d key twice) deletes an entire line of text.

You can select, copy, cut and paste text in command mode. Position the cursor at the left or right side of the text you want to copy and press the v key. Move your cursor to select text, and then press y to copy the selected text or x to cut it. Position your cursor at the desired location and press the p key to paste the text you copied or cut.

How to create and edit text file in linux by using terminal

Insert Mode

Aside from command mode, the other mode you need to know about is insert mode, which allows you to insert text in Vi. Entering insert mode is easy once you know it exists — just press the i key once after you’ve positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands.

How to create and edit text file in linux by using terminal

Once you’re done in insert mode, press the escape key to return to command mode.

Saving and Quitting

You can save and quit vi from command mode. First, ensure you’re in command mode by pressing the escape key (pressing the escape key again does nothing if you’re already in command mode.)

Type :wq and press enter to write the file to disk and quit vi. You can also split this command up — for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.

How to create and edit text file in linux by using terminal

Vi won’t let you quit if you’ve modified the file since you last saved, but you can type :q! and press enter to ignore this warning.

How to create and edit text file in linux by using terminal

Check out Nano if you’re looking for an easier-to-use terminal text editor. Most Linux distributions come with Nano installed, but embedded systems and other stripped-down environments often only include Vi.

How to create and edit text file in linux by using terminal

The cat command is a very popular and versatile command in the ‘nix ecosystem. There are 4 common usages of the cat command. It can display a file, concatenate (combine) multiple files, echo text, and it can be used to create a new file.

Displaying a file

The most common use of the cat command is to output the contents of a file. The following is an example that you can try.

In this simple example, we’re using a combination of echo and a redirect to create a file containing “Dance, Dance”. We then use the cat command to display the contents.

The output is as follows:

How to create and edit text file in linux by using terminalThe output of the example commands

(Con)cat

The previous example is actually a specific case of the cat command’s main function, which is to concatenate files for display. If we use the command the same way, but give it two or more files, then it outputs the concatenation for the files.

If we run the following commands:

The output is the contents of the 1st file, followed by the contents of the 2nd file. You can give cat many files and it will concatenate (combine) all of them. Notice however, that the cat command automatically inserts a line break between outputs.

How to create and edit text file in linux by using terminalThe output of two files concatenated

cat also provides some switches to to do things such as show non-print characters (-v), or number your lines (-n). A complete breakdown can be found in the man pages.

Echoing

This is a less common usage of cat , but is the basis for the next section. If you run the cat command with no commands, cat will run in interactive mode and echo anything you type until you exit the command.

How to create and edit text file in linux by using terminalRunning cat in interactive mode

In the example here, I’ve typed a single word per line. Each time I hit enter, the line was echoed.

You can also pipe text to cat , in which case that text is echoed. For example:

This will result in the following output:

How to create and edit text file in linux by using terminalPiping text into cat

Creating a File

In the previous examples, we’ve been using the echo command redirected to a file to create new files. Cat can be used in a similar way. In fact, we can use cat ‘s concat and echo functionality to create files.

We can create a file containing the concatenation of multiple files like this:

In the above example, we’re creating 3 files using echo , combining the 3 files into one using cat , and then displaying the new combined file using cat .

How to create and edit text file in linux by using terminalThe result of the above commands. We’ve created 3 files, then combined them into a single file using cat

We can also use cat ‘s interactive mode to create a file with the text that we type into the terminal.

How to create and edit text file in linux by using terminal

Each time you hit enter, it commits the text to the file. If you have uncommitted text and exit, it won’t be captured in the file.

This is a fantastic way to create a file quickly with the ability to enter the content of the file.

Using Touch to create a file instead

Sometimes you just need a file to exist. As an alternative to using cat to create a file, you can use the touch command.

The touch command was designed to update the modified timestamp of a file, but is commonly used as a quick way to create an empty file. Here is an example of that usage:

The touch command can create multiple files, update the modification and/or creation timestamps, and a bunch of other useful things. The full man pages can be found here.

Touch is commonly used to ensure that a file exists, and is a great command if you need an empty file quickly.

Summary

Cat is a very useful command. You can use it to create, display, and combine text files very quickly and easily.

If you only need a file to exist, but don’t mind (or require) it being empty, using touch is a great alternative.

Hughie Coles is a lead developer at Index Exchange. He writes about software architecture, scaling, leadership, and culture. For more of his writing, check out his blog on medium.

I’m Hughie, A software developer from Toronto. I’m currently a lead developer at Index Exchange. I write about scaling out, leadership, culture, and general dev stuff.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons – all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Search

How to create and edit text file in linux by using terminal

If you’re searching for info related to the VI editor, this article is for you. So, what’s VI editor? VI is a text editor that’s screen-oriented and the most popular in the Linux world. The reasons for its popularity are 1) availability for almost all Linux distros, 2) VI works the same throughout multiple platforms, and 3) its user-friendly features. Currently, VI Improved or VIM is the most used advanced counterpart of VI.

To work on the VI text editor, you have to know how to use the VI editor in Linux. Let’s find it out from this article.

Modes of VI Text Editor

VI text editor works in two modes, 1) Command mode and 2) Insert mode. In the command mode, users’ commands are taken to take action on a file. The VI editor, usually, starts in the command mode. Here, the words typed act as commands. So, you should be in the command mode while passing a command.

On the other hand, in the Insert mode, file editing is done. Here, the text is inserted into the file. So, you need to be in the insert mode to enter text. Just type ‘ i ’ to be in the insert mode. Use the Esc key to switch from insert mode to command mode in the editor. If you don’t know your current mode, press the Esc key twice. This takes you to the command mode.

Launch VI Text Editor

First, you need to launch the VI editor to begin working on it. To launch the editor, open your Linux terminal and then type:

And if you mention an existing file, VI would open it to edit. Alternatively, you’re free to create a completely new file.

VI Editing Commands

You need to be in the command mode to run editing commands in the VI editor. VI is case-sensitive. Hence, make sure you use the commands in the correct letter case. Also, make sure you type the right command to avoid undesired changes. Below are some of the essential commands to use in VI.

i – Inserts at cursor (gets into the insert mode)

a – Writes after the cursor (gets into the insert mode)

A – Writes at the ending of a line (gets into the insert mode)

o – Opens a new line (gets into the insert mode)

ESC – Terminates the insert mode

u – Undo the last change

U – Undo all changes of the entire line

D – Deletes the content of a line after the cursor

R – Overwrites characters from the cursor onwards

r – Replaces a character

s – Substitutes one character under the cursor and continue to insert

S – Substitutes a full line and start inserting at the beginning of a line

– Changes a character’s case

dd – Deletes the line

3dd – Deletes 3 lines

dw – Deletes a word

4dw – Deletes 4 words

x – Deletes a character at the cursor

cw – Changes the word

Commands for Moving within a File

For moving within a file, you should be in the command mode. Also, arrow keys can be used to navigate. Below are the commands used to navigate within a file.

k – Moves cursor up

j – Moves cursor down

l – Moves cursor right

h – Moves cursor left

Commands for Saving and Closing a File

To exit the text editor and save the changes to a file, you need to be in the command mode. Below are the commands to use for saving and closing a file in the editor.

Shift+zz – Saves a file and exits

:w – Saves a file and keeps the file open

:q – Exits without saving

:wq – Saves a file and quits

Summary

Through this article, we learned about the VI text editor. Here’s a summary of all the above facts discussed above. The VI text editor is the most used and popular Linux text editor. You can get it in almost any Linux distribution. There are two modes, Insert and Command. Command mode is used to take the user commands, while the Insert mode is used to edit text. In the earlier sections, we learned the commands used to work on a file. So, learning to work on the VI text editor will definitely give you advantages in the time of editing files and creating scripts.

Suparna is a freelance writer who writes about Linux including tips, tricks, and how-tos.

PowerShell can be used to perform different windows operations, such as creating folders, directories. Similarly, text files can also be handled using PowerShell; we can edit text files to append or remove the content from the text files.

We will demonstrate various ways to create and edit text files using PowerShell:

How to create and edit files using PowerShell

The following steps will enable you to create text files using PowerShell; moreover, this section also elaborates on different ways to edit text files.

Step 1: How to create a text file using PowerShell

There are two possibilities to consider while creating a new text file in PowerShell:

Possibility 1: Create the text file in the Present Working Directory (PWD): to accomplish this, execute the following command to make a new file: the order given below will create a text file named “file1” in the current directory:

How to create and edit text file in linux by using terminal

Possibility 2: If you want to get the new file in another folder, you must specify the complete path; and make sure that the targeted directory exists; Otherwise, you will be unable to make changes to the unknown directory or drive. The command given below will create a new text file “file2” in the targeted directory of drive “E“.

How to create and edit text file in linux by using terminal

Once you have created a text file, you are ready to move to step 2.

Step 2: How to write inside text files using PowerShell

It is observed that each track has its pros and cons, so users must choose according to their requirements. Following are three methods that you can consider writing inside a text file:

Replacing the Data: If you want to replace the existing content of the text file with a new one, you must go for Method 1.

Appending the Content: However, if you want to add some lines to the existing content, you must follow Method 2.

Using nano editor to edit text files in PowerShell: If you want to add or remove the content of a text file multiple times, then it is recommended to consider Method 3.

Method 1: Replacing the Data
Before going to set the content, first; read the file using the following command:

After execution, the output is shown below:

How to create and edit text file in linux by using terminal

If you want to write in a text file, execute the command given below to replace the text in the file1:

How to create and edit text file in linux by using terminal

Once your command is executed successfully, read the content of your file to check whether the text has been replaced or not. The command given below will print the content of “file1.txt

How to create and edit text file in linux by using terminal

You will notice that the text has been replaced:

Method 2: Appending the Content
Contrary to the first method, the second method will add the content to the existing data in the file; the previous content will also be available in the file:

To append the text, execute the following command: the command given below will add the text written in single quotes to file1.txt.

How to create and edit text file in linux by using terminal

After executing the above command, check whether the text has been added or not; to do that, run the following command to check. You will find that the line written in single quotes is now added as a new line to file1.txt.

How to create and edit text file in linux by using terminal

Method 3: Using nano editor to edit text files in PowerShell
Another way to make changes to text files is using the “nano” editor in PowerShell:

The “nano” editor is not available to access in PowerShell; you must install the package. Before this installation, you must ensure that you are running the PowerShell with Administrator privileges; otherwise, the installation will not be successful. Once you have run the PowerShell as Administrator; you can continue further:

Firstly, you have to install the “chocolatey” package; the “chocolatey” package supports the nano editor. So, it is necessary to install it before adding the nano editor, and you can install it using the command given below:

How to create and edit text file in linux by using terminal

After successful installation of the “choco” package; now, install the “nano” editor with the help of the command given below:

During installation, it will ask to press “Y” to proceed installation further:

How to create and edit text file in linux by using terminal

Once the above commands have been executed successfully, restart the PowerShell:

After that, open the targeted directory of your text file in PowerShell: Once you have reached the directory; check for the text file if it exists or not; to do this, run the following command in your PowerShell:

How to create and edit text file in linux by using terminal

After that, you can edit your text file using the “nano” editor with the help of the command given below.

How to create and edit text file in linux by using terminal

After execution, you will find that your text file will be opened in an editor, where you can add, delete, or replace the file’s content.

How to create and edit text file in linux by using terminal

Nano editor enables you to edit, delete and append text the same as other text editors. For instance, if you want to append text, start writing after the existing text. After appending, press “Ctrl+X” to exit from the editor; after this action, press “Y” to save changes or press “N” to discard changes. Moreover, you can remove the existing content and save the changes as described above.

Do you know how to create a file in Linux? If not, then this article will help you with Linux create file commands.

List of content you will read in this article:

Anyone who uses Linux operating system daily should be familiar with how to create a text file in Linux or Linux create file command. 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 how to create a file in Linux.

How to Create a Text File in Linux? [Varius Methods]

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

1. 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:

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

touch filename1.txt filename2.txt filename3.txt

2. 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. When using redirection to create a file, you should take care not to delete a significant existing file. To make an empty zero-length buffer, specifically indicate the name of the file you want to construct after the redirection operator:

3. Using the Cat Command to Create Text File

Cat command is mostly used to read and concatenate files, but it can also 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 CTRL +D keys to save the files.

4. 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:

5. 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 you need to generate 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 filename.txt
Some lines
Some other lines
EOF

Variables, special characters, and instructions will all be used in the heredoc’s body.

6. How to Create a Large Text File

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

6.1 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

Conclusion

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 that our described information can help you create a new file in Linux without having any trouble. if you have any other suggestions for creating a new file, you can comment via the below comment section.

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

If you manage to take a deeper glimpse inside the ecosystem of the Linux operating system environment, you will discover that its built-in commands are sufficient enough to solve most of our computing problems.

One such problem is the need to find and replace text, word, or string in a file especially when you are in a server environment. A solution to this problem lets you handle nagging issues like updating the “/etc/apt/sources.list” file after a successful Linux system upgrade.

Creating the Test Text File in Linux

Create a text file with a name like “test.txt” and populate it with some text, word, or string characters. Let this file be on the same path as your terminal instance.

How to create and edit text file in linux by using terminalCreate Text File in Linux

We will be using the cat command to flexible note the changes on our created test file.

How to create and edit text file in linux by using terminalView Text File in Linux

Using Sed to Find and Replace Text, Word, or String in a File

The power of this stream editor is in its easiness in accomplishing basic input streams transformation. The sed command offers a quick, easy, and timeless approach in finding and replacing targeted text, words, or string patterns in a file.

Find and Replace the First Occurrences of Text, Word, or String in File

From our created test file, we are going to update all the instances of the word “LinuxShellTips” with the alternative “this site”. The syntax of the sed command we will be using to accomplish this simple task is as follows:

With reference to the above sed command usage and syntax, we can replace our test file’s “LinuxShellTips” term with “this site” term as follows:

We can now use the cat command to preview the above-proposed changes to our test file to note if any substantial edits took place.

How to create and edit text file in linux by using terminalFind and Replace Word in Linux Using Sed Command

As you can see, the first two occurrences of the word “LinuxShellTips” have been replaced with the phrase “this site”.

Find and Replace All Occurrences of Text, Word, or String in File

Despite the above command having replaced all the targeted word patterns in our test file, its usage tends to be selective in most circumstances. The command works best for small files because, in big files, only the first few occurrences of a targeted word pattern might benefit from its ‘find and replace’ prowess.

To find and replace all occurrences of a word pattern in any editable file, you should adhere to the following sed command syntax.

As you have noted, the ‘g’ in the above find & replace command syntax acts as a global variable so that all the global occurrences of a term in a targeted file are considered.

How to create and edit text file in linux by using terminalFind and Replace All Occurrences of Word in File

The above command finds all the global occurrences of the term “this site” and replaces it with the term “LinuxShellTips”.

Using Awk to Find and Replace Text, Word, or String in File

If you are familiar with the awk command-line utility, then you know that its powerful scripting language metrics make it a pro at text processing. Linux system administrators and professional users find this tool effective in data extraction and reporting.

The awk command syntax for a simple find-and-replace operation looks like the following snippet:

In the above syntax, awk will substitute THE_OLD_TERM from THE_NEW_TERM in the TARGETED_FILE and print the resulting file content on the system terminal.

Let us take a practical approach:

How to create and edit text file in linux by using terminalFind and Replace Word in Linux Using Awk Command

With the dynamic functionalities of both sed and awk command tools, you should now find, replace, and overwrite text, word, or string patterns in a targeted file. These tools give a system user the needed flexibility while on a command-line interface.

Although you can edit text files in cPanel (if your account includes cPanel), it is often quicker and easier to do so from the command line. This article describes how to use the Nano and Vim editors to edit text files from the command line.

The Nano editor is probably easier for beginners to use initially. On the other hand, the Vim editor is in more widespread use, and has a long list of features. Try both editors, and use whichever one you feel more comfortable with.

Using the Nano editor

Editing files with the Nano text editor is easy. To open a file in Nano, type the following command at the command line:

Replace filename with the name of the file that you want to edit.

To edit the file, just start typing your changes. To navigate around the file, you can use the arrow keys on your keyboard. If the file’s contents are too long to fit on the screen, you can press Ctrl+V to move forward a page, and Ctrl+Y to move back a page.

When you are ready to save your changes, press Ctrl+O, verify the filename, and then press ENTER. To exit Nano, press Ctrl+X.

You can access Nano’s online help at any time by pressing Ctrl-G.

Using the Vim editor

To open a file for editing in Vim, type the following command at the command line:

Editing files with Vim is not as intuitive as with Nano. You can’t just start typing your changes, because Vim starts in normal mode. In normal mode, anything you type on the keyboard is interpreted as a potential command, not changes to the text.

To make changes to the text, you must enter insert mode. To do this, type i . Note that the status line at the bottom of the screen changes to —INSERT— . You can now make changes to the file. To navigate around the file while you are in insert mode, use the arrow keys and Page Up/Page Down keys.

To return to normal mode, press ESC. Note that the —INSERT— status line at the bottom of the screen goes blank. Now you can type commands to save your changes, search for text, and so on.

To write your changes without exiting Vim, type :w and then press ENTER. To exit Vim, type :q and then press ENTER. To write your changes and exit Vim at the same time, type :wq and then press ENTER.

More Information

This article is a very basic introduction to using the Nano and Vim text editors. Both of these editors, and Vim in particular, have many more features and customizations available:

By How to create and edit text file in linux by using terminalBalaji S

How to create and edit text file in linux by using terminal

Introduction to Linux Create File

In Linux Operating System creating a file is an important task since the user or administrator may be required to create a file multiple times in a single day. A file can be created either using the command line or by using the file manager. In this article, we will discuss multiple ways of creating a file. Commands like touch, echo cat and print and text editors like nano, vim and vi are used to create a new file.

How to Create a File?

Before creating a file you should check whether you have to write permissions in the parent directory. If you don’t have write permission then you will receive permission denied error. Use the ls command to view the data of the directory.

Web development, programming languages, Software testing & others

1. Using Touch Command

The touch command is one of the easiest ways to create a file in the Linux shell. To create a new file just type the touch command followed by the name of the file with the extension

Syntax: $ touch filename.filetype

Example:

$ touch touch.txt
$ touch touch.docx

If the file doesn’t exist, then the touch command will create one and if the file already exists then the command will modify its timestamps. While using this command we cannot write any data while creating a new file. By default the size of the newly created file is 0KB.

To view the newly created file using the ls command.

Command:

How to create and edit text file in linux by using terminal

To create multiple files simultaneously enter the file names and separate them with spaces.

How to create and edit text file in linux by using terminal

Example: $ touch file1.docx file2.docx file3.docx

2. Creating File with Redirection Operator

The redirect symbol (>) is also used to create a new file. As the name suggests the redirection operator pipes the output of the command to another command or file. To create a file using this operator, just type the file name after the redirection operator. Even in this method, you cannot enter the text directly. Unlike touch command, you can create only one file at a time. This is the shortest method to create a file in Linux OS.

Command: $ > test2.txt

If the file name already exists it will overwrite the file, otherwise the new file will be created.

How to create and edit text file in linux by using terminal

3. Using Cat Command to Create File

We can also use the cat command to create a file. One important advantage of this method is that you can start adding text to your file immediately. To create a new file just type the cat command followed by the redirection operator and then the filename and then press the Enter key.

Then you can enter the text immediately to your file. After completing, press Ctrl + D to save the file.

Syntax: $ cat > cat.txt

To view the file type the command below

Command: $ ls -l cat.txt

How to create and edit text file in linux by using terminal

4. Using Echo Command to Create File

This command prints the text that is entered as parameters to the command which is then redirected to the file. To create a file type the echo command, then type the text you want to display and then use the redirect symbol to write the contents to the file that you desire to create.

Use the following command line to create a file using echo command.

$ echo “ Linux is simple and easy to learn” > echosample.txt

To create only an empty file use the following command.

How to Create File Using Herdoc?

Heredoc or Here document refers to a kind of redirection that allows a user to pass multiple lines to a particular command. This method is used when you need to create a file that contains multiple lines.

Use the following command to create a file using heredoc

$ sample.txt
first line
second line
third line
EOF

1. Using DD Command

Sometimes we need to create large files for testing the write speed or testing the network speed. dd command is used when there is a need to create large files.

Example: To create a file of size 1 GB run the following command.

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

Here sample.test is the file name.

2. Create File with fallocate Command

fallocate is a utility command which allocates real disk space for a file.

$ fallocate -l 1G sample.test

The above command will create a file named sample.test of size 1 GB.

3. Create File With Print Command

This command is similar to the echo command and it provides some formatting functions. To create a file containing a single text line, use the following command.

$ printf ‘first line\n’ sample.txt

To add two lines, use the \n option to separate each line.

$ printf ‘first line\n second line’ sample.txt

Now let us see how to create a new file using text editors.

  • VI Text Editor

This is the oldest of all text editors in Linux system. It is used for editing text files directly.

To create a new file using Vi text editor, type the following:

Your screen will change to text editor mode. Press the I key to change to insert mode. Now type the contents of your file. To save the file and exit press the button Esc:x and press Enter key.

Output:

How to create and edit text file in linux by using terminal

  • VIM Text Editor

You may see that the Vi editor is not so user-friendly. Vim is a modified version of the Vi editor. Vim stands for Vi modified.

Use the following command to create a new file.

The screen will look like the Vi editor screen. Press I key to type the text and after typing, save the file and exit by typing the following.

Output:

How to create and edit text file in linux by using terminal

  • Using Nano Editor to Create New File

Nano editor is the latest and much easier editor of three text editors that we have seen so far.

To create a new file, type the following command.

On typing the above command you will be directly taken to the editing mode. It will also display a list of useful commands at the bottom of your screen. Enter the text and press Ctrl + O to save the file. To exit the nano editor press Ctrl+X.

Output:

How to create and edit text file in linux by using terminal

Recommended Articles

This is a guide to Linux Create File. Here we discuss the different methods of creating a new file in the Linux Operating System and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

Linux Training Program (16 Courses, 3+ Projects)

As you already know, we can easily create a single or group of directories at a time using mkdir command. It is also possible to create nested directories (a directory inside a directory) with mkdir command. Also, there are other ways to create directories too. This brief tutorial describes how to create directories from a text a file from command line in Linux.

Create Directories From A Text File In Linux

We usually create single directory using command:

The above command will create a directory called “dir1” in the current working directory.

To create multiple directories, we use:

This command creates three directories namely dir1, dir2, and dir3 in the current working directory.

To create nested directories, we do:

The -p option allows you to create parent directory if it is not created already. The above command creates dir2 within dir1 and dir3 within dir2. Here, dir1 is the parent directory to dir2 and dir3. dir2 is the parent directory to dir3. In other words, dir2 is the child directory of dir1 and dir3 is the child directory of dir2 and dir1.

I thought it was only way to create directories from commandline until I came to know about the command xargs. For those wondering, xargs is a Linux command which is used to build and execute command lines from standard input.

Now, let us get back to our topic which is creating directories from a text file using xargs.

Say for example, I want to create a the following directory structure.

Any idea how to create this directory structure from a file? No problem. Just put name of the directories one by one in a text file, say mydirectories.txt, as shown below.

How to create and edit text file in linux by using terminal

Then, run the following command to create the directories.

You can verify whether the directories have been created as you wanted using tree command.

A JAR (Java ARchive) is platform-independent file format used to aggregate many Java class files and associated metadata and resources such as text, images, etc, into a single file for distribution.

It allows Java runtimes to efficiently deploy an entire application in one archive file, and provides many benefits such as security, its elements may be compressed, shortening download times, allows for package sealing and versioning, supports portability. It also supports packaging for extensions.

In this article, we will show how to create a simple Java application and bundle it into a JAR file, and demonstrate how to execute a .jar file from the Linux terminal.

To do this, you must have java command line tool installed to launche a Java application, and the -jar flag to execute a program encapsulated in a JAR file. When this flag is used, the specified JAR file is the source of all user classes, and other class path settings are ignored.

How to Create a JAR File in Linux

1. First start by writing a simple Java class with a main method for an application called TecmintApp, for demonstration purpose.

Copy and paste the following code to TecmintApp.java file.

Save the file and close it.

2. Next, we need to compile and pack the class into a JAR file using the javac and jar utilities as shown.

3. Once tecmintapp.jar created, now you can excute the file using java command as shown.

From the output of the above command, we encountered an error. The JVM (Java Virtual Machine) couldn’t find our main manifest attribute, thus it could not locate the main class containing the main method (public static void main (String[] args)).

The JAR file should have a manifest that contains a line in the form Main-Class:classname that defines the class with the main method that serves as our application’s starting point.

4. To fix the above error, we will need to update the JAR file to include a manifest attribute together with our code. Let’s create a MANIFEST.MF file.

Copy and paste the following line to MANIFEST.MF file.

Save the file and let’s add the file MANIFEST.MF to our tecmintapp.jar using following command.

5. Finally, when we executed the JAR file again, it should produce the expected result as shown in the output.

For more information, see the java, javac and jar command man pages.

That’s all! In this short article, we have explained how to create a simple Java application and bundle it into a JAR file, and demonstrated how to execute a .jar file from the terminal. If you have any questions or supplementary ideas to share, use the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

How to create and edit text file in linux by using terminal

We are thankful for your never ending support.

Thanks for this easy to follow and informative guide.

My question refers to case statements for foreground and background colours.

You have used double quotes for the case terms, e.g. “Default” ) . However isn’t it best practice to use single quotes as in ‘Default’, as non variables are necessarily not being expanded? Or is there a specific use case here that I am unaware of?

How to create and edit text file in linux by using terminal

Yes, for values that don’t require (or specifically don’t want) interpolation, single quotes should be used.

I’m being a little sloppy here I will admit. It’s been a long, long time since I last did bash so I’m back in newbie territory it feels.

How to create and edit text file in linux by using terminalGeneral How to create and edit text file in linux by using terminalNews How to create and edit text file in linux by using terminalSuggestion How to create and edit text file in linux by using terminalQuestion How to create and edit text file in linux by using terminalBug How to create and edit text file in linux by using terminalAnswer How to create and edit text file in linux by using terminalJoke How to create and edit text file in linux by using terminalPraise How to create and edit text file in linux by using terminalRant How to create and edit text file in linux by using terminalAdmin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Updated June 25, 2022

What is the VI editor?

The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons which make it a widely used editor –

1) It is available in almost all Linux Distributions

2) It works the same across different platforms and Distributions

3) It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs

Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit a file.

To work on VI editor, you need to understand its operation modes. They can be divided into two main parts.

In this tutorial, you will learn more about-

Click here if the video is not accessible

vi Command mode:

How to create and edit text file in linux by using terminal

  • The vi editor opens in this mode, and it only understands commands
  • In this mode, you can, move the cursor and cut, copy, paste the text
  • This mode also saves the changes you have made to the file
  • Commands are case sensitive. You should use the right letter case.

vi Editor Insert mode:

  • This mode is for inserting text in the file.
  • You can switch to the Insert mode from the command mode by pressing ‘i’ on the keyboard
  • Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working.
  • To return to the command mode and save the changes you have made you need to press the Esc key

How to use vi editor

To launch the VI Editor -Open the Terminal (CLI) and type

And if you specify an existing file, then the editor would open it for you to edit. Else, you can create a new file.

How to create and edit text file in linux by using terminal

VI Editing commands

  • i – Insert at cursor (goes into insert mode)
  • a – Write after cursor (goes into insert mode)
  • A – Write at the end of line (goes into insert mode)
  • ESC – Terminate insert mode
  • u – Undo last change
  • U – Undo all changes to the entire line
  • o – Open a new line (goes into insert mode)
  • dd – Delete line
  • 3dd – Delete 3 lines.
  • D – Delete contents of line after the cursor
  • C – Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
  • dw – Delete word
  • 4dw – Delete 4 words
  • cw – Change word
  • x – Delete character at the cursor
  • r – Replace character
  • R – Overwrite characters from cursor onward
  • s – Substitute one character under cursor continue to insert
  • S – Substitute entire line and begin to insert at the beginning of the line

– Change case of individual character

Note: You should be in the “command mode” to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case.

Make sure you press the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, o, as required.

Moving within a file

  • k – Move cursor up
  • j – Move cursor down
  • h – Move cursor left
  • l – Move cursor right

You need to be in the command mode to move within a file. The default keys for navigation are mentioned below else; You can also use the arrow keys on the keyboard.

Saving and Closing the file

  • Shift+zz – Save the file and quit
  • :w – Save the file but keep it open
  • :q – Quit without saving
  • :wq – Save the file and quit

You should be in the command mode to exit the editor and save changes to the file.

Similarly, how do I create a .sh file in Linux terminal?

Steps to write and execute a script

  1. Open the terminal. Go to the directory where you want to create your script.
  2. Create a file with . sh extension.
  3. Write the script in the file using an editor.
  4. Make the script executable with command chmod +x .
  5. Run the script using ./ .

Secondly, how do I create a bash script in Linux? Bash as a scripting language. To create a bash script, you place #!/bin/bash at the top of the file. To execute the script from the current directory, you can run ./scriptname and pass any parameters you wish. When the shell executes a script, it finds the #!/path/to/interpreter .

Thereof, how do I create a .sh file?

  1. Create a file using a vi editor(or any other editor). Name script file with extension .sh.
  2. Start the script with #! /bin/sh.
  3. Write some code.
  4. Save the script file as filename.sh.
  5. For executing the script type bash filename.sh.

What does chmod do?

In Unix and Unix-like operating systems, chmod is the command and system call which is used to change the access permissions of file system objects (files and directories). It is also used to change special mode flags. The request is filtered by the umask. The name is an abbreviation of change mode.

We can create files from command line in two ways. The first way is to use fsutil command and the other way is to use echo command. If you want to write any specific data in the file then use echo command. If you are not bothered about the data in the file but just want to create a file of some specific size then you can use fsutil command.

To create file using echo

To create file using fsutil

Limitations

Fsutil can be used only by administrators. For non-admin users it throws up below error.

Thank you for the quick and dirty method since windows seems to lack the “touch” command.

How can we use these commands to ‘touch’ a file. Could anyone provide the exact command that can work as linux ‘touch’ for any type of file?

Windows does not lack “touch”. It is echo. As the post describes? type: echo “” Blah.cpp

for Windows users, try
echo README.md

touch works on a UNIX system.

dude, this isnt working on our school’s computers, can you tell another method that works on windows 8 computers please?

lmao it works at school PC

Make sure that you are typing in the code the right way. It happened to me when I typed in “echo This is a sample text file sample.txt”.

If you want to create a file with NO text in it do this:
at the CLI type “Echo.”: Your output should look like this:
C:\Temp>echo.

To write to a file:
C:\Temp> Echo. >test.txt

This will create a file with single space in the file.

How to create and edit text file in linux by using terminal

It is basic requirement for the command line work to create or edit text files. Vim and Emacs are powerful command line editors but those might be overwhelming for new Linux users. Nano text editor is straightforward and easy to use. It includes all the basic functionality same as other text editors such as UTF-8 encoding, syntax highlighting, search and replace with regular expression support, multiple buffers, spellchecking and more. In this tutorial we will show you how to install and use of nano text editor.

Install Nano Text Editor#

Nano text editor is by default included in macOS and most Linux distributions. The process depends on the operating system you are running. If Nano text editor is pre-installed, you can skip this section to start learning the basic Nano text editing commands. You can check whether nano is installed or not issue the below command:

It should show output as given below:

If you don’t have nano installed on your system, you can install it using the package manager of your distribution. Make sure you need administrator privileges for Ubuntu, Debian and CentOS to install nano.

Installing Nano on Debian and Ubuntu#

To install Nano text editor on Debian or Ubuntu system, issue the following command:

Installing Nano on CentOS and RHEL#

To install the Nano text editor on CentOS or RHEL based platforms, run this command:

Use Nano Text Editor#

Open and Create Files#

Type nano followed by the filename, to create or open an existing file. Make sure that if you want to edit an existing file, you must be in the directory where the file is located. Alternatively, you can specify the full path to the file. To be able to open a file you must have read permissions to the file.

It will open a new editor window and you can write or edit it. Use arrow keys on your keyboard to move the cursor around the text. At the bottom of this window, you can find some shortcuts to use with the Nano editor. Symbol ^ means that you must press CTRL + [Key] (CMD + [Key] for Mac users) to use the chosen command.

To save the changes made in the file and continue editing press CTRL + O.

To exit from the editor press CTRL + X . If any changes have been made to the currently open file, it will ask whether to save them or not otherwise it will exit right away. Input y for yes , or n for no , and then press ENTER .

Also, you can get a list of all commands by typing Ctrl+g .

Editing Files#

You can start typing and editing the content immediately after opening the file because nano is modeless editor. In order to select text go to the beginning of the text and press ALT + A . This will set a mark for selecting, then move over the text to be selected with the arrow keys.

To copy the selected text press ALT + 6 . This will copy text to the clipboard. To cut text press CTRL + K .

If you want to paste text press CTRL + U . If no text is selected before copying or pasting, it will copy/cut the entire line.

Searching and Replacing Text#

To search for a particular word or part of a text inside the editor, use the “where is” option with the Ctrl+W shortcut (^W) . This will open a search prompt where you can type in the text you want to find. To continue to the next result, use Alt+W (M-W) .

The search bar can also find specific line numbers. Press Ctrl+T (^T) while in it and the line number you want to find.

You can also search with regex (regular expressions). These represent a search pattern defined by a sequence of characters. To do so, use the Alt+R shortcut (M-R) .

If your goal is to find and replace text, press CTRL+W and then CTRL+R . It will ask for the search text and the text to be replaced by. Afterward, the editor will take you to the first instance of the text and ask you whether to replace it or all of the occurrences.

Select, Copy, Cut and Paste Text#

If you want to select part of a file, navigate to the starting of the text, press the Alt+A shortcut (M-A) and use the arrow keys to move over the text you wish to select. The selected text will be highlighted. If you want to cancel the selection press Ctrl+6

Next, you can copy the selected text with the Alt+6 or combination (M-6) or you can cut selected text with Ctrl+K (^K) . If you use these shortcuts without selecting any text prior, it will copy or cut the entire line of text.

To paste the text move the cursor to where you want to put the text and press Ctrl+u .

Save and Exit File#

To save changes made by you in a file, use the Ctrl+O (^O) keyboard combination. It will ask you to enter a file name or confirm the name of an existing file. If the file does not exists then it will create once you save it.

To exit out of Nano, press Ctrl+X (Nano displays it as ^X ). If there is any unsaved changes it will prompt whether you want to save the changes or not.

Set Nano as the Default Text Editor#

In most of Linux systems, the default text editor is set to vi. You can make nano as a default text editor by making change to VISUAL and EDITOR environment variables.

Bash users can export the variables in the

Conclusion#

You have learned how to install and use GNU nano text editor. It is a popular text editor and very straightforward for new Linux users. If you would like to learn more about GNU Nano visit the official nano documentation page.

If any question or suggestion, please leave comment below.

If our content helps you, please consider buying us a coffee