Monday, 31 October 2011

Linux practice tutorial1


Try out these tutorials you will come to know real linux  clearly lets do it buddy.......

if you just practice these tutorials then your basics are strong other wise try to learn .....
Chapter 1- Over View of Linux Systems

Exercises:

What is OS?
What are the different OS?
When is OS used?
What is partition and its use?
How many partitions can be done?
What are the layers in Linux Kernel Structure.
Which command is used to open a document(file with .doc extension)?
Which command can be used to open pdf?
Which command is used to run a presentation?
Name some applications provided by any Linux OS?
Which layer is called the Heart of Linux OS?

Chapter 2 - Command Line Interface

Contents:
                env variables
                bash variables
                bash commands
                built in commands
================================================================

Exercises:

What are the two types Interface between user and system?
What is CLI and GUI.
Which interface mainly depends on command entered using keyboard?
How can you switch from CLI to GUI in Linux?
Which are the different Shells.
Which Shell is more powerful and advanced?

Try the following command sequence:

[observe the differences that occur in the output it is so beautiful results ]
                 
                Cd  -> go back main home
                Pwd->print working directory or present working directory
                ls –al->list long list of files includeing .files
                cd .   ->
                pwd     (where did that get you)
                cd ..   ->goes to previous directory
                pwd  ->
                ls -al
                cd ..
                pwd
                ls -al
                cd ..
                pwd     (what happens now)
                cd /etc
                cd -
                pwd

Find  your home:
                cd
                ls
                pwd

                cd /home
                ls
                pwd

                Your home is: ______

Check the difference of following commands:
               
                cd
                ls -l
                ls -L
                ls -a
                ls -al
                ls -tl
                ls -rtl
                ls -l  Desktop/
                ls -ld  Desktop/

Check man pages:

                cd
                man ls (you may need to press q to quit)
                man pwd
                man cd (do you notice anything unusual)
                info ls
                info cd
                (check man and info pages of all commands)

Try out:
                cd
                echo hello world
                echo "hello world"
                env
                echo $SHELL
                echo $USER
                echo $USERNAME
                echo $PATH
                echo $PWD
                echo $PS1
                echo $EMERTXE
                hello = 5
                hello=5
                echo hello
                echo $hello
                PS1="[helloworld..........]"

Setting env Variable
                cd
                env      (check for varible INDIA)
                INDIA=INDIA
                env      (check for varible INDIA)
                export INDIA
                env        (check for varible INDIA)
                unset INDIA
                env        (check for varible INDIA)

Find out the various shell built in commands.
How can you find them?
Why are they called so?

Try out:
                history
                clear

Chapter 3 - User Interface
Contents:
                User Interface
                user specific commands
                misc useful commands
                redirection
                piping
                Exercises:
========================================================

1. Try these commands and check the output.
                whoami
                who
                id
                w
                date
                hostname
                arch
                uname -a
                dmesg
                df
                du

               
2. Check these
                ls             (do you see the output)
                ls > file1.c            (where is the output, open file1.c and check whether the output is there)
                ls -l > file.c   (open file1.c and check whether the output is there, check previous output)
                ls >> file.c            (open file1.c and check whether the output is there, check previous output)
                (more examples on Chapter 4)
               

Chapter 4
Contents:

                Directory and File System Structure
               
file system related commands
               
file related commands

file permission 
               
file links
               
advanced file related commands
               
file compressions
               
archiving with tar files
               
filters
               
patternmatching
===================================================================================

Exercises:


How is the root directory represented?

Write a command to list all files in /bin directory?

Where are the environtmental variables stored(Which directory)?

Which command is used to display all the contents of a file without actually opening it?


How will you print the following informations:
               
memory informations
               
cpu informations
               
file system informations

devices informations
               
linux version

Create an empty directory named 'Test'  

Create an empty file named 'file1.txt'

Try this:
stat -L file1.txt             
(What do you observe)

