Home Classroom Linux Basics Linux Basics Part 3

Linux Basics Part 3

1166

File System Hierarchy – Lay of the Land

In the previous issue of diGIT we discussed a little background of Linux and what it is meant by a distribution (a distro). Then we went on to find how to log into a Linux systems and how to execute a few simple commands. Today we are going to find out how to find your way through a Linux file system.

New Term: File System

By the term File System I mean the structured organization of files and directories in a system. Please do not confuse this with other uses of the same term to note a technical implementation of such an organization. For example, by file system I mean the things like /usr directory tree. And the technical implementations like ext2/ext3/ext4 (Linux), FAT16/FAT32 and NTFS (Microsoft Windows) will be referred to as file system types. To avoid confusion the term will be used on lower case (i.e: file system) to identify the file organization.

Linux as many of the Unix systems has a hierarchical file system. Which means that the structure of the file organization is a tree-like structure with a single root point. So we call that point “the root”

New Term: Root

In Unix world the term Root can mean different things. When taken in a file system context, root refers to the single point of a file system hierarchy.

When taken in users and system privileges root means the Superuser (i.e: the user with all the administrative privileges, generally know as a system administrator). The root users home directory is usually located at /root. You should not confuse this with the root directory (i.e: /). Do not worry, you can see what these means in a few minutes.

 

Let us start with a simple commands we learned last week.

Eg 1:

[gaveen@ravana ~]$ ls

Documents   Pictures    Public    Templates   Videos

Desktop Download    Music Projects    Software

The root directory (i.e: /) is the topmost directory/location in a Unix file system. Within that directory there will be other files and subdirectories which will form the file system. Let us now have a look at what directories are there in a common Linux system and what are the purposes of them. However note that these standards are more like conventions rather than written-in-stone-rules. If you wish to stray away from the standard no one will stop you from doing just that, but if you plan to make your system public it is best to follow the standard. Besides that is the point of having a standard.

As you already know the ls command lists the contents of a directory, in this case the home directory. Using this command let us list the contents of the root directory.

Eg 2:

[gaveen@ravana ~]$ ls /

bin    boot   dev    etc    export fun    home

lib    media  mnt    opt    proc   root   sbin

srv    sys    tmp    usr    var

Your actual directory listing of / may be different than mine. However you should be able to figure out at least a few common items. Now let is take a deeper look into a file system. For this purpose I am going to show a listing of the file system tree/hierarchy of my Fedora Linux system. Do not worry about the names as some of the directories are created and named by me.

For the clarity I have ended each directory name with a “/” (Eg: bin/, rc.d/) where there are no “/” characters after a filename (Eg: passwd, shadow). The numbers/letters noted are there as future referencing points. The numbers notify a standard directory while an English letter notify a directory created by me (non-standard).

 

Now that we have a directory tree to examine let us start with how to note a directory and its path. Let us take the above examples for a moment. We already know that the home directory for user gaveen is in /home/gaveen and that the root directory is at “/”. Let us take some new examples to identify how you could correctly specify a file/directory location.

Eg 3:

Let us take location [A]. The directory name is “fun”. It is located directly under the root directory so we can specify the location as

/fun

If you decide to specify it as ‘/fun/’ it is still fine. The last “/” makes sure that you are referring to a directory and looks clear to you. If you do not use it, there is no harm done.

 

As you might have guessed, the starting “/” refers to the root directory. From now on you can be sure that any file/directory path starting with “/” means that we are specifying its location from the root directory.

Eg 4:

Let us now take the location [B]. According to what we have talked so far can you try to guess the path for the [B]? It is

/fun/Games

Note the uppercase letter G in Games. Since the directory we are trying to refer to is actually named “Games” it should be specified exactly as Games. Any other usage of the word such as games, GAMES, GaMeS, etc. are not valid this is because of one important thing.

New Concept: Case Sensitivity

It should be noted that Linux is case aware and differentiate based on the case. In simple words Linux identified the difference between capital ans simple (uppercase and lowercase) letters. Because of this you can perfectly have all the four files named file, FILE, File, FiLE within the same directory. Unlike some operating systems (Eg: MS-DOS, Microsoft Windows 9x, Windows XP, etc.) you may or may not have used, Linux treats the above four files as four different files. Which means that you should be specifying file/directory names and also commands exactly as it is. For example, you cannot use “LS” instead of “ls” command.

The technical reasoning behind this is most Linux file system types (including the current de facto standard ext3) are both case sensitive and case preserving. Which means that they will not only identify the different between cases but also preserve the case properties. This is contrast to the NTFS file system type one could find with Microsoft Windows XP and similar. NTFS is case preserving, but does not differentiate between upper and lowercase.

Let us again get back to the examples we are discussing.

Eg 5:

Let us take [N] and try to specify its path. So it starts with “/” and the path goes like this

/home/gaveen/Software/Setups/NMS/Zenoss Core

But if you try to cd into it using this path it might produce an error similar to what you see below.

[gaveen@ravana ~]$ cd /home/gaveen/Software/Setups/NMS/Zenoss Core

bash: cd: /home/gaveen/Software/Setups/NMS/Zenoss: No such file or directory

So what went wrong? Why the system is trying to access a directory named Zenoss even though we wanted to access “Zenoss Core”?

The reason is the space in the directory name. While a space (whitespace) is a perfectly valid character to use in file and directory naming, Linux command line (in this case stated as “bash”) has a special use of the space and treats the space as a special character. To overcome this we can do a little trick.

Actually there are more than one way of doing this. The most common way is to tell the Linux command line that the space is not a special character. We can do this by adding a “\” (backslash) before a space (in a file/directory name) to escape the treatment of a special character.

