How I automate my OS 💻
Tags: productivity, bash, powershell
Published: 2019-11-27 11:06:36

---
title: How I automate my OS 💻
published: true
description: 
tags: productivity, Bash, Powershell
cover_image: https://thepracticaldev.s3.amazonaws.com/i/sx42h20j4a4s85sdb2i6.jpg
---

When I started developing, in particular on servers I started writing scripts to get them set up which mainly stemmed from the fact I didn’t know what I was doing. I would boot up a virtual machine or VPS and start tinkering with Apache or Nginx, change a setting nonchalantly not thinking about it. 10 minutes later I’m desperately trying to find which setting has changed, most of the time leaving me feeling like this:

![server mess](https://thepracticaldev.s3.amazonaws.com/i/2z2z45ffcydl9voy6zhl.jpg)


I thought there has to be a better way and there is! Enter Bash Scripts! 

Using Linux at the time made it easy to write scripts to install everything I needed for a new instance. I started to take what I called the destructible approach whereby I don’t install anything on an instance unless it’s in the script. What that meant is I could tear down my server and get it back to where it was in less than 10 minutes. Beautiful. 

![great success](https://steamuserimages-a.akamaihd.net/ugc/307738670230847301/02FCBC35FC39EC8A78464A4CB62F049987C44892/)

Then it clicked. Why not use this pattern in my normal setup? 🤯🤯🤯

Below I will explain how I achieved this on Linux and Windows.


# Linux <a name="linux"></a> 💻
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ixp1x8blye2pkvoud3wo.jpg)
* [Jump to windows](#windows)

I got to it and started creating what I now know as “Linux Tools” which is my set of scripts that do the above and more.


### Installs ⚡

As Linux is a command-line driven which means apps can be installed via the command-line, for example, to install a package/app the most common approach is to use a package manager, we call the one that comes with Ubuntu apt.

To install git you will issue the following commands (in an Ubuntu flavoured distribution): 

```
sudo apt update
sudo apt install git
```

- First command updates repositories
- Second installs git

The advantage of installing programs in this way is that it is scriptable. We can put the above into a script(.sh) file and then run from the command line.

If we saved the above into the file “installGit.sh” there are 2 commands used to get the script ready to execute and then run. 

```
sudo chmod u+x ./installGit.sh
sh ./installGit
```

- The first command makes the script executable and only needs to be run once on each script.

- The second command runs the script as if we had run the commands in the terminal ourselves.

Any command that can be executed in the terminal can be scripted this way allowing us to set up our environment extremely quickly. 

### Functions 🛠
I use functions to organise code, I call update at the start of the script to avoid having to call it in each function:
```
sudo apt-get update

# function definition
GIT_INSTALL () {
  sudo apt-get install git
}

# function call
GIT_INSTALL

```
This may seem like a lot of boilerplate but the advantage is that there may be many steps to installing an app that can be encapsulated into a single function, the above is the most simple example.


###Dotfiles 📁

The above explains the automation of installing packages but what blew my mind when first getting into this is being able to restore the apps the state I had them in previous to a reinstall 🤯 This is where Dotfiles come in. I’m not going to go into lots of detail about Dotfiles as you can find that on this awesome post:

{% link https://dev.to/mmphego/how-i-increased-my-productivity-using-dotfiles-2f4m %}

Simply put Dotfiles are user configuration files 🔧. This would hold any changes to settings you the user has made the program/app in question. One of the most common you may have come across is .gitconfig which stores info about your git account on your local system. When you run: 

```
git config credential.username “new_username”
```
It will update your .gitconfig with:

```
[user]
	email = julianiaquinandi@gmail.com
```

As mentioned I have a set of files that get copied to where they need to go as part of my install, you can find my dotfiles [here](https://github.com/kensleDev/linux-tools/tree/master/dotfiles)



###Aliases 👻

I use aliases to quickly perform commands without having to type the whole thing out. So in this scenario, you could map the alias “installOs” to run your installer script. It’s not a practical example as you're not likely to need to run your installer script all the time, although I thought it was worth mentioning because:
 
1. Aliases are awesome and save me lots of time
2. Part of my install script copies my aliases file from a repo to my local install ready to be consumed by the command line. 

If you haven’t used them before I would recommend checking this article for a primer:

{% link https://dev.to/aniketsmk/bash-aliases-aka-part-1-of-2000-of-how-to-get-more-done-in-less-time-3lp6 %}



#Windows <a name="windows"></a> 💻
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/2i9kad99boqo47vazvah.jpg)
* [Jump to linux](#linux)

Granted I have a lot more developer experience on Linux but I was able to recreate what I do to automate my OS on Windows with less effort than expected. 

### Installs ⚡

Unfortunately, Windows does not have a built-in package manager by default but there is a way to achieve command-line installs. [Chocolatey](https://chocolatey.org/) touts itself as “The Package Manager for Windows”.

To replicate the git install from above we will run the following:
```
choco install git
```

That’s it.

Seems easy and it is. The biggest issue I’ve found with chocolatey is it not having everything I need. You can search the registry [here](https://chocolatey.org/search)

To turn it into a script you could put the above into a Powershell script: “installGit.ps1” and run it from the command-line like any other script:

```
./installGit.ps1
```

### Dotfiles 📁
Dotfiles work the same in Windows as they do in Linux. They are user configuration files that I save to a repo and then copy to their required location to restore an app’s settings. 

To backup your git config on windows it’s as simple as copying C:\Users\YOUR_USERNAME\.gifconfig to a backup location/repo. If you want to restore it simply copy it back to its original location 😉

### Aliases 👻 & Functions 🛠

I found creating aliases a bit confusing at first but then I realised you can use functions that are defined in your Powershell [default profile](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-6).

Here’s a couple of examples from mine, the first is used to install packages from chocolatey by typing “install” The second to clone a repo by typing its name:

```

function install() {
  choco install -y $args
}

function clone() {
  clear
  echo “Enter repo name”
  echo “-----------------“
  $repo = Read-Host -Prompt ‘|=>‘

  $start=$repo.substring(0, 4)

  clear

  if ($start -eq “http”) {
    git clone $repo
  } Else {
    git clone git@github.com:kensleDev/$repo
  }

}
```

With the above in your Powershell profile, you can simply type the name of the function to call it e.g. 

```
install git

clone
```


#Wrapping Up 🏁
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/ux71ylf0t2x8vuoo27xd.jpeg)

I love scripting away menial tasks and this was a little look into how I achieve that within my OS. I am constantly refining my scripts and finding better ways to save time and concentrate on what’s important.

TLDR - An overview of how I automate the setup/maintenance of my OS/Environment via Bash/Powershell scripts.