stat --help              (check for all commands)


Try out these
               
                cd
                mkdir A
                cd A/  
                pwd               (where are you now)
                mkdir B1 B2 B3
                ls                   (what did you see)
                cd B1/
                pwd               (where are you now)
                ls                   (what did you see)
                touch fileb
                ls
                cd
                pwd               (where are you now)
                tree A/           (what did you see)
                cd A/B2/
                pwd               (where are you now)
                ls
                cd ..
                pwd               (where are you now)
                rmdir B3        (what happened)
                ls                  (what did you see)
                rmdir B1        (what happened)
                rm -r B1        (give 'y' to all queries)
                ls                  (what did you see)
                cd
                pwd

now try to delete directory A. Which command will you use? why?



Now make some directories and files in this structure, in your home directory
parent
|
|----child1
|    |
|    |----child4
|    |         |
|    |         |----child7
|    |              |
|    |              |-file4
|    |
|    |----child5
|                |
|                |-file3
|
|
|----child2
|    |
|    |----child6
|                |
|                |-file2
|
|----child3
|
|----file1


now try these:
                cd
                tree parent          (you shuld get this same structure as the output.)
                cp   parent/child1/child4/child7/file4    parent/     (What do you see)
                tree parent          (What do you see, where is your file4 located. why))
                mv   parent/child2/child6/file2   parent/child3/
               tree parent          (What do you see, where is your file2, why)
                mv   parent/child1/child5/       parent/child1/I-AM-CHILD5/
                tree parent         (What do you see, where is your directory child5 )

Now try to do these yourself:
                remove child2/
                copy child7/ inside parent
                mov child2 to child4/
                rename file1 to FILE1



Try these:
                cd
What is the use of cat?
                cat > new_cat_file.txt    (what happened?)
                cat new_cat_file.txt       (what did you see?)
                cat                               (what is it doing, enter hello, go on entering some words, what happened? how will you stop it? Why is it happenning so?)

Whatis the default input and output to cat?
               
                head -5  new_cat_file.txt        (What is the output)
                tail -3  new_cat_file.txt        (What is the output)
               
                wc new_cat_file.txt              (What is the output)
                dmesg > dmesg_file
                cat dmesg_file
                less dmesg_file            what is the difference between cat and less.
Try            more dmesg_file


Check out the outputs and reason
                dmesg | less                                      (what happened)
                ls | wc -l
                ls / | wc -l
                head -10 dmesg_file > d_file.txt
                tail - 5 dmesg_file > t_file.txt
               
                cat new_cat_file.txt d_file.txt t_file.txt file32.txt > file_redirect.txt           (what is the output)
                
                cat new_cat_file.txt d_file.txt t_file.txt file32.txt >> file_redirect.txt  2> error_file         (so whatis inside error_file)

Now write the commands for the following:
            
print only the 25th line of file 'dmesg_file'

find the number of words in the second last line of the file          d_file.txt
               
print the name of 2 files which are modified last.

print all the .txt files in your home directory .

find all the words with letters "ty" in the dmesg.        (hint: grep)
               

Create a file 'file_car_rep' with the following names;
                hyundai verna
                honda civic
                maruti swift
                maruti swift
                mahindra scorpio
                ford fiesta
                fiat linea
                fiat linea
                tata aria
                toyota etios
                tata aria
                maruti swift
                hyundai verna
                chevrolet cruze
                chevrolet cruze
                mahindra scorpio
                maruti swift
                mercedes benz
                mahindra scorpio
                nissan micra
                honda civic
                volkswagen vento
                toyota etios
                toyota etios

Now write the commands for the following, considering 'file_car_rep' as source file
                create a new file 'file_no_rep' in which names are not repeated.
                create a new file 'file_last_name, which contains only the second name of cars in file 'file_no_rep'
                split 'file_car_rep' to different files(name starts with split_file) which contain 3 lines each
                create file 'file-upper-case' in which all the characters are in upper case of 'file-car-rep'
                sort the names of file 'file_no_rep' in reverse order and store in file file_rev_sort


