Linux Alias Command

The Linux alias command is used to set command aliases, allowing users to define their own command shortcuts.

It enables you to execute commands in a simpler and more memorable way without having to type the full command each time.

If you simply enter alias, it will list all currently set aliases.

The effect of alias is only valid for the current login session; if you want it to be effective every time you log in, you can set the command aliases in .profile or .cshrc.

Syntax

alias [alias_name]=[command_name]

Parameter Description: If no parameters are added, it will list all currently set aliases.

Examples

1. Create an alias:

alias rm='rm -i'

This command adds the interactive delete parameter to rm, prompting for confirmation each time a file is deleted to avoid accidental deletion.

2. Display aliases:

alias

This command will display all aliases and their corresponding commands on the current system.

To delete an alias:

unalias rm

This command will delete the alias named rm.

3. Execute commands with root privileges:

alias sudo='sudo '

This command creates an alias named sudo so that you can execute commands with root privileges by prefixing the command with sudo.

Note: There is a space at the end of the command because without it, the command executed with root privileges cannot be recognized correctly.

4. Add timestamps to history:

alias history='history | awk "{CMD="date +\"[%Y-%m-%d %H:%M:%S]\"; print CMD " " $0 }" | cut -c 29-'

This command creates an alias named history that adds a timestamp when you execute the history command.

Note: This command contains multiple single and double quotes that need to be properly escaped.

5. Enable color output:

Using the “–color=auto” parameter in the alias command can automatically enable color output in terminals that support it, improving command line readability. For example:

alias ls='ls --color=auto'

This command creates a simplified ls command that automatically enables color output.

6. Create a “fake delete” alias to move files to the trash:

alias rm='mv -t ~/.trash'

Leave a Comment