Have you ever wondered how everything works when you use your computer and interact with different applications in the graphical user interface (or just GUI for short)? It can be a simple calendar, or a complex math application used for simulation generation. A GUI is a great way to interact with a computer. But before the GUI, there was the command-line interface (CLI) and to this day, some of the main administrative tasks are done in the CLI. In the following module, you will get the introduction to Linux CLI and learn how to navigate in Linux using a command-line interpreter.
A CLI allows users to interact with the computer the same way as you would with a graphical user interface (GUI). The main difference is that instead of a mouse, you only need to use a keyboard and commands. A command is a computer program that allows you to do a specific task.
The Prompt
The first thing you will notice in most command lines is the prompt. The prompt usually consists of the username of the logged-in account and the hostname of the system.
Linux command prompt explained:
1. <username>@<hostname>:<working directory><user privilege>
The #
and $
characters give you a visual overview of the current user privilege:
~: Indicates that the current working directory is the user's home directory.
#: Logged in as the root user with elevated privileges.
$: Logged in as an ordinary user.
Directory Structure
The Filesystem Hierarchy Standard (FHS) defines the structure of file systems on most Linux and other UNIX-like operating systems. In the following picture, you can see the standard Linux-based distribution directory structure.
A file path specifies a unique location in the file system. An absolute path is the full path to the directory or file from the filesystem root (/
). An absolute path to an index.html
file would be:
/var/www/index.html
A relative path is the path to a directory or file from the current working directory. If your present directory is /var then the relative path to the same index.html
file would be:
www/index.html
Navigation
Time to put your newly learned skills to use. Now that you know a bit about how the files and directories are structured in the Linux server, it is time to take a step further and try navigating around.
List Directory
Before actually moving around in the system, it would be wise to get to know your surroundings. This can be done easily by viewing the different files and directories in the system. To do so, you can use the ls
("list") command, which allows listing directories and files.
Example:
ls /the/directory/i/want/look/into
Current Directory
The current working directory means the directory you are currently located in. If you want to know your current working directory, you can use the pwd
("print working directory") command that prints the name of your current working directory.
Change Directory
When you want to navigate to a directory using a GUI, you would double-click on the directory’s icon. In the command-line interpreter you would use the cd
("change directory") command to change your current working directory. This can be done easily, by adding the directory you wish to navigate to, after the command itself:
Example:
cd /the/directory/i/want/to/navigate/to
Occasionally, you need to navigate to the current working directory’s parent directory. To avoid writing the whole absolute path that represents the parent directory, you can use two dots ..
to mark the parent directory. For example, if you are inside the /home/student/
directory and need to navigate to the/home/
directory, you could simply execute cd ..
. A single .
dot represents the current working directory.
That’s it for today!
Source: RangeForce