Search code examples
pythonregexbashmv

Move files having one or two digits in name at certain point


Let's say I have a folder full of files with a name in the format foo<number1>.bar.o<number2>. <number1> can consist of 1, 2 or 3 digits.

Now if I, for some odd reason, would want to move only the files at which <number1> consists of 1 or 2 digits, but not 3, I would write a python script:

#!/bin/python
import glob, shutil, re
for f in glob.glob("*.bar.o*"):
    print f
    numbers = map(int, re.findall('\d+', f))
    print numbers
    if numbers[0] < 100:
        shutil.move(f, "dir/" + f)

The question now: How can, if, this be done in less code, for example in one line (max 80 characters)?

Would prefer a solution usable in a bash shell.


Solution

  • unless I'm mistaken about what you mean:

    mv foo[0-9][0-9].bar* /wherever/you/like
    

    will catch 2 digit numbers

    For either you could do:

     mv $(ls |egrep "foo[0-9]{1,2}.bar") /wherever/you/like