Linux Directory Structure have covered how directories provide a tree like structure for the file system.

Directories are just containers for files and directories. Directories can be accessed by their name, a full path, or shortcut.

Directory Shortcuts

When working on Linux systems, a period (.) is often referred to as dot. This is no different than hotmail do com instead of hotmail period come. So, in Linux a single dot represents this or current directory. Two dots represent the parent directory. You can think of the parent directory as being the directory above your current directory. The cd command provides a shortcut too. If you type cd - , then you will be placed into your previous directory. In addition, if you type cd alone, then you will be placed into your home directory e.g. /home/robin .

.This or current directory
..The parent directory
cd -Change to the previous directory
cdChange to home directory

Directory Separator

The directory separator in Linux is the forward slash /. So you can think of directories as ending in /, and often this is understood. For example, if you run cd /home/robin , that is the same thing as running cd /home/robin/.

Executing Commands

In this section, we have talked about the environment variable of $PATH determines your command search path. You can specify a command with a full path. You can execute commands that are not in your path. And if you want to execute a command in the current directory you are in, you'd type ./command.

The $PATH environment variable determines where your shell will look for commands.

$ echo $PATH

The which tells us that if we run cat , that it would run bin/cat

$ which cat
$ cat sample.txt
hello world! This is content. 

We can also specify the full path to that and we get the same result.

$ /bin/cat sample.txt
hello world! This is content. 

However, if we want to run a different cat, then we could do something like this:

$ /home/robin/new_cat/cat
This is a new cat. Hi ~ 

Or, you can cd into this directory and run the command like this:

$ cd /home/robin/new_cat
$ ./cat
This is a new cat. Hi ~ 

Creating and Removing Directories

If you want to create a directory you can use the mkdir command.

Note: parameter in the breakets means it is optional.

mkdir [-p] directoryCreate a directory
rmdir [-p] directoryOnly remove directories that are empty (no file content)
rm -rf directoryRecursively delete everything (all files and directories) in that directory.

The -p is optional here. The -p stands for parents so if we want to create a directory structure that is more than just one directory deep we need to specify -p, so that it will create the parent directories.

For example:
$ mkdir -p one/two/three

Important: An important thing to note when you are working at the command line in Linux, there is no undo. When you delete something, it is gone. When you recursively delete something, all those files and directories are gone. You can't get them back. So, be careful when you are using rm -rf. One suggestion is to run ls to check the directories and files before rm.

Examples mkdir, rmdir and rm:

mkdir won't be able to create the threedir/ directory because its parent directory twodir/ does not yet exist.

$ mkdir onedir/twodir/threedir
mkdir: cannot create directory 'onedir/twodir/threedir': No such file or directory

In order to create a depth 3 directory, you need to specify -p:

$ mkdir -p onedir/twodir/threedir

Again, the rmdir command only remove empty directories.

$ rmdir onedir
rmdir: failed to remove 'onedir': Directory not empty

If you want to recursively remove them, you can type:

$ rm -rf onedir