Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi all, i saw how possible it was to make custom commands for your console, for example, if you wanted to open a file or program instead of typing the ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    52

    Custom commands

    Hi all, i saw how possible it was to make custom commands for your console, for example, if you wanted to open a file or program instead of typing the full version you would type something shorter and easier to remember. Something like this:

    user@shell~: open firefox

    How would i go about accomplishing this? Thanx in advance!

  2. #2
    Linux Newbie Geeth's Avatar
    Join Date
    Apr 2008
    Location
    Brisbane Aus
    Posts
    176
    Alias is what your looking for.

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    As Geeth said, read about "alias" in the bash man page.

    Its usage is pretty much straightforward for the most part, some examples taken from my own ~/.bashrc:

    Code:
    alias cp='cp -i'
    alias df='pydf'
    alias dvdplay='mount /mnt/dvd; mplayer /mnt/dvd/*; umount /mnt/dvd'
    alias emerge='emerge -Dv'
    alias grep='grep --colour=auto'
    alias h='history'
    alias ls='ls -h'
    alias mc='mc -x'
    alias mv='mv -i'
    alias nano='nano -w'
    alias nwn='cd ~/games/nwn; ./nwn'
    alias rm='rm -i'
    alias startx='startx \"$@\" & exit'
    alias undying='winerun ".wine/drive_c/Archivos de programa/EA Games/undying/System/" "../Undying.exe"'
    alias vbox='sudo /sbin/modprobe vboxdrv; VirtualBox; sudo /sbin/rmmod vboxdrv'
    Put them into ~/.bashrc and/or ~/.bash_profile.

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Posts
    52
    Thanx for you reply i92guboj, very helpful and educational. But i cant find the file bash_profile on my system. Im using Fedora 7 btw. As root im in /root dir. And i cant find those two files you mentioned. Any suggestions?

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Did you include a period in front of the filename. In other words the filename is .bash_profile and .bashrc

    Also these files are known as 'hidden' files so they won't show up in a 'ls -l' command. Use the command 'ls -la'.

  6. #6
    Just Joined!
    Join Date
    Dec 2007
    Posts
    52
    Well i added this to .bashrc

    Code:
    alias mydir='pwd'
    And got an error that the command doesnt exist

    bash: mydir: command not found
    What did i do wrong?

  7. #7
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    The file .bashrc is a script that's executed when you log in. So the new alias won't be created until you log off and back on. That or manually execute the script with the command:

    Code:
    . ./.bashrc

  8. #8
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    You dont have to log off/on after editing .bashrc file. Just source it.
    Code:
    source .bashrc
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  9. #9
    Just Joined!
    Join Date
    Dec 2007
    Posts
    52
    HAH! Thanx devil casper, that worked

  10. #10
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by devils casper View Post
    You dont have to log off/on after editing .bashrc file. Just source it.
    Code:
    source .bashrc
    I know it's kind of an overkill, but even if it's just for the sake of not having to write the dot-file name, I often just do "exec bash", whichi just exit this shell and runs a new one. That, indeed, emulates closing the terminal and opening a new one (forcing in the way the sourcing of the rc file).

    .bashrc and .bash_profile can be created if they don't exist. For the details you can check the bash man page. Basically, ~/.bash_profile is read on login shells, and ~/.bashrc is read on non-login shells (for example, when you open a terminal).

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...