CentOS 7 linux 基础操作命令-4 重定向,软硬连接。。。

1、tr 指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。

Usage:  tr   [OPTION]... SET1   [SET2]

  • -c, --complement:反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
  • -d, --delete:删除指令字符
  • -s, --squeeze-repeats:缩减连续重复的字符成指定的单个字符
  • -t, --truncate-set1:削减 SET1 指定范围,使之与 SET2 设定长度相等
  • --help:显示程序用法信息
  • --version:显示程序本身的版本信息
使用管道符号 ‘ | ’ 将管道符号前的标准输出数据传到tr命令,将标准输出类容中的空格替换为 ‘ : ’;
[root@test data]# cat hosts | tr ' ' ':'  
127.0.0.1:::localhost:localhost.localdomain:localhost4:localhost4.localdomain4
::1:::::::::localhost:localhost.localdomain:localhost6:localhost6.localdomain6

127.0.0.1:test

删除test字符
[root@test data]# cat hosts |tr -d 'test'
127.0.0.1   loclhost loclhost.loclom loclhost4 loclhost4.loclom4
::1         loclhost loclhost.loclom loclhost6 loclhost6.loclom6

127.0.0.1 

[root@test data]# cat hosts |tr -s 'localhost' 'a'
127.0.0.1   a a.adamain a4 a4.adamain4
::1         a a.adamain a6 a6.adamain6

2、软硬链接;

Linux 链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln 命令产生硬链接。

硬连接

硬连接指通过索引节点编号来进行连接。在 Linux 的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在 Linux 中,多个文件名指向同一索引节点是存在的。比如:A 是 B 的硬链接(A 和 B 都是文件名),则 A 的目录项中的 inode 节点号与 B 的目录项中的 inode 节点号相同,即一个 inode 节点对应两个不同的文件名,两个文件名指向同一个文件,A 和 B 对文件系统来说是完全平等的。删除其中任何一个都不会影响另外一个的访问。

硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

软连接

另外一种连接称之为符号连接(Symbolic Link),也叫软连接。软链接文件有类似于 Windows 的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。比如:A 是 B 的软链接(A 和 B 都是文件名),A 的目录项中的 inode 节点号与 B 的目录项中的 inode 节点号不相同,A 和 B 指向的是两个不同的 inode,继而指向两块不同的数据块。但是 A 的数据块中存放的只是 B 的路径名(可以根据这个找到 B 的目录项)。A 和 B 之间是“主从”关系,如果 B 被删除了,A 仍然存在(因为两个是不同的文件),但指向的是一个无效的链接。

说简单点硬连接就是同一个文件,取不同的名字,由于同一个文件的节点编号(innodes)在硬盘设备不会改变,改变了就不是同一个文件了,所以硬连接不可以跨分区创建。
[root@test data]# ln hosts test-hosts
[root@test data]# ls -li
total 8
109214521 -rw-r--r--. 2 root root 178 Feb 23  2017 hosts
109214521 -rw-r--r--. 2 root root 178 Feb 23  2017 test-hosts


软连接:简单点就是像windows中的快捷方式,软连接对源文件具有依赖性,当源文件找不到时,软链接将会失效。
[root@xiandian data]#  ln -s /data/test-hosts /opt/
[root@xiandian data]#  ln -s /data/test-hosts /mnt/
[root@xiandian data]#  ls -l /opt/
total 0
lrwxrwxrwx. 1 root root 16 Apr 18 18:12 test-hosts -> /data/test-hosts
[root@xiandian data]# ls -l /mnt/
total 0
lrwxrwxrwx. 1 root root 16 Apr 18 18:12 test-hosts -> /data/test-hosts

 

使用readlink命令可以查看链接文件的源文件地址;

[root@xiandian opt]# ls -l
total 0
lrwxrwxrwx. 1 root root 16 Apr 18 18:12 test-hosts -> /data/test-hosts
[root@xiandian opt]# readlink test-hosts 
/data/test-hosts

3、 file 命令 查看文件的类型;


Usage: file [OPTION...] [FILE...]

[root@test opt]# file test-hosts 
test-hosts: symbolic link to `/data/test-hosts'

4、seq 命令输入标准输出;

[root@xiandian ~]# seq --help
Usage: seq [OPTION]... LAST
  or:  seq [OPTION]... FIRST LAST
  or:  seq [OPTION]... FIRST INCREMENT LAST
Print numbers from FIRST to LAST, in steps of INCREMENT.

Mandatory arguments to long options are mandatory for short options too.
  -f, --format=FORMAT      use printf style floating-point FORMAT
  -s, --separator=STRING   use STRING to separate numbers (default: \n)
  -w, --equal-width        equalize width by padding with leading zeroes
      --help     display this help and exit
      --version  output version information and exit

-s 选项指定分割符,默认为换行符 \n 
[root@xiandian ~]# seq -s + 10
1+2+3+4+5+6+7+8+9+10

[root@xiandian ~]# seq 10
1
2
3
4
5
6
7
8
9
10

5、标准输出,标准输入,标准错误,重定向;tee命令,可以同时实现标准输出和重定向输出;

先看下: 标准输出(1),标准输入(0),标准错误(2);' > '为覆盖文件内容,' >> ' 为追加内容; 

如: 标准输出(1)
[root@xiandian ~]# ls
asd.sh                      HELP.MAIL         read.sh   test3.sh  test.sh
CentOS_7.2_x86_64_XD.qcow2  keystoneuser.txt  test2.sh  test4.sh

标准错误(2)

[root@xiandian ~]# ls /aszxcghqsdhjasdhjhja
ls: cannot access /aszxcghqsdhjasdhjhja: No such file or directory





重定向:
如:将标准输出,标准错误同时重定向到flie1文件中;

[root@xiandian ~]# ls /root/ /aszxcghqsdhjasdhjhja >file1 2>&1
[root@xiandian ~]# cat file1 
ls: cannot access /aszxcghqsdhjasdhjhja: No such file or directory
/root/:
asd.sh
CentOS_7.2_x86_64_XD.qcow2
file1
HELP.MAIL
keystoneuser.txt
read.sh
test2.sh
test3.sh
test4.sh
test.sh
或者:
[root@xiandian ~]# ls /root/ /aszxcghqsdhjasdhjhja 1>file2 2>&1
[root@xiandian ~]# ls /root/ /aszxcghqsdhjasdhjhja 2>file3 1>&2
[root@xiandian ~]# ls /root/ /aszxcghqsdhjasdhjhja >&file4(相对上面较新的用法)

如果直接使用:
# > file1             //为清空文件内容

标准输入(0):
[root@xiandian ~]# cat <<EOF   //这里输入EOF 结束标准输入,
> asd
> asd
> adas
> adas
> EOF
asd
asd
adas
adas

综合运用:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值