Moving around and looking at things
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.
Demo/teaching: 15 min
Exercise: 15-20 min
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 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
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
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
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
$ rm -r new-folder
Printing and comparing file contents
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
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
tail only print the beginning and the end:
$ head poem.txt $ tail poem.txt
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 (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. But some of them can be great homework exercise for later.
Connect to the cluster 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 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
Let us look at one of the file ,
./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
.outputfiles to the backup folder.
In the folder where we found the
.outputfiles (not the backup folder), rename the output files to now have the ending
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
backup? How would you move the folder?
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
lsto inspect the contents of the directories - combined with the wildcard symbol
$ ls ./* $ ls ./*/* $ ls ./*/*/*
There are better ways using the
findcommand, which will be taught later. But to spoil the suprise, here it comes:
$ find . -name "*.output"
catprints the whole file.
lesslook similar and allow to page (scroll) through the file. You can scroll with Enter and quit with “q”.
tailonly 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 (
diffproduces 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
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