Changing Directory

Unix

The Unix File System

In Unix like operating system the files are organized in a hierarchical directory structure which is similar to a tree like structure. The first directory in this directory structure is called the root directory and it contains all the other files and subdirectories.

The root directory is reprented by /

Print Current Directory

At any point of time, we are inside some directory and it becomes the current working directory for us. The current working directory will contain some files and path to its parent directory which lies above it and may contain some subdirectories which lies below it in the hierarchical directory structure.

In order to know the current working directory we use the pwd (print working directory) command.

[yusuf@linux ~]$ pwd

Listing Directory Content

In order to list the content of a directory we use the ls command.

[yusuf@linux ~]$ ls

If we want to list the content of a specific directory, say content of the etc directory which is inside the root directory we write the following.

[yusuf@linux ~]$ ls /etc

If we want to list the content of a multiple directories, say content of the etc and usr directory which is inside the root directory we write the following.

[yusuf@linux ~]$ ls /etc /usr
/etc:

---- content of /etc directory ---


/usr:

---- content of /usr directory ---

If we want to list the content of a directory in long format we type the following command.

[yusufshakeel@linux GitHub]$ ls -l
total 0
drwxr-xr-x  13 yusufshakeel  staff   442 May 15  2015 C-Project
drwxr-xr-x   7 yusufshakeel  staff   238 Mar 24  2015 Php-Project
drwxr-xr-x  12 yusufshakeel  staff   408 Sep  4 18:14 UglifyJS2
drwxr-xr-x  18 yusufshakeel  staff   612 Mar 24  2015 Web-App
drwxr-xr-x  21 yusufshakeel  staff   714 Aug 15  2015 bootstrap-datetimepicker
drwxr-xr-x   9 yusufshakeel  staff   306 Aug 23 21:22 chartjs
drwxr-xr-x   8 yusufshakeel  staff   272 Jul 24  2015 database-tutorial
drwxr-xr-x  14 yusufshakeel  staff   476 Aug 14  2015 datetimepicker
drwxr-xr-x  13 yusufshakeel  staff   442 Jun 29  2015 fonts
drwxr-xr-x   8 yusufshakeel  staff   272 Jun 13  2015 gcm
drwxr-xr-x   9 yusufshakeel  staff   306 Jan  9 14:16 google-services
drwxr-xr-x  21 yusufshakeel  staff   714 Nov 24 18:22 jQuery-File-Upload
drwxr-xr-x  23 yusufshakeel  staff   782 Oct  3 15:56 jquery_jeditable
drwxr-xr-x  31 yusufshakeel  staff  1054 Aug 14  2015 moment
drwxr-xr-x  38 yusufshakeel  staff  1292 May 29  2015 phpredis
drwxr-xr-x  14 yusufshakeel  staff   476 Sep  6 10:29 yusufshakeel.github.io
drwxr-xr-x  43 yusufshakeel  staff  1462 May 30  2015 zguide

Long format fields

FieldDescription
drwxr-xr-xAccess right to the file or directory. If the first character is a dash - then its a regular file. If the first character is d then its a directory. Next 3 characters defines the access rights of the owner. The next 3 characters defines the access rights of the group. Finally the last three characters defines the access right for everyone else.
13Number of hard links.
yusufshakeelUsername of the owner.
staffName of the group to which the file or directory belongs.
442Size of the file in bytes
May 15 2015Last modified timestamp.
C-ProjectName of the file or directory

Options and Arguments

Commands are often followed by some option which alters the behaviour of the command, and further, by some arguments upon which the command takes some action.

Following is a general format of a command.

[yusuf@linux ~]$ command -options arguments

We saw the ls option -l which listed the content of a directory in long format.

Commonly used options of ls command

OptionDescription
-aList all the files including the hidden files which normally start with a dot. For example .log
-FThis will put a / at the end listed names that are directories.
-hThis will display the file size in human readable format rather than in bytes.
-rThis will display the result in reverse order.
-SThis will sort the result by file size.
-tThis will sort the result by last modified time.

Changing directories

To change from one directory to the another we use the cd command.

[yusuf@linux ~]$ cd /etc

It will change the current working directory to etc directory inside the root directory.

Absolute and Relative Pathname

We can use absolute and relative pathname to change directory. An absolute pathname for a file or directory will start from the root directory /. So if we want to change our current working directory from /home/yusufshakeel to etc directory inside the root directory, we will use the following absolute path.

[yusuf@linux ~]$ cd /etc

A relative pathname starts from the current working directory. So if we consider the following directory structure.

yusuf
 |
 +--doc
 |  |
 |  +-- sample.txt
 |
 +--happy.c
 |
 +--sheet
    |
    +--v1
    |  |
    |  +--a.csv
    |
    +--main.csv

If our current working directory is yusuf then if we want to change the directory to doc, we will type in the following command.

[yusuf@linux ~]$ cd doc

Similarly, if we want to change the directory from yusuf to v1 which is inside the sheet directory, we will type the following relative pathname

[yusuf@linux ~]$ cd sheet/v1

Moving to parent directory

To move to the parent directory of the current working directory, we use the following command.

[yusuf@linux ~]$ cd ..

The file command

The file command is used to get a brief description of file.

[yusuf@linux ~]$ file test.php 
test.php: PHP script text