bash脚本挑战

时间:2010-01-22 18:35:23

标签: bash scripting

我需要编写一个bash脚本来遍历目录(包括子目录)的内容并执行以下替换:

  • 使用'bar'
  • 替换任何文件名中的'foo'
  • 使用'bar'
  • 替换任何文件内容中的'foo'

到目前为止,我所拥有的只是

find . -name '*' -exec {} \;

: - )

8 个答案:

答案 0 :(得分:2)

使用RH rename

find -f \( -exec sed -i s/foo/bar/g \; , -name \*foo\* -exec rename foo bar {} \; \)

答案 1 :(得分:2)

find "$@" -depth -exec sed -i -e s/foo/bar/g {} \; , -name '*foo*' -print0 |
while read -d '' file; do
    base=$(basename "$file")
    mv "$file" "$(dirname "$file")/${base//foo/bar}"
done

答案 2 :(得分:1)

更新时间:美国东部时间1632

  
      
  • 现在处理空格,但“读取项目”永远不会终止。更好,   但还是不对。将继续   正在努力。
  •   

aj @mmdev0:〜/ foo_to_bar $ cat script.sh

#!/bin/bash

dirty=true
while ${dirty}
do
    find ./ -name "*" |sed -s 's/ /\ /g'|while read item
    do
        if [[ ${item} == "./script.sh" ]]
        then
            continue
        fi
        echo "working on: ${item}"

        if [[ ${item} == *foo* ]]
        then
            rename 's/foo/bar/' "${item}"
            dirty=true
            break
        fi

        if [[ ! -d ${item} ]]
        then
            cat "${item}" |sed -e 's/foo/bar/g' > "${item}".sed; mv "${item}".sed "${item}"
        fi
        dirty=false
    done
done

答案 3 :(得分:1)

#!/bin/bash
function RecurseDirs
{
oldIFS=$IFS
IFS=$'\n'
for f in *
do
  if [[ -f "${f}" ]]; then
    newf=`echo "${f}" | sed -e 's/foo/bar/g'`
    sed -e 's/foo/bar/g' < "${f}" > "${newf}"
  fi
  if [[ -d "${f}" && "${f}" != '.' && "${f}" != '..' && ! -L "${f}" ]]; then
    cd "${f}"
    RecurseDirs .
    cd ..
  fi
done
IFS=$oldIFS
}
RecurseDirs .

答案 4 :(得分:0)

bash 4.0

#!/bin/bash
shopt -s globstar
path="/path"
cd $path
for file in **
do
    if [ -d "$file" ] && [[ "$file" =~ ".*foo.*" ]];then
        echo mv "$file" "${file//foo/bar}"
    elif [ -f "$file" ];then
        while read -r line
        do
            case "$line" in
                *foo*) line="${line//foo/bar}";;
            esac
            echo "$line"
        done < "$file"  > temp
        echo mv temp "$file"

    fi
done

删除'echo'以提交更改

答案 5 :(得分:0)

for f in `tree -fi | grep foo`; do sed -i -e 's/foo/bar/g' $f ; done

答案 6 :(得分:0)

又一个发现 - 解决方案:

find . -type f -exec bash -c '
path="{}"; 
dirName="${path%/*}";
baseName="${path##*/}";
nbaseName="${baseName/foo/bar}";
#nbaseName="${baseName//foo/bar}";
# cf. http://www.bash-hackers.org/wiki/doku.php?id=howto:edit-ed
ed -s "${path}" <<< $'H\ng/foo/s/foo/bar/g\nwq';
#sed -i "" -e 's/foo/bar/g' "${path}";  # alternative for large files
exec mv -iv "{}" "${dirName}/${nbaseName}"
' \;

答案 7 :(得分:0)

通过gregb(添加引号)修正find-exec方法:

# compare 

bash -c '
   echo $'a\nb\nc'
'

bash -c '
   echo $'"'a\nb\nc'"'
'

# therefore we need

find . -type f -exec bash -c ' 
...
ed -s "${path}" <<< $'"'H\ng/foo/s/foo/bar/g\nwq'"'; 
...
' \;