user2300940
user2300940

Reputation: 2385

move files that no not contain specific string

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

Upvotes: 2

Views: 1820

Answers (4)

Michael Jaros
Michael Jaros

Reputation: 4681

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).

Upvotes: 3

rblock
rblock

Reputation: 58

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

mv $(ls -I '*S*') /target/dir

Upvotes: 2

Divyanshu Srivastava
Divyanshu Srivastava

Reputation: 1507

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.

Upvotes: 0

alfunx
alfunx

Reputation: 3150

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

Upvotes: 1

Related Questions