Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a lot of recovered files of which many are invalid even though they appear to be ok by name and extension. This is expected.

Now I to need filter out those which are probably ok. I see to options:

For example, power point files (*.pptx) are actually zip containers that start with PK in the first two bytes. So the command

head --byte 2 filename

outputs PK for most of the good files whereas the bad files don't start with PK.

Question 1: How can I combine head with find to list out the files that match?

Another approach is the file command. It prints

Zip archive data, at least v2.0 to extract

for good power point files but simply

data

for bad files.

Question 2: How can I combine file with find to list out valid files?

There are also other file type but I can augment the technque if I only get the clue :)

Question 3: Are the more obvious ways to do this?

share|improve this question

migrated from stackoverflow.com Oct 10 '11 at 14:54

This question came from our site for professional and enthusiast programmers.

3 Answers 3

With find you could do something like

find . -type f -exec file "{}" + | awk  '/Zip archive data/ {print $1}' | sed 's/:$//'

which will print the filename (removing the trailing :) of files that file identifies as Zip archive data.

share|improve this answer
    
Thanks! awk is also a good solution, however I have found the -printf parameter to make things quite easy. –  marlar Oct 11 '11 at 8:35

I would recommend using grep, since it is ment for searching for text in a file.

I am not certain of the particulars you will need, but this should get you started: http://www.computerhope.com/unix/ugrep.htm

share|improve this answer
    
I know grep quite well, but I can't find an option to anchor the expression to the beginning of the file, only the beginning of line (the ^ operator). –  marlar Oct 10 '11 at 19:02
up vote 0 down vote accepted

I have discovered the -printf parameter to find that makes many things much easier than using -exec. So this command will identify the (probably) good files and move them into a subdirectory called good:

find . -type f -printf "file '%p' | grep 'Zip archive data' && mv '%p' good \n" | sh

For Word and Excel documents you can use the string "Author:" as valid doc files seem to have this attribute.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.