How to List All Binary File Extensions Within a Directory Tree

How to list all binary file extensions within a directory tree?

Here's a trick to find the binary files:

grep -r -m 1 "^"  <Your Root> | grep "^Binary file"

The -m 1 makes grep not read all the file.

Show and count all file extensions in directory (with subdirectories)

Like this, using uniq with the -c, --count flag:

find . -type f -name '*.*' | sed 's|.*\.||' | sort | uniq -c

See if binary files exist in subfolders

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.

List files ONLY in the current directory

Just use os.listdir and os.path.isfile instead of os.walk.

Example:

import os
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
# do something

But be careful while applying this to other directory, like

files = [f for f in os.listdir(somedir) if os.path.isfile(f)]

which would not work because f is not a full path but relative to the current directory.

Therefore, for filtering on another directory, do os.path.isfile(os.path.join(somedir, f))

(Thanks Causality for the hint)



Related Topics



Leave a reply



Submit