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)
有更简单的方法,但为了可移植性,我们可以使用一些分叉和反引号:
找 。-name *_compressed -exec sh -c 'cp {} `echo {} | sed 's/英文/西班牙文/'`' \;
| 归档时间: |
|
| 查看次数: |
2552 次 |
| 最近记录: |