2 Shell basics
Duration: 75 minutes
- You have learned what the Shell is.
- You know and have practived using the shell to…
- … navigate the file system
- … start programs
- … create, move and remove files
2.1 What is the Shell?
This session consists of some slides that are available here.
2.3 mkdir
The mkdir
command is used to create directories.
mkdir my-new-directory
You can also create multiple directories at once.
mkdir my-new-directory my-other-directory
You can create nested directories by using the -p
option. The -p
option will create all parent directories if they do not exist.
mkdir -p my-new-directory/nested-directory
2.4 cd
The cd
command is used to change directories.
cd my-directory
You can also use cd
with a path to navigate to a directory that is not in the current working directory.
cd /path/to/my-directory
..
refers to the parent directory.
refers to the current directory
Create the following (or similar) directory structure.
my-research-project/
data/
raw/
clean/
analysis/
results/
doc/
2.5 Understanding paths
Paths are used to specify the location of a file or directory in the file system.
They can be either absolute or relative. Absolute paths can be seen as the full address of a file or directory, whereas relative paths are relative to the current working directory. They can be seen as asking a local in the neighborhood for directions.
In Linux absolute paths look like this: /home/username/my-research-project/data/raw
Under MacOS they look like this: /Users/username/my-research-project/data/raw
And in Windows: C:\Users\username\my-research-project\data\raw
The home directory is the directory where you land when you open a terminal. It is usually /home/username
in Linux, /Users/username
in MacOS and C:\Users\username
in Windows.
To navigate to the home directory you can use the cd
command without any arguments.
2.6 Writing and viewing files
2.6.1 echo
The echo
command is used to print text to the terminal. When writing scripts, it is often used to print messages to the user.
echo "Hello, world!"
2.6.2 nano
nano
is a simple text editor that is often used in the shell.
To open a file in nano
:
nano my-file.txt
You can also create a new file by providing a name that does not exist.
To save and exit nano
: - Press Ctrl
+ O
to save - Press Enter
to confirm the file name
When you start nano
without providing a file name, you can specify the filename when you save the file (Ctrl
+ O
).
- Press
Ctrl
+X
to exit without saving.
nano’s commands are displayed at the bottom of the screen. They can be invoked by pressing Ctrl
and the corresponding letter.
2.6.3 less
less
is a program that allows you to view the contents of a file.
less my-file.txt
Navigate through the file using the arrow keys, or Page Up and Page Down.
To exit less
, press q
.
2.6.4 > and >>
The >
and >>
operators are used to redirect the output of a command to a file.
>
will overwrite the file if it already exists>>
will append the output to the file
echo "Hello, world!" > my-file.txt
echo "Hello, again!" >> my-file.txt
2.7 General syntax of a shell command
command -option argument
command
is the name of the command-option
is an option that modifies the behavior of the commandargument
is the input to the command
Options are usually preceded by a -
or --
. They can be combined, e.g. -a -b -c
can be written as -abc
.
Use the commands you have learned to create a README file in your research directory. Try various commands to create the file, modify it, and view its contents.
2.8 Moving and removing files
2.8.1 mv
The mv
command is used to move files and directories.
mv my-file.txt my-research-project/doc/
It can also be used to rename files.
mv my-file.txt my-new-file.txt
2.8.2 cp
The cp
command is used to copy files and directories.
cp my-file.txt my-file-copy.txt
To copy a directory, you need to use the -r
option.
cp -r my-research-project my-research-project-copy
2.8.3 rm
rm
The rm
command will permanently delete files and directories. There is no way to recover them.
When you are unsure about using rm, you can use the -i
option to prompt you before deleting each file.
The rm
command is used to remove files and directories.
rm my-file.txt
To remove a directory, you need to use the -r
option.
rm -r test/
2.9 Getting Help
The man
command is used to display the manual page of a command.
Often, you can also use the --help
option to get a brief overview of the command and its options.
For example
cp --help
2.10 Summary
- The shell is a powerful tool for interacting with the file system and running programs.
- You can navigate the file system using commands like
cd
,ls
, andpwd
. - You can create, move, and remove files using commands like
touch
,mv
,cp
, andrm
. - You can view the contents of files using commands like
cat
,less
, andnano
. - You can redirect the output of commands to files using
>
and>>
. - You can get help on commands using the
man
command or the--help
option.