Linux学习之---管理及I/O重定向

本文介绍了Linux系统中的I/O基础知识,包括默认输入输出设备和重定向概念。详细讲解了重定向操作,如使用`>`覆盖输出,`2>`重定向错误输出,以及`>>`追加输出等。还探讨了管道`|`如何连接命令,实现数据流的传递。最后提供了一系列练习题,帮助读者巩固和应用所学知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I/O知识

系统设定

        默认输入设备:标准输入,STDIN,0

                                 

        默认输出设备:标准输出,STDOUT ,1   程序正常输出的结果。


        标准错误输出,当程序执行异常,输出到这个地方。默认设备是显示器,STDERR,描述符是2


标准输入:键盘

标准输出和错误输出:显示器


I/O重定向:改变数据的输入或输出设备。

Linux: 

 >:输出重定向。覆盖输出。   仅仅重定向标准输出

2> :重定向错误输出

[root@localhost ~]# ls /varr/ 2> /tmp/var2.out
[root@localhost ~]# cat /tmp/var2.out 
ls: 无法访问/varr/: 没有那个文件或目录

[root@localhost ~]# ls /varr/ 2> /tmp/var2.out > /tmp/var3.out

>> : 追加输出,保留原有内容,在文件尾部追加内容。

2>> : 追加方式

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

[root@localhost ~]# ls /var > /tmp/var.out
[root@localhost ~]# cat /tmp/var.out 
account
adm
cache
caozesheng_py
crash



为了避免文件被覆盖,通过 以下命令开启功能。  使用set +C  取消此功能。

[root@localhost ~]# set -C
[root@localhost ~]# ls /var > /tmp/var.out
-bash: /tmp/var.out: 无法覆盖已存在的文件

[root@localhost ~]# ls /var >| /tmp/var.out                   //添加竖线来强制覆盖


 <:输入重定向

[root@localhost ~]# tr 'a-z' 'A-Z'
asdf
ASDF
^C
[root@localhost ~]# tr 'a-z' 'A-Z' /etc/fstab 
tr: 额外的操作数 "/etc/fstab"
Try 'tr --help' for more information.
[root@localhost ~]# tr 'a-z' 'A-Z' < /etc/fstab 

#
# /ETC/FSTAB
# CREATED BY ANACONDA ON SAT APR 16 18:39:54 2016
#
# ACCESSIBLE FILESYSTEMS, BY REFERENCE, ARE MAINTAINED UNDER '/DEV/DISK'
# SEE MAN PAGES FSTAB(5), FINDFS(8), MOUNT(8) AND/OR BLKID(8) FOR MORE INFO
#
/DEV/MAPPER/CENTOS-ROOT /                       XFS     DEFAULTS        0 0
UUID=0C8C0C2C-D3B8-4658-B899-0FED6A2E94D2 /BOOT                   XFS     DEFAULTS        0 0
/DEV/MAPPER/CENTOS-SWAP SWAP                    SWAP    DEFAULTS        0 0

<< :Here  Document  在此处生成文档。利用此功能在脚本中生成文档。

[root@localhost ~]# cat << <span style="color:#ff0000;">END    文档结束标记(通常用EOF --END OF FILE  或者 END)</span>
> The first line. 
> The second line.
> END
The first line.
The second line.

[root@localhost ~]# cat >> /tmp/file1.txt << EOF
> First 
> Second
> Third
> EOF
[root@localhost ~]# cat /tmp/file1.txt 
First 
Second
Third

既想输出到屏幕,也想保存到文件。HOW?

[root@localhost ~]# echo 'hello world.' | tee /tmp/hello.out
hello world.
[root@localhost ~]# cat /tmp/hello.out 
hello world.



管道

将一前个命令的输出当做后一个命令的输入。

cmd1 | cmd2 | cmd3 | ......

[root@localhost ~]# echo "hello world." | tr 'a-z' 'A-Z'
HELLO WORLD.

[root@localhost ~]# echo "centos" | passwd --stdin hive
更改用户 hive 的密码 。
passwd:所有的身份验证令牌已经成功更新。


[root@localhost ~]# cut -d : -f1 /etc/passwd | sort | tr 'a-z' 'A-Z'
ABRT
ADM
APACHE
AVAHI
AVAHI-AUTOIPD
BENY
BIN
CHRONY
COLORD

仅显示一个文件的行数

[root@localhost ~]# wc -l /etc/passwd |cut -d ' ' -f1
56


练习

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

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


2、取出当前系统上所有用户的shell,每种只显示一次,且按照顺序

[root@localhost ~]# cut -d: -f7 /etc/passwd | sort -u
/bin/bash
/bin/sync
/bin/tcsh
/sbin/halt
/sbin/nologin
/sbin/shutdown


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

[root@localhost ~]# file /var/log/*
/var/log/anaconda:           directory
/var/log/audit:              directory
/var/log/boot.log:           ASCII text, with CRLF line terminators, with escape sequences
/var/log/btmp:               DBase 3 index file
/var/log/btmp-20160510:      DBase 3 index file

[root@localhost ~]# cd /var/log
[root@localhost log]# file `ls /var/log`
anaconda:           directory
audit:              directory
boot.log:           ASCII text, with CRLF line terminators, with escape sequences
btmp:               DBase 3 index file
btmp-20160510:      DBase 3 index file
chrony:             directory


4、取出/etc/inittab 文件的第6行

[root@localhost log]# head -6 /etc/inittab | tail -1
#


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

[root@localhost log]# tail -9 /etc/passwd | head -1 | cut -d: -f1,7 | tee /tmp/users
user4:/bin/tcsh
[root@localhost log]# cat /tmp/users 
user4:/bin/tcsh


6、显示/etc 目录下所有以 pa 开头的文件,并统计个数

[root@localhost log]# ls -d /etc/pa*
/etc/pam.d  /etc/passwd  /etc/passwd-
[root@localhost log]# ls -d /etc/pa* | wc -l
3


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

[root@localhost log]# echo "alias cls=clear" >> ~/.bashrc









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值