Command line to delete matching files and director

2019-01-21 01:30发布

问题:

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

(Sadly DOS only)

回答1:

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, i.e. 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.)



回答2:

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.



回答3:

If your files are in subversion, then doing an export from the repository will give you a directory tree with the .svn files and any other cruft removed.



回答4:

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").



回答5:

On *nix or Cygwin:

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


回答6:

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...