Tuesday 7 August 2012

Essential Commands in Ubuntu


Hi friends, hope you have enjoyed most of the features in Ubuntu. From now, let's talk only technical. Here in this post I will introduce most of the basic commands you should know and practice them strongly.
We all know, in Unix environment, and so derived Linux, Command Line Interface (CLI) is most strongest and fastest way of communicating to your computer to do your tasks. Feel proud if you are using CLI because, almost, you will interact with Kernel and its subsidiaries. I know, 'Kernel' may be sounding little high for few people who worked much on Windows. Because, it is sad to say, Windows never specifies anything about the word 'Kernel'.
Kernel is a Heart of your Operating System. You might have heard about the 'Core' in your Operating System, yes it is same the Kernel. It act as bridge between your application and computer Hardware and manages your system resources.
There is lot more to discuss about Linux Kernel. For time being, you consider a Kernel in Linux are also sort of file (a very special files) which specially interact with your system hardware. Referring to above picture, above the Kernel there exists "Shell" layer, that is what is your Command Line Interface section. And hence, Terminal is tool that helps you to execute commands over Shell layer. Before we start Shell commands, let's do some stuff with your Terminal

Let's Play with Terminal:
Navigate to Applications > Accessories > Terminal. You will see user_name@computer_name:~$ prompt on screen; where user_name is your computer user name which you have logged in; computer_name is your system name you have provided while installing Ubuntu. Then, symbol ~ indicates your default work place  for 'user_name'.
To know what directory you are in, type pwd and press enter. 
Remember, commands are case sensitive and commands receives white space between different arguments. pwd stands for Present Working Directory. 
In my case, I got this output,
kiran@kiran-desktop:~$pwd
/home/kiran
kiran@kiran-desktop:~$
where "/home/kiran" is my present working directory. 
The symbol ~ will means same, the /home/kiran; So my default work place is /home/kiran.
Let's see what's the use of Default Work Place. 
Now change your directory to /home using cd command. cd stands for Change Directory.
kiran@kiran-desktop:~$cd /home/
kiran@kiran-desktop:/home$
Notice here, change in working location is indicated
Now if you type pwd, it will display /home as your present working directory. 
To go back, at any time, from any working location to your Default Working Location, just issue cd ~ and press enter.
kiran@kiran-desktop:/home$cd ~
kiran@kiran-desktop:~$

Keyboard Shortcuts with Terminal:
This section has some good stuffs to practice, go on,
[*] To erase the screen, use Ctrl+L. However, you can also use clear command in Terminal.
[*] Terminal is capable to run many Tabs. You can run multiple commands, multiple operations on different Tabs. Use Ctrl+Shift+T to open another new Tab. similarly you can open many Tabs whenever you want  to operate paralelly. 
[*] Use Ctrl+PageUp or Ctrl+PageDwn to navigate between Tabs.
[*] To open new Terminal Window itself, use Ctrl+Shift+N.
[*] Terminal is capable to do copy paste operations. If you have written commands in a text file, copy the command and in Terminal press Ctrl+Shift+V to paste it.
[*] Use Up and Down keys to get previously typed commands, in cyclic order.
[*] There is an auto command complete feature in Terminal. To demonstrate it, just type, cd /ho and press Tab key. See the auto completion feature, the Terminal will auto complete your command to cd /home/. Similarly all commands in Linux have auto complete capabilities in Terminal. 
[*] When there are multiple of commands initiating with same name, Terminal will display suggestions on double press of Tab key. To demonstrate type cd /m and press Tab key two times, Terminal will display media/ and mnt/ and waits for your next input. This means, cd can either be media/ or mnt/ directories both starts with letter m. Now, just add letter e (at same prompt cd /me) and press Tab key once, now see Terminal will auto complete your command to cd /media/, because now Terminal has only one directory option media/ which starts with letters me. Thus work it out on few commands for auto complete feature.
Make sure you are fully familiar with above Shortcut keys, you can do really quick operations with Terminal.

How do I get help on commands?
In Linux, help and information is always given to them who asks for it. They are nicely maintained information, which are called, mans or manuals.
To see manual page for pwd command, type man pwd and press enter. Similarly, you will get manual pages for all the commands used in Linux. If you have any doubt on any command at any time, about command input type, or arguments, or options, just do man about that command you will get the description displayed on your terminal. To come out of manual page press q from your keyboard.

Some more essential commands:
So far, we have seen cd and pwd commands. Let's see two main category commands, System Information related and Basic File related.

> System Information:
[*] date will display current system date on terminal.
[*] whoami is a command which displays who the user logged in as. It displays your current log-in information.
[*] uname display the current running Kernel and its version on terminal. The argument -a with this command (uname -a) displays full version information about your current running Kernel, where -a stands for all. Yes I said current running Kernel!!; What does a "current running" literally mean is, you can have a multiple versions of Kernels in single Linux Operating System, and you can chose one for present boot-up option. Those steps I will discuss in later sections. (Don't worry it's no big deal, once you go step by step and learn all basics, you will treat Kernel also as a simple file)
[*] whereis <app> command will display the probable location of app in your Linux system. Where <app> stands for application present in your system. Let me give an example, whereis bash displays probable location of application bash (may display /bin/bash - means bash is located at /bin/ directory) in your system. whereis vlc displays probable location of VLC Media Player located in your system [only if it is installed].

> Basic File Commands:
[*] ls is command to list directories and files in present directory. If you do ls -a it lists all the files, including system files and hidden files in present directory.
[*] cd, you already know, change directory. In Linux, to change directory one step back use cd .. (notice a white space between cd and ..
If you have done ls -a you might have noticed a . (a Dot) and a .. (a Dot Dot) entry in file list; remember Dot entry confine to "Present Directory" and Dot Dot entry confine to "Previous Directory". So to go to previous directory, you type cd .. 
[*] In Linux you can pass multiple argument to command, like if you want to jump two steps back from present directory /home/user/new_dir/ now you do cd ../.. it will automatically take you two steps back and /home/ will be your new location. Do cd ~ to change your present directory to your default work location.
[*] mkdir <dir_name> will create a new directory <dir_name>. If you have given a complete file path, the directory will be created at that specified file path; otherwise, if you just give directory name, it will be created at present directory.
[*] cp <file_name>  <dest_location> copies file with <file_name> to <dest_location>. You can give multiple files with comma separated argument like cp file1,file2,file3 location. Where, file1file2 and file3 are present in current directory. You may also copy different files from different path to one location. 
[*] To copy a directory instead of a file, do cp -r dir location. Where, flag -r indicates recursive.
[*] rm <file_name> will remove or delete the file with <file_name>. rm -r <dir_name> will delete the directory with name <dir_name> along with its contents, permanently.
[*] mv <file_name> <dest_location> will move the file <file_name> to <dest_location>. 
[*] Remember, you can also use mv command to rename the file to new name, use mv <file_oldname> <file_newname>.

It is not the end. We have to learn many other commands. I will write about those commands in further posts. Till then keep practicing above commands and have a good hand in them. Any time any help on any command, just do man <command>.

No comments:

Post a Comment