Why you should use the console to automate tasks

Why you should use the console to automate tasks

Hi! Today, I want to talk about how you can automate routine tasks using the terminal and why it’s worth doing. Maybe you’re not a fan of the command line, or you’ve always thought UI interfaces are much more convenient. But there are times when the terminal isn’t just a handy tool – it’s a real game-changer for speeding up processes. This is just my personal opinion, based on my experience working with the terminal. And perhaps, after reading this, some of you will be inspired to automate at least a few tasks yourself. When I use the terminal, I feel like a true hacker—fast, efficient, and free from the limitations of complex interfaces. Would you like to try it, too?

Who is this post for?

This post will be useful for those who want to automate their tasks, be more productive and generally learn how to work with the terminal. Yes, you may not love it yet, but believe me, there is not only power here, but pleasure in using it! And for those who have been using GUIs for a long time, I hope my experience can get you interested and take you to the next level.

Before going deeper, I'll say right away: everything I'm telling you is just my opinion based on experience. But I think you too can understand why the terminal is not only for super hackers, but also for everyday tasks.

What tools will you need?

Before you get started, you'll need to familiarise yourself with a few things:

  • Terminal - first you'll need to have it open on your machine (if you're on macOS, Linux, or WSL on Windows, you already have it).
  • Text editor - for writing and editing your scripts. This can be Visual Studio Code or something simple like nano or vim.
  • Bash is the command line shell we will be using. It is built into most operating systems.
  • Git - If you're not familiar with git, it's a must-have for version control and collaboration. You'll definitely need it if you want to save your automations and share them.

Now that you're all set, let's figure out where to start.

How to Get Started?

If you’re a beginner, don’t worry! The terminal is essentially a set of commands that perform specific tasks. Here’s how to take the first steps:

  • Open the terminal. On macOS or Linux, you can find the “Terminal” app, and on Windows, use PowerShell or WSL.
  • Try the echo command. This command outputs text. For example:

echo "Hello, world!"        

It’s a basic command to help you get acquainted with the terminal.

  • Create your first script. Open your text editor, and create a file with the .sh extension (e.g., my_first_script.sh). Write a couple of lines like this:

#!/bin/bash
echo "Hello, World!"        

Save the file and make it executable with the following command:

chmod +x my_first_script.sh        

Now you can run it with:

./my_first_script.sh        

Real-World Project Examples Where the Terminal Helps

Now let’s dive into some real-world tasks I’ve automated using the command line. Hopefully, they’ll inspire you and show how fast and powerful automation can be.

Project Backup

Let’s say you have an important project, and you need to back it up every evening. Instead of manually copying files, you can write a script:

#!/bin/bash
DATE=$(date +%Y-%m-%d)
SOURCE_DIR="/path/to/project"
BACKUP_DIR="/path/to/backup/$DATE"
cp -r $SOURCE_DIR $BACKUP_DIR
echo "Backup completed!"        

Password Generator

If you frequently create new passwords for different services, you can automate this process. Here’s a script to generate a 16-character password:

#!/bin/bash
PASSWORD=$(openssl rand -base64 16)
echo "Your new password is: $PASSWORD"        

Check Website Availability

To check if your website is online, you can use curl or get:

#!/bin/bash
URL="http://example.com"
STATUS=$(curl -s -o /dev/null -w "%{http_code}" $URL)
if [ $STATUS -eq 200 ]; then
    echo "$URL is up!"
else
    echo "$URL is down! Status: $STATUS"
fi        

Task Organization

It can be handy to automate your task list with reminders. For example, for personal projects:

#!/bin/bash
echo "Tasks for today:" > todo.txt
echo "- Backup project" >> todo.txt
echo "- Write report" >> todo.txt
cat todo.txt        

Update Notifications

You can set up a script that checks for new updates to your projects on GitHub:

#!/bin/bash
REPO="https://github.com/user/project"
git fetch origin
LATEST_COMMIT=$(git log origin/main -1 --oneline)
echo "Latest commit in repository: $LATEST_COMMIT"        

Git: Streamlining Repository Work

Now let’s talk about Git and how to automate routine Git tasks:

Script for Syncing Local Repository with Remote

This script automatically performs several actions: updates the local branch, merges changes from the remote repository, and then pushes changes back.

#!/bin/bash
# sync_git.sh

# Check if we're in a git repository
if [ ! -d ".git" ]; then
    echo "Git repository not found. Please navigate to a repository directory."
    exit 1
fi

# Check if we're on the correct branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $current_branch"

# Pull the latest changes from the remote repository
echo "Updating local branch..."
git pull origin $current_branch

# Check status
git status

# Push changes to the remote repository
echo "Pushing changes to the remote server..."
git push origin $current_branch

echo "Repository synchronized."        

Script for Creating and Switching to a New Branch

This script simplifies the process of creating a new branch and switching to it automatically.

#!/bin/bash
# create_branch.sh

# Check if a branch name argument is provided
if [ -z "$1" ]; then
    echo "Please provide a branch name!"
    exit 1
fi

# Create and switch to the new branch
git checkout -b "$1"

echo "Switched to new branch $1."        

Script for Cleaning Uncommitted Changes

This script allows you to discard all uncommitted changes, returning the working directory to the state of the last commit.

#!/bin/bash
# cleanup_changes.sh

# Check if there are any uncommitted changes
git status --porcelain | grep -q "^[ M]" 
if [ $? -eq 0 ]; then
    echo "Uncommitted changes found. Reverting them..."
    git reset --hard
    git clean -fd
    echo "All changes reverted and working directory cleaned."
else
    echo "No changes to revert."
fi        

Script for Generating a Repository Status Report

This script gathers information about the current state of the repository, including the status of branches, number of commits, and more, and outputs a report that can be saved or sent via chat.

#!/bin/bash
# git_report.sh

# Get the current date and time
date=$(date '+%Y-%m-%d %H:%M:%S')

# Repository status
repo_status=$(git status --short)

# List of branches and current branch
branches=$(git branch)

# Last commit
last_commit=$(git log -1 --oneline)

# Number of changes
changes_count=$(git status --porcelain | wc -l)

echo "Repository Report ($date)"
echo "--------------------------------"
echo "Current branch: $(git rev-parse --abbrev-ref HEAD)"
echo "Last commit: $last_commit"
echo "Number of changes: $changes_count"
echo "--------------------------------"
echo "Repository status:"
echo "$repo_status"
echo "--------------------------------"
echo "Branch list:"
echo "$branches"        

Script for Finding All Changed Files in the Last Few Commits

If you need to find all the files that have been modified in the last few commits, this script can help you do just that.

#!/bin/bash
# find_changed_files.sh

# Check for a commit count argument
if [ -z "$1" ]; then
    echo "Please specify the number of recent commits to check changes."
    exit 1
fi

# Get all modified files from the last N commits
git log -n $1 --name-only --pretty=format: | sort | uniq        

Script for Running Linter and Tests Before Commit

This script runs a linter and tests before you are allowed to make a commit. It helps prevent errors from being committed to the repository.

#!/bin/bash
# pre_commit_check.sh

# Run linter
echo "Running linter..."
npm run lint
if [ $? -ne 0 ]; then
    echo "Linter found errors! Please fix them before committing."
    exit 1
fi

# Run tests
echo "Running tests..."
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed! Please fix them before committing."
    exit 1
fi

echo "Linter and tests passed! Ready to commit."        

Conclusion

So, now you know how to start using the terminal for task automation. Remember, it not only speeds up your work but also gives you more control over your processes. This is just the beginning, and the possibilities with the terminal are much wider. In our company, Hyand Group , many team members already use such scripts to speed up their daily workflows.

And of course, this is just my opinion based on my experience. I’m not claiming that the terminal is the only way, but for me, it’s definitely the most convenient.

If you need more examples or tips on getting started, feel free to reach out!

To view or add a comment, sign in

Others also viewed

Explore topics