Joe Mornin
Joe Mornin

Reputation: 9134

See if binary files exist in subfolders

Say I have a directory structure like this:

+---A
|   +---A
|   \---B
+---B
|   \---A
|       \---A
+---C
|
|
[...]

How can I (1) crawl all folders and subfolders and (2) check if any files within those folders are binary files?

Upvotes: 1

Views: 972

Answers (1)

Jay jargot
Jay jargot

Reputation: 2868

find is usually used to search directory tree.

file -i can be used to print files mime type information.

Give this a try :

find . -type f -exec file -i {} + | grep ":[^:]*executable[^:]*$" |  sed 's/^\(.*\):[^:]*$/\1/'

-type f is a filter which selects regular files: not symbolic links, not directories etc.

The exec file -i {} + executes file -i on each regular file found in the directory tree.

file -i is printing mime type strings:

file -i /bin/bash
/bin/bash: application/x-executable; charset=binary

grep ":[^:]*executable[^:]*$" selects files with a mime type string which contains executable

sed 's/^\(.*\):[^:]*$/\1/' cleans up the line in order to print only filenames, without extra mime type information.

Upvotes: 2

Related Questions