Skip to main content

Creating Custom Commands Using `alias` in Linux

Introduction

In Linux, the alias command is a simple but powerful tool that allows users to create shortcuts or custom commands for longer commands. This can improve your workflow and speed up tasks. In this article, we'll explore how to create custom commands using alias, discuss the life cycle of these commands, and detail various options available for the alias command.

Basics of alias

At its core, alias allows you to assign a new name (alias) to a command or sequence of commands. The basic syntax for creating an alias is:

alias custom_name='your_command'

For instance:

alias l='ls -l'

With this alias set, typing l will now execute the ls -l command.

Examples of Custom Commands Using alias

1. Improving ls Output

Many users prefer colored output for the ls command:

alias ls='ls --color=auto'

2. Shortening Git Commands

If you frequently use git, you might find these shortcuts handy:

alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'

3. Navigation Shortcuts

alias ..='cd ..'
alias ...='cd ../..'

4. Enhanced Directory Listing

To list directories first, followed by files:

alias ll="ls -lv --group-directories-first"

5. Safety Nets

Protect against accidental deletions:

alias rm='rm -i'

The -i option prompts you before removing a file.

Life of an Alias

  1. Creation: As shown above, aliases are created using the alias command followed by the custom command name and the command it represents.

  2. Duration: By default, an alias created on the command line is temporary. It lasts only for the current session. If you close the terminal or log out, the alias is gone.

  3. Persistence: To make an alias permanent, you can add it to your shell’s configuration file, such as ~/.bashrc for the Bash shell. After adding, you can use source ~/.bashrc or just open a new terminal to have the alias available.

  4. Removal: To remove an alias, use the unalias command followed by the alias name, e.g., unalias ll.

  5. Viewing Aliases: To view all currently defined aliases, simply type alias without any arguments.

Table of alias Options

Technically, alias itself doesn't have a wide range of options like other Linux commands. Its main purpose is to define or check aliases. However, in relation to managing aliases, there's unalias for removing them. Here's a table for clarity:

Command/OptionDescription
aliasDisplay a list of all defined aliases in the current session.
alias [name]Display the command assigned to a specific alias.
alias name='command'Create a new alias.
unalias nameRemove an alias.
unalias -aRemove all defined aliases.

Conclusion

The alias command in Linux is a practical tool for streamlining your command-line operations. By creating custom commands that fit your workflow, you can enhance productivity and reduce the chances of errors. Remember to add frequently used aliases to your shell's configuration file to ensure they are available in every session.

What Can You DO Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle