Thursday, 18 June 2015

How to clear terminal history in Linux


Yup you read it correctly. How would you clear terminal history in Linux? Before we see how that is done lets address even better question why would be clean our command line history? I am not going to answer this  question for you :). It is same as asking why would you clear your browser history or launch incognito mode ? Sometime we have too - that's all I have.

 For Linux users who have not heard about this command just type history in your console and you can see your previously typed commands  there. 

I personally use it a lot. Specially to SSH to various linux servers (who would remember IP address of each). I simply do 

  • history | grep ssh
or even better option would be
  • (Ctrl + R) Start typing your command.
 But let's stick to our agenda for today. We will see how to clear our terminal history.

Clear terminal history in Linux

To clear the entire history you will have to execute command
  • history -c   OR
  •  history -cw

Note : This may just clear history for current terminal and may not be reflected to in other terminals. This did not work for me on my Ubuntu 14.04 machine. 

Deleting the source

Let's go a step ahead. All the bash history is stored in file 
  • ~/.bash_history
Go ahead and read that file. You can see your history getting saved there. So now you know the source. You can simply delete the history from this file.

Note : ~ notation is shortcut for current user. History is stored in a file called bash_history which is in root folder of the user.

To clear this file simply use following command
  • >~/.bash_history
Note : You will have to relaunch terminal to see the changes.

More details on your history File

Now you know where your history file is. Root folder of your user with file name - bash_history. But this is stored in your environment variable $HISTFILE
  • echo $HISTFILE
For me I get following result :

aniket@aniket-Compaq-610:~$ echo $HISTFILE

So to disable logging history what you can do is use following commands -

  • unset HISTFILE OR
You can also do
  • Turn Off : set +o history
  • Turn On : set -o history

Related Links

Sunday, 14 June 2015

Using Shared Preferences in your Android Application

Shared Preferences

Shared preferences are like sessions (if you can relate to sessions running in your browser). For eg. when you are shopping on a site like Amazon or Flipkart you don't have to login to browse each item, do you? That's because you are in a session and the session data is stored in cookies in your browser. It will get invalidated when you logout or when the time expires. Shared preferences in Android are something like that. You can save or persist your settings across activities when user navigates across them. One major difference from sessions though is that sessions expire where as shared preference is a persistence storage. We will shortly see the code for it.

NOTE : Shared preference is a persistence storage.

When to use shared preferences?

If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. (Source)


Lets set our goal first before we begin to write code. We are going to store setting in shared preference which will be used to play music or sound. So User can choose whether he wants to listen to the music or not.

Getting Started with using PreferenceActivity

Lets write our own class that extends PreferenceActivity

public class MyPreferenceActivity extends PreferenceActivity {
    private static final String OPT_MUSIC = "music";
    private static final Boolean OPT_SOUND_DEF = true; //default value of the setting

    protected void onCreate(Bundle savedInstanceState) {
    public static boolean getSound(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_MUSIC, OPT_SOUND_DEF);

Note : Notice the getSound() method that returns the boolean ? We will use that to see what setting is currently set. Also note it is static so we can directly call it using the class name. Note the use of PreferenceManager here and how we supply the default value (true in this case).

Loading Settings

As you can see above piece of code gets settings from an xml file named "settings". Inside folder res/xml  create a file named settings.xml and populate it with following content - 

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="" >

        android:title="@string/music_setting_title" />


In above xml I am showing a simple check box preference. It will be as simple as user checking and unchecking the checkbox to enable/disable music.

Displaying Settings

Now that we have coded for the music setting lets create a workflow to show it to the user. You can put in anywhere. For simplicity I am assuming it is one of the options in the options menu. Lets call it Settings (see 3rd option in below screenshot).

Now on click of it we should open the shared preference intent that we have created above. Code to do that is as follows - 

    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            Log.i(TAG, "Showing Preference Activity");
            startActivity(new Intent(this, MyPreferenceActivity .class));
        return true;

Now on click you should see another activity with a checkbox to enable/disable sound/music (Like below).

Obeying the Setting

That's the code to create and display the setting but we also need to obey our music playing logic according to this setting. So lets go ahead and see how that works.

        if(MyPreferenceActivity .getSound(context)) {
            try {
                final ToneGenerator tg = new ToneGenerator(
                        AudioManager.STREAM_NOTIFICATION, 100);
            } catch (Exception ex) {
                Log.e(TAG, "Could not play notification sound", ex);

What above code does is very simply. Get the sound setting and if it is enabled then play a beep tone else don't. You can easily inject this code in some View's onclicklistener.

Last but not the least

Lastly don't forget to list your preference activity in your applications manifest file :)

            android:name=".MyPreferenceActivity "
            android:label="@string/title_activity_prefs" >

Using shared preferences without PreferenceActivity

Yes you can simply use shared preferences without Preference activity as well. Refer to following syntax.

Setting value in Preference : 

SharedPreferences.Editor editor = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE).edit();

editor.putBoolean("music", true);

editor.putString("someStringSetting", "StringSettingValue");

editor.putInt("someIntSetting", 1);


Retrieving Value from preference :

SharedPreferences prefs = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); 

boolean musicPref = prefs.getBoolean("music", true);

String stringSetting = prefs.getString("someStringSetting", "StringSettingFefaultValue");

String intSetting = prefs.getString("someIntSetting", 1);

NOTE : You can only store primitive type of data like String, integer, boolean etc. You cannot store your custom Objects. To Store your object instances you can convert them in json string (serialize) and then store it. While retrieving convert json string to your object (deserialize) and use it.

Related Links

t> UA-39527780-1 back to top