How to run dos2unix for all files in a directory and subdirecty in Powershell
Asked Answered
C

1

3

I can run dos2unix on one file in PowerShell:

dos2unix ./assets/style.css

How to do this for all CSS files under ./assets/ and its subdirectories?

Chevrette answered 31/10, 2014 at 13:53 Comment(0)
S
7
'.\assets' | Get-ChildItem -Recurse -File -Filter '*.css' | ForEach-Object {
    dos2unix $_.FullName
}

Explanation

Get-ChildItem is like dir or ls (in powershell the latter 2 are aliases for that cmdlet). -File means return only files. -Recurse means recurse child directories. -Filter allows us to get only the file pattern desired.

Then we pipe that into ForEach-Object to execute a script block for each file returned and in there, we just execute the dos2unix command.

FullName is the property of the file object that contains the full path to the file.

Scarce answered 31/10, 2014 at 13:56 Comment(4)
Curious use of piping WRT '.\assets'. Why not just Get-ChildItem Assets -Recure -Filter *.css | Foreach {dos2unix $_.FullName}? Note that you don't need to quote *.css. Also, use of -File will only work on PowerShell v3 and later.Conceal
I prefer that style. So I tend to do things like $path | Join-Path -Child Path 'whatever' when possible. I just like the pipeline! Good point about -File.Scarce
Can you tell me how to run it quietly, so it doesn't print output?Chevrette
Try piping it to Out-Null. I'm on mobile now so I can't post a proper example.Scarce

© 2022 - 2024 — McMap. All rights reserved.