Hello! Hope you’re doing well. In this post we’ll talk about Command Line Interface(CLI) in Linux. In the last post we discussed about what Linux is. In this one we will get a taste of working with Linux command line. Lets go!
Topics covered in this post:
- What is a Shell?
- Terminal Emulators
- Linux Filesystem
- Navigating Linux filesystem in CLI
- Linux command behavior
1. What is a Shell?
When we refer to the command line what we really mean is “shell”. The shell is a program that takes keyboard commands and passes them to the operating system to carry out. Almost all Linux distributions supply a shell program from the GNU Project called bash.
The name “bash” is an acronym for “Bourne Again SHell”. It is an enhanced replacement for Shell(sh), the original Unix shell program written by Steve Bourne.
The popular shells used in linux are:
- C Shell (csh)
- Kron Shell (ksh)
- Z Shell(zsh)
Linux shell offers a way to interact with the kernel through commands. Ex:
ls lists all the files and folders in a directory. Each command represents a task to be performed.
Every shell bash or zsh will offer the similar functionality with same commands for the most part with some additional functionality of their own. One such example is the difference between bash and shell. The original shell didn’t offer command history(list of previously executed commands) and bash has it.
2. Terminal Emulators
A terminal is a program which passes the user input commands to the shell and displays the command output from shell to the user. A number of terminal emulators are available for Linux, but they all basically do the same thing; give us access to the shell.
Terminal offers a way to customize the appearance of the text(commands, progress bars, icons and output) displayed and much more. The possibilities for customization are endless. One such example is here.
3. Linux Filesystem
Linux is a hierarchical directory structure. This means that every directory can have files and other directories in them. When represented pictorially, it looks like a tree(data structure). The “Root” directory is the first directory in the filesystem and it has the various folders that are assigned for a purpose mentioned below:
Linux Filesystem. Source:Linux File Hierarchy Structure - GeeksforGeeks
When we first login to the Linux machine as a typical user we will be logged into the
/home/<username> directory. Only a root user will be logged into root (
4. Navigating Linux filesystem in CLI
One of the fundamental actions we perform in any operating system is navigating the filesystem. We are familiar with the graphical file manager in Windows and MacOS and even in the desktop linux distros. But, how about a server with only CLI? Lets dive in.
As soon as you login to the linux machine(ex: a server) as a user, you will be logged in to
/home/<username>) directory. In the below picture, I’ve logged into ubuntu using WSL on Windows. We will be using this going further.
At a given time, we are inside a single directory per terminal session. We can see the files contained in the directory and the pathway to the directory above us (called the parent directory) and any subdirectories below us. The directory we are standing in is called the current working directory.
Lets try two basic commands after logging in to linux terminal
whoami- shows the username
pwd- gives current working directory that we are in
ls- gives the list of files and directories in the current directory
This gives us a pretty good understanding about who we are(
whoami), where we are(
pwd) and what do we have in our current directory(
ls). To navigate the filesystem we use
cd is short for “change directory”, which allows us to change the current working directory.
cd command expects a folder or a path in the filesystem.
From the above example:
cd /usr/bin- change the current working directory to
~is notation for user’s home directory, which is
.represents current directory and
/testrepresents test directory under the current one.
Notice that the path is provided in two different ways. We have two ways to define a path in linux.
An absolute pathname begins with the root directory(
/) and follows the tree branch by branch until the path to the desired directory or file is completed.
A relative pathname starts from the working directory. To do this, it uses a couple of special notations to represent relative positions in the file system tree. These special notations are
.- current directory
..- parent directory to the current directory
- While navigating the subdirectories() within the current working directory, the
.can be ommited from the
cdcommand. For example: We are in pdir. pdir contains dir1 and dir1 has dir2. To navigate to dir2 from pdir, we can use
- Just enter
cdcommand and hit return to navigate to user’s home directory from any directory.
cd ~username- changes the working directory to the home directory of username
5. Linux command behavior
ls command gives the list of files and directories in the current working directory. But, what if we want more info while displaying the info? This is where command options and arguments comes handy. The options for a command will modify its behavior thus giving different results.
ls -a- displays all the files even the hidden files in the current working directory. Hidden files and directories start with
.. For example:
.profileis a hidden file and
.landscapeis a hidden directory in the below example.
ls -altr-displays all the files in the current working directory(
a), in long list(
l), sorted by modification date(
t) and in reverse order(
t). We can use multiple options for a command for a desired behavior.
ls /usr . -altrcommand takes two paths as arguments (
.) and displays the results for both the paths as shown below
An important question here is... do we have to remember all of the commands and their options available in linux? NO! You can’t possibly do that but we have a command for that as well.
man short for “manual” gives the full documentation for almost any command in linux. We can get documentation for any command using
man <command>. Lets try for
In the upcoming posts we will dive deep into working with files and directories in linux. Stay tuned!
- Linux Command Line Books by William Shotts
- What are the Different Types of Shells in Linux? - JournalDev
- https://www.cs.dartmouth.edu/~campbell/cs50/shell.html#:~:text=The shell is the Linux,shell executes the ls command.
- 22 Useful Terminal Emulators for Linux Desktop (tecmint.com)
- Difference Between Bash and Shell (With Table) – Ask Any Difference
- Linux File Hierarchy Structure - GeeksforGeeks
- How to install WSL2 (Windows Subsystem for Linux 2) on Windows 10 • Pureinfotech
Did you find this article valuable?
Support mani nekkalapudi by becoming a sponsor. Any amount is appreciated!