什么是文件描述符FD或者文件句柄?

通过构建一个带有编号标记的通道(文件描述符)的进程结构来管理打开的文件。今晨连接到文件,从而达到这些文件所代表的的数据内容或者设备。通过使用通道0、1、2(称为标准输入,标准输出,标准错误)的默认连接创建进程。进程使用3号及以上标号的通道连接其他文件。

掌握常见文件描述符的作用

 表示stdin(标准输入),默认连接为键盘,仅读取

 表示stdout(标准输出),默认连接为终端,仅写入

 表示stderr(标准错误),默认连接为终端,仅写入

+ 表示filename(其他文件),可以读取/写入

示例:

保存时间戳

[root@localhost ~]# date
2018年 08月 24日 星期五 :: CST
[root@localhost ~]# date > /tmp/saved-timestamp
[root@localhost ~]# cat /tmp/saved-timestamp
2018年 08月 24日 星期五 :: CST
[root@localhost ~]#

将一个文件的最后100行保存到另一个文件

[root@localhost ~]# tail -n  /var/log/demsg > /tmp/last--boot-messages
[root@localhost ~]#

将四个文件合并为一个

[root@localhost ~]# cat file1 file2 file3 file4 > /tmp/all-four-in-one
[root@localhost ~]#

累出家目录中的隐藏文件名和常规文件名保存到文件中

[root@localhost ~]# ls -a > /tmp/my-file-names
[root@localhost ~]# cat /tmp/my-file-names
.
..
anaconda-ks.cfg
.bash_history
.bash_logout
.bash_profile
.bashrc
.cache
.config
.cshrc
.dbus
Desktop
.gitconfig
initial-setup-ks.cfg
.mozilla
.pki
.ssh
.tcshrc
.viminfo
.xauthKYrxVH
.Xauthority
[root@localhost ~]#

使用普通用户对系统目录进行访问会被拒绝,将错误从定向到文件

[root@localhost ~]# find /etc -name passwd  > /tmp/errors
[root@localhost ~]#

将命令的输出和错误消息分别保存到单独的文件中

[root@localhost ~]# find /etc/-name passwd > /tmp/output  > /tmp/errors
[root@localhost ~]#

忽略并丢弃错误消息

[root@localhost ~]# find /etc/ -name passwd > /tmp/output > /dev/null
[root@localhost ~]#

基于输出及输入重定向实现管理

head /tail/wc/cut/sort/uniq/diff/patch/tr/grep

进程管道PIping

示例:

将ls长葛市输出分页显示

[root@localhost ~]# ls -l /usr/bin/ | less

