Using the shell

Aims

  • Create a local project directory
  • Create and edit README.md file

Learning outcomes

  • Build confidence in navigating the filesystem, creating, deleting and editing files and folders

The shell

Here we recall very briefly why we use the shell environment and how normal operations are typically performed. These topics have already been covered in the previous term.

The shell allows you to interact with a computer without the need of a graphical user interface (GUI). This has three main advantages:

  • it allows you to operate on files and folders programmatically
  • it allows you to interact easily with remote machines, e.g. the High Performance Computing facilities of the University of Bristol
  • it allows to have more direct access to low-level operations of your machine (installing and fine-tuning software, libraries and various components)
Important

It is essential to learn how to use the shell properly in order to understand and develop more advanced scientific computing tools.

Basic shell usage

  1. List files and directories: Displays the contents of the current directory.
ls
  1. Change directory: Moves to a different directory.
cd /path/to/directory
  1. Create a new directory: Creates a new folder.
mkdir new_folder
  1. Remove a file: Deletes a specific file.
rm file.txt
  1. Remove a directory: Deletes a directory and its contents.
rm -r directory_name
  1. Move or rename a file: Moves a file to a new location or renames it.
mv old_file.txt new_location/
  1. Copy a file: Copies a file to a new location.
cp file.txt /path/to/destination/
  1. Create a new file: Creates an empty file.
touch newfile.txt
  1. Edit a file: Opens a file in a text editor like nano for editing.
nano file.txt
  1. View the contents of a file: Displays the contents of a file in the terminal.
cat file.txt
  1. Search for text in a file: Searches for specific text in a file.
grep "search_term" file.txt
  1. Show current directory path: Displays the full path of the current working directory.
pwd
  1. Copy a directory: Copies a directory and its contents.
cp -r source_directory/ destination_directory/
  1. Move back one directory: Moves up one directory level.
cd ..
  1. Display disk usage: Shows the disk usage of files and directories.
du -sh *

To-dos

Now that we know how to manipulate folder and files, we start our project by creating

  • a dedicated directory
  • a README.md file to contain the project documentation
Task 1: create the local project directory

Now that we have overviewed basic shell commands, we can set off and create a local directory that will contain our project.

We name the project vicsek-cpp.

mkdir vicsek-cpp
Task 2: create the README.md file

Inside the folder, we can use our favourite editor (e.g. vim or nano or VSCode if we like) to create and edit the README.md file.

Include a header and a brief description. You will provide more documentation later on.

For the root working directory, typing

ls vicsek-cpp

should return

README.md
Task 3: Inspect file contents

There are many ways to quickly inspect the contents of a file in bash.

Try the command head, tail and cat on your file and check their documentation to understand how they work.