1

I have some folders in unix, lets say aa, ab, ac and so on. I have subfolders inside these folders. They are numbered like 100, 200 and so on. I want to delete some sub folders in each of these main folders. The sub folders to be deleted must be greater than a specific number(say anything above 700) How can I do this using a script? Please help.

3 Answers 3

2

I would use the find command. You can do something like this:

find . -name '[7-9][0-9][0-9]' -execdir echo 'rm -vr' {} +

Of course, you may need to tweak the pattern to hit the right names, but I would need more information to help with that.

Sign up to request clarification or add additional context in comments.

2 Comments

this won't work with directory names with more than 3 digits e.g. 9999
@dogbane hence my comment about needing more detail to tweak the pattern name ;)
0
#!/bin/bash

if [ $# -ne 2 ]
then
  echo Usage: $0 searchdir limit
  exit 1
fi

searchdir="$1"
limit="$2"
find $searchdir -type d |
egrep "/[0-9]+$" |
while read dirname
do
  let num=`basename "$dirname"`
 if [ $num -ge $limit ]
 then
    echo rm -rf "$dirname"
 fi
done

Run with: ./script.sh dirtosearch thresholdfordelete

When you're sure it's ok, remove the echo before rm -rf

Comments

0

You can do it all using find.

In the following command, find passes the files to sh which checks if they are >700 and if so echoes out a delete. (You can obviously remove the echo if you really want to delete.)

find . -type d -regex "^.*/[0-9]+$" -exec sh -c 'f="{}";[ $(basename "$f") -gt 700 ] && echo "rm -rf $f"' \;

Comments

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.