1

I'm making a game to help learn commands and I have directories that will all have the same number of files of different types in them. I need to be able to change part of the file name in each directory so it signifies that it is in deed a file from that directory.

e.g,

Dir 1 name "Orange" file 1 name: "water_orange.jpeg" file 2 name: 1_water_orange.jpeg" etc, etc 15 files

Dir 2 name "Indigo" file 1 name: "water_indigo.jpeg" file 2 name: "1_water_indigo.jpeg" etc etc 15 files

I have 7 directories with 15 files in each and I want to see if it's possible to change the colour part of the file name in each directory, without have to use the "mv" command on each individual file.

I've tried the "echo sed" command with both single and double quotes but no luck

i.e. echo sed "s/orange/indigo/g" and echo sed 's/orange/indigo/g'

any help sincerely appreciated

1
  • see the man pages for rename.ul and rename – Rinzwind Nov 16 '16 at 20:38
0

Something like the following should work:

for i in $(ls *.jpeg)
do 
mv -v $i $(ls $i | sed 's/orange/indigo/')
done

But this very clumsy syntax can be enormously improved, as steeldriver has suggested in the comments below, with the following elegant syntax:

for i in *.jpeg; do mv -v -- "$i" "${i/orange/indigo}"; done

Bear in mind that both of these examples are for single directories, and are not recursive...

5
  • Thank you so much. I've no idea what your code means at this point but in time I'm hopeful. On with the Games!!! Cheers – Edmuntu Nov 16 '16 at 21:18
  • 1
    IMHO there is no need ever to use for i in $(ls *.jpeg). Using for i in *.jpeg; do mv -v -- "$i" "${i/orange/indigo}"; done would be more efficient (avoiding THREE unnecessary external program calls) and would also work for files with whitespace in their names. – steeldriver Nov 16 '16 at 23:21
  • @steeldriver Indeed my answer is more than a little ugly! I shall remove it so your own more elegant answer can be placed... Oops looks like I cannot... – andrew.46 Nov 17 '16 at 1:11
  • @andrew.46 feel free to edit yours and I will upvote it – steeldriver Nov 17 '16 at 1:21
  • @steeldriver All done, and thanks for the syntax which I am using extensively for my own use :) – andrew.46 Dec 29 '16 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.