Last active
July 17, 2021 07:53
-
-
Save cecepm/adb9cae2a5071ffb6ade8ed39c32db93 to your computer and use it in GitHub Desktop.
belajar dengan xargs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# misal, ada file input.txt | |
# | |
# isinya: | |
# asdf1 | |
# asdf2 | |
# asdf3 | |
# dst... | |
cat input.txt |xargs | |
cat input.txt |xargs --verbose | |
cat input.txt |xargs -t | |
cat input.txt |xargs -t -n1 | |
cat input.txt |xargs -t -n2 | |
# buat file dari list yg ada di input.txt | |
cat input.txt |xargs touch | |
# copy semua file ke folder backups | |
mkdir backups | |
ls -1 |grep asdf |xargs cp -t backups | |
# hapus file | |
ls -1 |grep asdf |xargs rm | |
# bagaimana kalau nama filenya mengandung spasi | |
touch file\ {0..9} | |
ls -1 |grep file |ls | |
# error? | |
# solusi pertama, tapi kelemahannya arg cuma bisa 1 | |
ls -1 |grep file |xargs -I{} ls {} | |
# solusi lain, ganti return/new-line dengan null char | |
ls -1 |grep file |tr "\n" "\0" | |
# bisa di check pakai hexdump utk memastikan ada null char | |
ls -1 |grep file |tr "\n" "\0" |hexdump -c | |
# skrg tidak akan ada error lagi | |
ls -1 |grep file |tr "\n" "\0" |xargs -0 ls | |
# solusi lain, pakai find dan -print0 | |
# (print separator dengan null char) | |
find . -type f -iname "file*" -print0 | |
find . -type f -iname "file*" -print0 |xargs -0 ls | |
# tapi solusi di atas masih kurang safe, karena kalau | |
# misal filenya tidak ditemukan (hasilnya kosong), | |
# xargs masih akan dijalankan sekali untuk menghindari | |
# dijalankan sekali, gunakan opsi | |
# --no-run-if-empty atau -r | |
find . -type f -iname "cecep*" -print0 |xargs -0 ls | |
find . -type f -iname "cecep*" -print0 |xargs --no-run-if-empty -0 ls | |
find . -type f -iname "cecep*" -print0 |xargs -r -0 ls | |
# xargs jg bisa dijalankan paralel dengan | |
# menggunakan opsi -P. Untuk contoh, saya akan kasih | |
# sleep dan argumennya jadi terbatas cuma 1 | |
cat input.txt |xargs -I {} bash -c "echo {}" | |
cat input.txt |xargs -P2 -I {} bash -c "echo {} && sleep 2" | |
# contoh real nya mungkin utk mengconvert video dengan ffmpeg | |
ls -1 |grep nama_file_video |xargs -P2 -I {} ffmpeg -i {} dst | |
# xargs jg biasanya digunakan ketika program yg kita jalankan | |
# tidak bisa menerima arg list lagi, karena kena limit | |
# errornya: argument list too long | |
# misal, buat 200k files | |
touch file{000001..100000} | |
touch file{100001..200000} | |
ls -1 |grep file |wc -l | |
# sekarang coba hapus file | |
# harusnya muncul error spt ini: | |
# zsh/bash: argument list too long: rm | |
rm file* | |
# dengan xargs kita solve issue ini, karena xargs otomatis | |
# mensplit args sehingga tidak melebihi limit arg max | |
# cara check limit | |
echo "" |xargs --show-limits | |
# cara hapus 200k files | |
ls -1 |grep file |xargs rm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment