2012/07/23

List Your Personal Scripts from the Terminal, Alias Solution


last edit: 2012-0723

Notes: If you see brackets "[   ]" in code, that indicates you supply your own variable in place of the brackets. Example: [date] = You type in "2012-0723". Quotes do not have to be included.

One of the things I enjoy about Linux is the fun and power of the scripting with bash and python. However there comes a time when you accumulate so many scripts that you can't remember their names to run them. One solution is to create a keyboard short cut that lists all the scripts in your script directory. So instead of typing:
> clear; ls -l /home/chad/Documents/scripts
The short cut would be:
> lls
 Aliases are created in two places: /etc/bash.rc or ~/.bash_aliases (~ is a shortcut for your home directory, ex. /home/chad). For this example we will add entries to /etc/bash.rc will allow short cuts to all users including root.

Backup /etc/bash.rc in case you make a mistake
> cp /etc/bash.rc /etc/bash.rc.[date].bak or
> cp /etc/bash.rc /etc/bash.rc.2012-0723.bak
 Edit bash.rc with your favorite text editor
> nano /etc/bash.rc
or if using gnome shell or default Ubuntu
> gksudo gedit /etc/bash.rc

Find section labeled "# Alias definitions." and add your alias commands below this section. My examples are below. The first example lists all files in my script directory.
alias lls='clear; ls -l --almost-all --classify /home/chad/Documents/bin'
alias ll='ls --almost-all -l --classify'  # classify indicates file type
alias la='ls --almost-all'           # long, shows hidden files
alias l='ls -C --classify' #Columns, file types
Next save your file and then update your alias variables so that your terminal shell will we aware of them. You update your bash.rc file using the "source" shell command. The source command is useful to load function or variables stored in another file.
> source /etc/bash.rc
Now test your new alias shortcut and you should see all the files listed in your scripts directory.
> lls
You can change formatting of the ls command. See man ls for options.

No comments:

Post a Comment