首页 > 其他分享 >软硬链接和IO重定向及管道

软硬链接和IO重定向及管道

时间:2023-01-04 21:56:35浏览次数:45  
标签:file 软硬 重定向 cfg ks IO txt root Rocky8

  • 硬链接和软连接

ln filename linkname   (属性不变)

[root@Rocky8 ~]# ln a.txt aa.txt
[root@Rocky8 ~]# ls
aa.txt   a.txt  Desktop    Downloads       Music     Public Videos
anaconda-ks.cfg  data   Documents  initial-setup-ks.cfg  Pictures  Templates
[root@Rocky8 ~]# ll -i  a.txt aa.txt
34543754 -rw-r--r--. 2(链接数) root root 0 Jul 14 10:17 aa.txt
34543754 -rw-r--r--. 2 (链接数)root root 0 Jul 14 10:17 a.txt

注意:文件目录不支持硬链接创建

    不支持跨分区创建

    链接数增加

ln -s filename linkname 创建软连接

   注意:原文件需要补齐绝对路径

       或者原文件的相对路径是相对于软链接文件的路径,因此需要先返回根目录下

       支持创建目录和跨分区创建

   删除软链接:最后路径的目录不要带“/”,不然会把原文件的数据删除,有危险

  • 标准IO重定向和管道

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

提供三种I/O设备

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

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

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

  • I/O重定向redirect

>或者1>

Command  >  /path/to/file.out  2>  /path/to/file.err

[root@Rocky8 ~]# ls > /dev/pts/1
[root@Rocky8 ~]# ls anaconda-ks.cfg xxx 1> stdoutlog 2> stderrlog

&> 标准输出和标准错误共同放入一个文件中

[root@Rocky8 ~]# ls anaconda-ks.cfg xxx &> stderrlog
[root@Rocky8 ~]# cat stderrlog
ls: cannot access 'xxx': No such file or directory
anaconda-ks.cfg

Command  >  /path/to/file  2>&1  标准输出放在文件中,标准错误放在标准输出中

Command  2>  /path/to/file  1>&2 标准错误放在文件中,标准输入放在标准错误中

都可以放在同一个文件中

Command  2>&1  >  /path/to/file

从左到右执行,执行对的结果先往1里放,没有说明对的输出放置地方,将2(错误输出)打印在屏幕上,之后将1(标准输出)放在文件/path/to/file

>>追加

[root@Rocky8 ~]# ls initial-setup-ks.cfg >> stderrlog
[root@Rocky8 ~]# cat stderrlog
ls: cannot access 'xxx': No such file or directory
anaconda-ks.cfg
initial-setup-ks.cfg

  

   &>>追加重定向

   Command >> /path/to/file.out 2>&1

   < 标准输入

[root@Rocky8 ~]# echo 2*3 > data.txt
[root@Rocky8 ~]# bc < data.txt
6
[root@Rocky8 ~]# seq -s+ 100 > seq.log
[root@Rocky8 ~]# bc < seq.log
5050
  • 管道符:|

Cmd1 | cmd2 (cmd1:标准输出;cmd2:标准输入)

[root@Rocky8 ~]# echo 2+3 | bc
5

Cmd1  |&  cmd2 (cmd1可以是标准输出也可以是错误输出)

=Cmd1  2>&1  |  cmd2

[root@Rocky8 ~]# 2+3 |& bc

(standard_in) 1: illegal character: :

(standard_in) 1: syntax error

(standard_in) 1: illegal character: :

(standard_in) 1: syntax error

  • 高级重定向写法

Cmd  <<< ‘string’

输入字符串,Cmd支持stdin,回车输出结果

[root@Rocky8 ~]# tr 'a-z' 'A-Z' <<< 'i am tian'
I AM TIAN

   Cmd1 <  <(cmd2)=cmd1 | cmd2

   <(cmd2)标准输出写到一个临时文件里,文件内的内容传给cmd1,作为cmd1的标准输入

[root@Rocky8 ~]# bc < <(seq -s+ 10)
55
[root@Rocky8 ~]# ll <(seq -s+ 10)
lr-x------. 1 root root 64 Jul 15 10:28 /dev/fd/63 -> 'pipe:[119831]'(临时文件)

 

标签:file,软硬,重定向,cfg,ks,IO,txt,root,Rocky8
From: https://www.cnblogs.com/Lx-learner/p/17026106.html

相关文章