0Pricing
Linux Command Line Mastery · Leçon

`xargs` pour enchaîner des commandes

Apprenez à construire et à exécuter des lignes de commande à partir de l’entrée standard avec `xargs` afin d’automatiser efficacement les tâches.

`xargs` pour enchaîner des commandes est une leçon Linux Command Line Mastery gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Linux Command Line Mastery, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Linux Command Line Mastery comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Intro to `xargs`

xargs is a powerful command-line utility that acts as a bridge between commands. It takes input from standard input (stdin) and builds new command lines, then executes them.

  • It reads items (usually words or lines) from stdin.
  • It then uses these items as arguments for another command.
  • This is incredibly useful for automating tasks involving many files or inputs from other commands.

Basic `xargs` Usage

Let's see xargs in its simplest form. Here, echo sends a list of words to xargs. xargs then takes these words and passes them as arguments to a second echo command.

Try running this example:

echo "apple banana cherry" | xargs echo

Chaining `find` with `xargs`

One of the most common and powerful uses for xargs is processing files found by the find command. find lists files, and xargs then takes those names to perform an operation.

First, we'll create some dummy files, then use find to locate .txt files and xargs to display their names.

touch doc1.txt doc2.txt report.pdf
find . -name "*.txt" | xargs echo

Default Argument Handling

By default, xargs tries to take as many arguments as possible from its standard input and appends them to a single execution of the target command.

  • It reads items separated by spaces or newlines.
  • It builds a command by adding these items at the end.
  • If the list of items is too long for one command, it will automatically run the command multiple times.

Limit Arguments with `-n`

Sometimes you need to run the target command multiple times, with only a specific number of arguments each time. The -n option allows you to specify how many arguments xargs should pass to each command execution.

Here, xargs will run echo for every two words it receives:

echo "one two three four five six" | xargs -n 2 echo

Custom Placement with `-I`

When the input item isn't just the last argument, you need more control. The -I option allows you to define a placeholder string (e.g., {}) that xargs will replace with each input item.

This lets you insert the item anywhere in your command, not just at the end.

echo "fileA.txt fileB.txt" | xargs -I {} echo "Processing: {}"

Parallel Execution with `-P`

For tasks that can be run independently, xargs -P can significantly speed things up by executing multiple commands in parallel. You specify the number of parallel processes to run.

  • -P 0 tells xargs to run as many processes as possible.
  • Be cautious with resource-intensive commands, as this can overload your system.
  • This is excellent for batch processing, like resizing many images.

Safe Handling with `-0`

Filenames in Linux can contain spaces, newlines, or other special characters. If not handled correctly, these can break xargs, as it usually splits input by spaces or newlines.

The solution is to use find -print0 (which outputs null-terminated strings) with xargs -0 (which safely reads null-terminated strings).

touch "my file with spaces.txt"
find . -name "*.txt" -print0 | xargs -0 echo

Practical Use: Deleting Files

Let's combine what we've learned for a practical task: finding and deleting all .bak files in the current directory. We'll use find -print0 and xargs -0 rm for safety.

Caution: The rm command is permanent. Always double-check your commands before running!

touch file1.txt.bak file2.log.bak temp.bak
echo "Files before deletion:"
ls -l *.bak
find . -name "*.bak" -print0 | xargs -0 rm
echo "Files after deletion:"
ls -l *.bak

Quick Check on `xargs`

Consider the following command:

echo "one two three four five" | xargs -n 2 echo

What will be the exact output?

Recap: `xargs` for Automation

You've learned how xargs acts as a powerful bridge, taking standard input and turning it into arguments for other commands. It's a fundamental tool for efficient command-line automation!

  • It's essential for chaining commands like find with other utilities.
  • Options like -n, -I, -P, and -0 provide fine-grained control over command construction and execution.
  • Using xargs -0 with find -print0 is crucial for safely handling filenames with special characters.

Keep practicing to master this versatile tool and streamline your shell scripting!

Questions Fréquemment Posées

La leçon « `xargs` pour enchaîner des commandes » est-elle gratuite ?

Oui — le texte complet de « `xargs` pour enchaîner des commandes » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Linux Command Line Mastery, passe à CoddyKit PRO. Le cours Linux Command Line Mastery comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « `xargs` pour enchaîner des commandes » ?

Apprenez à construire et à exécuter des lignes de commande à partir de l’entrée standard avec `xargs` afin d’automatiser efficacement les tâches. Tu pratiques Linux Command Line Mastery avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Linux Command Line Mastery ?

Aucune expérience préalable n'est requise. Linux Command Line Mastery sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.

Combien de temps prend la leçon « `xargs` pour enchaîner des commandes » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Linux Command Line Mastery ?

Oui. Chaque leçon Linux Command Line Mastery inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Maîtriser les expressions régulières (Regex)
  2. Motifs avancés avec `grep` et `find`
  3. `xargs` pour enchaîner des commandes
  4. Modifier des flux à grande échelle avec sed et tr
← Retour à Linux Command Line Mastery