Monday, August 19

Unix basic commands with examples

Basic Commands :

   
The Commands which are basically use to operate an Operating System(O/S) is called Basic Commands.  

1) pwd :  present working directory ---> used to know the path of present user.

2) ls : list ------>It gives the list of files & Directories present in the path


3) ls -l or ll --------> Listing of all files along with attributes


4) ls -a ------> Lists all hidden files & directories


5) ls -r -------> lists all files & directories in reverse mode


6) ls -il---------> lists all files & directories along with inode numbers


7) ls -ld < directory name> ------> to view attributes of a particular directory


8) ls -R <directory.name> -------> to view tree structure of a directory


9) ls -a* ----------> to view all files & directories starts with a


10) man < command> ---------> to view entire options of a command


To create a file we have 3 methods :

1) touch

2) cat

3) vi(vim)

1) touch : With the help of touch command we can create an empty file.
    
      syntax: touch < filename>
        
             ex: touch f1

   with this command we can create n number of files at a time.
       
           syntax: touch  <file1> <file2> <file3>
           ex: touch mahesh raju naveen

2)cat:  By using cat we can create data-files
   
     syntax: cat > <filename>
         ex: cat  >mahesh
             
              (edit data)
             ( ctrl+d)(to save)

 To view content of a file

    
           cat < filename>

To add data in existing file


           cat >> <existing filename>
       
            (enter-data)
            (ctrl+d)

we cannot edit the written text through cat command. we can only add data to modify existing data we have to use editors. we have number of editors in linux

      
             ex: gedit ,nano, kedit, kate, emacs ,vim ,etc...
  
   Note : but we always use the best editor "vim" editor .

we can transfer an output as input for another file
        
           ex: to view output of file1 file2 we use 

                    cat file1 file2
 In the same way, we can transfer output of that command as input for another file 
    
           ex: cat file1 file2 > file3
here we are giving file1 file2's data as input for file3 , by this data of file1&file2 will copied into file3.

How To Practice In  Linux-System(command mode) :



1.ls -l  for listing the files as well as directories those are kept in

the particular working directory

syntax
[root@bsrtech ~]#ls -l

2.ls -la same as 'ls -l'but by this command we can also see the hiden

files.

syntax

[root@bsrtech ~]#ls -la

3.ls -li same as 'ls -la' but it will also shows us the inode number of

each and every file

syntax

[root@bsrtech ~]#ls -li

4.ls by this command we can see only file name nothing else


syntax

[root@bsrtech ~]#ls

5.clear it will clear the screen(short cut ctl+l)


syntax

[root@bsrtech ~]#clear

6.exit to end a current session as well current terminal logging


syntax

[root@bsrtech ~]#exit

7.touch to create a new empty file


syntax

[root@bsrtech ~]#touch

8.cd to change the working/present directory


 syntax

[root@bsrtech ~]#cd /home/mango
where '/home/mango' is the desired directory to be change from '/root'

9.cat to view the contents of a file and it is also used for creating a new file with some contents


 syntax

[root@bsrtech ~]#cat <file name> to view file contents
[root@bsrtech ~]#cat > newfilename enter,then you can write something in the file and then to save the file contents press clt+d then enter

10.mkdir to make a new directory


syntax

[root@bsrtech ~]#mkdir newdirname
you can also create a directory at your desired path without changing your present working directory

syntax

[root@bsrtech ~]#mkdir /home/mango/newdirname

11.rm to remove a empty file


syntax

[root@bsrtech ~]#rm filename

12.rmdir to remove a empty directory


syntax

[root@bsrtech ~]#rmdir directoryname

13.rm [-i/-r/-f] to remove a directory with its subdirectories as well as its files that is to remove a directory which already contains some files in it


syntax

[root@bsrtech ~]#rm -i directory/filename
               -i stands for interactively
              -r stands for recursively
              -f stands for forcefully

14.cp to copy something in a destination file or directory


syntax

[root@bsrtech ~]#cp sourcepath destinationpath

