How to use the command 'dircolors' (with examples)
The ‘dircolors’ command is used to output commands that set the LS_COLOR environment variable and style the ’ls’, ‘dir’, etc. commands. By using this command, users can customize the coloring of files and directories in their terminal.
Use case 1: Output commands to set LS_COLOR using default colors
Code:
dircolors
Motivation: By running the ‘dircolors’ command without any arguments, it will output a set of commands to set the LS_COLOR environment variable using the default colors. This can be useful when a user wants to restore the default colors for file types and extensions in their terminal.
Explanation: The command ‘dircolors’ without any arguments will read the user’s configuration file (usually ~/.dircolors) and output a set of commands in the format “LS_COLORS=‘code:colors’”. These commands can then be used to set the LS_COLOR environment variable.
Example output:
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33'
export LS_COLORS
Use case 2: Output commands to set LS_COLOR using colors from a file
Code:
dircolors path/to/file
Motivation: By specifying a path to a file as an argument to the ‘dircolors’ command, it will output a set of commands to set the LS_COLOR environment variable using the colors defined in that file. This allows users to customize the colors for file types and extensions according to their preferences.
Explanation: The command ‘dircolors’ with a path argument will read the specified file and output a set of commands in the format “LS_COLORS=‘code:colors’”. These commands can then be used to set the LS_COLOR environment variable.
Example output:
LS_COLORS='rs=0:di=01;34:ln=01;36:ex=01;32'
export LS_COLORS
Use case 3: Output commands for Bourne shell
Code:
dircolors --bourne-shell
Motivation: By using the ‘–bourne-shell’ argument with the ‘dircolors’ command, it will output a set of commands specifically for the Bourne shell (sh). This allows users who use the Bourne shell as their default shell to easily set the LS_COLOR environment variable.
Explanation: The ‘–bourne-shell’ argument instructs the ‘dircolors’ command to output the commands in a format compatible with the Bourne shell. The output will be a series of ’export’ commands that set the LS_COLORS environment variable.
Example output:
export LS_COLORS='rs=0:di=01;34:ln=01;36'
Use case 4: Output commands for C shell
Code:
dircolors --c-shell
Motivation: By using the ‘–c-shell’ argument with the ‘dircolors’ command, it will output a set of commands specifically for the C shell (csh). This allows users who use the C shell as their default shell to easily set the LS_COLOR environment variable.
Explanation: The ‘–c-shell’ argument instructs the ‘dircolors’ command to output the commands in a format compatible with the C shell. The output will be a series of ‘setenv’ commands that set the LS_COLORS environment variable.
Example output:
setenv LS_COLORS 'rs=0:di=01;34:ln=01;36'
Use case 5: View the default colors for file types and extensions
Code:
dircolors --print-data
Motivation: By using the ‘–print-data’ argument with the ‘dircolors’ command, it will output the default colors for file types and extensions. This is useful when a user wants to see the current color assignments and definitions.
Explanation: The ‘–print-data’ argument instructs the ‘dircolors’ command to output the default color assignments and definitions for file types and extensions. The output will be a list of color codes and their corresponding file types and extensions.
Example output:
FILE 00;38;5;244 # regular file
DIR 01;34 # directory
LINK 01;36 # symbolic link