0Pricing
Linux Command Line Mastery · Lezione

`xargs` per concatenare i comandi

Impari a costruire ed eseguire righe di comando dall’input standard usando `xargs` per automatizzare le attività in modo efficiente.

`xargs` per concatenare i comandi è una lezione Linux Command Line Mastery gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Linux Command Line Mastery, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Linux Command Line Mastery include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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!

Domande Frequenti

La lezione «`xargs` per concatenare i comandi» è gratuita?

Sì — il testo completo di «`xargs` per concatenare i comandi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Linux Command Line Mastery, passa a CoddyKit PRO. Il corso Linux Command Line Mastery include 4 lezioni in totale.

Cosa imparerò in «`xargs` per concatenare i comandi»?

Impari a costruire ed eseguire righe di comando dall’input standard usando `xargs` per automatizzare le attività in modo efficiente. Eserciti Linux Command Line Mastery con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Linux Command Line Mastery?

Non è richiesta alcuna esperienza precedente. Linux Command Line Mastery su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «`xargs` per concatenare i comandi»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Linux Command Line Mastery?

Sì. Ogni lezione Linux Command Line Mastery include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Padroneggiare le espressioni regolari (regex)
  2. Pattern avanzati con `grep` e `find`
  3. `xargs` per concatenare i comandi
  4. Modificare stream su larga scala con sed e tr
← Torna a Linux Command Line Mastery