is it possible to search for a file based on its type in a directory tree?

Hi list,

I have to look for a file of a specific type in a subdirectory. I do not
now its name nor other meta data, except of its type.

If I would have the file, I could determine its type using the file
command, like this:

$ file unknownfile

And the output would be:

unknownfile: Java KeyStore

This means, I am looking for a file, which is of type "Java KeyStore".

Is it possible to use find, to recursively look for all files in a
directory tree, which are of type "Java KeyStore"?

Can anyone help on this?

Best,
Patrick

what about a simple:

  find dir -type f -print0 | xargs -0 file | fgrep 'type'

Looks very promising! I will check it out and let you know about the
results.

Thank you very much!
Patrick

my 2 cents:

find [DIR] -type f -exec file {} \; | grep "Java KeyStore"

I tested with
find . -type f -exec file {} \; | grep "UTF-8 Unicode text"
and
find . -type f -exec file {} \; | grep "HTML document, UTF-8 Unicode text,
with CRLF line terminators"

if you get error "find: ‘xxxxxxxxxxxxxxxxxx’: Permission denied" ,
prepone command with sudo
sudo find . -type f -exec file {} \; | grep "UTF-8 Unicode text"

diego

attachment.htm (2.59 KB)

If you have a large amount of files to check, you mught want to
improve this "one-liner" with a couple of additional options to
exclude some file to be found _and_ tested against the file(1)
utility:

* for find, use -uid, -user, -group, -gid, -perm (and variations), if
you know the owner of the file, or -[a|m]time, +[a|m]time for
time-based attributes
* for file, check -e to exclude a couple of useless calls to file.

Additionally if you are sure the file you are seraching for has no
extension, try

find <dir> -not -name "*.*" and pipe it like above.

this would *NOT* return files with a dot in the name (so it excludes
also all files with extensions)

HTH,
STefano

...obviously I meant:

find <dir> -type f -not -name "*.*"

Cheers,
Stefano

Hi Steevie,

If you wish to have in the output only the file with his path, I would
try something like

for i in `find . -type f`; do [[ "$(file -b --mime-type $i)" =
"application/pdf" ]] && echo $i; done

Hope this helps,
Gianpaolo

Hi Patrick,

Hi Steevie,

If you have a large amount of files to check, you mught want to
improve this "one-liner" with a couple of additional options to
exclude some file to be found _and_ tested against the file(1)
utility:

My problem is, that I have no clue about the name nor other meta data of
the file. :frowning:

Was this the project of an intern who liked to joke? :stuck_out_tongue:

Well more seriously, without *any* information about the file, that's
like searching a needle in a haystack, so the only "optimisation" I
can think of is a divide&conquer approch, search for a few (sub)dirs
at a time, ignoring symlink. And perhaps a time-based search would
also help, if you have any clue about when the file was last accessed
or modified.

Oh, BTW, perhaps using locate -e instead of find would quicken the search.

m2c
Stefano

This should be moderately okay:
find /home/michele -type f -exec file --mime-type "{}" \; | awk '{if ($NF == "application/x-java-keystore") print $0 }'

hth,
Michele