Removing intermediate directories

We recently needed to remove the intermediate directories in a large number of parent directories. So, if we started with /A/B/C/D, we wanted to end up with /A/B/D. It seemed simple at the time, so I blithely said “Sure, I can write that easily as a shell script”.

Of course, I should’ve known better. The intermediate directories were using company and person names, so they often contained apostrophes, ampersands, dashes, etc. Rather than going a bit nuts trying to coerce the shell to work with these, I switched gears and converted the script to use Python instead. This required a little more work than the Bash script did, but had the desired effect of giving more consistent results. See here for the resulting script:

remove_intermediate_dirs

 

To use it, you’ll need to set to startPath to your parent directory (/A in our example). Also, I’ve saved it with the debugMode flag set, so you’ll need to set that to False when you think it’s safe to do so.

Leave a Reply