Sortuj pliki alfabetycznie przed przetwarzaniem


12

Używam polecenia

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

mieszać każdy plik w hierarchii folderów. Niestety sha256sumnie pobiera nazw plików findw alfabetycznym odrze. Jak to może zostać naprawione?

Chciałbym, aby zostały uporządkowane przed ich zaszyfrowaniem, aby były zaszyfrowane w kolejności alfabetycznej (ma to powód).


Znajdź pliki, potok do sortposortowania listy i potok do sha256sum
Sergiy Kolodyazhnyy

Sortowanie alfanumeryczne.
UTF-8

Odpowiedzi:


17

Za pomocą niektórych rur i sort

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

Wyjaśnienie

Od 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.

Od man sort

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

Od 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.

Przykład

% 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

Wartości w pierwszej kolumnie są takie same, ponieważ pliki nie mają żadnej zawartości w moim teście.


1
O tak! Null kończy zamiast nowego wiersza
użytkownik3591723,

1

Powinieneś być w stanie przesyłać dane wyjściowe z finddo sort.


Tak, ale wtedy nie ma -execprzełącznika.
UTF-8

2
Nie wierzę, że findma jakiś sposób na alfabetyczne wyjście, ale potokowanie do, sorta następnie używanie xargsdałoby oczekiwany wynik. find . -type f | sort | xargs sha256sum. Chociaż miałby problemy z podkatalogami.
użytkownik3591723,

Hacky sposób radzenia sobie z podkatalogami tofind . -type f | awk -F/ '{print $NF, $0}' | sort | awk '{print $2}' | xargs sha256sum
user3591723,

Spowoduje to wydrukowanie błędu 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

Domyślam się, że jeden z twoich plików ma pojedynczy cytat w nazwie
użytkownik3591723,
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.