If you’ve historically used just Windows or MacOS based systems, you’ve likely become accustomed to accessing files/folders through a GUI (Graphical User Interface). You click on icon, which opens a window with moe icons, and you click from one icon to the next to get to the one that you want to interact with. Linux of course has this capability as well, but many environments, especially server environments, will not have a GUI desktop for you to ‘click’ around in.
But how do you navigate the file system from a command line? You’ve followed the resources and guides to ssh
into your new server, and you’re just looking at a prompt that looks something like:
user@myserver:~$
Where is this? This is just the server right? Right…?
Well, sort of. You are ‘in’ the machine at this point. But where in the machine are you? Believe it or not, your terminal prompt will usually tell you. Note: these can be configured stylistically to your liking, so they won’t always look the same.
In the example above, that prompt shows you a lot of information.
user@myserver:~$ indicates the currently logged in user.
user@myserver:~$ indicates The hostname of the server or system you are currently logged in to.
user@myserver:~$ indicates the current working directory. That’s right. The little ~
symbol, known as the tilde, indicates the home directory of the currently logged in user.
If I were to run the pwd
command from any location in the file system, it would shown the current directory. pwd
actually stands for ‘Print Working Directory’. Using the example above:
user@myserver:~$ pwd
/home/user
That first forward slash in the file path is actually very important. “/” actually indicates the root directory in the file system. The top layer. If you were to think of a file system as a literal file cabinet, this here would be the drawer itself. Inside of that drawer are folders. A ‘folder’, better known as a subdirectory would be a folder inside the drawer. And within each ‘folder’ additional folders or subdirectories can exist, to an incredibly high file depth (yes are are some limitations, but in practical terms you can go quite deep).
To read the path /home/user
- you would want to think of the file cabinet having a folder inside, with the label ‘home’, and within that folder, another folder inside of it labeled ‘user’. If this system has mutliple users, each of them would have their own folder with a label matching their name, inside of the /home
directory. Each of those users would have their own folders inside, with files at each level.
There are a number of commands used for working with files directly from the command line. Here are some of the basics:
cd
- this will change directory allowing you to change the working directory.
Remember when I said that the ‘/’ was important? You can use cd
to change to a directory by listing the full path or the relative path.
ls
will list the contents of a directory. Similar to the cd
command, you can declare a path, or list the contents of the working directory.
Let’s show some examples of these commands below:
user@myserver:~$ ls /var/www
drwxr-xr-x 2 root root 4.0K Jun 9 2022 html
-rw-r--r-- 1 root root 477 Mar 12 22:41 index.html
The current working directory is that of the user named user
. user
listed the contents of the /var/www
directory.
user@myserver:~$ cd /var/www
user@myserver:/var/www$
From their home directory, user
changed their location the the /var/www
directory.
user@myserver:/var/www$ ls
drwxr-xr-x 2 root root 4.0K Jun 9 2022 html
-rw-r--r-- 1 root root 477 Mar 12 22:41 index.html
Once again, user
listed the contents of the /var/www
directory. Since they were already in this location, they needed only to use the ls
command without specifying a location.
user@myserver:/var/www$ cd html
user@myserver:/var/www/html$
user
changed directory again to the /var/www/html
directory. This time, since they were already in /var/www
, they only needed to declare the relative path of html
.
This is a bit of a simplified view of navigating the file system from the terminal, but is a pain point for many beginners. Practice is key to understanding this navigation, and once you get used to the layout and how to ‘move around’, you’ll be able to simplify your navigation with more advanced commands and shortcuts.