Linux cd command - explore Linux directory file system

Linux cd command is a command used to change directory in Linux command line terminal. The command just did that. There is no option available for the cd command. Changing directory in Linux is common. So you are going to use Linux cd command a lot when you are working in the Linux command line terminal.

Maybe you think that Linux cd command is easy to use. Of course it has no option, so you don't have to worry about that. But using cd command in Linux command line is not as easy as you think. Why? Because when we are using cd command, we are dealing with the Linux file system directory. The Linux cd command itself is simple but the Linux file system directory is not. So the main purpose of this tutorial is to use the Linux cd command to introduce the basic Linux file system directory to a Linux beginner.

Linux cd command introduction

Here is the manual page for cd command:

NAME
       cd - Change working directory

SYNOPSIS
       cd ?dirName?
_________________________________________________________________

DESCRIPTION
       Change  the current working directory to dirName, or to the home direc-
       tory (as specified in the HOME environment variable) if dirName is  not
       given.   Returns an empty string.  Note that the current working direc-
       tory is a per-process resource; the  cd  command  changes  the  working
       directory  for  all  interpreters  and  (in a threaded environment) all
       threads.

From the manual page, we can understand that what Linux cd command does is to change working directory. The description tells us that, to change directory, use cd dirName where the dirName is the target directory. If cd command was issued without argument, then it'll takes you to your home directory. Let's see an example of this.

Linux cd command examples

First, let's confirm our our home directory. We can check our home directory with Linux env command.

luzar@slackware:~$ env | grep HOME
JAVA_HOME=/usr/lib/java
HOME=/home/luzar
luzar@slackware:~$

That is our home directory. Now we can change to other directory with cd command syntax mentioned above. Let's say our target directory is / directory. So the Linux cd command would be:

luzar@slackware:~$ cd /
luzar@slackware:/$

We are now at the / directory. In Linux (and Unix) we call / a root directory. We are now at the highest directory level in the Linux file system. Please don't confused / directory and /root directory. Both are called root directory but the former is the highest directory in Linux and the latter is a home directory for the Linux lord, the root user. If we list / directory content, we can see that /root directory is in the / directory.

luzar@slackware:/$ ls
bin/   dev/  home/  lost+found/  mnt/  proc/  sbin/  sys/  usr/
boot/  etc/  lib/   media/       opt/  root/  srv/   tmp/  var/
luzar@slackware:/$

We can say that /root directory is a child directory and the / directory is a parent directory.

Now let's get back to our main purpose, which is to demonstrate description from the cd manual page. If we check our working directory now, we would be in / directory. We can prove that with Linux pwd command. The cd manual description states that if dirName is not given, the cd command would take us back to our home directory, which is /home/luzar. Let's see:

luzar@slackware:/$ pwd
/
luzar@slackware:/$ cd
luzar@slackware:~$ pwd
/home/luzar
luzar@slackware:~$

The example above proved it.

Let us see some more examples about Linux cd command. This time, please remember all the characters and their meaning below:




Character Meaning
. Current directory
~ Home directory
.. Upper directory or parent directory

Use ls -a to list all contents including hidden files in our current directory.

luzar@slackware:~$ ls -a
./                       .kde/       .qt/
../                      .kderc      .screenrc

The file which name starts with . such as .screenrc is a hidden file. Just ignore them for now because they belong to other topic. The important here are those in blue color. Refer to the table above, can you guess what they are? Yes. the single dot (.) is current directory and double dot (..) is upper directory. What happen if we cd into them?

luzar@slackware:~$ cd .
luzar@slackware:~$ pwd
/home/luzar
luzar@slackware:~$ cd ..
luzar@slackware:/home$ pwd
/home
luzar@slackware:/home$

When we issue cd . command, we still in the same working directory. Yes, because single dot (.) means current directory. But when we issue cd .., we were taken to the home directory. Why? Because double dot (..) means upper directory, and /home directory is our parent directory when we issue the command.

That's it. We have covered most of the important part of the Linux file system directory using Linux cd command. There are many other things in Linux file system directory that we can explore with the Linux cd command. We'll look at them later in other topics.

Back to Linux basic commands main page.


Post new comment

The content of this field is kept private and will not be shown publicly.
This blog uses the CommentLuv Drupal plugin which will try and parse your sites feed and display a link to your last post, please be patient while it tries to find it for you.

Custom Search