Navigating Files and Folders

The cd command lets you change directories. It’s the tool for changing our current working directory, or the “location” on the filesystem. As you’ve already seen, using cd by itself takes you straight to your home directory. Execute this command in your shell right now:

 $ ​​cd

Then, execute the pwd command to check your location. You will then see a file path displayed:

 $ ​​pwd
 /home/brian

Each slash in the path represents a part of the hierarchy. So, /home/brian is just another way to represent this:

 /
 └── home/
    └── brian

If you’ve built a website and referenced images or CSS files, or written code to work with files, you’re already familiar with the concept of specifying paths to files.

Your home directory is just one part of the filesystem. The filesystem is a hierarchy of files and directories, all starting at the root directory, which you represent with the forward slash character (/).

Other disks and filesystems are mounted as child directories on this filesystem. If you’re familiar with Windows operating systems, you may be used to having a C: drive or a D: drive, each with its own collection of files and directories. But on a Unix-like system, disks are mounted, or attached, to a single tree of directories and files. This includes optical media like DVDs and external disks. To access them, you will navigate to the directory that points to the device.

The root filesystem is the name given to the filesystem on a Unix-like system that contains the root directory (/) and where all other disks and filesystems are mounted.

The following figure illustrates this hierarchy:

images/file_system/structure.png

 

This figure shows the most common directories on a Unix-like filesystem. Here’s what each folder is for:

  • The filesystem root is represented by /, a single forward slash.

  • The /bin folder traditionally contains base commands and programs necessary to operate the operating system.

  • The /etc folder contains configuration files for system-wide applications, such as web servers or database servers.

  • The /home folder contains the home folders for users on the system. On macOS, this folder is called Users instead.

  • The /usr/bin folder contains additional commands and programs that build on top of those in the bin folder.

  • The /usr/local folder is where you can install your own system-wide commands and utilities.

  • The /opt folder is often where you’ll find, or install, software installed from source. This is a good place to put alternative versions of software so they won’t conflict with existing versions.

  • The /var folder is where programs typically put their data. For example, on many Linux systems, log files for various programs are stored in the /var/log directory. On a web server, you’ll often find web pages in /var/www.

  • The /tmp folder is a system-wide place for temporary files. It’s readable and writable by everyone on the system.

Using the cd command, you can navigate to any place on the filesystem using a single command. To jump to the root of the filesystem, execute this command:

 $ ​​cd​​ ​​/

Use the pwd command and you’ll see that the current working directory is /. Use the ls command to get a list of the directories and you’ll see something like the following:

 $ ​​ls
 bin dev initrd.img lib64 mnt root snap sys var
 boot etc initrd.img.old lost+found opt run srv tmp vmlinuz
 cdrom home lib media proc sbin swapfile usr

You’ll see the bin folder, the var folder, and the usr folder you saw in the preceding figure.

Use the cd command to navigate into the /var folder:

 $ ​​cd​​ ​​var

The prompt changes, and you can also use pwd to verify that you’re now in the /var directory.

 $ ​​pwd
 /var

Now, use the ls command again to see the contents of the var folder:

 $ ​​ls
 backups crash local log metrics run spool website
 cache lib lock mail opt snap tmp

Use the cd command to navigate into the log directory within this directory:

 $ ​​cd​​ ​​log
 $ ​​pwd
 /var/log

Notice that you moved to the var folder first, and then went into the log folder. You can shorten that a bit by telling cd to change to the child folder directly by specifying the path var/log. Return to the filesystem root and try it out:

 $ ​​cd​​ ​​/
 $ ​​cd​​ ​​var/log
 $ ​​pwd
 /var/log

From here you can use cd / to jump back to the root of the filesystem, or use cd to get to your home directory. But what if you wanted to get back to the var directory?

Relative and Absolute Paths

So far, you’ve been navigating using “relative paths.” That means you’ve been specifying the destination relative to your current working directory, or your current location on the filesystem. When you’re at the root of the filesystem, and you type cd var, the var directory is something you can see with the ls command. You and the var directory are in the same location on the filesystem, and the command you specified said “Switch to the var directory that’s in the same folder that I’m currently working in.”

