Search

Long listing and chmod

Aim: To explain the concept of user permissions and changing the permissions.

Every file in linux has 3 kinds of permissions Read,write and execute, these permissions are with respect to three kinds of users.
1. The owner of the file, i.e. the user who created the file.
2. The group to which the owner of the file belongs to.
3. Every one else or called as others.

To view the permissions that a file holds use the long listing format of the "ls" command. i.e. ls -l

For eg:

user@Desktop:~/folder1$ ls -l
total 12
drwxr-xr-x 2 satish satish 4096 2010-04-25 20:59 one
drwxr-xr-x 2 satish satish 4096 2010-04-25 20:59 three
drwxr-xr-x 2 satish satish 4096 2010-04-25 20:59 two
user@Desktop:~/folder1$

The first line in the long listing will is the total size of the folder.
From the second line onwards there are 17 columns. Which give the following information.

Column1:
The first field denotes what kind of file it is. A "d" indicates it is a directory . A "-" indicates it is a normal text file.
A "l" indicates it is a soft link. [ If you don't understand soft link then its ok, we will come to that later]

The remaining 9 fields of the columns one are the permissions of the file.
A "r" indicates "read" permission .
A "w" indicates write permission .
A "x" indicates execute permission.
The first set of three fileds indicate the permission of the owner of the file

The second set of three fileds indicate the permission of the group to which the owner belongs

The third set of three fields indicate the permission of the others, who are neither the owner of the file nor belong to the group of the owner.

The command to change permissions of a file is "chmod" which stands for change mode.

syntax: chmod "permissions" file

The permissions that you have we want to set can be passed in two ways.

octal numbers:
If you are not familiar with the octal numbers than skip this section. There is another way of setting the permission given in the next section.

We have three fields per user, a "1" in a field indicates that permission is eanbled a "0" indicates the permission is disabled.
Three bits in binary can maximum represent the number "7".

For eg the octal way of denoting the permissions , user having read,write execute, Group having read and write and others having only read permission would be

111110100 which when converted to octal would be 764.

so the command would be as follows.

user@Desktop:~/folder1$ chmod 764 "filename"
user@Desktop:~/folder1$


Changing File permissions using aplhabets:

The other way of setting the file permissions is using the "alphabets" "r" for read, "w" for write and "x" for execute.
A "+" signifies add permission and a "-" signifies remove permission.
To add read permission for a user use the "chmod" command as follows

user@Desktop:~/folder1/one$ chmod u+r file1
user@Desktop:~/folder1$


To add the execute permission for others we can use the "chmod" command as follows

user@Desktop:~/folder1/one$ chmod o+x file1
user@Desktop:~/folder1$


To remove write permission for the group, we can use the "chmod" command as follows

user@Desktop:~/folder1/one$ chmod g-w file1
user@Desktop:~/folder1$


To add a permission to users,group as well as others in one go, we can use the "chmod" command as follows

user@Desktop:~/folder1/one$ chmod a+x file1
user@Desktop:~/folder1$

We can also add or remove multiple permissions in one go as follows

user@Desktop:~/folder1/one$ chmod a+rw file1
user@Desktop:~/folder1$

The above command give read and write permission to every one.

Column 2:

The second column indicates the number of links to the file. We will get into the concept of links later.

Column3:Gives the owner of the file.

Column4: Gives the group to which the owner belongs to.

Column5: Gives the size of the file

Column6: Gives the last date on which it was modified.

Column7: Gives the time of modification.

Column8: Gives the file name itself.

Handling the various of packages like .tar, .tar.gz,.bin,.bundle etc

Aim: To make aware the reader how to handle various kinds of packages in linux

.tar:  Tape Archive.

A file with ".tar " extension is basically a collection of files and folders put into one package, which is some times called as a tar ball.

To get the contents out of such a file, in case of the new distros like ubuntu you can just right click on the file and select "extract here" or open using "archive manager" and extract it to where ever you want.
On command line such files are untarred using the command tar

To extract from a .tar file

syntax: tar [options]  "filename".tar

"tar" is the command to handle the ".tar" files.
General options
-x : Extract
-v: Verbose, prints on the terminal the names of the files extracted
-f:Indicating that the file name is being passed on the command line.

let us assume we have a file named "folder1.tar" which we want to untar.

For eg: 
user@Desktop:~/folder1$ tar -xvf folder1.tar
folder1/
folder1/file1
folder1/file2
user@Desktop:~/folder1$

The above example the output shows that the files that were extracted were "folder1" that had "file1" and "file2" in it. This would by default would be extracted to the present directory with the same name as the ".tar" file name i.e. "folder1".

Creating a "tar" file:

In case you have a huge set of files, with directories and subdirectories which you want to keep intact but still create a single compressed file of all the files. Then you can make use of the "tar" command.

Creating a "tar" archive retains the folder structure and when you untar the "tar" archive, all the files and folders would be untarred with exactly the same hierarchy.

For eg:

Assume we have a folder called "folder1" which has 3 folders named  "one" "two" and "three" and each of these folders in turn have a files named  "one_1", "one_2" ,"one_3" in  folder "one" .
"two_1", "two_2" and "two_3" in folder "two"
"three_1",three_2" and three_3" in folder "three"

To create a ".tar" file of the folder1 run the following command

user@Desktop:~/$ tar -cvf folder1.tar folder1
folder1/
folder1/one/
folder1/one/one_3
folder1/one/one_1
folder1/one/one_2
folder1/three/
folder1/three/three_3
folder1/three/three_1
folder1/three/threee_2
folder1/two/
folder1/two/two_3
folder1/two/two_1
folder1/two/two_2
user@Desktop:~/$

If you run the ls command now, you should be able to see a file named "folder1.tar" which is an archived file containing all the files of your folders. You can untar this file using the previously discussed command and the folder1 would appear with the three folders and the respective files in it.

options:
The options that we passed were
-c:  Create
-v: Verbose, that is why the file names were printed on the screen.
-f: To indicate that the filenames are being passed on the command line.



".tar" files are just archives and generally do not compress the size of the folders.
To compress the size you can use tools like "gunzip" or "bunzip".
That is why often you will come across files of the format

".tgz" or ".tar.gz" are ".tar" files that are compressed using the gunzip tool.
To extract them we will have to use the option "-z" with the "tar" command.


For eg:

Assume  we have a file "folder1.tar.gz"


user@Desktop:~/$ tar -xzvf  folder1.tar.gz
folder1/
folder1/one/
folder1/one/one_3
folder1/one/one_1
folder1/one/one_2
folder1/three/
folder1/three/three_3
folder1/three/three_1
folder1/three/threee_2
folder1/two/
folder1/two/two_3
folder1/two/two_1
folder1/two/two_2
user@Desktop:~/$

The command would prodcue a list of files that it is extracting. If the command executes succeefully, a folder by the name folder1 should be visible in your current directory.

Compressing a file using the gunzip:

To create a compressed file out of a tar file, we can make use of the command gzip.
For eg:
user@Desktop:~/$ gzip folder1.tar
user@Desktop:~/$ 

The above command should "zip" the .tar file into a file of the format "tar.gz".

Similar to gunzip, there is another tool that is often used for compression, i.e. bunzip.
The files compressed using bunzip are of the format ".tar.bz2".

To unzip a file compressed in the bunzip format add option "-j" to the tar commands.

For eg:

Assume we have a file named folder1.tar.bz2, which is a bunzip compressed file.

user@Desktop:~/$  tar -jxvf folder1.tar.bz2 
folder1/
folder1/one/
folder1/one/one_3
folder1/one/one_1
folder1/one/one_2
folder1/three/
folder1/three/three_3
folder1/three/three_1
folder1/three/threee_2
folder1/two/
folder1/two/two_3
folder1/two/two_1
folder1/two/two_2
user@Desktop:~/$

The output is similar to what we have seen while extracting the "gunzip" archive, a list of files being extracted.

To compress a file using bunzip we need to use the bzip2.

For eg:
user@Desktop:~/$ bzip2 folder1.tar
user@Desktop:~/$  

This will create a file of the format "folder1.tar.bz2".

.bin files:

.bin files are bascially installation files. To run the installation you need to give it execute permission. This can be done using the chmod command as follows.

user@Desktop:~/$ chmod 777 "filename"
user@Desktop:~/$ ./"filename"

.bundle files: 

.bundle files, similar to .bin files are basically installation files, which can be run as follows

user@Desktop:~/$  ./"filename"





.deb 


.deb packages are debian packages, they can be used only in debian based systems like Ubuntu or Linux Mint.

.deb packages are basically installation packages which can be installed using the following command.

user@Desktop:~/$ dpkg  -i "filename".deb 


.rpm 


.rpm stands for red hat package manager. These packages are basically installation packages used in red hat based systems. To install using this package run the following command

user@Desktop:~/$ rpm -ivh "filename".rpm 


Those the major files that we will come across in Linux.



That covers the major types of files we will come across in Linux.

Uninstall Linux

Note: This is post as of now only covers grub legacy that is ubuntu 9.04 and previous. The newer versions have grub2 in which some of the steps listed will not work. 

If you want have installed windows and linux in a dual boot system , and you make use of the grub to choose which one to boot into. Uninstalling Linux will be little work. Follow the following steps to uninstall Linux and remove the grub.

NOTE: Start this procedure only if you have a windows bootable installation  CD

1. Boot into your system and go to the windows disk management ( right click on my computer -> manage-> disk management)

2. Format the partition that has Linux in it. (just right click on the partition and select format. Say yes if it asks you for confirmation).

3. If you reboot the system now, you will be stuck in a screen that might look like this
GRUB>;
4. In case you do not have a windows installation CD and want to get into windows from the grub menu, run the following commands

         i. rootnoverify(hd0,0)  
         ii. chainloader +1
         iii. make active
         iv. boot

NOTE: The parameters (hd0,0) to the first command might be different in your system depending on how the windows has been installed. To find out other partition you can use the command "find /boot/grub/stage1"

5. If you have a windows installation cd boot from the cd and go into recovery mode and run the command  "fixmbr" and "fixboot"
  if you are using windows 7 then the commands would be bootrec /fixmbr
and bootrec /fixboot

6. If they are no errors, then reboot the system using the command "reboot".
  
If every thing has gone fine you should not see the grub menu any more and boot straight  into windows.

Unable to use a partition, even after formatting it and freeing it in windows.

Problem: Unable to use a partition, even after formatting it and freeing it in windows.

It might happen some times that some partitions get marked as "unusable" Even after you format the partition and delete it from your windows partition manager.

One of the possible reasons for this is that you have more 4 Primary partitions in your system, i.e. you have created 4 partitions in your windows machine and all of them are primary partitions.

The only solution in such a case is delete one of the primary partitions, and then recreate the partition as extended or merge it with the unusable partition and create a new bigger partition.

If you are looking for a tool to manipulate the partitions check out

http://sourceforge.net/projects/gparted/

The GUI is pretty user friendly and easy to use.

NOTE: Before you do any thing with your partitions please take a back up of your system.

Learn Linux-10 grep

AIm: To introduce th reader to the grep command and its various uses.

grep:

grep is used for searching patterns in a given file.
grep stands for Global Regular Expression Print, there seem to a lot of other full forms that are floating around in the net. But the working of grep fits perfectly to this acronym.

syntax: grep [options]  "pattern" filename 

The pattern could be any thing, strings, numbers mix of both etc.

grep by default, with out any options, will print the whole line that has the "pattern" present in it. Even if the "pattern" is a substring .

For eg:
user@Desktop:~/folder1$ grep "the" file1
creating the first file in vi editor
There are other editors that can also be used.
user@Desktop:~/folder1$

The contents of the file were

user@Desktop:~/folder1$ cat file1
creating the first file in vi editor
VI is a widely used editor.
There are other editors that can also be used.
user@Desktop:~/folder1$

Out of the three lines in the file, the second line does not have the word "the", hence the output of the command did not show the second line.
In the third line the word "the" is present as a substring of "There".

Note that by default "grep" is case sensitive.

grep comes with a huge number of options, which is why it is one of the most popular command in the world of Linux. You can have a  look at the options using the man command.

Let us look at some of the options and how to use them.

option:  -c 

-c is used to get a count of the number of lines that have the  pattern in them, Note it give the number of lines not the number of times tha pattern occurs.

To explore the options further, let us create a file with the following text in it.

Sharma always quarreled with his neighbours.
Not because he did not like them, but because he wanted to shout. That is strange isn't it. You fight because you want to shout. So one day I asked him, why do you keep fighting, if you feel like fighting. Why don't you just shout in your home. He said I tried doing that and they admitted me to THE mental hospital.

I have named the file as grep_example.

for eg:
user@Desktop:~/folder1$ grep -c you grep_example
1
user@Desktop:~/folder1$

Even though the word "you" appears twice in the secong line the output count is only "1" because both the "you" are on the same line and by default "-c" counts the number of lines that have the pattern.

option : -o

The "-o" option is used when you don't want to view the whole line but only the pattern that has matched.

For eg:
user@Desktop:~/folder1$ grep -o  the grep_example
the
the
user@Desktop:~/folder1$

We get the output "the" three times. That is because the pattern "the" is present in the input file three times.
once in the word "them". Once in the word "they" and once as the word "the" itself.

Try it out :
Can you used the "-c" and "-o" together such that get the exact count of number of times the pattern occurs instead of just the number of lines that have the pattern. Hint: You will have to use the "|" symbol that we learnt during the output redirection.

option: "-w"

The option "-w" is used when we want to match the pattern exactly, unlike the previous case where even if "the" was present as a substring it was being counted as a match. On the ohter hand if we want to match only if there is the word "the" only.

for eg:
user@Desktop:~/folder1$ grep -w the grep_example
that and they admitted me to THE mental hospital.
user@Desktop:~/folder1$

As compared to when we run the grep command with out the "-w" option, the output in this case in only the line that has the word "the" and does not list the lines that have "the" as a substring.

Option : -i

This option when we want our search to ignore the case, and search only for the matching string irrespective of the case.
For eg:

user@Desktop:~/folder1$ grep -o -i "the" grep_example
the
the
THE
user@Desktop:~/folder1$

We can see that the output also has "THE" in uppsercase too, which was not present when we searche for the same string with out the "-i" option.

option: -v

This option is used when we want to ouput all the lines that in a file that do not have the search pattern.

For eg:

user@Desktop:~/folder1$ grep -v you grep_example
sharma always quarreled with his neighbours. Not because he did not like them,that and they admitted me to THE  mental hospital.
user@Desktop:~/folder1$

The output only has the lines that do not have the string "you" in it.


option: -n

The option "-n" is used in case we want the ouput lines to be prefixed with their line numbers.

For eg:

user@Desktop:~/folder1$  grep -n "the" grep_example
1:sharma always quarreled with his neighbours. Not because he did not like them,
5:that and they admitted me to THE  mental hospital.
user@Desktop:~/folder1$

The number prefixed before the lines are the line numbers on which the matching pattern occurs.

Try it out: 
1. Use the grep options to get a count of number of lines that do not have the search pattern.


Refferences:
In case you want to learn grep in more detail you can refer to
Grep Pocket Reference

Virtual Memory

Aim: To introduce the concept of virtual memory and its working.

Virtual memory as the name suggests is not a memory that is physically present. Its used to make the processor believe that you have more physical memory (RAM memory) than is actually present .But how is it done ?

Lets assume that you have an application,X, that requires 1GB of data to run and lest also assume that your system also has 1GB of RAM. So effectively it means that to run the application ,X, you will have to dedicate whole of your RAM. Which would also mean you can not do any thing else while the application runs. But does, X, in reality require 1 GB of data all the time ? The answer is obviously NO. It will use the data as and when required. So we need not keep the data in the RAM always. Then where else do we store it ? Yes, in the secondary memory. But the secondary memory is almost the slowest device in the memory hierarchy and accessing data from the secondary memory again and again will only make your application run a lot slower.

The second problem is suppose your application needs 2GB of memory and you only have 1GB of memory. What happens to the addresses that the processor would generate beyond the 1GB of memory ? It would lead to a memory fault or out of memory fault.

The above two problems can both be handled using the Virtual memory concept. The logic used is the application would not use the whole data at the same time, but the CPU will be made to feel that it has all the memory that is needed to run the memory available to it using virtual addresses. But In reality the data will reuse the RAM locations as and when the processor asks for the data and the Vitaul addresses will be mapped to these RAM locations.
The Following figure should make it a little more clear.





As the figure shows the virtual addresses 1 and 12 both are mapped to the RAM location 1. So the memory actually being used is only for one location, but the processor gets the feeling that all the memory locations are present in the RAM.

The memory addresses that are present in the RAM are called the working set of addresses.
NOTE: Please note the memory addresses are never 1,2 etc, this was used just for the easier understanding. 

The processor generates the Virtual address but who does the conversion from the virtual address to the physical address? Who maintains the mapping from the virtual address to the physical address?

The mapping is done by the MMU ( Memory Management Unit). This is basically a hardware that does the mapping from the virtual addresses to the physical addresses.

Will soon be updated with :
What is paging, replacement algorithms. How are addresses mapped in page tables.



To understand about how Linux manages its virtual memory you can refer to
Understanding the Linux Virtual Memory Manager by Mel Gorman

Useful Commands


Commands that are useful to know, might come handy some time.


free: Displays the memory details, i.e memory free and used etc. Also shows the memory usage of swap. Useful when you want to see how much of your RAM is being used by some application.

vmstat: Gives statistics of the virtual memory usage. Similar to "free" but a little more detailed. We can also get statistics for a period of time continously for eg

vmstat 1 10

would print the current statistics for 10 seconds.

lsof :  Lists all the open files. Including different system files opened by processes. 


Learn Linux -9 Output Redirection



Aim: To introduce the reader to command redirection.


We have learnt quite a few commands by now, now lets have look at how the commands can be combined together or how can we use or store the output of a command.


Output Redirection:


Symbol used : " >" or ">>"


Syntax: "command" ">" "filename"
"command" ">>" "filename"


The ">" redirects the output of the command on its left to any file that is on its right. Please remember that ">" overwrites any existing file with the same filename or creates a new file if the file does not exist.


The ">>" redirects the output of the command on its left to the file on its right. But the difference in working from ">" is that, if the file exits, the data will be appended at the end and not over written.


For eg:

user@desktop:~/folder1$ cat file1 > file_redirect

user@desktop:~/folder1$




The command on the left of ">" is "cat". Which we know would output the contents of "file1". If we wanted to save this output to another file named "file_redirect" all we will have to do is put a ">" after "file1" and then give the name of the new file "file_redirect". If the file with the name "file_redirect" does not exist, it will be created and if the file does exist it will be over written.


For Eg:

user@desktop:~/folder1$ cat file2 >> file_redirect
user@desktop:~/folder1$


The above command would append the contents of the "file2" to "file_redirect". If the file did not exist before, it would create the file.




Input Redirection:

Symbol Used : "<"

Syntax: "command" "<" "file"

Just as we sent the output of one command to some file, we can send the input to a command from a file instead of from the terminal


To understand the working o the input redirection, lets look at a command "tr", which stands for translate.

tr:
The command is meant for translating all the occurrences of one string to other in the input


syntax: tr "string1" "string2"


string1: What characters have to transformed
string2: What should the characters should be transformed to.


Unlike "cat" and other commands we can not pass a filename as an input, but have to pass the input from the keyboard. The example below should make it clearer.




user@desktop:~/folder1$ tr cd CD

cd
CD
df
Df
user@desktop:~/folder1$


In the above example we have passed the the characters "c" and "d" to be transformed to upper case characters "C" and "D". Note that the transformation happens on each character respectively and not on a string as whole.
After you enter the command, the prompt will disappear and the cursor will be blinking waiting for you to input a string.
In the example as soon as we input "cd", it was transformed to "CD". Then we entered "df" which was transformed to "Df". Thus transforming each of the characters separately.


But what if we want to transform all the small case "a" to upper case "A" in a file. Typing the whole file on the terminal is surely not an optimal way of doing it.


In such cases input redirection can be used as shown below.


Foreg:

user@desktop:~/folder1$ tr a A < file1
creAting the first file in vi editor
VI is A widely used editor.
There Are other editors thAt cAn also be used.
user@desktop:~/folder1$



From the ouput it is evident that each occurrence of the small case "a" has been transformed to upper case "A".


Try it out:
Can "tr" be used to delete characters from an input file instead of transforming it.
Read the man page and find out what option to use for it.





Pipe:


Symbol : "|"



Syntax: "command1" | "command2"




Pipe i.e. the symbol " | " is used when we want use the output of one command as the input to another command. (The symbol is generally on the key above the enter key i.e shift+ "\").


Previously we saw how we can use input redirection to send a whole file as input to the "tr" command. We can also use "|" to send input to the "tr" as shown below.

For eg:


user@desktop:~/folder1$ cat file1 | tr a A

creAting the first file in vi editor

VI is A widely used editor.
There Are other editors thAt cAn also be used.
user@desktop:~/folder1$



What we did in the above example was send the output of the command "cat" as input to the command "tr". Which in turn transformed all the "a" to "A" and printed the output on the screen. There is not limit on how many pipes we can use together, i.e. if we want we can put one more "|" after the "tr" command and send the output as an input to some other command.


"|" is used extensively in scripting and is a very useful tool.



Try it out:

Can you write a command (i.e, using multiple commands) that will use all the redirection symbols we have learnt together.




Learn LInux: 8 - Using Commands: mv,rm,wc,who,sort,echo

Aim: To introduce the reader to a few more commands of Linux

The commands that we will look into in this chapter are



mv: Move.
The command mv is used to rename or move files.
syntax:



Note : Please note that if you use "mv" the previous copy is no longer available.
mv is very often used to rename files.
For Eg if we want to rename a file named "file1" as newfile then



If we run the command "mv" on our file1 and give the new name as newfile. "file1" will be replaced by a file named "newfile" as shown by the "ls" command.
Other than renaming mv is used to move a file from one location to another.
For example if we want to move a file name file1 from
/home/user/tes1/ to /home/user/temp1/ then



mv can be made interactive by using the -i option and the move can be forced with out any questions using -f option.

rm: remove
The command "rm" is used to remove files or directories.

syntax :



For example to delete or remove a file by the name newfile we can use


This will remove or delete the file named "newfile".
You can use the "-i" option to make it interactive or "-f" to force it.
By default most of the Linux have the interactive version of the "rm" that is, it will prompt a question confirming whether you want to delete or not. You can force the removal without being prompted by using the "-f" option.
Like "cp" "rm" also deltes only with files by default, but you can use it to delete folders too by using the "-r" option as we did with the "cp" command.

There is another command "rmdir" which can also be used to delete folders but rmdir works only for folders that do not have any files or other folders in it.

wc: Word Count.
This command is used to get a count of the number of lines,words and bytes present in a file.
syntax:



For Eg:



The first number gives the number of lines in the file.
The second number gives the number of words
The third number gives the number of bytes in the file.
And the last string is the name of the file itself.
The output can be restricted only to the line count or only the word count or the character count using the options "-l","-w","-c" respectively.

For Eg: If you want only the number of lines in the file



The number of characters can be counted using the option "-c".

Try it out:
What would happen if you run the "wc" command on a folder ?

sort:
Sort is used to sort the lines of text in a file alphabetically.
Syntax:



To understand the working of the command let us create a file with the following contents,named file_sort





We can see from the output that the words, which are different lines have been arranged alphabetically. But please note that the original file is not modified, the sorted content is only put out on the terminal.
In case you want to use the command "sort" to sort numbers, remember to add and option "-n". Because sort by default only works on strings.

Create a file with the following contents





You can reverse the sorting, i.e. in decreasing order by using the "-r" option .
Try it out:
Can you read the man page and find out the option that will enable you to write the output to a specific file.

who:

"who" is used to find out all the users who are currently logged into your system, and their details
syntax:





The ouput lists out all the users who are logged in, the "tty" represents the terminal that the user is logged in to. The remaining two fields are the date and the time of their log in.
If you want to only count the number of users you can use the option "-q" . Instead of the names it will output only the number of users logged in.

echo:
Echo just prints on the terminal what ever you pass to it.

syntax:



For eg:



The prompt by default goes to a new line after printing the string. You can prevent it by using the option "-n".
The major use of the "echo" is in script where you want to prompt the user for some input or inform the user about some action.

Learn Linux-7 More Commands

Aim: Introduce the reader to a few more commands of linux.

The commands that we will look into in this chapter are

cat
head
tail
cp
mv

To work with the above commands we would need to create a few files, and put some content into it. So go into one of the folders that we have created in the previous chapter and create two files, "file1" and "file2" with any content that you wish to put in them.

Note: Any thing put between [ ] while mentioning the syntax means, its not a mandatory field, and is meant to specify the options.
cat:
Cat basically stands for concatenate, meaning what ever files are passed to the command all of them will be printed on the output screen at once.
The major use of the command is to view the contents of the file with out opening them.

syntax: cat [options]  "filename"

For eg:

user@desktop:~/folder1$ cat file1       
creating the first file in vi editor           
user@desktop:~/folder1$   
                 

What ever you would have put into the file "file1" would be output on to the terminal.
You can also pass multiple files at the same time to the "cat" command, the contents of each file will be displayed one after the other.

For eg:

user@desktop:~/folder1$ cat file1 file2
creating the first file in vi editor          
this is second  file                                
user@desktop:~/folder1$
                    


"cat" comes with a few options, which you can see using the "man" command. One of the useful options is "-n" . This prefixes a line numbers to every line of the output.
Open the "file1" that you have created and enter a few lines of text.
Now run the "cat" on it as shown below.

user@desktop:~/folder$cat -n file1                                    
     1 creating the first file in vi editor                                  
     2 VI is a widely used editor.                                             
     3 There are other editors that can also be used.            
user@desktop:~/folder1$      
                                               

As you can see in the output, every line has been prefixed with the line number. 


Try it out:


1. What happens if you use the -n options giving more than one file as input to cat, does each file's output start with "1"
2. What happens when you run "cat" with out giving any input file.




Head:

Assume your file has 1000 lines, and you want to view the top 5 lines of the file. If you used the "cat" command, it will print out all the 1000 lines on to the screen and you will have to scroll up all the way to have a look at the top 5 lines.
This is surely not the optimum way to work.
So Linux provides you a work around, the command head.

syntax: head  "filename"
 

The number of lines that you need is passed as an option  i.e.

head -n filename

Where the "n" is the number of lines to be displayed.

From the previous run of "cat"  we know that file 1 has 3 lines. Let's use head to view only the first line

Foreg:

user@desktop:~/folder1$ head -1 file1
creating the first file in vi editor         
user@desktop:~/folder1$
                    

The other options available with "head"  are not as useful as the -n, you can try them out when you feel you are comfortable

Tail:

We just saw how to view the top lines of a file, how about when you want to view only the bottom few lines of a file.

syntax : tail "filename"

Just like "head" tail also takes number of lines to be displayed as a option.
i.e. tail -n "filename" where "n" is the number of lines you want to view.

For Eg:

user@desktop:~/folder1$
 tail -1 file1               
There are other editors that can also be used.
user@desktop:~/folder1



cp: 

"cp" is a command used for copying, files or folders.

syntax:  cp [options] "source"  "destination"

source: The file or the folder name, or path to the file or folder which you want to copy
Destination:
The file or the folder name, or path to the file or folder into which you  want to copy the file

Lets us use the cp command to create a copy of file1 by the name of file1_copy.

user@desktop:~/folder1$ cp file1 file1_copy
user@desktop:~/folder1$ 
                           




Run the command "ls" after running the above "cp" command. 

user@desktop:~/folder1$ ls              
file1 file2 file1_copy                           
user@desktop:~/folder1$   
              


The new file that we just created using the "cp" command is also visible.

some useful options that are available with cp are

 -i : Interactive, i.e. it will prompt a confirmation question before copying in case a file name by the same name exits in your current directory.

For eg:

If we try to make a copy of file2 by the same name file1_copy,  and we don't use the -i option. Then the file3 will be over written, and we would lose the previous copy of file3.
In case you want to make sure that the system checks before it does the over write use the option -i.

user@desktop:~/folder1$ cp -i  file1 file1_copy
cp: overwrite `file1_copy'?



If you type "y" when the above prompt comes the the "file1_copy" will be over written, if you press "n" it will not be over written.

-f:  Force
In some systems the "cp" command is such that it prompt for confirmation every time you try to copy, in such systems if you do not want to be prompted then use the "-f"  option, this will force the copying with you asking you the question.

-r: Recursive

Suppose you want to copy a whole directory instead of a file. In our example suppose we want to create a copy of folder1 and call it folder1_copy.
If you try using the copy command directly like we used it for files you would get the following error

user@desktop:~/folder1$ cp folder1 folder2
cp: omitting diretory `folder1'                    
user@desktop:~/folder1$   
                         


This is because by default "cp" is meant only to copy files and not folders. In case you want to copy folders use the option "-r".

This means recursively copy all the files in the folder to the new folder mentioned.

user@desktop:~/folder1$ cp -r folder1 folder2

You can combine any of the options together and use it. For eg you can interactively copy a folder by using the options "i" and "r" together.

Try it out:
1. What do you think will be the output of the following command
"cp file1 file2 folder1" ?
Where file1 and file2 are simple files and folder1 is a directory.

Error 17 while trying to reinstall the grub

 Issue:  Error 17 while  trying to reinstall the grub 


Background: 

 If you happened to install ( or reinstall ) windows after installing Linux, you would not be able to see the grub menu. That is because our dear windows assumes that it is the only OS on the system and over writes the MBR ( Master Boot Record) which erased the Grub ( boot loader for Linux).
The easiest work around is always install Windows first and then Linux, because Grub boot loader makes sure to keep what is previously installed.

The standard way of reinstalling Grub, in case you are not using Grub2 is.

Boot using a Live CD
open a terminal
Type the following commands

  • sudo grub ( this should take you to a grub prompt) 

  • root (hd0,0)
  • setup (hd0). 
  • quit. 
There are chances that you when you type the third command ( i. e. setup (hd0) ) 
you might get an error  
error 17 can not mount partition ( or some thing similar) 

This is because the command that we executed before i.e. root (hd0,0)  we have passed the wrong number in the second argument. To know what to pass 

run the command ( in the Grub Prompt itself) 
find /boot/grub/stage1 

this will give you an output with the right partitions ( i.e.  hd(0, "some number"). 

Use this number in the above "root" command and the run the "setup" command and you should be able to reinstall the grub successfully. 

Learn Linux -6 VI editor. Basics.

Aim: Introduce user to usage of the "vi" editor.

Now that we have learnt how to create directories, and use them, lets look at how do we create simple text files.
Linux by default comes with quite a few editors, the most famous ones being the VI and emacs.
If you happen to be using a new distro of Linux like Ubuntu, you sure will have a text editor just like the famous notepad of Windows.
The editors, vi and emacs are famous mainly because the support  a number of programming and scripting languages and they are easy to use from the terminal while writing programs or scripts.

We will look into the VI editor, its usage and various options available with it.

Note: In the new versions of ubuntu, the vi editor installed in not a fully equipped one, please use the synaptic package manager to update it by installing the package "vim". To learn more about how to install packages using synaptic package manager, have a look at
 " http://tuxthink.blogspot.com/2010/03/using-synaptinc-package-manager.html"


Creating Files using the "vi" Editor:


Syntax : vi  "filename"


For Eg:

user@desktop:~$ vi file1


A file will open up, with a blinking cursor, and the name of the file on the left corner below.  As shown below


In "vi" editor you can not just start typing right away like the usual editors.
The usage of mouse is very restricted in "vi" , more often only for copying and pasting only, which also can be done by commands.

"vi" editor has three modes of working
          Text mode :  In text mode, you can enter the text in to the file, delete it edit it etc,just like you can do in any other editor.
           Command Mode: This is some thing special you might not have come across. The command mode is basically to execute commands, when you are in command mode you can not enter any thing in the file but you can run commands. For Eg: You can run command to copy a few lines or paste a few lines . Etc.
           Visual mode:  This is used when you want to use certain commands on specific set of text, we will not be looking into this now.
  


Text Mode: 


To enter into text mode press "i" after you open the file using the above mentioned command, you should see a tag "insert" appear at the screen below, where there was file name before. As shown below. 










Now you can start typing into the file. You an use the arrow keys to move about the lines. The delete and backspace work as usual.

Command Mode: 


Once you are in the text mode, you can change to command mode by pressing the "esc" key. This will remove the "insert " tag that had appeared while you were in the text mode. As shown below.


To run any command in the command mode first you have to type ":", i.e. a colon, and then type the command.
Some simple commands  :
To save the file that we just created type ":w" as shown below


and press "enter", if the file was saved successfully you will see the tag below change as shown below.




To quit the file, with out saving  the command is "q".

To save and quit, combine both the commands that we used , i.e. "wq".

Deleting a line:  "dd"  To delete a line in the vi editor, move the cursor to the line and just press "dd" , i.e. d twice. Remember you need to be in command mode,and  this need not be entered with ":". Just hit "dd" and the line will get deleted.

Deleting multiple lines: "numberdd": To delete multiple lines just press the number of lines you want to delete followed by the "dd". For Eg if we want to delete 3 lines
press "3dd" on the keyboard and 3 lines should get deleted.

Note: "dd" actually cuts the lines, you can use "dd " when you want to cut-paste also. 

Copying a line: To copy a line, move the cursor to the line you want to copy, enter the command mode, and hit "yy" (two times "y"). The line will get copied

Copying Multiple lines: "numberyy"To copy multiple lines the logic is similar to the delete, i.e type the number of lines to copy followed by "yy". For eg To copy 3 lines type "3yy" .

Pasting lines: To paste the lines that are copied using the "yy" or cut using "dd", move the cursor to the line below which you want to paste this new line and hit "p". The new line will get pasted.

These are the very basic commands of the "vi" editor. "vi" is very powerful editor with a number of options and we surely will look into it more detail once you get comfortable using it.

Note:Some times the editor might not quit when you run "q", then you can force it to quit by using the command "q!". 


If you want to learn more about vi you can refer http://www.vim.org/index.php

Next: More commands of Linux.

Learn Linux -5 Basic Commands with Options

Aim: To explore the basic commands of linux further with its options.


We have learnt the following four commands

mkdir: creates a diretory
ls: lists the contetns of a directory
cd: Change directory
pwd: displays your present working directory
man: Displays help pages.


All the commands in linux come with a number of options, which often come very handy. If you did try looking at the man page for any of the commands that we learnt in the previous chapter, you would have seen a lot of options listed for all of them.
We will learn some of the important options for each of the commands, the rest of them am sure you will learn yourself when you feel the need for it.

The standard format for passing the options to a command is

command  -"options"   "arguments"  


Please note the "-" its  required. 

mkdir: 


There are not many options with this command but one useful one we can look into currently is the -v.

just "mkdir" does not tell us whether the folder was created successfully or not  so if you want to see if the folder was indeed created or not, use the option -v

for eg:

user@desktop:~$mkdir -v folder2  

mkdir: created directory `folder2`  
user@desktop:~$                            



ls: 

"ls" comes with a huge number of options. We will learn a few most commonly used. 


Long listing: 


The "-l"  option with "ls"  gives more detailed listing of the contents of your folder. That is information like when was the file edited, how big is it etc.  

For eg:

user@desktop:~$ ls -l                                       
drwxr-xr-x 2 user user 4096 2010-04-09 14:16 folder1 
drwxr-xr-x 2 user user 4096 2010-04-09 14:16 folder2  
user@desktop:~$                                                                 

  • The first column having the fields with letters r,w etc represent permissions we will understand that a little later 
  • Second column is the number of links, don't bother about it now. 
  • Third column is the username  of who created the file. 
  • Fourth column is the Group name to which the user belongs to (More on this later) 
  • Fifth coumn is the size of the folder or file 
  • Sixth column is the date on which it was last modified 
  • Seventh column is the time at which it was modified
  • Eight column is the name of the folder file etc. 
There are a lot more options with "ls",  once we learn how to create files will look into a few more of them. 

cd:
Most often, there will be no manual entry for cd, that is because it comes built in with bash(Bourne Again shell) and is not a seperate executable like other 
commands. 
One useful information to be aware while using cd is how to go back folders. 
For Eg: 

user@desktop:~$ cd folder1
user@desktop:~/folder1$    

We are in folder1 now, what if we want to come out of folder1 and got to folder2? 

use ". ." (yes two dots)  with cd 

user@desktop:~/folder1$  cd .. 
user@desktop:~$                      
user@desktop:~$ cd folder2     
user@desktop:~/folder2$         

or  a shorter way of doing it is. 

user@desktop:~/folder1$  cd ../folder2
user@desktop:~/folder2$                         


So that was a little introduction to the options available with the basic commands that we have learnt. Hope it has been smooth until now. 

Lets look to a few more commands in the next chapter.  

Learn Linux -4 Basic Commands

Aim: To introduce the reader to a few basic commands of Linux.

Now that we know how to launch a terminal and how to move about in a terminal lets learn some commands that we can run on the terminal.
If you want to refresh how to use terminal. Have a quick look at
http://tuxthink.blogspot.com/2010/04/learn-linux-2-using-terminal.html

Now Open the terminal. Try these commands out as you read, its the best way to get a hang of the commands.

We will start with running the commands and then go then later dive into the commands in detail.

mkdir: Make Directory. 


Creates directory or folder by name specified.


Syntax:    mkdir  "directory name" 


Eg:  mkdir folder1

once you type the command and the name of the folder you want to create, hit enter. if every thing goes fine, the cursor will simply move to the next line.

user@desktop:~$ mkdir folder1  
user@desktop:~$                       


ls: list


Only ls Lists the contents of the folder you are working in. There are ways to lists contents of other folders, we will come to that a little later.

syntax:    ls  

If you did run the "mkdir" and there was no error, how do you check did the folder actually get created or not ?
Just type "ls" on the command prompt and hit enter. It should list out all the folders in the folder/directory you are working in.

user@desktop:~$ ls 
folder1                      
user@desktop:~$    




cd: Change Directory 


The command changes the directory you are working to the new directory that is mentioned with the command.

syntax: cd  "directory name" 


For Eg:  cd folder1

Type "cd" followed by a folder name, in our case "folder1" which we just created.
Hit enter and if the command runs proparly, the command prompt will move to the next line. But do you see any difference in the prompt. See the example below, you should be able to see the folder name before the "$". 

user@desktop:~$ cd folder1
user@desktop:~/folder1$     




pwd: Present Working Directory


The command displays the folder that you are currently working in.

syntax:  pwd


Type "pwd" and hit enter, you should see the path to your directory displayed as shown below.

user@desktop:~/folder1$pwd 
/home/user/folder1                  
user@desktop:~/folder1$        


Remember the filesystems ? If you look at the path, it starts from "/" the mount point from where all folders originate in Linux. 



man: Manual Pages. 


The command displays help pages for any command that is passed to it.

syntax: man "command name". 


For eg : man ls

If you run the above example you will be shown a whole page explaining the working of the command ls and the various options that comes with it.  If you don't understand the options don'e worry we will get to it.
So in case any time while using linux you want to learn about a command just use the command "man".
To exit the man pages just press "q"

Another command similar to man is "info" which also works similarly but provides a little more in depth information.





Great so now we know how to create a directory, check the contents of a directory, change into a directory and find out the path to the directory we are working in. Also get help for any command we want. 

If you have got these commands working, lets explore them further with a lot of options that are available with them in the next chapter. 

Learn Linux -3 The File System

Aim: The article aims to introduce the Linux Filesystem to the reader.

Filesystem: 


Before we start learning about the Filesystem followed by linux, let us quickly have a look at what is a FIlesystem all about. We will only look at the basics and may be come back for a more in depth understanding later. 

The data stored in the hard disks, CDs or any other storage media need to be accessed by the operating system to present it to the user. For Eg: In windows when we click open c: and open the c:\windows, it should show us only the files that are a part of the windows folder. 
Every operating system uses a specific format to store and access the data on the storage media . This format is called as the filesystem. 
The standard filesystem used by the widows is FAT(File Allocation Table) and NTFS(New Technology File System).

With out a filesystem it would be very difficult to efficiently manage and  retrieve the data.

Linux Filesystem:


The Linux file system follows a hierarchal directory structure. Unlike windows there are not partitions like c:\ or d:\. All the directories originate from a single point "/" called as the mount point.  The following Image should make it more clearer. 



The directories shown in the above figure that originate from "/" are the default folders present when Linux is installed. Every user other than the root is has a home folder in  "/home/username".  
The root on the other hand has his home folder under the "/" itself i.e.  "/root"

If you have not logged in as "root" you will be able to create files and folders only after "/home/username/". 


For Eg: Here is a screen shot of the home folder for the user test (in ubuntu)



The filesystem that Linux follows is ext3(Third extended) or ext4(Fourth Extended ).


There is a lot more in the domain of filesystems but to start with linux, this much information should be fine.

If you have got the hang of the file systems.... then lets move to learn using the commands on the terminal. 

Useful Links

Reinstalling Grub after it gets erased due to installation of windows

https://help.ubuntu.com/community/RecoveringUbuntuAfterInstallingWindows

Create a Live cd :

http://www.linux-live.org/


Info on Grub2:


https://help.ubuntu.com/community/Grub2
http://www.dedoimedo.com/computers/grub-2.html

Graphical tool to modify grub

http://grubconf.sourceforge.net/

Tools to take a back up or create a CD of your current linux system.
http://geekconnection.org/remastersys/ubuntu.html
http://www.fsarchiver.org/Main_Page
http://www.sysresccd.org/Main_Page

Setting up NTLMAPS in ubuntu to take case of the Proxy Authenticaton problem while using the package manager.
http://ubuntuforums.org/showthread.php?t=758702


Linux Books:

Linux Device Drivers 2nd editions by By Alessandro Rubini & Jonathan Corbet

http://www.xml.com/ldd/chapter/book/

Linux Kernel In a Nutshell Online Book

http://www.kroah.com/lkn/

How to remove GRUB

http://www.cyberciti.biz/faq/linux-how-to-uninstall-grub/


Scripting:

http://linuxcommand.org/script_library.php

Script to convert the man pages to pdf

http://www.linuxquestions.org/questions/linux-general-1/how-to-make-shell-script-wait-for-key-press-to-proceed...-687491/#post3360763

General links:

http://wiki.linuxquestions.org/wiki/Slackware-

Links#.22.27.27.27Slackware_LQ_Suggestions_Links.21.27.27.27.22

http://rute.2038bug.com/index.html.gz

Article on writing Usb Driver

http://www.linuxjournal.com/article/4786

To get info about booting

http://bootinfoscript.sourceforge.net/

Installing with out  CD using .iso

http://ubuntuforums.org/showthread.php?t=666267

NS2 Installation Steps

http://vanitdiscovery.blogspot.com/2009/05/ns-2-installation-on-ubuntu-904.html


Restore Grub from live cd

http://ubuntuforums.org/showthread.php?t=224351

Boot info script

http://ubuntuforums.org/showthread.php?t=1291280

Live cd downloads
http://www.livecdlist.com/

About RootSudo

https://help.ubuntu.com/community/RootSudo

Learn Linux -2 Using the Terminal

Aim of the article: Introduce the reader to the usage terminal in Linux.

Linux has always been known for its powerful commands. A number of linux lovers just can't get themselves to use the mouse once they get used to using the command line.

The command also help use create scripts, which in turn help us automate a number of tasks.

The Terminal:

The terminal is the interface where you can enter the commands and view the results. The terminal works only with commands and mouse is not supported ( except for select copy and paste).

If you have installed ubuntu you can open a terminal by going to
Applications-> Accessories -> Terminal.

Other distributions might have it under a different path. For eg: the red hat variations have the terminal under "system tools".

Once you click on the terminal, a white window should pop up with a blinking balck cursor. This is your Terminal.
Behind the blinking cursor will be your username, followed by the directory you are currently working in and then the $ prompt.
For eg if the user name you have logged in with is "user1"

user1@desktop:~$

desktop is the name of your computer.
what follows after : is your current directory. ( We will come to meaning of ~ later).

The "$" signifies the end of the prompt, from where you can start typing your command.

Before starting to learn commands lets learn how to navigate in the terminal using the keyboard.

type any thing on the open terminal. What ever you type will appear on the screen.

Foreg:

user1@desktop:~$ testingtheterminal

  • You can delete what you have typed by using the backspace.
  • You can move ahead or behind on the line using the arrow keys.
  • You can delete specific characters in between by moving the cursor to the character and using the delete key.
  • To execute or run the command you enter on the terminal hit enter.
  • In our case now as we have not entered a correct command we will get an error
bash:testingtheterminal: command not found
Which means the computer did not understand what you asked it to do.
  • Now if you use the up arrow key, you will be able to go back to the command you executed before. Which means the terminal remembers what ever you do and you can use the up and down arrow keys to move to the commands you executed before and need not type it again and again.
  • To copy some text from the terminal using a mouse select the text-> right click the mouse->click on copy. The text will be copied and can be pasted to where ever you want.
  • Mouse can also be used to scroll up and down in the terminal. Open the terminal and keep hitting the enter key as until the prompt exceeds the window.Now you can scroll up and down the terminal using the scroll wheel of your mouse.
  • You can open any number of terminals simultaneously, you can also have tabs in the terminals. Click on the file option in your terminal, you will find and option for new tab.
  • There are a few advanced options also available which makes working on terminals even more simpler, we will explore them as we learn the commands.






Learn Linux -1 Overview

Aim of the article : Introduce the user to the world of Linux.

History and Features:

  • Linux is an open source Operating system, which means you can view the source code of the operating system, edit it do what ever you feel like ( at your own risk ;-) ).
  • Linux was created out of Minix(another operating system) by Linus Trovalds in 1992.
  • Linus created the OS just as a hobby.
  • Its official Mascot is the Tux the penguin ( said to be derived from Trovalds and Linux )
Tux
  • Linux is a multiuser  and multitasking operating system , it supports 6 default  consoles at a time.
  • The Linux kernel is monolithic.
  • You can download the Linux kernel source code from http://kernel.org/
Kickstart:


The superuser
Every Linux system has a superuser called as a root. A superuser is like an administrator who can do any thing in the system, edit any of the system files and do all other administrative tasks.
Linux being open source, the root can also modify the kernel source files and hence one has to be very careful while working as root.

To make the systems more secure the newer distributions of Linux come with the root user disabled. Any user can become the root , if he has the administrative privileges, by using the command sudo. This ensures that any one who is running a command or a script becomes a root only when it is necessary and prevents any accidental damage to the system.

Linux Modes:

You can work in two modes in any linux system

  • Text Mode
  • Graphical Mode
In text mode, you can work only with commands and the mouse is not available, where as the graphical modes is the one in which have the graphical windows to work with and also the mouse is available for use.

If you are not looking for programming or scripting in Linux then this info should be enough for you to start using the system. The newer ubutnu versions as very user friendly and you should not face much problems in it.

In case you want to learn to use the powerful commands available with linux, and learn how to write scripts then move to the second chapter.

Reference Books: 

As a Beginner one of the best books available for Linux is
A Practical Guide to Linux by Mark G Sobell

If you want to learn specifically about Ubuntu or Redhat , then the same author also has written books for it also.
A Practical Guide to Ubuntu Linuxby Mark G Sobell 

A Practical Guide to Fedora and Redhat Linux Linuxby Mark G Sobell 

Some more books Specifically on Ubuntu
Ubuntu Pocket Guide and Reference

Ubuntu Kung Fu: Tips, Tricks, Hints, and Hacks

Follow by Email