No announcement yet.

Batch matching and renaming similar filenames via bash?

  • Filter
  • Time
  • Show
Clear All
new posts

    Batch matching and renaming similar filenames via bash?

    I have several folders with 10 files in it. Their names are specific to their content.
    I have another set of folders with files of the same content but at a higher resolution (video files) and similar names - same at the beginning but with additional characters at the end.
    I want to rename the second set of file to the same names as the first set of files but don.t want to manually copy and paste a bizillion times.

    So something like this:

    file 1xxx
    file 2xxx
    file 3xxx

    file 1
    file 2
    file 3

    Becomes this:
    file 1xxx
    file 2xxx
    file 3xxx

    and so on. So the "file 1" part remains the same but "xxx" is matched to to the correct file and added on to the name.

    I'm pretty sure I can do it in bash, but wondering if someone (<clearing throat> JLittle ) knows a short cut or special trick I don't know.

    More specifically, the first 23 characters of the two file sets' names are the same. The "xxx" part on the first set is of varying length and there are spaces in both sets of file names.

    The full file names look like:
    "Series name - s01e01 - Episode Name.mp4"

    and the target files look like:
    "Series name - s01e01 - .mp4"

    So I want to match the first 23 characters, then rename the file to the full name.

    I could totally do this easily in postgresql but my bash-foo is not so strong...
    Last edited by oshunluvr; Jan 20, 2024, 02:10 PM.

    Please Read Me

    OK, this works but it's REAL ugly:

    for L in *.mp4; do
    X=`echo $L | cut -c 1-23`
    mv ../dir2/"$X".mp4 ../dir2/"$L"

    The script goes in the directory holding the names and "dir2" is the directory to rename. No error checking or any kind of checking.

    Please Read Me


      Originally posted by oshunluvr View Post
      No error checking or any kind of checking.
      But you have btrfs, so....
      Windows no longer obstructs my view.
      Using Kubuntu Linux since March 23, 2007.
      "It is a capital mistake to theorize before one has data." - Sherlock Holmes


      • oshunluvr
        oshunluvr commented
        Editing a comment
        YES! Snapshots are a real thing! LOL

      My advice with bulk renaming tasks is to generate a list of mv commands, perhaps even thousands of them. One makes mistakes when generating the list, whereas making a mistake with mv in a loop can easily, silently, trash lots of files, in whatever scripting language one uses. After generating the list, one checks it, then checks again. After the checks, one just sources the list.

      I'm usually giving this advice too late; the last such time was only a few months ago.

      At least, using bash, one can initially code the loop using echo mv instead of mv.

      In generating the list, I usually use my text editor, starting with ls > changes.txt. Some block commands and a search and replace might take seconds.

      Instead of
      X=`echo $L | cut -c 1-23`
      one can use ${L:0:23}, no need for an intermediate variable, and orders of magnitude faster.
      Regards, John Little


        Cool, thanks John. I hoped you'd have a comment

        I agree that looping something potentially destructive is a bad idea. I tested my script on 3 files that I duplicated before I ran it "live" to be sure all was well. I had 40 files in 4 folders that I needed to rename this way and did NOT want to manually do that many.

        I really discovered the power of scripting tasks when I went to work with a postgresql database. The company provided a GUI front end to the database with over 200 identical schemas. But as you might expect, it was onerous to do almost anything because the GUI made you select a field to edit - one at a time.

        By learning some postgresql and using PGAdmin I was eventually able to make tens of thousands of edits within milliseconds. It really became enjoyable to take a task that could have taken weeks to complete and have it done in less than a few hours of script writing. However, I always had in mind the possibility of a typo wreaking havoc - and it did on many occasions. I was smart enough to make schema backups before hitting the "run" button!

        I'm not near as well versed in Bash as I'd like but each task that I can automate leaves me with some new knowledge that I can take forward.

        Thanks again for your input. It's much appreciated.

        Please Read Me