الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

xargs

Turn lines from a pipe into arguments of a command — and run it in batches or in parallel.

المستوى
أداة DevOps
المجموعة
النصوص والبحث
يحتاج root
لا
الحزمة
findutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

Many commands do not read standard input; they need arguments. xargs reads words or lines from the pipe and puts them on the command line of another command. With -P it runs many copies at the same time — a simple way to make scripts faster.

Use -0 together with find -print0 so file names with spaces work.

طريقة الكتابة

COMMAND | xargs [OPTIONS] [COMMAND [ARG ...]]

الخيارات 11

الأكثر استخدامًا

Options

-0, --null
Items end with a NUL character (use with find -print0).
-I {}
Replace {} in the command with each item; one run per item.
-n N
At most N arguments per run.
-P N
Run N processes at the same time (0 = as many as possible).
-L N
At most N lines per run.
-d DELIM
Items end with DELIM (for example -d '\n').
-r, --no-run-if-empty
Do not run the command when there is no input.
-t, --verbose
Print each command before it runs.
-p, --interactive
Ask before each run.
-a FILE
Read the items from FILE.
--max-chars N
Limit the length of the command line.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Delete old log files safely (spaces in names work)

    find /var/log/app -name '*.log' -mtime +14 -print0 | xargs -0 -r rm -f
  2. 2One command per item with {}

    cat hosts.txt | xargs -I {} ssh {} 'uptime'
  3. 3Pull many images in parallel (4 at a time)

    cat images.txt | xargs -P 4 -n 1 docker pull
  4. 4Delete all Evicted pods

    kubectl get pods -A --field-selector=status.phase=Failed -o jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name}{"\n"}{end}' | xargs -r -n 2 sh -c 'kubectl delete pod -n "$0" "$1"'
  5. 5grep in the files that find found

    find . -name '*.yaml' -print0 | xargs -0 grep -l 'image: nginx'
  6. 6See the commands before they run

    echo web1 web2 | xargs -t -n 1 ping -c 1
  7. 7Check many URLs fast

    xargs -P 8 -I {} curl -s -o /dev/null -w '%{http_code} {}\n' {} < urls.txt
  8. 8Remove merged Git branches

    git branch --merged main | grep -v 'main$' | xargs -r git branch -d
  9. 9Archive files in batches of 100

    find data -type f -print0 | xargs -0 -n 100 tar -rf data.tar
  10. 10Lines (not words) as items

    printf 'my file.txt\nother file.txt\n' | xargs -d '\n' ls -l
  11. 11Ask before each delete

    ls *.tmp | xargs -p rm

انتبه

  • Without -0/-d '\n' xargs splits on spaces and quotes: "my file" becomes two items.
  • Without -r GNU xargs runs the command once with no arguments when the input is empty — dangerous with rm.
  • Output of parallel runs (-P) can mix; write each result to its own file if order matters.

نصائح للحفظ

رموز الخروج

0All runs exited 0.
123A run exited with 1–125.
124A run exited with 255.
125A run was killed by a signal.
126The command cannot run.
127The command was not found.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man xargs