如何使用 sed 更改 find 的结果并将结果传递给 cp?

Mic*_*ott 5 sed solaris find macos

在solaris 中,我想将find 命令找到的所有文件复制到稍微不同的路径。以下脚本基本上为 find 找到的每个文件执行 cp。例如:

cp ./content/english/activity1_compressed.swf ./content/spanish/activity1_compressed.swf
cp ./content/english/activity2_compressed.swf ./content/spanish/activity2_compressed.swf
...

#!/bin/bash

# Read all file names into an array
FilesArray=($(find "." -name "*_compressed.swf"))

# Get length of an array
FilesIndex=${#FilesArray[@]}

# Copy each file from english to spanish folder
# Ex: cp ./english/activity_compressed.swf ./spanish/activity_compressed.swf
for (( i=0; i<${FilesIndex}; i++ ));
do
    source="${FilesArray[$i]}"

    # Replace "english" with "spanish" in path  
    destination="$(echo "${source}" | sed 's/english/spanish/')"

    cp "${source}" "${destination}"
done

exit 0;
Run Code Online (Sandbox Code Playgroud)

看起来有点多,我想知道如何在 find 和 cp 命令中使用 sed 来实现相同的目的。我本来希望有以下类似的东西,但显然括号不是改变操作顺序的可接受方法:

find . -name *_compressed -exec cp {} (echo '{}' | sed 's/english/spanish/')
Run Code Online (Sandbox Code Playgroud)

Joh*_*n T 7

有更简单的方法,但为了可移植性,我们可以使用一些分叉和反引号:

找 。-name *_compressed -exec sh -c 'cp {} `echo {} | sed 's/英文/西班牙文/'`' \;

  • 我不确定他的确切环境,但如果所有文件都只在 1 个目录中,他可以在那里`cd`,然后就只有 `cp *_compressed.swf ../spanish` (2认同)