[LINUX] Give the history command a date and time and collect the history files of all users with a script

Overview

As a quick way to get the operation log on linux, I added the date and time to the user's ~ / .bash_history.

Setting

If you execute the history command, you can see the history of the commands executed in the past, but since there is no date and time, you do not know when it was executed. Therefore, add the following sentence to ~ / .profile.

HISTTIMEFORMAT='%y/%m/%d %H:%M:%S ';

If you write it in /etc/profile.d/history.sh, you can set it for each individual account, but this time we want to apply it to the whole, so write it in ~ / .profile.

setting file Usage Example
~/.profile -Describe what applies to the entire session at login Describe something that does not depend on the type of shell
~/.bashrc Describe what is only used in bash alias
Shell options Prompt settings ~/.bash_profile

I referred to this article. Linux: Understand the difference between .bashrc and .bash_profile this time

Get script

Since the file name for each user is the same in ~ / .bash_history, prepare a folder for each user and copy it with the following script.


#!/bin/sh
#Let user1 be the working user and delete the previous file in the working folder before copying
sudo find /home/user1/log/ -name ".bash_history" -type f -print | xargs rm -f

#user1,user2,Copy user3 files to user1's log folder
for a in "user1" "usesr2" "user3" ; do

sudo cp -f /home/$a/.bash_history /home/user1/log/$a/;

done

result

You can get a file like this.

Recommended Posts

Give the history command a date and time and collect the history files of all users with a script
[Python] Rename all image files in a specific folder by shooting date and time
I just wanted to extract the data of the desired date and time with Django
Get UNIXTIME at the beginning of today with a command
Let's execute the command on time with the bot of discord
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
Process the contents of the file in order with a shell script
If you give a list with the default argument of the function ...
Upload data to s3 of aws with a command and update it, and delete the used data (on the way)
Replace the directory name and the file name in the directory together with a Linux command.
Use shutil to delete all folders with a small number of files
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
The story of having a hard time introducing OpenCV with M1 MAC
How to get the date and time difference in seconds with python
The story of making a sound camera with Touch Designer and ReSpeaker
Make a DNN-CRF with Chainer and recognize the chord progression of music
Generate all files with a specific extension
Create a command to search for similar compounds from the target database with RDKit and check the processing time
Notice the completion of a time-consuming command
I wanted to know the number of lines in multiple files, so I tried to get it with a command
Zip-compress any file with the [shell] command to create a file and delete the original file.
Specify the start and end positions of files to be included with qiitap
Get the stock price of a Japanese company with Python and make a graph
Python script that makes UTF-8 files with all BOMs under the folder without BOMs
How to get a list of files in the same directory with python
[Python] Get the files in a folder with Python
Check the date of the flag duty with Python
Script to tweet with multiples of 3 and numbers with 3 !!
Get a list of IAM users with Boto3
Compile and run Rust with a single command
A discussion of the strengths and weaknesses of Python
Rename and move files (directories) with mv command
Predicting the goal time of a full marathon with machine learning-③: Visualizing data with Python-
The story of Airflow's webserver and DAG, which takes a long time to load
How to use the grep command to recursively search directories and files to a specified depth
[Shell script] Calculates the elapsed time from the START and END log files and outputs it.
Collect cat images at the speed of a second and aim for the Cat Hills tribe
Collect tweets about "Corona" with python and automatically detect words that became a hot topic due to the influence of "Corona"
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
"Stop committing Japanese files to git on Mac> <" For the time being, I wrote a script to search for incompatible Japanese files on Mac and Linux.
A script that pings the registered server and sends an email with Gmail a certain number of times when it fails