5

I want to have a complete and sorted list of my bash history. But there are some problems:

  1. By doing history the list never shows the complete list and only 1000 commands are shown.
  2. In my $HOME directory there is a file named .bash_history that shows 2000 results.
  3. When I want to make a sorted .txt file out of my history I do:

    history | sort > History.txt
    

    But because of numbers the commands are not sorted alphabetically and are sorted by number.

Is there any way to do what I want?

2 Answers2

8

sort can sort input by the field specified by the user to -k, so strictly speaking something like this would be what you are looking for.

history | sort -k2 > History.txt  # or -K3 if you have $HISTTIMEFORMAT, etc set

Additionally, there's often a difference between what's available in the output of history command vs what's recorded in ~/.bash_history in that the latter is not updated after every command (is usually updated only on logout), so using history | sort .. is better at giving you a current view.

shalomb
  • 251
7

How about:

cat ~/.bash_history | sort > Sorted_history.txt

to have an unlimited bash history, have a look at this QA:

https://stackoverflow.com/questions/9457233/unlimited-bash-history

Bruni
  • 11,099