convert the directory 'Desktop' in your home, to a file (name starting with file_desktop.)
include the directory 'Documents' also to file_desktop
print the contents of converted file 'file_desktop' on the screen to check whether 'Desktop' and 'Documents' are there.
compress the file 'file_desktop. '
move the compressed file to a new folder called 'Test'   (create and move if necessary)
restore the commpressed file to its original size
extract the directory 'Documents' from the compressed file



Chapter 5
                Vim Editor
                cursor movement
                exiting from a file
                search mode

==============================================================
Exercises:

What are three modes in vim editor?
How will you open a file in vi editor?
Consider you are in escape mode, how will move to editing mode? Then how will you move back to escape mode?
How will you save the file and remain in the file without quitting?
which command will you use to copy 10 lines? in which mode?
Which is the command to append to the last of the line?

Commands used in vi editor in command mode:
                to move to the bottom of the file(first line )
                to move to the start of the file.(last line )
                to copy 10 lines
                to paste the lines above the cursor
                to delete from the cursor to the end of a line
                to delete  a line
                to move to the end of the line
                to delete a character
                to undo
                to move one page down / up
                to  search a word in the file
                to replace a string by another globally
                to replace a string by another globally by choice
                to move  until the start of the next word
                to move to the matching parenthesis or bracket
                to move to the nth line
                to move to another file without closing the current one
                to copy the contents of othe file without closing the current file
                to set the line numbers in the file
                to save the file with another name

visual mode
                to select some block of data
                to copy them
                to delete them
                to paste them

----------------------------------- ------------------- ------------------------- --------------------------------------------------------------------------------
Find how many processes are running in your system.
Find process with highest pid value.
Find file with largest size.
Find directories with maximum number of subdirectories.
Find the file with largest size and save its information in a file called "long_file".
Command to copy line number 5 to 10 from file1 to file2.
Write a command to count the number of charecter in 15th line of a file.
write a pipeline sequence to list all files which has got executive permission for group.
List files having "x" anywhere in their names except for first and last charecter.
List files having any digit in their filename.
Find number of shells in your system.
Find number of files in your login directory.
Find number of .c files not starting with main.
Find number of header files in a given .c file
Find number of files in your login directory. Any type of file other than directory
Find number of .c and .h files in your login directory
///////////////////////////;;;;;;;;;;;;;;;;;;;;;;;;;;;////////////////////////////////////

sample programs that are very complex dont be frightened by seeing this programs its just to make you understand 
these were written by experienced people so if you get experience you can also write them ...................

#include<stdio.h>

#define RESET                   0
#define BRIGHT                 1
#define DIM                       2
#define UNDERLINE            3
#define BLINK                    4
#define REVERSE               7
#define HIDDEN                 8

#define BLACK                  0
#define RED                      1
#define GREEN                  2
#define YELLOW                 3
#define BLUE                     4
#define MAGENTA              5
#define CYAN                    6
#define WHITE                   7

void textcolor(int attr, int fg, int bg);
int main()
{             
                textcolor(BRIGHT, RED, BLACK);               
                printf("Anchu\n");
                textcolor(RESET, WHITE, BLACK);             
                return 0;
}

void textcolor(int attr, int fg, int bg)
{              char command[13];

                /* Command is the control command to the terminal */
                /* echo "^[[0;40;47mIn Color" */
                sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
                printf("%s", command);
}


‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’///////////////////////////////////////’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

#include <stdio.h>
#include <stdlib.h>

#define RESET                   0
#define BRIGHT                                1
#define DIM                      2
#define UNDERLINE       3
#define BLINK                   4
#define REVERSE                             7
#define HIDDEN                               8

