約20Kファイルのフォルダがあります。ファイルは、パターンに従って命名されているxy_{\d1,5}_{\d4}\.abc、例えばxy_12345_1234.abc。次のコマンドを使用して、最初の10Kを圧縮したいと思います。
ls | sort -n -k1.4,1.9 | head -n10000 | xargs tar -czf xy_0_10000.tar.gz
ただし、結果のファイルには約2Kのファイルしかありませんでした。
ls | sort -n -k1.4,1.9 | head -n10000 | wc -l ただし、期待どおりに10000を返します。
ここで基本的なことを誤解しているようです...
Linux Mint 17.1、GNU tar 1.27.1でzsh 5.0.2を使用しています
編集:
@Archemarによって提案されたフォークは非常にもっともらしいように聞こえ、最新のフォークが結果のファイルを上書きします-ファイルにはファイルの「テール」が含まれています-7773から9999。
の結果xargs --show-limit:
Your environment variables take up 3973 bytes
POSIX upper limit on argument length (this system): 2091131
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2087158
Size of command buffer we are actually using: 131072
交換-cして-rか、-u私の場合には動作しませんでした。エラーメッセージはtar: Cannot update compressed archives
両方-rを使用すると-u無効になり、失敗しますtar: You may not specify more than one '-Acdtrux', '--delete' or '--test-label' option
との置き換えも無効-cである-aように見え、同じtar: You must specify one of the '-Acdtrux', '--delete' or '--test-label' optionsように失敗しますが、問題azfを認識Acdtruxしていないため、私には矛盾しているようです。
編集2:
しかし私がしようとすると
ls | sort -n -k1.4,1.9 | head -n10000 | tar -czf xy_0_10000.tar.gz -T - 私は得る
tar: option requires an argument -- 'T'
まあ、おそらくファイル名はtarに達しませんか?しかし、それは彼らのように見えます、私が実行すると
ls | sort -n -k1.4,1.9 | head -n10000 | tar --null -czf xy_0_10000.tar.gz -T - 私は得る
tar: xy_0_.ab\nxy_1_...<the rest of filenames separated by literal \n>...998.ab
Cannot stat: File name too long
では、なぜtarがファイル名を表示しないのですか?
find持っている、-print0代わりに改行の区切り文字としてnullバイトを使用するオプションを。フラグでsortそれを処理でき-zます。head、残念ながらnullバイト区切り文字の処理を処理していませんが、この回答にはtrswap \nと\0beforeとafter を使用するソリューションがありますhead。tar持っている--null -T -からヌル区切りのファイル名を読み取ることstdin。