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

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

我使用命令

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

对文件夹层次结构中的每个文件进行哈希处理。遗憾的是,sha256sum无法find按字母顺序获取文件名。如何修复此问题?

我想订购它们是按字母顺序散列的(这是有原因的)。

答案1

使用一些管道和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

   -r
        If the standard input does not contain any nonblanks, do not run
        the command. Normally, the command is run once even if there is
        no input. This option is a GNU extension.

   -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:16 a
-rw-rw-r--  1       0 Aug 17 08:16 a b
-rw-rw-r--  1       0 Aug 17 08:16 b
-rw-rw-r--  1       0 Aug 17 08:16 c

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

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

答案2

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

相关内容