Getting Familiar with Unix Environments#
This page is designed to provide an introduction to Unix-type shell commands. On MacOS, the standard Terminal is zsh
(as of 2019) which is a Unix-type shell. On Windows, the cygwin project provides a Unix-like environment that uses the same commands.
Summary of Commands#
The commands introduced in this page are summarized in the following table for convenience:
Command |
Description |
Commonly-Used Options |
---|---|---|
pwd |
Obtains the present working directory |
|
ls |
Lists the contents of the present working directory |
-l (verbose); -a (show hidden) |
cd |
Changes directory to a different location |
|
cp |
Copies files from one location to another |
-r (recursively copy from directories) |
ln |
Links one file to another |
-s (symbolic) |
cat |
Print contents of a file to the terminal |
|
head |
Print contents of the start of a file |
-n (number of lines) |
tail |
Print contents of the end of a file |
-n (number of lines) |
mkdir |
Make a directory |
|
rm |
Removes file(s) from the system |
-r (recursively remove from directories) |
rmdir |
Remove directory |
-R (remove a directory and all of the files inside of it) |
Tutorial#
Getting Started#
To get started with this introductory tutorial, use the following steps:
Download the model_config_example.zip example file set from the Github repository for this book and unzip it.
Open the Unix shell you use on your machine.
The model_config_example
directory contains several subdirectories containing various files in the following format:
![Model File Structure](../_images/model_file_structure.png)
This directory structure mimics the typical structure used for MITgcm models.
Copying and Linking Files#
Copying Files#
Files can be copied with the cp
command (short for copy). The syntax for the copy command is to first list the path of the file to copy and then the destination for that file. For example, suppose you are in the run
directory and would like to copy the file eedata
from folder namelist
. In this case, the copy can be completed with:
cp ../namelist/eedata eedata
The first argument is the source file and the second is the destination file. If you want the file name to stay the same, then you can optionally provide a directory. For example, you can use the .
symbol to indicate the current directory as the destination as:
cp ../namelist/eedata .
Just like with file paths, wildcards can be used to copy multiple files at once that follow the same pattern.
π€ Try it for yourself:#
Copy all files from namelist
that start with data
to the run directory:
π‘ Solution:#
Top copy all of the files starting with the characters data
from the namelist
directory to the current directory, use cp ../namelist/data* .
Linking Files#
Often we would like files to be in multiple locations at the same time. For example, two different models may rely on the same underlying data. In this case, we donβt need two complete copies of the data - each model just needs to know where the data located. In these circumstances, a symbolic link is desirable.
We can symbolicly link files with the ln -s
command. For example, suppose we wanted to link the bathymetry.bin
file from the input
directory into the run
directory. In this case, we could use
ln -s ../input/bathymetry.bin .
π€ Try it for yourself:#
Link the rest of the folders and files from the input
directory into your run
directory.
π‘ Solution:#
To see what other files are in the input directory, we can use the ls ../input
command.
After discovering the names of the three other items, we can use
ln -s ../input/exf .
,ln -s ../input/obcs .
, andln -s ../input/tile001.mitgrid .
.
Making and Editing Files and Directories#
Editing Files#
We can make and edit files with a variety of different editors that often come pre-installed on command line interfaces.
One popular editor is the nano
editor which can be opened with the nano
command. For example, we can create and edit a README.txt
file with the following call:
nano README.txt
The nano
text editor will open in the terminal directly and will have the following appearance.
![The Nano Text Editor](../_images/nano_screenshot.png)
You can edit the text in the file directly. Then, when your edits are complete, use CTRL-X to save and exit.
Alternatively, we can use our native text editors to edit files directly. To create a file, we can use the touch
command.
touch notes.txt
Then, in MacOS, we can use TextEdit open and edit this file with
open -a TextEdit notes.txt
Similarly, on Windows, we can use the Notepad program with:
notepad.exe notes.txt
π€ Try it for yourself:#
Create a file called notes.txt
and add some notes to the file.
To see the text that the file contains, you can use cat
:
cat README.txt
The code above returns the following contents, assuming that the file was created with nano
as shown above:
This file contains information about the model configuration
To see just the start of the file, you can use head
:
head README.txt
To see just the end of the file, you can use tail
:
tail README.txt
Creating Directories#
Directories can also be creates and removed using tools on the command line.
To make a directory, we use use the mkdir
command (short for make directory). For example, suppose you wanted to make a directory called diags
. This can be achieved with
mkdir diags
Shell Profiles#
Each shell has a given profile - a script that is automatically run when you open a terminal. For example, the default terminal on MacOS is zshell which has the default shell profile in the .zshrc
file. Similarly, bash shells have profiles in .bashrc
and/or .bash_profile
. Note that the .
in front of each file ensures that the file is hidden.
All shell profiles should be placed in your home directory.
You can see your hidden profile in your home directory using the -a
flag on the ls
command with:
ls -a $HOME
To edit your profile, you can use the nano
editor as described above.
π€ Try it for yourself:#
Add a welcome message for yourself in your profile. Then, open a new terminal window to double check that your profile is working.
π‘ Solution:#
Use your favorite text editor to open your profile file. Then, use the echo
command to print a message, e.g. echo "Welcome Mike"
.
Deleting Files and Directories#
We can delete files with the rm
command - short for remove. For example, if we no longer wanted the temporary note that we made before, we could remove it with
rm tmp_note.txt
Warning
Deleting files in a Unix Environment is PERMANENT. There is no Trash or Recycle bin and there will not be a note asking you if youβre sure you would like to delete that file. Before using the rm
command, be sure to think for an extra second or two about what you intend to do.
To remove a directory, such as diags
, we can use the similar rmdir
command as
rmdir diags
One caveat (or perhaps safety net) of this command is that the directory must be empty before it is deleted. If you would like to remove the directory and all of the files inside of it, you can use the recursive option for the command as
rmdir -r diags
Again, caution should be used with deleting directories because the contents cannot be recovered after they are gone.
π€ Try it for yourself:#
Suppose you made some changes to the exf
package and need to recompile the model to reflect these changes. Remove all exf
files from the build directory.
π‘ Solution:#
rm ../build/exf*