从bash脚本中删除du输出的文件部分
问题描述:
我试图在我的bash脚本中调用du
时删除输出。我只是试图打印出当前目录的大小。所以它看起来像这样:从bash脚本中删除du输出的文件部分
DIRSIZE=$(du -hs $1)
printf "The size of the directory given is: %s\n" "$DIRSIZE"
我所要的输出是这样的:
The size of the directory given is: 32K
但是,我命令目前输出:
The size of the directory given is: 32K /home/dir_listed/
有一种简单的方法来消除目录?
答
随着AWK:
DIRSIZE=$(du -hs $1 | awk '{print $1}')
仅取第一个字段从du
输出并保存到DIRSIZE
。
随着sed的:
DIRSIZE=$(du -hs $1 | sed 's/[[:space:]].*//')
从第一space
删除到行尾的并保存到DIRSIZE
。
随着切:
DIRSIZE=$(du -hs $1 | cut -f 1)
只取第一个字段从du
输出其是标签分隔并保存到DIRSIZE
。
答
试试这个:
DIRSIZE=$(du -hs $1 | awk '{print $1}')
printf "The size of the directory given is: %s\n" "$DIRSIZE"
pipe into'awk'NF - ' – 2015-02-24 08:38:33
或'du -hs“$ 1”| awk'{print“给出的目录大小为:”$ 1}“ – tripleee 2015-02-24 08:40:08