Monday, 9 February 2015

Basic Linux commands

Basic commands:
ls -ltr   (list in directory date n time wise)
more filename.txt  (To open the file n linux)
info grep  (information  about grep command)
cd (change directory)
pwd (to know the current directory)

Linux Commands For Developers:
1]  man
Using this command you can get the usage and description of all Linux commands. For example, if you want to know about “ls” command and its options, just execute “man ls” command in the terminal to list its usage and description.
Syntax: man <command name>
man ls

2]  touch, cat and less
Touch command is used to create any type of file in Linux systems with “0” size. When working with Linux you might want to create files in the server. You can make use of touch command to do that.
Syntax: touch <filename>
touch demo.txt

Cat command is used to view the contents of a file. You cannot edit the contents of the file using cat. It just gives a view of the file. cat doesn’t support scrolling using keyboard.
Syntax: cat <filename>
cat demo.txt

Less command also gives the view of a file. less is very fast and you can use the arrow keys to scroll up and down to know the start and end of the file. There is also “more” command, which is used to view the file but it allows only forward scrolling using “enter” key. It doesn’t support backward scrolling.

Syntax: less <filename>
        more <filename>

3]  Grep:

Grep command is used for searching specific string patterns in a file.
Syntax: grep "<search string>" <filename>
        grep "rahul" test.txt

grep --color -r "text" * 
Note: r means recursive. This will find for 'text' in all files in that directory.

4] tar
tar command is used to create and extract archive files. “-cf” and “-xf” flags are used for creating and extracting archives.
Syntax: tar <options> <archive-name> <file/folder name>
Let’s create a tar archive out of test.txt file
tar -cf test.tar test.txt

Let’s extract the test.tar archive to the destination folder “demo” using “-C” flag.
tar -xf test.tar -C /root/demo/

5] find
Find command is used for finding files. You can find the files using its name with “-name” flag.
find -name test.txt
You can also find folder using its name by using”/ -name” flag.
find / -name passwd

6] diff
diff command is used to find the difference between two files. Diff command analyses the files and prints the lines which are different. Let’s say we have two files test and test1. you can find the difference between the two files using the following command.

Syntax:  diff <filename1> <filename2>
               diff  test.txt  test1.txt

7]  Uniq
uniq command is used for filtering out the duplicate line in a file.
Syntax: uniq <filename>
             uniq test.txt

8]  Copy Files or Folders
 8.1) Copy single file demo.txt to destination directory data:
       $ cp demo.txt data

8.2) Copy 2 files demo.txt and demo.txt1 to destination absolute path directory /home/usr/rapid/ :
        $ cp demo.txt demo.txt1 /home/usr/rapid/

8.3) Copy directory src to absolute path directory /home/usr/rapid/ :
       $ cp src /home/usr/rapid/ 

9] Move/Rename
9.1) Move demo.txt demo.txt1 files to /home/usr/rapid/ directory:
     $ mv demo.txt demo.txt1 /home/usr/rapid/

9.2) Rename file main.c to main.bak:
     $ mv main.c main.bak