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

  • Jo Miran@lemmy.ml
    link
    fedilink
    arrow-up
    8
    arrow-down
    1
    ·
    23 hours ago

    Let’s see, and please do not execute this “as is” since I am pulling this out of my butt. Assuming directory name is “dir1”, the file to be renamed is “crunk”, and the new name is “chunk”, then… (I assumed bash)

    find dir1 -type f -name 'crunk' -exec bash -c 'mv "$0" "${0/crunk/chunk}"' {} \;
    
      • GaumBeist@lemmy.ml
        link
        fedilink
        arrow-up
        2
        ·
        19 hours ago

        Presumably so the regex in the parameter expansion/replacement works, since you can’t do that to the placeholder {} string that find uses

      • Jo Miran@lemmy.ml
        link
        fedilink
        arrow-up
        2
        arrow-down
        1
        ·
        22 hours ago

        Nice. Could you recreate my mess using it? We all carry bad habits and inneficiencies, so I’m down to learn a cleaner, more efficient way.

      • Jo Miran@lemmy.ml
        link
        fedilink
        arrow-up
        4
        ·
        23 hours ago

        find dir1 – find something within directory “dir1”

        -type f – specifies that what you are looking fir is a file

        -name ‘crunk’ – the file name is ‘crunk’

        -exec bash -c – for the results, execute bash command

        The command executed is move (mv)

        ‘mv “$0” “${0/crunk/chunk}”’ {} ; – move from crunk to chunk

          • GaumBeist@lemmy.ml
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            18 hours ago

            Idk how versed you are on Bash Parameter Expansion or the find command, so I’d like to expand (pun intended) a little more on Jo Miran’s explanation (if you already know, I’ll leave this for others who may not):

            find’s -exec (and -execdir) option takes everything after it until a semicolon — which usually needs to be escaped, so the shell doesn’t accidentally treat it as the command separator special character — as a command to run and arguments to pass to that command. Furthermore, when using the -exec option, find treats all instances of {} as places where it should substitute the files it matched

            So breaking down -exec bash -c 'mv "$0" "${0/crunk/chunk}"' {} \; really just tells find to take the name of a file it found (in this example it would only match dir1/crunk) and put it after the command bash -c 'mv "$0" "${0/crunk/chunk}"'

            So now the command to run looks like bash -c 'mv "$0" "${0/crunk/chunk}"' dir1/crunk

            this command spawns a (sub)shell, bash, tells it to run the next argument as a command, -c, gives it that command to run, mv "$0" "${0/crunk/chunk}", and passes filename as an argument, dir1/crunk

            So now let’s talk shell parameters

            Usually $0 is a special parameter that references the shell (or script) that invoked the command. In the case of using the -c option, bash actually changes $0 to be the argument after the command to run: dir1/crunk

            So now the command looks more like

            mv "dir1/crunk" "${0/crunk/chunk}"

            So let’s finally get to the finish line: shell parameter expansion. Shell parameters (aka shell variables) can be written with curly braces around the name, so $SHELL and ${SHELL} refer to the same thing. But the curly braces can also let the shell know that if it sees certain special characters after the variable’s name, it should do some transformations to the contents of the variable.

            In this case ${0/crunk/chunk} takes the contents of $0, searches for the first instance of the string ”crunk", and replaces it with “chunk” before inserting it into the command.

            So now the final command to run looks like

            mv "dir1/crunk" "dir1/chunk”


            Also worth mentioning that the -name option of find accepts wildcards in its argument.

            I would also recommend using the -execdir option instead of -exec in this specific case, because it will run commands from inside the directories where it finds the files. In this case, that means {} would expand to ./crunk instead of dir1/crunk; this will be relevant in about 3 paragraphs.

            So now you can tweak the command to your needs. If you wanted to find more than one file that, for example, all had a “u” somewhere in the name, you could do so thusly

            find dir1 -name ”*u*"

            And then if you wanted to change the “r” in the filenames to “l”, you could do:

            find dir1 -name "*u*" -type f -execdir bash -c 'mv "$0" "${0/r/l}"' {} \;

            Note that you could not do this with the regular -exec option, as it would try to mv dir1/crunk dil1/crunk and throw an error because that directory (dil1) likely doesn’t exist… and even if it did, you don’t want your command moving files to different directories without your knowledge

            Notice that it also only changed the “r” in dir1 to an ”l”, and left the “r” in “crunk” alone? That’s not a typo on my part, that’s the intended behavior of Shell Parameter Expansion. If you wanted to replace all "r"s in filename, you would have to change the expression to ${0//r/l} (note the double slash)

            Seriously, it’s worth reading that page from gnu.org. Parameter expansion can get incredibly powerful, and it’s much easier to use the right format (${VAR/%r/h}) than trying to combine the most simple ones to achieve the same goal (e.g. DO NOT DO THIS: ${${VAR//r/h}/h/r}; it won’t even work as intended and it’s unnecessarily complex to read)

  • zeppo@lemmy.world
    link
    fedilink
    English
    arrow-up
    4
    ·
    23 hours ago

    You can do that with the find command. Also xargs is a good one to know for stuff like this, but when commands have the capacity to work on a list already like find, you don’t need xargs. You can use find -exec rename -n with a regex perhaps. I’m not a linux system currently and can’t test a snippet, but I’ll check back later if the question is still open.

    • sinextitan@lemmy.worldOP
      link
      fedilink
      arrow-up
      1
      ·
      23 hours ago

      I’ll edit the post w the solution I find but as this can be done many ways, feel free to comment your solution.

  • HaraldvonBlauzahn@feddit.org
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    17 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

  • thingsiplay@lemmy.ml
    link
    fedilink
    arrow-up
    1
    ·
    20 hours ago

    Here is a different approach involving loops instead. This allows for greater control over what happens. Note in this script the rename() function just echos the command without any effect. This is just a demonstration, so you can experiment with the arguments and edit the script first.

    If you have only one argument that is then used to search with find, no slashes in search term is allowed. I didn’t want complicate the script further and left it as it is.

    #!/usr/bin/env bash
    
    # Rename files
    #
    # This script has 3 ways to input filenames.
    #
    # If only one argument is given, then its used as search term to match
    # filenames. If more than one argument
    #
    # is given, then instead searching for files it will rename all given
    # filenames.
    #
    # If no filename is given, then list of files will be read from stdin pipe. In
    # that case combine it with a tool like find.
    #
    # Change the below function rename() to set what to do with each old filename.
    # The default is to echo, so change that first.
    #
    #
    # Examples:
    #
    #   renameclean.sh '*txt'
    #
    #   renameclean.sh 'dir1/random?file.txt' 'dir2/subdir/anotherfile.zip'
    #
    #   find dir -type f -name '*.txt' | renameclean.sh
    
    rename() {
        old="${1}"
        new="${old}"
    
        # Special characters such as ?, * or " need a backslash to match them
        # literally.
        new="${new/\?/REPLACEMENT}"
        new="${new/\*/REPLACEMENT}"
        new="${new/\"/REPLACEMENT}"
    
        echo mv -- "${old}" "${new}"
    }
    
    # If no argument is given, then read list from stdin.
    if [ "${#}" == 0 ]; then
        while read -r file; do
            rename "${file}"
        done
    # If one argument is given, then use it as a search name.
    elif [ "${#}" == 1 ]; then
        find . -type f -name "${1}" -print0 |
            while read -r -d $'\0' file; do
                rename "${file}"
            done
    # If more than one argument is given, then use all arguments as filenames to
    # rename.
    else
        for file in "${@}"; do
            rename "${file}"
        done
    fi
    
    • Jo Miran@lemmy.ml
      link
      fedilink
      arrow-up
      2
      ·
      23 hours ago

      My guess is that he was doing a “find . | grep filename”, which is a way to output a list of all occurrences within that directory structure, hence why they mentioned grep.

  • Björn@swg-empire.de
    link
    fedilink
    arrow-up
    1
    ·
    24 hours ago

    Are you maybe looking for mmv (multimove)?

    It’s hard to tell what you actually want to do.

    But you should not need to use grep to finde specific files. Just bash’s own abilities should be enough.

    For example */*a* would go into every directory and look for files with “a” in them.

    For more complex operations find can also be used.

    • sinextitan@lemmy.worldOP
      link
      fedilink
      arrow-up
      3
      ·
      23 hours ago

      basically I’m mirroring my music library to my new phn and Android shits itself when it encounters ?,*," in filenames. I used to manually remove them but today I decided to actually learn how to automate.

        • sinextitan@lemmy.worldOP
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          17 hours ago

          I didn’t get my music from yt-dlp. and even if the source title contains special chars, yt-dlp uses chars that look the same but aren’t.

          edit: am aware of the feature but the one I mentioned does the job for me

          • thingsiplay@lemmy.ml
            link
            fedilink
            arrow-up
            1
            ·
            18 hours ago

            yt-dlp has an option to rename downloaded files to restrict to ASCII characters (and another option to restrict filename length). I usually use my own wrapper script around yt-dlp, but I guess you already have your own. So for that, you can use the option --restrict-filenames to “Restrict filenames to only ASCII characters, and avoid “&” and spaces in filenames”. See all options with man yt-dlp. Try it out.

            • sinextitan@lemmy.worldOP
              link
              fedilink
              arrow-up
              1
              ·
              17 hours ago

              Ik of yt-dlp being able to rename the media that I download using it. but I didn’t use it to get my music. plus it’s not a prob that I have w yt-dlp. even w/o yt-dlp using their same same but not same chars, Linux doesn’t mind ?,* and " in filenames. but I do like the feature as / does present probs.

      • eleijeep@piefed.social
        link
        fedilink
        English
        arrow-up
        1
        ·
        22 hours ago

        Are you just trying to remove special characters from the filename, or are you also trying to move them to new directories?

        • sinextitan@lemmy.worldOP
          link
          fedilink
          arrow-up
          1
          ·
          21 hours ago

          trying to remove them so that I can transfer to Android. I can use grep to improve my manual process but I’d love for it to be automatic.

      • Björn@swg-empire.de
        link
        fedilink
        arrow-up
        1
        ·
        22 hours ago

        Nice, extra hard because those symbols all have special meanings when searching something. Nice puzzle, I wish I had the energy to solve it right now.

  • whatiswrongwithyou@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    22 hours ago

    | is usually my solution.

    Quoting variables in scripts is my other go to before I actually start tracing what went wrong.