How do you open a command line?
You can launch your terminal by pressing Ctrl + Alt + T or search from dash by pressing Meta / Super / Windows button and type terminal.
If you’re using Gnome, Terminal is probably under Accessories in your Application menu, or you can also try Alt F2 to bring up a launcher widget into which you can type «t-e-r-m-i. » to see all the terminal options.
Worth adding that Terminal is probably under Accessories in your Application menu (it is for me anyhow, but I gave Unity the boot. Maybe Unity doesn’t have an Application menu). You can also try alt f2 for a launcher widget where you can start to type «t-e-r-m-i. » and get a list of terminal applications available to you.
Since I’m not using gnome now, I cannot update my answer. If you have gnome, feel free to edit my answer by adding screenshot for both gnome and gnome classic.
Press Ctrl Alt T on the keyboard. If you prefer, there should be something called Terminal in your programs menu. You can search for it by pressing «Windows» key and typing «terminal». Remember, commands in Linux are case sensitive (so upper- or lower-case letters matter).
Command line can be accessed in couple of ways.
One is by using terminal emulator . The default terminal emulator for Ubuntu ( with default Unity interface ) is gnome-terminal. It can be accessed by either pressing Ctrl Alt T or by pressing the Ubuntu icon on the launcher and typing in the word «Terminal» or «gnome-terminal»
As you can see in the image, I’ve multiple terminal apps available, and indeed one can install more than one terminal emulator.
Another way to access command line is through the virtual console, called TTY. There are 6 virtual consoles available. Number of each console corresponds to the number of Fx key, where x is the number. For instance, to access TTY1, press Ctrl Alt F1 . To return to graphical environment , press Alt F7 , or repeatedly press Alt Arrow Left/Right until you exit.
There are other, rather hackish ways to access command line , but for the 99% of the day-to-day tasks terminal emulator and tty are sufficient.
How to Use the Linux Command Line: Basics of CLI
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.
Basics Commands
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
Moving around
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:
Documents/ Downloads/
[swapnil@swaparch ~]$ cd /home/swapnil/Downloads/
[swapnil@swaparch 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.
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/
total 4
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/
total 4.0K
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.