linux 重定向 log 大小,Linux标准重定向

一切皆文件,都是文件的操作

三种I/O设备

标准的输入输出

程序:指令 数据

读入数据:Input

输出数据:Output

系统中打开一个文件系统自动分配文件描述符,除了0,1,2是固定的,其他的都是不固定的

打开的文件都有一个fd:file descriptor (文件描述符)

Linux给程序提供三种I/O设备

标准输入 (STDIN) -0 默认接受来自终端窗口的输入

标准输出 (STDOUT) -1 默认输出到终端窗口

标准错误 (STDERR) -2 默认输出到终端窗口

一个终端运行tail命令打开一个文件

[root@C8-1 ~]# echo {a..z}{1..9} >IOtest.test

[root@C8-1 ~]# cat IOtest.test

……

[root@C8-1 ~]# tail -f IOtest.test

……

另一个终端查看proc文件夹

[root@C8-1 ~]# ll /proc/2053/fd/

total 0

lrwx------. 1 root root 64 Jun 20 06:13 0 -> /dev/pts/0

lrwx------. 1 root root 64 Jun 20 06:13 1 -> /dev/pts/0

lrwx------. 1 root root 64 Jun 20 06:13 2 -> /dev/pts/0

lr-x------. 1 root root 64 Jun 20 06:13 3 -> /root/IOtest.test

lr-x------. 1 root root 64 Jun 20 06:13 4 -> anon_inode:inotify

其中0,1,2,文件描述符

查看当前shell

每一个窗口对应一个shell,每个窗口都有自己的输入输出

[root@C8-1 ~]# ps aux |grep bash

root 1691 0.0 0.1 26544 3368 pts/0 Ss 01:59 0:00 -bash

root 2069 0.0 0.2 26412 4984 pts/1 Ss 06:08 0:00 -bash

root 2106 0.0 0.0 12108 1056 pts/1 R 06:23 0:00 grep --color=auto bash

[root@C8-1 ~]# echo $$

2069

[root@C8-1 ~]# ll /proc/$$/fd

total 0

lrwx------. 1 root root 64 Jun 20 06:08 0 -> /dev/pts/1

lrwx------. 1 root root 64 Jun 20 06:08 1 -> /dev/pts/1

lrwx------. 1 root root 64 Jun 20 06:08 2 -> /dev/pts/1

lrwx------. 1 root root 64 Jun 20 06:25 255 -> /dev/pts/1

lr-x------. 1 root root 64 Jun 20 06:08 3 -> /var/lib/sss/mc/passwd

lrwx------. 1 root root 64 Jun 20 06:08 4 -> 'socket:[54566]'

系统中每一个程序运行,都会分配一个进程编号,并且对应的都有固定的三个文件描述符0,1,2

[root@C8-1 ~]# ll /proc/self/fd

total 0

lrwx------. 1 root root 64 Jun 20 06:29 0 -> /dev/pts/1

lrwx------. 1 root root 64 Jun 20 06:29 1 -> /dev/pts/1

lrwx------. 1 root root 64 Jun 20 06:29 2 -> /dev/pts/1

lr-x------. 1 root root 64 Jun 20 06:29 3 -> /var/lib/sss/mc/passwd

lrwx------. 1 root root 64 Jun 20 06:29 4 -> 'socket:[56107]'

lr-x------. 1 root root 64 Jun 20 06:29 5 -> /var/lib/sss/mc/group

lr-x------. 1 root root 64 Jun 20 06:29 6 -> /proc/2111/fd

标准重定向

I/O重定向 redirect 改变方向 扳道工

把当前设备默认输入输出改变方向

格式

命令 操作符好 文件名

1> 或 > 输出重定向 >|强制覆盖

2> 错误重定向 不止错误,也可能是警报或提示信息

&> 所有,包括1和2

示例1:

使用echo配合重定向创建空文件

echo默认输出为换行符,如果不换行需要添加-n参数

[root@C8-1 ~]# ll

total 0

[root@C8-1 ~]#

##使用echo创建文件redirect.test

[root@C8-1 ~]# echo > redirect.test

##查看文件显示有一个字节

[root@C8-1 ~]# ll

total 4

-rw-r--r--. 1 root root 1 Jun 20 06:59 redirect.test

##cat内容显示空行

[root@C8-1 ~]# cat redirect.test

[root@C8-1 ~]# hexdump -C redirect.test ##使用hexdump查看二进制文件,发现真的有一个换行符

00000000 0a |.|

00000001

##使用echo加-n参数重定向覆盖原文件

[root@C8-1 ~]# echo -n > redirect.test

##查看发现文件大小为0

[root@C8-1 ~]# ll

total 0

-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test

##cat查看为空

[root@C8-1 ~]# cat redirect.test

[root@C8-1 ~]# hexdump -C redirect.test ##使用hexdump查看为真空

[root@C8-1 ~]#

示例2:

提示符重定向测试

[root@C8-1 ~]# ll

total 0

-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test

[root@C8-1 ~]# type rm

rm is aliased to `rm -i'

[root@C8-1 ~]# rm redirect.test 2> delR.log

[root@C8-1 ~]# ll

total 4

-rw-r--r--. 1 root root 47 Jun 20 07:11 delR.log

-rw-r--r--. 1 root root 0 Jun 20 07:02 redirect.test

[root@C8-1 ~]# cat delR.log

rm: remove regular empty file 'redirect.test'? [root@C8-1 ~]#

提示被隐藏了,并不代表不能继续执行

[root@C8-1 ~]# rm redirect.test 2> delR.log

y

[root@C8-1 ~]# ll

total 4

-rw-r--r--. 1 root root 47 Jun 20 07:14 delR.log

[root@C8-1 ~]#

示例3:

如果想屏幕上的显示的东东不显示,可以重定向到文件,如果连临时文件都不想要,可以重定向到null

##拍一拍衣袖,不带走一丝云彩

[root@C8-1 ~]# rm -rf /* /.[^.]* &> /dev/null

追加

">>"

"2>>"

"&>>"

在原有基础上追加内容

合并重定向

加()或{}

[root@C8-1 ~]# (date;ls) > dl.log

[root@C8-1 ~]# cat dl.log

Sat Jun 20 07:50:11 EDT 2020

2020-06-16_09:36:55.log

2021-06-17_09:37:53.log

618

618bak

anaconda-ks.cfg

dl.log

linux.txt

win.txt

[root@C8-1 ~]# {ls;date;} > dl.log ##花括号前需要有一个空格,结尾需要;

-bash: syntax error near unexpected token `}'

[root@C8-1 ~]# { ls;date;} > dl.log

[root@C8-1 ~]# cat dl.log

2020-06-16_09:36:55.log

2021-06-17_09:37:53.log

618

618bak

anaconda-ks.cfg

dl.log

linux.txt

win.txt

Sat Jun 20 07:52:05 EDT 2020

[root@C8-1 ~]#

来源:https://www.icode9.com/content-3-713851.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值