View on GitHub

Settings_Linux

Useful bash scripts and settings for managing a Linux server

Instructions on how to add an alias for a complicated command

Last updated 2018-06-28 by Adam Lu


  1. Open a terminal

  2. Type one of the following commands to edit the file ~/.bash_aliases:

    # Use the vim editor (alias for vi ~/.bash_aliases)
    vi_alias
       
    # Use the nano editor (alias for nano ~/.bash_aliases)
    nano_alias
    
  3. Create the alias by adding a line that looks like this:

    alias <alias name>='complicated command'
    

    For example,

    alias store_dotfiles='cd ~/Settings_Linux/scripts; bash store_dotfiles.sh; cd -'
    
  4. To refresh your shell session and apply the new alias, type one of the following commands :

    # Source the ~/.bash_aliases file
    source ~/.bash_aliases
       
    # Equivalently, use the custom alias:
    bash_refresh
    
  5. To store your new ~/.bash_aliases file, type the custom command alias:

    # store_dotfiles is an alias for 'cd ~/Settings_Linux/scripts; bash store_dotfiles.sh; cd -'
    store_dotfiles
    

    This will store your ~/.bash_aliases file as the file ~/Settings_Linux/sample_dotfiles/sample_DOTbash_aliases_<user>.txt, where <user> is your user name.

  6. In the event that your ~/.bash_aliases file is changed (by Adam or anyone else who applied the command alias setup_all_homedirs), you can do one of the following:

    • Restore your settings by typing the following command, where <user> is your user name:

      # setup_dotfiles is an alias for 'bash setup_dotfiles.sh'
      #	and setup_dotfiles.sh is located in /usr/local/bin
      setup_dotfiles <user>
      
    • Compare your new settings with your previously saved settings and edit with vim, where <user> is your user name:

      vimdiff ~/.bash_aliases ~/Settings_Linux/sample_dotfiles/sample_DOTbash_aliases_<user>.txt