开发者

bash scripting challenge

开发者 https://www.devze.com 2022-12-17 16:19 出处:网络
I need to write a bash script that will iterate through the contents of a directory (including subdirectories) and perform 开发者_运维问答the following replacements:

I need to write a bash script that will iterate through the contents of a directory (including subdirectories) and perform 开发者_运维问答the following replacements:

  • replace 'foo' in any file names with 'bar'
  • replace 'foo' in the contents of any files with 'bar'

So far all I've got is

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

:-)


With RH rename:

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


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


UPDATED: 1632 EST

  • Now handles whitespace but 'while read item' never terminates. Better, but still not right. Will keep working on this.

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


#!/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 .


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

remove the 'echo' to commit changes


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


Yet another find-exec solution:

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}"
' \;


correction to find-exec approach by gregb (adding quotes):

# 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'"'; 
...
' \;
0

精彩评论

暂无评论...
验证码 换一张
取 消