One shell to rule them all, one shell to find them, one shell to bring them all and in the same distro bind them.
Command line is one of the many strengths of Linux based systems. Why is it a strength? There is no one answer; there are many answers. I agree that the graphical user interface (GUI) makes it easier for a user to interact with their system and that's what new users may need to get started with Linux; that's what I needed when I was starting off with Linux back in 2005. But as I matured as a user I found CLI (command line interface) was more efficient than fiddling with the buttons of a tool.
CLI also allows users to be independent of distros. Just look at the derivates of Ubuntu, even if they use the same code-base they have different tools to do the same job. Different desktop environments on the same distro need different ways to perform the same task. A user has to un-learn and then re-learn the process of doing the same thing while they hop between distros. Furthermore if we move between Fedora, openSUSE and Arch, it becomes even more complicated.
But once you understand that in Debian-based systems apt-get or dpkg are the commands that you need to manage software, life becomes easy. Then it desn't matter whether you are on Ubuntu or Lubuntu.
When I was dependent on a GUI, I used to get worried whether that particular distro has that feature or not - it was all about certain features being exposed or hidden through the GUI. One simple example is that Gnome's Nautilus doesn't allow batch rename of files where as KDE's Dolphin does. As a result the user of x distro or DE hesitates in trying out other projects fearing they won't find the same tools. A Gnome user doesn't have to sacrifice such a useful function, thanks to the command line.
But that's not all command line does. It also saves system resources which are consumed by GUIs. So if you are on a slower system, you are better off with the command line than GUI.
People tend to think command line is difficult; it's not. It's more or less like SMSing to your PC, telling it what to do.
So without further ado let's learn some basics of command line.
Get the shell
Shell is basically a program that turns the 'text' that you type into commands/orders for your computer to perform. As such there is a set structure of commands; different OSes may use a different structure to perform the same task.
There are many Shells available for Linux, but the most popular is Bash (Bourne-Again shell) which was written by the GNU Project. Another more modern shell with more features is 'zsh' which you can install for your distribution (we will talk about shells in a later article).
If you are using a desktop environment then you need a terminal emulator to emulate the terminal within that interface. Different distros come with their own terminal emulators: KDE comes with Konsole and Gnome comes with Gnome Terminal.
When you open a terminal emulator, by default you are in the home directory of the logged in user. You will see the name of the logged in user followed by the hostname. $ means you are logged in as a regular user, whereas # means you are logged in as root.
Unless you are performing administrative tasks or working inside root directories never work as root as it will change the permissions of all directories and files you worked on, making root the user of those directories and their content.
You can list all directories and files inside the current directory by using the ls command.
[swapnil@swaparch ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
To change to any directory, use the cd command. You can also use the 'Tab' key which will auto completes the path. Use forward slash to enter directories. So if I want to change directory to 'Downloads' which is inside my home folder, we run cd and then give the path. In this case 'swapnil' is the username. You need to type your username:
[swapnil@swaparch ~]$ cd /home/swapnil/Downloads/
As you can see in the third line, 'Downloads' directory has moved inside the square brackets, which denotes that currently we are inside this directory. I can see all subdirectories and files inside Downloads directory by running the ls command.
You don't have to give the complete path if you want to move inside the sub-directory of the current directory. Let's say we want to move inside the 'Test' directory within the current 'Downloads' directory. Just type cd and the directory name, in this case it's 'Test', without any slash.
[swapnil@swaparch Downloads]$ cd Test
If you want to change to another directory just follow the same pattern: cd PATH_OF_DIRECTORY . If you want to move one step back in the directory then use cd . . /. To go back two directories use cd . . /. . /and so on.
But if you want to get out of the current directory and go back to home, simply type cd.
Seeing is believing
You don't have to change directory to see its content. You can use the ls command in following manner:
[swapnil@swaparch ~]$ ls /home/swapnil/Downloads/Test/
There is no place to hide
To see hidden directories and files use -a option with the ls command.
[swapnil@swaparch ~]$ ls -a /home/swapnil/Downloads/Test/
Size does matter
In order to see the size of directories and files you can use -l option with the ls command. It will also tell the permissions of the files and directories, their owners and the time/date of modification:
[swapnil@swaparch ~]$ ls -l /home/swapnil/Downloads/Test/
drwxr-xr-x 2 swapnil users 4096 Mar 26 11:55 Test_2
The command gave us the file size in a form hard to understand. If you want to get the file size in human readable format then use ls -lh command:
[swapnil@swaparch ~]$ ls -lh /home/swapnil/Downloads/Test/
drwxr-xr-x 2 swapnil users 4.0K Mar 26 11:55 Test_2
If you want to get a simple list of all the directories and files inside a location, without extra info such as file size, etc., use ls -R command. This command will give a very long output (depending on how many files are there) as directory trees.
Let's create some directories
If you want to create new directories the command is mkdir. By default the directory will be created in the current directory. So give the complete path of the location where you want the directory to be created:
So if I want to create a directory 'distros' inside the 'Downloads' directory, then this is the command I will run:
[swapnil@swaparch ~]$ mkdir /home/swapnil/Downloads/distros
If you want to create a sub-directory inside a new directory then use '-p' option with 'mkdir'. I am going to create a directory called 'distro' along with a sub-directory called 'opensuse' inside it. If I run the mkdir command with '/distro/opensuse' as the path, it will throw an error that the directory 'distro' doesn't exist. That's when the option 'p' comes at play and creates all the directories in the given path:
mkdir -p /home/swapnil/Downloads/distros/opensuse
This command will create new directory 'distros' and sub-directory 'opensuse' inside it.
And now let's delete them
If you want to delete any file or directory the command is 'rm' (for files) and 'rm -r' (for directories). You need to be very careful with this command because if you fail to give the correct path of the file or directory then it will remove everything from the current directory and you may lose precious data. The command is simple:
If I want to remove the opensuse directory, the command would be:
rm -r /home/swapnil/Downloads/distros/opensuse/
However, if you want to delete all the content of a directory without deleting the directory itself use the '*' wildcard with a slash. Let's say I want to delete all the content of opensuse directory:
If there are sub-directories inside, for example, opensuse directory then you will need that '-r' option to also delete the sub-directories:
rm -r /home/swapnil/Downloads/distros/opensuse/*
That's all for today. This article will make you pretty comfortable with the command line. In the next article we will take you to the next level of managing your system via CLI.
Till then, cd bye