如何從 shell 計算縮圖檔名?

如何從 shell 計算縮圖檔名?

不久前我開始注意到,在 Xfce4 中,當我將一些文件發送到垃圾箱時,tumbler(Xfce4 縮圖器)會在相當長的一段時間內導致非常高的 I/O 負載。在調查該問題後,我發現它正在掃描 ~/.thumbnails 目錄,該目錄非常大。

所以我決定編寫一個 cron 腳本來定期清理 ~/.thumbnails 目錄,但有一個特定的大型視訊檔案目錄,tumbler 需要一些時間,有時甚至會失敗,才能為其創建縮圖。

這個想法是刪除所有縮圖,除了這些影片的縮圖。但為了保留這些縮圖,我必須找到它們的名字。問題是縮圖儲存時使用 URI 的 md5sum 以及 PNG 副檔名命名。

看完之後不倒翁來源,我發現縮圖的名稱是在以下行中產生的:

md5_hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);

g-compute-checksum-for-string 的文檔說:

g_compute_checksum_for_string(GChecksumType checksum_type,
                              const gchar *str,
                              gssize length);

checksum_type: a GChecksumType 
str:           the string to compute the checksum of
length:        the length of the string, or -1 if the string is null-terminated.

簡而言之,名為的檔案的縮圖/home/teresaejunior/File 01.png將儲存在 .thumbnails/ 目錄中,如下所示a8502be3273541e618b840204479a7f9.png

根據縮圖規格,URI 是file://filename。我對“空字符”做了一些研究,認為\0可以解決問題。為了達到這個結果a8502be3273541e618b840204479a7f9,我相信以下內容應該會起作用:

printf "file:///home/teresaejunior/File 01.png\0" | md5sum

但這f507285c45d293fa66bc0b813d17b6e6反而回來了。

有人可以給我一些建議嗎?我相信我的printf路線有缺陷。我的命令與 有何不同g_compute_checksum_for_string

答案1

計算MD5時不包含NUL字元。相反,是空格字元導致了您的問題。檔案名稱是URL編碼:

$ printf '%s' 'file:///home/teresaejunior/File%2001.png' | md5sum
a8502be3273541e618b840204479a7f9  -

以下是使用 Perl 進行轉換的一種方法:

$ perl -MURI::file -MDigest::MD5=md5_hex \
  -e 'printf "%s.png\n", md5_hex(URI::file->new(shift))' \
  '/home/teresaejunior/File 01.png'
a8502be3273541e618b840204479a7f9.png

相關內容