[gaveen@ravana ~]$ cd /home/gaveen/Software/Setups/NMS/Zenoss\ Core

While this is the preferred way let us check another workaround.

[gaveen@ravana ~]$ cd “/home/gaveen/Software/Setups/NMS/Zenoss Core”

Both these techniques work. But the first one is used mostly because it works well with the tab-completion feature of the command line. We can discuss tab completion and a few more command line features in the next issue.

Exercise: Specify the paths in the above tree diagrams

Now that we know how to refer to any file/directory in a file system you can test your knowledge by specifying paths to all the other files and directories. Try to complete the listings from A – O and 1 – 17.

Absolute paths and relative paths

You can now traverse a Linux file system. For example let us go the location noted by [G].

Eg 6:

[gaveen@ravana ~]$ cd /home/gaveen/Software/Setups/

[gaveen@ravana Setups]$ pwd

/home/gaveen/Software/Setups/

As you can remember the pwd command will assure that you are inside the Setups directory.

Now what would you do if you want to change into [H]? You can try the following of course.

[gaveen@ravana ~]$ cd /home/gaveen/Software/Setups/Development

[gaveen@ravana Development]$ pwd

/home/gaveen/Software/Setups/Development

If you are feeling a little frustrated about the repetitive typing you had to do, there is goos news. You do not have to type the full path always. Let us see.

First you are already in “/home/gaveen/Software/Setups/” after the example 6. The directory named “Development” is right under that directory. Rather than typing in the full path you can just say go to the “Development” directory from this point. This is how you do it.

Eg 7:

[gaveen@ravana Setups]$ pwd

/home/gaveen/Software/Setups/

[gaveen@ravana Setups]$ cd Development/

[gaveen@ravana Development]$ pwd

/home/gaveen/Software/Setups/Development

There is one important thing to notice here. When you entered “cd Development”, there was no “/”(forward slash) before ‘Development’, thus not asking the command line to start from the root directory. This short variation of path is known as a relative path.

New Term: Absolute/Full Path

An absolute path is a path specified starting from the root directory. Eg: /home/gaveen/Software/Setups/Development

New Term: Relative Path

A relative path is a path specified from your current location on a file system. Which means the path you specify is relative to your current location. Because of this if you specify a relative path from a different working location (pwd) it will not be correct. The point is that a relative path is always relative to your current working directory and does not start with a “/”. Eg: Development/

The “.” and the “..”

If you have been following this far you might be wondering what to do in a case like this.

Eg 8:

[gaveen@ravana Development]$ pwd

/home/gaveen/Software/Setups/Development

You are in “/home/gaveen/Software/Setups/Development” but want to get to [K] which is “/home/gaveen/Software/Setups/Servers”.

As you can see both ‘Development’ and ‘Servers’ directories are located within a common parent directory “Setups”. But you are currently located one level below the ‘Setups’ directory (i.e: Setups/Development). You already know that we have to use the “cd” command. And you may be thinking of specifying the full path “Servers” directory. However there is a shortcut.

Eg 9:

[gaveen@ravana Development]$ pwd

/home/gaveen/Software/Setups/Development

[gaveen@ravana Development]$ cd ..

[gaveen@ravana Setups]$ pwd

/home/gaveen/Software/Setups

[gaveen@ravana Setups]$ cd Servers/

[gaveen@ravana Servers]$ pwd

/home/gaveen/Software/Servers

What we used was the “..” with the “cd” command. That is two dots.

New Concept: “.” and “..”

In Unix/Linux file and directory path convention the “..” refers to the parent directory of your current working location while the “.” refers to your current working directory.

So the work in example 9 can be done like the following way too.

[gaveen@ravana Development]$ pwd

/home/gaveen/Software/Setups/Development

[gaveen@ravana Development]$ cd ../Servers/

[gaveen@ravana Servers]$ pwd

/home/gaveen/Software/Servers

As you can see we combined our understanding of relative paths and the “..” notation to get it done.

Here is a few useful hints to make your life easy when you browse through your file system.

$ cd ~

or just

$ cd

shall take you directly to your home directory. While

$ cd -

shall take you to the directory where you were before your last “cd” command.

There is one last tip I have to share before we conclude this issues article. That is, when you need to change into a directory quickly but temporarily and change back again. Say you are in “/home/gaveen/Software/Servers” and have a quick change into “/fun” and get back. This is how you do it.

Eg 10:

[gaveen@ravana Servers]$ pwd

/home/gaveen/Software/Servers

[gaveen@ravana Servers]$ pushd /fun

/home/gaveen/Software/Servers /fun

[gaveen@ravana fun]$ pwd

/fun

[gaveen@ravana fun]$ popd

/home/gaveen/Software/Servers

[gaveen@ravana Servers]$ pwd

/home/gaveen/Software/Servers

Please note that the “pwd” commands listed in all the examples are there for the clarity. You are not required to type all those commands for just the functionality. For example you only need the “pushd” and “popd” commands from example 10.

Next Issue

Congratulations! Now you know everything you need to know in order to have a comfortable traversing of a Linux file system.

In next issue we can discuss more tips, hints and details about the Linux command line/shell. Until then have a good read of this important topic of file system and try to do the little work I mentioned above. If you are not clear about what are all the directories about, do not worry. We can discuss their purpose too in the next issue.

Comments

comments

Gaveen is a Linux System Administrator with several years of industry experience and is a strong FOSS promoter. He is pending graduation from University of Wolverhampton and also holds a certification from Red Hat. He also conducts Linux trainings with Red Hat training partners. Being highly enthusiastic about computing, he supports a keen interest in Ruby programming too. When not distracted by his wide range of interests including reading, music, cricket, etc.,he regularly blogs at Gaveen's Blog

NO COMMENTS

Leave a Reply