If you're going to make dir
an alias, I suggest alias dir='dir --color=auto -l'
. That is probably the behavior you are looking for. This has the benefit of maintaining the other aspects of how dir
is different from ls
, and of not relying or--nor even being affected--by any ls
alias you might have.
You can define aliases and shell functions that have the same name as the name of an external command (like ls
and dir
), and they can be defined to call that external command with additional options passed to it. That's what the alias suggested above does. So perhaps you used to have a dir
alias (or shell function) that ran dir -l
.
When that isn't done, though, dir
works like ls -Cb
. The ls
and dir
commands are different. The purpose of the dir
command is to be a command like ls
whose behavior is unaffected by whether or not it is being run in a terminal. For this reason, you might consider not making dir
an alias at all, and simply using ls
or ll
instead. ll
is a commonly defined alias for ls -l
.
While alias expansion is not determined by whether or not the output is a terminal, it is determined mostly by whether or not the shell in which you run the command is interactive. (There are ways to override that, but usually you don't want to.) Furthermore, --color=auto
colorizes only when the output device is a terminal, so that you don't get usually undesired color codes if you (for example) redirect the output to a file.
However, if you want to change what happens when you run dir
, then the reason I suggest making it an alias of dir --color=auto -l
is that this adds the options you're asking for, while still implicitly using -C
and -b
. The presence of -C
is not important, since it's overridden by -l
. -C
means to display in columns, and -l
means to display as a detailed list. It can't do both so it does whatever the last option you pass it tells it to do. But -b
is still significant; it causes weird characters in filenames to be escaped, so that you can usually copy filenames shown into terminal commands without modification.
Of course, you could instead use alias dir='ls -bl --color=auto'
. This will achieve the same effect (you needn't include -C
as it is overridden by -l
anyway, as explained above). Or at least it would have the same effect if you had no ls
alias. Alias expansion is not fully recursive, which is why you can alias ls
to a command that starts with ls
(or dir
to a command that starts with dir
). But alias expansion nonetheless continues until either the result is not an alias or further expansion would produce a recursive cycle. Defining your dir
alias not in terms of ls
frees you from unexpected effects resulting from whatever ls
may be aliased to.
As for where to put the alias definition, you certainly can put it directly in the .bashrc
file residing in your home directory, as mikewhatever suggests. In this case, that has the benefit that you already almost certainly have alias definitions for ls
in that file, and you could put your dir
alias near there. Your .bashrc
file may even already have commented-out (with #
) aliases that you can uncomment and use.
However, I suggest instead putting new alias definitions--the ones you write yourself--in .bash_aliases
. The default .bashrc
files in Ubuntu will source that file if it exists. That is, .bashrc
usually contains:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
If for some reason your .bashrc
file doesn't have that, then you can add it (or just the lines that don't start with #
, if you don't feel like reproducing the comments).