Create/modify files

Instructor note

Total: 30min (Teaching: 25Min | Discussion:0min | Breaks:0min | Exercises:0Min| Type-along-pause:5 min)


  • Should be logged in to one of the HPC systems


  • Questions

    • How to create or modify text files without leaving the terminal

    • What is terminal text editor ?

    • How to view a file content

  • keypoints

    • echo redirection

    • cat

    • nano editor

Create files with echo redirection

The command echo is used to display content on terminal windows. By using redirection we can store the content in a file instead of displaying it.

# Create with the echo, if there is already content in the `new-file-with-echo.txt`
# then that content will be overwritten
[MY_USER_NAME@CLUSTER_NAME ~]$ echo "Created with echo" > new-file-with-echo.txt 
# Append content to a file.Notice the two less than signs   
[MY_USER_NAME@CLUSTER_NAME ~]$ echo "Last line added with echo" >> new-file-with-echo.txt 

Create files with cat

cat can also be used to create a file in addition to be used to view content. cat can create files by typing in content or by using the content of one or more files. As do with other bash commands, cat can be combined with other commands to achieve complex tasks well.

# Create a file
[MY_USER_NAME@CLUSTER_NAME ~]$ cat > new-file-with-cat.txt 

# CTRL + D to stop adding content and end the operation.
# Create a file using content from two other files
[MY_USER_NAME@CLUSTER_NAME ~]$ cat file1 file2 > combined-content

What is a terminal text editor and why do we need one ?

A terminal text edit is a program that you can invoke write from the terminal and add, remove, edit and modify content without a Graphical User Interface (GUI). The navigation and opening and closing the program is done using keyboard shortcuts (not using mouse clicks). There are many reasons that we need one.

  • When we establish a connection to a remote machine we do not usually invoke a GUI, so we do our work right on the terminal

  • Plain text and no artifacts or meta-data. In a terminal text editor we have the content as plain text

Examples of a popular terminal text editors are

  • nano

  • vi/vim

  • Emacs

NANO editor

Nano is a simple text editor that is easy to get started, although it is not the one with most features. To open nano

[MY_USER_NAME@CLUSTER_NAME ~]$ nano learn-nano.txt

The above command will open a windows like the following, if a files named learn-nano.txt is not present in your working directory. If by any chance if there is a file with that exact name nano will open with the current content of that file.

nano editor

After nano opens, navigation is done with the arrow keys on the keyboard.

  • To move up, down, left and write, corresponding arrow keys

  • To start a new line ENTER key

  • To save the file, hold down the CTRL and press O. If you are on Mac use the Mac key instead of the CTRL key.

  • To exit nano, hold down the CTRL and press X. If you are on Mac use the use the Mac key instead of the CTRL key. When you try to exit, if you had not saved the last edit nano will prompt you to save the file first

Instructor note

  • Demonstrate the nano operations.

  • Pay attention to be slow during operations.

  • Show what will happen if you do not specify a filename when opening nano.

  • Show what will happen if you edit file name during saving a files.

Instructor note

For more details about this command use the man pages