Saturday, December 22, 2018

Unix/Linux Commands Notes

List of Command with options

1.  grep : Find lines that match a regular expression

Syntex:
grep -A<N> "string" <FILENAME> => Print N lines after match
grep -B<N> "string" <FILENAME> => Print N lines before match
grep -C<N> "string" <FILENAME> => Print N lines around match
grep -i "string" <FILENAME> => Print irrespect of case sensitive word search
grep -w "string" <FILENAME> => Print full word search
grep -c "string" <FILENAME> => Print counting the matches
grep -l "string" <FILENAME> => Print only the filenames which matches the pattern
grep -e PatternOne -e PatternTwo -e PatternThree file =>Searching multiple patterns at once

2.  find : It will display the pathnames of all files in the current director and all subdirectories.
find => [find . or find -print or find . -print]
   find / -name abc =>It will search abc file in the entire system.
   find /home/abc -name '*.txt' => Find all .txt files in directories abc.
   find /home/abc -iname abc.txt => Find files using name and ignoring case.
   find . -type f -perm 0777 -print => Find files based on their permissions.
   find . -type f ! -perm 0777 -print => Find files based on their permissions.
   find / -perm /u=s => Find all SUID set files
   find / -perm /a=x => Find all executable files
   find / -perm /u=r => Find all readonly files
   find . -type f -name "file1.txt" -exec rm -f {} \; => Find and remove file1.txt
   find . -type f -name "file1.txt" -print0 |xargs -0 rm -f => find and remove file1.txt
   find . -type f -empty -exec rm -f {} \; => Find all empty files and remove.
   find / -mtime 10 => Find all the files that were modified 10 days modified back.
   find / -atime 10 => Find all the file that were accessed 10 days back.
   find / -mtime +10 -mtime -20 => Find all the files which are modified more than 10 days back and less that 20 days
   find / -mmin 1 => Find all the files modified in last 1 minute
   find / -type -f -cmin 1 => Find all the files changed in last 1 minutes.
   find / -size -1M => Find all the files which size is less than 1 MB.
   find / -size +1M -size -3M => Find all the files which size is more than 1MB and less than 3MB.
   find . \(-name a.out -o -name '*.o' -o -name 'core'\) -exec rm {} \; => Clean out temporary files.

   find / -size +1G -mtime +300 -exec rm -rf {}; => Find files which are more thatn 1GB and not accessed for the past 10 months and delete them.

Unix/Linux Commands Notes

List of Command with options 1.  grep :  Find lines that match a regular expression Syntex: grep -A<N> "string" <F...