Bash (Unix shell)

How to Change the Colors of Directories and Files in the ls Command

If you’ve run the ls command in Bash, you’ll notice that the directories and files you see are colorized according to their type. You can customize your own color scheme to choose different text colors, background colors, and formatting like bold and underline.

How This Works

The color scheme is stored in the LS_COLORS variable. To view your current color scheme, you can tell the Bash to print the contents of the variable:

echo $LS_COLORS

You’ll see a long list of file types and number codes. We’ll explain how to create a list like this yourself.

Before playing around with this, we recommend saving the current contents of the LS_COLORS variable to another variable. This will allow you to quickly restore the default settings without signing out of the shell and signing back in, or closing and reopening the terminal window. To save the current content of the LS_COLORS variable to a new variable named ORIGINAL, run:

ORIGINAL=$LS_COLORS

At any time, you can run the following command to undo your changes and restore the default colors:

LS_COLORS=$ORIGINAL

Your changes are always temporary until you edit a file to make them your new defaults. You can always sign out and sign back in or close and reopen a terminal window to restore the colors to their default setting. However, this makes it easy to do so with a single, quick command.

How to Set Custom Colors

The LS_COLORS variable contains a list of file types along with associated color codes. The default list is long because it specifies different colors for a number of different file types.

Let’s start a basic example to demonstrate how this works. Let’s say we want to change the color of directories from the default bold blue to bold red. We can run the following command to do so:

LS_COLORS="di=1;31"

The di=1;31 bit tells ls that directories (di) are (=) bold (1;) red (31).

However, this is just a very simple LS_COLORS variable that defines directories as one color and leaves every other type of file as the default color. Let’s say we want to make files with the .desktop file extension an underlined cyan color, as well. We can run the following command to do so:

LS_COLORS="di=1:31:*.desktop=4;36"

This tells ls that directories (di) are (=) bold (1;) red (31) and (:) any file ending in .desktop (*.desktop) is (=) underlined (4;) cyan (36).

This is the process for assembling your list of file types and colors. Specify as many as you like in the form filetype=color, separating each with a colon (:) character.

To assemble your own list, you’ll just need to know the list of color codes and file type codes. This uses the same numerical color codes you use when changing the color in your Bash prompt.

How to Change the Colors of Directories and Files in the ls Command

If you’ve run the ls command in Bash, you’ll notice that the directories and files you see are colorized according to their type. You can customize your own color scheme to choose different text colors, background colors, and formatting like bold and underline.

How This Works

The color scheme is stored in the LS_COLORS variable. To view your current color scheme, you can tell the Bash to print the contents of the variable:

echo $LS_COLORS

You’ll see a long list of file types and number codes. We’ll explain how to create a list like this yourself.

Before playing around with this, we recommend saving the current contents of the LS_COLORS variable to another variable. This will allow you to quickly restore the default settings without signing out of the shell and signing back in, or closing and reopening the terminal window. To save the current content of the LS_COLORS variable to a new variable named ORIGINAL, run:

ORIGINAL=$LS_COLORS

At any time, you can run the following command to undo your changes and restore the default colors:

LS_COLORS=$ORIGINAL

Your changes are always temporary until you edit a file to make them your new defaults. You can always sign out and sign back in or close and reopen a terminal window to restore the colors to their default setting. However, this makes it easy to do so with a single, quick command.

How to Set Custom Colors

The LS_COLORS variable contains a list of file types along with associated color codes. The default list is long because it specifies different colors for a number of different file types.

Let’s start a basic example to demonstrate how this works. Let’s say we want to change the color of directories from the default bold blue to bold red. We can run the following command to do so:

LS_COLORS="di=1;31"

The di=1;31 bit tells ls that directories (di) are (=) bold (1;) red (31).

However, this is just a very simple LS_COLORS variable that defines directories as one color and leaves every other type of file as the default color. Let’s say we want to make files with the .desktop file extension an underlined cyan color, as well. We can run the following command to do so:

LS_COLORS="di=1:31:*.desktop=4;36"

This tells ls that directories (di) are (=) bold (1;) red (31) and (:) any file ending in .desktop (*.desktop) is (=) underlined (4;) cyan (36).

This is the process for assembling your list of file types and colors. Specify as many as you like in the form filetype=color, separating each with a colon (:) character.

To assemble your own list, you’ll just need to know the list of color codes and file type codes. This uses the same numerical color codes you use when changing the color in your Bash prompt.

How to Customize (and Colorize) Your Bash Prompt

Most Linux distributions configure the Bash prompt to look something like username@hostname:directory$ . But you can configure the Bash prompt to contain whatever you like, and even choose whatever colors you like.

The example steps here were performed on Ubuntu 16.04 LTS. The process should be the same on other Linux distributions, although the default Bash prompt and settings in the .bashrc file may be a bit different.

Where the Prompt Variable is Stored

Your Bash prompt configuration is stored in your user account’s .bashrc file, which is at ~/.bashrc. So, if your username is bob, the file is at /home/bob/.bashrc.

You can open the file to view the current Bash variable. We’ll use nano as our example text editor, although you could also use vi, emacs, or any other text editor you’re comfortable with. Open a Terminal and run:

nano ~/.bashrc

