Bash allows to give instructions to a Unix operating system. The first thing we need to know is how storage is organized on such as system.
Structure
The Unix filesystem is a rooted tree of directories. The root is denoted by /.
Several directories exist under the root. Here are a few:
/bin This is where binaries are stored.
/boot There, you can find the files necessary for booting the system.
/home This directory contains all the users home directories.
These directories in turn can contain other directories. /home for instance contains the directories:
/home/user001
/home/user002
/home/user003
…
The home directory of each user contain many files and directories.
Navigation
Working directory
The current working directory can be obtained with:
pwd# print working directory
Your turn:
What is your current working directory?
Changing directory
To navigate to another directory, you use cd (change directory) followed by the path of the directory.
Example:
cd /home
Because /home was the parent directory (one level above in the rooted tree) of our working directory, we could have also navigated there with cd .. — the two dots represent one level up (a single dot represents the working directory).
Your turn:
What do you think will happens if you run cd .. from /home?
What do you think will happens if you run cd . from /home?
From any location, you can always go back to your home directory (e.g. /home/user009) by running cd without argument. Alternatively, you can use cd ~. This is because ~ gets expanded by the shell into the path of your home. Finally, you can use cd $HOME. $HOME is an environment variable representing the path of your home.
Your turn:
Try using cd - (that’s the minus sign) a few times. What does this command do?
Absolute and relative paths
Absolute paths give the full path from the root (e.g. /bin, /home/user009/file).
Relative paths give the path relative to the working directory (e.g. ../dir/file, dir)
Your turn:
Is ~ an absolute or relative path?
In the filesystem below, names with edges represent directories and names without represent files. The current working directory is /home/user001.
What is the output of ls? (have a look at this section if you need a refresher)
What is the output of ls ../..?
The output of ls /thesis/src is:
ls: cannot access ‘/thesis/src’: No such file or directory
Why?
What are 2 ways to navigate to the results directory?
From the results directory, what are 2 ways to print the content of the src directory?
Creating files and directories
Files can be created with a text editor:
nano newfile.txt
The file actually gets created when you save it from within the text editor.
or with the command touch:
touch newfile.txt
This creates an empty file.
touch can create multiple files at once:
touch file1 file2 file3
New directories can be created with mkdir. This command can also accept multiple arguments to create multiple directories at once:
mkdir dir1 dir2
Deleting
Files can be deleted with rm.
Directories can be deleted with rm -r (recursive) or, if they are empty, with rmdir.
Be careful that these commands are irreversible.
Copying, moving, and renaming
Copying is done with the cp command.
Example:
cp thesis/src/script1 thesis/ms
Moving and renaming are both done with the mv command.
Examples:
# rename script1 to scriptmv thesis/src/script1 thesis/src/script# move graph1 to the ms directorymv thesis/results/graph1 thesis/ms# this also works:# mv thesis/results/graph1 thesis/ms/graph1