Wednesday, June 11, 2014

Basic Linux commands for DBAs - Part III


Search Related

Command
Description And Example
 grep
 The grep command is used to search for a text in the file or from a list
 
 $ grep "linux" file1.txt           -- Search for lines containing "linux"
 $ grep kalyan/etc/passwd   -- Search for lines containing "kalyan"
 
-i- case insensitive serach
 
$ grep -i "kalyan"  file1.txt     --Search for lines containing "kalyan" and ignore the cas
 
-n displays line number
 
$ grep -i -n "linux" t1.txt     -- Search for lines containing "linux" and ignore the case and displays line number on which line it find the word linux
 
Search string in multiple files
 
$ grep -i "linux" *.txt           -- Search for lines containing "linux" in all *.txt files
 
-c Displays Occurrences counts from
 
$ grep -ic "linux" *.txt          -- Search for lines containing "linux" in all .txt files and displays only occurrences count
 
-v Inverts the search to display lines that doesn't match
 
$ grep -vic "linux" *.txt           -- Displays lines that doesn't has "linux" word on it and displays only occurrences count
 
Regular Expressions with Grep
 
^d        -- Displays lines start with d
 
$ grep "^t" -in t1.txt               -- Displays lines start with T and -i- is used to ignore the case
 
$ grep "^[TL]" -in t1.txt           --Displays lines start with either T or L
 
$ grep "[^0-9]" -in t1.txt           --Displays lines contains atleast one-numeric value, the meaning of ^ caret will change if you                
                                                        enclosed with square braces.
 
$ ps -ef | grep "mysql"            --Displays processes which has mysql in it.
 
$ ls - l | grep "^d"                     --Displays only directories
 
$ grep "x$" t2.txt                      --Displays lines ends with "x"
 
$ grep "5\.." t1.txt                    --Suppose we have data in a file with version numbers like 5.1, 5.x and 5.5 like that, It will search those lines and give you output
 
$ grep "[A-Z][A-Z]"  t1.txt         --Displays lines containing two capital letters (Ex : EA, SA etc.,)
 
 
 find
Find command is used to locate files in the linux / unix server
 
Syntax  : fine where-to-look criteria what-to-do
 
All arguments to find command are optional.
 
$ find                              -- Displays paths of all files in the current directory and sub-directories.
 
$ find . -print                 -- Displays exact same result as above command
 
$ find / -name "test.txt" -- Search for the file test.txt on all the current file system
 
$ find . -name "Kalyan*" -- List filenames starts with Kalyan, If you wan to ignore case then use -i.
 
$ find . -name "*.txt"      -- Lists  only .txt files
 
We can also specify multiple locations as below to search for any particular file
 
$ find /var/lib /usr/tmp $HOME -name "*.txt"           -- File locations must be separated by space.
 
-type option is a parameter to separate the files and directories which can be used along with find command, This can be used only if you want to make your search specific to one particular type
 
$ find . -type f -name "t1.txt"                --Search for a file with name t1.txt in the current location
 
$ find . -type d -name "binaries"          -- Search for a directory name binaries in the current location
 
$ find . -type f -name "*.java"
 
$ find . -type f -perm 0555 -print           -- Displays files which has permissions 555 (read and execute for all )
 
$ find . -type f -perm 0777 -print -exec chmod 644 {} \; -- Search for files which has 777 and changes permissions to 644
 
$ find . -type f -name "t1.txt" -exec rm -f {} \;     -- find and remove t1.txt file in the current folder
 
$ find . -type f -empty            -- Find all empty files
 
$ find . -type d-empty            -- Find all empty directories
 
-user  -- This option helps to find files based on the owner type
 
$ find . -user root       -- Displays files whose owner is root in the current directory
 
$ find . -group bin         -- Displays files whose group is bin in the current directory
 
$ find . -group bin -name "t2.txt"       -- Search files by group
 
-size and -mtime  -- Size option is helps to search files by size and mtime helps to search files by modification time
 
$ find . -size +10M          --Find files bigger than 10 MB size in current directory
 
$ find . -size -10M           --Find files lesser than 10 MB size in current directory
 
$ find . -mtime 1             --Search files and folders that are modified in last 24 hours
 
$ find . -mtime 10           --Search for files and folders that are modified in last 10 days
 
$ find . -mtime 10 -type f    -- Search for only files that are modified in last 10 days
 
$ find . -mtime 10 -type d    -- Search for only directories that are modified in last 10 days
 
Advanced
 
$ find . -type f \( -name "*.txt" -o -name "*.t" \)   -- Search files with multiple extentions
 
Find and Copy
 
find . -type f \( -name "*.txt" -o -name "*.t" \) -exec cp {} txtfiles
 
 find . -type f -name "*.txt" -exec grep -i "linux" {} \;
 

 

No comments: