Thursday, July 10, 2008

Using Aliases to Make Life Easier

Earlier on, I covered on my bash script that opens the SD Card folder in File Manager. Now, when I'm using the terminal console or shell, I need to go into the SD Card folder at times too. Instead of typing out the long path everytime, eg. cd /home/user/MMC-SD/Kingston, I tried writing a bash script that would change directories for me. That didn't work, as the script will go back to its original directory after exiting. The only way it could work, according to URLs I read up, was to type ". /usr/bin/myscript" or "source /usr/bin/myscript", using "source" and indicating the full path of the script, which IMHO, defeats the purpose of creating a shortcut bash script.

Then, I came across the topic of aliases, which as the name suggests, provide alternative names for calling commands. For example, you can use 'dir' in place of 'ls' even though 'dir' is a DOS command. This is because the alias has been set up in .bashrc. I have added 2 aliases, which I will use as examples later, that go straight into the SD Card folder and into a sub-sub-folder in my SD Card folder.

  1. Press Ctrl-Alt-T to open the terminal console. You should be in /home/user.

  2. Backup your .bashrc in case things mess up. Type
    "sudo cp /home/user/.bashrc /home/user/.bashrc.backup"
    (In case you haven't noticed, filenames starting with '.' are hidden. To see them, type 'ls -la')

  3. Open your .bashrc by typing "sudo nano /home/user/.bashrc".

  4. Go all the way to the bottom and add your aliases. This is what I have:

    #special alias by Zion to change dir to SD Card
    alias cdsd='cd /home/user/MMC-SD/SDHC4GB_Zn'
    alias cdeee='cd /home/user/MMC-SD/SDHC4GB_Zn/Docs/asus-eee'

  5. Press Ctrl-O to save and Ctrl-X to exit.

  6. Type ". /home/user/.bashrc" or "source /home/user/.bashrc" to reload .bashrc.

  7. Now, whenever I type 'cdsd' at the command prompt, it will change to the SD Card folder. The same goes for 'cdeee' (without the quotes).

(^ v ^)

No comments: