# Create/modify files ```{instructor-note} Total: 30min (Teaching: 25Min | Discussion:0min | Breaks:0min | Exercises:0Min| Type-along-pause:5 min) ``` ```{prereq} - 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. ```console # 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 ``` ```console # 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](https://training.pages.sigma2.no/tutorials/unix-for-hpc/episodes/moving-around.html)). `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. ```console # 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. ``` ```console # Create a file using the content from two other files [MY_USER_NAME@CLUSTER_NAME ~]$ cat file1 file2 > combined-content ``` ## Using a text editor `````{tabs} ````{group-tab} Visual Studio Code We can use Visual Studio Code to display, create, edit, and delete files on a remote machine. Logging in to the remote machine using the `Remote - SSH` extension allows you to open a folder on the remote machine and work on files as if they were stored locally on your machine. 1. Click the **Explorer** icon in the left sidebar to open the **Explorer** view. ![explorer_icon](/images/vscode_explorer_icon.png) 2. A menu should open on the left side of the screen. Click the **Open Folder** button to open a folder on the remote machine. ![explorer_open_folder](/images/vscode_open_folder_2.png) 3. Select a folder on the remote machine to open. The *home* directory, `/cluster/home/USERNAME`, should be the default option. You may be asked if you trust the authors of the files in the folder. Click **Yes** to trust the authors and open the folder. ![remote_open](/images/vscode_remote_open.png) 4. Now that your home folder is open, you can create, edit, and delete files as if they were stored locally on your machine. The left sidebar shows the contents of the opened directory. You can create a new file by clicking the **New File** button at the top of the sidebar. ![new_file](/images/vscode_new_file.png) ![remote_file_opened](/images/vscode_remote_opened_file.png) ```` ````{group-tab} Terminal text editor ## What is a terminal text editor 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 why we might use 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 ```{note} 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 ```console [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](/images/nano.svg) 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 ``` ```` `````