命令行大全

cksum

rose1 发表于 2020-08-25 15:36浏览次数:

在类似Unix的操作系统上,cksum命令计算每个输入文件的循环冗余校验(CRC)和字节数,并将其写入标准输出。 本文档介绍了cksum的GNU / Linux版本。

查看英文版

目录:

1 cksum 运行系统环境

2 cksum 说明

3 cksum 语法

4 cksum 例子

cksum 运行系统环境

Linux

cksum 说明

文件的校验和是一种检查其数据从一个地方传输到另一个地方时是否已损坏的简单方法。如果文件的校验和值在传输前后相同,则不太可能偶然发生任何数据损坏,例如由于信号噪声。

假设您有一个文件myfile.txt,其中包含以下文本:

This is my original file.

您可以使用cksum计算校验和:

cksum myfile.txt

...这将是输出:

4164605383 26 myfile.txt

在这里,4164605383是校验和,26是数据量(以字节为单位)。如果将文件内容更改为此:

This is no longer my original file.

...然后再次运行cksum,您将看到以下内容:

cksum myfile.txt
632554699 36 myfile.txt

校验和有很大的不同,我们还可以看到还有十个字节的数据。

即使字节数与原始字节相同,校验和也将不同:

This is a corrupted file.
cksum myfile.txt
2256884274 26 myfile.txt

...而且即使只有一个字符不同,它也会发生巨大变化:

This is my original file?
cksum myfile.txt
3832066352 26 myfile.txt

重要:简单的校验和(例如由cksum工具生成的校验和)仅对检测意外数据损坏有用。这并不是要防止恶意更改文件。事实证明,攻击者可以精心修改文件,以产生相同的cksum校验和。因此,如果您需要绝对确定文件与原始文件相同,请使用更强大的方法。我们强烈建议使用SHA256 算法来验证数据完整性。您可以使用GNU rhash等工具生成和验证SHA256 哈希和。

The checksum of a file is a simple way to check if its data has become corrupted when being transferred from one place to another. If the checksum value of the file is the same before and after being transferred, it is unlikely that any data corruption has accidentally occurred — from signal noise, for example.

Let's say you have a file, myfile.txt, containing the following text:

This is my original file.

You can calculate the checksum using cksum:

cksum myfile.txt

...and this will be the output:

4164605383 26 myfile.txt

Here, 4164605383 is the checksum, and 26 is the amount of data, in bytes. If you change the contents of the file to this:

This is no longer my original file.

...and run cksum again, you will see the following:

cksum myfile.txt
632554699 36 myfile.txt

The checksum is very different, and we can also see that there are ten more bytes of data.

The checksum will be different even if the number of bytes is same as the original:

This is a corrupted file.
cksum myfile.txt
2256884274 26 myfile.txt

...and it will change dramatically even if only one character is different:

This is my original file?
cksum myfile.txt
3832066352 26 myfile.txt

Important: Simple checksums, such as those produced by the cksum tool, are useful only for detecting accidental data corruption. It's not meant to protect against malicious alteration of a file. It's been proven that an attacker could carefully make changes to a file that would produce an identical cksum checksum. Therefore, if you need to be absolutely certain that a file is identical to the original, use a more powerful method. We highly recommend using the SHA256 algorithm for verifying data integrity. You can generate and verify SHA256 hash sums using tools such as GNU rhash.

查看英文版

查看中文版

cksum 语法

cksum命令的命令语法非常简单。指定一个或多个要检查的文件:

cksum [FILE]...

...或一个选项:

cksum [OPTION]

如果您运行的cksum没有文件名和选项,它将为从标准输入读取的数据创建校验和。

Options

FILE

您要检查的文件名。

--help

显示帮助消息,然后退出。

--version

显示版本信息,然后退出。

The command syntax of the cksum command is very straightforward. Either specify one or more files to be checked:

cksum [FILE]...

...or an option:

cksum [OPTION]

If you run cksum with no file names and no options, it will create a checksum for data read from standard input.

Options

FILE

The name of the file you want to check.

--help

Display a help message, and exit.

--version

Display version information, and exit.

查看英文版

查看中文版

cksum 例子

cksum file.txt

计算file.txt的校验和和字节数,并输出值和文件名。输出将类似于以下内容:

1740057581 19 file.txt

在这里,1740057581是校验和,19是文件中的字节数,file.txt是文件名。

cksum myfile.txt myfile2.txt

上面的命令将为文件myfile.txtmyfile2.txt生成校验和和字节数。输出将类似于以下内容:

3832066352 26 myfile.txt
3722946153 34 myfile2.txt
cksum < myfile.txt

上面的命令会将myfile.txt的内容重定向到cksum,它将从标准输入读取数据并输出校验和和字节数。

cat myfile.txt | cksum

上述命令将猫的内容的myfile.txt和管输出到校验和,其从标准输入读取它。

cksum

运行不带任何选项的cksum将允许您键入所需的任何内容,然后按Enter键以换行。输入完文本后,可以按Ctrl-D表示标准输入已结束,并且cksum将输出您输入的文本的校验和和字节数。

cksum file.txt

Calculate the checksum and bytecount of file.txt and output the values along with the file name. Output will be similar to the following:

1740057581 19 file.txt

Here, 1740057581 is the checksum, 19 is the number of bytes in the file, and file.txt is the file name.

cksum myfile.txt myfile2.txt

The above command will generate checksums and bytecounts for the files myfile.txt and myfile2.txt. Output will resemble the following:

3832066352 26 myfile.txt
3722946153 34 myfile2.txt
cksum < myfile.txt

The above command will redirect the contents of myfile.txt to cksum, which will read the data from standard input and output a checksum and bytecount.

cat myfile.txt | cksum

The above command will cat the contents of myfile.txt and pipe the output to cksum, which reads it from the standard input.

cksum

Running cksum with no options will allow you to type anything you like, pressing enter for new lines. When you are finished entering text, you can press Ctrl-D to signal the end of standard input, and cksum will output the checksum and bytecount of the text that you entered.

查看英文版

查看中文版