0

I am taking some online courses. I want to seperate all commands. That is, I want to save all commands that I execute during classes on a different memory file than my other system commands. This will make my work easy when I work on the system and when I work on classes.

Is it possible to save terminal commands on different files and then load previous commands from a specific file? If yes, how can this be done?

If you are not able to understand the problem properly, here`s an example.

Type 1:- During classes, I use commands for compiling and executing files, like "gcc abcd.c -o abcd" and "abcd"

Type 2:- But, while working on system, I use commands like "sudo apt install ..." or "sudo apt purge ..." Now, when I want to use previous system commands after a class, I will be having lots of commands of type 1 and it will be annoying. So, I want to save type 1 and type 2 commands differently on different files and load a specific file whenever I require.

Details about the system:- Shell - /bin/bash

NAME="Ubuntu"
VERSION="16.04.7 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.7 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

UPDATE
According to @Marc Vanhoomissen, I was able to create another history file and named it .classes (Note:- .class is used for java files. so I can`t use it). I open terminal, see that it is using bash_history as HISTFILE. Then I change to .classes, but the commands did not change. The command in the image is NOT specified in .classes file. On top of that, it does not show the commands of .classes file at all. The last command is not in the .classes file

.classes file
None of these commands can be seen in the terminal even after loading .classes history file

4
  • You've provided no OS, release details or shell details. I type loads of commands for support purposes on my boxes so I don't have any of them reach my command history so it's not cluttered with packages queries for others on sites like this. I also have the date & time of the commands stored in the history, which I find useful (and maybe useful to you too..) but commands and how varies particularly on shell, but also OS & release (and if local, remote-user etc)
    – guiverc
    Mar 24 at 7:54
  • One approach would be to use different user-accounts for the different tasks.
    – mook765
    Mar 24 at 8:39
  • @Master_Nachi you need to clear current history (this does not clear to file just the memory) history -c then re-read the file history -r
    – bac0n
    Mar 24 at 14:29
  • Thanks @bac0n, it worked! but I need to clear and read history every time I open terminal. No problem, i`ll manage. Thanks again. Mar 24 at 16:54
3

The list of commands you entered is stored in a specific file, specified normally in the .bashrc file of your user home directory. This file is customizable and you can change the name of the file by setting:

HISTFILE=.class

or

HISTFILE=.pers

Instead, you can just specify

$ HISTFILE=.class

or

$ HISTFILE=.pers

at the command prompt. This will create separate files.

0

In bash, the versatile history has a single file. That file can changed on a per-session basis, but not per-command.

You did not specify how you would split your commands into "types" as you call them. Lacking that info, you can probably use grep:

history | grep gcc > history_gcc.txt

will create file history_gcc.txt containing all commands with gcc.

Related:

  1. Can I create a separate bash history file for each terminal profile?
  2. https://unix.stackexchange.com/questions/56446/shell-deal-with-multiple-command-history-files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.