Create/modify files

Instructor note

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

Prerequisites

  • Should be logged in to one of the HPC systems

Objectives

  • 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 contents on terminal window. By using redirection we can store the content in a file instead of displaying it.

# Using echo to create a file. NB: if there is already content in `new-file-with-echo.txt`
# this will be overwritten
[MY_USER_NAME@CLUSTER_NAME ~]$ echo "Created with echo" > new-file-with-echo.txt 
# Append content to a file using echo together with two less-than signs   
[MY_USER_NAME@CLUSTER_NAME ~]$ echo "Last line added with echo" >> new-file-with-echo.txt 

Danger

NB! The > will overwrite any contents in the file.

If you want instead to append make sure to use >>

Create files with cat

cat can be used to create a file in addition to be used to view content (which we learned in Moving around and looking at things). cat can create files by typing in content or by using the content of one or more files. As with other bash commands, cat can be combined with other commands to achieve complex tasks.

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

# CTRL + D to stop adding content and end the operation.
# Create a file using the 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 editor is a program that you can invoke right 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

We recommend to use **nano** if you do not have any other preferences, 
since it is easy to get start with. But you can use your preferred editor. 

Examples of other popular terminal text editors are
* vi/[vim](https://www.openvim.com)
* [Emacs](https://www.gnu.org/software/emacs/tour/)

[Visual Studio Code](https://code.visualstudio.com) is a popular source code 
editor. It is free and open source and runs on your desktop and is available 
for Windows, macOS, and Linux. It has the ability to connect to remote systems
 through ssh/sftp. This [guide](https://documentation.sigma2.no/code_development/guides/vs_code/connect_to_server.html) will show you how to connect to NRIS clusters
 with Visual Studio Code.

NANO editor

Nano is a simple text editor that is easy to get started with, 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 window like the one shown below if a file named learn-nano.txt is not present in your working directory. If by any chance there is a file with that exact name, nano will open that file and show its contents.

nano editor

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

  • To move up, down, left and right, use the corresponding arrow keys

  • To start a new line, press 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

Note: nano has a menu at the bottom showing what key-combinations to use for the most important commands, so you don’t have to memorize these.

Instructor note

  • Demonstrate the nano operations.

  • Pay attention to be slow during operations so everyone can follow along.

  • 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