总用量
-rwxr-xr-x. root root 11月 [
-rwxr-xr-x. root root 8月 a2p
-rwxr-xr-x. root root 8月 abrt-action-analyze-backtrace
-rwxr-xr-x. root root 8月 abrt-action-analyze-c
-rwxr-xr-x. root root 8月 abrt-action-analyze-ccpp-local
-rwxr-xr-x. root root 8月 abrt-action-analyze-core
-rwxr-xr-x. root root 8月 abrt-action-analyze-oops
-rwxr-xr-x. root root 8月 abrt-action-analyze-python

计算ls输出的行数并且保存到文件

[root@localhost ~]# ls | wc -l > /tmp/how-many-files
[root@localhost ~]# cat /tmp/how-many-files [root@localhost ~]#

将ls输出的前10行保存到文件

[root@localhost ~]# ls -t | head -n  > /tmp/ten-last-changed-files
[root@localhost ~]# cat /tmp/ten-last-changed-files
Desktop
initial-setup-ks.cfg
anaconda-ks.cfg
[root@localhost ~]#
只有三行。。。

在终端商显示ls列表,同时将文件列表存储到文件中

[root@localhost ~]# ls -l | tee /tmp/saved-output
总用量
-rw-------. root root 8月 : anaconda-ks.cfg
drwxr-xr-x. root root 8月 : Desktop
-rw-r--r--. root root 8月 : initial-setup-ks.cfg
[root@localhost ~]# cat /tmp/saved-output
总用量
-rw-------. root root 8月 : anaconda-ks.cfg
drwxr-xr-x. root root 8月 : Desktop
-rw-r--r--. root root 8月 : initial-setup-ks.cfg
[root@localhost ~]#

确定当前窗口的终端设备,将ls结果作为邮件发送,并在此窗口查看输出内容

[root@localhost ~]# ls -l | tee /dev/pts/ | mail -s subject seven_nighter@.com
总用量
-rw-------. root root 8月 : anaconda-ks.cfg
drwxr-xr-x. root root 8月 : Desktop
-rw-r--r--. root root 8月 : initial-setup-ks.cfg
您在 /var/spool/mail/root 中有邮件
[root@localhost ~]#

掌握参数传递机制Xargs

xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具,它擅长将标准输入数据转换成命令行参数,xargs能够处理管道或者stdin并且将其转换成特定的命令的参数。xargs也可以将单行或者多行文本输入转换为其他格式,例如多行变单行,单行变多行。xargs的默认命令时echo,空格是默认的定界符,这就意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代,xargs是构建单行命令的重要组件之一。

具体操作百度。。

wc - print newline, word,and bytes counts for each file

计算文件的行数,单词数,字节数

[root@localhost ~]# wc /etc/passwd
/etc/passwd
[root@localhost ~]#
[root@localhost ~]# wc -l /etc/passwd
/etc/passwd 行数
[root@localhost ~]#
[root@localhost ~]# wc -w /etc/passwd
/etc/passwd 单词数
[root@localhost ~]#
[root@localhost ~]# wc -c /etc/passwd
/etc/passwd 字节数
[root@localhost ~]#

按列提取文件

-d 指明列分隔符 默认tab

-f 选择输出的区域

-c 指定字符位置

[root@localhost ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@localhost ~]# cut -c1- /etc/passwd
roo
bin
dae
adm
lp:
syn
shu

sort - sort lines of text files

排序输出,默认按照首字符从头至尾的顺序排序

-r 逆序(倒叙)

-n 按数字排序

-t 指明分隔符 与 -k 连用

-k 按指定的域排序

[root@localhost ~]# sort -t: -k3 /etc/passwd | cut -d: -f3 | head -n 

[root@localhost ~]# 
[root@localhost ~]# sort -t: -k3 -n /etc/passwd | cut -d: -f3 | head -n 

[root@localhost ~]# 
[root@localhost ~]# sort -t: -k3 -n -r /etc/passwd | cut -d: -f3 | head -n 

[root@localhost ~]# 

。。。。更加纤细的请自行百度。

运维基础-IO 管道的更多相关文章

  1. Linux系统运维基础测试题

    1    Linux运维基础测试题(第一关) 通过这段时间学习Linux基础命令,为了检测自己对Linux基础命令掌握的情况,从网上整理13到测试题,并将其整理出来供大家参考学习. 1.1    习题 ...

  2. 第一阶段·Linux运维基础-第1章·Linux基础及入门介绍

    01-课程介绍-学习流程 02-服务器硬件-详解 03-服务器核心硬件-服务器型号-电源-CPU 01-课程介绍-学习流程 1.1. 光看不练,等于白干: 1.2 不看光练,思想怠慢: 1.3 即看又 ...

  3. Linux运维基础采集项

    1. Linux运维基础采集项 做运维,不怕出问题,怕的是出了问题,抓不到现场,两眼摸黑.所以,依靠强大的监控系统,收集尽可能多的指标,意义重大.但哪些指标才是有意义的呢,本着从实践中来的思想,各位工 ...

  4. Linux运维基础

    一.服务器硬件 二.Linux的发展史 三.Linux的系统安装和配置 四.Xshell的安装和优化 五.远程连接排错 六.Linux命令初识 七.Linux系统初识与优化 八.Linux目录结构 九 ...

  5. linux运维基础知识

    linux运维基础知识大全 一,序言 每一个微不足道的知识,也是未来的铺垫.每一份工作的薪资职位,也是曾经努力的结果. 二,服务器 1,运维人员工作职责: 1)保证数据不丢失:2)保证服务器24小时运 ...

  6. HBase运维基础--元数据逆向修复原理

    背景 鉴于上次一篇文章——“云HBase小组成功抢救某公司自建HBase集群,挽救30+T数据”的读者反馈,对HBase的逆向工程比较感兴趣,并咨询如何使用相应工具进行运维等等.总的来说,就是想更深层 ...

  7. linux运维基础__争取十月前研究的差不多

    转来的一编,考虑在十月前研究的差不多 linux运维人员基础 1.很多地方经常会用到的rsync工具 实施几台服务器的同步效果 我们公司就是使用这个工具完成服务器的游戏的服务端和客户端同步,有几个文章 ...

  8. 网络配置——Linux运维基础

    今天把Linux的网络配置总结了一下,尽管并不难可是是个比較重要的基础.然后我也不知到自己以后是否会做运维,可是我知道自己比較喜欢刨根问底.还有就是我很珍惜我以前掌握过的这些运维的技能.今天突然间问自 ...

  9. [转帖] Linux运维基础知识学习内容

    原作者地址:https://www.cnblogs.com/chenshoubiao/p/4793487.html 最近在学习 linux  对简单的命令有所掌握 但是 复杂的脚本 shell pyt ...

