Skip to main content

grep and find Commands in Linux

Updated 25 min readMarkdown
Part of seriesLinux

Both commands sometimes are confusing because they can be used to "search" something. However, they behave differently.

Command Searches for Example question
find Filenames, directories, file types, file location. "Where is my test.txt file?"
grep Characters, words, or sentences inside file(s). "Which file contains 'report'?"

It would be better to understand about the ls command here before you learn about find and grep commands.

Find

Basic structure:

find [location/path_to_search] [conditions]

πŸ“ Info:

  • In find, you can search in any location different than where you are.

  • Common [conditions] are

    • -name

    • -iname or -name

    • -type

    • -print

  • Without a condition, find just prints everything it sees.

Search everything in current directory

find .

πŸ“ Info:

  • . means current directory.

  • If you wanna search for something in the other directory (not current directory), you have to write full directory path after find.

  • This command shows all files and folders recursively because no condition is applied.

Search by filename - exact name

The option of -name is needed to find by filename using an exact filename.

find [location/path_to_search] -name "[filename and extension]"

πŸ“ Info:

  • [filename and extension] is the filename, including the extension.

  • Without quote on the filename, the code will behave unexpectedly.

  • Search in the whole location like find / makes the operation very slow. Define specific location as you can, for instance find Downloads.

  • A dot (.) in the [location/path_to_search] represents the working directory as search location.

Case study:

  • you wanna find a file named myreport.pdf,

  • that file is in the Downloads directory, and

  • you are in the Home directory.

Thus, the command would be:

find ~/Downloads -name "myreport.pdf"

⚠️ WARNING:

  • Because your current directory location is different than the directory of target file, you cannot use dot (.). Instead, you have to define full path of the directory of target file.

  • Without inserting -name, Terminal will think that you are giving the second path: "myreport.pdf" in the Downloads directory which does not exist and resulting error message: find: β€˜myreport.pdf’: No such file or directory.

  • If the file name is different, even only for a single character, like "myreport1.pdf", "my report.pdf", "Myreport.pdf", "MyReport.pdf", or else, the finding result is none or you will eventually find a wrong file. Therefore, find command is case-sensitive.

Search by filename - some characters of filename

This method allows you to find some characters in the filename, instead of the full characters. For example, there are hundreds of similar filename, but you remember that the file you need contains characters of "new". Even though you don't remember where the "new" exactly (either in the front, middle, back, between front and middle, or else), you can find files with the results close to your need.

The basic syntax is the same as search by filename using exact name, but you need to insert asterisk (also called as wildcard) before and/or after the characters you remember, depending on which part you do not know/remember like this:

find [location/path_to_search] -name "*[characters]*"

πŸ“ Info:

  • Asterisk (*) means everything, regardless of the number of characters or no character.

  • If you forget or do not know the other characters of the filename in the beginning (i.e. you are not sure whether the filename is "mynew.txt", "reportnew.txt", "thisisnew.txt"), you need to put the asterisk before the characters you know/remember: *[characters].

  • If you forget or do not know the other characters of the filename in the end (i.e. you are not sure whether the filename is "new01.txt", "newOK.txt", "newyes.txt"), you need to put the asterisk after the characters you know/remember: [characters]*.

  • If you forget or do not know the other characters of the filename in the beginning and end (i.e. you are only sure the middle of the filename contains "new"), you need to put the asterisk before and after the characters you know/remember: *[characters]*.

This command:

find . -name "*.pdf"

will give results all files with any filename, as long as the extension is pdf.

Case study:

  • you wanna find a file where you forget the exact filename, but you are sure that the middle contains "rem",

  • that file is in the Downloads directory, and

  • you are in the Home directory.

Thus, the command would be:

find ~/Downloads -name "*rem*"

The result would be like this:

