0

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?

Dominique
  • 16,450
  • 15
  • 56
  • 112
Ramya
  • 9
  • 3
  • 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 Answers3

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
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