简体   繁体   中英

Command line to delete matching files and directories recursively

How can I recursively delete all files & directories that match a certain pattern? eg remove all the ".svn" directories and the files they contain?

(Sadly DOS only)

Since you're looking for a DOS solution, last week's post was almost identical and the consensus was:

Command line tool to delete folder with a specified name recursively in Windows?

for /d /r . %d in (.svn) do @if exist "%d" rd /s/q "%d"

or

for /f "usebackq" %d in ("dir .svn /ad/b/s") do rd /s/q "%d"

Actually, SVN also gives you the option to export a working directory without the .svn/_svn directories.

Afterthoughts, three years later: I think the reason people end up needing to recursively delete the .svn/_svn folders is because they've directly copied their local working copy to a new location in order to do a folder comparison of their modified version compared to a clean export, ie after something goes awry with the modified local working copy. (At least that's why I've needed it. It's definitely easier/faster to just use 'svn export' when that's possible.)

Is this Unix or Windows? On Unix, an easy solution is

find . -name '.svn' -type d | xargs rm -rf

This searches recursively for all directories (-type d) in the hierarchy starting at "." (current directory), and finds those whose name is '.svn'; the list of the found directories is then fed to rm -rf for removal.

If you want to try it out, try

find . -name '.svn' -type d | xargs echo

This should provide you with a list of all the directories which would be recursively deleted.

如果你的文件是在subversion中,那么从存储库导出将为你提供一个目录树,其中包含.svn文件和任何其他文件。

Something like this may do the trick, but of course be careful with it!

find . -name ".svn" -exec rm -rf {} \;

Try something like this first to do a dry run:

find . -name ".*" -exec echo {} \;

Note that the empty braces get filled in with the file names and the escaped semicolon ends the command that is executed (starting after the "-exec").

在* nix或Cygwin上:

find -name .svn -print0 | xargs -0 rm -rf

If you want to copy it without exporting and eliminating the .svn from the projects, you shold use the /EXCLUDE option from XCOPY.

Like this:

xcopy /S/Q/EXCLUDE:svn.excludelist [path_from] [path_to\]

Observe the "\\" (backslash) on the [path_to]. It determines that it's an output directory, so, xcopy will not question if it's a file or a directory.

The svn.excludelist is a text file containing the patterns to ignore on copy separated by line.

For Example:

.svn
.svn\
\.svn\
.obj
.o
.lib
\src\

And so on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM