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:
- In the terminal, I enter the commands like this-
$ command1
$ command2
$ command3
$ command4
$ command1
- Now, these commands should get saved in a text file say
commandrec
like this
command1
command2
command3
command4
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)
- The
commandrec
file will be looking something like this
31/05/12 12:00:00
command1
command2
command3
command4
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?
How to successfully experiment with changing these settings in
.bashrc
.bash_history
to another file withcp
.rm ~/.bash_history
after you change.bashrc
with history control parameters, otherwise the combo of old and new entries may give you weird results.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
:History size is set to 10000, duplicates are automatically eliminated so it's plenty of space :)
Timestamping
export HISTTIMEFORMAT="%F %T"
, this will display the time/date before each line when usinghistory
HISTTIMEFORMAT
to your choice/locale based onstrftime
Your aim is to learn new commands? I recommand to use CLI Companion:
You get it by running:
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.
Going on Dirk's suggestion of going through
.bash_history
and chris's suggestion of using thesort
command, what you probably want to do is: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 bycat
-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.
Timestamping is easy. To do it, just add the following line to
~/.profile
or similar:To uniqely record every new command is tricky. First you need to add to
~/.profile
or similar:Then you need to add to
~/.bash_logout
:in your home directory check the
.bash_history
file, it saves every command you enterHow about a simple:
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