I want to delete folders using regexp in a Mac terminal.0129_0140 (no delete)0140_0140 (delete)0150_0160 (no delete)0170_0170 (delete)I just want to delete folders such as 0140_0140 , 0170_0170 .(Added)I want to delete the nonempty folders, recursively.
Delete folders whose name has 4 digit numbers two times
regular expression
Non-recursiveWith ksh93 (on OS/X available as ksh):rmdir {4}(\d)_\1(beware it could delete a directory called {4}(\d)_\1 if there's no file matching that pattern).With zsh (on OS/X available as zsh):setopt extendedglobrmdir [0-9](#c4)_[0-9]##(/e:'[[ ${REPLY%_*} = ${REPLY#*_} ]]':)(that one also has the benefit of only considering files of type directory, using the / glob qualifier above).With bash or other POSIX shell (like the sh of most systems including OS/X):set -- [0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]for f do  [ ${f#*_} = ${f%_*} ] && set -- $@ $f  shiftdonermdir $@(beware it could delete a directory called [0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9] if there are no XXXX_XXXX files in the current directory).Using find and grep:find . ! -name . -prune -type d -name '[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]' |  grep -x '\./\(.*\)_\1' |  xargs rmdirWith BSD find (as found on OS/X):find . -maxdepth 1 -regex './\([0-9]\{4\}\)_\1' -type d -deleteWith GNU find (as typically not found on OS/X unless installed via macports/homebrew/fink...):find . -maxdepth 1 -regextype grep -regex './\([0-9]\{4\}\)_\1' -type d -deleteRecursively:ksh93:set -o globstarrmdir -- **/{4}(\d)\1(beware that it won't remove 1111_1111 in case there's a 1111_1111/2222_2222 as it will try to remove the 1111_1111 one first which it can't as there's a 2222_2222 dir in it, ksh93 doesn't have the od glob qualifier (for depth-first order) of zsh)zsh:setopt extendedglobrmdir -- **/[0-9](#c4)_[0-9]##(Dod/e@'[[ ${${REPLY:t}%_*} = ${REPLY##*_} ]]'@)BSD find:LC_ALL=C find . -regex '.*/\([0-9]\{4\}\)_\1' -type d -deleteGNU find:LC_ALL=C find . -regextype grep -regex '.*/\([0-9]\{4\}\)_\1' -type d -delete