Command Line Interface is very important for the Linux world especially when it comes to Linux server administration. Through this interface administering the server can become a bliss if you are familiar with it and know how to use its power commands. The term "command line" might seem separate, but it actually relies on a program called the shell. This shell acts like a translator, taking the commands you type and giving them to the operating system for execution. In the world of Linux, most distributions use a shell program named bash, created by the GNU Project. Bash stands for "Bourne-Again SHell," a nod to its role as an improved successor to the original Unix shell (sh) developed by Steve Bourne.
Going Through the Terminal Door: Terminal Emulators
Even with a fancy graphical interface (GUI), getting down to the nitty-gritty of Linux often involves the command line. But how do we access that from a world of windows and icons? That's where terminal emulators come in.
Think of them as virtual doors – programs that provide a window into the world of the shell. Most desktops come with their own default emulator, like Konsole for KDE and Gnome Terminal for GNOME (though you might just see it called "Terminal"). There are also many others available, all offering that core functionality of connecting you to the shell.
The choice of emulator eventually boils down to personal preference. Some offer a more basic experience, while others are packed with features. As you explore the command line, you'll likely discover which emulator best suits your workflow.
Meet the Terminal
Launch the terminal emulator. Once it comes up, we should see something like this:
[hamed@server ~]$
This is called a shell prompt, and it will appear whenever the shell is ready to accept input. While it might vary in appearance somewhat depending on the distribution, it will typically include your username@machinename, followed by the current working directory (more about that in a little bit) and a dollar sign.
If the last character of the prompt is a hash mark (#) rather than a dollar sign, the terminal session has superuser privileges. This means either we are logged in as the root user or we selected a terminal emulator that provides superuser (administrative) privileges.
Let’s try a few simple commands. Let’s begin with the date command, which displays the current time and date.
hamed@server:~$ date
Thu Jul 11 10:53:53 +0330 2024
A related command is cal, which, by default, displays a calendar of the current month.
hamed@server:~$ cal
July 2024
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
In Ubuntu you need to install this command first using:
hamed@server:~$ sudo apt install ncal
To see the current amount of free space on our disk drives, enter df
hamed@server:~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 400532 2956 397576 1% /run
/dev/vda2 49453260 15954664 30965840 35% /
tmpfs 2002652 28 2002624 1% /dev/shm
tmpfs 5120 0 5120 0% /run/lock
tmpfs 400528 4 400524 1% /run/user/1000
Likewise, to display the amount of free memory, enter the free command.
hamed@server:~$ free
total used free shared buff/cache available
Mem: 4005304 1357660 1353336 101116 1294308 2296532
Swap: 2097148 0 2097148
If we press the up arrow, we will see that the previous command entered reappears after the prompt. This is called command history. Most Linux distributions remember the last 1,000 commands by default. Press the down arrow and the previous command disappears.
Linux is a multi-user operating system and you can change between users. Sometimes you need to become anther user, for example a system user such as postgres, to perform certain things. You do this by su command.
hamed@server:~$ su postgres
Password:
You should enter the password for the user, and if correct you become that user, and commands that are executed is actually run by that user. The command prompt also changes and shows the current user, but if you are in doubt you always can check by:
hamed@server:~$ whoami
hamed
When you want to know in which folder you are working you can use
pwd:
hamed@server:~$ pwd
/home/hamed
You can see the contents of current directory using ls command:
hamed@server:~$ ls
Desktop Music Pictures Videos Public Documents Downloads
These were some basic Linux commands. We can end a terminal
session by closing the terminal emulator window, by entering the exit
command at the shell prompt, or by pressing ctrl-D.