Sunday, 9 August 2015

LINUX : SHELL & BASIC COMMANDS

Command line is one of the many strengths of Linux based systems.

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 
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.
[prathima@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 'prathima' is the username. You need to type your username:
Documents/ Downloads/
[prathima@swaparch ~]$ cd /home/prathima/Downloads/
[prathima@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 sub-directories 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:
ls /PATH_OF_DIRECTORY
Example:
[prathima@swaparch ~]$ ls /home/prathima/Downloads/Test/

There is no place to hide

To see hidden directories and files use -a option with the ls command.
[prathima@swaparch ~]$ ls -a /home/prathima/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:
[prathima@swaparch ~]$ ls -l /home/prathima/Downloads/Test/
total 4
drwxr-xr-x 2 prathima 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:
[prathima@swaparch ~]$ ls -lh /home/prathima/Downloads/Test/
total 4.0K
drwxr-xr-x 2 prathima 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:
mkdir /path-of-the-parent-directory/name-of-the-new-directory
So if I want to create a directory 'distros' inside the 'Downloads' directory, then this is the command I will run:
[prathima@swaparch ~]$ mkdir /home/prathima/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/prathima/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:
rm /path-of-the-directory-or-file
If I want to remove the opensuse directory, the command would be:
rm -r /home/prathima/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:
rm /home/prathima/Downloads/distros/opensuse/*
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/prathima/Downloads/distros/opensuse/*

No comments:

Post a Comment