When you list files on the Linux command line, many of the file names show up in colors, as you’ve probably noticed. The colors used depend on the file types. For example, the names of directories will show up in a blue font. Here’s a list of the colors you can expect to see and what they represent:
- Blue: directory
- Green: executable or recognized data file
- Cyan: symbolic link file
- Yellow with black background: device
- Magenta (pink): graphic image file
- Red: archive file
- Red with black background: broken link
- Yellow with black background: named pipe
If you’d like to get a quick confirmation on how this works, create an empty directory (e.g., mkdir TEST) and move into it with the cd command. Then run the following commands to create a sampling of the various file types:
mkdir directory
touch script; chmod 750 script
ln -s /tmp symlink
touch image.jpg
touch archive.tgz
mknod named-pipe p
ln -s /tmp/yadda broken_link
mknod named-pipe p
touch file.zip
touch text
Once you run these commands, you will see a colorful listing whenever you run the ls command. The only one of the eight file types mentioned in the bullet list above that will not be set up will be the device file. Run the command “ls /dev/null” to see an example of the colors used for displaying devices.
When you run the ls -l command to get a long listing of the files you just set up in your TEST directory, you will notice that all of the files except two have a file size of 0. This demonstrates that the colors used to display these files are not based on file content but on file names and permissions. The symlink and the broken_link files will have a file size that reflects the length of the file and directory names that they point to, but the content is displayed in the listing itself (e.g., symlink -> /tmp has a size of 4 characters).
Coloring text in your scripts
File listings aren’t the only way to add color to the command line on Linux. You can also create colored text in your scripts. The key to making this easier is having the codes available to display the colors easily accessible. Here’s a list of variables that you could use to give names for a group of colors codes to make the colors easier to use. You might consider putting these lines into a text file that you can source as needed (e.g., with a command like . Colors).
# foreground colors
BLACK="33[30m"
RED="33[31m"
GREEN="33[32m"
YELLOW="33[33m"
BLUE="33[34m"
MAGENTA="33[35m"
CYAN="33[36m"
WHITE="33[37m"
GRAY="33[90m"
PINK="33[91m"
NORMAL="33[0;39m"
To issue a line of text that uses some of these colors, run a command like that shown below.
$ echo -e $GREEN I $YELLOW love $BLUE color $NORMAL
I love color
The -e option with the echo command is described in the man page as “enable interpretation of backslash escapes” but it also allows these color options to work.
How much the letters stand out will, of course, depend on the background color in your window, but you should see the colors selected. The NORMAL setting returns the font color to the default so that you don’t end up displaying all of your text in the last color used.
To avoid having two blank characters in between the words in your phrase, use a command like this instead:
$ echo -e $GREEN I$YELLOW love$BLUE color$NORMAL
I love color
If you want additional colors, you could add these to your Colors file:
LTGREEN="33[32m"
LTYELLOW="33[33m"
LTBLUE="33[34m"
LTMAGENTA="33[35m"
LTCYAN="33[36m"
LTWHITE="33[37m"
NOTE: If ever you mess up and your text colors don’t go back to normal, issue the command below to fix the problem.
$ echo -e $NORMAL
Background colors
You can also use color settings to change the background color of your screen. The command shown below would change your background color after the command is issued, leaving the space above it with the prior background color.
echo -e "33[45m"
echo -e "33[102m"
The command below would reset the colors back to their normal setting – the same as using $NORMAL.
echo -e "33[0;39m"
If you want to add background colors to your Colors file, you could use these entries:
# background colors
BLACKBG="33[40m"
REDBG="33[41m"
GREENBG="33[42m"
YELLOWBG="33[43m"
BLUEBG="33[44m"
MAGENTABG="33[45m"
CYANBG="33[46m"
WHITEBG="33[47m"
GRAYBG="33[100m"
PINKBG="33[101m"
The command below would issue a “Hope it’s fun!” message using pink letters on a blue background.
echo -e $BLUEBG$PINK"Hope it's fun!"$NORMAL
Wrap-up
Changing font and background colors can make messages stand out and can be entertaining when you want to get a deeper look into how colors work in your terminal window. I hope you enjoy dabbling in colored messages!
Source:: Network World