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
    It's not a useful as you might think - see my answer in the comments of https://askubuntu.com/questions/80371/bash-history-handling-with-multiple-terminals/80882#80882 – waltinator Jun 03 '18 at 16:00

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
  • 10,542
  • 3
    and if you don't want exact repeats, you could "cat ~/.bash_history | sort | uniq > Sorted_history.txt" – jpezz Jun 03 '18 at 19:46
  • 4
    @jpezz: or more simply < ~/.bash_history sort -u > Sorted_history.txt. And you can use history -a first to "append history lines from this session to the history file". – Peter Cordes Jun 04 '18 at 05:05
  • history | cut -c 30- | sort -u -i | ack "ssh" will (for me) cut off the variable parts (time codes and indexing) and then sort. I do get some duplicates, I think because of different whitespace. Pipe it through "ack" as shown to get simple searching. – pbhj Jun 05 '18 at 15:07