• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

learn very useful alias command with examples

January 25, 2014 by Sharad Chhetri 2 Comments

Here in this post,we will learn very useful alias command with examples.In linux and Unix like operating system,mainly we use the alias command to shorten the long command.It is something like instead of writing long command in terminal,you type a few letters on terminal and your desired command get run.

To understand the meaning of alias,I would like to simply give example from our life.For eg. a person has name very long name like Mr. Ching chong lamb chong lamba bla bla,rather then addressing him we have his alias name called Mr. bla . So here rather than speaking a very long name of person,we are using his alias name that is Mr. bla .

Similarly,if you have very long command like vi /etc/apache2/conf.d/example-file.conf, instead of typing long command.We can make its alias command like viapache. Then only hitting the command viapache, it will open the file /etc/apache2/conf.d/example-file.conf in vi editor .

How to set alias

To set the alias of a command,we have to use following syntax in terminal.

Note: If you run alias command directly into terminal.Once you logout or system get restarted,the alias of a command will be unset itself. To set alias permanently we have to edit ~/.bashrc , /etc/profile,~/.bash_profile depends upon the user requirement.

alias name=[value]

Example 1: To clear the terminal screen

alias cls="clear" 

Example 2: To list directory(including hidden)

alias ldir="ls -la|grep ^d"

Example 3: File modified in last 30 days in /etc folder

alias f30="find /etc/ -iname "*.conf" -mtime -30 -print"

Example 4: We can use any name for alias. Even you can also set alias with your name like alias sharad="ls -lhrt"

Example 5: Combining two or more commands in alias.

alias ldf60="find /etc/ -iname "*.conf" -mtime -30 -print;ls -la|grep ^d" 

How to permanently set alias

Here we will set the alias command for a particular user and all user.

Set permanent alias for particular user

Go to user’s home directory and edit any one of the file called .bash_profile or .profile .and write your alias command in the file

Example;

cd /home/username/

vi ~/.bash_profile

alias cls="clear"

To immediately make the change available ,now run below command .(Note:When the user login back ,the changes will be effective automatically)

source /home/username/.bash_profile 

Set permanent alias for root

Edit the /root/.bashrc file and write your alias command

vi /root/.bash_profile

alias cls="clear"

To make it effective immediately.(Note:When the root user login back and after system reboot,the changes will be effective automatically)

source /root/.bashrc 

To set alias for all users in system

Edit /etc/profile file and write your alias command in it.For doing this change you must be root or the user must have superuser privelege

vi /etc/profile

alias cls="clear" 

Save the file and runt the source command for immediately applying the changes effect

source /etc/profile 

How to unset or remove alias

To remove the particular alias from the system use the below given syntax

unalias alias-name

example: unalias ldf60 (see example 5 in above paragraph)

To remove all alias with single command

unalias -a

Note: In system might have the alias has been set permanently. Check the files /etc/profile and in user’s home directory ~/.bash_profile,~/.profile and ~/.bashrc. In some system. Hence remove or comment the line related to the alias which you want to remove

Tip: You may also use alias with already available internal/external command .
for eg. In this example we have set rm command alias which is interactive. When every time user hit the command rm ,it will ask yes or no for deletion of file/directory

alias rm="rm -i" 

Sometimes,after setting above given alias, you need the removal of many files with interaction.In that case use along with alias name.Then it will be work like your previous internal/external command without interaction.
See below example

rm *.txt 

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Useful tail command with examples in Linux/Unix
  2. Learn Linux Date Command With Examples
  3. Learn Vagrant provision with shell provisioner examples
  4. 6 df Command Examples To Check Mounted Filesystem On Linux
  5. 6 important examples of cd command on Linux and Unix Systems
  6. Learn tar command and know about its precaution
  7. Useful 3 Pastebin links which I mostly use
  8. 30 useful Linux terminal keyboard shortcuts
  9. How To Find Absolute Full Path Of Command On Linux / Unix : which command
  10. type Command : Display Information About Command Type On Linux

Filed Under: Linux, Linux Commands Tagged With: linux command

Reader Interactions

Comments

  1. buster says

    December 29, 2016 at 6:29 pm

    You did a great job on the lesion. which of the bash file the best to set one alias?

    Reply
    • Sharad Chhetri says

      December 30, 2016 at 12:30 pm

      Hi Buster,
      File called /etc/profile will implement the alias for all users.

      Depends upon your requirement you can opt the method.

      Regards
      Sharad

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

How to find when Operating system was installed in linux CentOS and Red Hat

GNU Screen

linux release renew dhcp assigned ip address

Run the script using nohup without hitting enter key two times

postgres database backup script using database user password inside

The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form

Show visitor ip address php code

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy