Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Renaming files with a list

  1. #1
    Join Date
    Dec 2012
    Beans
    9

    Renaming files with a list

    I have a lot of files I'd like to rename automatically. I've been doing it manually and it's tedious. It's a list of pictures, with names. I would like to add a number to the beginning of the filename, which is easy with many programs and commands. I use Thunar for this a lot, for instance. However, this time the numbers are not going to be in numerical order. They are included in a list, matched to the pictures.

    This is a section of the files:
    Culzean Castle 2, Scotland.jpeg
    Culzean Castle 3, Scotland.jpeg
    Culzean Castle 4, Scotland.jpeg
    Desmond Castle, Ireland.jpeg
    Dolbadarn Castle, Wales.jpeg
    Donnington Castle, England.jpeg
    Doonagore Tower 1, Ireland.jpeg
    Doonagore Tower 2, Ireland.jpeg
    Doonagore Tower 3, Ireland.jpeg
    Doonagore Tower 4, Ireland.jpeg

    And these are the new names:
    171Culzean Castle 01, Scotland.jpeg
    218Culzean Castle 02, Scotland.jpeg
    503Culzean Castle 03, Scotland.jpeg
    751Culzean Castle 04, Scotland.jpeg
    189Desmond Castle, Ireland.jpeg
    190Dolbadarn Castle, Wales.jpeg
    191Donnington Castle, England.jpeg
    204Doonagore Tower 01, Ireland.jpeg
    456Doonagore Tower 02, Ireland.jpeg
    635Doonagore Tower 03, Ireland.jpeg
    816Doonagore Tower 04, Ireland.jpeg

    Is there some way to use the second list to rename the first list? It's not a terribly important question, but it does seem to me there should be some way to have the computer do all the hard work.

  2. #2
    Join Date
    Dec 2014
    Beans
    2,604

    Re: Renaming files with a list

    If the names in the second list are the same as the ones in the first list except for the numbers in the beginning, then you don't even need to have the first list for a script to do the renaming.
    Code:
    #!/usr/bin/env /usr/bin/bash
    while read filename ; do
        oldname=${filename##[0-9][0-9][0-9]}
        mv "${oldname}" "${filename}"
    done
    This reads a filename from standard input, removes three digits (and I do mean three digits; if there are less, then nothing will be removed; if there are more, then only three will be removed and the old name generated will probably be wrong...) from the beginning to generate the old name, and then renames the old name to the new one. You'd call this with a redirection, something like 'script < filelist'.

    Holger

  3. #3
    Join Date
    Dec 2012
    Beans
    9

    Re: Renaming files with a list

    Thank you for a quick reply. I think you misunderstood what I want to do. Your script removes three digits. I want to add three digits. If they were consecutive, I could use Thunar and have it done a long time ago. I'm starting with files named as in the first list above. I want to rename them to match the second list above. The main name stays the same, but different, non-consecutive numbers, are to be added at the beginning of the filename. I can do this in a document by concatenation, but I haven't figured out how to rename the actual files.

  4. #4
    Join Date
    Dec 2014
    Beans
    2,604

    Re: Renaming files with a list

    I assumed that you already have both (text) files, the one with the original names and the one with the new names including the numbers. I pointed out that the file with the original names isn't needed if the new names are the same as the original names except for the three leading digits (which I now find to not be the case; there are some changes beyond just the added digits at the beginning, specifically an added '0' in several names). My script removes the digits to (re-)generate the old names and then renames from re-generated old name to the new name as read from the file. If you want to see how it works without any risk, just put an 'echo ' command at the front of the fourth line so that it reads 'echo mv "${oldname}" "${filename}"' and it will print out the 'mv' commands it would do (without the quotes which are necessary because the names contain spaces; those get swallowed by the shell while parsing the arguments).

    I did it that way because it's the simplest solution and more would not be needed if the numbers at the beginning were the only change.

    Code:
    #!/usr/bin/env bash
    declare -a oldnames
    declare -a newnames
    mapfile -t oldnames < $1
    mapfile -t newnames < $2
    if [[ ${#oldnames[*]} != ${#newnames[*]} ]] ; then 
      echo lists are not the same length; aborting
      exit
    fi
    for (( i=0 ; i<${#oldnames} ; i++ )) do
      mv "${oldnames[i]}" "${newnames[i]}"
    done
    This script needs the filenames of the two lists as parameters, the name of the list with the old names first. It expects the lists to have the same number of lines (it checks and aborts if that's not the case) and to be aligned with each other (so whatever is on e.g. line 20 of the file given as the first parameter gets renamed to whatever name is given on line 20 of the file given as the second parameter).

    Holger

  5. #5
    Join Date
    Dec 2012
    Beans
    9

    Re: Renaming files with a list

    Ah, now I see our problem, we're talking past each other. You assume I have two text files and want to make one look like the other. I have only one text file, the one with the new names. The first list above is the actual list of filenames from my directory. I want to rename these files by adding these non-consecutive numbers.

    I assumed there was some simple way to let the computer do this work. I can do it manually, but it's tedious. Thunar lets me add consecutive numbers almost instantly. But it won't handle non-consecutive numbers. I've used computers for decades and Ubuntu for almost half that time. I've just never needed to do something like this. Is there some sort of "for x, do y" thing that'll help me?

  6. #6
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: Renaming files with a list

    This is the question I posed to my AI assistant.

    Consider in Ubuntu viewing a directory of files perhaps with jpeg extensions. There is a separate text list of files where each filename matches a file in the Thunar directory but with an integer prefix of 3 numerals. Suggest a Thunar addon which can run through the directory of files, one by one, and rename to the nearest match in the text list. Indicate an error if files do not match.

    I don't want to keep giving AI assistants results since it might breach terms of usage .. but a Python script is suggested and there is reference to Thunar Bulk Renaming Utility.

    If you are stuck I will gamble and post the advice.

  7. #7
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: Renaming files with a list

    Here is a starter.
    https://docs.xfce.org/xfce/thunar/bu...%20main%20menu.

    This can be automated by referring to a list of files and finding nearest match not exact match.

    Leverage regex expressions.
    Last edited by dragonfly41; 1 Week Ago at 11:28 PM.

  8. #8
    Join Date
    Dec 2014
    Beans
    2,604

    Re: Renaming files with a list

    Yeah, we're talking past each other. My first solution (post #2) did just what you are saying now. It took the names from the file, removed the three digits at the beginning of the (new) name to get the name the file has on disk and then renamed that file to the name it found in the file. The 'mv' command needs the old name of the file to rename it, how else is it going to know what file to work on ? But you said you didn't want to remove digits, you want to add them ...

    Another solution would be to generate a script for the renaming
    Code:
    ls *jpeg|sed 's/^\(.*\)$/mv "\1" "\1"/'>scriptfile
    This script would then have one line per file looking like:
    Code:
    mv "Culzean Castle 2, Scotland.jpeg" "Culzean Castle 2, Scotland.jpeg"
    You would then edit this file in a text editor to add the numbers to the second name (the new name to be given to the file). Once you have edited all the new file names in there and possibly removed the files you don't want to rename, you can run the script either by making it executable with chmod and calling it or by running 'bash scriptfile'.

    If there's some logic to the numbers you want at the beginning of the names, please state it because then it might be possible to generate the numbers automatically; otherwise a boring typing job is what it is.

    Holger
    Last edited by Holger_Gehrke; 1 Week Ago at 12:17 AM.

  9. #9
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: Renaming files with a list

    I don't know the scope of your project but I suspect that you might be better rethinking your indexing plan. Your choice of mixed numeric and text is rather odd and you might be going down a rabbithole.

    I would start from the premise that you need to access web assets across several countries even if they end up in a desktop or printed mode (say using Scribus desktop publication).

    Install Zotero used by professionals.

    Then the assets in Zotero collections can be accessed through public/private collections .. and/or .. within Ubuntu desktop.

    In other words create vectors (hyperlinks) between web assets and desktop assets.

    Your Culzean Castle images (et al) will be held in Zotero private repository. To pull them into desktop apps, leverage Zotero plugins.

  10. #10
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,708

    Re: Renaming files with a list

    How about this:
    Pass your list of required names through this ugly awk command and save the output to required_commands:
    cat required_names | awk '{q="\047"}{new=$0}{sub(/^[0-9]*/,"")}{sub(/\y0+/,"")}{print("mv "q $0 q" "q new q)}' > required_commands
    examine required_commands to see if it's right. If so execute them with
    cat required_commands | bash
    Last edited by The Cog; 1 Week Ago at 07:34 PM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •