How to make and edit text files

When using the linux command line there will be many occasions where you need to manipulate text files. This includes:

  1. Making an empty text file.
  2. Editing an existing file.
  3. Renaming a file.

For the exercises in this post I will be using JSLinux hosted by Fabrice Bellard you can access it here.

Making an empty text file

If you want to make an empty file and fill it later on you can use the command touch followed by your chosen file name.

The usefulness of creating empty files is not immediately obvious, but they can be very helpful during script development (we will go into more detail in a later post).

Here is an example where we use the touch command line tool to create an empty text file called “file.txt”

touch file.txt

Next, we can use ls to check the file was created.

Looks like it worked!

So, what if we want to add some text to this file?

Opening and editing a file

You may already be familiar with graphical text editors, such as BBEdit or TextEdit on your desktop computer, but if we want to edit text files on the command line this means we need to find a suitable command line tool.

Introducing nano

nano is an easy to use and versatile command line tool which you can use to create and edit files.

Editing files with nano

We can go ahead and edit the file we created earlier “file.txt” using nano.

Let’s add in some text! To do this type the following into the linux terminal:

nano file.txt

This opens our file using the text editor, you should see a box representing the file and inside at the start of the file is a green cursor.

We can now type inside this box, try it out by writing ‘hello’.

The save sequence

Next we need to save the file.

To save the edited file we use the ctrl + x, shift + y and press enter .

After typing ctrl + x you will see this prompt in the bottom left hand corner:

Next type shift + y to tell nano you want the file to be saved ( shift + n gives you the option to leave the file unchanged)

Then you will see another prompt:

This is giving you the option to rename the file you can use the arrow keys or back space to edit the file name if you need to.

In this example we don’t want to change the name, so next press the enter key.

Creating text files with nano

We can create a text file using nano by typing nano followed by the name we want to call our file.

Try it out! Type:

nano file2.txt

Notice that this automatically brings up the nano editor.

But wait! The file is not created yet, we need to add text and go through the save sequence to create it.

Let’s do it, this time type in “hello again

Repeat the save sequence( ctrl + x, shift + y and press enter ) to save it, this will save the file containing our text as “file2.txt“.

We can check the file exists by typing ls and if we want to check our text saved correctly we can use the more command to print out the contents of the file.

ls

Then:

more file2.txt

Your terminal should look like this:

Renaming a text file

Finally, we can use nano to rename a file during editing but, what if we want to rename a file without opening it?

In this case we can make use of the mv or ‘move’ command.

This command is usually used to move a file from one location to another but we can also use it to rename a file in the same directory.

Try it out by changing the name of “file.txt” to “file3.txt” and use ls to check that it worked.

mv file.txt file3.txt

Alternatives to nano

Not all linux distributions come with nano and the standard linux text editor is a program called VIM.

VIM has more features and is more complex to use compared to nano.

Love it or hate it there’s a game to help you learn – you can check it out here: https://vim-adventures.com/

Happy Text Editing!