---
title: "ubuntu下的命令行"
author: "libaohan"
date: "2010-06-04T16:49:57Z"
tags: []
categories: ["操作系统"]
slug: "ubuntu"
draft: false
lastmod: "2010-06-04T16:49:57Z"
id: 684242
url: "https://libaohan.iteye.com/blog/684242"
---

1、如何把命令运行的结果保存到文件当中?
   这个问题太简单了，大家都知道，用 > 把输出转向就可以了
   例子:
   $ ls > ls.txt
   $ cat ls.txt 
   说明: 
   > 是把输出转向到指定的文件，如文件已存在的话也会重新写入，文件原内容不会保留
   >> 是把输出附向到文件的后面，文件原内容会保留下来
2、如何能在输出信息的同时把信息记录到文件中?
   我们可以使用这个命令: tee
   例子:
   $ ls | tee ls_tee.txt
   $ cat ls_tee.txt
   备注：
   使用 tee时,如果想保留目标文件原有的内容怎么办？
     可以使用 -a参数
     -a, --append
     append to the given FILEs, do not overwrite
     附加至给出的文件，而不是覆盖它
3、多个命令的输出都需要记录，可以用script
   script这个命令很强大，可以记录终端的所有输出到相应的文件中
   例子：
        $ script
        Script started, file is typescript
        $ 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
        $ exit
        exit
        Script done, file is typescript
        $ cat typescript
        Script started on 2009年02月08日 星期日 18时56分52秒
        $ 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
        $ exit
        exit

        Script done on 2009年02月08日 星期日 18时57分00秒

      说明:
        我们在启动script时没有指定文件名，它会自动记录到当前目录下一个名为 typescript的文件中
          也可以用 -a参数 指定文件名
          例子:
          &nb