1

I have a query on unix move for files not matching pattern. Example below:

Directory listing

 20150325
 20150326
 20150327
 20150328
 archieve

Now, I want to move all the files not matching 20150328 into archieve directory with a single command. Please help......

Bhabani Shankar
  • 1,267
  • 4
  • 22
  • 41
  • Assuming they're all in the same folder (you haven't said!) `for x in 2015* ; do if [ ! "$x" = "20150328" ] ; then mv $x archieve; fi ; done` – lurker Mar 30 '15 at 13:47
  • http://stackoverflow.com/questions/4612157/how-to-use-mv-command-to-move-files-except-those-in-a-specific-directory – Saty Mar 30 '15 at 13:48

2 Answers2

2

find with the -name parameter and the ! negation operator:

find . -type f ! -name 20150328 -exec mv {} archieve \;

The {} matches the file just found, and the escaped semi-colon terminates the exec'ed command. To exclude multiple files, just repeat the ! -name filename clause

rojomoke
  • 3,765
  • 2
  • 21
  • 30
  • This one worked .. but with some warning message like below: mv: 0653-405 ./archieve/20150327 and archieve/20150327 are identical. for all the files – Bhabani Shankar Mar 30 '15 at 14:12
  • Also how about multiple files. I want to move all the files leaving files with 2 conditions... say .. as per above example I want to keep 20150328 and 20150327. – Bhabani Shankar Mar 30 '15 at 14:16
  • @Bhabani Shankar see my added sentence about multiple exclusions. I'm not sure why you're getting warnings about identical files., Are all your source files in one directory with unique names? – rojomoke Mar 30 '15 at 15:00
1

execute: shopt -s extglob after that do: mv !(20150328) "destination"

Mintri
  • 133
  • 5