输出重定向

命令输出重定向的语法为:

command > file 或 command >> file

这样,输出到显示器的内容就可以被重定向到文件。果不希望文件内容被覆盖,可以使用 >> 追加到文件末尾

[root@hy ~]# who
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# who > output.txt
[root@hy ~]# cat output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)

输入重定向(<可省略)

command < file 
[root@hy ~]# wc -l output.txt
2 output.txt
[root@hy ~]# wc -l < output.txt

  

[root@hy ~]# cat < output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# cat output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)

输入输出重定向深入

一般情况下,每个 Unix/Linux 命令运行时都会打开三个文件:
  • 标准输入文件(stdin):stdin的文件描述符为0,Unix程序默认从stdin读取数据。
  • 标准输出文件(stdout):stdout 的文件描述符为1,Unix程序默认向stdout输出数据。
  • 标准错误文件(stderr):stderr的文件描述符为2,Unix程序会向stderr流中写入错误信息

一般情况下,只默认将stdout重定向到文件

[root@hy ~]# ls /root/ > output.txt
[root@hy ~]# cat output.txt
anaconda-ks.cfg
install.log
install.log.syslog
output.txt
use

stderr 重定向到 file

command 2 > file 或 command 2 >> file
[root@hy ~]# l /root/ > output.txt
-bash: l: command not found
[root@hy ~]# cat output.txt
[root@hy ~]# l /root/ 2> output.txt
[root@hy ~]# cat output.txt
-bash: l: command not found

stdout 和 stderr 合并后重定向到 file

command > file 2>&1 或 command >> file 2>&1

对 stdin 和 stdout 都重定向

[root@hy ~]# who > output.txt
[root@hy ~]# cat output.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# cat output.txt > hy.txt
[root@hy ~]# cat < output.txt > hy1.txt
[root@hy ~]# cat hy.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
[root@hy ~]# cat hy1.txt
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)

Here Document

1.它的作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。

2.delimiter字符可自定义

语法:

command << delimiter
document
delimiter

实例

[root@hy ~]# wc -l << EOF
> root tty1 2015-09-03 16:21
> root pts/3 2015-09-03 19:09 (192.168.11.1)
> EOF
2

  

wc -l << EOF
root tty1 2015-09-03 16:21
root pts/3 2015-09-03 19:09 (192.168.11.1)
EOF

/dev/null文件

如果希望执行某个命令,但又不希望在屏幕上显示输出结果,那么可以将输出重定向到 /dev/null:

command > /dev/null

如果希望屏蔽 stdout 和 stderr,可以这样写:

command > /dev/null 2>&1

shell之路 Linux核心命令【第一篇】管道符与重定向的更多相关文章

  1. Linux核心命令

    Linux核心命令 strace(查看系统调用的一个过程) 例:strace cat /test.txt netstat perf top pidstat mpstat dstat vmstat sl ...

  2. 《Linux大棚命令百篇下》网络篇的总结

    本文是<Linux大棚命令百篇下>网络篇的总结 ping -c 指定数量,在windows下会自动停止,linux下会一直ping下去 -q 简短报告 -s 指定每次ping的数据包大小, ...

  3. Linux常用命令-解压缩篇

    前言 Linux常用命令中,有很多用于对文件的压缩或解压,本文将介绍这些解压缩命令中不常见却非常实用的用法. tar tar是linux中最常用的解压缩命令.tar命令可用于处理后缀名为tar,tar ...

  4. LINUX常用命令 --- 权限篇

    linux常用命令 linux用户权限相关 root 用户    相当于群主    超级用户 sudo命令   相当于群管理员 普通用户    群成员 查看用户id信息      使用linux    ...

  5. 『学了就忘』Linux基础命令 — 33、管道符

    目录 1.管道符介绍 2.管道符应用 (1)例子1: (2)例子2: (3)例子3: 1.管道符介绍 管道符|,也是Shell命令. 管道符的作用是链接多个命令,把命令1的结果作为命令2的操作对象. ...

  6. Linux apt命令使用 以及 文本流和重定向

    apt (Advanced Packaging Tool) 是一个在Debian和Ubuntu中的Shell前端软件包管理器. apt命令执行需要超级管理员权限(root). apt语法 apt [o ...

  7. Linux命令第一篇

    作业一: 1)   新建用户natasha,uid为1000,gid为555,备注信息为“master” natasha:x:1004:555:master:/home/natasha:/bin/ba ...

  8. Linux基础命令第一天

    一.命令行bash基本操作 1,shell 用户不能直接操作内核,所以用户操作通过shell传递给内核,Linux下叫shell,就相当于Windows下的cmd shell分为两种: GUI:图形界 ...

  9. [linux time命令学习篇] time 统计命令执行的时间

    注意: 命令后面一定要有分号; http://codingstandards.iteye.com/blog/798788 用途说明 time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统 ...

随机推荐

  1. Alibaba Cloud Linux 2 LTS 正式发布,提供更高性能和更多保障!

    在Alibaba Cloud Linux 2(原Aliyun Linux 2)上线一年之际阿里云对外正式发布Alibaba Cloud Linux 2 LTS版本.LTS版本的发布对于Alibaba ...

  2. C语言实现链式队列

    链式队列,简称"链队列",即使用链表实现的队列存储结构. 链式队列的实现思想同顺序队列类似,只需创建两个指针(命名为 top 和 rear)分别指向链表中队列的队头元素和队尾元素, ...

  3. Python操作rabbitmq系列(六):进行RPC调用

    此刻,我们已经进入第6章,是官方的最后一个环节,但是,并非本系列的最后一个环节.因为在实战中还有一些经验教训,并没体现出来.由于马上要给同事没培训celery了.我也来不及写太多.等后面,我们再慢慢补 ...

  4. 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"

    文档根元素 "beans" 必须匹配 DOCTYPE 根 "null" (2011-11-20 21:26:41) 转载▼ 标签: 杂谈 分类: spring- ...

  5. css定位有哪几种方式

    一.position 属性规定元素的定位类型,它一般有以下四个值: 默认static 相对定位relative 绝对定位absolute 固定定位fixed 元素可以使用的顶部,底部,左侧和右侧属性定 ...

  6. Wireshark的两种过滤器与BPF过滤规则

    Wirshark使用的关键就在于过滤出想要的数据包,下面介绍怎么过滤. 抓包过滤器 Wirshark有两种过滤器,一个是抓包过滤器,一个是显示过滤器,他们之间的区别在于抓包过滤器只抓取你设置的规则,同 ...

  7. 5. class--extends

    ES5: function article(x,y){ this.x = x; this.y = y; } article.prototype.say = function() { return (t ...

  8. DEV gridview 合并单元格

    private void gv_docargo_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e ...

  9. .NET Core 初识

    什么是 ASP.NET Core? ASP.NET Core 是一个新的开源和跨平台的框架,用于构建如 Web 应用.物联网(IoT)应用和移动后端应用等连接到互联网的基于云的现代应用程序.ASP.N ...

  10. C#多线程(12):线程池

    目录 线程池 ThreadPool 常用属性和方法 线程池说明和示例 线程池线程数 线程池线程数说明 不支持的线程池异步委托 任务取消功能 计时器 线程池 线程池全称为托管线程池,线程池受 .NET ...