How to Recursively Search for Files with Certain Extensions

Recursively find files with a specific extension

My preference:

find . -name '*.jpg' -o -name '*.png' -print | grep Robert

How can I grep recursively, but only in files with certain extensions?

Just use the --include parameter, like this:

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example

That should do what you want.

To take the explanation from HoldOffHunger's answer below:

  • grep: command

  • -r: recursively

  • -i: ignore-case

  • -n: each output line is preceded by its relative line number in the file

  • --include \*.cpp: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)

  • ./: Start at current directory.

How do I grep recursively in files with a certain extension?

find allows you to run a program on each file it finds using the -exec option:

find -name '*.out' -exec grep -H pattern {} \;

{} indicates the file name, and ; tells find that that's the end of the arguments to grep. -H tells grep to always print the file name, which it normally does only when there are multiple files to process.

How to retrieve recursively any files with a specific extensions in PowerShell?


If sorting by Length is not a necessity, you can use the -Name parameter to have Get-ChildItem return just the name, then use [System.IO.Path]::GetFileNameWithoutExtension() to remove the path and extension:

Get-ChildItem -Path .\ -Filter *.js -Recurse -File -Name| ForEach-Object {
[System.IO.Path]::GetFileNameWithoutExtension($_)
}

If sorting by length is desired, drop the -Name parameter and output the BaseName property of each FileInfo object. You can pipe the output (in both examples) to clip, to copy it into the clipboard:

Get-ChildItem -Path .\ -Filter *.js -Recurse -File| Sort-Object Length -Descending | ForEach-Object {
$_.BaseName
} | clip

If you want the full path, but without the extension, substitute $_.BaseName with:

$_.FullName.Remove($_.FullName.Length - $_.Extension.Length)

Recursively searching for files with specific extensions in a directory

You'd need to add a wildcard to each extension for fnmatch.filter() to match:

fnmatch.filter(filenames, '*' + extension)

but there is no need to use fnmatch here at all. Just use str.endswith():

for root, dirnames, filenames in os.walk(folder):
for filename in filenames:
if filename.endswith(extensions):
matches.append(os.path.join(root, filename))

or expressed as a list comprehension:

return [os.path.join(r, fn)
for r, ds, fs in os.walk(folder)
for fn in fs if fn.endswith(extensions)]

Recursively Find Files With Particular File Extension and Content

What do you mean by keyword? Is that a word, present inside the file? Or is it a part of the filename?

In case it's the part of the filename, you can use file: in the Windows search, like in following example:

file:*keyword*.py

This will show you all files, called *keyword*.py. After you've done that, you might change your Windows explorer's view, clicking on the "View" tab and choose "Details", this will also show you the directory where those files are located.

grep recursively for a specific file type on Linux

Consider checking this answer and that one.

Also this might help you: grep certain file types recursively | commandlinefu.com.

The command is:

grep -r --include="*.[ch]" pattern .

And in your case it is:

grep -r --include="*.html" "onblur" .


Related Topics



Leave a reply



Submit