Use grep to find content in files and move them if they match

前端 未结 9 915
无人共我
无人共我 2020-12-12 13:05

I\'m using grep to generate a list of files I need to move:

grep -L -r \'Subject: \\[SPAM\\]\' .

How can I pass this list to the mv command

相关标签:
9条回答
  • 2020-12-12 13:48

    Work perfect fo me :

    • move files who contain the text withe the word MYSTRINGTOSEARCH to directory MYDIR.

      find . -type f -exec grep -il 'MYSTRINGTOSEARCH' {} \; -exec mv {} MYDIR/ \;

    I hope this helps

    0 讨论(0)
  • 2020-12-12 13:50

    This is what helped me:

    grep -lir 'spam' ./ | xargs mv -t ../spam

    Of course, I was already in required folder (that's why ./) and moved them to neighboring folder. But you can change them to any paths.

    I don't know why accepted answer didn't work. Also I didn't have spaces and special characters in filenames - maybe this will not work.

    Stolen here: Grep command to find files containing text string and move them

    0 讨论(0)
  • 2020-12-12 13:50
    mv `grep -L -r 'Subject: \[SPAM\]' .` <directory_path>
    

    Assuming that the grep you wrote returns the files paths you're expecting.

    0 讨论(0)
提交回复
热议问题