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
How to create or modify text files without leaving the terminal
What is terminal text editor ?
How to view a file content
Create files with
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
> will overwrite any contents in the file.
If you want instead to append make sure to use
Create files with
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
# 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
Examples of a popular terminal text editors are
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.
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
To save the file, hold down the
O. If you are on Mac use the Mac key instead of the
To exit nano, hold down the
X. If you are on Mac use the use the Mac key instead of the
CTRLkey. 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.
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.
For more details about this command use the man pages