Linux基础——文件或目录或内容查找方法(awk/sort/ls/find)
一、awk升序降序使用
二、sort升序降序使用
问题:主机/root目录被文件占满,导致系统空间不足触发告警
# 检查/root目录的目录及文件大小,sort按照递归方法降序打印前10条记录(显示当前目录及文件大小)
du -sh ./*|sort -h -r | head -10

# 检查/root目录、子目录(3层)、文件大小,按照sort递归降序打印前10行
du --max-depth=3 -h /root/|sort -h -r | head -10

三、ls查找最新文件或目录
1、参数“-t”参数newest first查看最新创建的目录或文件
[root@harbor strace]# ls --help | grep time -c with -lt: sort by, and show, ctime (time of last with -l: show ctime and sort by name; otherwise: sort by ctime, newest first --full-time like -l --time-style=full-iso time (-t), version (-v), extension (-X) --time=WORD change the default of using modification times; access time (-u): atime, access, use; change time (-c): ctime, status; birth time: birth, creation; with -l, WORD determines which time to show; with --sort=time, sort by WORD (newest first) --time-style=TIME_STYLE time/date format with -l; see TIME_STYLE below -t sort by time, newest first; see --time -u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time, newest first

[root@harbor ~]# ls -alt | head total 85604 drwxr-xr-x 15 root root 4096 Dec 10 10:42 strace dr-xr-x---. 47 root root 4096 Dec 10 10:42 . -rw------- 1 root root 104 Dec 10 09:41 .Xauthority -rw------- 1 root root 23911 Dec 9 14:50 .bash_history drwx------ 15 root root 4096 Dec 9 14:18 .config dr-xr-xr-x. 22 root root 4096 Dec 9 14:05 .. drwx------ 8 root root 4096 Dec 9 13:58 .cache -rw------- 1 root root 14480 Dec 9 13:58 .viminfo drwxr-xr-x 3 root root 4096 Dec 9 11:16 tiptop

四、find查找文件或目录
1、查看范围1M~50M之间权限是0111的文件
find /root -size +1M -size -50M -perm 0755 | xargs ls -lh

2、查找关键字ssh的路径
find / -name ssh 2>&1 | grep -v "Permission denied"

3、查找目录、清空子cgroups层级数、删除子cgroups
find /sys/fs/cgroup/memory -type d -name "*test*" \ -exec sh -c 'echo 0 > $1/memory.force_empty && rmdir $1' _ {} \;

4、只查找目录所有文件
find /sys/fs/cgroup/memory -type d
5、查看cgroup.procs文件统计数量
find /sys/fs/cgroup -name "cgroup.procs" -exec wc -l {} + | tail -1

稳步前行,只争朝夕。
浙公网安备 33010602011771号