
11 ) Search inside compressed files $ gzip test.txt Lastly, filter out the tomcat default port using grep.

lt prints the display listening sockets and TCP sockets respectively.

The ss (socket statistics) command provides a lot of information by displaying details on socket activity and port listening. 10 ) Search Socket Statistics (ss) using grep $ ss -lt | grep -w 8080 Piping grep command to filter out tomcat and other processes. In the above example, aux refers to options of ps command where p option prints all the running processes in the system, u option prints user of the process and lastly x option prints the processes that have not been executed from the terminal. This command is the most common and frequently used Linux command that provides more and detailed process info of the system. 9) Search Process Status (ps) $ ps aux | grep -w tomcat Use grep with pip | to filter out the specific packages and the –i option will ignore-case in the filtering process. dpkg -l will do the listing of all the packages available in the system. Using Debian base Linux will provide many preinstall dot deb packages, dpkg command handle all the dot deb packages. 8) Finding specific dot deb package listing $ dpkg -l | grep -i nginx Later grep command can filter out the required text. In the second example, the cat command appends all the text from filename2 to filename1 at the endpoint of filename1 text. Searching specific phrases or from multiple files might be quite complicated. $ cat filename1 filename2 filename3 | grep -i ‘search-text’Ĭat command allows us to view a single file, multiple files, concatenate files. :~$ ls -l /etc | grep sudoĭrwxr-xr-x 2 root root 4096 Feb 12 10:17 sudoers.d In Linux pipe command lets you send the output of one command to another. Listing files and directories using the ls command will be easier if there are fewer files or dir.įor many files and directories, we can filter out them using grep with pipe ( | ) which helps us to locate the file faster. $ 6) Listing files and directories using grep Passing this option will show results with the line number which help us to locate the search texts in files. We can do so in the grep command by adding the hyphen n (-n) option. Searching words or sentences is easier using the grep command but it will be much easier if we could also view the line numbers where the search word is located. $ 5) Searching the line or word along with the line numbers etc/ufw/les:-A DOCKER-USER -i enp0s3-j DROP etc/ufw/les:-A DOCKER-USER -i enp0s3 -j ufw-user-input It informs the command to check all files recursively from all directories and sub-directories of the given path. Let’s assume we have files in directories and their sub-directories also have some files, we want to search words or sentences from all those files to do so we need to add hyphen r (-r). $ 4) Search words recursively from files & directories For that, we use the hyphen w (-w) option to perform such a search. What if we want to search by words only not by sub-string. In the above example, the grep command shows the output based on the sub-string. In such a case we can use the -i option to get outputs case-insensitively. Suppose we have a paragraph that contains the same words with different cases. Nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologinīy default grep command output our searched text case-sensitively. Example is shown below: $ grep nobody /etc/passwd The output will be the row containing search text or word. If our working directory and file directory are different we need to specify the file path with filename. Then, we simply need to use the grep command then specify the regex pattern or word we want to search. 1) Search text from the file.Ĭreate a new text file that contains some text. Let’s dive into examples of grep command. Regex is a sequence of characters that specifies a search pattern. The pattern implies plain text or regular expression (shortened as regex) to search matching sets of data. You can use the following command to check info on all options. S/".*//p removes everything from the first remaining " to the end of the line and prints the result.Option refers to the extra parameters to invoke or access the inbuilt feature. S/.*": "// removes everything from the beginning of the line to the last occurrence of ": ". Using sed $ echo '"io.": "kube-system",' | sed -n '/"io."/ selects only those lines that contain "io." and performs the commands in braces on them. * (which matches everything to the end of the line) with * with matches everything up to but not including the first ": $ echo '"io.": "kube-system",' | grep -Po '(?<="io.": ")*'

We need to make just one small change to the grep -P command.
