简体   繁体   中英

How to copy all files Norway.png in the same directory recursively with the name Bouvet-Island.png in Linux

I have a directory with flags (/flags), where in multiple directories are the filename Norway.png. Because the Bouvet Island flag is not present, the Norway is equivalent for that country (Island), simply I need to copy all Norway.png file to the same directory with the name Bouvet-Island.png.

(Here are the flags: https://downloads.gosquared.com/pixels/flags.zip )

Consider each Norway.png is in different directories and it has different sizes.

I tryied something like this:

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

But I get this messages:

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

Using find with -exec options:

find /var/www/images/flags -name Norway.png \
    -exec sh -c 'cp {} $(dirname {})/Bouvet-Island.png' \;

May be the Norway.png is not there in all the directories. You can add a check as below for file in /var/www/images/flags/*/Norway.png; do if (-f $file) cp "$file" "Bouvet-Island.png"; done;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM