在目录和子目录中查找并替换sed,除了少数目录

时间:2015-12-29 08:02:16

标签: shell

我想用目录结构中的bar替换foo,但我想跳过几个文件夹

我正在使用此命令:

find ./ -type f -exec sed -i -e 's/foo/bar/g' {} \;

2 个答案:

答案 0 :(得分:0)

#!/bin/bash # Makes programs, downloads sample data, trains a GloVe model, and then evaluates it. # One optional argument can specify the language used for eval script: matlab, octave or [default] python make if [ ! -e text8 ]; then if hash wget 2>/dev/null; then wget http://mattmahoney.net/dc/text8.zip else curl -O http://mattmahoney.net/dc/text8.zip fi unzip text8.zip rm text8.zip fi CORPUS=text8 VOCAB_FILE=vocab.txt COOCCURRENCE_FILE=cooccurrence.bin COOCCURRENCE_SHUF_FILE=cooccurrence.shuf.bin BUILDDIR=build SAVE_FILE=vectors VERBOSE=2 MEMORY=4.0 VOCAB_MIN_COUNT=5 VECTOR_SIZE=50 MAX_ITER=15 WINDOW_SIZE=15 BINARY=2 NUM_THREADS=8 X_MAX=10 $BUILDDIR/vocab_count -min-count $VOCAB_MIN_COUNT -verbose $VERBOSE < $CORPUS > $VOCAB_FILE if [[ $? -eq 0 ]] then $BUILDDIR/cooccur -memory $MEMORY -vocab-file $VOCAB_FILE -verbose $VERBOSE -window-size $WINDOW_SIZE < $CORPUS > $COOCCURRENCE_FILE if [[ $? -eq 0 ]] then $BUILDDIR/shuffle -memory $MEMORY -verbose $VERBOSE < $COOCCURRENCE_FILE > $COOCCURRENCE_SHUF_FILE if [[ $? -eq 0 ]] then $BUILDDIR/glove -save-file $SAVE_FILE -threads $NUM_THREADS -input-file $COOCCURRENCE_SHUF_FILE -x-max $X_MAX -iter $MAX_ITER -vector-size $VECTOR_SIZE -binary $BINARY -vocab-file $VOCAB_FILE -verbose $VERBOSE if [[ $? -eq 0 ]] then **if [ "$1" = 'matlab' ]; then matlab -nodisplay -nodesktop -nojvm -nosplash < ./eval/matlab/read_and_evaluate.m 1>&2 elif [ "$1" = 'octave' ]; then octave < ./eval/octave/read_and_evaluate_octave.m 1>&2 else python eval/python/evaluate.py fi** fi fi fi fi 要跳过目录:

-prune

...如果您想匹配不需要的目录的完整路径,请使用 find . -type f -o \ \( \( -name unwanted-dir1 -o -name unwanted-dir2 \) -prune -false \) \ -exec sed -i -e 's/foo/bar/g' {} \; 代替-path ./path/to/your-dir ...

答案 1 :(得分:0)

 find . -type f -o    \
     \( \( -name unwanted-dir1 -o -name unwanted-dir2 \) -prune -false \)    \
     -exec sed -i -e 's/foo/bar/g' {} \;

这不起作用,因为它相当于

 find . -type f -o    \
     \( \( -name unwanted-dir1 -o -name unwanted-dir2 \) -prune -false \) -and \
     -exec sed -i -e 's/foo/bar/g' {} \;

因为 - 和 假定省略了运算符 -and 优先级高于 -o ,因此永远不会评估 -exec

如果 -type 测试位于外括号内,则此方法有效:

 find . \( -type f -o    \
         \( -name unwanted-dir1 -o -name unwanted-dir2 \) -prune -false \)    \
        -exec sed -i -e 's/foo/bar/g' {} \;

或者,括号更少:

find . \( -name unwanted-dir1 -o -name unwanted-dir2 \) -prune -o    \
       -type f  -exec sed -i -e 's/foo/bar/g' {} \;

或没有括号,David C. Rankin在评论中提出。