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
What Is The Fastest Way to Display an Image in Qt on X11 Without Opengl
Cx_Oracle: Distutils.Errors.Distutilssetuperror: Cannot Locate Oracle Include Files
Tar Error: Unexpected Eof in Archive
Why The Size of an Empty Directory in Linux Is 4Kb
Writing a Syscall to Count Context Switches of a Process
Are System Calls on Windows Inherently Slower Than Linux
Watermarking Video from The Linux Command Line
Trouble with Vagrant - "404 - Not Found"
How to Check Character Encoding of a File in Linux
Starting Ddd with Remote Gdbserver
Does I2C Driver Need to Be Implemented Just Like Any Other Character Device Driver
How Does Linux Support More Than 512Gb of Virtual Address Range in X86-64