How to Add New Shortcut Commands in Linux: A Step-by-Step Guide

add-new-shortcut-command-in-linux

Creating shortcut commands (also known as aliases) in Linux can enhance your productivity by simplifying long or repetitive commands. In this approach, you’ll learn how to add new custom shortcuts or aliases in Linux.

Method 1: Create a Temporary Shortcut Command

Temporary shortcuts are only valid for the current terminal session. Once you close the terminal, the shortcut will be lost.

Step 1: Create a Temporary Alias

Use the following syntax to create a temporary alias:

alias shortcut_name='command_to_execute'

Example: alias ll=’ls -la’

In this example, typing ll will run the ls -la command, listing all files in long format.

Method 2: Create a Permanent Shortcut Command

To make the shortcut persistent across terminal sessions, you need to define it in a shell configuration file.

Step 1: Open the Shell Configuration File

Depending on your shell, open the appropriate configuration file:

For Bash (most common):

vim ~/.bashrc

For Zsh (if using Zsh):

vim ~/.zshrc
alias update='sudo apt update && sudo apt upgrade -y'

This alias will allow you to type update instead of running both apt update and apt upgrade.

After saving the changes, reload the shell configuration:

source ~/.bashrc # For Bash

Method 3: Create Custom Scripts as Shortcuts

If you frequently use a sequence of commands, you can create a custom script and run it like a command.

Step 1: Create the Script

Create a new script file in /usr/local/bin or another directory in your PATH:

sudo nano /usr/local/bin/myshortcut

In the file, add your commands:

#!/bin/bash
echo "Updating the system..."
sudo apt update && sudo apt upgrade -y

Set the correct permissions:

sudo chmod +x /usr/local/bin/myshortcut

You can now run the script by typing:

myshortcut

Method 4: Set Environment Variables as Shortcuts

You can use environment variables to store and reuse specific paths or commands.

Step 1: Add the Variable to Your Shell Configuration

Open your shell configuration file (~/.bashrc or ~/.zshrc):

vim ~/.bashrc

Add the following line:

export MY_PROJECT_PATH='/home/user/my_project'

To add new shortcut command in linux follow the bellow steps.

Apply the Changes

source ~/.bashrc

Thats it.