6. Linux输入输出重定向
1.输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中。
输入重定向中用到的符号及其作用

输出重定向中用到的符号及其作用

1)通过输出重定向将原本要输出到屏幕的信息写入到文件中。
[root@Centos test]# man bash > readme.txt
[root@Centos test]# ll
total 304
-rw-r--r--. 1 root root 122 Aug 4 16:58 a.txt
-rw-r--r--. 1 root root 303223 Aug 5 15:45 readme.txt
[root@Centos test]# head -n 5 readme.txt
BASH(1) General Commands Manual BASH(1) NAME
2)重定向的覆盖写入和追加写入
[root@Centos test]# echo "Hello I'm Xinghen1216" > readme.txt
[root@Centos test]# cat readme.txt
Hello I'm Xinghen1216 [root@Centos test]# echo "I'm glad to meet you" >> readme.txt
[root@Centos test]# cat readme.txt
Hello I'm Xinghen1216
I'm glad to meet you
3)把命令的报错信息写入到文件(常用于执行自动化的shell脚本中)。
[root@Centos test]# ll abc.txt
ls: cannot access abc.txt: No such file or directory
[root@Centos test]#
[root@Centos test]# ll abc.txt 2>error.txt
[root@Centos test]# ll
total 12
-rw-r--r--. 1 root root 122 Aug 4 16:58 a.txt
-rw-r--r--. 1 root root 53 Aug 5 16:00 error.txt
-rw-r--r--. 1 root root 43 Aug 5 15:50 readme.txt
[root@Centos test]# cat error.txt
ls: cannot access abc.txt: No such file or directory
2.管道命令符
把前一个命令原本要输出到屏幕的数据当作是后一个命令的标准输入。
举例:1)通过匹配关键词/sbin/nologin 找出了所有被限制登录系统的用户
[root@Centos test]# grep "/sbin/nologin" /etc/passwd | wc -l
35
2)用翻页的形式查看/etc目录中的文件列表及属性信息
[root@Centos test]# ll /etc/ | more
total 1348
drwxr-xr-x. 3 root root 101 Jul 30 01:27 abrt
-rw-r--r--. 1 root root 16 Jul 30 01:36 adjtime
-rw-r--r--. 1 root root 1518 Jun 7 2013 aliases
-rw-r--r--. 1 root root 12288 Jul 30 01:38 aliases.db
drwxr-xr-x. 2 root root 51 Jul 30 01:28 alsa
3)解决自动化脚本中遇到的修改密码需要输入两次密码确认的问题
举例:不需确认,将root密码直接修改成"centos"
[root@Centos test]# echo "centos" | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
4)计算1+2+...+100
[root@Centos test]# echo {1..100}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
[root@Centos test]#
[root@Centos test]# echo {1..100} | tr ' ' '+'
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100
[root@Centos test]#
[root@Centos test]#
[root@Centos test]# echo {1..100} | tr ' ' '+' | bc
5050
6. Linux输入输出重定向的更多相关文章
- 全面理解Linux输入输出重定向
全面理解Linux输入输出重定向 本教程通过视频方式讲解shell操作,理解感念,教程通俗易懂,比起看一大堆文档要舒服的多.本次教程主要讲解 Linux Shell 中支持输入输出重定向,用符号&l ...
- Linux输入输出重定向和文件查找值grep命令
Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...
- [转帖]Linux教程(12)- linux输入输出重定向
Linux教程(12)- linux输入输出重定向 2018-08-21 22:57:02 钱婷婷 阅读数 49更多 分类专栏: Linux教程与操作 Linux教程与使用 版权声明:本文为博主原 ...
- Linux 输入输出重定向, &>file, 2>&1, 1>&2
Linux 输入输出重定向, &>file, 2>&1, 1>&2 一.1和2在Linux中代表什么 1.1 输出重定向 1.2 输入重定向 1.3 绑定重定 ...
- linux 输入输出重定向
输入输出重定向 1,输入输出重定向,是针对过滤器的,不针对,编辑器和交互工具 2,>号只把正确的标准输出重定向,输出错误信息,可以用2> 3,新建或清空文件可以直接用>filenam ...
- linux输入输出重定向
http://www.cnblogs.com/chengmo/archive/2010/10/20/1855805.html 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据 ...
- [100]linux输入输出重定向
一目了然版本: &号含义: 参考 参考:非常经典,值得一看,我是在linux爱好者公众号里发现的. 下面是我自己的一些总结. linux的命令数据流 在Linux下,当一个用户进程被创建的时候 ...
- linux - 输入输出重定向 及 管道
> 正确结果重定向 2> 错误结果重定向 &> 正确和错误全部重定向 >> 追加,其它同> 标准输出实际上就是显示器,比如我们使用cat命令打开一个文件,文 ...
- Linux输入输出重定向练习
1.date >> 123 date > 123 2.abc 2>123 abc 2>>123 abc 2>/dev/null 标准输出重定向到回收站 3. ...
随机推荐
- 软件工程UML第一次作业
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...
- SparkSql自定义数据源之读取的实现
一.sparksql读取数据源的过程 1.spark目前支持读取jdbc,hive,text,orc等类型的数据,如果要想支持hbase或者其他数据源,就必须自定义 2.读取过程 (1)sparksq ...
- k8s之RBAC授权模式
导读 上一篇说了k8s的授权管理,这一篇就来详细看一下RBAC授权模式的使用 RBAC授权模式 基于角色的访问控制,启用此模式,需要在API Server的启动参数上添加如下配置,(k8s默然采用此授 ...
- JavaCV更新到1.5.x版本后的依赖问题说明以及如何精简依赖包大小
javaCV全系列文章汇总整理 javacv教程文档手册开发指南汇总篇 前言 JavaCV更新到1.5.x版本,依赖包也迎来了很大变化,体积也变大了不少.很多小伙伴们反馈,之前很多1.3.x和1.4. ...
- Linux find 命令的初步实现(C++)
Implement a myfind command following the find command in UNIX operating system. The myfind command s ...
- zabbix自定义监控nginx
nginx配置ngx_status 1.编译安装时带上--with-http_stub_status_module参数 2.vi nginx.conf location ~* ^/ngx_status ...
- vmstat参数详解
vmstat 5 可以使用ctrl+c停止vmstat,可以看到输出依赖于所用的操作系统,因此可能需要阅读一下手册来解读报告 第一行的值是显示子系统启动以来的平均值,第二行开始展示现在正在发生的情况, ...
- pscp 从win10远程传输文件到centos7,多个虚拟机之间传文件
一.将下载的pscp.exe拷贝到C:\Windows\System32 上传文件 win10 --> linux1 C:\Users\xy>pscp C:\BaiduNetdiskDow ...
- AWS IoT Greengrass是什么?V1和V2版本及其差异
AWS IoT Greengrass Greengrass主要是用于边缘计算或者机器学习有关,对于详细了解请阅读结尾处的官方文档,文档内容也较为丰富. 目录 AWS IoT Greengrass ...
- LuoguP5488 差分与前缀和
题意 给定一个长为\(n\)的序列\(a\),求出其\(k\)阶差分或前缀和.结果的每一项都需要对\(1004535809\)取模. 打表找规律 先看前缀和,设\(n=5\),\(k=4\),按照阶从 ...