1

I would like to move files using mv that do not contain the letter S in the filename. Could not find anyhting in the mv manual. Maybe combination with find or grep? It has to be case-sensitive.

input:

  file1
  fileS1
  file2
  fileS2

file to move:

  file1
  file2
user2300940
  • 2,355
  • 1
  • 22
  • 35

4 Answers4

3

You can do the selection in pure Bash without any extra software, if you enable extended globbing, which is off by default:

shopt -s extglob
mv !(*S*) /target/dir

For more information, search for extglob in the bash(1) manual page (the info is at the second match).

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
2

You could also use the Ignore-pattern switch from ls, like:

mv $(ls -I '*S*') /target/dir
rblock
  • 58
  • 5
1

You can use find with the -not flag for example.

find /path/to/source/dir -type f -not -name '*S*' \
    | xargs mv -t /path/to/target/dir
alfunx
  • 3,080
  • 1
  • 12
  • 23
0

GREP's -v flag can also be used here. According to the docs,

-v, --invert-match Invert the sense of matching, to select non-matching lines.

Just use

ls | grep -v '*S*' | xargs mv -t target_dir/

Also, see this post.

Divyanshu Srivastava
  • 1,379
  • 11
  • 24