Skip to main content

LPI E - alias

LPI E - alias

* Awesome Tricks and Hacks at end

Introduction

An alias is a way to create a shortcut or alternative name for a command in Linux. When you define an alias, you create a new name for a command that is already installed on your system. You can then use the new name to run the command instead of the original name.

Temporary Session alias

Aliases are typically used to simplify the use of frequently used commands or to create shortcuts for long or complex commands. 

For example, to create an alias for the ls -la command, you can run the following command:

$ alias ll='ls -la'

After defining the alias, you can use the ll command to execute the ls -la command. The alias will be active for the duration of the current shell session.

Permanent Sessions alias

If you want to make the alias persistent across multiple sessions, add the alias command to your shell's startup file (e.g. ~/.bashrc for the Bash shell).

  1. Open the .bashrc file in your home directory with a text editor (e.g. nano ~/.bashrc).
  2. Scroll to the bottom of the file and add the following line
    • (e.g alias up="cd ..")
  3. Save and close the file

Now, the next time you start a Bash session, the up alias will be available

HOW CAN I FIND ALL THE ALIASES?

Temporary Session alias

To find all the aliases that are currently defined on your Linux system, you can use the alias command without any arguments. This will display a list of all aliases that have been defined in the current shell session.

$ alias

This will display a list of all aliases currently defined in your shell session. Each alias will be listed as a name=value pair, where name is the alias name and value is the command that the alias represents.

Permanent Sessions alias

If you want to see the aliases that are defined in your shell startup files (such as ~/.bashrc or ~/.bash_profile), use the grep command to search for alias in those files:

$ grep "alias" ~/.bashrc ~/.bash_profile

This will display any lines that contain the word alias in your ~/.bashrc or ~/.bash_profile files, which may include alias definitions.

Note that aliases are specific to each user's shell session, so if you want to see aliases defined for another user, you will need to log in as that user and run the alias command.

HOW TO CHECK IF A COMMAND IS AN ALIAS?

The "type" command is used to display information about a command, including whether it is an alias, a shell built-in, or an executable file.

For example, to check if ll is an alias, you can run:

$ type ll

If ll is an alias, the output will show:

$ ll is aliased to 'ls -la'

If ll is not an alias, the output will show the location of the executable file or indicate that it is a shell built-in.

Similarly, you can use the type command to check if a command is a built-in or an executable file:

$ type cd
cd is a shell builtin

$ type ls
ls is aliased to 'ls --color=auto'
or
ls is /bin/ls

The output will indicate that ls is an alias or an executable file respectively.

HOW TO NOT OVERWRITE EXISTING ALIASES?

By default, if you define an alias with the same name as an existing alias, the new alias will overwrite the old one without warning. However, you can set the alias command to show a warning message if you try to redefine an existing alias by including the -p option.

To define an alias ls that shows a warning message if the alias already exists, you can use the following command:

$ alias -p ls='ls --color=auto'

If the ls alias already exists, this command will display the warning message:

bash: alias: ls: warning: overwrite existing alias

You can then decide whether to overwrite the existing alias or choose a different name for your new alias.

Note that the -p option is not available in all versions of the alias command, so you may need to check your system's documentation or man pages to see if it is supported.

Alternative to -p

If the -p option is not available in your version of the alias command, you can use a combination of the alias and grep commands to check if an alias already exists before defining a new one.

For example, to check if the ls alias already exists before defining a new one, you can use the following command:

$ alias | grep -w "ls"

This command will display any aliases that match the string "ls" exactly. If the command returns any output, it means that an alias with that name already exists. You can then decide whether to overwrite the existing alias or choose a different name for your new alias.

To define a new alias ls only if it doesn't already exist, you can use the following command:

$ alias ls='ls --color=auto' 2>/dev/null || echo "Alias 'ls' already exists"

This command defines the ls alias and redirects any error messages to /dev/null to suppress them. If the alias command fails (i.e., if an alias with that name already exists), it will display the message "Alias 'ls' already exists" instead.

