Wednesday 2 October 2013

Basic Linux Bash command

1. Pathname
To types of pathname are there, Relative pathname and Abosolute pathname

Relative pathname does not begins with /, it is always relative to the present working directory.
Suppose your present working directory is /home/myuser, so if you type
$ pwd ($ is the prompt, you type pwd), it will display /home/myuser, this is the absolute path.
Now if you type cd games, then games here is relative to /home/myuser and your present directory changes to /home/myuser/games. IF you have type cd /home/myuser/games the
same thing happened but in this case you have given absolute path.

2. ls
Lists the contents of the current working directory
(pwd - "Print Working Directory" To see the current working directory)

ls -a
List all files including the files with Dot prefix

ls -l

List name of files with details like permission,date time (created/modified)

Commands can be combined
Say you want to list all files with details you type
ls -a -l or ls -al
This joining can be only done for Short options
for long options like -all you cannot join to options
(example ls -al)

Long and short options can be mixed.
ls --all -l

3.  . and .. (dot and dotdot) directory
    Every directory (even empty) contains two directory in it. One is . (dot) and another is .. (dotdot)
    . (dot) directory is actually the shortcut to the current working directory and .. (dotdot) is a     shortcut to the parent working directory. For example if you type cd .. (you present directory is   /home/myuser/games) you will navigate to /home/myuser.
   For example if you type ls ../ (your pwd is /home/myuser/games) it will list all files and   directories in  myuser folder.

4. rm  - Commands used to delete a file.
   
    Command is - rm filename.extensionname
     rm file1.txt

     rm does not remove directory but you can remove list of files in a directory by this command
     rm -r directoryname.  (-r recursive method)
     rm -r Music.  This command will remove all the files in the directory Music.

     You cannot remove files with rm if you do not have write permission on the files.

     
5. cp - Copies files / directory from source to destination

    cp file1.txt file2.txt
    cp /home/myuser/Music/a.txt /home/myuser/a.txt

    cp can also copy directories and sub directories in a recirsive manner.

    cp -r dir1 dir2.  This copies all contents of dir1 including files and subdirectories to dir2.

6. mv - Moves files / directories from source to destination
    mv oldfilename.txt newfilename.txt (this is similar to renaming a file).
    mv /home/myuser/Music/olddir /home/myuser/newdir

7. cat,tac and less command
     cat file1.txt prints the content of the file
     cat file1.txt file2.txt file3.txt displays content of all the three files concatenated.
   
     tac file1.txt prints the content of the file in reverse order.
   
     less file1.txt displays contents of the file pagewise.

8.  file command Gives information about the file type
      file file1.bmp  Bitmap image

9.  find command searches the entire directories/subdirectories starting from the source           directory specified for a certain file/ directory specified. Wildcard can be used.
    
      find / -name "*.txt" searches all files with extension txt starting from /

10. locate command does the same as find but build an index before searching which makes the           search more faster.
      locate abc.txt
      locate command cannot be used to search a newly created file until it is indexed, in that case          you have to use the find command.

11. chmod
      syntax is - chmod permissions filename
      chmod u+x file1.sh. Set/Adds executable (x) permission to current user (u) (u+x).
      chmod u-x file1.sh.  Unset/removes executable (x) permission to current user (u) (u-x).
      chmod go-rwx file1.sh Unset/removes read,write,executing permission from the group (g) the
      user belongs to and all others (o). So with this permission only user has the rights. Rights are
      revoked from everybody else.

12. gzip and gunzip
     
      gzip  compreses a file 
      gzip file1.txt  this command compress the file file1.txt to file1.txt.gz
     
      gunzip file1.txt.gz this command uncompress the file file1.txt.gz to file1.txt
     
      gzip works best for text files.
      gzip is not an archiver, it's a compressor. Archiver archives multiple files to a single file like winrar or winzip,
      but gzip compresses only a single file.
      To archive files you need to use tar command.

13. tar archive a directory containing multiple files
     create
     tar -cvf Musicbkup.tar /home/myuser/Music, c is to create the archive, v is for verbose mode       
     (prints all the files names to be archived), f is for target tar filename (Musicbkup.tar),
     if you mention f option you need to give the output archivename.
   
     tar -czvf Musicbkup.tar.gz /home/myuser/Music, z is added to the options -cvf to compress the archived file.
       
     extract
     tar -xvf Musicbkup.tar, extract all files from the archive.

     tar -xzvf Musicbkup.tar.gz, extract and uncompress all files from the compressed .gz.

     tar -xzvf Musicbkup.tar.gz actually perform two commands into one. First its gunzip
     Musicbkup.tar.gz into Musicbkup.tar and then extract all files from Musicbkup.tar.
   
14. head and tail commands
    head -n 10 file1.txt prints the first 10 lines of the file file1.txt, option n being the number of lines.
    head does the same thing as less commands but less commands prints page by pagee but head
    commands prints the desired number of lines.
   
    tail -n 10 file1.txt prints the last 10 lines of the file file1.txt, option n being the number of lines.
   
15. du command prints the amount of space a specified diretory use in the disk.
    du -sh Desktop if s option is not given then du command prints size information of all the subdirectories under
    specified directories, s option summarizes all the information and only shows the space occupied by it. h option
    gives the size information in MB or GB instead of bytes, if you do not provide h option result will come into byte units.

16. grep command searches for pattern in the content of a file specified.
    grep sun file1.txt searches for pattern sun output can be like this sun,sunny,sundry,prasun.
   
17. history command list all the previous commands you have typed. Up and down arrow browse through history in lifo method.
    when you type any command the commands stored into the memory but when you end terminal session all commands are appended
    into a file automatically. Now when you are into one session and you want to see the command history on another terminal
    session, it will not show because it is not written onto disk yet. You can type history -a which flushes the memory and
    append current history in the file and you can see those commands when you type history from other terminal session.
   
18. export command to set environment variable on your current session
    export VAR_NAME=value general convension is to set the variable in upper case.
    export ORACLE_HOME=u01/oracle/app/db_1
    unset VAR_NAME - command to unset the environment variable is
    unset ORACLE_HOME
   
19. vi / vim editor vi or vim editor is a text editor which can be used to create file, edit contents of the file.
    command is vi filename or vim filename
    some commands in insert mode
    'i' turn insert mode on by moving the cursor to the next cursor location
    'a' turn insert mode keeping the cursor where it is.
    'i' to insert text or to say to turn the editor to insert mode
    'esc' to turn off the insert mode and enter to command mode
   
    this commands works when not in command mode
    'h' move the cursor to the left.
    'k' move the cursor up.
    'l' move the cursor to the right.
    'j' move the cursor down.
    'b' move one word left.
    'w' move one word right.
    'x' delete one character right (del).
    'X' delete one character left (backspace).
    'V' Highlight / Salect a complete line.
    'j' to hightlight the line following the selected line downwards.
    'y' (yank) to copy a selected line / paragraph.
    'p' paste a copied line / paragraph on top of the copied section.
    'P' paste a copied line / paragraph below the copied section.
    'd' delete a selected line / paragraph.
    :w writes changes to disk / save the file.
    :q quits vim.
    :wq writes changes to disk and then quit vim.
    :q! discard changes to file and then quit vim.
   
19. env - a command to see all the environment variable set in the current session.   

20. sort  this command sorts lines in a file in alphabetical order.
    sort file1.txt

No comments: