linux io重定向指令,linux详细命令说明5(IO重定向和管道)

系统设定

默认输出设备:屏幕(廉价,比将数据显示在A4纸上廉价,电子束打在屏幕上显示,电子束消失则数据丢失)

默认输入设备:键盘

I/O重定向:改变系统默认输入/出的地方

关于输出重定向命令和案例

输出重定向理解为将输出默认指向的屏幕转移到你命令指定的文件中

>: 覆盖输出目标文件内的内容

>>:追加输出

[root@sliver114 ~]# type set

set is a shell builtin

[root@sliver114 ~]# help set

....

-C If set, disallow existing regular files to be overwritten

by redirection of output.

....

Using + rather than - causes these flags to be turned off.

set -C: 禁止对已经存在文件使用覆盖重定向;

强制覆盖输出,则使用 >|

set +C: 关闭上述功能

[root@sliver114 ~]# set -C

[root@sliver114 ~]# ls /usr/ > /tmp/var.out

-bash: /tmp/var.out: cannot overwrite existing file

[root@sliver114 ~]# ls /usr/ >| /tmp/var.out 实现强制输入

[root@sliver114 ~]# cat /tmp/var.out

bin

etc

games

include

....

2>: 只能重定向错误输出 对于正常输出则无能为力

2>>: 追加方式

eg:

[root@sliver114 ~]# set +C

[root@sliver114 ~]# ls /varr > /tmp/var3.out 2> /tmp/err.out

[root@sliver114 ~]# cat /tmp/err.out

ls: /varr: No such file or directory

&>: 重定向标准输出或错误输出至同一个文件

[root@sliver114 tmp]# ls /var1 &> var1.out

[root@sliver114 tmp]# cat var1.out

ls: /var1: No such file or directory

[root@sliver114 tmp]# ls /var &> var1.out

[root@sliver114 tmp]# cat var1.out

account

cache

cvs

db

关于输入重定向:

<

eg:

[root@sliver114 tmp]# cat < /etc/fstab 将/etc/fstab的内容作为输入来取代键盘

LABEL=/ / ext3 defaults 1 1

LABEL=/boot /boot ext3 defaults 1 2

....

eg:

[root@sliver114 tmp]# cat << END // 此时你用什么单词 那么下面你输入内容后 文件内容就以什么单词来结束,但是文档内容不包含这个单词,单词长以EOF或者END (EOF= end of file)

> this is hello

> this is world

> END

this is hello

this is world

关于输入输出重定向组合使用的例子:

看这个输入输出重定向综合的例子,实现像文件中输入内容(觉得通过在win下创建文件然后通过ssh工具远程提交岂不是更方便。或者通过vim来创建文件并添加内容也能达到如下命令的目的)

[root@sliver114 tmp]# cat >> /tmp/myfile.txt << EOF

> this is my ioredirect

> this is sep

> EOF

[root@sliver114 tmp]# cat /tmp/myfile.txt

this is my ioredirect

this is sep

关于管道:

管道:前一个命令的输出,作为后一个命令的输入

命令1 | 命令2 | 命令3 | ...

上面表示:  将命令1的输出结果作为输入流,输入到命令2中做处理,然后命令2处理的结果作为输入流输入到命令3中做处理...

案例如下:

0 字母大写

[root@sliver114 tmp]# echo 'hello world' | tr 'a-z' 'A-Z'

HELLO WORLD

0.1

仅展示文件行数据

[root@sliver114 tmp]# wc -l /etc/passwd

35 /etc/passwd

[root@sliver114 tmp]# wc -l /etc/passwd | cut -d' ' -f1

35

[root@sliver114 tmp]# ls -l /usr/bin | head -2 显示前两行结果

total 110584

-rwxr-xr-x 1 root root 31208 Jul 21 2011 [

1、统计/usr/bin/目录下的文件个数;

# ls /usr/bin | wc -l

2、取出当前系统上所有用户的shell,要求,每种shell只显示一次,并且按顺序进行显示;

# cut -d: -f7 /etc/passwd | sort -u

3、思考:如何显示/var/log目录下每个文件的内容类型?

4、取出/etc/inittab文件的第6行; 先获取前六行 然后在获取倒数第一行

[root@sliver114 tmp]# head -6 /etc/inittab | tail -1

# Modified for RHS Linux by Marc Ewing and Donnie Barnes

5、取出/etc/passwd文件中倒数第9个用户的用户名和shell,显示到屏幕上并将其保存至/tmp/users文件中;

[root@sliver114 tmp]# tail -9 /etc/passwd | head -1 | cut -d: -f1,7 | tee /tmp/test.txt

apache:/sbin/nologin

[root@sliver114 tmp]# cat test.txt

apache:/sbin/nologin

6、显示/etc目录下所有以pa开头的文件,并统计其个数; ls -l 表示仅仅展示自身属性 并不展示自身下的孩子 这里要求仅展示以pa开头的文件 因此需要用到 -d

[root@sliver114 tmp]# ls -d /etc/pa*

/etc/pam.d /etc/pam_smb.conf /etc/passwd /etc/passwd.OLD

/etc/pam_pkcs11 /etc/pango /etc/passwd-

[root@sliver114 tmp]# ls -d /etc/pa* | wc -l

7、不使用文本编辑器,将alias cls=clear一行内容添加至当前用户的.bashrc文件中;

# echo "alias cls=clear" >> ~/.bashrc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值