#define BLACK                  0
#define RED                       1
#define GREEN                 2
#define YELLOW                               3
#define BLUE                     4
#define MAGENTA                         5
#define CYAN                    6
#define                WHITE                   7

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

char *attrs[] = {"NORMAL", "BRIGHT", "DIM", "UNDERLINE", "BLINK",
                "REVERSE", "HIDDEN", "EXIT"};

char *colors[] = {"BLACK", "RED", "GREEN", "YELLOW", "BLUE", "MAGENTA",
                "CYAN", "WHITE", "EXIT"};

void textcolor(int attr, int fg, int bg);

int print_menu(char *array[], int n_options, char *title);

int main()
{              int attr, fg, bg;
                int attr_size, colors_size;
                char *c;
                c = (char *) malloc (20);

                attr_size = ARRAY_SIZE(attrs);
                colors_size = ARRAY_SIZE(colors);
                while(1)
                {              printf("\n");
                                attr = print_menu(attrs, attr_size, "Choose the attr you want:");
                                if(attr == attr_size - 1)
                                                break;
                                fg = print_menu(colors, colors_size, "Choose the foreground you want:");
                                if(attr == colors_size - 1)
                                                break;
                                bg = print_menu(colors, colors_size, "Choose the background you want:");
                                if(attr == colors_size - 1)
                                                break;
                                printf("\n");
                                textcolor(attr, fg, bg);   
                                printf("Enter your Name\n");
                                scanf("%s", c);
                                system("clear");
                                textcolor(attr, fg, bg);   
                                printf("\t\t%s\n ", c, attrs[attr], colors[fg], colors[bg]);
                //            printf("This is what you get if you use the combination %s attribute %s foreground and %s background", attrs[attr], colors[fg], colors[bg]);
                                textcolor(RESET, WHITE, BLACK);
                }
                return 0;
}

int print_menu(char *array[], int n_options, char *title)
{              int choice, i;
                for(i = 0;i < n_options; ++i)
                                printf("%d.%s\n", i, array[i]);
                printf("%s", title);
                scanf("%d", &choice);
                return choice;
}                             

void textcolor(int attr, int fg, int bg)
{              char command[13];

                /* Command is the control command to the terminal */
                /* echo "^[[0;40;47mIn Color" */

                sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
                printf("%s", command);
}

To create Hard link :
                we have a file "a-file.txt" that contains the string "The file a-file.txt".
                Now we use the "ln" command to create a link to a-file.txt called b-file.txt.

                                 ln a-file.txt b-file.txt

                The two names a-file.txt and b-file.txt now refer to the same data.
                cat a-file.txt
                                The file a-file.txt
                cat b-file.txt
                                The file a-file.txt

                If we modify the contents of file b-file.txt, then we also modify the contents of file a-file.txt:
                                vi b-file.txt (edit some thing in the file).

                                % cat b-file.txt
                                                The file a-file.txt has been modified.
                                % cat a-file.txt
                                                The file a-file.txt has been modified.

                If we remove the file a-file.txt, we can can still access the data through the file b-file.txt

                % rm a-file.txt
                rm: remove `a-file.txt'? y
                % cat b-file.txt
                                The file a-file.txt has been modified.

To create Soft link :

                we use the -s option of the ln to create a soft link:


                                % ln -s a-file.txt Symbolicb-file.txt
                                % ls -F

                A symbolic link, that ls -F displays with a @ symbol,
                If we change the file Symbolicb-file.txt, then the file a-file.txt is also modified
               
                If we remove the file a-file.txt, we can no longer access the data through the symbolic link Symbolicb-file.txt

                % rm a-file.txt
                rm: remove `a-file.txt'? y
                % cat Symbolicb-file.txt
                                cat: Symbolicb-file.txt: No such file or directory
search for the character n in filename and replace it with xyz.
$ sed -i 's/n/xyz/g' filename
NB -i is for actually doing it in the file. Without it, it would just display what change it would do

No comments:

Post a Comment