M BUZZ CRAZE NEWS
// general

How do i make dir have colors for files/directories like ls does it?

By John Parsons

Im running Ubuntu 18.04 and I'm very used to typing dir instead of ls as a habit. When i use ls, I get the directory contents listed with different colors for files and directories, as to be expected, however, when I type dir, I get the same thing without colors. I am trying to make dir either use the colors ls uses, or emulate it itself by any means possible. These attempts have been tried unsuccessfully:

  • dircolors
  • Aliasing dir as ls in my .bashrc
  • Trying to find where the dir command/alias is located ( I didn't find it, and had no luck googling its location )
  • putting 'dir --color=always' and 'dir --color=auto' in my .bashrc, which works once, but subsequent calls do not
1

3 Answers

You can alias dir to dir --color by adding the following line to .bashrc

alias dir="dir --color"

Adding just dir --color=always will not create the alias, but will instead run the command when you start a new login shell.

You also need to restart your terminal after modifying the .bashrc file or you can run

source ~/.bashrc

Note: dir --color=always and dir --color are equivalent. The default value for the color param is always when it is omitted.

Hello and welcome to Ask Ubuntu!

man dir points to:

--color[=WHEN] colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never'; more info below

So you're looking for the flag --color as you correctly thought.

Fact is, you should't put the command dir --color in your .bashrc but you should create a new alias:

alias dir='dir --color'

Also, on my system, dir colors are exactly the same as ls.

For enable color support of ls and also add handle another aliases, you can add manual as default color.

if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' alias dir='dir --color=auto'
fi

Hope this helps.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy