Sunday, 29 July 2018

linux - Multiple directories: Powershell equivalent of "mkdir dir{1..9}"?


What is the syntax to create multiple directories with PowerShells md (or mkdir, New-Item...) equivalent to the 'nix command mkdir ch{1..9} i.e.


~/parent_dir/  
ch1/
ch2/
ch3/
ch4/
ch5/
ch6/
ch7/
ch8/
ch9/

I've looked in the man pages and get-help for examples, but I do not know the syntax for PowerShell to do such a simple thing. Thank you.



Answer



What is the syntax to create multiple directories with PowerShell


Use the following command:


0..9 | foreach $_{ New-Item -ItemType directory -Name $("ch" + $_) }

How it works:



  • 0..9 the range operator .. generates the sequence of numbers 0, 1, ... 9

  • the numbers are pipelined | to the next command

  • foreach loops (through each number in turn)

  • { ... } is a script block

  • New-Item -ItemType directory -Name $("ch" + $_) creates the directories

  • $_ is an automatic variable that represents the current object in the pipeline (the number)


Example:


> 0..9 | foreach $_{ New-Item -ItemType directory -Name $("ch" + $_) }


Directory: F:\test


Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 25/09/2016 14:57 ch0
d----- 25/09/2016 14:57 ch1
d----- 25/09/2016 14:57 ch2
d----- 25/09/2016 14:57 ch3
d----- 25/09/2016 14:57 ch4
d----- 25/09/2016 14:57 ch5
d----- 25/09/2016 14:57 ch6
d----- 25/09/2016 14:57 ch7
d----- 25/09/2016 14:57 ch8
d----- 25/09/2016 14:57 ch9

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...