Search code examples
fileunixfindmove

Move files containing X but not containing Y


To manage my backup sync folder, I am trying to come up with a command that would move files beginning with string1* but NOT ending with *string2 from /folder1 to /folder2

What would a command containing such two opposite conditions (HAS and HAS NOT) look like?


Solution

  • #!/bin/bash
    for i in `ls -d /folder1/string1* | grep -v 'string2$'`
    do
       ls -ld $i | grep '^-' > /dev/null # Test that we have a regular file and not a directory etc.
       if [ $? == 0 ]; then
          mv $i /folder2
       fi
    done