>>> HACKS AND TRICKS!

Chain commands together:

$ alias startapp='cd /path/to/app; sudo service app start; tail -f /var/log/app.log'

With this alias, the system administrator can start a specific application and follow its log in one command by typing startapp.

Add default flags:

$ alias ll='ls -l --color=auto'

With this alias, the ll command will display the contents of the current directory in long format and with color, making it more readable than the default ls command.

Create shortcuts:

$ alias findfile='find /path/to/search -name'

With this alias, the system administrator can search for a file in a specific directory by typing findfile filename 

Use as reminders:

alias remindme='echo "Remember to perform task X"'

With this alias, the system administrator can set a reminder for themselves by typing remindme. 

Avoid mistakes:

$ alias rm='rm -i'

With this alias, the system administrator will be prompted for confirmation before deleting files with the rm command, reducing the risk of accidentally deleting important files. 

Use quotes to include spaces

If you want to include spaces in your alias, you should enclose the entire command in quotes, like this:

$ alias myalias='long command with spaces'

Use backslashes to escape special characters

If you need to include special characters like $, &, |, or > in your alias, you should use a backslash (\) to escape them. For example:

$ alias myalias='command1 \| command2 > output.txt' 

Use the unalias command to remove an alias:

If you no longer need an alias, you can remove it with the unalias command. For example:

$ unalias myalias 

Use aliases in scripts

You can also use aliases in scripts to make them more readable and easier to write. Just be aware that aliases defined in your shell session may not be available in the script, so you may need to define them explicitly in the script.

Use a separate file to manage aliases

If you have many aliases or want to keep them separate from your shell configuration files, you can create a separate file to manage them.

For example, you can create a file called .bash_aliases in your home directory and add your aliases there. Then, you can include that file in your shell configuration file (e.g. .bashrc) like this:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Use an alias to automate a sequence of commands

You can create an alias that runs a sequence of commands only if a certain condition is met. For example, you can create an alias that checks if a certain file exists before running a series of commands. 

Here's an example:

$ alias run-if-file-exists='if [ -e ~/my-file.txt ]; then echo "File exists!"; command1; command2; fi'

This alias will check if the file my-file.txt exists in the user's home directory, and if it does, it will print "File exists!" and run command1 and command2. 

Use an alias to create a shortcut with a conditional statement

You can create an alias that runs a command with a certain flag or option only if a certain condition is met. For example, you can create an alias that runs the ls command with the -l option only if a certain file exists. Here's an example:

$ alias ls-if-file-exists='if [ -e ~/my-file.txt ]; then ls -l ~/my-file.txt; else ls; fi'

This alias will check if the file my-file.txt exists in the user's home directory, and if it does, it will run the ls command with the -l option for that file. If the file doesn't exist, it will run the ls command without any options.

Use an alias to create a conditional prompt

You can create an alias that changes the prompt of your terminal based on a certain condition. For example, you can create an alias that changes the prompt to include the current directory only if you're in a certain directory. 

Here's an example:

$ alias my-prompt='if [ "$PWD" == "/path/to/my/directory" ]; then PS1="\[\e[32m\]\w\[\e[m\]\\$ "; else PS1="\[\e[32m\]\\$ "; fi'

This alias will check if the current directory is /path/to/my/directory, and if it is, it will change the prompt to include the current directory in green color. If the current directory is not /path/to/my/directory, it will change the prompt to just show the $ symbol in green color. 

In conclusion, aliases are a powerful tool for system administrators that can greatly increase their productivity and efficiency. By creating custom commands that suit their specific needs, they can save time and avoid making mistakes. 

We hope that the examples and tricks we've provided in this blog have inspired you to try out aliases in your own Linux environment. Experiment with creating your own aliases and see how they can benefit your daily work as a system administrator. 

With a little bit of practice, you'll be able to streamline your workflow and become more productive than ever before.