我有一个带有flags(/ flags)的目录,其中多个目录中的文件名为Norway.png。由于没有布维岛旗帜,挪威相当于该国家(岛屿),我只需要将所有Norway.png文件复制到名为Bouvet-Island.png的同一目录。
(以下是旗帜:https://downloads.gosquared.com/pixels/flags.zip)
考虑每个Norway.png位于不同的目录中,它有不同的大小。
我尝试过这样的事情:
#!/bin/bash
echo "Renaming..."
# shell-expansion to loop specified files
for file in /var/www/images/flags/*/Norway.png; do
cp "$file" "Bouvet-Island.png";
done;
但是我收到了这条消息:
cp: target ‘./flags/shiny/16/Norway.png’ is not a directory
cp: target ‘./flags/shiny/48/Norway.png’ is not a directory
cp: target ‘./flags/shiny/24/Norway.png’ is not a directory
cp: target ‘./flags/shiny/32/Norway.png’ is not a directory
cp: target ‘./flags/shiny/64/Norway.png’ is not a directory
cp: target ‘./flags/flat/16/Norway.png’ is not a directory
cp: target ‘./flags/flat/48/Norway.png’ is not a directory
cp: target ‘./flags/flat/24/Norway.png’ is not a directory
cp: target ‘./flags/flat/32/Norway.png’ is not a directory
cp: target ‘./flags/flat/64/Norway.png’ is not a directory
答案 0 :(得分:2)
将find
与-exec
选项一起使用:
find /var/www/images/flags -name Norway.png \
-exec sh -c 'cp {} $(dirname {})/Bouvet-Island.png' \;
答案 1 :(得分:0)
可能是所有目录中都没有Norway.png。您可以添加支票,如下所示 对于/var/www/images/flags/*/Norway.png中的文件;做 if(-f $ file) cp" $ file" "布维-Island.png&#34 ;; 完成的;