How to spot code that are malicious?

I lost some important files on my Library folder. Is it possible that I caught a virus? How can I identify codes that are malicious and which are not? Is it possible to prevent it? If so, what steps do I need to take? Are there any programs that can destroy this?
