1

I have a script where I want to find all direct subdirectories that is purely numerical in a folder and copy them to a specified destination. I have issues to get regexp to work with + and * in find. The folder structure in the example is as follows:

0/
1/
2/
3/
3.02asd/
3a/
4/
44/
45/
451/
452/
453/
4531/
4532/
45321/
45322/
45323/
666aa/
66a/
66aaa/
temp27/

I have gotten this to work with the following commands:

find . -type d -maxdepth 1 -name "[0-9]" | while read f
do
    mv $f $TESTPATH
done

find . -type d -maxdepth 1 -name "[0-9][0-9]" | while read f
do
    mv $f $TESTPATH
done

find . -type d -maxdepth 1 -name "[0-9][0-9][0-9]" | while read f
do
    mv $f $TESTPATH
done

find . -type d -maxdepth 1 -name "[0-9][0-9][0-9][0-9]" | while read f
do
    mv $f $TESTPATH
done

find . -type d -maxdepth 1 -name "[0-9][0-9][0-9][0-9][0-9]" | while read f
do
    mv $f $TESTPATH
done

Not very nice but it works, but this should probably be possible with something like:

find . -type d -maxdepth 1 -name "[0-9]+"

or

find . -type d -maxdepth 1 -name "[0-9][0-9]*"

But it seems that + doesn't work and * is wildcard it seems.

0

1 Answer 1

8

Use -regex instead of -name, e.g.

find . -maxdepth 1 -type d -regex ".*/[0-9]*"
Sign up to request clarification or add additional context in comments.

3 Comments

Becareful, you have to put the -maxdepth 1 option before -type d.
@Daimrod: actually I think it works OK either way around - I just tested it to be sure. Performance is probably better if you put the -maxdepth 1 first though.
Yes both works, but I think it's faster and at least you don't have a warning. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.