Shorten Frequent Linux Commands with Aliases

Shorten Frequent Linux Commands with Aliases

Intro

When using Linux on the terminal, there are commands that we use frequently. Most of them are short to type out, like cd, ls or rm.

But I’m sure we all have some commands that are very long, and yet we have no trouble typing it out because we run it so often. Or, you may have commands where you add the same arguments every time to the point where you want that argument to be the default.

In this article I will explain what alias are in shell scripting. After you try this out, you will be able to save a lot of time from typing out commands!

Alias

An alias in Linux is a user-defined shortcut for longer commands.

To setup an alias, you use the alias command (surprise!). The command takes the following format:

alias [New and short command]=[Old and long command]

For example, say you want a new ls alias with specific parameters. You might do something like this:

rolzy ~/projects/temp $ alias la='ls -la'
rolzy ~/projects/temp $ la
total 5020
drwxr-xr-x 13 rolzy rolzy    4096 Jan 23 18:05 .
drwxr-xr-x 18 rolzy rolzy    4096 Dec  8 16:16 ..
-rw-r--r--  1 rolzy rolzy       7 Jul 12  2023 .python-version
-rw-r--r–-  1 rolzy rolzy    1931 Jan 23 18:05 temp

And voila! You can now type out la and save 4 keystrokes.

Examples

alias python="python3"
alias gs='git status'
alias push='git push'
alias pull='git pull'
alias sa='source env/bin/activate'

Here are some aliases that I setup. They are all long commands that I run frequently.

Sessions

One thing to note is that aliases are session-bound. Once you exit out of your terminal, your aliases will be gone as well. To get your alias back, you will have to run the alias command again.

To have your aliases on every session, I recommend you to setup your aliases in your .rc files. These files run at the beginning of every session. If you have your aliases defined in there, it will be setup and ready to go every time you start a new terminal session.

Your .rc file will depend on the shell you are using.

  • If you’re using bash, your .rc file will be ~/.bashrc.
  • If you’re using zsh, your .rc file will be ~/.zshrc.

Arguments

You can also send arguments to aliases as well. For example, if you have

alias la='ls -la'

already setup, you can then pass in arguments to the command!

For example, la test_dir will be the equivalent to ls -la test_dir.

With this in mind, I like to set some git related commands like these:

alias gd="git diff"
alias ga="git add"
alias gcm="git commit -m"

and then run a set of git commands like this:

git diff test.py becomes gd test.py

git add -p test.py becomes ga -p test.py

git diff --staged test.py becomes gd --staged test.py

git commit -m "Make some changes to test.py" becomes gcm "Make some changes to test.py"

Conclusion

And thats it! If you have any aliases that you use, let us know in the comments :)

Read more

zshの模様替えをしました

zshの模様替えをしました

💡この記事は下の動画の受け売りです: はじめに 今年も早いもので、すでに三月。春の始まりです。 春といえば新しい生活。心機一転! 周りの環境が大きく変わる方も多いのではないでしょうか? 今年の春、私の人生に大した変化はないのですが、それでも人生にちょっと新しい風を吹かしたいな~と思うので、今回思い切って模様替えをしようと思います! ターミナル環境のね。 zshellって? シェル (shell) とは、OSとユーザーの仲介役。ターミナルを開いたときに動くプログラムがシェルです。 そのシェルにも色々あるんです。sh, bash, fish, ksh などなど…。今回は、その中のzsh というシェルの話。 zshは高いカスタマイズ性が人気です。zsh そのものの機能と豊富なプラグインでターミナル環境を自分の好きなように設定できます。 私もzshを使い始めて数年たちますが、欲しいところにちゃんと手が届く、という印象です! …とか言いつつ私は面倒くさがりなので、自分で.zshrc をイジることはしません。YouTubeやSNSなどで見つけたカッコいいタ

By Roland Thompson
Terraformを使って"AWS Lambdaとその取り巻き"を召喚しよう

Terraformを使って"AWS Lambdaとその取り巻き"を召喚しよう

はじめに 人生、いろんな「派閥」ってありますよね。私が属するITインフラ業界にも色々あります。どのクラウドを使うか、どのIDEを使うか、どのOSを使うか…枚挙に暇がありません。 インフラ(IaC)言語もその一つ。私はこの業界に足を踏み入れてから、ずっとCloudFormation派閥です。私はAWS専門だったので、AWS公式のCloudFormationで仕事が成り立ってました。 しかし最近、AzureやDatabricks関連の仕事も私に降ってくるようになりました。そうなると、AWS限定のCloudFormationでは対応できません。 そんなとき、複数のクラウドプラットフォームに対応できるTerraformという存在を耳にしました。 Terraformの練習として、AWS Lambdaとその取り巻き(ECR, IAM, Secrets Manager, CloudWatch, SQS) を召喚してみたので、この記事にまとめます。 環境構築 まずは公式マニュアルを参考にTerraformをインストールしましょう。 ターミナルからTerraformが動けば、インストー

By Roland Thompson