News for hackers. Stuff that matters.
Re:I'm “still afraid to use spaces in file names” years old by mtift on Thursday November 11, 2021 @11:37AM (#29189919)
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.
Re:I'm “still afraid to use spaces in file names” years old by tgbugs on Thursday November 11, 2021 @12:45PM (#29190767)
Word of warning from hard experience: rn is a really dangerous thing to name a function because it is one char away from rm.
Re:I'm “still afraid to use spaces in file names” years old by spurgu on Thursday November 11, 2021 @01:24PM (#29191189)
One char away also physically on the keyboard (maybe that's what you meant?).
Re:I'm “still afraid to use spaces in file names” years old by tgbugs on Thursday November 11, 2021 @02:15PM (#29191882)
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.
Re:I'm “still afraid to use spaces in file names” years old by jatone on Thursday November 11, 2021 @07:17PM (#29194932)
laughs in dvorak
Re:I'm “still afraid to use spaces in file names” years old by kataklasm on Friday November 12, 2021 @04:48AM (#29197720)
cries in colemak
Re:I'm “still afraid to use spaces in file names” years old by Extigy on Thursday November 11, 2021 @03:47PM (#29192943)
I once ran “crontab -r” instead of “crontab -e” and also thought that was terrible design for the same reason.
Re:I'm “still afraid to use spaces in file names” years old by post-it on Thursday November 11, 2021 @01:23PM (#29191177)
Looks like it's typically run without any arguments, so it's probably fine.
Re:I'm “still afraid to use spaces in file names” years old by lioeters on Thursday November 11, 2021 @02:13PM (#29191861)
A typo can go the other way, like "rn somefile" where it was meant to remove a file but instead it renames all files.
Re:I'm “still afraid to use spaces in file names” years old by itsbenweeks on Tuesday November 16, 2021 @09:13AM (#29240723)
Agree. Having this function exit if any arguments are passed to it seems like a good safety measure.
Re:I'm “still afraid to use spaces in file names” years old by TheSkyHasEyes on Thursday November 11, 2021 @01:04PM (#29190970)
ren would be better than rn. :)
Re:I'm “still afraid to use spaces in file names” years old by eyelidlessness on Thursday November 11, 2021 @11:14PM (#29196248)
Note to self: snag “notTerseAtAllMoreVerboseIdentifiersForGreatGood.js” on NPM
Re:I'm “still afraid to use spaces in file names” years old by donio on Thursday November 11, 2021 @12:29PM (#29190580)
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.
Re:I'm “still afraid to use spaces in file names” years old by OskarS on Thursday November 11, 2021 @11:59AM (#29190211)
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?

Re:I'm “still afraid to use spaces in file names” years old by michaelt on Thursday November 11, 2021 @12:28PM (#29190576)
> 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...

Re:I'm “still afraid to use spaces in file names” years old by mywittyname on Thursday November 11, 2021 @01:03PM (#29190951)
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.

Re:I'm “still afraid to use spaces in file names” years old by jedimastert on Thursday November 11, 2021 @02:25PM (#29192015)
A better question might be "how did it get there in the first place?"
Re:I'm “still afraid to use spaces in file names” years old by dredmorbius on Thursday November 11, 2021 @03:27PM (#29192724)
Presume all inputs are hostile.

Whether people or processes, something is likely to introduce the character at some point.

Re:I'm “still afraid to use spaces in file names” years old by ggm on Thursday November 11, 2021 @03:30PM (#29192752)
Sw which converts -- and __ on the fly. Same sw converts quote pairs "for your convenience"
Re:I'm “still afraid to use spaces in file names” years old by eyelidlessness on Thursday November 11, 2021 @11:16PM (#29196259)
Opt+- if you use macOS, long press on - if you use any Apple touch OS.
Re:I'm “still afraid to use spaces in file names” years old by mtift on Thursday November 11, 2021 @06:15PM (#29194478)
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.
Re:I'm “still afraid to use spaces in file names” years old by OskarS on Friday November 12, 2021 @05:32AM (#29197925)
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?
Re:I'm “still afraid to use spaces in file names” years old by niccl on Thursday November 11, 2021 @01:38PM (#29191354)
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:

   find . -depth -name '* *' | while IFS= read -r f ; do mv -i "$f" "$(dirname "$f")/$(basename "$f"|tr ' ' _)" ; done
Re:I'm “still afraid to use spaces in file names” years old by BiteCode_dev on Thursday November 11, 2021 @04:59PM (#29193741)
Thanks 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.

Re:I'm “still afraid to use spaces in file names” years old by l0b0 on Thursday November 11, 2021 @06:27PM (#29194584)
If you're a developer you're doing yourself a big disservice by not learning how to deal with special characters.
Re:I'm “still afraid to use spaces in file names” years old by mtift on Thursday November 11, 2021 @06:38PM (#29194672)
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.
Re:I'm “still afraid to use spaces in file names” years old by mrzool on Thursday November 11, 2021 @05:02PM (#29193758)
You might be interested in detox:

https://github.com/dharple/detox

Re:I'm “still afraid to use spaces in file names” years old by theshowmustgo on Thursday November 11, 2021 @04:50PM (#29193643)
Nice 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.
Re:I'm “still afraid to use spaces in file names” years old by mtift on Thursday November 11, 2021 @06:21PM (#29194523)
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.
Re:I'm “still afraid to use spaces in file names” years old by BiteCode_dev on Thursday November 11, 2021 @05:00PM (#29193748)
rename will stop and output and error.
Re:I'm “still afraid to use spaces in file names” years old by Tempest1981 on Thursday November 11, 2021 @12:08PM (#29190318)
Surely you must run into conflicts now and then?
Re:I'm “still afraid to use spaces in file names” years old by nybble41 on Thursday November 11, 2021 @01:44PM (#29191431)
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".)

Re:I'm “still afraid to use spaces in file names” years old by mtift on Thursday November 11, 2021 @06:28PM (#29194592)
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.
Re:I'm “still afraid to use spaces in file names” years old by nybble41 on Friday November 12, 2021 @06:04PM (#29205531)
Without 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.)
Re:I'm “still afraid to use spaces in file names” years old by cerved on Thursday November 11, 2021 @05:22PM (#29194007)
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
Re:I'm “still afraid to use spaces in file names” years old by IX-103 on Thursday November 11, 2021 @06:20PM (#29194514)
You missed ~ You really don't want to create a directory named "~".....