Wednesday, 13 August 2014

Basic Linux Commands II



File Related Commands:

Touch:

Create single and multiple file at a time
# touch <file name>
# touch <file1> <file2> <file3>
# touch {1..100}

cat:

Can create a file.
Appending text to a file.
View or Display or Read.
Can Combine two files data into third file.
# cat <file name>           (to view or read file)
# cat > (file name)           (to create a file) and for saving “ctrl+ d”.)
# cat >> <file name>           (to append text to existing file)
# cat <f1> <f2> >> <f3>           (to add f1 f2 to f3)
# cat -n <file name>           (view file with line no.)
# cat <f1> <f2>           (view multiple files)

vi (editor):

You can use vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read a text file.

vi editor modes:

Insert mode
command mode
Execute mode

Insert mode or write mode:

Used for entering test.
When you first enter the editor, you are in the command mode.
To enter the write mode, type the letter ”I” for inserting text.

Command mode:

This mode enables you to perform administrative tasks such as saving files, finding and replacing. In this mode, whatever you type is interpreted as a command.
To enable this mode press esc+shift+; 

Execute mode:

In this mode you can delete, copy, paste, replace etc. words or lines in a file.
For enabling the mode press “esc” button.

Commands in vi:


  • To copy a line           “esc yy”
  • To copy 3 lines           “esc 3yy”
  • To paste a line           “esc p”
  • To page down           “esc+shift+g”
  • To page up           “esc+shift+h”
  • To insert a new line below           “esc+o”
  • To insert a new line above           “esc+shift+o”
  • To delete a line           “esc+dd”
  • To delete a multiple lines           “esc+4dd”
  • To delete a character           “esc+x”
  • To delete a word           “esc+dw”
  • To undo changes           “esc+u”
  • To replace a character       “esc+r <new character>”
  • To replace a word in a line      “shift+:s/search/newword/g”
  • To find and replace word in file  “shift:%s/search/newword/g”
  • To search a word           “shift+:/word”
  • To go to a particular line           “shift+:15”
  • To set line number           “shift+: set nu”
  • To unset number           “shift+:nonu”
  • To save a file without quite           “shift+:w”
  • To quite without save           “shift+:q”
  • To save and quite           “shift+:wq”
  • For forcefully save, quite, save and quite  “shift+:w! or q! or wq!”


For deletion “rm”:

This command used to delete a file or directory.
# rm <file name>
# rmdir <directory name>          
(delete empty dir.)
# rm -rf <directory name>               (force fully delete dir)
# rm –rf *           (delete all files or directories)

Copy “cp”:

This command is used to copy any file or directory.
# cp <file name> <destination>
eg: 
# cp test.txt /u01
# cp -r <directory name> <destination>
eg:
# cp -r /u01/testdir  /u02
# cp –r * <destination>
# cp <f1> <f2> <f3> <destination>

Cut or Move “mv”:

This command is used to cut/move any file or directory.
# mv <file name> <destination>
# mv –r <file name> <destination>
# mv * <destination>
# mv <f1> <f2> <f3> <destination>

Grep:

This command used to find particular word in a files.
# grep word <f1> <f2>
# grep –i word <f>
          (for ignoring case)

File viewing Commands:

Head

This command is used to view top line of a file.
The default is 10 line
We can view more numbers of line by using option.

# head <file name>
#head -50 <file name>
            (to view 50 lines)

Tail:

This command used to view bottom lines of any file.
The default is 10 line
We can view more numbers of line by using option.

# tail <file name>
# tail -50 <file name>          
(to see last 50 lines)

Listing files/directories “ls”:

This command is used to list files or directories.
# ls
# ls –a
            (lists hidden files)
# ls –ltr              (list files with details)

Pwd:

This command used to see present working directory.
# pwd