2.2 Directory operation (pwd, cd, mkdir, rmdir)



Next, we learn about the directory. After learning two basic commands (pwd, cd), we learn about some special directories.

2.2.1 Display current directory (pwd)

The directory has a hierarchical structure. This pwd (Print Working Directory) is used to display the current position in that hierarchy. Enter as follows.

Format

pwd


Example

$ pwd ↵
/home/okada↵


Currently, you can see that this user is in the directory /home/okada. / is a symbol indicating a directory break.

2.2.2 Change directory (cd)

You can change the current directory. That is the cd (Change Directory) command. Specify the destination directory. If you do not specify a directory, you will move to the home directory (described below). Let's move from the /home/okada directory to the /usr directory.

Format

cd [directory]


Practice: Move directory


$ pwd ↵
/home/okada
$ cd /usr ↵
$ pwd ↵
/usr


Moving directory succeeded.

2.2.3 Make directory (mkdir)

The command to make a directory is mkdir.

Format

mkdir directory


Options

-p
Remove also upper directories of the specified directory.

If you want to make a directory dir2 under the directory dir1 and make a directory dir3 under the directory dir2, then you can enter follows.

Example

$ mkdir dir1 ↵           (without -p option)
$ mkdir dir1/dir2 ↵
$ mkdir dir1/dir2/dir3 ↵


It executed the mkdir command in three times. Because there is a restriction that it is not possible to make the directory if the upper directory does not exist. It is possible to execute it at a time as follows by the use of -p option.

Example

$ mkdir -p dir1/dir2/dir3 ↵            ( with -p option)


Practice: Execute rmdir with -p option


$ mkdir -p dir1/dir2/dir3 ↵           (directory dir1/dir2/dir3 was made)
$ ls -l
drwxr-xr-x 2 okada okada 4096 Mar 16 07:46 Desktop
drwxr-xr-x 2 okada okada 4096 Mar 16 09:30 dir1
-rw-rw-r-- 1 okada okada 187 Mar 16 08:59 hosts.bak
-rw-rw-r-- 1 okada okada 187 Mar 16 09:05 hosts.new2
$ cd dir1
$ ls -l
drwxr-xr-x 2 okada okada 4096 Mar 16 09:30 dir2
$ cd dir2
$ ls -l
drwxr-xr-x 2 okada okada 4096 Mar 16 09:30 dir3


2.2.4 Remove directory (rmdir)

The command that remove directory is rmdir.

Format

rmdir directory


Options

-p
Remove also upper directories of the specified directory.


The feature of rmdir is to be able to delete the directory only when it is empty. When the file exists in the inside, it is not possible to delete it. There is -r option in rm command(2.1.5). With rm - r, you can delete the directory regardless of the existence of the file.

Practice: Execute rmdir with -p option


$ touch dir1/dir2/dir3/file       create file in dir3
$ rmdir -p dir1/dir2/dir3↵
            if some files exists in dir1,dir2,dir3, we can’t remove the directory
rmdir: `dir1/dir2/dir3/' : directory is not empty
$ rm -r dir1/dir2/dir3
            rm command with -r option, we can remove all files in specific directory


2.2.5 Special directories

In Linux, there are special directories and their symbols representing them. The following are the typical ones.


This is the current working directory. "." is called as "Period" or "Dot".


It is a directory one level higher. If you are currently in /home/okada, /home is the parent directory.


It is a directory that becomes user's work beginning position. The user right after login is always in the home directory. The home directory is different for each user. "~" is called as "tilde".


It is the highest level directory of the hierarchy.

2.2.6 Absolute path and Relative path

It is assumed that you are in the directory of /usr/local. At this time, there are two ways to specify the file /usr/bin/xxx.

1. /usr/bin/xxx
2. ../bin/xxx

In case of 1, the directory/file name is specified from the most significant directory (/). In case of 2, the directory/file name is specified from your current working directory. The first way is called as the absolute (path) specification, and the second way is called as the relative (path) specification.

In case of absolute (path) specification, the path are always same, even if you change your working directory. In case of relative (path) specification, the path description should be changed when you move to other directory. In addition, absolute (path) specification is always specified from /, so the path description may become very long. The relative (path) specification is characterized in that, when the directory is very close from the current position, the path description can be short.

In this case, we assume that the home directory of user okada is /home/okada.

Figure2-2
Figure 2-2: /home and other directories structure


Practice: Move to various directories 1


$ pwd ↵
/home/okada       Now, we are at /home/okada (1.)
$ cd .. ↵       Move to the parent directory (2.)
$ pwd ↵
/home $ cd /usr ↵       Move to /usr (3.)
$ pwd ↵
/usr
$ cd ~ ↵       Move to the home directroy (4.)
$ pwd ↵
/home/okada $ cd ../../ ↵       Move to two's higher directory. (5.)
$ pwd ↵
/
$ cd ↵       When nothing specified, move to the home directory (6.)
$ pwd ↵
/home/okada



Practice: Move to various directories 2


Then, let's move to various directories.


$cd / ↵       Move to the root directory
$pwd ↵
/       We are in the root directory
$cd ~ ↵       Move to the home directory
$pwd ↵
/home/okada       We are in the home directory. Home directory is different for each user.
$cd .. ↵       Move to the parent directory
$pwd ↵
/home
$cd ../usr ↵       Move to usr directory under the parent directory
$pwd ↵
/usr       We are in /usr


We could look at how we moved.


Previous Next