How to use mv command to move only selected files? [duplicate]

What is the command to move the all files to target directory except *.trg files?

Tried the below command but it's not working:

mv !(*.trg) tgtdir

You have an extended glob pattern, !(*.trg), which will only work if the extglob shell option is enabled.

As the output of shopt extglob shows:

extglob off

you don't have the option enabled.

So you need to enable extglob by:

shopt -s extglob

Then your command should work.

Also, your command can be made more compact by:

mv -t tgtdir !(*.trg|tgtdir)

Use find with a negated -name argument:

find . ! -name '*.trg' ! -name . -maxdepth 1 -exec mv {} <tgtdir> \;

! -name . excludes current directory and -maxdepth 1 assures only files and directories in the current one will be in the search results.

Just like with plain mv, depending on where your tgtdir exists you might need to exclude it too.