Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to move my files from one directory to other. But there is some issues. My file name pattern is like:

  apple.0, apple.<n>, n -> {0,1,2,3 ...~ }

so mv apple.* will not work, because I need to keep apple.0, which is always the active one.

How do I move them with exceptions (in this case, keeping apple.0)?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

If you have bash and don't care about also matching files like apple.not-a-number, try

shopt -s extglob
mv apple.!(0) /new/directory
share|improve this answer
    
bash: !: event not found is thrown. –  Olgun Kaya Aug 5 '11 at 9:13
    
I am gonna try ls, egrep and mv with pipes. –  Olgun Kaya Aug 5 '11 at 9:15
    
That won't quite work, you cannot use the && operator to combine these. First you need to run the shopt -s extglob option to set the shell to use extended globing mode, then you can use that glob pattern. The way this answer is formatted the shell tries to do the globing on the command line before any of the commands (including the one about enabling the option) are run. –  Caleb Aug 5 '11 at 12:16
    
@OlgunKaya: This will work and match your files just like you want if you first run shopt -s extglob then in any following command or later in your script use that pattern. –  Caleb Aug 5 '11 at 12:17
    
@Caleb Thanks, you are right. I've fixed it but Marcel's answer is still the better one. –  jw013 Aug 5 '11 at 12:18

if you're using bash you should be able to use

mv apple.[^0]* /other/directory/

this will move any files of which the extension does not start with 0 ( "^" at the beginning of [ ] means "not" in bash). If you're sure there's only one character as an ending you could also use

mv apple.[^0] /other/directory/

and if you have to make sure only files which end in numbers you could use

shopt -s extglob
mv apple.@([1-9])*([0-9]) /other/directory/

this would not match apple.01 or similar though....

and a last one which should get all the apple files ending in numbers

shopt -s extglob
mv apple.*([1-9]|[0-9]+([0-9])) /other/directory/
share|improve this answer
    
Cool. I didn't know bash supported regexes. :) –  balki Aug 5 '11 at 12:19
1  
Technically globs aren't quite normal regex. The glob [1-9][0-9]* will actually match any pattern that begins with two digits in the range 10-99 followed by anything, e.g. 23apples would match. –  jw013 Aug 5 '11 at 12:49
    
I got something mixed up (regex vs. globs/expansions as balki and jw023 point out) :-) the last option does not what I explained it does... so I fixed it with new options based on the solution by jw0123. –  Marcel G Aug 5 '11 at 13:56
    
@balki bash and regexes are like husband and wife. –  mr_eclair Aug 5 '11 at 17:17
    
With extglob, you get the power of regexps but in a wildcard syntax. The zsh analog to these bash commands is to use setopt ksh_glob instead of shopt -s extglob, or to use setopt extended_glob then mv apple.*~apple.0 /other/directory/. –  Gilles Aug 6 '11 at 0:02

To be as specific as you want on the file to move and not to move

find /source/directory -maxdepth 1 -name "apple.*" ! -name "apple.0" -exec mv {} /new/directory \;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.