ファイルの内容、ファイル名、ディレクトリ名、許可されたスペースの「大規模置換」のためのBash機能

ファイルの内容、ファイル名、ディレクトリ名、許可されたスペースの「大規模置換」のためのBash機能

私のUbuntuシステムでは、ファイルの内容、ファイル名、ディレクトリ名を「ほとんど置き換える」ことがよくあります。たとえば、ソースコードを別のアプリケーションのテンプレートにコピーする場合です。

~/.bashrc動作しますが、検索または置換文字列にスペースが含まれていると失敗する関数を設定しました。私はこれが原因だと信じていますsedこのコマンドはスクリプトでスペースを許可しません。CDパスにスペースが含まれている場合、パス変数の変更も失敗します。

パラメータは、($ 1)ディレクトリ、($ 2)検索テキスト、($ 3)代替テキストです。

すべてのパラメータにスペースが含まれるようにこのスクリプトを改善できますか?

deepreplace() {


    if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]
    then
        echo "A parameter is missing"
    else

        cd $1

        vfind=$2
        vreplace=$3 

        # Replace the string in file

        find . -type f -exec sed -i "s/$vfind/$vreplace/g" {} +

        # Replace the string in file names, then directories


        find . -type d -name "*$vfind*" | while read f; do mv $f $(echo $f | sed "s/$vfind/$vreplace/g"); done
        find . -type f -name "*$vfind*" | while read f; do mv $f $(echo $f | sed "s/$vfind/$vreplace/g"); done
    fi
}

ベストアンサー1

コメントで述べたように、純粋なbashソリューションは私のユースケースには適していないと確信しています。

私はNode.js / npmに依存するコマンドを選択しました(NodeはUnixではなく(Windows / Mac)を含む私が作業している環境で非常に一般的だからです)。

2つのnpmパッケージによって異なります名前変更そして変える

これらのライブラリを使用すると、正規表現やその他の高度な名前変更/交換シナリオをサポートするという利点があります。

Node.js/npmをインストールした後:

# Globally install the required packages (this prevents npx from trying to install in current directory if they do not already exist)

$ npm install -g renamer
$ npm install -g replace

# Set variables (dir, find, replace) for directory, find and replace strings, then run change directory, rename and replace, before reverting to original directory.
# npx is not strictly required, but have included to prevent collisions with command names on the PATH.

$ dir="./example_dir" && 
   find="example_find" && 
     replace="example_replace" && 
       cd "$dir" && 
        npx renamer --find "$find" --replace "$replace" "**" && 
            npx replace "$find" "$replace" . -r 
                && cd -

同等の.bashrc関数は次のとおりです。

renamereplace() {

    # Install global packages if not installed

    npm list -g renamer || npm install -g renamer
    npm list -g replace || npm install -g replace

    # Alias positional arguments

    dir="$1"
    find="$2"
    replace="$3" 

    # Change to replace directory

    cd "$dir"

    # Rename in directory and file names

    npx renamer --find "$find" --replace "$replace" "**" 

    # Replace in file contents

    npx replace "$find" "$replace" . -r 

    # Revert current directory

    cd -
}

以下から呼び出すことができます。

$ renamereplace "./example_dir" "example_find" "example_replace"

注意:データの損失を防ぐには、a)このスクリプトを理解し、b)このコマンドが実行されるディレクトリを確認し、c)上記のコマンドを実行する前に、すべての重要なデータをバックアップしていることを確認してください。

おすすめ記事