4

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?

1
  • This might help
    – 50-3
    Sep 6, 2013 at 8:20

2 Answers 2

5
for a in *;do grep -q ERROR_1 "$a" && mv "$a" subfolder_1 || mv "$a" subfolder_2;done

This should work.

0
4

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 *
do
    grep -f ERROR_1 "$f"
    if [ $? -eq 0 ]; then
       mv "$f" subfolder_1
    else
       mv "$f" subfolder_2
    fi
done
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .