Remove too deep folders in bash on OSX

125 views Asked by At

a program created folders recursively. it is too deep, the full path string length is longer than the MAX (getconf ARG_MAX), for example: /A/B/C/A/B/C/A/B/C//A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C/A/B/C …… so "sudo rm -fr /A" says "Bad address".

How to create a script to deal with it? Thanks,

2

There are 2 answers

1
Abhi Beckert On

Interesting problem.

I guess you could create a command line tool with Xcode (file -> new project -> command line tool, insert code, then click the "run" toolbar button).

int main(int argc, const char * argv[])
{
  @autoreleasepool {

    NSURL *url = [NSURL fileURLWithPath:@"/a/b/c/d/..."];

    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtURL:url error:&error];
    if (error) {
      NSLog(@"%@", error);
    }

  }

  return 0;
}
0
Mark Setchell On

If it's a separate disk with its own filesystem mounted at /A, unmount it and reformat it.

If not, run something like this (very untested):

cd /A

then

cd A || cd B || cd C && rm -rf A* B* C*

and keep executing it, hitting up arrow to repeat and executing again till it works...

Good luck!