Originally posted by iStudent 2003:
I want to change the ls command to ls -l. I know i have to use aliases, but I don't know exactly how to them.
It is very common to use 'll' as an alias for 'ls -l'... in fact it is the default on many Unix systems.
First find out which shell you are using because the syntax is slightly different for bash than tcsh. tcsh was the default shell for 10.2 and earlier and bash for 10.3 and later. Type:
echo $SHELL
If you are using the bash shell (10.3 and later) just type:
alias ll='ls -l'
From now on, in that particular terminal window, whenever you type ll at the command prompt it will be translated to ls -l. To remove the alias just type:
unalias ll
To make it permanent so it will work in any shell as that user just add the alias line to your ~/.bashrc file. Whenever you log in the .bashrc file is parsed and the alias will be automatically set. You can have as many aliases as you like in .bashrc, one alias per line. If you want to make it global (the same for all users of the system) just add the alias line to /etc/bashrc. At any time you may type the command alias with no arguments to see the current list of aliases that you have set.
If you are using the tcsh shell (10.2 and earlier) just type:
alias ll ls -l
and it will behave just like the one in bash. To remove it type:
unalias ll
as before.
To make it permanent add it to your ~/.cshrc file. To make it global add it to the /etc/csh.cshrc file. It is really pretty simple.
Some other common aliases are (in bash format):
alias cp='cp -i'
alias l.='ls -d .[a-zA-Z]*'
alias mv='mv -i'
alias rm='rm -i'
Usually it is not a good idea to alias a standard command such as ls to something with different behaviour. Such as:
alias ls='ls -l'
This is because it can have unexpected side effects when strung (piped) together with other commands. It is better to give the alias a different name. In the above example, for instance, there is an alias for rm that remaps rm to rm -i. This is not necessarily a bad idea because it puts the rm command into "interactive mode" and the command will prompt you for each file that you want to delete. If you type something like:
rm *
You will be asked for each and every file:
rm: remove `filename'?
to which you can answer y or n. To escape this behaviour one time just type:
\rm *
and the aliased rm will behave as usual. As always, to escape a command altogether just type Ctrl-c.
I have heard it said that if one has to use a complex command more often than once in a row that it should be aliased.
Another VERY useful feature of the bash shell (I don't know if there is an equivalent in tcsh) is recursive command history editing. Type at the prompt:
Ctrl-r
and then start typing the first few characters of some long or complex command that you typed earlier in your session. The prompt will then start to recursively traverse your command history automatically completing the command for you as you type. You can also edit the line.
Have fun!