4

Well to put it simply, I have duplicate files in a folder, with this form:

file.ext
file(1).ext
file(2).ext
file(3).ext
otherfile.ext
otherfile(1).ext
otherfile(2).ext
...

I want to move only file.ext and otherfile.ext to another folder. Is it possible to do it in bash?

I thought that maybe awk would be helpful?

4

In bash:

shopt -s extglob # activates extended pattern matching features
mv !(*\(+([0-9])\)).ext /path/to/target/

The regular expression matches all files, that don't end with (n).ext, where n is one or more numbers: +([0-9]).

You can check it with echo:

echo !(*\(+([0-9])\)).ext 

Prints:

file.ext otherfile.ext
  • extglob is not regex. – Arthur2e5 Sep 18 '15 at 21:45
  • @Arthur2e5 It isn't what's usually called “regex”, but it is a regular expression, with a different syntax from the usual regex syntax. Well, except that the ! operator isn't a regular expression feature. – Gilles Sep 18 '15 at 22:18
3

It depends. If we can assume that any file whose name contains ( should be ignored, you could just do:

shopt -s extglob ## turns on fancy glob patterns
mv !(*\(*) /path/to/target

If you can have other names that contain ( or if you need to only move those files which are duplicated, you can do:

for f in *\(*; do 
    ## Does this file have an original?
    [[ -e "${f%%(*}.${f##*.}" ]] && 
        ## Move the original
        mv "${f%%(*}.${f##*.}" /path/to/target; 
done
-1

Try something like that:

$ ls  *ext | grep -P '[^)]\.ext' | xargs -iXXxxFILEXXxx mv XXxxFILEXXxx target/

grep will filter out files with brackets in names and xargs will run command mv on results. The ugly XXxxFILEXXxx label is for placing file names as mv argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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