Unix

Top 25 Unix Commands With Examples

 

Hello Friends,

In this tutorial,you will learn top Unix commands which are handy in day to day work,specially from Java developer point of view.

 

1.  How to create a new file in Unix

touch <fileName>

Example : touch abc.txt

2. How to see content of file in Unix(without able to edit)

cat <FileName>

    Example : cat abc.txt

3. How to open file in editor mode

vi <File Name>

Example : vi abc.txt

4. How to edit File and add content in File in Unix

a) Open file with following command

vi abc.txt

b)   Press I ,it will bring file in edit mode.

c)   Type whatever you enter in file.

d)   Type :wq! and enter.This will save content you entered in file.

5. How to find a File/Directory with name starting with particular character in Unix

    ls -ltr | grep <character>*

   Example : Find file/directory starting with name “ab”

   ls -lrt | grep ab*   

6. How to give/change permissions of File or Directory.

File permissions in numeric format and their meaning :

0 – no permissions

1 – execute only

2 – write only

3 – write and execute

4 – read only

5 – read and execute

6 – read and write

7 – read, write and execute

  By default,when we create a File in Unix ,it is created with permission 666(read/write).

  By default,when we create a Directory in Unix,it is created with permission  777(read/write/execute).

Along with numeric notation,Unix permissions can also be represented by following characters:

Reference       Class      Description

u                     user        the owner of the file

g                     group     users who are members of the file’s group

o                     others     users who are not the owner of the file or members of the group

a                     all           all three of the above, is the same as ugo

r                     read         read a file or list a directory’s contents

w                   write        write to a file or directory

x                    execute    execute a file

6.1. How to make File read only in Unix

chmod  <permission> <fileName>

Example : chmod 400 abc.txt

6.2. How to give File Read/Write Permissions in Unix

    chmod <Permission> <FileName>

    Example : chmod 777 abc.txt

7. How to find list of all the links in a directory in Unix

    ls -lrt | grep “^l”

   ^ – This signifies start of each line

   l  – Each symbolic link has “1” in the beginning of line like below :


   lrwxrwxrwx  1 gg99a weblog 3 Dec  9 22:20 latest -> 1.3


  So this command prints all lines from result of ls -lrt,which starts with “l” and all symolic link’s lines start with “l”,hence the result.


8. How to see content of current directory

   ls

  This will not give details about Files or directories.

9. How to see content of current directory with details such as 

   – the exact size of the file

   – who owns the file

   – who has the right to look at it

   – when it was last modified.

 ls -lrt



10. How to see all files  including the ones whose filenames begin in a dot

ls -a 

This however does not give details as mentioned in point 9.

11. How to see List of all background process

    Jobs

12. How to bring a background process to foreground

   fg

13. How to kill a process

  kill <ProcessId>

14. How to see current date in Unix

   date

15. How to see History of commands executed 

   history 

   Using only “history” will give you all the commands executed in current session.

   If you want to see specific type of command,use grep along with history as below :

  For example ,if you have executed command to create directory multiple times,you can  execute history command as below.It will list mkdir command all the times you have executed previously in current session.

history | grep  mk

16. How to clear the console in Unix

    clear

17. How to check ,how much space is left on the current drive in Unix
    df -h

18. How to see which process is taking how much time in Unix
    top

    Press shift + O to get all options by which you can sort.For example ,to sort by PID,press ‘a’ and then press any key.It will sort all the processes by PID.

19. How to copy File from one host to another in Unix

     When you are sending files from current machine to remote machine
    scp <current machine path to file,which we want to send to remote machine> user@remote machine:/remote path

  Example : scp /var/log/was/abc.txt GA99@test.com:/var/log/remoteDirectory

When you want to retrieve file(s) from remote machine

    scp user@remote machine:/remote path to file  <current machine path where we want to download the file>

    Example : scp user@remote machine:/var/log/remoteDirectory/abc.txt  /var/log/was

20. How to find ,how many times a word is there in File
    grep -c  <word to find> <FileName>

    Example : grep -c Hi abc.txt

    Above command will give count of word Hi in File abc.txt.

21. How to execute previously executed command i nUnix
      !<Previous Command>

     This is really handy when your command includes lots of parameters and options.You need not type all those parameters and options again with this shorthand.

Example : Say I executed following command:

 ls -lrt | grep abc

Now I want to execute this command again,I just have to type following to execute above command again :

!ls

22. How to find all the files with certain content in Unix
   find . –name <type of files to be searched> | xargs grep <content to be matched>

   Example : 
   find . -name “*.txt” | xargs grep “Hi”

23. How to find file(s) which have been modified since last day,today or more than 1 day

  23.1)Find all files in current directory and sub directories which has been modified exactly 1 day back.

  find . -mtime 1
23.2) Find all files in current directory and sub directories which has been modified more than 1 day before
 find . -mtime +1
23.3) Find all files in current directory and sub directories which has been modified less than 1 day before.

 find . -mtime -1

24. How to create Soft link in Unix
   ln -s <SoftLink name> <Directory which soft link will point to>

  Example : ln -s latest 1.0

  So here you are pointing latest softlink to directory 1.0

24. How to update Soft link in Unix
  ln -nsf <New directory> <Soft link>

  Example : Say you want to point latest now to new directory 1.1,then you will execute following command :

 ln -nsf 1.1 latest

25.How to remove Soft link in Unix
 rm <Softlink name>

That’s it for today.

 

Published on System Code Geeks with permission by Gaurav Bhardwaj, partner at our SCG program. See the original article here: Top 25 Unix Commands With Examples

Opinions expressed by System Code Geeks contributors are their own.

Gaurav Bhardwaj

Gaurav has done Masters in Computer applications(MCA) and is working in Software development field for more than 7 years in Java/J2EE technologies. He is currently working with one of top MNC. He has worked on various frameworks like Struts, Spring, JSF, Velocity, iBatis, MyBatris, Hibernate, Junit, Dozzer. He likes to explore new technologies and share his thoughts by writing technical blog. He is founder of JavaSolutionsGuilde.blogspot.com.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button