Grep Command

Grep command in Unix/Linux is a powerful tool that searches for matching a regular expression against text in a file, multiple files or a stream of input.

1.Search for a string “linux” in a file using  grep command in unix

This is the basic usage of grep command. It searches for the given string in the specified file. 

grep “linux” index.html

2.Insensitive case search with grep -i

The below grep command searches for the words like “LINUX”, “Linux”, “linux” case insensitively.

grep -i “linux” index.html

3.Searching for a string in multiple files

This command will search for “linux” string in multiple files at a time. It searches in all files with file1.txt, file2.txt and along with different extensions too like file1.html, file2.php and so on.

grep “linux” file*.*

4.Specifying the search string as a regular expression pattern

It is a very powerful feature and can use as a regular expression with much effectively. In the below example, it searches for all the pattern that starts with “fast” and ends with “host” with anything in-between. i.e To search “fast[anything in-between]host” in index.html file.

grep “fast.*host” index.html

5.Search the pattern recursively using -r option

The below command will search linux in the “/etc” directory recursively.

grep -r linux /etc/

Find Command
1.Find Files Using Name in Current Directory

Find all the files whose name is test.txt in a current working directory.

# find . -name test.txt

./tecmint.txt

2.Find Files Using Name and Ignoring Case

Find all the files whose name is test.txt and contains both capital and small letters in /home directory.

# find /home -iname test.txt

./test.txt

./Test.txt

3.Find Directories Using Name

Find all directories whose name is Test in / directory.

# find / -type d -name Test

/Test

4.Find all PHP Files in Directory

Find all php files in a directory.

# find . -type f -name “*.php”

./test.php

./login.php

./index.php

5.Find Files With 777 Permissions

Find all the files whose permissions are 777.

# find . -type f -perm 0777 -print

6.File all Hidden Files

To find all hidden files, use below command.

# find /tmp -type f -name “.*”

7.Find Last 50 Days Modified Files

To find all the files which are modified 50 days back.

# find / -mtime 50

8.Find Last 50 Days Accessed Files

To find all the files which are accessed 50 days back.

# find / -atime 50

9.Find Last 50-100 Days Modified Files

To find all the files which are modified more than 50 days back and less than 100 days.

# find / -mtime +50 –mtime -100

10.Find Accessed Files in Last 1 Hour

To find all the files which are accessed in last 1 hour.

# find / -amin -60

PS (processes status)
1.List All Processes in Current Shell

If you run ps command without any arguments, it displays processes for the current shell.

$ ps

2.Print All Processes in Different Formats

Display every active process on a Linux system in generic (Unix/Linux) format.

$ ps -A

OR

$ ps -e

3.Display Group Processes

If you want to list all processes owned by a certain group (real group ID (RGID) or name), type.

$ ps -fG apache

OR

$ ps -fG 48

4.Find top running processes by highest memory and CPU usage in Linux

$ ps -eo pid,ppid,cmd,%mem,%cpu –sort=-%mem | head

OR

$ ps -eo pid,ppid,cmd,%mem,%cpu –sort=-%cpu | head

5.To kill an Linux processes/unresponsive applications or any process that is consuming high CPU time

First, find the PID of the unresponsive process or application.

$ ps -A | grep -i stress

Then use the kill command to terminate it immediately.

$ kill -9 2583 2584

6.Display User Running Processes

You can select all processes owned by you (runner of the ps command, root in this case), type:

$ ps -x

7.To display a user’s processes by real user ID (RUID) or name, use the -U flag

$ ps -fU user

OR

$ ps -fu 1000

8.To select a user’s processes by effective user ID (EUID) or name, use the -u option

$ ps -fu user

OR

$ ps -fu 1000

9.Print All Processes Running as Root (Real and Effecitve ID)

The command below enables you to view every process running with root user privileges (real & effective ID) in user format.

$ ps -U root -u root

10.Finally, since ps displays static information, you can employ the watch utility to perform real-time process monitoring with repetitive output, displayed after every second as in the command below (specify a custom ps command to achieve your objective).

$ watch -n 1 ‘ps -eo pid,ppid,cmd,%mem,%cpu –sort=-%mem | head’