I have a source code which is having text and binary file. I have to find and collect all the human unreadable files present in source code. How I can do this?
Asked
Active
Viewed 860 times
0
-
https://stackoverflow.com/questions/16760378/how-to-check-if-the-file-is-a-binary-file-and-read-all-the-files-which-are-not – Maroun Jun 30 '20 at 11:37
-
I have tried this grep -q ASCII $name && echo $name Binary || echo $name Text . but all png files displaying as a text file. o/p:wear-round-4.png Text mobile-1-list.png Text wear-round-3.png Text menu.png Text main.png Text main.png Text – Ramya Jun 30 '20 at 11:48
3 Answers
1
This piece of code returns a list of all non ascii text files in current directory. Hope this will help:
for i in `find . -type f`; do file $i; done |grep -v text | cut -d : -f 1
You could replace the .
(dot) after the find
with any other location in your filsystem.

Far Had
- 11
- 2
-
Maybe remove `ASCII` so it filters out other text encodings like UTF-8 as well: `$ echo 'hellö wørld' | file -` gives `/dev/stdin: UTF-8 Unicode text`. – Thomas Jun 30 '20 at 11:59
-
Instead of two unquoted expansions, you could do `find . -type f -exec file {} \;` | grep ...` – Benjamin W. Jun 30 '20 at 12:44
-
1
Although the answer of Far Had is correct, you don't even need a for-loop for this. As you state yourself, all your files are within one directory, so you can simply run:
file *
The answers containing "text" (be it ASCII, unicode or something else) indicate human readable files.

Dominique
- 16,450
- 15
- 56
- 112
0
One way is to use perl (File::Find module) like this:
perl -MFile::Find -e '@directories=shift || "."; sub wanted { ! -T && print "$File::Find::name\n"; }; find(\&wanted, @directories);'
NOTE: The above command defaults to searching the current directory. To search a specific directory e.g. /tmp, just type the above command followed by a space and /tmp

LeadingEdger
- 604
- 4
- 7