4

There are some files and folders containing the word apple.

Like,

Root directory = AppleWorld
 ->AppleStore -> apple.png, appleStore 
 ->StoreOfApple -> apples -> peaches.png apples.jpg

I want to make all apple to orange

So the result must be

Root directory = OrangeWorld
 ->OrangeStore -> orange.png, orangeStore 
 ->StoreOfOrange-> oranges -> peaches.png oranges.jpg`

So, there should be at least 2 operations for apple and Apple to orange and Orange

I tried all rename commands.

For example these

find ./ -execdir rename 's/Apple*/*Orange*/' '{}' \;
find ./ -iname 'Apple*' -execdir mv -i '{}' 'Orange*' \;

but did not work. What can I do?

1 Answers1

4

Method

  • Change directory to the top of the directory tree, that you want to treat (change names of directories and files).

    cd top-of-target-directory-tree
    
  • Then run the following commands, dry run to check and then do it, if the check looks good,

    find . -depth -execdir rename -n 's/Apple/Orange/;s/apple/orange/' "{}" \;
    
    find . -depth -execdir rename 's/Apple/Orange/;s/apple/orange/' "{}" \;
    

Test example

$ find
.
./Apple3
./Apple3/Apple4
./Apple3/apple4.1
./Apple3/Apple4/apple5
./apple1
./apple2
$ find -type d
.
./Apple3
./Apple3/Apple4

$ find -type f
./Apple3/apple4.1
./Apple3/Apple4/apple5
./apple1
./apple2

Dry run

$ find . -depth -execdir rename -n 's/Apple/Orange/;s/apple/orange/' "{}" \; 
rename(./apple4.1, ./orange4.1)
rename(./apple5, ./orange5)
rename(./Apple4, ./Orange4)
rename(./Apple3, ./Orange3)
rename(./apple1, ./orange1)
rename(./apple2, ./orange2)

Do it

$ find . -depth -execdir rename 's/Apple/Orange/;s/apple/orange/' "{}" \;

Check it

$ find
.
./orange1
./Orange3
./Orange3/Orange4
./Orange3/Orange4/orange5
./Orange3/orange4.1
./orange2
sudodus
  • 46,324
  • 5
  • 88
  • 152
  • 2
    Looks good - with GNU find you could use + in place of \; although how much difference it will make is debatable (it still uses at least one invocation per -execdir, IIRC) – steeldriver Jun 07 '18 at 18:38
  • 1
    @steeldriver, thanks for guiding me to this solution :-) – sudodus Jun 07 '18 at 18:41
  • `find . -depth -execdir rename -n 's/Apple/Orange/;s/apple/orange/' "{}" ;

    find . -depth -execdir rename 's/Apple/Orange/;s/apple/orange/;s/APPLE/ORANGE' "{}" ;` i used those and it worked. thank you both :)

    – caner aydin Jun 08 '18 at 08:29
  • 1
    @caneraydin, You are welcome. I'm glad that it works for you :-) – sudodus Jun 08 '18 at 08:34