Scroll down to the PS1= section. The first variable looks rather complicated because it includes color information—we’ll explain that later. The second variable, without color information, reads as follows:

${debian_chroot:+($debian_chroot)}\u@\h:\w\$

This is still a little complicated due to the ${debian_chroot:+($debian_chroot)} bits. These just tell Bash to let you know if you’re using a Debian chroot environment and normally won’t be shown. Ignoring those, here’s the default structure of the Bash prompt variable:

\u@\h:\w\$

\u indicates your username, @ indicates the @ sign, \h indicates the hostname (computer name), : indicates the : character, \w indicates the working directory, and \$ indicates a $ if you’re a normal user account or # if you’re root. So, putting that all together, you get username@hostname:working_directory$.

To change your Bash prompt, you just have to add, remove, or rearrange the special characters in the PS1 variable. But there are many more variables you can use than the default ones.

Leave the text editor for now—in nano, press Ctrl+X to exit. We’ll show you how to experiment with variables before actually writing a new one into your .bashrc file.

How to Create a Custom Bash Prompt

Your Bash prompt configuration is stored in the PS1 variable. To save the contents of the PS1 variable into a new variable, run the following command:

DEFAULT=$PS1

You can now set the PS1 variable to different values to experiment. For example, the first line here would set your prompt to a basic “user$” prompt, while the second would set your prompt to a basic “user:working_directory$” prompt.

PS1="\u\$ " PS1="\u:\w\$ "

If you ever want to get back to your default prompt, just run the following command.

PS1=$DEFAULT

Bash will be restored to its default prompt thanks to the fact that you saved those default settings earlier. Note that any changes you make here are only temporary for the current Bash session, so you can always sign out and sign back in or close and reopen the terminal window to go back to your default prompt. But the above line makes it possible to easily get back to your default Bash prompt without the hassle of signing out or closing a window.

You can add any characters or text to the variable. So, to prefix the default prompt with “Hello World”, you could use:

PS1="Hello World \u@\h:\w\$ "

Now that you’ve got the basics down, you just need to know what all the special characters are. You probably won’t care about many of these, but here’s the full list as it appears in the Bash manual:

  • A bell character: \a
  • The date, in “Weekday Month Date” format (e.g., “Tue May 26”): \d
  • The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required: \D{format}
  • An escape character: \e
  • The hostname, up to the first ‘.’: \h
  • The hostname: \H
  • The number of jobs currently managed by the shell: \j
  • The basename of the shell’s terminal device name: \l
  • A newline: \n
  • A carriage return: \r
  • The name of the shell, the basename of $0 (the portion following the final slash): \s
  • The time, in 24-hour HH:MM:SS format: \t
  • The time, in 12-hour HH:MM:SS format: \T
  • The time, in 12-hour am/pm format: \@
  • The time, in 24-hour HH:MM format: \A
  • The username of the current user: \u
  • The version of Bash (e.g., 2.00): \v
  • The release of Bash, version + patchlevel (e.g., 2.00.0): \V
  • The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable): \w
  • The basename of $PWD, with $HOME abbreviated with a tilde: \W
  • The history number of this command: \!
  • The command number of this command: \#
  • If the effective uid is 0, #, otherwise $: \$
  • The character whose ASCII code is the octal value nnn: \nnn

The Best Keyboard Shortcuts for Bash (aka the Linux and macOS Terminal)

Bash is the default command-line shell on most Linux distributions, from Ubuntu and Debian to Red Hat and Fedora. Bash is also the default shell included with macOS, and you can install a Linux-based bash environment on Windows 10.

The bash shell features a wide variety of keyboard shortcuts you can use. These will work in bash on any operating system. Some of them may not work if you’re accessing bash remotely through an SSH or telnet session, depending on how you have your keys mapped.

Want to Learn More? Get Linux Training from the Linux Foundation

Learn everything you could want to know about Linux with comprehensive online Linux courses and certification tests that include the real-world experience and first-hand knowledge of what it takes to be a successful Linux developer or system administration professional, all from a web browser in the comfort of your home.

The Linux Foundation is the nonprofit consortium dedicated to fostering the growth of Linux. Founded in 2000, they are supported by the leading technology companies and developers from around the world.

Working With Processes

Use the following shortcuts to manage running processes.

  • Ctrl+C: Interrupt (kill) the current foreground process running in in the terminal. This sends the SIGINT signal to the process, which is technically just a request—most processes will honor it, but some may ignore it.
  • Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process. To return the process to the foreground later, use the fg process_name command.
  • Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.

Controlling the Screen

The following shortcuts allow you to control what appears on the screen.

  • Ctrl+L: Clear the screen. This is similar to running the “clear” command.
  • Ctrl+S: Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
  • Ctrl+Q: Resume output to the screen after stopping it with Ctrl+S.

Moving the Cursor

Use the following shortcuts to quickly move the cursor around the current line while typing a command.

  • Ctrl+A or Home: Go to the beginning of the line.
  • Ctrl+E or End: Go to the end of the line.
  • Alt+B: Go left (back) one word.
  • Ctrl+B: Go left (back) one character.
  • Alt+F: Go right (forward) one word.
  • Ctrl+F: Go right (forward) one character.
  • Ctrl+XX: Move between the beginning of the line and the current…