bash 如何对除 .svn 目录下的文件外的所有文件递归运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2228039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:55:43  来源:igfitidea点击:

How to run a command recursively on all files except for those under .svn directories

bashunixshellfind

提问by zr.

Here is how i run dos2unix recursively on all files:

这是我在所有文件上递归运行 dos2unix 的方法:

find -exec dos2unix {} \;

What do i need to change to make it skip over files under .svn/ directories?

我需要更改什么才能跳过 .svn/ 目录下的文件?

回答by Paul R

Actual tested solution:

实际测试的解决方案:

$ find . -type f \! -path \*/\.svn/\* -exec dos2unix {} \;

回答by pixelbeat

Here's a general script on which you can change the last line as required. I've taken the technique from my findreposcript:

这是一个通用脚本,您可以根据需要更改最后一行。我从我的findrepo脚本中获取了该技术:

repodirs=".git .svn CVS .hg .bzr _darcs"
for dir in $repodirs; do
    repo_ign="$repo_ign${repo_ign+" -o "}-name $dir"
done

find \( -type d -a \( $repo_ign \)  \) -prune -o \
     \( -type f -print0 \) |
xargs -r0 \
dos2unix

回答by Tore Olsen

Just offering an additional tip: piping the result through xargs instead of using find's -exec option will increase the performance when going through a large directory structure if the filtering program accepts multiple arguments, as this will reduce the number of fork()'s, so:

只是提供一个额外的提示:如果过滤程序接受多个参数,通过 xargs 管道而不是使用 find 的 -exec 选项将提高性能,因为这将减少 fork() 的数量,所以:

find <opts> | xargs dos2unix

查找 <选项> | xargs dos2unix

One caveat: piping through xargs will fail horribly if any filenames include whitespace.

一个警告:如果任何文件名包含空格,通过 xargs 管道将严重失败。

回答by ghostdog74

find . -path ./.svn -prune -o -print0   | xargs -0 -i echo dos2unix "{}" "{}"

if you have bash 4.0

如果你有 bash 4.0

shopt -s globstar
shopt -s dotglob
for file in /path/**
do
  case "$file" in
    */.svn* )continue;;
  esac
  echo dos2unix $file $file
done

回答by yogsototh

In bash

在 bash 中

for fic in **/*; dos2unix $fic

Or even better in zsh

甚至在 zsh 中更好

for fic in **/*(.); dos2unix $fic