Hello everyone, it’s gutsytechster !!
I have learnt something courageous, something gutsy, well you can say it because today we are going to learn about linux. Of course, you must have heard about this. Just like windows, linux is also an operating system. If you ask, what’s an operating system, then in simple words, an OS manages the communication between the hardware and software. Without an OS, your software wouldn’t function. Linux has both GUI and CLI. But we’re gonna learn about CLI(Command Line Interface).
Often, people who are new to linux, consider it as difficult and hard to operate, I thought it too, when I started using it. But now, I reckon it is as user friendly as any other operating system. Basically, linux was devloped on Unix by Linus Torvalds, who was a computer science student. He was facing some problems using Unix, so he made some improvements in it and devised a kernel and named it as linux. A kernel is the innermost part of an operating system. Well, do not get confuse if linux is a kernel or an OS. When I say linux as an OS, then I include all of its distributions which uses linux as a kernel. Now you may want to know what a distribution is? Then, let me explain. Linux has a number of version to suit nearly any type of user. These versions are called as distributions. Some of these are:
- Ubuntu Linux
Each distribution has its own look on the desktop, some opt for modern user interface, while some goes with traditional look. So it’s up to you which you want to choose. If you are a beginner, then I’ll propose you should use Ubuntu. If you want to know more about these distributions and looking for how to install Ubuntu then you can refer here . One more thing, linux is a Free and Open Source Software.
Since, we are going to learn the command line, be prepared. No, I’m not scaring you. When people hear the word “terminal”, they look scary and though it may seem daunting, believe me it is not. You just need to Open your terminal and write commands and see your work done in seconds. As it works directly with interpreter, its processing speed is fast and it doesn’t mean that you have to ditch GUI . It’s just like that everything has its own advantage, so works like file management and data extraction are more suited to terminal and tasks like video editing or audio manipulation are preffered to be done in GUI. Just remember one thing, choose the best for your job !
Getting start with terminal
Well, I assume that you all are using Ubuntu, so to open the terminal, you can press “Alt+Ctrl+T” or you can directly go to top of menu bar, click it and search for terminal, you will find it easily. When your terminal will be opened, you will see something like it:
here “raj” is my username.
“raj-System-Product-Name” is my computer name or host name.
“:”(colon), is used as a seperator.
“~”(tilde),refers that you are in your home directory.
“$”, shows that you are working as a regular user and command can be written after it. If you are working as a root user this will be replace by “#”
“@” is just a seperator, which seperates your username and your hostname.
Now let’s have a look on the commands:
1. pwd : It’s a short term for “print working directory”. This command will show in which directory you are working in.
2. cd path: It is used to change the directory and take you to the directory whose path is given. If you don’t give any path, it will take you to the home directory. Notice that there is a space between cd and path. It has following variations:
- cd ~ : It will take you to the home directory, same as when no path is given.
- cd .. : It will move one directory up or take you to the parent directory.
- cd / : It will take you to the root directory.
3. ls : It will list all the folders(directories) and files in present in your current directory. It has some command line options which extend its work as:
- ls -R : It will show all the files not only in directories but in subdirectories.
- ls -l : It will show the long listing of all the files and folders (not sub-folders).
- ls -a : It will show all the hidden files also. (Hidden files start with “.”(dot))
You can also use two command line options altogether. For e.g. ls -al will give long listing of all the files including hidden files.
4. touch filename : To create a file with a given filename.
5. cat filename : To view the contents of a file on the terminal screen.
6. rm filename : To remove a file from the current directory. If your file is in any other directory, then give the path accordingly.
7. mv source destination : To move a file or directory, use mv command. This command is also used to rename a file or directory as: mv existing_name new_file_name. To move more than one file from source use -t command line option as: mv file1 file2 file3 -t destination.
8. mkdir dir_name : To make directory, we use mkdir command.
9. rmdir dir_name : To remove a directory, we use rmdir command, but for that the directory must be empty. Else you have to use a command line option “-rf” which stands for recursive forcefully. So to delete non-empty folders, we need to use “rm -rf dir_name” . Don’t forget to give space between rm, -rf and dir_name. But be cautious while using this command as using “rm -rf / ” will delete your root and your operating system will be destructed.
10. man command_name : To get help on any command that we don’t know much.
11. history : This command provide, all the entered commands for the current terminal session.
12. clear : This command will clear your terminal screen as it was when you opened the terminal.
13. pr file_name : This command helps in formatting a file when printing on a terminal. It has a no. of command line options. Search through the man page of the command and you will get to know.
14. lp filename: This command will print the hard copy of a file, provide specific hardware device must be connected(printer). You can use lpr filename also.
15. cp source destination : This command is used to copy the file from source to destination. But it will not copy the directory, for directory you have to use the “-r” command line option as “cp -r source destination“. To copy more than one file use “-t” as command line option as: “cp file1 file2 file3 -t destination”.
16. nano filename: nano is a default text editor present in linux, so you can edit your text whenever you want.
17. whoami :You can read it as “who am I”. This command will give you, your user name.
18. sudo commands : The commands which have security privileges of the superuser or root can be executed by sudo command. It will ask you the password everytime you use it.
(i) sudo apt-get update: To update the installed packages.
(ii) sudo apt-get install packagename: To install a package
(iii) sudo adduser username: To add a user.
19. less : When we use “cat” for a large file, the prompt jumps to last page and we can’t see the content in the middle. So we use “less” command for this as cat filename | less .
20. grep string_to_be_searched: It searches for the string given. It has some amazing command line options, search through man page for this command.
21. sort filename : To sort the file alphabetically. To reverse order use -r command line option.
Well, since we are working in terminal, we are trying to use the keyboard as much as we can, and for this shortcuts for various operations are available. To look for these shortcuts. Go to Edit -> Keyboard Shortcuts option in your terminal. You will find some amazing shortcuts. As we are talking about shortcuts, there are a few interesting things like, for the commands you’ve entered before just a little ago and you need to write that command again, so instead of writing it again you can use up arrow and down arrow.
Similarly we have a option of tab completion, while writing path for a command, when we know that there are a few files which are unique, you can use tab instead of writing the whole path. Write a few words of starting and press tab and you will see that the path would be written automatically.
Permissions in Linux
Every file and directory is assigned with three types of owners i.e. user(u), group(g) and other(o). And each owner has three permissions which are read(r), write(w) and execute(x). They will be given in this order only i.e. “rwx“. When you apply “ls -l” in a directory, what you see is something like this:
. . drwxr-xr-x 4 raj raj 4096 Nov 26 10:51 Downloads -rw-r--r-- 1 raj raj 8942 Oct 5 00:13 examples.desktop . .
Now, we will break it into parts to understand, how the permission is given to a owner.
- The first letter of permission in (i) is “d” which implies that it is a directory and in (ii) is “-” which implies that it is a file.
- Now look for next three letters which are “rwx” in which each letter stands for its respective permission. These permissions are for user i.e. home.
- Then next three letters are “r-x”, in which “-” means no permission for writing. These permissions are for group. Whoever in the group will have these permissions.
- Now, the next three letters which are “r-x”. These permissions are for others.
To change any permission we will use chmod command as:
chmod permissions filename
To give permissions, first you have to mention, to whom u r granting or revoking the permissions by specifying keywords as “u”,”g”,”o”,”a” for user, group, other and all respectively. Then you have to mention whether you are granting or revoking the permissions by using “+” and “-” for granting and revoking respectively. After that you have to specify, which permission u r giving by keywords “r”,”w”,”x”.
Let’s have an example :
. . drwxr-xr-x 2 raj raj 4096 Oct 5 00:18 Documents . raj@raj-System-Product-Name:~$ chmod u-r Documents . d-wxr-xr-x 2 raj raj 4096 Oct 5 00:18 Documents . .
As you can see, I revoked the reading permission from user.
You can also give permissions using octal representation of binary numbers.
Let’s have an example of that:
. . d-wxr-xr-x 2 raj raj 4096 Oct 5 00:18 Documents . raj@raj-System-Product-Name:~$ chmod 755 Documents . drwxr-xr-x 2 raj raj 4096 Oct 5 00:18 Documents . .
Now I have given “755” as chmod argument . Let’s see, what does it mean exactly
- “7” stands for binary “111”. Since first three characters refer to the permission for user where “rwx” are “111” i.e. all the permissions are given to user.
- “5” stands for binary “101”. Since second three characters refer to the permission for group where “rwx” are “101” i.e. writing permission hasn’t given to the group, else read and execute permissions are given.
- Here also, “5” stands for binary “101”. Since last three characters refer to the permission for other where “rwx” are “101” i.e. writing permission hasn’t given to the other, else read and execute permissions are given.
That’s all with permissions.
With this, we have covered some important topics of linux, there is a lot more to explore which is beyond this blog’s reach. So if you are curious to learn about linux more, you can refer here.
Meet you in the next blog with some interesting topic !!