Hacker News new | past | comments | ask | show | jobs | submit login

Got a directory full of files with underscores in the names? Want spaces instead? Easy, type this:

dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "_"} | foreach { $New=$_.name.Replace("_"," ") Rename-Item -path $_.Fullname -newname $New -passthru }

EDIT: I purposely posted the first Google result. You obviously Googled as well as your code got it backward. But really, why isn't it this? It's worked since the 1980s. You'll dig it since it uses extra apostrophes.

    rename '_' ' ' *



Dir | rni –NewName { $_.name –replace " ","_" }

That command does some extra stuff, which would make the bash equivalent also complicated.


Yep. Technically the request included recursive rename, and to avoid renaming directories, but gosh that's not hard:

    dir -rec -file | rni –newname { $_.name –replace "_"," " }
Edit: Oops, request did not include recursive, the original sample solution did.

FWIW top Google results for "unix recursively rename files" give below solutions (slightly different scenarios but you get the idea). Not particularly shorter, or less cryptic:

    find . -iname "*dbg*" -exec rename _dbg.txt .txt '{}' \;

    shopt -s globstar nullglob
    rename _dbg.txt .txt **/*dbg*


His 'rename' wasn't recursive.

<Quote>Y'all are still missing the point. We live in a world where people use CoffeeScript to avoid typing a semicolon. When you Google a PowerShell problem the Google results come back looking insane. You can play code golf here all you want but even this version looks nuts to outsiders

It's a cultural problem. You don't solve it by shouting subtly-differing versions of your culture, louder. ".</Quote>

You're not getting the point. Powershell is just short, and sometimes shorter if you actually learn it.

"When you Google a PowerShell problem the Google results come back looking insane" - This is a problem with the age of powershell. Bash has been around for ever, so there is lots of good quality content on the web.


Y'all are still missing the point. We live in a world where people use CoffeeScript to avoid typing a semicolon.

When you Google a PowerShell problem the Google results come back looking insane. You can play code golf here all you want but even this version looks nuts to outsiders.

It's a cultural problem. You don't solve it by shouting subtly-differing versions of your culture, louder.


That wasn't what I Googled. And Microsoft is announcing bash and Linux shell subsystems today for Windows so y'all are really missing the point. Maybe dev goes one way and ops goes another. But probably not. I'm not betting on Powershell.


"You obviously Googled as well as your code got it backward. " - No, just a simple mistake from writing it on the fly.

This isn't even probably the smallest, somebody smarter than me could do better.

There's probably a PowerShell module that has your equivalent.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: