Skip to content
Snippets Groups Projects
Commit 758e51e2 authored by Fanny Wegner's avatar Fanny Wegner
Browse files

Delete UNIX_HPC_exercise_instructions.md

parent e4bbadf9
No related branches found
No related tags found
No related merge requests found
## Before you start
If you have questions about any command, you can always type `man [command]` to get an explanation and all possible additional options.
## Exercise 1 - Navigating the filesystem on command line
**Objective:** get familiar with navigating the directory tree and listing
the content of directories.
1. **Print your current working directory** with the `pwd` command. This will
show you where you currently are in the directory tree.
2. **Navigate to the `exercises/` directory** (the one you unpacked from the
zip archive file). If you are already in the `exercises/` directory,
navigate to its parent with `cd ..` and then come back to it.
3. **Try to run the command `cd .`**. What happens? What does the `.` stand
for?
4. **List the content** of the `exercises/` directory with `ls`, `ls -l`,
`ls -lh`, and `ls -lha`.
* What do the `-l`, `-h` and `-a` options do?
* **Hint:** you can use `man ls` to display the help for the `ls` command.
To exit the help, simply type `q` on your keyboard.
* *Note:* one-letter options can be grouped together, so `ls -lha` is the
same as `ls -l -h -a`.
* *Note:* some options have both a "short" and a "long" form. E.g. `ls -a`
is the short form for `ls --all`.
<br>
<details><summary><b>Exercise solution</b></summary>
<p>
1. Printing the current working directory:
```sh
pwd
```
2. Navigate to the current directory, or navigate in and out of it.
```sh
# If you are not already in the practicals directory.
# Note: in the command below, you need to replace /path/to/directory/ with
# the actual absolute or relative path of the directory where your
# "practicals" directory is located.
cd /path/to/directory/exercises
pwd
# If you are already in the practicals directory.
cd .. # Change to parent directory.
pwd #
ls -l #
cd exercises/ # Go back into the practicals/ directory.
```
3. The `.` symbol is a shortcut for the current directory. So running `cd .`
has no effect since it simply changes to the same directory we are already
in.
The `.` shortcut is useful in some situations. E.g. if you want to copy
a file to the current directory you can do `cp /file/to/copy .`, or you
can run an executable located in the current directory with `./run_me.sh`.
4. Listing the content of the `exercises/` directory with different `ls`
options. the effect of the different options is described in the comments
of the code block below.
```sh
ls # Prints the names of files and directories
ls -l # List content of the subdirectory in "long listing" format. This
# provides additional details for each file/directory, such as
# its permissions, its size and its last modified date.
ls -lh # Adding the "-h" option displays file sizes in "human readable"
# format. The size of files are shown in kB, MB, GB, instead of
# their size in bytes (octets).
ls -lha # Adding the "-a" option additionally displays hidden files and
# directories. These are files/directories whose name starts with
# a dot ".". The "-a" is the
# Hidden files are often used to store program configurations.
```
**Tip:** It is possible to define a shorthand for longer commands that you use often, a so called `alias`. On Science Cluster, there are already some pre-defined useful aliases, among them `ll` (standing for `ls -lFh`) and `la` (standing for `ls -lA`).
</p>
</details>
<br>
## Exercise 2 - Creating and moving directories and files
**Objective:** learn to use the `mkdir`, `cp` and `mv` commands.
1. **Create directories** with the `mkdir` command:
* At the root of the `exercises/` directory, create 2 new
sub-directories: `species_by_genus` and `species_by_common_name`.
* In `exercises/species_by_genus/`, create 2 new sub-directories:
`Dendrolagus` ([tree-kangaroos](https://en.wikipedia.org/wiki/Tree-kangaroo))
and
`Crocidura` ([a genus of shrews](https://en.wikipedia.org/wiki/Crocidura)).
* In `exercises/species_by_common_name/`, create 2 new sub-directories
named `B`, and `R`.
2. **Copy files** using the `cp` command:
* From the directory `RedList_mammals/`, make a copy of all files of the
genuses `Dendrolagus` and `Crocidura` into their respective
sub-directories in `species_by_genus/`.
* From the directory `RedList_mammals/`, copy the file for the
[Red Wolf](https://en.wikipedia.org/wiki/Red_wolf) *Canis rufus* to the
directory `species_by_common_name/`.
3. **Move and rename files** with the `mv` command:
* Enter the `species_by_common_name/` directory.
* In the directory, move the file `Canis_rufus` into subdirectory `R`.
* Rename the `Canis_rufus` file you just moved into the subdirectory `R` to
the common name of the species: `Canis_rufus` -> `Red_wolf`.
4. **Copy and rename files** with the `cp` command:
* Similarly to what we did for the Red Wolf file, we will now copy and
rename the file for the
[Black Rhinoceros](https://en.wikipedia.org/wiki/Black_rhinoceros)
*Diceros bicornis*, but this time in a single step with the `cp` command.
* Copy the file `Diceros_bicornis` from its original location (in
`RedList_mammals`) into `species_by_common_name/B`, while directly
renaming it to the common name of the species: `Black_rhino`.
5. **Copy, rename and delete directories**:
* Go back to the root of the `exercises/` directory.
* Copy the entire directory `species_by_genus/Dendrolagus/` with all its
content to the root of `exercises/`.
* Rename the directory to `Tree-kangaroos`.
* Delete the directory `Tree-kangaroos` and its content **in a safe way**.
<details><summary><b>Exercise solution</b></summary>
<p>
1. Create the `IUCN_species_by_genus` and `IUCN_species_by_common_name`
directories.
```sh
# Option 1: create one directory after the other.
mkdir species_by_genus
mkdir species_by_common_name
# Option 2: create both directories with a single command.
mkdir species_by_genus species_by_common_name
# Option 3: use brace expansion to avoid repeating the common part of
# the directory names.
mkdir species_by_{genus,common_name}
```
Create sub-directories `Dendrolagus` and `Crocidura`:
```sh
# Option 1: create sub-directories from the exercises/ directory.
mkdir species_by_genus/Dendrolagus species_by_genus/Crocidura
# Option 2: enter the species_by_genus/ directory, then create the
# sub-directories "Dendrolagus" and "Crocidura".
cd species_by_genus/
mkdir Dendrolagus Crocidura
cd ..
# Option 3: same as option 1, but using brace expansion to avoid repetition.
mkdir species_by_genus/{Dendrolagus,Crocidura}
```
Create sub-directories `R` and `B`:
```sh
mkdir species_by_common_name/{R,B}
```
*Note:* using the `-p` option of `mkdir`, it is possible to create
multiple levels of directories in a single command. For example, we could
create all the directories from this exercise in a single command:
```sh
mkdir -p species_by_{genus/{Dendrolagus,Crocidura},common_name/{R,B}}
# Pro-tip: if you want to preview the output of a brace expansion by
# the shell, you can run the command prefixed with "echo": it will print
# the command that would be executed to the terminal without running the
# command.
echo mkdir -p species_by_{genus/{Dendrolagus,Crocidura},common_name/{R,B}}
```
2. Copy files for `Dendrolagus` and `Crocidura`:
```sh
cp RedList_mammals/Dendrolagus_* species_by_genus/Dendrolagus/
cp RedList_mammals/Crocidura_* species_by_genus/Crocidura/
```
Copy the file for the Red Wolf:
```sh
cp RedList_mammals/Canis_rufus species_by_common_name/
```
3. Move and rename the Red Wolf file:
```sh
cd species_by_common_name/
mv Canis_rufus R/ # Move the file into its subdirectory.
mv R/Canis_rufus R/Red_wolf # Rename the files to the common name of the species.
```
4. Copy and rename the file for teh Black Rhino in a single `cp` command:
```sh
# Note: this assumes you are currently in directory "species_by_common_name".
cp ../RedList_mammals/Diceros_bicornis B/Black_rhino
```
5. Copy, rename and delete a directory:
```sh
cd .. # Go back to the root of the `exercises/` directory.
cp -r species_by_genus/Dendrolagus/ . # Copy the directory and its content.
mv Dendrolagus/ Tree-kangaroos # Rename the directory.
ls -l
# To delete the directory in a safe way, we first delete all the files
# inside it, and then delete the empty directory with "rmdir".
# Note that "rmdir" will not delete a directory if it's not empty - this
# is a safety behavior to avoid deleting large number of files by mistake.
rm Tree-kangaroos/*
rmdir Tree-kangaroos
```
*Note:* the faster way to delete the directory and all of its content would
be to use the command: `rm -rf Tree-kangaroos`.
This recursively delete the directory, and therefore one has to be careful
to be deleting the correct directory, as you can otherwise very quickly
delete large amounts of data by mistake.
</p>
</details>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment