Non-recursive
With 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 extendedglob
rmdir [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"
shift
done
rmdir "$@"
(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 rmdir
With BSD find
(as found on OS/X):
find . -maxdepth 1 -regex './\([0-9]\{4\}\)_\1' -type d -delete
With 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 -delete
Recursively:
ksh93
:
set -o globstar
rmdir -- **/{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 extendedglob
rmdir -- **/[0-9](#c4)_[0-9]##(Dod/e@'[[ ${${REPLY:t}%_*} = ${REPLY##*_} ]]'@)
BSD find
:
LC_ALL=C find . -regex '.*/\([0-9]\{4\}\)_\1' -type d -delete
GNU find
:
LC_ALL=C find . -regextype grep -regex '.*/\([0-9]\{4\}\)_\1' -type d -delete
find
command, but thefind
on OS/X is not GNUfind
. – Stéphane Chazelas Feb 10 '16 at 10:23