输出重定向

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

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. python3(二十一) pip

    先确保安装了windows的Python的pip 出现上图说明安装了,命令未找到则没有安装 安装一个图形处理的第三方库 Anaconda安装第三方库 我们经常需要用到很多第三方库,如MySQL驱动程序 ...

  2. tf.nn.dropout 激活函数

    tf.nn.dropout(x,keep_prob,noise_shape=None,seed=None,name=None) 参数: x:一个浮点型Tensor. keep_prob:一个标量Ten ...

  3. 第一天 简单的python认证登陆代码

    #!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyu welcome = '''-----------welcome to home----- ...

  4. 今天探究的CSS属性是box-sizing;

    首先BOX-SIZING属性是CSS3的属性: 语法: box-sizing : content-box || border-box || inherit 取值说明 1.content-box:此值为 ...

  5. jmeter5.1.1 生成html报告

    1.首先需要准备好 .jmx 脚本 2.修改jmeter.properties文件(把注解去掉,报告中才能展示所需信息) jmeter.save.saveservice.output_format=x ...

  6. SpringBoot与单元测试JUnit的结合

    有些人认为,写单元测试就是在浪费时间 ,写完代码,依然还是能够进行测试的.但是,还是建议写单元测试的,可以让你的条理更加清晰,而且当某个功能出现问题时,可能通过单元测试很容易的定位和解决问题.本文主要 ...

  7. 牛顿迭代法的理解与应用( x 的平方根)

    题目来源与LeetCode算法题中的第69题,具体内容如下(点击查看原题): 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只 ...

  8. 支付宝开源非侵入式 Android 自动化测试工具 Soloπ

    Soloπ(SoloPi)是支付宝开源的一个无线化.非侵入式的Android自动化测试工具,公测版拥有录制回放.性能测试.一机多控三项主要功能,能为测试开发人员节省宝贵时间. 本文是SoloPi团队关 ...

  9. 基于TextRank算法的文本摘要

    本文介绍TextRank算法及其在多篇单领域文本数据中抽取句子组成摘要中的应用. TextRank 算法是一种用于文本的基于图的排序算法,通过把文本分割成若干组成单元(句子),构建节点连接图,用句子之 ...

  10. 总结vscode调试vue,nodejs的各种方法

    之前写项目一直都是console.log()来调试的,浪费了很多时间,现在整理一下用vscode对nuxt(vue)前后端进行调试的方法 前端的调试 chrome+launch 使用chrome调试, ...