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
This is also an example of concatenating two commands with &&
: first cd
is performed to place you in your home folder, then cd demo-moving-around
which is a subfolder of your home directory.
Sometimes we need to make new folder/directory:
$ 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 folder (and must be a folder):
$ 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
flag 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.
Connect to the cluster - check the hostname and locate where in the file system you are:
$ hostname $ pwd
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
Step into the exercise folder:
$ cd exercise-moving-around
Make a drawing on paper (or a mental picture) of the folder structure that you find under exercise-moving-around.
Find out in which subfolder are example output files with the ending
.output
.Let us look at one of the files ,
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
Find a directory called “backup”. Copy all the
.output
files to the backup folder.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
.Among the output files there are two files with the same content (verify with
diff
), can you remove one of the duplicate files?How would you copy the
inputs
folder underbackup
? How would you move the folder?
Solution
These commands will produce different outputs depending on where we are. Examples:
$ hostname login-4.saga
$ pwd /cluster/home/user
This step will hopefully produce the exercise folders. Nothing to change here.
Nothing to change here.
Drawing or mental picture of the folder structure. Something like this:
exercise-moving-around ├── backup │ └── old │ └── old-computation.out ├── inputs │ └── example.in ├── results │ ├── computation-a.output │ ├── computation-b.output │ ├── computation-c.output │ ├── computation-c2.output │ ├── computation-d.output │ ├── computation-e.output │ └── computation-f.output └── scripts └── example.sh
One rudimentary was is to use
ls
to inspect the contents of the directories - combined with the wildcard symbol*
$ ls ./* $ ls ./*/* $ ls ./*/*/*
There are better ways using the
find
command, which will be taught later. But to spoil the suprise, here it comes:$ find . -name "*.output"
cat
prints the whole file.more
andless
look similar and allow to page (scroll) through the file. You can scroll with Enter and quit with “q”.head
andtail
only print the beginning and the end.First navigate to the results folder:
$ cd results
Then copy all files
$ cp *.output ../backup
Here is a manual way (later we will learn more elegant ways to rename all in one go):
$ mv computation-a.output computation-a.out $ mv computation-b.output computation-b.out ...
These two files have identical content (
diff
produces no output):$ diff computation-c.output computation-c2.output
You can remove one of them like this:
$ rm computation-c2.output
We can copy the entire folder like this:
$ cp -r inputs backup
Or we can move it:
$ mv inputs backup
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
.