Tuesday, 28 March 2017

Useful Linux Commands - Part 01

1. Command to grep a process and kill

We can use xargs as following. Consider we have a process sleep.sh
 ps -ef  | grep sleep.sh | awk  '{print$2}' | xargs kill -9
 ps -ef  | grep sleep.sh | grep -v 'grep' | awk  '{print$2}' | xargs kill -9


2. Command to list top 10 large sized file in /var directory

sudo du -a /var | sort -n -r | head -n 10

3. Consider demo.csv file with following content.
mukesh@ubuntu:~/Desktop$ cat demo.csv
sno,Item,cost
1,Tea,20
2,Burger,25
3,Ice cream,30
4,Candy,15

Problem 01: Write a command to iterate all the items available in demo.csv file.

cat demo.csv | awk -F ',' '{print $2}' | grep -nv [0-9]
1:Item
2:Tea
3:Burger
4:Ice cream
5:Candy

Problem 02 : Write command to sum up the the cost of items in demo.csv

$ awk -F ',' '{s+=$3}END{print s}' demo.csv
awk -F ',' '{total = total + $3}END{print "Total Amount collected = "total}' demo.csv
Total Amount collected = 90

4. Write a for loop from 1 to 5 in single command

Syntax : for i in {1..5}; do COMMAND-HERE; done
$ for i in {1..5}; do echo $1; done
1
2
3
4
5

5. Write command to all the files in /home/mukesh/Desktop directory.

$ for i in /home/mukesh/Desktop/* ; do echo "$i"; done

6. Write command to grep 2 more lines after the pattern match

$ grep -A2 Tea demo.csv
1,Tea,20
2,Burger,25
3,Ice cream,30

7. Write command to grep 2 more lines before the pattern match

$ grep -B2 Ice demo.csv
1,Tea,20
2,Burger,25
3,Ice cream,30

8. Command to print date 1 day before and and after in YYYY-MM-DD format

refdate=`date +"%Y-%m-%d"`
1 day before date:
refdate=`date --date="1 day before" +%Y-%m-%d`

$ date --date="1 days ago"
$ date --date="1 day ago"
$ date --date="yesterday"
$ date --date="-1 day"

$ date --date="next day"

9. Command to sed the multiple patterns

 sed 's/ab/~~/g; s/bc/ab/g; s/~~/bc/g' file.txt > file_tmp.txt

10. list all files which were created in last 1 Hrs

find <search_dirname> -cmin -60 # creation time
find <search_dirname> -mmin -60 # modification time
find <search_dirname> -amin -60 # access time

11. Command to find all files modified,accessed and permission changed on the 7th of June, 2007:


$ find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08
$ find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30
$ find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30

12. find the files which were created on more/less than 30 days and created exact on 30th day ago

we can use -ctime option with find command as following
    More than 30 days ago: -ctime +30
    Less than 30 days ago: -ctime -30
    Exactly 30 days ago: -ctime 30

13. Command to grep multiple pattern

grep  -e '2015-12-02' -e '2015-12-03' dateFile.txt
grep  '2015-12-02\|2015-12-03' dateFile.txt

14. Command to display cpu info of your linux/Unix system

 Following command will list number of processor on the server and its details.
 cat /proc/cpuinfo

15. How to list top memory consuming process.

 Top + Press Shift  m

16. Command to check linux version

Note : CentOS is based on Redhat Enterprise Linux, while Ubuntu/Ubuntu Server has its roots in Debian
# For ubuntu
 cat /etc/os-release

    root@ubuntu:/etc/ansible# cat /etc/os-release
    NAME="Ubuntu"
    VERSION="12.04.5 LTS, Precise Pangolin"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu precise (12.04.5 LTS)"
    VERSION_ID="12.04"

# For centos
 cat /etc/system-release
 cat /etc/redhat-release
  CentOS release 6.5 (Final)




Related Posts Plugin for WordPress, Blogger...