Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a few hundreds of output files, of which some contain error messages. Lets say I want to automatically put all files containing the string "ERROR_1" in a "subfolder_1", and the rest in a second "subfolder_2". This should be possible by piping grep somehow. What would be a useful syntax?

share|improve this question
This might help – 50-3 Sep 6 '13 at 8:20
up vote 5 down vote accepted
for a in *;do grep -q ERROR_1 "$a" && mv "$a" subfolder_1 || mv "$a" subfolder_2;done

This should work.

share|improve this answer

This script will move the files into the subfolders. Modify the wildcard * as required (e.g. *.log) - this code will search all files in the current directory

for f in *
    grep -f ERROR_1 "$f"
    if [ $? -eq 0 ]; then
       mv "$f" subfolder_1
       mv "$f" subfolder_2
share|improve this answer

Your Answer


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.