LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-17-2013, 06:01 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Angry 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?
 
Old 10-17-2013, 06:27 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,260

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
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.
 
Old 10-17-2013, 06:59 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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>

Last edited by PTrenholme; 10-17-2013 at 07:20 PM.
 
Old 10-17-2013, 10:43 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Code:
shopt -s extglob
mv !(666).nii /target
More info: http://mywiki.wooledge.org/glob
 
1 members found this post helpful.
Old 10-18-2013, 02:55 PM   #5
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Looking for the single command...but thanks for your suggestions
,

shopt -s extglob? What is that?
 
Old 10-18-2013, 04:14 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
enabling extended globing support. shopt allows changing the default options. It happens to be a built in command to bash.
 
Old 10-19-2013, 03:37 PM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by jpollard View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux Heavily Used in the Enterprise by 1999 - And SCO Knew It or Could/Should Have LXer Syndicated Linux News 0 05-11-2011 03:30 PM
Reading ISO/IEC 9899:1999 carbonfiber Programming 4 09-20-2009 03:25 PM
LXer: EU regulating Microsoft like it's 1999 LXer Syndicated Linux News 0 01-17-2009 11:30 PM
Newbie to Debian/Linux, what about O'reilly's book of 1999 bucovaina78 Debian 5 10-22-2008 08:13 PM
Tiny 1999 working with WinXP but not Debian or Xubuntu- why is this? sonichedgehog Linux - Hardware 2 05-03-2008 01:24 PM


All times are GMT -5. The time now is 07:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration