有什么方法可以使用命令将所有终端输出保存到文件中吗?
command > file.txthistory > file.txt,我需要完整的终端文本就像是 terminal_text > file.txt
hee*_*ayl 110
您可以使用script. 它基本上会保存该script会话中终端上打印的所有内容。
来自man script:
script makes a typescript of everything printed on your terminal.
It is useful for students who need a hardcopy record of an
interactive session as proof of an assignment, as the typescript file
can be printed out later with lpr(1).
Run Code Online (Sandbox Code Playgroud)
您script只需script在终端中键入即可启动会话,所有后续命令及其输出都将保存在typescript当前目录中命名的文件中。您也可以通过以下方式将结果保存到不同的文件script:
script output.txt
Run Code Online (Sandbox Code Playgroud)
要注销script会话(停止保存内容),只需键入exit.
下面是一个例子:
script makes a typescript of everything printed on your terminal.
It is useful for students who need a hardcopy record of an
interactive session as proof of an assignment, as the typescript file
can be printed out later with lpr(1).
Run Code Online (Sandbox Code Playgroud)
现在,如果我阅读文件:
script output.txt
Run Code Online (Sandbox Code Playgroud)
script也有许多选项,例如安静运行-q( --quiet) 而不显示/保存程序消息,它还可以运行特定命令-c( --command) 而不是会话,它还具有许多其他选项。检查man script以获得更多想法。
小智 20
我也遇到了同样的问题,经过一番搜索,想出了这个解决方案:
添加到您的.bash_aliases这个:
# 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'
}
Run Code Online (Sandbox Code Playgroud)
并在您的.bashrc文件末尾添加以下内容:
smart_script
Run Code Online (Sandbox Code Playgroud)
完成此操作后,“脚本”命令将在每个终端会话中执行一次,将所有内容记录到~/Terminal_typescripts/raw.
如果需要,您可以在事后 (在会话结束时)通过键入savelog或保存当前会话日志savelog logname- 这会将当前原始日志复制到~/Terminal_typescripts/manual该文件夹中,并在此文件夹中创建可读的 .txt 日志。(如果您忘记这样做,原始日志文件仍将在其文件夹中;您只需要找到它们即可。)此外,您也可以通过键入startnewlog.
会有很多垃圾日志文件,但你可以不时清理旧的,所以问题不大。
(基于https://answers.launchpad.net/ubuntu/+source/gnome-terminal/+question/7131,https://askubuntu.com/a/493326/473790)