文件管理
1、查看当前目录下的文件大小
root:/opt # du -sh * | sort -n #查看当前目录下的所有文件大小并从小到大排序
root:/ # df -lh #查看server分区情况,包括当前系统分区(包括交换分区swap)的”文件系统、容量、已用 、可用、已用% 、挂载点“等实时装况
root:/opt # du -sh /etc/ #查看/etc目录大小(h参数将文件大小人性化显示,s表示size)
root:/opt # du -ah /etc/ #显示/etc目录下面所有文件的大小
2、创建文件
root:~/test # touch {1..100}.txt #创建从1.txt 到 100.txt文件
root:~/test # touch {a..f}.txt #创建从a.txt到f.txt文件
root:~/test # touch {a,c,c}.txt #创建a.txt b.txt c.txt 文件
使用date格式化时间:(date + <> 以不同的格式输出当前时间,各种格式之间可以拼接)
root:~/test/caiwu # date +%F
2019-02-24
root:~/test/caiwu # date +%T
17:09:54
root:~/test/caiwu # date +%F-%T
2019-02-24-17:12:18
root:~/test # date +%Y%m%d
20190224
命令引用:
方法一:$(command)
node01:~/test # touch $(date +%F).txt
方法二:·(command)·
node01:~/test # touch `date +%F`.txt
根据时间来创建文件
node01:~/test # touch $(date +%Y%m%d%H%M%S).TXT
. .. 20190224172724.TXT
3、复制文件
node01:~/test # cp 2019-02-24.txt ../ #复制2019-02-04.txt文件到上级目录
4、删除文件
rm [选项] <参数> 文件名称
rm -rf 文件名称 #递归且不提示删除文件
rm -rf * #递归且不提示删除当前文件夹下所有文件(谨慎使用)
node01:~ # rm -rf /test/* 删除test目录下的所有文件
5、统计文件行数(wc)
node01:~ # wc /etc/passwd
32 77 1783 /etc/passwd
#统计一个文件 “37”表示文件有37行,“77”表示文件有77个单词,“1783”表示有1783个文字
node01:~ # wc -l /etc/passwd # 统计文件函数
32 /etc/passwd
6、查看文件内容(仅支持查看文本文件)
[root@node01:~]$ cat /etc/passwd #查看文件etc/passwd文件全部内容
[root@node01:~]$ cat -n /etc/passwd #以显示行号的方式查看文件
[root@node01:etc]$ less /etc/passwd #分页显示文件内容,有上下移动功能
[root@node01:etc]$ more /etc/passwd #分页显示文件内容,不能上下移动
[root@node01:etc]$ head <-10> /etc/passwd #显示文件前几行,默认前10行,-n指定行数。
[root@node01:etc]$ tail <-10> /etc/passwd #显示文件后几行,默认后10行,-n指定行数。
7、管道符(|)的使用
管道符用于连接多个命令,前一个命令的结果做为后一个命令的输入。
[root@node01:~]$ head -5 /etc/passwd | tail -1 #查看/etc/passed 文件的前五行的最后一行
mail:x:8:12:Mailer daemon:/var/spool/clientmqueue:/bin/false。
[root@node01:~]$ ls -lhS /etc/ | head -5 #查看/etc/目录下按大小排列前五位的文件
[root@node01:~]$ ifconfig | head -5 #查看网络配置输出的前五行内容
管道符命令可以多层嵌套连接。
8、查找文件或目录(find)
按文件名称查找
[root@node01:~]$ find /etc/ -name "*.conf" #查找etc/目录下所有以.conf结尾的文件,通配符要用“”包起来
按文件大小查找
[root@node01:~]$ find /etc/ -size +1M #查找/etc/目录下文件大小大于1M的文件
[root@node01:~]$ find /etc/ -size 1M #查找/etc/目录下文件大小等于1M的文件
[root@node01:~]$ find /etc/ -size -1M # 查找/etc/目录下文件大小小于1M的文件
按文件修改时间查找
[root@node01:~]$ find / -mtime -7 #查找7天内修改的文件
[root@node01:~]$ find / -mtime +7 #查找7天前修改的文件
[root@node01:~]$ find / -ctime -2 #查找2天内创建的文件
[root@node01:~]$ find / -atime -2 #查找2天内活跃的文件
按文件类型查找
[root@node01:~]$ find /dev/ -type b #查找/dev目录下类型为块文件
[root@node01:~]$ find /dev/ -type l #查找/dev目录下类型为软连接的文件,f为普通文件,d为目录文件
复合条件查找
[root@node01:~]$ find / -mtime +7 -a -size -10M #查找修改时间大于七天且文件大小小于10M的文件。-a表示and的意思,是与的关系。
find其他用法
[root@node01:test]$ find -name "*.txt" -exec rm -rf {} \; #将当前文件下所有.txt文件都删除
[root@node01:test]$find -name "*.jpg" -exec cp {} /tmp \; #将当前文件下所有.jpg文件都复制到/tmp目录
说明:find -name "*.txt" -exec rm -rf {} \; 命令中find -name "*.txt"查找到所有.txt文件,-exec表示要对这些文件执行某些操作,rm -rf表示执行删除操作,{}表示要删除的对象,将填充find找到的内容,\;是与-exec组成的固定格式,即-exec [opetion] \;的基本格式。