/home/mylinux//Downloads/newlorem.txt
/home/mylinux//Downloads/reallorem.txt
/home/mylinux//Downloads/loremyes.txt
/home/mylinux//Downloads/lorem5.txt
/home/mylinux//Downloads/lorem2.txt
/home/mylinux//Downloads/lorem3.txt
/home/mylinux//Downloads/lorem4.txt

⚠️ WARNING: Because find command is case sensitive, if you change one or more character to capital, the result will be none or refers to a wrong file.

Search by filename - some characters of filename, insensitive

In the situation where you can only remember the pronounciation or characters in a filename, but you are not sure whether they are capitalised or lowercase or uppercase, you need to ask find to ignore the capital letter status. To use this, use -iname, instead of -name.

This code:

find . -iname "*lor*"

will give results like this:

/home/mylinux//Downloads/new Lorem.txt
/home/mylinux//Downloads/Lorem Ipsum.txt
/home/mylinux//Downloads/notloremanymore.txt
/home/mylinux//Downloads/lORem.txt
/home/mylinux//Downloads/loREM.txt
/home/mylinux//Downloads/LoReM.txt

Search for directories only

find . -type d

will only return the results directories.

If you want to filter the results only directories containing certain characters in the directory names, use this command:

find . -type d -name "*[characters]*"

πŸ“ Info:

  • The [characters] are the characters that you want to find in the directory names.

  • The command can still work if the order is swapped like this: find . -name "[characters]" -type d

Search for files only

find . -type f

will only return the results files.

If you want to filter the results only directories containing certain characters in the filenames, use this command:

find . -type f -name "*[characters]*"

πŸ“ Info:

  • The [characters] are the characters that you want to find in the filenames.

  • The command can still work if the order is swapped like this: find . -name "[characters]" -type f

Search for files and delete them

find . -type f -iname "*freetube*" -exec rm {} +

Case study:

  • You wanna search for all files which the filename contains "report",

  • the search is for all subdirectories as well,

  • all files you found will be deleted, and

  • you need to ensure whether the files that you want to delete are correct.

Thus, the command would be 2 commands:

find . -type f -iname "*report*"

to look at them and ensure you are not gonna delete wrong files.

The result would be like this:

./Annual Report.pdf
./Draft Report.odt
./Screenshot of Report Structure.png
./Presentation for report.pptx

Afterwards, you can proceed with the rm command which is put right after the find command like this:

find . -type f -iname "*report*" -exec rm {} +

πŸ“ Info:

  • The find command is already recursively by default. Therefore, no need to use -r option as in the grep command.

  • -exec rm {} + to delete the files found efficiently.

Search empty files

find . empty

Search result filename and extension only

Please remember that a default result have pattern:

[full path]/[filename and extension]

If you need a clean, filename and extension only in the result list, use this command:

find . -type f -iname "*[characters]*" -print | while read file; do basename "$file"; done

πŸ“ Info:

  • find . searching in the current directory (.).

  • -type f to find files only.

  • -iname "*[characters]*" to find any file containing specific characters (case-insensitive) in the filename, regardless of the characters position.

  • -print to print the full path.

  • while read file; do basename "$file"; done to strip the directory path, showing only the filename.

Combining find and ls commands

Filtering specific characters, specific extensions, sort by filesize

⚠️ WARNING: This difficulty is quite high.

The result of find is only the path, filename and extension. When you want something more detail, you need to combine find and ls commands. You have to use find command first and then ls command later because you only want to see the details related to the files you find.

Case study:

  • in the Download directory, you want to find all files containing characters "the" (insensitive),

  • you want to find based on those characters, but only when the extension is either JPG or PDF, and

  • you want to show detailed information of files match your find criteria.

Thus, the command would be:

find . \( -iname "*the*.pdf" -o -iname "*the*.jp*g" \) -exec ls -lhS {} +

Or, in the multi-line version:

find . \
\( -iname "*the*.pdf" -o -iname "*the*.jp*g" \) \
-exec ls -lhS {} +

