Change the color and format of the Bash prompt

Recently I got bored with the standard prompts and colors of the Bash shell in Linux, and decided to tweak them a bit.  What I decided to do was to make the root user account have a red prompt, mainly so I wouldn’t forget to log out once I was done and also to distinguish it from normal user accounts.

Editing the Bash Prompt

In order to change the color of the bash prompt, you need to edit the PS1 environment variable.  You can do this by typing the following in the bash shell:

export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m"


This will temporarily change the color of the bash prompt to red, and some other parts of the prompt as well.

Here is a basic breakdown of each part of the command:

The export PS1= is telling the shell that you are changing the PS1 variable

The \e[31;1m controls what foreground and background the prompt will have. 31 = Red foreground, 1 = black background.

The "[\u@\h \@ \W]#" means “your username”@”hostname” “localtime” “working directory”.

Making PS1 Environment Variable changes Permanent

If you want to make the changes permanent, you will have to append this line of code to the end of your “.bashrc” file in your home directory. You can do this with by editing the .bashrc file with any text editor in linux (nano, vi, vim, gedit) and manually add in the following line export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m" or this way:

1.  Put export PS1="\e[31;1m[\u@\h \@ \W]# \e[0m" into a text file called "filenamehere"
2.  Then type the following command:

cat filenamehere >> ~.bashrc

3.  Then type exit in the terminal
4.  Open up a new terminal and you then should see the changes to the bash prompt.

This effectively uses cat to pint the export PS1 command to standard output in the shell, then with the append operator ">>" it will redirect the export PS1 command and add it to the end of the .bashrc file.  It's a fun and simple shell script! :)   CAUTION!!  Make sure you use the append operator ">>" and NOT the redirection ">" operator which will delete everything in the .bashrc file and replace it with what ever you redirected.

Reverse changes to PS1 Environment Variable
If you ever want to reverse the changes you have made to your PS1 environment variable, do the following:

1.  Open a new terminal window
2.  Type "vim .bashrc"
3.  Use the arrow keys on the keyboard to scroll down to where it has the "Export PS1=" line.
4.  Press the letter "d" twice on the keyboard to delete the whole line.
5.  Press escape
6.  press ":"
7.  after the colon, type "qw" for quit and write.  This will quit vim and save the file.
8.  Exit the terminal
9.  The next time you open a terminal for that user, you will have the default PS1 settings again.

If you would like to get more details and info about editing PS1 environment variables, visit the site in the references section below.  It has a wealth of info on the subject.  Enjoy!!

References:
http://www.funtoo.org/en/articles/linux/tips/prompt/

  • Share/Bookmark

How to prevent yourself from accidentally deleting files in Unix/Linux

Everyone at some point has faced that horrible moment when you realize that you just deleted a critical file, and start scrambling to find ways to recover it before it is too late.  If you haven’t faced this situation yet, you are the lucky few who have been spared from this horrible experience so far.  Just the other day, I found a really creative solution in Linux that will prevent you from accidentally deleting files by mistake.  It’s actually not that complicated either, and I am sure you will be using this technique to protect your most valuable files after your through reading my how-to.

Step 1 – Choose a Directory

HOWTO-prevent-accidental-delete

As you can see, I have created a directory called “test” and have created a ton of empty files and directories for testing purposes.  It was fun writing the Perl script to create all those “crap” files.

Step 2 – Create a empty file call “-i” in the chosen directory

HOWTO-prevent-accidental-delete-2

To do this simply use the touch command to create the empty file “-i”, as such: “touch ./-i”

Step 3 – Verify that “-i” exists in the current directory

HOWTO-prevent-accidental-delete-3.1

To make sure “-i” was created successfully, list the directory contents by invoking the “ls” command as show above.  Once you do that you will notice a new file called “-i” listed in the current directory.

Step 4 – Try to delete the files in the current directory

HOWTO-prevent-accidental-delete-3.9

Try deleting all of them at once by using “rm *”.

WARNING – make sure you have the “-i” the the directory before you try to remove any files!  Without the “-i” file there will be nothing to stop the rm command from removing your precious files!!

Below is a screenshot of what should happen when you try to delete any file in that directory.

HOWTO-prevent-accidental-delete-4

Did you notice what happened?  When we tried to delete any file in that directory, It would prompt us to confirm if we really wanted to delete each file.  This happens because the “-i” file is immediately interpreted by the Unix/Linux command line as a option for the “rm” command, which prompts before deleting a file.  If you man rm and look through the man page you will find the following entry:

  -i, --interactive
	      prompt before any removal

So that is the magic behind creating the empty “-i” file and how it helps prevent you from deleting file by mistake. Now don’t forget that if you use the rm -f command, it will delete the file no matter what!! That’s why it’s called the FORCE option!

Use this technique to your advantage and spread the word!

  • Share/Bookmark

Understanding Unix/Linux Load Averages

I was looking at some linux server the other day, and was taking a look of the load averages using the “top” command.  After staring at the load averages for a while, I thought to myself ‘What does that mean exactly?” and “How do they get those numbers?”.  Well after some research I have found out that the load average is not a % of cpu usage, but is a representation of the load on the CPU by processes waiting for their time to use the CPU.

Below is a screenshot of the top command for reference.  Notice in the top right are the load averages “1.23″ 1.17″ “1.05″.  The first load average is for 1 minute, the middle is for 5 minutes, and the last one is for 15 minutes.

top and load averages

Now the important thing to remember about Unix/Linux load averages is that it is not a % of cpu usage, but how many processes are waiting to use the cpu.  The higher the load average, the more load the server in under.  Right about now you are probably wondering ” well what is a normal load average?” or “What is a high load average?”, the easiest way to determine if your load average is normal or high is to look at it in the following way:

CPU Cores Normal Load Average
1 1.00
2 2.00
3 3.00
4 4.00
8 8.00
16 16.00

As you can see a normal load average for a single core cpu is 1.00 while a dual core cpu can handle a load average of 2.00.  If it helps you out, you can think of 1.00 to be %100 for a single core and 2.00 be %100 for a dual core, and so on.

top showing each individual core (8 Cores)

Above is a screenshot of top showing each individual cores ultilization.  This is an 8 core cpu, so it’s normal load average is 8.00.  You can see this output in top by pressing “1″ on the keyboard.  Also, if you were wondering how I got top to display different colors, just press “z” on the keyboard.

The key thing to remember is that load averages in Unix/Linux are not % for cpu utilization, it is merely a representation of how many processes are waiting for their turn to use the processor.

References
http://www.linuxjournal.com/article/9001

http://blog.raamdev.com/2008/11/22/understanding-the-linux-load-averages

  • Share/Bookmark