The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using Rename to move files snufse UNIX for Dummies Questions & Answers 1 04-09-2008 09:57 AM
how to move files monicasgupta Shell Programming and Scripting 1 03-14-2008 12:53 PM
move files uniksbro UNIX for Advanced & Expert Users 4 07-31-2007 08:42 AM
move files ust Shell Programming and Scripting 5 07-03-2005 11:41 PM
move files mta UNIX for Dummies Questions & Answers 2 01-16-2004 09:22 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-01-2007
Registered User
 

Join Date: Nov 2007
Posts: 2
How to move all files except one?

Hello,

I have to move all files, except one, from one directory to another. I tried it with mv and find:

mv -f $(find $SOURCE -maxdepth 1 ! -name "FileNotToMove") $TARGET

It nearly worked out, but unfortunately not only the content of the $SOURCE directory got moved, but also the directory itself. I am just a beginner and don't have any ideas left how to do it in a proper way.

Last edited by Kim Walisch; 01-21-2008 at 06:45 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-01-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,493
Code:
mv -f $(find $SOURCE -maxdepth 1 ! -name "FileNotToMove" -type f) $TARGET
There is one problem with this - if the number of files you find is huge or if the files have spaces in the names you will get problems. If you exceed ARG_MAX (max number of bytes as an argument) you will get an error. If the file has a space in the filename mv will think it is two different files.
Reply With Quote
  #3 (permalink)  
Old 11-01-2007
Registered User
 

Join Date: Nov 2007
Posts: 2
Ok, so I cannot use this variation. Is there an other way to do it, without using for or while and comparing each filename?
Reply With Quote
  #4 (permalink)  
Old 11-02-2007
Registered User
 

Join Date: Feb 2002
Posts: 16
i think you could use :

find $SOURCE -type f ! -name "FileNotToMove" -exec mv {} $TARGET \;

You will not have the ARG_MAX problem.

Hope it helps

Olivier
Reply With Quote
  #5 (permalink)  
Old 11-02-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
bash:
Code:
shopt -s extglob
mv "$SOURCE"/!(FileNotToMove) "$TARGET"
ksh:

Code:
mv "$SOURCE"/!(FileNotToMove) "$TARGET"
zsh:

the same as bash, only change shopt ... to setopt kshglob
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0