Navigating your way around the Linux file system

With a handful of commands and a trick or two, you can move around the Linux file system with ease and never get lost.

Navigating a field of uncertainty and doubt questions
Thinkstock

One of the first things Linux users need to learn is how to move around the Linux file system and, eventually, how to make it even easier to move around the file system. This post describes both the basic commands you need and some smart moves to make navigating easier.

Absolute and relative paths

Before we get moving, it’s important to understand the difference between absolute paths (like /home/jdoe) and relative paths (like images/photos and ..). Absolute paths always begin with a / that, of course, represents the base of the file system. If the specified path doesn’t start with a /, it’s relative. Here are some examples of both relative and absolute paths:

/tmp             <=== absolute (a directory inside /)
/home/jdoe/poems <=== absolute (one of jdoe’s subdirectories)
reports          <=== relative (inside the current directory)
..               <=== relative (the directory that contains the current directory)
.                <=== the current location

Fortunately, getting back home is about the easiest thing you can ever do. You just type cd (change directory) without any arguments and you’ll end up there.

Using pwd

No matter where you’re headed, it always helps to know where you’re starting from. The pwd command tells you where you are right now. If you just logged in, that will be your home directory. If you’ve been exploring the file system for a while, it could be anywhere that your user account allows you to go. After all, you can’t enter a directory if you don’t have read access.

When you use the pwd command to determine your current location, it will tell you where you are situated in the file system using an absolute path.

$ pwd
/home/justme/images

The only complication is that, if you move into a directory using a symbolic link and ask where you are, cd will show you the location using the link.

$ ln -s /tmp/test testdir
$ cd testdir
$ pwd
/home/shs/testdir

You can, however, try a command like the one below to see where you really are in the file system:

$ pwd
/home/shs/testdir
$ echo  $( dirname $(realpath "symlink") )
/tmp/test					<=== aha!

Using the tree command

To get a bigger picture of where you are at any point in time, you can use the tree command. It will display a multi-level view of the files and directories in your current location, providing you with insights into the directory and its contents.

$ tree
.
├── bin
├── file1
├── file2
├── file3
├── myfile
├── mypipe0
└── testdir -> /tmp/test

Using cd

The cd (change directory) command will move you into any directory you ask to move to as long as 1) it exists and 2) you have the execute permission. You can only list files if you have read access.

The requested location might be a specific full path, a directory relative to your current location or a location that is pointed to by a symbolic link. Here are some examples:

$ cd /tmp
$ pwd
/tmp
$ cd
$ pwd
/home/justme
$ cd ..
$ pwd
/home

Using ~

Another way to move back into your home directory is to type cd ~ and, of course, press return. This works because the ~ (tilde) key represents your home directory. That said, this use of ~ isn’t the only thing it’s good for. You can also use ~ to reference your home directory when you’re somewhere else. Here’s an example:

$ pwd
/tmp
$ cp report ~

This command will copy the file named “report” into your home directory quicker than you could type "/home/justme".

Making it easier

If there are locations in the file system that you need to move into fairly frequently, don’t sit there typing command like "cd /development/docs/reports" to get there. Instead, create a symbolic link with a command like that shown below to move yourself there faster and more easily.

$ ln -s /development/docs/reports ~/reports

Afterwards, you can get to the directory by typing “cd reports” (or "cd ~/reports if you're not starting from your home directory) and copy files from it with a command like one of these:

$ cp reports/latest .     <== if you're in your home directory
$ cp reports/latest ~     <== if you're in your home directory or not

The first command copies the file to your current location. The second copies it to your home directory.

You can also use the symbolic link to move into the directory whenever you need to and regardless of where you are:

$ cd ~/reports
$ ls -ltr | tail -1
-rw-rw----. 1 justme justme 22 Aug 22 11:11 report

Wrap-up

It’s relatively easy (that’s not meant as a pun!) to move around the Linux file system, but good to know that you don’t need to rely on absolute pathnames to get you where you want to go or to copy files to or from other directories.

Related:

Copyright © 2023 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022