example: [root@bsrtech ~]#cp /home/mango/webmin.rpm /root/abcd

in this example the webmin.rpm file will be copied in /root/abcd directory

15.mv to move one file or directory from one place to another place, it is also used for renaming a directory or file.


syntax

[root@bsrtech ~]#mv source destination
[root@bsrtech ~]#mv oldfilename newfilename [to change the file name]

16.man to view the mannual page of commands for syntax


syntax

[root@bsrtech ~]#man commandname

17.info to view the information about any command


syntax

[root@bsrtech ~]#mkdir info

18.--help to view the help doccuments of a command


syntax

[root@bsrtech ~]#commandname --help

19.dir to view the subdirectories and filesn under the directory


syntax

[root@bsrtech ~]#dir

20.su - to become a super user


syntax

[mahesh@bsrtech ~]$su -

output wil be
[root@bsrtech ~]#

21.who by this command you can see the user name and their ip addresses

who have loged in on your server

syntax

[root@bsrtech ~]#who

22.whoami this command shows your current logged in terminal user name


syntax

[root@bsrtech ~]#whoami

23.who am i this command shows you the logged in terminal number and user name and more detailed information


syntax

[root@bsrtech ~]#who am i

24.pwd to view the present working directory


syntax

[root@nettech root]#pwd

25.rpm -ivh to intall a rpm package


syntax

