@linux文件处理
1|0
1|01. 特殊权限
1. setuid
权限位作用于属主的位置 可执行的权限位 执行这条命令的时候,相当于这条命令的所有者 root
s 表示
[root@qls ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
[root@qls ~]# ll /usr/bin/rm
-rwxr-xr-x. 1 root root 62952 Oct 31 2018 /usr/bin/rm
[root@qls ~]# su - qls01
Last login: Thu Jul 23 12:24:52 CST 2020 from 10.0.0.1 on pts/4
[qls01@qls ~]$ rm -rf /opt/
rm: cannot remove ‘/opt/’: Permission denied
[qls01@qls ~]$ ll -d /
dr-xr-xr-x. 17 root root 224 Jul 22 10:21 /
[qls01@qls ~]$ logout
[root@qls ~]# rm -rf /opt/
[root@qls ~]# ll /opt/
ls: cannot access /opt/: No such file or directory
[root@qls ~]# mkdir /opt/
[root@qls ~]# chmod 557 /
[root@qls ~]# ll -d /
dr-xr-xrwx. 17 root root 224 Jul 24 08:38 /
[root@qls ~]# su - qls01
Last login: Fri Jul 24 08:37:21 CST 2020 on pts/0
[qls01@qls ~]$ rm -rf /opt/
[qls01@qls ~]$ logout
[root@qls ~]# chmod -w /
chmod: /: new permissions are r-xr-xrwx, not r-xr-xr-x
[root@qls ~]# chmod o-w /
[root@qls ~]# ll -d /
dr-xr-xr-x. 16 root root 213 Jul 24 08:39 /
[root@qls ~]# ll /usr/bin/rm
-rwxr-xr-x. 1 root root 62952 Oct 31 2018 /usr/bin/rm
[root@qls ~]# chmod u+s /usr/bin/rm
[root@qls ~]# ll /usr/bin/rm
-rwsr-xr-x. 1 root root 62952 Oct 31 2018 /usr/bin/rm
[root@qls ~]# mkdir /opt
[root@qls ~]# su - qls01
Last login: Fri Jul 24 08:39:42 CST 2020 on pts/0
[qls01@qls ~]$ rm -rf /opt/
[qls01@qls ~]$ logout
[root@qls ~]# mkdir /opt
[root@qls ~]# chmod u-s /usr/bin/rm
[root@qls ~]# ll /usr/bin/yum
-rwxr-xr-x. 1 root root 801 Nov 5 2018 /usr/bin/yum
[root@qls ~]# su - qls01
Last login: Fri Jul 24 08:42:10 CST 2020 on pts/0
[qls01@qls ~]$ yum install -y sl
Loaded plugins: fastestmirror
You need to be root to perform this command.
[qls01@qls ~]$ yum install -y mysql-server
Loaded plugins: fastestmirror
You need to be root to perform this command.
[qls01@qls ~]$ logout
[root@qls ~]# ll /usr/bin/yum
-rwxr-xr-x. 1 root root 801 Nov 5 2018 /usr/bin/yum
[root@qls ~]# chmod u+s /usr/bin/yum
[root@qls ~]# ll /usr/bin/yum
-rwsr-xr-x. 1 root root 801 Nov 5 2018 /usr/bin/yum
[root@qls ~]# su - qls01
Last login: Fri Jul 24 08:45:28 CST 2020 on pts/0
[qls01@qls ~]$ yum install -y mysql-server
Loaded plugins: fastestmirror
You need to be root to perform this command.
#普通用户不能使用yum,命令,即使添加了setuid的权限也不行
#此权限不能乱给 知道就行
[root@qls ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54160 Oct 31 2018 /usr/bin/cat
[root@qls ~]# stat /usr/bin/cat
File: ‘/usr/bin/cat’
Size: 54160 Blocks: 112 IO Block: 4096 regular file
Device: 803h/2051d Inode: 201349408 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-07-24 08:52:48.370831557 +0800
Modify: 2018-10-31 03:16:01.000000000 +0800
Change: 2020-07-24 08:52:58.961832157 +0800
Birth: -
[root@qls ~]# stat /usr/bin/passwd
File: ‘/usr/bin/passwd’
Size: 27832 Blocks: 56 IO Block: 4096 regular file
Device: 803h/2051d Inode: 201636086 Links: 1
Access: (4755/-rwsr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-07-23 09:58:46.365163401 +0800
Modify: 2014-06-10 14:27:56.000000000 +0800
Change: 2020-07-06 02:14:21.159994247 +0800
Birth: -
setuid的权限是4开头的
[root@qls ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
[root@qls ~]# chmod -x /usr/bin/cat
[root@qls ~]# ll /usr/bin/cat
-rw-r--r--. 1 root root 54160 Oct 31 2018 /usr/bin/cat
[root@qls ~]# chmod u+s /usr/bin/cat
[root@qls ~]# ll /usr/bin/cat
-rwSr--r--. 1 root root 54160 Oct 31 2018 /usr/bin/cat
[root@qls ~]# chmod +x /usr/bin/cat
[root@qls ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54160 Oct 31 2018 /usr/bin/cat
[root@qls ~]# chmod u-s /usr/bin/cat
[root@qls ~]#
大S 和 小s的区别
拥有执行权限,设置setuid之后是小s
没有执行权限,设置setuid之后是大S
2. setgid
权限为作用在属组的x为 使用 s表示 有大S 和小s
用户在某个目录下新创建的目录或者文件,默认的所属组是自己的基本组
当设置setgid之后,用户新创建的目录或者文件的默认所属组不在是自己的组了
默认的组就是这个目录的所属组 让多个用户能够共享一个目录
[root@qls ~]# mkdir /data
[root@qls ~]# ll -d /data
drwxr-xr-x 2 root root 6 Jul 24 09:06 /data
[root@qls ~]# chmod 770 /data
[root@qls ~]# ll -d /data
drwxrwx--- 2 root root 6 Jul 24 09:06 /data
[root@qls ~]# groupadd ops_group
[root@qls ~]# chgrp ops_group /data/
[root@qls ~]# ll -d /data/
drwxrwx--- 2 root ops_group 6 Jul 24 09:06 /data/
[root@qls ~]# useradd ops01
[root@qls ~]# useradd ops02
[root@qls ~]# useradd ops03
[root@qls ~]# id ops03
uid=1046(ops03) gid=1048(ops03) groups=1048(ops03)
[root@qls ~]# usermod -aG ops_group ops01
[root@qls ~]# usermod -aG ops_group ops02
[root@qls ~]# usermod -aG ops_group ops03
[root@qls ~]# su - ops01
[ops01@qls ~]$ touch /data/ops01.txt
[ops01@qls ~]$ ll /data/ops01.txt
-rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 /data/ops01.txt
[ops01@qls ~]$ logout
[root@qls ~]# su - ops02
[ops02@qls ~]$ touch /data/ops02.txt
[ops02@qls ~]$ ll /data/ops02.txt
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 /data/ops02.txt
[ops02@qls ~]$ logout
[root@qls ~]# su - ops03
[ops03@qls ~]$ touch /data/ops03.txt
[ops03@qls ~]$ ll /data/ops03.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 /data/ops03.txt
[ops03@qls ~]$ logout
[root@qls ~]# ll /data/
total 0
-rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt
[root@qls ~]# chmod g+s /data/
[root@qls ~]# ll -d /data/
drwxrws--- 2 root ops_group 57 Jul 24 09:11 /data/
[root@qls ~]# stat /data/
File: ‘/data/’
Size: 57 Blocks: 0 IO Block: 4096 directory
Device: 803h/2051d Inode: 818813 Links: 2
Access: (2770/drwxrws---) Uid: ( 0/ root) Gid: ( 1045/ops_group)
Access: 2020-07-24 09:11:26.981894897 +0800
Modify: 2020-07-24 09:11:17.173894341 +0800
Change: 2020-07-24 09:13:03.894900384 +0800
Birth: -
[root@qls ~]# ll /data/
total 0
-rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt
[root@qls ~]# echo "root" > /data/root.log
[root@qls ~]# ll /data/
total 4
-rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt
-rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log
[root@qls ~]# su - ops01
Last login: Fri Jul 24 09:10:22 CST 2020 on pts/0
[ops01@qls ~]$ echo "ops01" > /data/ops01.log
[ops01@qls ~]$ logout
[root@qls ~]# su - ops02
Last login: Fri Jul 24 09:10:52 CST 2020 on pts/0
[ops02@qls ~]$ echo "ops02" > /data/ops02.log
[ops02@qls ~]$ ll /data/
total 12
-rw-rw-r-- 1 ops01 ops_group 6 Jul 24 09:15 ops01.log
-rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt
-rw-rw-r-- 1 ops02 ops_group 6 Jul 24 09:15 ops02.log
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt
-rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log
[ops02@qls ~]$ vim /data/ops01.log
[ops02@qls ~]$ cat /data/ops01.log
ops01
ops02
[ops02@qls ~]$ umask
0002
[ops02@qls ~]$ ll /data/
total 12
-rw-rw-r-- 1 ops01 ops_group 12 Jul 24 09:16 ops01.log
-rw-rw-r-- 1 ops01 ops01 0 Jul 24 09:10 ops01.txt
-rw-rw-r-- 1 ops02 ops_group 6 Jul 24 09:15 ops02.log
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt
-rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log
[ops02@qls ~]$ rm -f /data/ops01.txt
[ops02@qls ~]$ ll -d /data/
drwxrws--- 2 root ops_group 90 Jul 24 09:20 /data/
[ops02@qls ~]$ mkdir /data/test
[ops02@qls ~]$ ll /data/test
total 0
[ops02@qls ~]$ ll /data/
total 12
-rw-rw-r-- 1 ops01 ops_group 12 Jul 24 09:16 ops01.log
-rw-rw-r-- 1 ops02 ops_group 6 Jul 24 09:15 ops02.log
-rw-rw-r-- 1 ops02 ops02 0 Jul 24 09:11 ops02.txt
-rw-rw-r-- 1 ops03 ops03 0 Jul 24 09:11 ops03.txt
-rw-r--r-- 1 root ops_group 5 Jul 24 09:14 root.log
drwxrwsr-x 2 ops02 ops_group 6 Jul 24 09:21 test
3. sticky 粘滞位
权限作用于 others 的x为 使用 t 表示 小t 大T
给目录设置, 一个目录所有用户都拥有管理的权限 777 针对此目录设置一个粘滞位
所有用户都可以在这个目录进行创建、删除文件的权利 但是只能管理自己的文件 或者管理员拥有管理所有文件的权限 root
[root@qls ~]# ll -d /tmp/
drwxrwxrwt. 15 root root 4096 Jul 24 09:02 /tmp/
[root@qls ~]# stat /tmp/
File: ‘/tmp/’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 803h/2051d Inode: 67108936 Links: 15
Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-07-24 09:49:40.026024737 +0800
Modify: 2020-07-24 09:02:34.229864730 +0800
Change: 2020-07-24 09:02:34.229864730 +0800
Birth: -
[root@qls ~]# mkdir /test
[root@qls ~]# chmod 777 /test
[root@qls ~]# ll -d /test
drwxrwxrwx 2 root root 6 Jul 24 09:50 /test
[root@qls ~]# echo "root" /test/root.txt
root /test/root.txt
[root@qls ~]# echo "root" > /test/root.txt
[root@qls ~]# su - ops01
Last login: Fri Jul 24 09:14:53 CST 2020 on pts/0
[ops01@qls ~]$ echo "ops01" > /test/ops01.txt
[ops01@qls ~]$ logout
[root@qls ~]# su - dev01
Last login: Wed Jul 22 10:49:56 CST 2020 from 10.0.0.1 on pts/1
[dev01@qls ~]$ echo "dev01" > /test/dev01.txt
[dev01@qls ~]$ logout
[root@qls ~]# ll /test/
total 12
-rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt
-rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:51 ops01.txt
-rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt
[root@qls ~]# su - dev01
Last login: Fri Jul 24 09:51:51 CST 2020 on pts/0
[dev01@qls ~]$ ll /test/
total 12
-rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt
-rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:51 ops01.txt
-rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt
[dev01@qls ~]$ rm -f /test/ops01.txt
[dev01@qls ~]$ ll /test/
total 8
-rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt
-rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt
[root@qls ~]# chmod o+t /test/
[root@qls ~]# stat /test/
File: ‘/test/’
Size: 39 Blocks: 0 IO Block: 4096 directory
Device: 803h/2051d Inode: 67588796 Links: 2
Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-07-24 09:53:31.177037826 +0800
Modify: 2020-07-24 09:53:25.993037532 +0800
Change: 2020-07-24 09:55:02.412042992 +0800
Birth: -
[root@qls ~]# su - ops01
Last login: Fri Jul 24 09:51:32 CST 2020 on pts/0
[ops01@qls ~]$ echo "ops01" > /test/ops.txt
[ops01@qls ~]$ ll /test/
total 12
-rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt
-rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:55 ops.txt
-rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt
[root@qls ~]# su - dev01
Last login: Fri Jul 24 09:52:59 CST 2020 on pts/0
[dev01@qls ~]$ ll /test/
total 12
-rw-rw-r-- 1 dev01 dev01 6 Jul 24 09:52 dev01.txt
-rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:55 ops.txt
-rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt
[dev01@qls ~]$ rm -f /test/ops.txt
rm: cannot remove ‘/test/ops.txt’: Operation not permitted
[dev01@qls ~]$ rm -f /test/dev01.txt
[dev01@qls ~]$ ll /test/
total 8
-rw-rw-r-- 1 ops01 ops01 6 Jul 24 09:55 ops.txt
-rw-r--r-- 1 root root 5 Jul 24 09:51 root.txt
[dev01@qls ~]$ logout
[root@qls ~]# rm -f /test/ops.txt
[root@qls ~]#
1|02. 特殊属性
特殊属性不受普通权限的限制
lsattr #显示特殊属性
chattr #设置特殊属性
a #这个文件只能追加内容和查看 别的什么都做不了 删除 移动 复制可以
i #什么都操作不了
[root@qls ~]# touch test.txt
[root@qls ~]# touch test.log
[root@qls ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 24 10:08 test.log
-rw-r--r-- 1 root root 0 Jul 24 10:08 test.txt
[root@qls ~]# lsattr test.log
---------------- test.log
[root@qls ~]# lsattr test.txt
---------------- test.txt
[root@qls ~]# echo "test" > test.log
[root@qls ~]# echo "test" > test.txt
[root@qls ~]# ll
total 8
-rw-r--r-- 1 root root 5 Jul 24 10:09 test.log
-rw-r--r-- 1 root root 5 Jul 24 10:09 test.txt
[root@hzl ~]# chattr +a test.log
[root@hzl ~]# ll test.log
-rw-r--r-- 1 root root 5 Jul 24 10:09 test.log
[root@hzl ~]# lsattr test.log
-----a---------- test.log
[root@hzl ~]# cat test.log
test
[root@hzl ~]# vim test.log
[root@hzl ~]# vim test.log
[root@hzl ~]]# echo "hello" > test.log
-bash: test.log: Operation not permitted
[root@hzl ~]# echo "hello" >> test.log
[root@hzl ~]# cat test.log
test
hello
[root@hzl ~]# rm -f test.log
rm: cannot remove ‘test.log’: Operation not permitted
[root@hzl ~]# mv test.log /tmp/
mv: cannot move ‘test.log’ to ‘/tmp/test.log’: Operation not permitted
[root@hzl ~]# cp test.log /tmp/
[root@hzl ~]# chattr +i test.txt
[root@hzl ~]# lsattr test.txt
----i----------- test.txt
[root@hzl ~]# cat test.txt
test
[root@hzl ~]# rm -f test.txt
rm: cannot remove ‘test.txt’: Operation not permitted
[root@hzl ~]# mv test.txt /tmp/
mv: cannot move ‘test.txt’ to ‘/tmp/test.txt’: Operation not permitted
[root@hzl ~]# cp test.txt /tmp/
[root@hzl ~]# echo "hello" > test.txt
-bash: test.txt: Permission denied
[root@hzl ~]# echo "hello" >> test.txt
-bash: test.txt: Permission denied
1|03. 重定向的简单概述
将原本要输出到屏幕上面的内容指定到一个文件中 将原本来从键盘上面输入的内容,改为从命令或者文件当中读取的方式 输出即把相关对象通过输出设备显示出来,输出⼜分正确输出和错误输出两种 ⼀般情况下都是标准输出 标准输出设备为显示器,标准输⼊设备为键盘。 linux中⽤状态表示 0 代表标准输⼊ 1 代表标准正确输出 2 代表标准错误输出。
输出重定向的简介: 正常输出是把内容输出到显示器上,⽽输出重定向是把内容输出到⽂件中 > : 代表覆盖 >> : 代表追加 Ps:标准输出的1可以省略
例:
# ifconfig > test.log
即把ifconfig执⾏后显示的正确内容,全部追加到test.log.,输出页面不显示任何输出结果
#注:错误输出重定向>与>>后边不要加空格
#没有改变输⼊的⽅向,默认键盘,此时等待输⼊
[root@egon ~]# tr 'N' 'n'
No
no
[root@egon ~]# tr 'N' 'n' < file.txt
#没有改变输⼊的⽅向,默认键盘,此时等待输⼊
[root@egon ~]# grep 'root'
oldboy
root
root
[root@hzl ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
# 读写块设备
[root@hzl ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20
[root@hzl ~]# dd </dev/zero >/file2.txt bs=1M count=20
# mysql如何恢复备份,了解即可,不⽤关注。
[root@hzl ~]# mysql -uroot -p123 < bbs.sq
为什么需要使用重定向 :
1. 定时任务的执行结果 数据重要需要保存
2. 后台程序的结果输出重定向到一个文件中
3. 一些数据非常重要,需要保存
4. 一些错误需要检查,即输出定向到空文件保存
5. 正确和错误的信息都需要保存起来,指定输出到文件中
stdin #标准输入 0 #从键盘上面读取需要输入的内容,或者从命令及文件中读取需要输入的内容
stdout #标准输出 1 #默认情况是将正确的信息标准输出到屏幕上方
stderr #错误输出 2 #默认情况是将错误的信息标准输出到屏幕上方
[root@hzl ~]# cat 1.txt
hello #标准输出
[root@hzl ~]# tail -f passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
^Z
[1]+ Stopped tail -f passwd
[root@hzl ~]# ps
PID TTY TIME CMD
10861 pts/0 00:00:00 bash
10879 pts/0 00:00:00 bash
10980 pts/0 00:00:00 su
10981 pts/0 00:00:00 bash
11039 pts/0 00:00:00 su
11040 pts/0 00:00:00 bash
14928 pts/0 00:00:00 tail
14930 pts/0 00:00:00 ps
[root@hzl ~]# ll /proc/14928/fd
total 0
lrwx------ 1 root root 64 Jul 24 10:51 0 -> /dev/pts/0
lrwx------ 1 root root 64 Jul 24 10:51 1 -> /dev/pts/0
lrwx------ 1 root root 64 Jul 24 10:51 2 -> /dev/pts/0
lr-x------ 1 root root 64 Jul 24 10:51 3 -> /root/passwd
lr-x------ 1 root root 64 Jul 24 10:51 4 -> anon_inode:inotify
[root@hzl ~]# jobs
[1]+ Stopped tail -f passwd
[root@hzl ~]# fg %1
tail -f passwd
```
1|04. 输出重定向详解
#echo就是将需要输出到屏幕上的内容输出到指定的文件中
表示符号详解:
> #标准覆盖,全部覆盖 #将输出的正确的内容输出到文件中,需要输出的文件不存在时,会自动创建新的文件
>> #标准追加,将正确输出全部追加到文件中 #将正确的内容追加到指定文件的尾部,当文件不存在时,会自动创建文件
2> #标准覆盖-表示将错误的输出覆盖到指定的文件中 #将错误的信息覆盖到指定的文件中
2>> #标准追加-表示件错误的输出全部追加到指定的文件中 #将错误的信息追加到文件的尾部
例:
#将输出内容全部覆盖到文件中
[root@hzl ~]# echo "hello" > 123.txt
#将命令输出的内容全部覆盖到指定的文件中
[root@hzl ~]# ip a s eth0 > ip.txt
[root@hzl ~]# cat ip.txt
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#合并文件,将多个命令输出结果全部覆盖到指定的文件中
[root@hzl ~]# cat /etc/hosts /etc/resolv.conf > new.txt
[root@hzl ~]# cat new.txt
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# Generated by NetworkManager
nameserver 223.5.5.5
[root@hzl ~]# cat 123.txt
#标准追加,将输出的内容追加到指定的文件中
hello
[root@hzl ~]# echo "test" >> 123.txt
[root@hzl ~]# cat 123.txt
hello
test
#标准错误输出,将命令执行的错误输出覆盖到指定的文件中
[root@hzl ~]# ls /roott
ls: cannot access /roott: No such file or directory
[root@hzl ~]# ls /roott 2>err.txt
[root@hzl ~]# cat err.txt
ls: cannot access /roott: No such file or directory
[root@hzl ~]# ls /roott 2>>err.txt
[root@hzl ~]# ls /roott 2>>err.txt
[root@hzl ~]# ls /roott 2>>err.txt
[root@hzl ~]# cat err.txt
ls: cannot access /roott: No such file or directory
ls: cannot access /roott: No such file or directory
ls: cannot access /roott: No such file or directory
ls: cannot access /roott: No such file or directory
#把正确的和错误的信息都保存到一个文件中,
[root@hzl ~]# ls /roott > file.txt 2>&1 #不推荐
[root@hzl ~]# cat file.txt
ls: cannot access /roott: No such file or directory
[root@hzl ~]# mkdir /roott
[root@hzl ~]# ls /roott > file.txt 2>&1
[root@hzl ~]# cat file.txt
[root@hzl ~]# ls /roott &>file.txt
[root@hzl ~]# ls /roottt &>file.txt
[root@hzl ~]# cat file.txt
ls: cannot access /roottt: No such file or directory
#将正确的信息和错误的信息重定向到空
[root@hzl ~]# ls /roottt &> /dev/null
#把正确的和错误的放在不同的文件中
[root@qls ~]# ls /rooot >> file1.txt 2>> file2.txt
[root@qls ~]# ll
total 4
-rw-r--r-- 1 root root 0 Jul 24 11:14 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
[root@qls ~]# cat file1.txt
[root@qls ~]# cat file2.txt
ls: cannot access /rooot: No such file or directory
[root@qls ~]# ls /root >> file1.txt 2>> file2.txt
[root@qls ~]# ll
total 8
-rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
[root@qls ~]# cat file1.txt
file1.txt
file2.txt
[root@qls ~]# cat file2.txt
ls: cannot access /rooot: No such file or directory
1|05. 输入重定向详解
将原本从键盘上面输入的内容,改为从命令或者文件当中读取
符号
< #标准输入 #将原本从默认的键盘中读取数据改为由命令或者文件中读取
<< #标识符限定输入重定向 #从键盘中读取内容,直到遇到标识符的分解符为止
例:
[root@qls ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# grep 'root' < /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# grep root
oldboy
root
root
rottttroot
rottttroot
[root@qls ~]# dd if=/dev/zero of=/root/test.log bs=1M count=50
50+0 records in
50+0 records out
52428800 bytes (52 MB) copied, 0.0362247 s, 1.4 GB/s
[root@qls ~]# ll
total 51208
-rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
-rw-r--r-- 1 root root 52428800 Jul 24 11:45 test.log
[root@qls ~]# ll -h
total 51M
-rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
-rw-r--r-- 1 root root 50M Jul 24 11:45 test.log
[root@qls ~]# dd </dev/zero >/root/oldboy.log bs=10M count=100
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB) copied, 21.3836 s, 49.0 MB/s
[root@qls ~]# ll -h
total 1.1G
-rw-r--r-- 1 root root 20 Jul 24 11:15 file1.txt
-rw-r--r-- 1 root root 52 Jul 24 11:14 file2.txt
-rw-r--r-- 1 root root 1000M Jul 24 11:48 oldboy.log
-rw-r--r-- 1 root root 50M Jul 24 11:45 test.log
[root@qls ~]# cat file1.txt
file1.txt
file2.txt
[root@qls ~]# tr 't' 'T' < file1.txt
file1.TxT
file2.TxT
数据库导入表
[root@qls ~]# mysql -uroot -p123 < all.sql
[root@qls ~]# cat >>file3.txt
123
345
678
456
EOF
123
^C
[root@qls ~]# cat file3.txt
123
345
678
456
EOF
123
[root@qls ~]# cat >>file4.txt<<EOF
> 123
> dfer
> rghrt
> gergrtg
> EOF
[root@qls ~]# cat file4.txt
123
dfer
rghrt
gergrtg
[root@qls ~]# cat 123.sh
cat<<EOF
1. rge
2. fgo
3. jfrio
4. hfoih
EOF
[root@qls ~]# sh 123.sh
1. rge
2. fgo
3. jfrio
4. hfoih
1|06. 管道技术
| 表示管道
连接左右两个命令的使用 把前面的命令作为标准输出通过管道交给后面的命令 作为标准输入
只能把正确的信息交给后面的命令,错误的信息的不会传递
[root@qls ~]# head passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# head passwd | cat -n
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet)
RX packets 99054 bytes 61039138 (58.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 55989 bytes 5452694 (5.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@qls ~]# ifconfig eth0 | awk 'NR==2'
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
[root@qls ~]# ifconfig eth0 | awk 'NR==2' | awk '{print $2}'
10.0.0.100
[root@qls ~]# awk -F: '{print $3}' passwd | sort -rn | head
1046
1045
1044
1043
1042
1041
1040
1039
1038
1037
#管道技术中的tee技术
需要保存一些标准输出的内容
[root@qls ~]# echo $RANDOM | md5sum | cut -c 1-8 | tee pass.txt |passwd --stdin qls01
Changing password for user qls01.
passwd: all authentication tokens updated successfully.
[root@qls ~]# cat pass.txt
100ada15
-a #追加
tee 和 重定向的区别
重定向 #把要输出到屏幕上面的内容重定向到指定的文件中
tee #把要输出到屏幕上面的内容重定向到指定的文件中 ,并且可以继续输出一份到屏幕上面
#管道中xargs技术
让一些不支持管道的命令支持管道 把前面命令的执行结果以文件的参数传递方式传递给后面的命令
[root@qls ~]# ls | xargs sed -i 's#root#oldboy#g'
[root@qls ~]# find /var/log/ -type f -name "*.log" |xargs cp -t /opt
[root@qls ~]# find /var/log/ -type f -name "*.log" |xargs -I {} cp {} /mnt/
__EOF__
本文作者:ଲ小何才露煎煎饺
本文链接:https://www.cnblogs.com/zeny/p/15121627.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/zeny/p/15121627.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文来自博客园,作者:ଲ小何才露煎煎饺,转载请注明原文链接:https://www.cnblogs.com/zeny/p/15121627.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报