πŸ“ Info:

  • find . to search from current directory.

  • -iname is case-insensitive filename matching.

  • *the*.pdf to find filenames contain "the" characters anywhere (either in the beginning, middle or back) with the extension of PDF.

  • *the*.jp*g to find filenames contain "the" characters anywhere (either in the beginning, middle or back) with the extension of JPG or JPEG.

  • -o (little O, not number zero) is OR function. The number of -o is the number of conditions minus 1. For example, if there are 2 conditions (search for PDF and JPG), the number of -o is 1.

  • \( [conditions] \) is group conditions like in maths ( condition1 OR condition2 ).

  • -exec to run another command on found files.

  • -lhS show detailed long listing in a human-readable format and sorted by filesize (the biggest file at the top).

  • {} is the placeholder for found files.

  • + to run an efficient batching (run ls command for many files together, instead of once per file.

  • \ at the end of the line means to continue command to the next line.

Visual logic flow of the code above:

find current directory
WHERE:
    filename contains "the" (case-insensitive)
AND:
    extension is .pdf OR .jpg

THEN:
    run ls -lhS on results

Example result:

-rw-r--r-- 1 mylinux admin 120K May 14  giantTHEfile.pdf
-rw-r--r-- 1 mylinux admin  5M May 10  TheBeach.jpg
-rw-r--r-- 1 mylinux admin 2.1M Apr 28  another_the_file.pdf

Filtering specific editing date period, specific extensions, sort by modified time

find . \( -iname "*.pdf" -o -iname "*.jp*g" \) -newermt "YYYY-MM-DD-START" ! -newermt "YYYY-MM-DD-END" -exec ls -lht {} +

πŸ“ Info:

  • find . to search from current directory.

  • \( [conditions] \) is group conditions like in maths ( condition1 OR condition2 ).

  • -iname "*.pdf" is case-insensitive filename matching to find all PDF files.

  • -o (little O, not number zero) is OR function.

  • -iname "*.jp*g" is case-insensitive filename matching to find all JPG and JPEG files.

  • -newermt command.

    • -newermt "YYYY-MM-DD-START" is the start of date period. DO NOT write "START" in the syntax because it is written there to tell you which one is the start, which one is the end.

    • ! means not (logical negation).

    • -newermt "YYYY-MM-DD-END" is the start of date period. DO NOT write "END" in the syntax because it is written there to tell you which one is the start, which one is the end.

    • For example, -newermt "3000-01-01" ! -newermt "3000-01-05" means to filter all files that are modified after (newer) 1 January 3000, but NOT after 5 January 3000. In the other words, filter is applied for date period between 1 to 5 January 3000.

    • You can also use time as filter using this format: YYYY-MM-DD HH:mm, where HH is in 24-hour format and mm is in minute.

  • -lht show detailed long listing in a human-readable format and sorted by modified date (the newest file at the top).

  • {} is the placeholder for found files.

  • + to run an efficient batching (run ls command for many files together, instead of once per file.

  • \ at the end of the line means to continue command to the next line.

Visual logic flow of the code above:

find current directory

WHERE:
    extension is PDF, JPEG OR JPG
AND:
    modified between May 1 and May 9

THEN:
    display detailed info
    using human-readable filesize
    sorted by newest modification time

Another example, to find any files modified 7 days ago:

find . \( -iname "*.pdf" -o -iname "*.jpg" \) -newermt "7 days ago" -exec ls -lht {} +

Grep

grep stands for "Global Regular Expression Print." It is a command-line utility in Unix and Unix-like operating systems used for searching plain-text data for lines that match a specified pattern. The name reflects its functionality: it searches globally (across files) for lines that match a regular expression (regex) and prints those lines to the output.

The input of grep command can be from various sources.

  1. Files. For example, you search for lines contain characters you want to find.

  2. Regex. Complex pattern needs regex to filter, and then the results will be the grep input.

  3. Piping. Output of another (or other) command will be the grep input.

Different than find command, grep command does not need the path in its argument. Whereas, find always needs path in its arguments.

Basic Syntax:

grep [options] "pattern" [file...]

Note that the " can be replaced by '. However, to prevent a single quote mark in the character that needs to be searched, it's better to use double quote. For example, you wanna search for something like this: grep 'don't', the grep command has a problem because you use single quote 3 times, instead of 2 times.

File extensions that grep can work with

Format Grep directly?
TXT, CSV, HTML, PHP, PY, CSS, MD, LOG, TEX, JSON, XML Yes
DOCX, XLSX, PPTX, ODT, ODS, ODP ⚠️ Not directly (use zipgrep or unzip first)
DOC, XLS, PPT (legacy binary Office) ⚠️ Usually no (use strings or converters)
XLSB, XLL, XLA and other binary add-ins ⚠️ Usually no (use specialized tools)

Search for characters among files

grep "[characters]" file1.txt file2.txt

πŸ“ Info:

  • You can use as many documents or files as the sources.

  • Filename uses no quote mark ("), but caracters should be between two quote marks "characters".

  • Filename should be a complete path and filename if:

    • your current directory and source directory is different, or

    • your sources (more than one) are located in the different directories.

  • You can use any characters in "characters" like numbers, letters and symbols.

  • You can use -i to search in insensitive way.

Case study:

  • you wanna find all lines containing characters "port",

  • Source files:

    • myreport.pdf in Downloads,

    • future.html in Documents, and

  • you are in the Home directory.

Thus, the command would be:

grep -i "port" ~/Downloads/myreport.pdf ~/Documents/future.html

Sample output:

myreport.pdf:This report is published in 2000 in New York.
myreport.pdf:The increasing of public activities in the airport.
future.html:We are waiting for teleport technology.
myreport.pdf:Portable fire extinguishers are available in the Port A.

πŸ“ Info:

  • The output is insensitive which covers "---port", "port---", "Port", and similar pattern.

Search for multiple criteria

Searching for lines based on multiple criteria in one go is efficient rather than search several times. The biggest advantage is we can seel all lines as a result, then inspecting them is easier, instead of looking at different result. To allow this, use this command:

grep -i -e "*[characters1]*" -e "*[characters2]*" -e "*[characters3]*" [filename and extension]

This command has the same function, but more difficult to inspect:

grep -i -E "*[characters1]*|*[characters2]*|*[characters3]*" [filename and extension]

The good thing of using -e is we can inspect a long criteria like in this example:

grep \
-e "*error*" \
-e "*warning*" \
-e "*fail*" \
.txt

Using -i is always useful to prevent blank or no result. If it's necessary, use -r as well to search in all subdirectories.

You can also save a bunch of criteria in a file, and then run the command to read that criteria file. The command:

grep -f [filename and extension of criteriafile] [filename and extension of target file]

Grep output into a TXT file

Sometimes the result of grep can be very long and looking in the Terminal can iritate your eyes. Hence, save the output in a TXT format would save your live. Use this command:

grep -i "port" ~/Downloads/myreport.pdf ~/Documents/future.html > "Grep result.txt"

πŸ“ Info:

  • The result file ("Grep result.txt") is located in the home directory. To save it in a specific directory ither than Home, write full path before the filename.

  • The > to write the result into a new filename. If the a file with the same name exists, this command will generate a new file that replaces the old file (all contents in the old file will be gone).

  • To prevent content replacing, use >> which will append the result (the existing content will be kept).

Using either > or >> will disable the showing the result in the Terminal. To show the result in the Terminal too, use this command:

grep -i "port" ~/Downloads/myreport.pdf ~/Documents/future.html | tee "Grep result.txt"

πŸ“ Info:

  • | to combine commands.

  • tee to show the result on the Terminal and save the result into a new file (replace only, cannot append).

Search recursively

grep -r "characters" .

πŸ“ Info:

  • . at the end of the command means current directory, regardless of the depth and size of the directories under it.

  • grep puts the . at the end, while find puts the . right after find.

  • If there is a different between your current directory and the source directory, you should write full path.

Inverting match

grep -v "[characters]" [filename and extension]

Case study:

  • You wanna show all lines in the file "abc.txt" NOT containing colon character (:),

  • the file is in the same location where you are, and

  • source rows:

-- SYSTEM UPTIME --

Server A: 99.98%

Server B: 100.00%

Downtime: 0m 0s

-- SECURITY LOG SUMMARY --

Total Login Attempts : 1,245

Successful Logins : 1,239

Failed Logins : 6

Unauthorized Blocks : 0

Thus, the command would be:

grep -v ":" abc.txt

Sample output:

-- SYSTEM UPTIME --

-- SECURITY LOG SUMMARY --

Inverting match and skip blank lines

Combination of searching by excluding all lines containing specific characters and excluding all blank lines.

grep -v "[characters]" [filename and extension] | grep -v "^$"

πŸ“ Info:

  • -v "^$" to exclude blank lines.

  • -v (after grep ) to exclude all lines containing certain characters.

  • "[characters]" filter in grep command.

Case study:

  • You wanna show all lines in the file "abc.txt" which DO NOT containing characters ":",

  • you wanna exclude all lines containing blank (no character at all),

  • the file is in the same location where you are, and

  • source rows:

-- SYSTEM UPTIME --

Server A: 99.98%

Server B: 100.00%

Downtime: 0m 0s

-- SECURITY LOG SUMMARY --

Total Login Attempts : 1,245

Successful Logins : 1,239

Failed Logins : 6

Unauthorized Blocks : 0

Thus, the command would be:

grep -v ":" abc.txt | grep -v "^$"

Sample output:

1:-- SYSTEM UPTIME --
2:-- SECURITY LOG SUMMARY --

Without excluding blank lines, the result would be:

1:-- SYSTEM UPTIME --
2:
3:-- SECURITY LOG SUMMARY --

Showing files contain or not contain specific characters

This function is similar to the -v, but has a solid difference as follows.

Option Operates on Output
-v Lines Non-matching lines (invert line matches)
-l Files Filenames that contain at least one match
-L Files Filenames that contain no matches

-l is the opposite of -L.

Case study:

You have 3 files with contents as follows.

fruit1.txt:

Apples Apricot Banana Blackberry Blueberry Melon Cherimoya Cherry Christmas Melon Clementine Cranberry Crenshaw Kiwi Lemon Mango Nectarine Orange Pineapple Quince Raspberry Tomato

fruit2.txt:

Apples Apricot Banana Blackberry Blueberry Melon Cherimoya Cherry Christmas Melon Clementine Cranberry Crenshaw Durian Kiwi Lemon Mango Nectarine Orange Pineapple Quince Raspberry Tomato

fruit3.txt:

Apples Apricot Banana Blackberry Blueberry Melon Cherimoya Cherry Christmas Melon Clementine Cranberry Crenshaw Kiwi Lemon Mango Nectarine Orange Pineapple Quince Raspberry Grape Tomato

If you run:

grep -iv "durian" *.txt

Result:

fruit1.txt:Apples Apricot Banana Blackberry Blueberry 
fruit1.txt:Melon Cherimoya Cherry Christmas Melon 
fruit1.txt:Clementine Cranberry Crenshaw
fruit1.txt:Kiwi Lemon Mango Nectarine Orange 
fruit1.txt:Pineapple Quince Raspberry Tomato
fruit2.txt:Apples Apricot Banana Blackberry Blueberry 
fruit2.txt:Melon Cherimoya Cherry Christmas Melon 
fruit2.txt:Kiwi Lemon Mango Nectarine Orange 
fruit2.txt:Pineapple Quince Raspberry Tomato
fruit3.txt:Apples Apricot Banana Blackberry Blueberry 
fruit3.txt:Melon Cherimoya Cherry Christmas Melon 
fruit3.txt:Clementine Cranberry Crenshaw
fruit3.txt:Kiwi Lemon Mango Nectarine Orange 
fruit3.txt:Pineapple Quince Raspberry Grape Tomato

If you run:

grep -il "durian" *.txt

Result:

fruit2.txt

If you run:

grep -iL "durian" *.txt

Result:

fruit1.txt
fruit3.txt

Filtering filenames and lines

grep --exclude="*[characters to be skipped in the filename]*" "[characters to be found in the lines]" [filename and extension]

Case study:

  • Look at the examples: fruit1.txt, fruit2.txt, fruit13txt,

  • you also have another file called allfruits.txt,

  • you don't need to look at any files which the filename contain "all",

  • based on the filename exclusion, you need to find all lines containing "berry".

Thus, the command would be:

grep --exclude="*all*" "berry" *.txt

Result:

fruit1.txt:Apples Apricot Banana Blackberry Blueberry 
fruit1.txt:Clementine Cranberry Crenshaw
fruit1.txt:Pineapple Quince Raspberry Tomato
fruit2.txt:Apples Apricot Banana Blackberry Blueberry 
fruit2.txt:Clementine Cranberry Crenshaw Durian
fruit2.txt:Pineapple Quince Raspberry Tomato
fruit3.txt:Apples Apricot Banana Blackberry Blueberry 
fruit3.txt:Clementine Cranberry Crenshaw
fruit3.txt:Pineapple Quince Raspberry Grape Tomato

πŸ“ Info:

  • Do not use "" in defining target files (explanation is below these bullets).

  • The command above basically runs 2 commands:

    • File selection

      • Consider all files matched by *.txt.

      • Ignore any whose filename matches *orange*.

    • Content search

      • Open the remaining files.

      • Print lines matching "Apple".

  • To exclude directories, use --exclude-dir="*[characters to be skipped in the filename]*"

Comparison all commands with and without "" (or '') in defining target files.

Expression Meaning
*.txt All files ending with .txt.
"*.txt*" or '*.txt' All files with file name litterally .txt ( is a filename).

The double filtering is useful when you only wanna search in certain files and you know exactly what filenames should be excluded. The result files (not been excluded) will be treated as an input in the search for lines operation. This trick is usefull when there are so many files in a directory. Therefore, the search for line operation will be easier because irrelevant files already excluded in the first place.

Match exact word

Based on the 3 files used in case study of -l and -L, if you run:

grep -i "berry" *.txt

Result:

fruit1.txt:Apples Apricot Banana Blackberry Blueberry 
fruit1.txt:Clementine Cranberry Crenshaw
fruit1.txt:Pineapple Quince Raspberry Tomato
fruit2.txt:Apples Apricot Banana Blackberry Blueberry 
fruit2.txt:Clementine Cranberry Crenshaw Durian
fruit2.txt:Pineapple Quince Raspberry Tomato
fruit3.txt:Apples Apricot Banana Blackberry Blueberry 
fruit3.txt:Clementine Cranberry Crenshaw
fruit3.txt:Pineapple Quince Raspberry Grape Tomato

All lines containing "berry", including any fruits containing "berry", will be shown. Maybe it's not what you need. If you need to search for word, use this command:

grep -iw "grape" *.txt

Result:

fruit3.txt:Pineapple Quince Raspberry Grape Tomato

If you run:

grep -iw "berry" *.txt

there will be no result.

Counting matches

grep -c "[characters]" [filename and extension]

Case study:

  • You wanna know how many lines containing ":", and

  • the same file and same directory as in the previous case study are applied.

Thus, the command would be:

grep -c ':' abc.txt

Sample output:

7

Line numbering

grep -n "characters" .

Case study:

  • You wanna search for all lines containing "server" (insenstiive),

  • you need to find in all subdirectories,

  • you wanna know at which line each result is, and

  • the same file and same directory as in the previous case study are applied.

grep -irn 'server' abc.txt

Result:

2:Server A: 99.98%
3:Server B: 100.00%

πŸ“ Info:

  • The line number refers to the line number in the file you run, not the line of result.

  • The result pattern: [line number][colon][line of result] > no space among them.

Using Regular Expressions

Regular expressions (regex) syntax can also be used in grep command.

Search for a number pattern

grep -E "[regex pattern]" [filename and extension]

Case study:

  • you wanna find all lines containing pattern XX-YYYY where XX is 2-digit of alphabet (from A to Z, either in capital or non-capital) and YYYY is 4-digit of numbers (from 0 to 9),

  • the search is insensitive,

  • the files can be in the parent directory, or subdirectories (deep until several layers underneath), and

  • target files are all files in the current working directory.

Thus, the command would be:

grep -riE "[a-zA-Z]{2}-[0-9]{4}" .

Filtering the running process

If you know nad remember the exact name of application:

ps aux | grep "[application name]"

If you only remember some characters:

ps aux | grep -i "[characters in the application name]"

Example command: ps aux | grep "*fox*" gives the result:

mylinux 18868 0.2 0.1 19016 2600 pts/1 S+ 10:11 0:00 grep --color=auto -i fox

which means:

  • mylinux: This is the username of the user who is running the command.

  • 18868: This is the Process ID (PID) of the grep command that was executed.

  • 0.2: This indicates the percentage of CPU usage by this process.

  • 0.1: This shows the percentage of memory usage by this process.

  • 19016: This is the total virtual memory size of the process in kilobytes.

  • 2600: This is the resident set size (RSS), which indicates the amount of physical memory (RAM) used by the process in kilobytes.

  • pts/1: This indicates the terminal (pseudo-terminal) from which the command was run.

  • S+: This indicates the state of the process. "S" means the process is sleeping, and "+" indicates that it is in the foreground process group.

  • 10:11: This is the time when the command was started.

  • 0:00: This shows the total CPU time used by the process, which is 0 minutes and 0 seconds in this case.

  • grep --color=auto -i fox: This is the actual command that was executed. It shows that grep is searching for the pattern "fox" in a case-insensitive manner, and --color=auto indicates that it will highlight the matches if the output is directed to a terminal.

  • When the output is only one line, it indicates that the showed line is the only match found by the grep command. This happens because the pattern fox is not matching any other running processes.

Filtering file type

ls -l | grep .[extension]

Case study:

  • You wanna show all Python files in a directory, and

  • the running directory is the same as the directory of Python files.

Thus, the command would be:

ls -l | grep ".py"

The result would be like this:

-rw-rw-r-- 1 ab cd   1469 Aug  5  2025 v.py
-rw-rw-r-- 1 ab cd    109 Jan 12 11:43 w.py
-rw-rw-r-- 1 ab cd    362 Jan 14 13:47 x.py
-rw-rw-r-- 1 ab cd   3907 Apr  2 10:14 y.py

πŸ“ Info:

  • ab indicates the owner.

  • cd indicates the group name associated with the file.

  • Numbers (i.e. 109, 362) indicates the size of the file in bytes

Filtering command history

history | grep "[command or characters of command]"

Case study:

  • You have thousands of command history lines, and

  • you wanna check what commands use used related to "sudo apt install".

Thus, the command would be:

history | grep "sudo apt install"

The result would be like this:

  950  sudo apt install w3m
 1110  sudo apt install openjdk-17-jre
 1193  sudo apt install vlc
 1277  sudo apt install spotify-client
 1345  sudo apt install kitty
 1850  sudo apt install gnome-keyring
 2509  sudo apt install clementine

About the author

  • Safety professional by day, Linux explorer by night.

No comments yet. Be the first to comment.