sed 删除以 'du` 开头的行

sed 删除以 'du` 开头的行

我打算检查磁盘空间du -s

[root@iz2ze9wve43n2nyuvmsfx5z /]# du -s /* | sort -k 1|sed "/du/d"
du: cannot access ‘/proc/31265/task/31265/fd/4’: No such file or directory
du: cannot access ‘/proc/31265/task/31265/fdinfo/4’: No such file or directory
du: cannot access ‘/proc/31265/fd/4’: No such file or directory
du: cannot access ‘/proc/31265/fdinfo/4’: No such file or directory
0   /bin
0   /dev
0   /lib
0   /lib64
0   /proc
0   /sbin
0   /sys
158784  /boot
16  /lost+found
2458148 /usr
37500   /etc
496 /run
4   /home
4   /media
4   /mnt
4   /opt
4   /srv
534680  /root
568 /tmp
935544  /var

所以我开始删除包含的行du

1、sed“/du/d”

[root@iz2ze9wve43n2nyuvmsfx5z /]# du -s /* | sort |sed "/du/d" | head -n 6
du: cannot access ‘/proc/31563/task/31563/fd/4’: No such file or directory
du: cannot access ‘/proc/31563/task/31563/fdinfo/4’: No such file or directory
du: cannot access ‘/proc/31563/fd/4’: No such file or directory
du: cannot access ‘/proc/31563/fdinfo/4’: No such file or directory
0   /bin
0   /dev
0   /lib
0   /lib64
0   /proc
0   /sbin

2、删除不以数字开头的行sed "/[^\d]*/d"

    [root@iz2ze9wve43n2nyuvmsfx5z /]# du -s /* | sort -k 1|sed "/[^\d]*/d" | head -n 6
    du: cannot access ‘/proc/31612/task/31612/fd/4’: No such file or directory
    du: cannot access ‘/proc/31612/task/31612/fdinfo/4’: No such file or directory
    du: cannot access ‘/proc/31612/fd/4’: No such file or directory
    du: cannot access ‘/proc/31612/fdinfo/4’: No such file or directory

but return the lines I want to delete

3、sed "/\d*/d"

[root@iz2ze9wve43n2nyuvmsfx5z /]# du -s /* | sort -k 1|sed "/\d*/d" | head -n 6
du: cannot access ‘/proc/31703/task/31703/fd/4’: No such file or directory
du: cannot access ‘/proc/31703/task/31703/fdinfo/4’: No such file or directory
du: cannot access ‘/proc/31703/fd/4’: No such file or directory
du: cannot access ‘/proc/31703/fdinfo/4’: No such file or directory

如何删除包含“du”的行?

答案1

du: cannot access ...正如 Steeldriver 评论的那样,以 开头和结尾的行:No such file or directory是来自 du 的错误消息;它枚举了 /proc 中的这些目录,但相应的进程在对du它们的“磁盘”空间求和之前就已经退出,当它尝试访问那些现在不存在的目录时会导致错误。

值得庆幸的是,du将这些错误发送到 stderr,这样您就可以拦截它们并使用以下命令删除它们:

du -s /* 2>/dev/null | sort -n

我还更改了您的sort命令,假设您想按数字排序,最大尺寸在最后。

相关内容