Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've got an example of powershell getting verbose here: http://flukus.github.io/2015/03/13/2015_03_13_Powershell-is-... . Ignore the rest and look at the example where I'm trying to copy a directory recursively and with a filter. In bash the same task is a relatively simple one liner:

  find . -type f -name '*.html' -exec cp {} /home/new_dir/{} \;
If you know a simpler way in powershell please post it though.


Well, how about this:

  ls . -r -file -fi *.html | cp -d /home/new_dir/
It is a bit shorter than your bash equivalent, still doing the same thing.

I think you're mistaken about what `cp` (`Copy-Item`) is intended to do. Its main purpose is to copy, not to filter. Yes, `Copy-Item` supports filtering because it's part of the "common parameters", but to be more in line with the cmdlets' original purposes, you should `ls` (`Get-ChildItem`) first, because it has more filtering capability, and then pipe the results to `cp`.

Is it complex? No, actually its complexity is exactly the same as that of your `find` example! `find` is more or less equivalent to `ls` in that both gathers the list of files that meet certain criteria. And then, like `find` invokes `cp` multiple times to do actual copying, `ls` (`Get-ChildItem`) feeds its result to `cp` (`Copy-Item`). They are structured in a similar way.

I'd even say the PS one-liner is more akin to "the UNIX philosophy". In the bash one-liner, there is a direct parent-child relationship between `find` and `cp`, which doesn't utilize pipes at all. Whereas the PS one-liner connects two equivalent processes (`ls` and `cp`) with a pipe. This is exactly what I'd call "small processes work together to get the job done", which is again the UNIX way.


Well yes, that's an example that's relying on the find utility, for which the equivalent in the windows world (at least for file management) is robocopy.

    robocopy /r $pathFrom $pathTo *.html 

If you wanted to exclude say 'main.js' then it'd be:

    robocopy /r $pathFrom $pathTo *.html /xf main.js 
..and /xd is for excluding directories too.

I guess copy-item should be smarter to be able to handle this.

Also, for what it's worth, your example in the linked post is a bit more verbose than it really needs to be. You're comparing a full-up programming language to nant there. So yeah, it's a bit more verbose there. But I bet I can come up with a counter-example where nant is a giant nightmare to get right (or requires just dropping straight to executing external commands) (There's a reason I've killed off all usage of nant years ago, and gone to powershell for build scripts)




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: