在处理之前按字母顺序对文件进行排序


12

我使用命令

find . -type f -exec sha256sum {} \; > sha256SumOutput

散列文件夹层次结构中的每个文件。不幸的是,sha256sum没有从find字母顺序中获取文件名。如何解决?

我希望在对它们进行哈希处理之前对它们进行排序,以便按字母顺序对它们进行哈希处理(这是有原因的)。


查找文件,管道以sort对列表进行排序以及管道以sha256sum
Sergiy Kolodyazhnyy 2015年

字母数字排序。
UTF-8

Answers:


16

使用一些管道和 sort

find . -type f -print0 | sort -z | xargs -r0 sha256sum > sha256SumOutput

说明

man find

   -print0
        True; print the full file name on the standard output, followed
        by a null character (instead of the newline character that -print
        uses). This allows file names that contain newlines or other
        types of white space to be  correctly  interpreted by programs
        that process the find output.  This option corresponds to the -0
        option of xargs.

man sort

   -z, --zero-terminated
        line delimiter is NUL, not newline

man xargs

   -0   
        Input items are terminated by a null character instead of by
        whitespace, and the quotes and backslash are not special (every
        character is taken literally).  Disables the end of file string,
        which is treated like any  other  argument. Useful when input
        items might contain white space, quote marks, or backslashes.
        The GNU find -print0 option produces input suitable for this mode.

% ls -laog
total 4288
drwxrwxr-x  2 4329472 Aug 17 08:20 .
drwx------ 57   20480 Aug 17 08:20 ..
-rw-rw-r--  1       0 Aug 17 08:15 a
-rw-rw-r--  1       0 Aug 17 08:15 a b
-rw-rw-r--  1       0 Aug 17 08:15 b
-rw-rw-r--  1       0 Aug 17 08:15 c

% find -type f -print0 | sort -z | xargs -r0 sha256sum                  
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  ./a
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  ./a b
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  ./b
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  ./c

第一列中的值相同,因为文件在我的测试中没有任何内容。


1
哦,是的!空终止而不是换行符
user3591723

1

您应该能够将输出从find传送到sort


是的,但是那没有-exec开关。
UTF-8

2
我不相信find有什么方法可以按字母顺序排列输出,但是管道传递到sort然后使用xargs会给出预期的输出。find . -type f | sort | xargs sha256sum。虽然这将有问题与子目录..
user3591723

处理子目录的find . -type f | awk -F/ '{print $NF, $0}' | sort | awk '{print $2}' | xargs sha256sum
hacky方法

这将显示错误xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option sha256sum: invalid option -- 'l' Try 'sha256sum --help' for more information.
UTF-8

我的猜测是您的文件之一的名称中带有单引号
user3591723
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.