文件管理之文件查找,上传下载,输出重定向

一. 查找命令 find 

1.按文件名查找: -name

[root@localhost ~]# find /etc -name "ifcfg-eth0"
[root@localhost ~]# find /etc -iname "ifcfg-eth0" # -i忽略大小写
[root@localhost ~]# find /etc -iname "ifcfg-eth

2. 按文件大小: -size
[root@localhost ~]# find /etc -size +3M # 大于3M
[root@localhost ~]# find /etc -size 3M   等于3M
[root@localhost ~]# find /etc -size -3M  小于3M
[root@localhost ~]# find /etc -size +3M -ls # -ls 找到后的处理动作

3.指定查找目录的深度: -maxdepth
[root@localhost ~]# find / -maxdepth 5 -a -name "ifcfg-eth0"   -a表示并且(不写默认就是-a)  -o表示或者

4.按时间查找: -atime(访问时间) -mtime(修改时间) -ctime(改变时间) 
[root@localhost ~]# find /etc -mtime +3   修改时间超过3天
[root@localhost ~]# find /etc -mtime 3     修改时间等于3天
[root@localhost ~]# find /etc -mtime -3    修改时间小于3天

5.按文件属主,属组查找: -user -group
[root@localhost ~]# find /home -user egon         属主是egon
[root@localhost ~]# find /home -group it                属组是it
[root@localhost ~]# find /home -user egon -group it        属主是egon,属组是it
[root@localhost ~]# find /home -user egon -a -group it    同上
[root@localhost ~]# find /home -user egon -o -group it    属主是egon或者属组是it
[root@localhost ~]# find /home -nouser                            没有属主的文件
[root@localhost ~]# find /home -nogroup                          没有属组的文件
[root@localhost ~]# find /home -nouser -o -nogroup         没有属主或者没有属组的文件

6.按文件类型:
[root@localhost ~]# find /dev -type f #普通文件
[root@localhost ~]# find /dev -type d # 目录
[root@localhost ~]# find /dev -type l # 链接文件
[root@localhost ~]# find /dev -type b # 块设备文件
[root@localhost ~]# find /dev -type c # 字符设备文件
[root@localhost ~]# find /dev -type s # 套接字文件
[root@localhost ~]# find /dev -type p# 管道文件

7.根据inode号查找: -inum
[root@localhost ~]# find / -inum 1811  查找iNode号是1811的文件

8.按文件权限查找: -perm
[root@localhost ~]# find . -perm 644 -ls
[root@localhost ~]# find . -perm -644 -ls
[root@localhost ~]# find . -perm -600 -ls
[root@localhost ~]# find /sbin -perm -4000 -ls 
[root@localhost ~]# find /sbin -perm -2000 -ls 
[root@localhost ~]# find /sbin -perm -1000 -ls

9.找到后的处理动作:
-print     打印输出(默认可以不加)
-ls         查看文件信息
-delete  删除
-exec    非交互
-ok        交互
[root@localhost ~]# find /etc -name "ifcfg*" -print 
[root@localhost ~]# find /etc -name "ifcfg*" -ls
[root@localhost ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp ;   非交互复制
[root@localhost ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp ;       交互复制
[root@localhost ~]# find /etc -name "ifcfg*" -exec rm -rf {} ;             非交互删除
[root@localhost ~]# find /etc -name "ifcfg*" -delete                           删除

find结合xargs:
[root@localhost ~]# find . -name "egon*.txt" |xargs rm -rf
[root@localhost ~]# find /etc -name "ifcfg-eth0" |xargs -I(大写) {} cp -rf {} /var/tmp
[root@localhost ~]# find /test -name "ifcfg-ens33" |xargs -I {} mv {} /ttt
[root@localhost ~]# find /ttt/ -name "ifcfg*" |xargs -I {} chmod 666 {}


二. 文件管理之上传下载

wget主要用于远程下载
wget -O(大写) 指定本地路径  链接地址
ps:如果wget 下载提示无法建立SSL连接,加上选项
--no-check-certificate
wget --no-check-certificate -O  本地路径  远程连接地址

curl主要用于测试
[root@localhost ~]# curl  -o(小写)  文件名(可以自定义修改)   远程连接地址
下载提示无法建立SSL连接,加上选项-k
curl -k -o 123.png https://www.xxx.com/img/hello.png

sz :下载,将服务器上选定的文件下载到本机.
rz  :上传,运行该命令会弹出一个文件窗口,从本地选择文件上传到服务器.


三. 输出与重定向

输出即把相关对象通过输出设备显示出来, 输出又分正确输出和错误输出,一般情况下,输出设备为显示器,标准输入设备为键盘.
Linux中
0代表标准输入
1代表标准正确输出
2代表标准错误输出

1. 输出重定向分类
重定向的特性:覆盖文件
1>(>):标准输出重定向,将命令执行的正确结果输出到指定的文件或者设备中
2>:错误输出重定向
 
追加重定向:不会覆盖文件
1>>(>>):标准输出追加重定向,将命令执行的正确结果追加输出到文件末尾
2>>:错输出追加重定向,将命令执行的错误结果追加输出到文件末尾
<<:标准输入重定向:将键盘敲的内容,输入到命令或者文件中
 
 
2.输出重定向的使用:
#将正确内容重定向到文件中,错误会输出到屏幕(会覆盖源文件)
ifconfig eth0 > a.txt
 
#将正确的内容追加到文件中,错误会输出到屏幕(不会覆盖源文件)
echo "This is network conf" >> a.txt
 
#将错误的内容输出到文件中,正确的会输出到屏幕(会覆盖源文件)
find /etc -name "*.conf" 2>b.txt
 
#合并输出,错误的正确的内容都会输出到一个文件(会覆盖源文件)
find /etc -name "*.conf" >c.txt  2>&1
find /etc -name "*.conf" >c.txt 2>c.txt
find /etc -name "*.conf" &>c.txt
 
#将错误输出重定向到 ‘黑洞’,正确内容输出到屏幕
ls /root/ /err 2>/dev/null
 
#将错误输出重定向到 ‘黑洞’,正确内容输出到1.txt文件中
ls /root/ /err >1.txt 2>/dev/null
 
原文地址:https://www.cnblogs.com/allenzhu128/p/13881524.html