Issue
Deep in the subdirectories are a few specific files containing a string that indicates they are the target directory I need. (This concerns creating virtual GPUs, but is beside the point.) I can find the files I need with this command:
cat `find ../../devices -name "name"` | grep 8Q
which results in this output:
GRID T4-8Q
GRID T4-8Q
GRID T4-8Q
GRID T4-8Q
but this does not tell me the filepath to those 4 files.
How can I rewrite this command so that it finds these 4 files, but also tells me the filepath?
TIA, Rick
Solution
find
has an exec
test that can do about anything you want on found files. If the executed command exits with 0 status the test passes. In the command {}
is replaced with the file name. The command must be terminated with \;
(there are other terminators with different behaviors but \;
is what you need here). In your case grep -q 8Q {} \;
is the command to use. It exits with status 0 if and only if the file contains 8Q
. find
also has a printf
test that can print the directory part (%h
).
You can try:
find ../../devices -type f -name "name" -exec grep -q 8Q {} \; -printf '%h\n'
If the found file is a real file (-type f
) and its name is name
(-name "name"
) and the file contains 8Q
, then the directory part is printed.
Answered By - Renaud Pacalet Answer Checked By - Katrina (WPSolving Volunteer)