Linux alias Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux alias command to create custom shortcuts for frequently used commands. You will understand the concept of aliases, create and manage them in the current shell session, and persist them across shell sessions. This lab will help you improve your productivity and efficiency when working in the Linux command line.

The lab covers the following steps:

  1. Understand the Concept of Aliases in Linux
  2. Create and Manage Aliases in the Current Shell Session
  3. Persist Aliases Across Shell Sessions

The alias command is a built-in feature in most Linux shells, and it does not require any additional installation. However, it's important to note that aliases are specific to the shell you are using, such as Bash or Zsh, and may not be available in all shell environments.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") subgraph Lab Skills linux/source -.-> lab-422542{{"`Linux alias Command with Practical Examples`"}} linux/echo -.-> lab-422542{{"`Linux alias Command with Practical Examples`"}} linux/ls -.-> lab-422542{{"`Linux alias Command with Practical Examples`"}} linux/nano -.-> lab-422542{{"`Linux alias Command with Practical Examples`"}} end

Understand the Concept of Aliases in Linux

In this step, you will learn about the concept of aliases in Linux. Aliases are shortcuts or nicknames for commands in the shell. They allow you to create custom commands that can save you time and improve your productivity.

To understand the concept of aliases, let's start with a simple example. Imagine you frequently use the ls -l command to list files in a long format. Instead of typing this every time, you can create an alias for it:

alias ll='ls -l'

Now, whenever you type ll in the terminal, it will execute the ls -l command.

Aliases can be used for any command or sequence of commands. For example, you can create an alias to quickly navigate to a frequently used directory:

alias projects='cd ~/project'

Now, you can simply type projects to change to the ~/project directory.

Aliases are stored in the shell's configuration file, such as .bashrc or .zshrc, depending on the shell you are using. This allows you to persist the aliases across shell sessions.

Example output:

$ alias ll='ls -l'
$ ll
total 12
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 documents
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 downloads
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 pictures

In the example above, we first create an alias ll for the ls -l command. Then, we use the ll alias to list the files in the current directory in long format.

Create and Manage Aliases in the Current Shell Session

In this step, you will learn how to create and manage aliases in the current shell session.

First, let's create a simple alias to shorten the ls -l command:

alias ll='ls -l'

Now, whenever you type ll in the terminal, it will execute the ls -l command.

You can also create aliases for more complex commands or command sequences. For example, let's create an alias to quickly navigate to the ~/project directory and list the contents:

alias projects='cd ~/project && ll'

Now, you can simply type projects to change to the ~/project directory and list the files in long format.

To view the list of all aliases currently defined in your shell session, use the alias command without any arguments:

alias

This will display all the aliases you have created.

If you want to remove an alias, you can use the unalias command:

unalias ll

This will remove the ll alias from the current shell session.

Example output:

$ alias ll='ls -l'
$ ll
total 12
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 documents
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 downloads
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 pictures
$ alias projects='cd ~/project && ll'
$ projects
total 4
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 docs
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 src
$ unalias ll
$ ll
ls: cannot access 'll': No such file or directory

In the example above, we first create the ll alias, then the projects alias, and finally remove the ll alias.

Persist Aliases Across Shell Sessions

In the previous step, you learned how to create and manage aliases in the current shell session. However, these aliases are temporary and will be lost once the shell session is closed. In this step, you will learn how to persist aliases across shell sessions.

To make aliases persistent, you need to add them to the shell's configuration file, such as .bashrc or .zshrc, depending on the shell you are using. This way, the aliases will be loaded and available every time you start a new shell session.

Let's start by creating a new alias for the mkdir command:

alias mkd='mkdir -p'

This alias will create a new directory and any necessary parent directories with a single command.

Now, to make this alias persistent, you need to add it to your shell's configuration file. If you are using the zsh shell, the configuration file is located at ~/.zshrc. You can open this file with a text editor:

nano ~/.zshrc

At the end of the file, add the following line:

alias mkd='mkdir -p'

Save the file and exit the editor.

From now on, the mkd alias will be available in all your future shell sessions. To verify this, open a new terminal and try using the mkd alias:

mkd new_directory

This should create a new directory named new_directory and any necessary parent directories.

Example output:

$ alias mkd='mkdir -p'
$ mkd new_directory/subdirectory
$ ls -l
total 4
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 new_directory

In the example above, we first create the mkd alias, then use it to create a new directory with a subdirectory, and finally verify that the directory was created.

Summary

In this lab, you will learn about the concept of aliases in Linux, which are shortcuts or nicknames for commands in the shell. Aliases can save you time and improve your productivity by allowing you to create custom commands. You will learn how to create and manage aliases in the current shell session, as well as how to persist them across shell sessions by storing them in the shell's configuration file.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like