Learn how to create your own bash commands in less than 4 minutes.

Original author: Dler Ari
  • Transfer
In this article, I will teach you how to create your own aliases for the bash commands, as well as how to simultaneously run several commands with one bash command.

TL; DR The first part describes why pseudonyms are so important, how much time they save, etc., but if you just want to learn how to create your own pseudonyms, go to step 1.

Productivity increase

The older we get (I know, time is running), the more responsibility falls on our shoulders: we support our family, control our personal budget, spend time with our relatives, take our children to kindergarten and do other adult activities.

Time is a very important factor that affects the productivity of specialists, especially programmers. There are more responsibilities, less learning time, which means that you need to work efficiently.

Every week I plan to publish recommendations and tips on modern web development languages ​​for all those who want to start a business, train others or simply improve their own skills.

Programmer's World

We programmers often have one project to run the same bash command many times, for example   cd ..ls -l or   pwd. From the fact that we run these commands once a week, the performance will not decrease, but if you run them twice a day, in the end, the efficiency will drop.

Some bash commands are short, others are long. Some are difficult to remember, others are easy. The main thing is to speed up the workflow (increase efficiency), and for this you can create declarative commands (readable code) that are easy to remember and write.

Do not forget that it is not necessary to create pseudonyms for each terminal command, only for those that you reuse. Also note that some pseudonyms may turn out to be reserved words, so test them first, otherwise you may accidentally replace another important command.

Make git commands shorter

I did a simple test to demonstrate how much time it takes to make changes on Github. An average programmer usually takes about 20-25 seconds to push changes to github.com. For example, every week you perform  15 times, and the push takes 20 seconds in general.

# Test
git add .
git commit - m "minor changes"
git push -u origin master

git push

  • A week takes 5 minutes
  • It takes 20 minutes a month
  • Per year - 4 hours

These 3 commands can be replaced with a single alias lazyman "minor changes", and instead of 20 seconds we get 5.

  • It will take 1.25 minutes a week.
  • In a month it will take 5 minutes.
  • Per year — 1 hour

Overall, productivity will increase by 75% (four times). It was a simple example. Now imagine how much time you can save on such commands as run apache server && run tests && report data && close or  gcc project-source-code.c -o executable-file-name, which we run 15-30 times a day.

How do I calculate the performance increase? (for bore)

# Formula
((old - new) / old) * 100%

= ((20 sec - 5 sec) / 20 sec) * 100%
= 75 % (performance increase)

Before you start creating pseudonyms

When creating aliases, they are usually placed in a file ~/.bashrc. This is a hidden file in the home directory, accessible from anywhere. However, it is considered good practice to keep system files separate from personal files. To do this, create a new file called   ~/.custom_aliases and add all the aliases there. Also do not forget to perform after this  source ~/.custom_aliases, otherwise they will not work.

Step 1 - Create a custom_aliases file

All generated aliases should be stored in this file.

# create file
touch ~/.custom_aliases

Step 2 - Open the file custom_aliases

Open the file in a text editor, either through  gedit  or  code  (Visual Studio Code), or as you see fit.

Visual Studio Code (if installed) Gedit
# opens file
code ~/.custom_aliases

# opens file
code ~/.custom_aliases

Step 3 - Create aliases

Let's make a simple alias: when we enter “welcome” into the bash terminal, “Welcome John Doe.” Will appear on the screen.

alias welcome='echo "Welcome $USER."'

Step 4 - Update Changes

Before running the newly created bash command, you must update the file custom_aliases.

# update file
source ~/.custom_aliases

Step 5 - Run the new bash command

Type the following in your favorite command shell. Well done! You have just created your own file to store nicknames. And now let's look at what kinds of commands you can create.

# command line
> Welcome John Doe.

Own bash aliases (personal)

Below are a few bash commands that I use to speed up my workflow.

Recommendation: To preserve the structure when adding a large number of aliases, divide them into groups - as in the example below - using comments. Note that operating systems have differences, so these commands must first be run in the terminal and verify their performance before adding them to the file .

# Version Control
alias gs="git status"
alias gd="git add ."
alias gp="git push -u origin master"

# Directory
alias diskusage="df -h"
alias folderusage="du -ch"
alias totalfolderusage="du -sh"

# Various
alias opencustomaliases="code ~/.custom_aliases"
alias updatecustomaliases="source ~/.custom_aliases"
alias updatethenupgrade="sudo apt-get update && sudo apt-get upgrade"


Running multiple commands

You can create a single bash command, through which you can run multiple commands. There are two ways: you can write a function or create an alias.

Example 1 - Creating a function

# Multiple commands

function lazyman() {
git add .
git commit -a -m "$1"
git push -u origin master

Example 2 - Creating a nickname

# Multiple commands

alias lazyman="git add . && git commit -a -m '$i' && git push -u origin master"

Do not forget to update the file custom_aliases, for which you need to run  source ~/.custom_aliasesand enter lazyman "First commit".

Commercial break. I want to introduce a new project LOOKING.HOUSE - it collected more than 150 points looking glass in 40 countries. You can quickly execute the host, ping, traceroute, and mtr commands.

Also popular now: