Using the terminal to run similar things on multiple folders

Using the terminal to run similar things on multiple folders

·

3 min read

We often find ourselves doing similar/repetitive tasks on the computer which can get real tedious real fast😫.

So today let's start with something really simple and get the base set up so that we can build and shape this into whatever we want later😉.

Windows PowerShell

I've made each command into separate code blocks just in case you are using the mouse🐭

$projs = "dir1", "dir2", "dir3"

Here we are making a variable called projs and added all the folder names we'll need

$projs | ForEach-Object { cd $_; mkdir hello; pwd; cd ..; }

Here we pipe our projs variable into a for each function so that we can execute the same piece of code in each folders one by one. Then we cd into the first directory, made another directory called hello and then printed the current directory before finally exiting back into the root folder.

I see that I've been using folders, dirs, directories and fols interchangeably and I'd like to confirm that that's exactly what I did.

Also I'd like to point out that cd, mkdir, and pwd are just aliases in PowerShell and you can get what they are mapped to by running the Get-Alias command followed by the alias that you want to look up Eg: Get-Alias cd or without any arguments to get the list of all current aliases.

Linux, MacOS - bash

Same effect can be achieved through Similar bash commands as well

declare -a dirs=("dir1" "dir2" "dir3")

since arrays have to be explicitly declared like this in bash

for i in "${dirs[@]}";do cd $i;mkdir hello;pwd;cd ..;done

As I said "similar😅" lol