I have folders setup like this:/path/to/directory/SLUG_1/SLUG_1 - SLUG_2 - SLUG_3SLUG_2 is a year, and it may have a letter after the year, like 1994 or 2003a.I would like to rename those files to:/path/to/directory/SLUG_1/SLUG_2 - SLUG_3I'm getting pretty close with this command:find $root -mindepth 2 -maxdeth 2 -type d | sed s#\(\(/path/to/directory/[^/]*/\).* - \([0-9]\{4\}[a-bA-B]\? - .*\)\)#mv \1 \2\3# This prints:mv /path/to/directory/SLUG_1/SLUG_1 - SLUG_2 - SLUG_3 /path/to/directory/SLUG_1/SLUG_2 - SLUG_3Which is exactly the command I want to execute. But I can't execute it. I tried assigning the output to a variable and executing it by calling the variable. That didn't work. I tried a few variations on that idea, and I got errors.It feels like I'm missing some tool here. Some iterating tool that makes this job easy. Any suggestions? 
Batch rename folders with a single bash command
bash;command line;find;mv;gnu
Another answer, using GNU find to handle the renaming.  This method is robust regardless of what characters may be in the filename.If I understand your use case rightly, you want to rename directories that start with the full name of their parent directory.  In other words, if your directory is named like so:/some/path/abcdefghi/abcdefghi - something - else/You want to rename it like so:/some/path/abcdefghi/something - else/Since you specify GNU as a tag on this question, you can use the GNU extensions to the find command and handle this like so:find . -mindepth 2 -maxdepth 2 -type d -regextype posix-egrep -regex '.*/([^/]+)/\1[^/]+' -exec sh -c 'new=$(sed -r s:/([^/]+)/\\1 ?-? ?([^/]+)\$:/\\1/\\2: <<<$1); mv $1 $new' find-sh {} \;Test results:[vagrant@localhost test]$ mkdir -p SLUG_1/SLUG_{1\ -\ SLUG_{2..4}\ -\ SLUG_5,7\ -\ something}[vagrant@localhost test]$ find . -type d../SLUG_1./SLUG_1/SLUG_1 - SLUG_2 - SLUG_5./SLUG_1/SLUG_1 - SLUG_4 - SLUG_5./SLUG_1/SLUG_1 - SLUG_3 - SLUG_5./SLUG_1/SLUG_7 - something[vagrant@localhost test]$ find . -mindepth 2 -maxdepth 2 -type d -regextype posix-egrep -regex '.*/([^/]+)/\1[^/]+' -exec sh -c 'new=$(sed -r s:/([^/]+)/\\1 ?-? ?([^/]+)\$:/\\1/\\2: <<<$1); mv $1 $new' find-sh {} \;[vagrant@localhost test]$ find . -type d../SLUG_1./SLUG_1/SLUG_3 - SLUG_5./SLUG_1/SLUG_4 - SLUG_5./SLUG_1/SLUG_2 - SLUG_5./SLUG_1/SLUG_7 - something[vagrant@localhost test]$ 