我在理解 Linux 命令的含义时遇到了麻烦du -sk * |sort -rn|head
。我知道 du 用于显示磁盘使用情况,但我在理解命令的其余部分时遇到了麻烦。有人可以分解这里到底发生了什么吗?还可以建议一些好的资源来详细研究复杂的linux命令吗?
问问题
35 次
1 回答
1
学习 Unix 命令的好资源是手册页。
man du
:
Summarize disk usage of the set of FILEs, recursively for directories. -B, --block-size=SIZE scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below -k like --block-size=1K -s, --summarize display only a total for each argument
man sort
:
Write sorted concatenation of all FILE(s) to standard output. -r, --reverse reverse the result of comparisons -n, --numeric-sort compare according to string numerical value
man head
:
Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name.
该命令显示 10 个最大的目录或文件。
一步步:
#! /bin/bash
echo 'Create 20 files with random size.'
for i in {1..20}; do
dd if=/dev/zero of="$i".data bs=1k count="$RANDOM"
done
echo 'Show the size of all 20 files in default units.'
du -s *.data
echo 'Show the size of all 20 files in KB.'
du -sk *.data
echo 'Show the size of all 20 files in KB and sort it in numeric order.'
du -sk *.data | sort -n
echo 'Show the size of all 20 files in KB and sort it in reverse numeric order.'
du -sk *.data | sort -rn
echo 'Show just the first 10 files of the previous output.'
du -sk *.data | sort -rn | head
于 2022-01-04T11:15:04.377 回答