Command aliases in linux

Linux alias command: How to create and use Linux aliases

Unix/Linux aliases FAQ: Can you share some examples of the Linux alias command?

Using Linux aliases

Aliases in Unix and Linux operating systems are cool. They let you define your own commands, or command shortcuts, so you can customize the command line, and make it work the way you want it to work. In this tutorial I’ll share several Linux aliases that I use on a daily basis.

Alias examples

I’ve found it very helpful to create aliases to make my command line life easier. For instance, a lot of people don’t like the name of the grep command, and wish they could change it to search . With the alias command you can do exactly that:

Now you can just type search instead of grep at your Linux command line:

search 'Flinstone' StoryOfBedrock.txt

In another simple alias example, instead of always typing this ls command to get a directory listing:

I’ve created an alias so I only have to type the lowercase letter «L» like this:

Whenever I use this alias, it’s exactly the same as if I had typed out the longer ls -al Linux command.

Using aliases like this you can create anything from simple shortcuts like this to powerful custom commands.

How to define a Linux alias

Creating a Linux alias is very easy. You can either enter them at the command line as you’re working, or more likely, you’ll put them in one of your startup files, like your .bash_profile or .bashrc files, so they will be available every time you log in.

I created the l alias above by entering the following command into my .bash_profile file:

As you can see, the Linux alias syntax is very easy:

  1. Start with the alias command
  2. Then type the name of the alias you want to create
  3. Then an = sign, with no spaces on either side of the =
  4. Then type the command (or commands) you want your alias to execute when it is run. This can be a simple command, or can be a powerful combination of commands.

Unix/Linux alias examples and syntax

To get you going, here is a list of sample Linux aliases I use all the time. I’ve pretty much just copied them here from my .bashrc file:

alias dirs="ls -al | grep '^d'" alias l="ls -al" alias lf="ls -aFG" alias lm="ls -al|more" alias h="history" alias html="cd /web/apache/htdocs/devdaily/html" alias logs="cd /web/apache/htdocs/devdaily/logs" alias qp="ps auxwww|more" alias nu="who|wc -l" # GIT alias gpom="git push origin master" alias gs="git status" alias gb="git branch" alias gco="git checkout"

As you can see, you can get as creative as you want, and pipe commands together to do just about anything. In the last alias shown I’ve actually combined three Linux commands in a row into one alias to get the output I want.

Читайте также:  How to install telegram desktop linux

Because the Unix shell is very programmable and because the output of commands is very consistent and reliable, you can create your own aliases (or macros if you prefer) to do just about anything.

Have a favorite Linux alias?

If you have a favorite Linux alias command/example you’d like to share, feel free to leave it in the comments section below.

Источник

How to Create and Use Alias Command in Linux

Linux users often need to use one command over and over again. Typing or copying the same command again and again reduces your productivity and distracts you from what you are actually doing.

You can save yourself some time by creating aliases for your most used commands. Aliases are like custom shortcuts used to represent a command (or set of commands) executed with or without custom options. Chances are you are already using aliases on your Linux system.

List Currently Defined Aliases in Linux

You can see a list of defined aliases on your profile by simply executing alias command.

Here you can see the default aliases defined for your user in Ubuntu 18.04.

List Aliases in Linux

You can create an alias with a single character that will be equivalent to a command of your choice.

How to Create Aliases in Linux

Creating aliases is relatively easy and quick process. You can create two types of aliasestemporary ones and permanent. We will review both types.

Creating Temporary Aliases

What you need to do is type the word alias then use the name you wish to use to execute a command followed by » your custom command here»

Here is an actual example:

You can then use «wr» shortcut to go to the webroot directory. The problem with that alias is that it will only be available for your current terminal session.

If you open new terminal session, the alias will no longer be available. If you wish to save your aliases across sessions you will need a permanent alias.

Creating Permanent Aliases

To keep aliases between sessions, you can save them in your user’s shell configuration profile file. This can be:

The syntax you should use is practically the same as creating a temporary alias. The only difference comes from the fact that you will be saving it in a file this time. So for example, in bash, you can open .bashrc file with your favorite editor like this:

Читайте также:  Настройка графики в linux