You can navigate the filesystem much more quickly by using the entire path to a directory from the root of the filesystem. Let’s navigate to the /usr/bin directory. In a GUI, you’d have to go to the /usr folder first, and then open the bin directory. But the cd command can take entire file paths, so you can jump to the /usr/bin directory in a single command.

 $ ​​cd​​ ​​/usr/bin

This command says:

  1. Go to the root of the filesystem.
  2. Then go into the usr folder.
  3. Finally, go into the bin folder.

Try moving around to a few locations this way. Navigate to the /usr/lib directory, and then back to the /var directory:

 $ ​​cd​​ ​​/usr/lib
 $ ​​pwd
 /usr/lib
 $ ​​cd​​ ​​/var
 $ ​​pwd
 /var

In this example, you didn’t have to navigate back up to the parent directory, or jump back to the filesystem’s root. You included the root of the filesystem in the path to the file by specifying the leading forward slash.

So instead of specifying a directory relative to your current working directory, you specify the absolute location of the directory.

Dots and Double Dots

Two shortcuts help us work with files and directory paths when working on the command line; the dot (.) and the double dot (..).

Switch to the /var directory and use the ls -alh command to get a list of the files in the current directory, including the hidden ones:

 $ ​​cd​​ ​​/var
 $ ​​ls​​ ​​-alh

Take a look at the top two entries in the listing:

 total 56K
»drwxr-xr-x 14 root root 4.0K Mar 2 18:20 .
»drwxr-xr-x 24 root root 4.0K Mar 2 11:39 ..
 ...

The first entry is a dot (.). It represents the current directory. The second entry, the double dot (..), represents the directory that contains the current directory. This is referred to as the parent directory.

To quickly change to the parent directory, /, execute the command cd ..:

 $ ​​cd​​ ​​..
 $ ​​pwd
 /

The command cd .. changes your working directory to the parent directory, regardless of where you’re currently located on the filesystem. But if you need to quickly visit the directory above the parent, you can execute this command:

 $ ​​cd​​ ​​../..

Each .. entry in this command means “up one folder.”

Try it out. Return to your home folder, then navigate to the /home directory’s parent folder (which should be the filesystem root) using the double dot shortcut:

 $ ​​cd
 $ ​​pwd
 /home/brian
 $ ​​cd​​ ​​../..
 $ ​​pwd
 /

Using that command, you’ve jumped to the root directory of the filesystem.

Use Pen and Paper

images/aside-icons/tip.png

It can be a little difficult to visualize how the .. notation works. One trick that helps even experienced command-line users is to sketch out a directory structure on paper or a whiteboard and use it as a reference. Place your pen on the starting point, and trace up the hierarchy one level for each double dot. Do this a few times and you’ll probably be able to visualize this structure in your head.

You can take this a step further if you like. Change the current working directory to the /usr/bin folder, and then navigate to the /var folder using the shortcuts in a single command:

 $ ​​cd​​ ​​/usr/bin
 $ ​​cd​​ ​​../../var
 $ ​​pwd
 /var

In this example, you’re telling the cd command to go up two folders and then go into the var folder. But that’s a lot of typing. This kind of relative path navigation works well in applications and websites where you might move the files around and you want things to be relative to where you’ve stored them. But when navigating around on a filesystem on the terminal, it’s sometimes quicker to just use the cd command with an absolute path like cd /var—much less typing.

Try this little exercise to make sure you’re comfortable navigating around using the double-dot syntax:

  1. Use cd to go to your home folder.
  2. Use cd ../../var to go to the /var folder.
  3. Use cd ../usr to get to the /usr folder.
  4. Use cd local to get to the /usr/local folder.
  5. Use cd ../bin to get to the /usr/bin folder.
  6. Return to your home folder with cd.

Using the .. notation can sometimes help you move around a little more quickly than typing the entire path each time. Other times it might be quicker to use the absolute path instead. Knowing both ways will help you move as quickly as possible no matter what situation you find yourself in.

Using the cd command, you can get from one part of the filesystem to another in a single command. But sometimes you’ll find yourself switching back and forth between just a few locations. Let’s look at some more efficient ways to do that.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.137.117