in reply to Removing all directories recursively except one
A few thought-questions and food-for-thought bits here:
(1) Might this be an “XY Problem?” Might the thing that you are “ultimately trying to accomplish,” in fact be a well-known problem that has been addressed already ... say, by a command such as logrotate? “Writing a brand-new program,” in whatever language it might be, is a pretty-cool thing to have done, unless (heh, heh ...) at the end you discover that someone else, a dozen years ago or more, already did it infinitely-better. (Ouch!) Frankly, this requirement smells a lot like just such a situation.
(1a) I say this from “red-faced experience.” ’Nuff said ...
(1b) To make this assessment fairly, you must “drag yourself, kicking-and-screaming, away from How,” and force yourself to stare at What.
(2) If you do find that you need to monkey with a directory-structure in this way, let me strongly advise you to write your code so that it does the job in two distinct stages. First, it should scan the target directory to gather-up a list of contained directories ... remembering (Ouch! #2 ...) to ignore "." and "..". Fully complete the scan, then process your list. In other words, do not attempt to manipulate the directory structure during the scan.
|
---|