Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been on Linux for more than 6 months now but never went too much into the CLI (command-line interface or terminal or shell)

Now as I ask questions here, get answers, or help from other sites, I learn new commands...

How can I can store every new command in a text file? Only new/*unique* commands, not repetitions of the same command.

Here's an example:

  1. In the terminal, I enter the commands like this-

ubuntu@ubuntu:~$ command1

ubuntu@ubuntu:~$ command2

ubuntu@ubuntu:~$ command3

ubuntu@ubuntu:~$ command4

ubuntu@ubuntu:~$ command1

  1. Now, these commands should get saved in a text file say commandrec like this-





NOTE:The last command in the terminal which was again command1 is not recorded/saved again in the text file.

And the next time I open the terminal, and enter a new command command 5, it should get appended to the list in commandrec

(but if the command was used earlier on some other date, it should still be ignored. For example, command 1 entered again along with command 5 on a new day/time but command1 not recorded as already used)

  1. The commandrec file looking something like this-

31/05/12 12:00:00





01/06/12 13:00:00 command 5

(the time and date thing would be great if possible, but okay even if that isn't there)

This way, I can have a record of all commands used by me to date.

How can this be done?

share|improve this question
what do you want to do with the list that gets generated? if you just want to view it, you could do the sort thing @Chris suggested whenever you need to read the file and output it to a permanent file –  Dirk Hartzer Waldeck Jun 1 '12 at 9:19
@DirkHartzerWaldeck...yes i want to view it as and when needed..the chris thing will definitely help..its gr8...if nothing else comes up,thats the best way...but would like to have it like what i have mentioned in the question(time,date,etc) and plus..if i copy this file from .bash_history to another one and then sort it,i'll have to manually copy it everytime!(i want to copy it and not edit the .bash_history because i want to add comments to it) –  Nirmik Jun 1 '12 at 9:22
you could create a bash script file that automates this, so instead of opening a text file, you'll run a script that adds and sorts a text file and then opens it. this way you could also add extra info as needed. –  Dirk Hartzer Waldeck Jun 1 '12 at 9:53
@DirkHartzerWaldeck...well i do not know bash scripting! if you could help.. –  Nirmik Jun 1 '12 at 9:55
@virpara...as per the above comments...a bash script needed! i know you can! ;) –  Nirmik Jun 1 '12 at 9:56
show 2 more comments

5 Answers

Your aim is to learn new commands? I recommand to use CLI Companion:

CLI Companion is a tool to store and run Terminal commands from a GUI. People unfamiliar with the Terminal will find CLI Companion a useful way to become acquainted with the Terminal and unlock its potential. Experienced users can use CLI Companion to store their extensive list of commands in a searchable list.

enter image description here

You get it by running:

sudo add-apt-repository ppa:clicompanion-devs/clicompanion-nightlies
sudo apt-get update
sudo apt-get install clicompanion

If you don't want to add the PPA try this file for 12.04 (latest version 1.1-6, released on 2012-04-14 - check this page for newer versions.) - or remove the PPA after installing with sudo add-apt-repository -r ppa:clicompanion-devs/clicompanion-nightlies .

PS: I found this question which could be helpful, too.

share|improve this answer
Despite not directly addressing the verbage of the question, I think it addresses the goal of the question. –  hbdgaf Jun 1 '12 at 8:58
Well thanx for that..it definitely is useful :) bit not exactly what i want..it will help though :) –  Nirmik Jun 1 '12 at 9:17
add comment

How to successfully experiment with changing these settings in .bashrc

  • Before experimenting: Save your current .bash_history to another file with cp.
  • While experimenting with what works: rm ~/.bash_history after you change .bashrc with history control parameters, otherwise the combo of old and new entries may give you weird results.
    • e.g., all entries before timestamps were enabled will show the same current date/time!
  • Once you find something that works, DO NOT rm any more!!

  • Note also that any changes to .bashrc are reflected only when you exit and then instantiate a new terminal/shell.

Increasing history size and eliminating duplicates

  • Add these lines to your ~/.bashrc:

    export HISTCONTROL=erasedups
    export HISTSIZE=10000
  • History size is set to 10000, duplicates are automatically eliminated so it's plenty of space :)


  • Time can be set easily by adding another line of the form export HISTTIMEFORMAT="%F %T", this will display the time/date before each line when using history
    • Example:
      izx@preciseunity:~$ history
      1  2012-06-01 07:16:22 rm ~/.bash_history
      2  2012-06-01 07:16:23 exit
      3  2012-06-01 07:16:27 ls
      4  2012-06-01 07:16:31 test
      5  2012-06-01 07:16:34 ls /etc
      6  2012-06-01 07:16:36 down
      7  2012-06-01 07:16:37 up
      8  2012-06-01 07:16:40 hustory
      9  2012-06-01 07:16:48 history
    • Note that because duplicates are eliminated, the time shown for a command is the latest (newest) time it was last executed.
    • You can customize the HISTTIMEFORMAT to your choice/locale based on strftime
share|improve this answer
could u give me a more stepwise approach?i dint get rm with this n stuff.. :( –  Nirmik Jun 1 '12 at 14:35
See edits; all it says is, when you are playing with settings, safer to delete .bash_history each time you change something (since the old entries won't change, and will have weird effects; e.g. adding time will show today's date/time for all old entries before the change). Once you have setting you like, no need to do this :) –  izx Jun 1 '12 at 14:42
It's worth pointing out that when you say eliminate "duplicates" here, this means eliminating the same command lines in the history. But if you used the same command with different files or parameters, they will not count as duplicates. –  Chan-Ho Suh Jun 1 '12 at 16:51
@Chan-HoSuh: Yes, good point. black-magic-voodoo /etc is not the same as black-magic-voodoo ~/. Eliminating those kinds of duplicates would probably require a perl script or multiple passes through the usual text processing commands. –  izx Jun 1 '12 at 22:24
The only issue I see with this is multiple terminals clobbering each other's history –  hbdgaf Jun 3 '12 at 8:10
show 1 more comment

Going on Dirk's suggestion of going through .bash_history and chris's suggestion of using the sort command, what you probably want to do is:

cat ~/.bash_history | sed 's/sudo //g' | sort -u +0 -0 | cut -d\  -f1 > commandrec.txt

Second command in the pipeline will delete occurrences of 'sudo ' so when you sort you will sort the commands that you may type only after invoking sudo.

Bash history doesn't keep track of the time/date the command was used, but just off the top of my head, you could write a script that will be invoked when you exit the terminal. The script would call the above command (but save to commandrec-$DATE.txt where you have something like DATE = `date "+%Y-%m-%dT%H:%M:%S"`). Then you could diff with .bash-history and keep only the new commands in commandrec-$DATE.txt. So you would have a collection of such dated commandrec files. Actually, since .bash_history is limited (I think default on my system was 500), you should probably diff with the file you get by cat-ing all your commandrec*.txt files and .bash_history.

Then anytime you actually want to look at all your commands to date, you can cat all your files to a file.

share|improve this answer
add comment

in your home directory check the .bash_history file, it saves every command you enter

share|improve this answer
or just type history to get a list, | grep something to find something. –  izx Jun 1 '12 at 7:20
Well that is usefull...but that is history..commands are stored multiple times as and when entered..i want to have them just once..plus I want it to be a different file so that I can add comments to it..editing .bash_history would cause troubles i guess –  Nirmik Jun 1 '12 at 7:21
also if time and date could be added to such an entry.. –  Nirmik Jun 1 '12 at 7:22
@izx..Check the edited question for what i exactly mean.. –  Nirmik Jun 1 '12 at 7:32
This is not exactly what you are looking for, but maybe a little closer. You could try sort's unique option e.g. sort -u ~/.bash_history. This will sort the .bash_history file but eliminate duplicates. –  Chris Jun 1 '12 at 7:50
show 2 more comments

How about a simple:

  1. Grab the information from .bash_history (All commands used in the terminal)
  2. Sort them out Alphabetically
  3. Show only Uniques
  4. Send them to a file

cat .bash_history|sort|uniq > commands.txt

If you only want the command (Without taking parameters into consideration) a simple cut will do:

cat .bash_history|cut -d ' ' -f1|sort|uniq > commands.txt

If you want to know how many times a command was executed you could:

cat .bash_history|cut -d ' ' -f1|sort|uniq -c > commands.txt

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.