How to remove all .svn directories from my application directories

前端 未结 11 2586
臣服心动
臣服心动 2020-12-04 04:26

One of the missions of an export tool I have in my application, is to clean all .svn directories from my application directory tree. I am looking for a recursiv

11条回答
  •  抹茶落季
    2020-12-04 05:16

    What you wrote sends a list of newline separated file names (and paths) to rm, but rm doesn't know what to do with that input. It's only expecting command line parameters.

    xargs takes input, usually separated by newlines, and places them on the command line, so adding xargs makes what you had work:

    find . -name .svn | xargs rm -fr
    

    xargs is intelligent enough that it will only pass as many arguments to rm as it can accept. Thus, if you had a million files, it might run rm 1,000,000/65,000 times (if your shell could accept 65,002 arguments on the command line {65k files + 1 for rm + 1 for -fr}).

    As persons have adeptly pointed out, the following also work:

    find . -name .svn -exec rm -rf {} \;
    find . -depth -name .svn -exec rm -fr {} \;
    find . -type d -name .svn -print0|xargs -0 rm -rf
    

    The first two -exec forms both call rm for each folder being deleted, so if you had 1,000,000 folders, rm would be invoked 1,000,000 times. This is certainly less than ideal. Newer implementations of rm allow you to conclude the command with a + indicating that rm will accept as many arguments as possible:

    find . -name .svn -exec rm -rf {} +
    

    The last find/xargs version uses print0, which makes find generate output that uses \0 as a terminator rather than a newline. Since POSIX systems allow any character but \0 in the filename, this is truly the safest way to make sure that the arguments are correctly passed to rm or the application being executed.

    In addition, there's a -execdir that will execute rm from the directory in which the file was found, rather than at the base directory and a -depth that will start depth first.

提交回复
热议问题