国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > cksum 以获得更好的体验。

# Bash 的 cksum 命令

cksum [OPTION]... [FILE]...

!subtitle:功能

计算和校验文件的校验和。

!subtitle:类型

可执行文件(/usr/bin/cksum),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -a, --algorithm=TYPE - 校验和算法;支持:sysv, crc, md5, sha1, sha224, sha256, sha384, sha512, blake2b, sm3,默认为 crc

    • --base64 - 输出 BASE64 编码而不是十六进制编码

    • -c, --check - 从文件中读取校验和进行校验

    • -l, --length=BITS - 结果保留的长度(二进制位),只能用于 blake2b 不能超过 512 且必须是 8 的倍数

    • --raw - 输出原始数据而不是十六进制编码;通常用于重定向

    • --tag - 生成 BSD 风格的输出(默认)

    • --untagged - 生成反向、无标签(不附带算法类型)风格的输出

    • -z, --zero - 以空字符(\0)作为行的结尾,而不是换行符(\n

    • --ignore-missing - 校验时忽略缺失的文件

    • --quiet - 校验时不打印成功的消息

    • --status - 校验时不打印任何消息;可以通过返回值检查是否成功

    • --strict - 校验时严格检查格式

    • -w, --warn - 校验时遇到格式不正确的行时产生警告

    • --debug - 校验时显示所使用的实现

    • --help - 显示帮助

    • --version - 显示版本

  • FILE - 文件列表

# 示例

!subtitle:计算

$ cksum 1.txt                           # 默认为 CRC
2503682644 3208 1.txt
$ cksum -a sha224 1.txt                 # 指定 SHA224
SHA224 (1.txt) = e97bd3540da0cf24cf74a776a523ed7c89e89d435c9f59070f9dbb64
$ cksum -a sha224 --base64 1.txt        # 输出 BASE64
SHA224 (1.txt) = 6XvTVA2gzyTPdKd2pSPtfInonUNcn1kHD527ZA==
$ cksum -a sha224 --raw 1.txt           # 输出原始数据
 ͤδ§v¥#켉蜃\Y»d
$ cksum -a sha224 --untagged 1.txt      # 输出反向无标签风格,无法用于校验
e97bd3540da0cf24cf74a776a523ed7c89e89d435c9f59070f9dbb64  1.txt

!subtitle:校验

$ cksum -a sha512 1.txt 2.txt 3.txt > sum.txt
$ cksum -c sum.txt                      # 校验
1.txt: OK
2.txt: OK
3.txt: OK

# 相关命令

命令 说明
b2sum 计算和校验 BLAKE2 值
md5sum 计算和校验 MD5 值
sha1sum 计算和校验 SHA1 值
sha224sum 计算和校验 SHA224 值
sha256sum 计算和校验 SHA256 值
sha384sum 计算和校验 SHA384 值
sha512sum 计算和校验 SHA512 值

# 推荐阅读

# 手册

CKSUM(1)                         User Commands                        CKSUM(1)

NAME
       cksum - compute and verify file checksums

SYNOPSIS
       cksum [OPTION]... [FILE]...

DESCRIPTION
       Print or verify checksums.  By default use the 32 bit CRC algorithm.

       With no FILE, or when FILE is -, read standard input.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -a, --algorithm=TYPE
              select the digest type to use.  See DIGEST below.

       --base64
              emit base64-encoded digests, not hexadecimal

       -c, --check
              read checksums from the FILEs and check them

       -l, --length=BITS
              digest length in bits; must not exceed the max  for  the  blake2
              algorithm and must be a multiple of 8

       --raw  emit a raw binary digest, not hexadecimal

       --tag  create a BSD-style checksum (the default)

       --untagged
              create a reversed style checksum, without digest type

       -z, --zero
              end  each  output  line  with NUL, not newline, and disable file
              name escaping

   The following five options are useful only when verifying checksums:
       --ignore-missing
              don't fail or report status for missing files

       --quiet
              don't print OK for each successfully verified file

       --status
              don't output anything, status code shows success

       --strict
              exit non-zero for improperly formatted checksum lines

       -w, --warn
              warn about improperly formatted checksum lines

       --debug
              indicate which implementation used

       --help display this help and exit

       --version
              output version information and exit

   DIGEST determines the digest algorithm and default output format:
       sysv   (equivalent to sum -s)

       bsd    (equivalent to sum -r)

       crc    (equivalent to cksum)

       md5    (equivalent to md5sum)

       sha1   (equivalent to sha1sum)

       sha224 (equivalent to sha224sum)

       sha256 (equivalent to sha256sum)

       sha384 (equivalent to sha384sum)

       sha512 (equivalent to sha512sum)

       blake2b
              (equivalent to b2sum)

       sm3    (only available through cksum)

       When checking, the input should be a former output of this program,  or
       equivalent standalone program.

AUTHOR
       Written by Padraig Brady and Q. Frank Xia.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       Full documentation <https://www.gnu.org/software/coreutils/cksum>
       or available locally via: info '(coreutils) cksum invocation'

GNU coreutils 9.4                 April 2024                          CKSUM(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14