Search This Blog

Sunday, February 21, 2021

TIL : tự động lưu lại toàn bộ các câu lệnh & kết quả vào file txt

#linux #ubuntu #bashrc 

Save all the terminal output to a file - Unix & Linux Stack Exchange

I too faced the same problem and after some search came up with this solution:

Add to your .bash_aliases this:

# Execute "script" command just once  smart_script(){      # if there's no SCRIPT_LOG_FILE exported yet      if [ -z "$SCRIPT_LOG_FILE" ]; then          # make folder paths          logdirparent=~/Terminal_typescripts          logdirraw=raw/$(date +%F)          logdir=$logdirparent/$logdirraw          logfile=$logdir/$(date +%F_%T).$$.rawlog            # if no folder exist - make one          if [ ! -d $logdir ]; then              mkdir -p $logdir          fi            export SCRIPT_LOG_FILE=$logfile          export SCRIPT_LOG_PARENT_FOLDER=$logdirparent            # quiet output if no args are passed          if [ ! -z "$1" ]; then              script -f $logfile          else              script -f -q $logfile          fi            exit      fi  }    # Start logging into new file  alias startnewlog='unset SCRIPT_LOG_FILE && smart_script -v'    # Manually saves current log file: $ savelog logname  savelog(){      # make folder path      manualdir=$SCRIPT_LOG_PARENT_FOLDER/manual      # if no folder exists - make one      if [ ! -d $manualdir ]; then          mkdir -p $manualdir      fi      # make log name      logname=${SCRIPT_LOG_FILE##*/}      logname=${logname%.*}      # add user logname if passed as argument      if [ ! -z $1 ]; then          logname=$logname'_'$1      fi      # make filepaths      txtfile=$manualdir/$logname'.txt'      rawfile=$manualdir/$logname'.rawlog'      # make .rawlog readable and save it to .txt file      cat $SCRIPT_LOG_FILE | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > $txtfile      # copy corresponding .rawfile      cp $SCRIPT_LOG_FILE $rawfile      printf 'Saved logs:\n    '$txtfile'\n    '$rawfile'\n'  }  

And to the end of your .bashrc file add this:

smart_script  

After you've done this, "script" command will be executed once in every terminal session, logging everything to ~/Terminal_typescripts/raw.

If you want, you can save current session log after the fact (in the end of the session) by typing savelog or savelog logname – this will copy current raw log to ~/Terminal_typescripts/manual and also create readable .txt log in this folder.  (If you forget to do so, raw log files will still be in their folder; you'll just have to find them.)  Also you may start recording to a new log file by typing startnewlog.

There will be a lot of junk log files, but you can clean old ones from time to time, so it's not a big problem.

(Based on https://answers.launchpad.net/ubuntu/+source/gnome-terminal/+question/7131 , https://askubuntu.com/a/493326/473790 )



No comments:

Post a Comment

CON NGƯỜI và TINH THẦN

CON NGƯỜI và TINH THẦN 1 –  " Nếu có một gã trọc phú hàng to súng lớn, có thể mua biệt thự alibaba cho em ở, xe lếch xù cho em đi, nạp ...