![grep options linux grep options linux](https://net2.com/wp-content/uploads/2019/10/word-image-23.jpeg)
:~# grep -E "linuxtechi|root" /etc/passwd Example 12) Getting Search pattern from a file (grep -f) Example 11) Search multiple patterns or words (grep -e)įor example i want to search ‘linuxtechi’ and ‘root’ word in a single grep command, then use -e option in grep command followed by search pattern :~# grep -e "linuxtechi" -e "root" /etc/passwd Note : Grep command allow the search based on exact word using ‘-w’ option, example is shown below, :~$ grep -w cook /mnt/my_dish.txtĪbove command will search and look for the lines which have “cook” word.
![grep options linux grep options linux](https://www.web24.com.au/wp-content/uploads/2016/10/grep-small-500x295.png)
Let’s take an example, i want to search “LinuxTechi” word in the passwd file. i option in the grep command ignores the letter case i.e it will not discriminate upper case or lower case letters while searching Example 10) Ignore letter case while searching (grep -i) To search and list all the empty or blank lines from a file use the special character combination ‘^$’ in grep command, example is shown below: :~# grep ^$ /etc/shadowĪs there is no empty line in /etc/shadow file, so nothing is displayed. Example 9) Search all the empty or blank lines of a file (grep ^$) etc/passwd-:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bashĪbove command will search ‘linuxtechi’ word in the “/etc” directory recursively.
![grep options linux grep options linux](https://anglehit.com/wp-content/uploads/2018/03/ps-ppid.png)
To search a word recursively in all the files of a folder then use ‘-r’ option in grep command, example is shown below: :~# grep -r linuxtechi /etc/ It will do the searching recursively in current working directory. When we run grep command without mentioning the file names then it will display the lines from all files that contains the matching string. :~# Example 8) Search word or pattern recursively (grep -r) List all the lines of /etc/passwd that ends with “ bash” word. :~# Example 7) Search all the lines that ends with specific word (grep $)īash shell treats dolar sysmbol ‘$’ as a special character which marks the ends of line or word. Let’s display the lines which starts with “root” word in the file /etc/passwd, run beneath grep command :~# grep ^root /etc/passwd :~# grep -v linuxtechi /etc/passwdĮxample 6) Search all the lines that starts with specific pattern (grep ^)īash shell treats caret symbol (^) as a special character which marks the beginning of line or a word. List all the lines of the file /etc/passwd that does not contain specific word “linuxtechi”. Use ‘-n’ option in grep command, In our example pattern is ‘linuxtechi’ :~# grep -n linuxtechi /etc/passwdģ9:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bashīelow is the another example which displays the line and its number after matching a word ‘root’ in /etc/passwd and /etc/shadow file :~# grep -n root /etc/passwd /etc/shadowĮxample 5) Print the lines excluding the pattern (grep -v) Let’s suppose we want list line and its number which matches the pattern or word. :~# Example 4) Search the pattern in the file along with associated line number (grep -n) :~# grep -l linuxtechi /etc/passwd /etc/shadow /etc/fstab /etc/mtab Let’s assume we want to list the files which contain word ‘linuxtechi’ from multiple files, to do so use ‘-l’ option in grep command followed by word (pattern) and files. :~# Example 3) List the name of those files which contain word ( grep -l) etc/passwd:linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash To search ‘linuxtechi’ word in multiple files like /etc/passwd, /etc/shadow and /etc/gshadow, run :~# grep linuxtechi /etc/passwd /etc/shadow /etc/gshadow :~# Example 2) Search the pattern in the multiple files Linuxtechi:x:1000:1000:linuxtechi,:/home/linuxtechi:/bin/bash
![grep options linux grep options linux](https://4.bp.blogspot.com/-yR5VSt8m8Z0/WXxBqBY_NLI/AAAAAAAAJHg/e2lp2rUW10MrpT0sRwNUNYDoJ_LRFRHjQCLcBGAs/w610-h248/grep%2Brecursive%2Bexample%2BUNIX%2BLinux.png)
Search the “linuxtechi” word in the file /etc/passwd file, :~# grep linuxtechi /etc/passwd When we run grep command followed by search string or pattern then it will print the matching line of a file. Example 1) Search the pattern or word in a file In this tutorial we will discuss 14 useful linux grep command examples, let dive into the examples.