How To Change Directory In Linux

Change Directory In Linux
Change Directory In Linux.

Introduction

This guide will show you how to navigate around your file system using the Linux terminal.

Your computer will have at least one drive which is required to boot the operating system. The drive you boot from is generally a hard drive or SSD but can be a DVD drive or USB drive.

The operating system on your computer will provide a naming mechanism so that you can interact with each of the drives.

If you are used to the Windows operating system then you will be aware that each drive is given a drive letter.

The general naming convention is as follows:

  • A - Lecacy drive letter commonly used for floppy disks
  • B - Legacy drive letter commonly used for a secondary floppy disk
  • C - Main drive used to boot the operating system
  • D - DVD drive
  • E onwards - USB drives, external network drives etc

Each drive will be split into a tree consisting of folders and files. For instance a typical C drive might look something like this:

  • C:\
    • ​Users​
      • ​Your Username
        • ​Document Settings
          • My Documents
          • ​My Videos
          • My Music
    • Windows
      • ​system32
      • syswow64
    • Program Files
      • ​office
      • skype

​The contents on your C drive will differ and the above is just an example but as you can see the top level is the drive letter and then there are three folders underneath (users, windows, program files). Under each of these folders there will be other folders and below those folders more folders.

Within Windows you can navigate around the folders by clicking on them within Windows Explorer. You can also open a command prompt and use the Windows cd command to navigate around the folder structure.

Linux also provides a method for naming drives. A drive in Linux is known as a device so every drive starts with "/dev" because devices are treated like files.

The next 2 letters refer to the type of drive. Modern computers tend to use SCSI drives and therefore this is shortened to "SD".

The third letter starts at "A" and for each new drive it moves up a letter. (i.e: B,C,D). Therefore commonly the first drive will be called "SDA" and more often than not is either the SSD or hard drive used to boot the system. "SDB" usually refers either to a second hard drive, a USB drive or external hard drive. Each subsequent drive gets the next letter along.

Finally there is a number which denotes the partition.

A standard harddrive therefore is usually called /dev/sda with individual partitions called /dev/sda1, /dev/sda2 etc.

Most Linux distributions provide a graphical file manager similar to Windows Explorer. However as with Windows you can use the Linux command line to navigate around your file system. 

Your Linux system is laid out in a tree format with the / directory at the very top and various other directories underneath.

Common folders under the / directory are as follows:

  • bin
  • boot
  • cdrom
  • dev
  • etc
  • home
  • lib
  • lib64
  • lost+found
  • media
  • mnt
  • opt
  • proc
  • root
  • run
  • sbin
  • srv
  • sys
  • tmp
  • usr
  • var

You can find out what all these folders are used for by reading this guide showing 10 essential command for navigating the file system using Linux.

Basic Navigation Using The cd Command

Most of the time you will want to work within the confines of your home folder. The structure of your home folder is much like the "My Documents" folders within Windows.

Imagine you have the following folder setup under your home folder:

  • Home
    • Username
      • Pictures
        • Family Photos
        • Holiday Photos
        • Christmas Photos
      • Downloads
      • Videos
        • Christmas 2015
        • Christmas 2014
      • Music
        • Reggae
        • Jazz
        • Rock

When you open a terminal window you will generally find yourself in your home folder. You can confirm this using the pwd command.

pwd

The results will be something along the lines of /home/username.

You can always get back to the /home/username folder by typing the cd tilde command:

cd ~

Imagine you are in the /home/username folder and you want to get to the Christmas Photos folder.

You can do it in many different ways.

For example you can run a series of cd commands as follows:

cd Pictures
cd "Christmas Photos"

The first command would move you down from the username folder down to the Pictures folder. The second command takes you down from the Pictures folder to the Christmas Photos folder. Note that "Christmas Photos" is in quotes as there is a space in the folder name.

You can also use the backslash instead of the quotes to escape the space in the command. For example:

cd Christmas\ Photos

Instead of using two commands you could have just used the one as follows:

cd Pictures/Christmas\ Photos

If you weren't in the home folder and you were in a much higher level folder such as / you can do one of a number of things.

You could specify the entire path as follows:

cd /home/username/Pictures/Christmas\ Photos

You could also use the tilde to get to the home folder and then run the command as follows:

cd ~
cd Pictures/Christmas\ Photos

The other way is to use the tilde all in one command as follows:

cd ~/Pictures/Christmas\ Photos

What this means is that it doesn't matter where you are in the file system you can get to any folder below the home folder by using the notation ~/ as the first characters in the path.

This helps when trying to get from one low level folder to another. For example imagine you are in the Christmas Photos folder and now you want to go to the Reggae folder which is under the Music folder.

You could do the following:

cd ..
cd ..
cd Music
cd Reggae

The two dots signify that you want to go up a directory. If you want to go up two directories you would use the following syntax:

cd ../..

And three?

cd ../../..

You could have specified the cd command all in one command as follows:

cd ../../Music/Reggae

Whilst this works it is much better to use the following syntax as it saves you having to work out how many levels you need to go up before going down again:

cd ~/Music/Reggae

Symbolic Links

If you have symbolic links it is worth knowing about a couple of switches which define the behaviour of the cd command when following them.

Imagine that I created a symbolic link to the Christmas Photos folder called Christmas_Photos. This would save having to use the backslash when navigating to the Christmas Photos folder. (Renaming the folder would probably be a better idea).

The structure now looks like this:

  • Home
    • Username
      • Pictures
        • Family Photos
        • Holiday Photos
        • Christmas Photos
        • Christmas_Photos
      • Downloads
      • Videos
        • Christmas 2015
        • Christmas 2014
      • Music
        • Reggae
        • Jazz
        • Rock

The Christmas_Photos folder isn't a folder at all. It is a link pointing to the Christmas Photos folder.

If you run the cd command against a symbolic link which points to a folder you will be able to see all the files and folders within that folder.

According to the manual page for CD the default behaviour is to follow symbolic links.

For example look at the command below

cd ~/Pictures/Christmas_Photos

If you run the pwd command after running this command you will get the following result.

/home/username/Pictures/Christmas_Photos

To force this behaviour you can use the following command:

cd -L ~/Pictures/Christmas_Photos

If you want to use the physical path you need to enter the following command:

cd -P ~/Pictures/Christmas_Photos

Now when you run the pwd command you will see the following results:

/home/username/Pictures/Christmas Photos

Summary

This guide has shown you everything you need to know in order to successfully work your way around the file system using the Linux command line. 

To find out about all of the potential options click here for the cd manual page.

More From Us