Comment by mtift
4 years 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.
Word of warning from hard experience: rn is a really dangerous thing to name a function because it is one char away from rm.
One char away also physically on the keyboard (maybe that's what you meant?).
Yeah, 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.
2 replies →
I once ran “crontab -r” instead of “crontab -e” and also thought that was terrible design for the same reason.
Looks like it's typically run without any arguments, so it's probably fine.
A typo can go the other way, like "rn somefile" where it was meant to remove a file but instead it renames all files.
Agree. Having this function exit if any arguments are passed to it seems like a good safety measure.
ren would be better than rn. :)
Note to self: snag “notTerseAtAllMoreVerboseIdentifiersForGreatGood.js” on NPM
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:
I bind it to "_" in dired-mode.
Overly 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?
> 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...
To add, it's a character that gets magically inserted for no reason in various situations.
It's up there with those damn angled quotes.
A better question might be "how did it get there in the first place?"
2 replies →
Opt+- if you use macOS, long press on - if you use any Apple touch OS.
I totally agree that for some people, this could be a terrible command to have around. However, I know that it has been working for me for about 8+ years or so. I almost always run in in my ~/Downloads folder on files that I don't really care about. I download a lot of academic papers and books, and this just saves me a lot of time to put files in the format I like: author--paper-title.pdf. And that's part of the reason why I make all of the dashes the same, so if I'm opening something by an author, I can easily autocomplete and not have to remember how to make other sorts of dashes on the command line.
For a download folder in particular, this does sound like a great idea. You'd break the list in the browser or whatever, but who cares about that?
I 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:
Thanks to all the comments in this threads, I now have "sudo apt install rename detox" in my install script, and:
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.
If you're a developer you're doing yourself a big disservice by not learning how to deal with special characters.
I 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.
You might be interested in detox:
https://github.com/dharple/detox
Nice but how do you prevent overwrites? What about directories/folders and the files in that directory/folder?
I have:
But also:
Would not like to lose files like the the srt.
Yeah, 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.
rename will stop and output and error.
Surely you must run into conflicts now and then?
That'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".)
Totally 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.
1 reply →
I wonder if rename has an -e flag like sed. It might be worth baking this into one monolithic regex if you call this often
You missed ~ You really don't want to create a directory named "~".....