Ik I can use grep to find filenames with specific characters and then print that to a list but idk how to utilize said list to rename. I found another solution which’s using mv but couldn’t figure out how to make it work w subdirectories. I think using both grep and mv in unison’s the answer just idk how to do it. plz halp

  • HaraldvonBlauzahn@feddit.org
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    9 hours ago

    Two ways:

    1. Say you want to rename the files Sevilla_big{number}.jpg to Sevilla_small{number}.jpg:

    in bash, using the extglob option:

     shopt -s extglob
    for f in pics/**/Sevilla_big*.jpg
    do 
         mv "${f}" "${f/big/small/}"
    done
    

    (If you want to type it in a single line, you need to add semicolons as separators.)

    The ‘"’ are only needed if file names contain whitespaces.

    1. Using find:

      find . -name ‘Sevilla.*big.jpg’ > t

      now, you edit the file named “t” with Emacs or vim so that you duplicate the file names, modify them as you want, and copy them using the copy-rectangle command right to the collumn with the original names. (Vim also offers this functionality, it is super useful to learn!) Delete any names of files you don’t want to change. save the result to t.

    Then, in bash:

     <t while read a b; do mv $a $b; done
    

    Alternatively, you could also insert the mv command as a first collumn in t, save t and do:

    source t
    

    This variant wouldn’t work with spaces in filenames.

    1. Bonus option:

    Use the mmv command