4

I am currently taking a Linux class and working on a project in the command line. The very last thing I need to do is to 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 downloading part is done through the IDE, but I am having a hard time finding the answer to how to make that file that saves everything I did in the project.

CPPUIX
  • 137

2 Answers2

6

I suppose you just need to run:

history > filename.txt
joahim
  • 71
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
  • 40,956