[root@bsrtech ~]#rpm -ivh packagename.rpm
rpm stands for 'redhat package manager'

      -i stands for install

     -v stands for verbose mode
     -h stands for with hash sign(#)

26.rpm -q to querry about any rpm package

syntax

[root@bsrtech ~]#rpm -q packagename

27.rpm -e to uninstall a rpm package


synatx

[root@bsrtech ~]#rpm -e package

28.find / -name to find any file or directory in linux file system


syntax

[root@bsrtech ~]#find / -name filename

29.su username to switch from one user to another users home directory


syntax

[root@bsrtech ~]#su  mahesh

output will be
[mahesh@bsrtech ~]$cd
[mahesh@bsrtech ~]$

30.su - username to switch from one user to another user users home directory directly


syntax

[root@bsrtech ~]#su -mahesh
[mahesh@bsrtech ~]$

 31.useradd to create a new user

synatx

[root@bsrtech ~]#useradd username

32.passwd to give a password of a user


syntax

[root@bsrtech ~]#passwd mahesh
 output will be
give a password for user mahesh:(here you have to type a password for mahesh user)
confirm password:(again type the same password)

33.userdel to remove a user from linux


syntax

[root@bsrtech ~]#userdel mahesh

34. groupadd to add a new group

syntax

[root@bsrtech ~]#groupadd <groupname>

35.gruopdel to delete a group


syntax

[root@bsrtech ~]#groupdel  <groupname>

36.chown to change the ownership of a file or directory


syntax

[root@bsrtech ~]#chown <ownername>  <filename>

example:
[root@bsrtech ~]#ls -l
output
-rw-------          1        root       root          1433  Sep 10 16:33   anaconda-ks.cfg
(permission) (links)  (own)  (group own) (size)      (date)        (File-name)

[root@bsrtech ~]#chown mahesh  /file1
in this example /file1  file owner will be change to mahesh user
effect
[root@bsrtech /]##ls -l
-rw-r--r-- 2 mahesh root 4096 Sep 05 12:05  file1

37.chgrp to change the group ownership of a file or directory


syntax

[root@bsrtech ~]#chgrp <owner-of-group> <filename>
example

[root@bsrtech /]#ls -l
-rw-r--r-- 2 mahesh root 4096 Sep 05 12:05  file1

[root@bsrtech ~]#chgrp mahesh /file1
effect

[root@nettech /]#ls -l
-rw-r--r-- 2 mahesh root 4096 Sep 05 12:05  file1

38. chmod to change the permission of a file or directory


   -rw-r--r--       2      mahesh    root          4096   Sep 05 12:05    file1
(permission) (links)  (own)  (group own) (size)       (date)        (File-name)

 - stands for file

  d stands for directory

 r stands for read only permission  READ=4

 w stands for write only permission WRITE=2

 x stands for execute only permission EXECUTE=1

drwxrw-rw- 

    FIRST OCTET FOR DENOTING THE DIRECTORY OR FILE OR LINK FILE ETC.

    SECOND THREE OCTET FOR USER OR OWNER PERMISSION (rwx OR 7 IN OCTAL VALUE)


    THIRD THREE OCTET FOR GROUP PERMISSION (rw- OR 6 IN OCTAL VALUE)


    FORTH THREE OCTET FOR OTHERS PERMISSION (rw- OR 6 IN OCTAL VALUE)


SYNTAX

[root@bsrtech /]#chmod value file or directory-name

example

[root@bsrtech /]#ls -l
-rw-r--r-- 2 mahesh root 4096 Sep 05 12:05  file1

[root@bsrtech /]#chmod 666 /file1

[root@bsrtech /]#ls -l
-rw-rw-rw- 2 mahesh root 4096 Sep 05 12:05  file1

40.usermod to modify the user profile


synatx

[root@bsrtech ~]#usermod <parameter> <groupname> <username>




File Prompting Commands :


head    :   used to view first 10 lines of a file

   
    syntax :   head <filename>
    ex: head /etc/passwd

To view 4 lines of a file
  
    head -n <no of lines> <filename>
    ex: head -4 /etc/passwd

Tail : is used to view last ten lines of a file

    syntax: tail <filename>
    ex: tail /etc/passwd

To view last 10 lines of a file
  
    tail -10 /etc/passwd

more : used to see the content pagewise but we cannot scroll up

    syntax: more < file name>
    ex: more /etc/passwd

less: used to see the content pagewise we can scroll up&down

  syntax: less <filename>
  ex: less /etc/passwd

date: used to view current date&time

to change date

date  <mm-dd-hr-min-year>

ex: date 120111452013
  it means 12th month 01 date 11:45 11 hours 45 minutes 2013 year
or
  date -s <content>
  date -s "wed june 20 20:46:51 IST 2013"

cal : cal is used to view present months calender

cal to view a particular month,particular years calender

 cal <Month> <year>

cal 5 1986 -------------> to view may 1986 calender
cal 2050 ----------------> to view entire 2050 year calender 
    |                 --------> this symbol is called as pipe it is used to link conmmands

  ex: ls | grep pot

grep : is used to skip something specially from the output
here in example from output of ls we are grepping pot

ll | grep "^d" ---------> here from the out put of ll we are greping only directories
mkdir <directory name> -----> to create a directory
ex: mkdir mahesh

To create multiple directories :

mkdir <dir1> <dir2> <dir3>
ex: mkdir d1  d2  d3

To create nested directory

mkdir -p <dir1/dir2/dir3>
mkdir -p world/asia/india/ap/vskp/mahesh

To change the directory

 cd < path of directory>

To change directory one level back
 cd ..

To change directory two levels back
 cd ../..

Removing a file or directory
to remove a file
rm <filename>
to remove an empty directory 
 rmdir <dir-name>

To remove directory recursively & forcefully
rm -rf -> to delete directory with out asking yes or no

copying a file

cp <sorce> <destination> -----> to copy a file
ex: cp file1 /etc/
cp -rf <source > <destination> ---> to copy entire directory along with subdirectories & files
ex: cp -rf mahesh /opt/
cp -a <source> <destination> -----> to copy a file or directory along with permissions
ex: cp -a Server /var/ftp/pub

To move a file or directory

mv < source> <destination>
mv mahesh /opt/users/

To rename a file or directory

mv <oldname> <newname>

To create chain commands

In linux we can execute "n" number of commands at a single command line
syntax: command1;command2;command3;

ex: date;cal2013;date 120111452013;mkdir mahesh;cd mahesh;cd

1 comment:

:: Linux - Legends ::