Aliases are a great way to save time and make your Linux command line experience more efficient. This blog post will teach you how to create and store Alias in Linux.
- Aliases let you create shortcuts to commands
alias dir='ls -laF'
- Use alias by itself to see all set aliases
- Use alias followed by an alias name to see alias value
$alias dir
alias dir='ls -laF'
Alias
Aliases are shortcut names for longer commands. If you have commands that you run often, but take a considerable amount of typing, you can reduce these to an alias:[user@statuionX ~]$ alias lf="ls -FCa"
The alias value must be a single word and so you will almost always want to quote the value as shown.
Even a relatively short command, if executed often, can save considerable typing if reduced to an alias:[user@stationX ~]$ alias c=clear
Aliases can also be used for security purposes to force you to use certain flags:[user@stationX ~]$ alias rm="rm -i"
In this case, if you ever want to use the rm command itself, instead of your alias, you can precede the command with a backslash:[user@stationX ~]$ \rm -r Junk
Example: –
Goal
Create an alias that provides easy acccess to your shell’s history, and which is re-instated automatically for new shells.
- Device a command that would print the last 10 lines of bash’s command history.
Suggested Solution:
history 10 - Create an alias called h that runs the history command devised above:
[amar@amar-pc ~]$ alias h='history 10'
[amar@amar-pc ~]$ alias
alias h='history 10'
..... output omitted .... - Create a new shell (for example, by opening a new tab with Ctrl-Shift-t).
- Attempt to execute the h alias. This should not work. Why?
- Open ~/.bashrc in a text editor and locate the line:
# User specific aliases and functions
Add your alias to the line immediately below:
alias h=’history 10′
save the file and exit.
Note: if not found ./bashrc file then you can create. - Source the newly modified file:
[amar@amar-pc ~]$ source ~/.bashrc
- Attempt to run h again. This time it should succeed. The same should be true if you create another new shell.