DEV Community

Cover image for Bash aliases that you need - works with Git Bash too!
Eden Jose
Eden Jose

Posted on • Updated on

Bash aliases that you need - works with Git Bash too!

When you were first exploring Linux, you were ecstatic as you open the terminal for the first time and enter your first few commands: exploring the files and directories.

But as you go along and use it more and more in your labs and in your work, you feel the need to shorten some of this commands to lessen the time in typing them or simply because you tend to forget some of the commands which you don't use very often - hey, we all do.

So, what do you do? You create aliases for them.

Now, these are just some of the commands that I tend to use daily.
I might update this once in a while as I learn more but I'm all ears on what others find to be useful aliases that can be added to this list.

Right, let's dive in!


📑 First things first

There are two ways to create aliases. The first one is by using the alias command followed by the shortcuts commands. As an example, you can go to your terminal and enter the command below to add an alias to list the contents of a folder in chronological order, with the most recent one at the bottom:

alias ltr='ls -ltr'
Enter fullscreen mode Exit fullscreen mode

However if you manually type the alias command directly at the terminal to create the shortcuts, those aliases will only be valid during the duration of that session. Once you close that session and open a new one, you cannot use ltr anymore.

The best way to make sure you can create aliases and use them across sessions is by editing them in the .bashrc file.


📌 Oh, another thing

This bash aliases works on the Linux Terminal. If you're using a Windows machine, editing on VSCode, and you're repeatedly navigating files through Git Bash because you're still not using WSL, then this aliases would still work fine.

You can simply go to your home directory and go to the etc folder in git and edit the aliases.sh. Note that Git may be in your Program Files folder or Program Files (x86), depending on what path you specified during the installation of Git Bash.

cd /c/Program\ Files/Git/etc/profile.d/aliases.sh
Enter fullscreen mode Exit fullscreen mode

If there is no aliases.sh, you can also create one inside that directory.

mkdir aliases.sh
Enter fullscreen mode Exit fullscreen mode

Similarly, if you're using a company computer where you don't have sufficient administrator privileges to modify files in the Program Files folder, you can simply go to your home directory and create a .bashrc file.

cd /c/Users/JohnSmith
mkdir .bashrc
Enter fullscreen mode Exit fullscreen mode

As for any Linux environment, you can find the .bashrc in your home directory, /home/username/.bashrc

You can now start putting your aliases in either file by using nano or vi.

nano .bashrc
vi .bashrc
Enter fullscreen mode Exit fullscreen mode

In Linux, make sure to source your .bashrc file once you're done putting your aliases there so that they become active without logout/login.

$ source ~/.bashrc 
Enter fullscreen mode Exit fullscreen mode

Now, onto the helpful commands

Of course, these are just a few of what normally use. You're free to suggest in the comments what you think is useful! 😄

Since I frequently access the the Downloads folder for the zip file or packages, I created a shortcut for it.

alias dloads='cd /c/Users/Eden\ Jose/Downloads'
Enter fullscreen mode Exit fullscreen mode

I did the same for the Desktop folder. You can simply do the same when you repeatedly go inside some directories in your machine.

alias desktop='cd /c/Users/Eden\ Jose/Desktop'
Enter fullscreen mode Exit fullscreen mode

I also started with Powershell before I shifted to Linux, so I still tend to incorrectly enter PS commands in the terminal, like clearing the display. So I did an alias for that as well.

alias cl='clear'
Enter fullscreen mode Exit fullscreen mode

This one I learned from my work - when I was repeatedly staging, committing, and pushing my edits to our Bitbucket repo. You can do all of that it just 6 letters!

alias gitacp='git add -A;git commit;git push'
Enter fullscreen mode Exit fullscreen mode

Now, I knew I might add new shortcuts over time, so I did an alias for that too!

alias addalias='vi /c/Program\ Files/Git/etc/profile.d/aliases.sh'
Enter fullscreen mode Exit fullscreen mode

Now, if someone asks me what file I use to put my aliases on, I can easily find that file,

alias wherealias='ll /c/Program\ Files/Git/etc/profile.d/aliases.sh'
Enter fullscreen mode Exit fullscreen mode

To find a particular command that I used previously,

alias histgrep='history | grep'
Enter fullscreen mode Exit fullscreen mode

As an example, I can find previous commands with 'Dockerfile'

alias wherealias='ll /c/Program\ Files/Git/etc/profile.d/aliases.sh'
Enter fullscreen mode Exit fullscreen mode

Two most common commands: cdll

I also find myself repeatedly going inside a directory by running cd and listing the contents by ll. So I also created a shortcut for that. However, instead of using alias I used a function for this and added that function to my aliases.sh.

# Initially plan to use an alias to combine 'cd' and 'll', but keep getting roadblocks.
# After some researching in the internet, most resources point out that if alias can't do it,
# you can always use a function.

# WHAT: Go to chosen directory ($1) and then list out contents.

function cdll() {
        cd "$@" && ls -la
}

Enter fullscreen mode Exit fullscreen mode

For this one, you can read more about it here

There's also been some ongoing question on when to use alias, when to use scripts, and when to use functions. I think this one is worth looking into when you're trying to optimize your workflow. Here's some links which I find useful:

Similarly, you can also some of the links below for additional tricks you can add to your sleeves:


Wrapping it all up

This is just a simple tutorial-like on how you can start using aliases. We're always looking for ways to save time on repetitive activities and knowing this simple keystroke shortcuts can definitely help a lot.

Discussion (0)