Thursday, 7 May 2015

Basic Linux / Unix Commands with Examles




1)
tar command examples

Create a new tar archive.

              $ tar cvf archive_name.tar dirname/


Extract from an existing tar archive.

            $ tar xvf archive_name.tar


View an existing tar archive.

         $ tar tvf archive_name.tar






2)
grep command examples

search a string pattern in the given file.





search a string pattern in the given file (case insensitive).


print the matched line and along with the 3 line after that word



Search for a given string in all files recursively.



3)
find command examples

Limit depth of directory traversal




The following command searches for files by their name in that specified directory.


We can also find file or directory with wild charecter.



4)
vim command examples

Go to the 3rd line of file

                   $ vim +3 filename.txt
Go to the first match of the specified

                  $ vim +/search-term filename.txt

Open the file in read only mode.

                  $ vim -R  file_name


5)
diff command examples

diff command check the difference between the two file that we given and also it will Ignore white space while comparing.

           

6)
sort command examples



Sort the file content in ascending order










Sort the file content in descending order






7)
ls command examples

Display filesize in human readable format (e.g. KB, MB etc.,)

                      $ ls -lh

      anusree@anusree-Inspiron-5547:~/anusree$ ls -lh
      total 96K
      -rw-rw-r-- 1 anusree anusree  117 Apr 14 15:26 anureesaju.tar.gz
      drwxrwxr-x 4 anusree anusree 4.0K May  7 08:23 anusaju
      -rw-rw-r-- 1 anusree anusree   22 Mar 16 12:11 anu.txt
      -rw-rw-r-- 1 anusree anusree    0 Mar 16 12:11 anu.txt~
      -rw-rw-r-- 1 anusree anusree  171 Feb 23 01:30 django_notes
      drwxrwxr-x 4 anusree anusree 4.0K Apr  6 22:48 extra


Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr

                  $ ls -ltr

      anusree@anusree-Inspiron-5547:~/anusree$ ls -ltr
      total 96
      -rw------- 1 anusree anusree   250 Feb 15 12:22 pay_hr_rt.py
      -rw------- 1 anusree anusree   319 Feb 15 12:32 pay_tryex.py
      -rw------- 1 anusree anusree   133 Feb 20 13:11 fibb.py
      -rw-r--r-- 1 anusree anusree    20 Feb 21 10:02 hello.py
      -rw-rw-r-- 1 anusree anusree   321 Feb 21 20:47 looping_tec.py
      drwxrwxr-x 6 anusree anusree  4096 Feb 23 00:40 venv
      -rw-rw-r-- 1 anusree anusree     0 Feb 23 01:24 Untitled Document~




8)
pwd command

    pwd is Print working directory.



9)
cd command examples

     Use “cd -” to toggle between the last two directories


10)
shutdown command examples

Shutdown the system and turn the power off immediately It need the administration power.
         
       $ sudo shutdown -h now

Shutdown the system after 10 minutes.

       $ sudo shutdown -h +10

Reboot the system using shutdown command.

      $ shutdown -r now

11)
ps command example

ps command is used to display information about the processes that are running in the system..

To view current running processes.

            $ ps -ef

12)
free command example
This command is used to display the free, used, swap memory available in the system.



  
       -b, --bytes
              Display the amount of memory in bytes.

       -k, --kilo
              Display the amount of memory in kilobytes.  This is the default.

       -m, --mega
              Display the amount of memory in megabytes.

       -g, --giga
              Display the amount of memory in gigabytes.

       --tera Display the amount of memory in terabytes.

       -h, --human
              Show all output fields automatically scaled to shortest three digit unit and display the units of print out.  Following units are used.

                B = bytes
                K = kilos
                M = megas
                G = gigas
                T = teras


      
13)
top command example

The  top  program  provides  a dynamic real-time view of a running system.


$ top -h  ----->   Show library version and the usage prompt, then quit.



14)
df command examples

Displays the file system disk space usage. By default df -k displays output in bytes.



 -i, --inodes
              list inode information instead of block usage





-h, --human-readable
              print sizes in human readable format (e.g., 1K 234M 2G)





15)
kill command example:

Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.


     syntax:
          $ kill process_id


16)
rm command examples:

   remove files or directories

Get confirmation before removing the file.
    $ rm -i filename



 It is very useful while giving shell metacharacters in the file name argument.
Print the filename and get confirmation before removing the file.

        $ rm -i file*

Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.

        $ rm -r example

17
cp command examples

        copy files and directories

Copy file1 to file2 preserving the mode, ownership and timestamp.

       $ cp -p file1 file2

Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.

       $ cp -i file1 file2


18)
mv command examples

Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.

      $ mv -i file1 file2

Note: mv -f is just the opposite, which will overwrite file2 without prompting.

mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.

      $ mv -v file1 file2


19)
cat command examples

You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.
         $ cat file1 file2

While displaying the file, following cat -n command will prepend the line number to each line of the output.



20)
chmod command examples

chmod command is used to change the permissions for a file or directory.

Give full access to user and group (i.e read, write and execute ) on a more than one file.

      $ chmod ug+rwx file.txt file2.txt



Revoke all access for the group (i.e read, write and execute ) on a specific file.

       $ chmod g-rwx file.txt

Apply the file permissions recursively to all the files in the sub-directories.

        $ chmod -R ug+rwx file.txt



21)
passwd command examples

Change your password from command line using passwd. This will prompt for the old password followed by the new password. If you only type the command passwd without any options the the password of root user will changed

                 $ passwd

To change the password for a particular user
           
             $ passwd USERNAME




Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.

              $ passwd -d USERNAME



22)
uname command examples

Uname command displays important information about the system such as — Kernel name, Host name, Kernel release number,
Processor type, etc.,

Sample uname output from a Ubuntu laptop is shown below.
 




If you want to know all the information about system the type the option -a




23)
whereis command examples

When you want to find out where a specific Unix command exists

for example, where does ls command and cat command exists?



24)
whatis command examples

Whatis command displays a single line description about a command.



25)
man command 

Display the man page of specific command


Following 8 sections are available in the man page.

  1. General commands
  2. System calls
  3. C library functions
  4. Special files (usually devices, those found in /dev) and drivers
  5. File formats and conventions
  6. Games and screensavers
  7. Miscellaneous
  8. System administration commands and daemons


26)
tail command examples

Print the last 10 lines of a file by default.

     $ tail filename.txt

Print N number of lines from the file named filename.txt

      $ tail -n N filename.txt







27)
su command examples

Switch to a different user account using su command. Super user can switch to any other user without entering their password.

           $ su - USERNAME

28)
mysql command example

mysql is a simple SQL shell with input line editing capabilities. It supports interactive and noninteractive use. When used interactively


To connect a local msql database
       $ mysql -u root -p




29)
date command example

print or set the system date and time


If you want to know a particular day in the paricular date







30)
wget command examples

The quick and effective method to download software, music, video from internet is using wget command


     $ wget link-for-download-the-file