grep, find, awk and sed commands

grep examples:

  • Miscellaneous grep usages:
    • grep -ri -e “Error|Exception” ./SystemOut*
    • grep “pattern” ssl_access_log* | grep ‘*/Apr/2015’ | less
    • grep -e user1 -e user2 /etc/passwd /etc/shadow
  • Search the given string and count results
    • grep “pattern” ssl_access_log* | wc -l
  • Search the string in the given directory:
    • grep -Ri “open for e-business” /logs/
    • grep -C 2 “open for e-business” /logs/SystemOut*
  • Search a string in current directory and sort the output
    • grep “open for e-business”   $(ls -tr *)
  • Exclude one or more string
    • tail -fn1000 ./ssl_access_logs|grep -vE ‘200|404
  • List only files:
    • ls -al /tmp | grep ‘^[-]’      (To count the files append | wc -l )
    • ls -F /tmp | grep -v | wc -l
    • ls -al /tmp | grep ‘^[-l]’     (Lists regular files and symbolic links)
  • List only directories:
    • ls  -al /tmp | grep ‘^[d]’   (To count the folders append | wc -l )
  • :

find examples:

  • find / -type f -iname *.txt
  • find / -type d iname mydir
  • find / -perm /a=x
  • find / -type f -perm 0777 -print -exec chmod 644 {} \;
  • find / -type f -name “*.txt” -exec rm -f {} \;
  • find / -type f -empty                   (Find empty files)
  • find /tmp -type f -name “.*”     (Find hidden files)
  • find /home -user user1             (Find files of a user1)
  • find /home -group grp1            (Find files of a group)
  • find / -mtime 7                           (Find files modified in last 7 days)
  • find / -atime 7                            (Find all files accessed 7 days back)
  • find / -mtime +5 –mtime -10  (Find files modified between 5 and 10 days)
  •  find / -size +5M -size -10M  (Find files greater than 5 & less than 10 MB)
  • find / -type f -name *.txt -size +10M -exec rm {} \;

awk examples:

  • awk ‘{ print $0 }’ /etc/passwd             (Prints entire file)
  • awk -F: ‘{ print $1 }’ /etc/passwd        (Prints first field of every line)
  • awk -F: ‘{ print $1 $6 }’ /etc/passwd  (Prints first and sixth field of every line)

 sed examples:

  • Replace first occurrence of str1 with str2:
    • sed s/str1/str2/    file.txt
  • Replace all occurrences of str1 with str2 and save file:
    • sed -i s/str1/str2/g file.txt
  • Replace all occurrences of str1 with str2 from lines 1 to 3
    • sed 1,3s/str1/str2/g file.txt
  • Replace 01 with JAN, 02 with FEB and 03 with MAR
    • sed -e ‘s/01/JAN/’ -e ‘s/02/FEB/’ -e ‘s/03/MAR/’ file.txt
Advertisements

About Ishtiaque

I am IBM Certified Infrastructure Systems Architect, TOGAF 9 Certified, ITIL V3 Foundation certified and Oracle Certified Export in Java Web Component Developer with over 10 years of support and development experience in IBM middleware software and Java. Additionally, have a sound grip in databases, Linux and OpenStack administration. I hold the following certifications: IBM Certified Infrastructure Systems Architect TOGAF 9 Certified Oracle Certified Expert, Java EE6 Web Component Developer Oracle Certified Professional – Java 6 Programmer ITIL v3 Foundation Certified IBM Certified Solution Architect – Cloud Computing Infrastructure V1 IBM Certified System Administrator – WebSphere Portal V8, V7, V6.1, V6 IBM Certified System Administrator – WebSphere Application Server V7, V6.1 IBM Certified System Administrator – AIX V7 IBM Certified System Administrator – WebSphere MQ V7 IBM Certified Deployment Professional – Business Process Manager Advanced V7.5 IBM Certified Solution Advisor – Cloud Computing Architecture V3 IBM Certified Solution Developer – WebSphere Portal V5.1
This entry was posted in LFCS, Linux. Bookmark the permalink.