📜 ⬆️ ⬇️

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

In this article, I will teach you how to create your own aliases for 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 nicknames, 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 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 training 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 their own business, teach others, or simply improve their own skills.

Programmer's World


We programmers often have to run the same bash commands over the same project 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 fall.

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 you do not need to create pseudonyms for each terminal command, only for those that you reuse. Also note that some pseudonyms may 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 on github.com.

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


Let's say every week you do git push 15 times, and the push takes 20 seconds in general.


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


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 ~/.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 execute source ~/.custom_aliases after this, 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)
# opens file
code ~/.custom_aliases

Gedit
# 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 custom_aliases file.

# update file
source ~/.custom_aliases


Step 5 - Run the new bash command


Type the following in your favorite command shell.

# command line
welcome
> Welcome John Doe.


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.

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.

# 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"


Note that the operating systems have differences, so these commands must first be run in the terminal and check their performance before adding them to the custom_aliases file.

Running multiple commands


You can create a single bash command, through which you can execute several 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 custom_aliases file, for which you need to run source ~/.custom_aliases and 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.


Source: https://habr.com/ru/post/420347/


All Articles