Elektrine lite

← Feed

@sinextitan@lemmy.world

renaming filenames inside of subdirectories

2026-07-21 12:35 UTC

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

Replies (7)

  • @bjoern_tantau@swg-empire.de 2026-07-21 12:45

    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.

    Open ##3986040

  • @JoMiran@lemmy.ml 2026-07-21 12:50

    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}"' {} \;

    Open ##3986149

  • @tom_s@friendica.ambag.es 2026-07-21 12:50

    @sinextitan@lemmy.world Why don't you use find and rename for this? Much more comfortable.

    Open ##3986156

  • @zeppo@lemmy.world 2026-07-21 12:55

    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.

    Open ##3986221

  • @whatiswrongwithyou@lemmy.ml 2026-07-21 14:37

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

    Open ##3988131

  • @thingsiplay@lemmy.ml 2026-07-21 16:24

    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. #!/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

    Open ##3990231

  • Two ways: 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 Using find: find . -name ‘Sevilla.*big.jpg’ > t now, you edit the file 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. 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 (You could also insert the mv command as a furst collumn in t, save t and do: source t Bonus option: Use the mmv command

    Open ##3992796