Recursively find files with a specific extension
My preference:
find . -name '*.jpg' -o -name '*.png' -print | grep Robert
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.
Python recursively find files with specific extension
>>> 'venom_trailer.Mp4'.endswith('mp4') # <-- file having .Mp4 extension is still a valid video file so it should have been counted.
False
>>> 'venom_trailer.Mp4'.lower().endswith('mp4')
True
#------------
>>> file_name = 'venom_trailer.Mp4'
>>> last_occurance_of_period = file_name.rfind('.')
>>> file_extension = file_name[last_occurance_of_period:]
>>> file_extension
'.Mp4'
>>> file_extension.lower() == '.mp4'
True
#------------
# replace this line
for fn in fs if fn.endswith(extensions)
# with this
for fn in fs if fn.lower().endswith(extensions)
# or with this
for fn in fs if fn[fn.rfind('.'):].lower() in 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.
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)]
recursive search of file with specific extension
You want the sum after filtering all the files:
def fileCount(path, extension):
count = 0
for root, dirs, files in os.walk(path):
count += sum(f.endswith(extension) for f in files)
return count
files
returns a list of files so sum(f.endswith(extension) for f in files)
will give you the count of all the files ending with the given extension.
Or just return the sum of all:
def fileCount(path, extension):
return sum(f.endswith(extension) for root, dirs, files in os.walk(path) for f in files)
Recursively find files with a certain extension using FindFirstFile and FindNextFile
You need to search recursively for each subdirectory. I happen to have some code to do this, the following code might help.
#include <functional>
#include <io.h>
enum enumflags {
ENUM_FILE = 1,
ENUM_DIR,
ENUM_BOTH
};
//return value:
// False means that the searching has been aborted by the callback function.
// It will return true otherwise.
bool enumsubfiles(
const std::wstring &dir_with_back_slant, //for example: L"C:\\", L"E:\\test\\"
const std::wstring &filename, //for example: L"123.txt", L"*.exe", L"123.???"
unsigned int maxdepth, //0 means not searching subdirectories, 1 means maximum depth of subdirectories is 1,
// pass -1 to search all the subdirectories.
enumflags flags, //search files, directories, or both.
std::function<bool(const std::wstring &dir_with_back_slant, _wfinddata_t &attrib)> callback
)
{
_wfinddata_t dat;
size_t hfile;
std::wstring fullname = dir_with_back_slant + filename;
std::wstring tmp;
bool ret = true;
hfile = _wfindfirst(fullname.c_str(), &dat);
if (hfile == -1) goto a;
do {
if (!(wcscmp(L".", dat.name) && wcscmp(L"..", dat.name))) continue;
if (((dat.attrib&_A_SUBDIR) && (!(flags&ENUM_DIR))) || ((!(dat.attrib&_A_SUBDIR)) && (!(flags&ENUM_FILE)))) continue;
ret = callback(dir_with_back_slant, dat);
if (!ret) {
_findclose(hfile);
return ret;
}
} while (_wfindnext(hfile, &dat) == 0);
_findclose(hfile);
a:
if (!maxdepth) return ret;
tmp = dir_with_back_slant + L"*";
hfile = _wfindfirst(tmp.c_str(), &dat);
if (hfile == -1) return ret;
do {
if (!(wcscmp(L".", dat.name) && wcscmp(L"..", dat.name))) continue;
if (!(dat.attrib&_A_SUBDIR)) continue;
tmp = dir_with_back_slant + dat.name + L"\\";
ret = enumsubfiles(tmp, filename, maxdepth - 1, flags, callback);
if (!ret) {
_findclose(hfile);
return ret;
}
} while (_wfindnext(hfile, &dat) == 0);
_findclose(hfile);
return ret;
}
Here is an example of the usage of the function above:
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
//the default code page of my console window is 936
setlocale(CP_ACP, ".936");
enumsubfiles(L"C:\\", L"*.exe", 1, ENUM_FILE, [](const std::wstring &dir_with_back_slant, _wfinddata_t &attrib)->bool
{
std::wcout << dir_with_back_slant << attrib.name << '\n';
return true; //return true to continue, return false to abort searching.
});
return 0;
}
And you will get the following output:
C:\OpenSSL-Win64\unins000.exe
C:\putty\PAGEANT.EXE
C:\putty\PLINK.EXE
C:\putty\PSCP.EXE
C:\putty\PSFTP.EXE
C:\putty\PUTTY.EXE
C:\putty\PUTTYGEN.EXE
C:\Windows\ampa.exe
C:\Windows\bfsvc.exe
C:\Windows\explorer.exe
C:\Windows\HelpPane.exe
C:\Windows\hh.exe
C:\Windows\notepad.exe
C:\Windows\regedit.exe
C:\Windows\RtCRU64.exe
C:\Windows\SECOH-QAD.exe
C:\Windows\splwow64.exe
C:\Windows\winhlp32.exe
C:\Windows\write.exe
C:\测试\测试.exe
The maxdepth I passed to the function is 1. Pass -1 to search all the subdirectories.
Related Topics
Linux/Bash, Using Ps -O to Get Process by Specific Name
Mongodb Data Directory /Data/Db Not Found
Abuse Curl to Communicate with Redis
What's the Difference Between "Env" and "Set" (On MAC Os X or Linux)
How to Create Virtual Ethernet Devices in Linux
Using Iconv to Convert from Utf-16Le to Utf-8
Writing to Serial Port from Linux Command Line
Differencebetween Xterm-Color & Xterm-256Color
What Does Anon-Rss and Total-Vm Mean
Permanently Change Disassembly Flavor in Gdb
Getting Stty: Standard Input: Inappropriate Ioctl for Device When Using Scp Through an Ssh Tunnel
Limiting the Time a Program Runs in Linux