Find a place in the file, where you want to keep the aliases. For example, you can add them in the end of the file. For organizations purposes you can leave a comment before your aliases something like this:

#My custom aliases alias home=”ssh -i ~/.ssh/mykep.pem [email protected]” alias ll="ls -alF" 

Save the file. The file will be automatically loaded in your next session. If you want to use the newly defined alias in the current session, issue the following command:

To remove an alias added via the command line can be unaliased using unalias command.

$ unalias alias_name $ unalias -a [remove all alias]
Conclusion

This was a short example on how to create your own alias and execute frequently used commands without having to type each command again and again. Now you can think about the commands you use the most and create shortcuts for them in your shell.

Источник

Linux alias Command: How to Use It With Examples

Depending on the type of work you do on your Linux system, you may need to enter the same long and complicated commands frequently. The alias command lets you create shortcuts for these commands, making them easier to remember and use.

In this tutorial, we will show you how you can create, review, and remove command aliases in Linux.

Linux alias command

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window or command line
  • A text editor, such as Vim or nano

What Is an Alias in Linux?

In Linux, an alias is a shortcut that references a command. An alias replaces a string that invokes a command in the Linux shell with another user-defined string.

Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors. Aliases can also replace commands with additional options, making them easier to use.

Linux Alias Syntax

The alias command uses the following syntax:

The different elements of the alias command syntax are:

  • alias : Invokes the alias command.
  • [option] : Allows the command to list all current aliases.
  • [name] : Defines the new shortcut that references a command. A name is a user-defined string, excluding special characters and ‘alias’ and ‘unalias’, which cannot be used as names.
  • [value] : Specifies the command the alias references. Commands can also include options, arguments, and variables. A value can also be a path to a script you want to execute.

Note: Enclosing the value in single quotation marks () will not expand any variables used with the command. To expand the variables, use double quotation marks («).

Create Aliases in Linux

There are two types of aliases to create in Linux:

  • Temporary. Add them using the alias command.
  • Permanent. These require to edit system files.
Читайте также:  Локальный репозиторий oracle linux

Create a Temporary Alias in Linux

Use the alias command to create a temporary alias that lasts until the end of the current terminal session. For instance, creating c as an alias for the clear command:

Note: The alias command allows you to include multiple commands as the value by dividing them with the pipe symbol (|).

If you want to reference any additional command options when creating an alias, include them as a part of the value. For example, adding move as an alias for the mv command with the option of asking for confirmation before overwriting:

Note: Learn more about the mv command in our guide to moving directories in Linux.

Another use for aliases is to create a shortcut for running scripts. To do this, provide the absolute path to the script as the value:

alias frename='Example/Test/file_rename.sh'

In this example, using frename as a command runs the file_rename.sh bash script.

Create a Permanent Alias in Linux

To make an alias permanent, you need to add it to your shell configuration file. Depending on the type of shell you are using, use:

Start by opening the shell configuration file in a text editor. In this example, we are using the Bash shell and nano text editor:

Scroll down until you find a section that lists default system aliases. For ease of use, create a separate section with a descriptive comment and add your aliases using the alias command syntax.

#Custom aliases alias c='clear' alias move='mv -i' alias frename='Example/Test/file_rename.sh' 

Adding custom aliases to the bash configuration file

Once you add all of the new alises, press Ctrl+X, type Y and press Enter to save the changes to the configuration file.

The new aliases automatically load in the next terminal session. If you want to use them in the current session, load the configuration file using the source command:

List All Aliases in Linux

Using the alias command on its own displays a list of all currently set aliases:

Listing all current aliases in Linux

Another method is to add the -p flag. This option displays the list in a format suitable for input to the shell:

Remove Aliases in Linux

To remove an alias, use the unalias command with the following syntax:

For instance, to remove the frename alias:

Removing an alias using the unalias command

Adding the -a option allows you to remove all aliases:

Removing all aliases using the unalias command

The example above shows how alias does not return any results after the unalias -a command.

After reading this tutorial, you should be able to use the alias command to create and manage aliases on your Linux system. This will help streamline your work and make terminal commands easier to use.

To learn more about other commands in Linux, check out our Linux commands cheat sheet.

Источник

Оцените статью
Adblock
detector