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


This question already has an answer here:

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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »