把linux终端运行结果保存到文件里面.docVIP

  • 21
  • 0
  • 约3.13千字
  • 约 3页
  • 2017-06-13 发布于北京
  • 举报
把linux终端运行结果保存到文件里面 一,如何把命令运行的结果保存到文件当中?   这个问题太简单了,大家都知道,用 把输出转向就可以了   例子:   [lhd@hongdi ~]$ ls ls.txt   [lhd@hongdi ~]$ cat ls.txt   1.gtkrc-2.0   2009   a   amsn_received   a.tar.gz   说明: 是把输出转向到指定的文件,如文件已存在的话也会重新写入,文件原内容不会保留    是把输出附向到文件的后面,文件原内容会保留下来 二,如何能在输出信息的同时把信息记录到文件中?   我们在上面的例子中可以看到,我们使用输出转向,命令在终端上的输出转向到了文件中,但如果我希望能同时在终端上看到输出信息怎么办?   我们可以使用这个命令: tee   解释一下tee的作用:   read from standard input and write to standard output and files   它从标准输入读取内容并将其写到标准输出和文件中   看例子:   [lhd@hongdi ~]$ ls | tee ls_tee.txt   1.gtkrc-2.0   2009   a   amsn_received   a.tar.gz   [lhd@hongdi ~]$ cat ls_tee.txt   1.gtkrc-2.0   2009   a   amsn_received   a.tar.gz 备注:使用 tee时,如果想保留目标文件原有的内容怎么办?   可以使用 -a参数   -a, --append   append to the given FILEs, do not overwrite   附加至给出的文件,而不是覆盖它 三,多个命令的输出都需要记录,可以用script   script这个命令很强大,可以记录终端的所有输出到相应的文件中   看例子:   [lhd@hongdi ~]$ script   Script. started, file is typescript   [lhd@hongdi ~]$ ls   1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm   2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm   [lhd@hongdi ~]$ exit   exit   Script. done, file is typescript   [lhd@hongdi ~]$ cat typescript   Script. started on 2009年02月08日 星期日 18时56分52秒   [lhd@hongdi ~]$ ls   1.gtkrc-2.0 c.tar kmess-2.0alpha2.tar.gz secpanel-0.5.3-1.noarch.rpm   2009 DownZipAction.php kmesslog secpanel-0.5.4-2.noarch.rpm   [lhd@hongdi ~]$ exit   exit   Script. done on 2009年02月08日 星期日 18时57分00秒   说明:   1,我们在启动script时没有指定文件名,它会自动记录到当前目录下一个名为 typescript的文件中。也可以用 -a参数 指定文件名   例子:   [lhd@hongdi ~]$ script. -a example.txt   Script. started, file is example.txt   此时终端的输出内容被记录到 example.txt这个文件中   2,退出script时,用exit   感到奇怪吗?事实上script就是启动了一个shell   看一下ps auxfww 的信息就知道了   lhd 17738 0.1 3.2 152028 33328 ? Sl 18:30 0:03 /usr/bin/konsole   lhd 17740 0.0 0.1 6372 1720 pts/1 Ss 18:30 0:00 \_ /bin/bash   lhd 17900 0.0 0.0 5344 628 pts/1 S 19:01 0:00 | \_ script   lhd 17901 0.0 0.0 5348 464 pts/1 S 19:01 0:00 | \_ script   lhd 17902 0.5 0.1 6372 1688 pts/2 Ss

文档评论(0)

1亿VIP精品文档

相关文档