Сортировать файлы по алфавиту перед обработкой

Я использую команду

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

для хеширования каждого файла в иерархии папок. К сожалению, sha256sum не получает имена файлов из find в алфавитном порядке. Как это можно исправить?

Я бы хотел, чтобы они были упорядочены , прежде чем они будут хэшированы, поэтому они хэшируются в алфавитном порядке (это имеет причину).

16
задан 17 August 2015 в 09:26

2 ответа

Использование некоторых каналов и 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.

From man sort

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

From 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

Значения в первом столбце такие же, так как в моем тесте файлы не имеют содержимого.

24
ответ дан 17 August 2015 в 19:26
  • 1
    @MikulasKuzmiak:) Я отредактирую с полным объяснением теперь. Я шел мобильный ранее и настолько медленный для ввода – Zanna 8 November 2016 в 11:04

У вас должна быть возможность просто перенаправить ваш вывод из find в sort .

1
ответ дан 17 August 2015 в 19:26
  • 1
    @MikulasKuzmiak там, редактируя сделанный, не стесняются спрашивать, не ли это ясно:) – Zanna 8 November 2016 в 11:44

Другие вопросы по тегам:

Похожие вопросы: