LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH mv *.nii all but 1999.nii ?? (https://www.linuxquestions.org/questions/linux-newbie-8/bash-mv-%2A-nii-all-but-1999-nii-4175481216/)

Drigo 10-17-2013 05:01 PM

BASH mv *.nii all but 1999.nii ??
 
So Lets say I have this files in a folder:

123.nii
132.nii
343.nii
454.nii
666.nii
767.nii
.
.
.

I want to move all the one in *.nii to a different folder but 666.nii ?

mv ./*.nii /<OTHER LOCATION> but 666.nii?? How do I do it?

suicidaleggroll 10-17-2013 05:27 PM

I'm sure there's a single command that could do this with ease, probably using some combination of find with "! -name", but whenever I need to do this I just go the simple route

Code:

mkdir temp
mv 666.nii temp
mv *.nii /some/other/dir
mv temp/* .
rmdir temp

Just takes a few seconds, and there's no complicated command to try to remember or get the syntax right.

PTrenholme 10-17-2013 05:59 PM

for f in *.nii;do [ "${f}" != "666.nii" ] && mv ${f} <destination>;done

Proof of concept, using rm instead of mv:
Code:

$ for ((i=1;i<10;++i)) do touch ${i}${i}${i}.nii;done
$ ls *.nii
111.nii  222.nii  333.nii  444.nii  555.nii  666.nii  777.nii  888.nii  999.nii
$ for f in *.nii;do [ "${f}" != "666.nii" ] && rm ${f};done
$ ls *.nii
666.nii
$ rm 666.nii

Warning: The code above assumes that none of the .nii file names contain any character listed in ${IFS}. (e.g., blank, etc.)


<edit>
This should also work: find ./ -maxdepth 1 -name '*.nii" ! -name '666.nii' -exec mv '{}' <destination> ';' and, by removing the -maxdepth 1 directive, you could apply the logic to your current directory and all subdirectories of it.
</edit>

<edit2>
Another thought: If your directory only contains *.nii files, you could rename the directory (mv can do that), and then recreate the directory and move the file(s) you want back into it.
</edit>

TobiSGD 10-17-2013 09:43 PM

Code:

shopt -s extglob
mv !(666).nii /target

More info: http://mywiki.wooledge.org/glob

Drigo 10-18-2013 01:55 PM

Looking for the single command...but thanks for your suggestions :)
,

shopt -s extglob? What is that?

jpollard 10-18-2013 03:14 PM

enabling extended globing support. shopt allows changing the default options. It happens to be a built in command to bash.

TobiSGD 10-19-2013 02:37 PM

Quote:

Originally Posted by jpollard (Post 5048269)
enabling extended globing support. shopt allows changing the default options. It happens to be a built in command to bash.

Exactly. Since how globbing works is dependent on the shell in use here an example for Zsh, which I personally find more intuitive:
Code:

setopt extendedglob
mv *.nii~666.nii /target



All times are GMT -5. The time now is 10:31 PM.