Linux Command Line Part 4

Published on January 26, 2023 at 7:24 am by LEW

Introduction

In this post we will be looking at the cd (change directory) and ls (list directory content). To do so we will need to discuss a couple of file system concepts first (the file tree and links). These will most likely be the two most used terminal commands for many users.

Linux File Tree Caveat: From a high level, the Linux file tree is fairly standardized across distributions. However, things are not so orderly when we zoom in for a closer look. Basically there are some variations in how each distro deals with lower level directories and configuration files. Since we are not attempting any sort of configuration at this point, we will be on pretty safe ground, as far as standardization goes.

Please remember that I am using Arch for this series of posts. I will highlight any differences I’m am aware of when we come tom them.

The Root Directory

Everything in a Linux system resides in the root directory, which is designated by a forward slash “/”. Therefore any top level directories are prefixed with a “/”. For example, everyone’s personal home directory exists within a top level directory called home. Prefixing the “/”, we write this as /home. Since your user directory is a sub directory of home, it is written as /home/user. This is an absolute path. That is to say the directory reference goes all the way up to the root of the file system.

There are a number of top level directories, but at this point we only need to be concerned with a two of them, /home and /etc.

/home: All user home directories reside here. Your user folder contains both user data and local configuration information. At this point just be aware that the local configuration data is there.

Note: when we talk about a user home directory, we are talking about yours, which will have your user name instead of the name user.

/etc: This is where many of your system wide configuration files reside. Many Linux configuration files are plain text and can be modified by a user with proper access permissions. If you are looking for a configuration file it is probably located in this directory someplace.

In the next post we will go into more detail about configuration files.

Relative Paths

We have touched on absolute paths above. There is another type of path that we call relative. This is the path form your current location in the file tree to another location in the file tree.

There are two special directory elements used in relative paths. The single dot or “.”, and the double dot or “..’. The single dot notation references the current directory. The double dot notation references files in the parent directory. Generally you will almost never use the single dot notation, except for running scripts (for a later post).

Another notation is a name without the forward slash. If a file or folder is within your current working directory, you can access them with just the name and no forward slash.

This can be a bit confusing at first, so a couple of examples are in order. Lets assume you have in your home directory folders for Projects, Documents, and Graphics. Within your Project directory you have folders for CodeProject1 and ImageProject1. This will give us the following directory structure.

/
|_ home
   |_ user
   |  |_ Documents
   |  |_ Graphics
   |  |_ Projects
   |    |_ CodeProject1
   |    |_ ImageProject1
   |_ OtherUser

If we have a file called TestImage2.png located in the graphics folder, then from our user directory we could reference it in several different ways.

Absolute Path from the root or top of the directory tree.

/home/user/Graphics/TestImage2.png

We can use the absolute path no matter where we are in the directory tree, because it is always from the top of the directory tree. So they are consistent, but can tend to be somewhat long depending on how deep we are in the directory tree.

From our user directory there are two possible relative paths.

Graphics/TestImage1.png
./Graphics/TestImage1.png

The relative path works only from our current location. We have used both the no forward slash notation and the single dot notation.

Relative path from the Project sub folder.

../Graphics/TestImage1.png

In this example we use the double dot notation to move up to the parent directory (/home/user). Then we move back down into the Graphics folder.

Links

Links are small files that point to files at some other location. They are a type of shortcut. For example lets say you have a large pdf manual that you want to access from multiple sub directories. Instead of navigating the directory tree each time, or adding multiple copies in the sub directories, we create a link in each directory pointing back to the larger document. Selecting the link is virtually the same as selecting the original document in most cases.

The cd Command

Now that we have talked about the directory structure, lets learn how to move around in it. For this we will use then cd command. Since we have discussed man pages previously, you might want to review the man page for cd.

$ man cd

You will see there are a few options for the cd command, but in this post we will not be using them. Also for this example assume we are starting in our home directory (/home/user). Also refer to the above example for example directory structure reference.

Note that most shells you run across will have something called tab completion. What this means is if you type a few letters and press tab, the shell checks all possibilities, and if it finds only one, will complete what you are typing for you. This can save a lot of time and prevent typing mistakes.

We want to move from our user directory to our CodeProject1 directory. Here are three ways using the cd command, first absolute path, then relative no forward slash, and finally relative with single dot. I suggest using whichever one is easiest for you.

$ cd /home/user/Projects/CodeProject1
$ cd Projects/CodeProject1
$ cd ./Projects/CodeProject1

Now lets move back to our user directory using the double dot. We are two directories deep, so we have to use it twice. Once to get from CodeProject1 to Projects, and once to get from Projects to user. Note the forward slash between double dot entries.

$ cd ../..

The ls command

It is somewhat pointless to move around the file tree if we cannot look at what is in a directory. For that we will use the ls command. As before, you might want to check the man page for ls, as we will be using a couple of options with the command (specifically -l, -h, and -a).

From our last command above we should be in our user directory. Lets use the ls command to see what is here.

$ ls
Documents    Graphics    Projects

Note: Your user directory may have other files or folders in it. Like Music, Videos, Pictures, and Downloads. This will depend on the distribution and desktop you installed.

Lets add the -a option to the command and see what happens.

$ ls -a

The -a option shows everything in a directory. Additional hidden files will be dependent on your system. But you are probably going to have some hidden files like .bashrc and .profile. You may also have some hidden directories, like .config. Any file/directory that starts with a dot will be hidden. Do not mess with these files at this point. In later posts we will be talking about them. If you look back to previous posts in this series, you should see a reference to the PS1 prompt and the .bashrc file.

Lets add the -l option. This will list all non hidden files within the directory with some additional information.

$ ls -l
drwxr-xr-x  2  user  user  4096  JAN 22 13:34  Docuemnts
drwxr-xr-x  2  user  user  4096  JAN 22 13:21  Graphcs
drwxr-xr-x  2  user  user  4096  JAN 22 12:35  Projects

The first letter shows these are directories, the next several are permissions. This is followed by owner and group, then size, date and time, and finally file name. Do not worry about all this data at this point. I will be covering most of it in future posts.

The next thing to try is combining options, and we will add the -h or human readable option also. You can add all options separately (-a -l -h) or you can combine them (-alh).

$ ls -alh

Links will show up in a directory listings “name -> Path”. We will be discussing them more later.

For the final example of this post, we will add a path to the ls command to show us the contents of that directory. In this case we want to look at the /etc directory. Fair warning, this directory will have lots of folders and files.

$ ls -alh /etc

Conclusion

A lot of information has been doled out about directory structure, paths, and links. You should take some time to explore your system using the cd and ls commands. Both should be non destructive, so will not cause any system issues.

In the next post we will look at permissions, links, and pipes. We will also add the commands cat, less, and more.

The Linux Command Line Part 1; An Introduction

The Linux Command Line Part 2; The Prompts, some basics

The Linux Command Line Part 3; echo, whoami, pwd

The Linux Command Line Part 4; cd and ls

The Linux Command Line Part 5: ln, cat, more, less

Add New Comment

Your email address will not be published. Required fields are marked *