4

I am currently taking a Linux class and working on a project in the command line. I the very last thing I need to do is save all of my work into a log file. The exact phrasing is: "Create a log file of all the commands you have utilized to this point. Title this file Log_File.txt and download it for submission" The downnloading part is done through the IDE but I am having a bitch of a time finding the answer to how to make that file that saves everything I did in the project. Anything would help. -Thanks

Corey Lewis
  • 41
  • 1
  • 2
  • Which shell are you using? Most (if not all) shell keep a history of the commands used, reviewing your shell’s manual will tell you which commands to use to obtain them. – dessert Sep 30 '19 at 16:38
  • i am using bash? I believe. – Corey Lewis Sep 30 '19 at 16:41
  • 1
    The script command will save all screen output (and keyboard input) to a file named typescript. – FedKad Sep 30 '19 at 19:48
  • SInce command history ("$HISTFILE" default value $HOME/.history) is overwritten when you login again, see https://askubuntu.com/questions/80371/bash-history-handling-with-multiple-terminals/80882#80882 my answer. – waltinator Sep 30 '19 at 22:55
  • @waltinator In bash the default value of $HISTFILE is ~/.bash_history and its lines are dropped in order to stay inside the $HISTFILESIZE limit of by default 2000 lines – why would it be overwritten on login? – dessert Oct 02 '19 at 06:01

2 Answers2

5

I suppose you just need to run:

history > filename.txt
joahim
  • 61
  • 3
0

To manage the session history of a bash shell there’s the history command. Let’s have a look at the relevant parts of help history:

$ help history
history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
Display the history list with line numbers, prefixing each modified
entry with a `*'.  An argument of N lists only the last N entries.

Options:
  -a        append history lines from this session to the history file
  -w        write the current history to the history file

If FILENAME is given, it is used as the history file.  Otherwise,
if HISTFILE has a value, that is used, else ~/.bash_history.

When a bash shell is started it reads the last (by default 1000) lines from the user’s history file (by default ~/.bash_history) and builts a session history in the RAM. When you now execute a command line it saves this line to the session history and drops the first line instead – the session history once reached the limit of the 1000 lines doesn’t exceed this limit.

Following this, in order to save only the session history i.e. the command lines you executed in this particular terminal window in a file ~/session_history, it’s:

history -a ~/session_history

If you however want to save the 1000 lines of history the session currently holds in memory, i.e. commands from older sessions and the current one, it’s:

history -w ~/session+old_history

If you want to save the whole history of all sessions closed so far, limited to 2000 lines by default, you just need to copy your default history file:

cp ~/.bash_history ~/closed-sessions_history

If you want to manually save a session’s history to this file without closing the session, do:

history -a

If you did that in every open terminal, your history file is up to date with all the command lines you used to this point, copying it now gives you a full history of closed and open sessions:

cp ~/.bash_history ~/all-sessions_history
dessert
  • 39,982