Moving around and looking at things

Objectives

  • Learn how to navigate through folders.

  • Learn how to create, copy, move, rename, and remove files and folders.

  • Learn how to see the contents of files in the terminal.

  • Learn how to compare files.

Instructor note

  • Demo/teaching: 15 min

  • Exercise: 15-20 min

Note

  • First we will demonstrate a couple of commands and later we will use these in an exercise.

  • If you want to type-along with the instructors, you can download and extract the example like this:

    cd
    wget https://gitlab.sigma2.no/training/tutorials/unix-for-hpc/-/raw/master/content/episodes/moving-around/demo-moving-around.tar.gz --no-check-certificate
    tar xvf demo-moving-around.tar.gz
    cd demo-moving-around
    

How to find back home

When you first connect to a cluster, you land in your home folder (try it and verify the location with pwd). These can tell us where our home folder is located:

$ echo $HOME
$ echo ~

All of these can bring you back to your home folder root:

$ cd $HOME
$ cd ~
$ cd

Creating, copying, renaming, moving, and removing

Note

Before we continue let’s make sure we are back in the demo folder:

cd && cd demo-moving-around

Sometimes we need a new folder:

$ mkdir new-folder

Let’s copy a file:

$ cp poem.txt poem2.txt

We can copy the file into another folder:

$ cp poem.txt new-folder

You can also copy several files at once. The last argument is the destination:

$ cp poem.txt poem2.txt some-folder

The command mv can be used to move files or folders or to rename them (for Unix both are the same thing):

$ mv poem2.txt poem3.txt

Removing a file:

$ rm poem3.txt

Removing a directory (you can check what -r does with man rm):

$ rm -r new-folder

Printing and comparing file contents

The command cat can be used to concatenate several files into one but many people use it on one file only to print its contents:

$ cat poem.txt

Let’s try the commands more and less. They behave similarly and allow to page (scroll) through the file. They are useful if you don’t want to see the whole long file in one go. You can scroll with Enter and quit with “q”:

$ more numbers.out
$ less numbers.out

Commands head and tail only print the beginning and the end:

$ head poem.txt
$ tail poem.txt

The command diff is super useful to compare files or to check whether files are identical:

$ diff unfinished.txt poem.txt

1c1
< Working title
---
> Success is counted sweetest

Exercise

Exercise (20 min): Navigate in the file system and look at file content

It is perfectly fine to only do part of the steps below. They may be too many for 15-20 minutes, so if you don’t reach them all, do them as homework later on.

  1. Connect to the cluster and locate where in the file system you are:

    $ hostname
    $ pwd
    
  2. Download and extract the exercise folder:

    cd
    wget https://gitlab.sigma2.no/training/tutorials/unix-for-hpc/-/raw/master/content/episodes/moving-around/exercise-moving-around.tar.gz --no-check-certificate
    tar xvf exercise-moving-around.tar.gz
    
  3. Step into the exercise folder:

    $ cd exercise-moving-around
    
  4. Make a drawing on paper (or a mental picture) of the folder structure that you find under exercise-moving-around.

  5. Find out in which subfolder are example output files with the ending .output.

  6. Let us look at one of the file , computation-a.output in ./exercise-moving-around/results/ and compare the outputs of the following commands:

    $ cat  computation-a.output
    $ more computation-a.output
    $ less computation-a.output
    $ head computation-a.output
    $ tail computation-a.output
    
  7. Find a directory called “backup”. Copy all the .output files to the backup folder.

  8. In the folder where we found the .output files (not the backup folder), rename the output files to now have the ending .out instead of .output.

  9. Among the output files there are two files with the same content (verify with diff), can you remove one of the duplicate files?

  10. How would you copy the inputs folder under backup? How would you move the folder?

Keypoints

  • With a handful of commands we can navigate, move, rename, copy.

  • In Unix there are often many ways to achieve something.

  • Commands often have options/”flags”. You can check manual pages with man.