10 ms·
I have an overly-aggressive function in my .bashrc to rename all files in the current directory: # Rename all files in a directory rn() { rename "s/ /-
by mtift 5y ago
I have an overly-aggressive function in my .bashrc to rename all files in the current directory:
# Rename all files in a directory
rn() {
rename "s/ /-/g" *
rename "s/_/-/g" *
rename "s/–/-/g" *
rename "s/://g" *
rename "s/\(//g" *
rename "s/\)//g" *
rename "s/\[//g" *
rename "s/\]//g" *
rename 's/"//g' *
rename "s/'//g" *
rename "s/,//g" *
rename "y/A-Z/a-z/" *
rename "s/---/--/g" *
rename "s/---/--/g" *
}
I use this all the time, especially when I download files.
- Tempest1981 5y agoSurely you must run into conflicts now and then?
- nybble41 5y agoThat's the most beautiful part! After running this script there are no more conflicts, because it just silently overwrites all but one version of the "cleaned" filename. (Also—that entire function is super inefficient and could be replaced with a single invocation of "rename".)
- mtift 5y agoTotally inefficient. But for me it's readable and practical. This is mostly just a convenience function for me to help store files in a format I like rather than something I need optimized. If it ever started to feel slow, sure I could optimize. But for now, when I still occasionally download a file that has some weird character and I just prefer to add another line to my function.
- nybble41 5y agoWithout changing the design too much, you could rearrange it like so to avoid renaming multiple times and still have the option to just "add another line": # Rename all files in a directory rn() { rename \ -e "s/ /-/g" \ -e "s/_/-/g" \ -e "s/–/-/g" \ -e "s/://g" \ -e "s/\(//g" \ -e "s/\)//g" \ -e "s/\[//g" \ -e "s/\]//g" \ -e 's/"//g' \ -e "s/'//g" \ -e "s/,//g" \ -e "y/A-Z/a-z/" \ -e "s/---/--/g" \ -e "s/---/--/g" \ * } Though I would at least take advantage of character classes to reduce the number of substitutions: # Rename all files in a directory rn() { rename \ -e 's/[ _—]/-/g' \ -e 's/[:\(\)\[\]",]//g' \ -e "s/'//g" \ -e 'y/A-Z/a-z/' \ -e 's/--+/--/g' \ * } (I'm using the `rename` command provided by the `rename` Debian package, a.k.a `file-rename`. The options may vary if you're using a different version.)
- OskarS 5y agoOverly aggresive is right! I don't know if this is genius or deranged! I'm leaning towards genius and stealing the idea. By the way: what's your beef with en dashes? I mean, if it was "everything should be 'HYPHEN-MINUS' (U+002D)", then fine, but why specifically en dashes and not em dashes?
- michaelt 5y ago> By the way: what's your beef with en dashes? Of all the changes in that list, removing the character that doesn't appear on a standard keyboard seems like the least controversial...
- mywittyname 5y agoTo add, it's a character that gets magically inserted for no reason in various situations. It's up there with those damn angled quotes.
- jedimastert 5y agoA better question might be "how did it get there in the first place?"
- dredmorbius 5y agoPresume all inputs are hostile. Whether people or processes, something is likely to introduce the character at some point.
- ggm 5y agoSw which converts -- and __ on the fly. Same sw converts quote pairs "for your convenience"
- eyelidlessness 5y agoOpt+- if you use macOS, long press on - if you use any Apple touch OS.
- mtift 5y ago
- donio 5y agohttps://github.com/dharple/detox https://github.com/dharple/detox is a nice tool for this. Sane defaults but configurable. In addition to CLI I use it from emacs dired-mode too: (defun my-dired-detox () (interactive) (dired-do-shell-command "detox" nil (dired-get-marked-files)) (revert-buffer)) I bind it to "_" in dired-mode.
- tgbugs 5y agoWord of warning from hard experience: rn is a really dangerous thing to name a function because it is one char away from rm.
- eyelidlessness 5y agoNote to self: snag “notTerseAtAllMoreVerboseIdentifiersForGreatGood.js” on NPM
- TheSkyHasEyes 5y agoren would be better than rn. :)
- itsbenweeks 5y agoAgree. Having this function exit if any arguments are passed to it seems like a good safety measure.
- spurgu 5y agoOne char away also physically on the keyboard (maybe that's what you meant?).
- Extigy 5y agoI once ran “crontab -r” instead of “crontab -e” and also thought that was terrible design for the same reason.
- tgbugs 5y agoYeah, the physical layout is the primary concern. I should have noted that since there is ambiguity because n and m also happen to be next to each other in the alphabet.
- jatone 5y agolaughs in dvorak
- 5y ago
- niccl 5y agoI use this snippet, to change spaces to underscore for directories and files in the current directory and below. Haven't made it a function yet, but should. I got it from stack overflow or somewhere, but no attribution. Thanks to whoever did it first: find . -depth -name '* *' | while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ' ' _)" ; done
- theshowmustgo 5y agoNice but how do you prevent overwrites? What about directories/folders and the files in that directory/folder? I have: Movie Bla (2020) Movie Bla (2020).mp4 But also: Movie_Bla_(2020) Movie_Bla_(2020).mp4 Movie_Bla_(2020).srt Would not like to lose files like the the srt.
- BiteCode_dev 5y agorename will stop and output and error.
- mtift 5y agoYeah, sometimes I end up renaming things I don't want to, but it really doesn't happen all that often. And sometimes I throw caution to the wind, add some excitement to my life, and rename a bunch of files (not for anything professional) in some really old directory and hope I don't break anything. But I'm not aiming for perfect with this comment. I just mentioned in another comment, but the vast majority of times I run this is in my ~/Downloads folder on files I don't really worry about breaking.
- BiteCode_dev 5y agoThanks to all the comments in this threads, I now have "sudo apt install rename detox" in my install script, and: normalize_names() { rename "s/-/_/g" * detox -s lower * } in my .bashrc. I've thrown some edge cases at it, and it handles it super well. It deals with consecutive "_", remove leading garbage, normalize unicode, and even prevents naming conflicts by opting out early. Thanks you.
- mrzool 5y agoYou might be interested in detox: https://github.com/dharple/detox https://github.com/dharple/detox
- cerved 5y agoI wonder if rename has an -e flag like sed. It might be worth baking this into one monolithic regex if you call this often
- IX-103 5y agoYou missed ~ You really don't want to create a directory named "~".....
- l0b0 5y agoIf you're a developer you're doing yourself a big disservice by not learning how to deal with special characters.
- mtift 5y agoI agree. I am a developer and I know how to deal with special characters. But this isn't something I use professionally. I just prefer not to have to deal with special characters in the pdfs, m4as, txts, and other files that I use on a daily basis. When I write papers, I'll write ū or Ñ or ç or whatever (incidentally, I have a lot of shortcuts in my .vimrc for those). I would not say I am "afraid" to use spaces in filenames, but I get a certain satisfaction storing academic papers in the author--paper-title.pdf format and my notes in author--paper-title.md because it helps me find things.