随机推荐

  1. Synergy 使用方法备忘

    Synergy 是 Symless 公司推出的「鼠标键盘共享软件」(mouse and keyboard sharing software),其功能还包括「共享剪贴板」(sharing clipboa ...

  2. Java数据库连接JDBC用到哪种设计模式?

    还没看桥接模式,占tag 桥接模式: 定义 :将抽象部分与它的实现部分分离,使它们都可以独立地变化. 意图 :将抽象与实现解耦. 桥接模式所涉及的角色 1.  Abstraction :定义抽象接口, ...

  3. 【python自动化】python 常用时间获取方法

    代码如下: import datetime import time DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" DATE_FORMAT = &quo ...

  4. xsy 1845 - GCD

    from NOIP2016模拟题34 Description 给定一个长度\(n\le 10^6\)的序列, 给定\(A, B\) 给出一个序列,要求你通过如下两个操作使得序列中所有数的最大公约数大于 ...

  5. Python [拷贝copy / 深度拷贝deepcopy] | 可视化理解

    Python 是一门面向对象的语言, 在Python一切皆对象. 每一个对象都有由以下三个属性组成: ------------------------------------------------- ...

  6. cogs2060 除法表达式

    http://blog.csdn.net/sdfzyhx/article/details/52254071 作为分母的数当然是越少越好.将x2作为分母,其他作为分子,不断约分,最后判断. /*by S ...

  7. CentOS找不到想要的镜像版本?

    CentOS找不到想要的镜像版本? 情景: 当学习Linux时,一般教程不是最新的,教程里的CentOS版本也就不是最新的,这个时候, 在看着教程练习的时候就需要安装指定的版本,避免因为版本不同造成困 ...

  8. WKWebView与js交互中产生的内存泄漏

    最近开发中突然发现富文本帖子详情内存没有释放掉,找了好久问题都没找到,终于今天发现了问题,先上一点代码片段 WKWebViewConfiguration *configuration = [[WKWe ...

  9. inotify+rsync的初步配置

    inotify的简单配置  Linux内核从2.6.13开始,引入了inotify机制.通过intofity机制,能够对文件系统的变化进行监控,如对文件进行创建.删除.修改等操作,可以及时通知应用程序 ...

  10. 使用vue-element-admin框架开发时遇到的跨域问题

    之前 使用js和jquery开发时也碰到过接口请求时的跨域问题, 但是在使用vue-element-admin开发也碰到这个问题,而且不能使用之前的方法解决,查过不少资料,找到一个很好的方法解决了这个 ...