Bird
Raised Fist0
Linux CLIscripting~20 mins

xargs for building commands from input in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
xargs Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this xargs command?
Given a file names.txt containing:
alice
bob
carol

What is the output of:
cat names.txt | xargs -I {} echo Hello, {}
Linux CLI
cat names.txt | xargs -I {} echo Hello, {}
A
Hello, alice
Hello, bob
Hello, carol
BHello, alice bob carol
C
Hello, alice
bob
carol
DHello, alice Hello, bob Hello, carol
Attempts:
2 left
💡 Hint
Remember that xargs runs the command once per input line when using -I.
💻 Command Output
intermediate
1:30remaining
How many files are deleted by this command?
Assuming the current directory has files: file1.txt, file2.txt, file3.txt, what does this command do?
ls *.txt | xargs rm

How many files are deleted?
Linux CLI
ls *.txt | xargs rm
A1
B3
C0
DDepends on xargs version
Attempts:
2 left
💡 Hint
xargs passes all arguments to rm in one go unless limited.
🔧 Debug
advanced
2:00remaining
Why does this xargs command fail?
Consider this command:
echo "one two three" | xargs echo

It outputs:
one two three

instead of:
one
two
three

Why?
Linux CLI
echo "one two three" | xargs echo
ABecause the shell merges all output into one line
BBecause echo does not accept multiple arguments
CBecause xargs -n 1 is ignored when input is from echo
DBecause echo outputs all arguments on one line, xargs runs echo once with all words
Attempts:
2 left
💡 Hint
Check how echo behaves with multiple arguments.
📝 Syntax
advanced
2:00remaining
Which xargs command correctly replaces {} with input lines?
You want to run mv on files listed in files.txt, renaming each by adding .bak. Which command works?
Acat files.txt | xargs -i mv {} {}.bak
Bcat files.txt | xargs mv {} {}.bak
Ccat files.txt | xargs -I {} mv {} {}.bak
Dcat files.txt | xargs -I mv {} {}.bak
Attempts:
2 left
💡 Hint
The placeholder {} must be specified with -I option.
🚀 Application
expert
2:30remaining
How to safely delete files with spaces using xargs?
You have a file list.txt containing filenames, some with spaces, e.g., my file.txt. Which command safely deletes all files listed?
Acat list.txt | tr '\n' '\0' | xargs -0 rm
Bcat list.txt | xargs -I {} rm {}
Ccat list.txt | xargs rm
Dcat list.txt | xargs -d '\n' rm
Attempts:
2 left
💡 Hint
Use null character as separator to handle spaces in filenames.

Practice

(1/5)
1. What does the xargs command do in Linux?
easy
A. It builds and runs commands using input as arguments.
B. It lists files in a directory.
C. It changes file permissions.
D. It displays the current directory path.

Solution

  1. Step 1: Understand the purpose of xargs

    xargs reads input and uses it to build commands by passing the input as arguments.
  2. Step 2: Compare with other options

    Options B, C, and D describe other commands like ls, chmod, and pwd, which are unrelated to xargs.
  3. Final Answer:

    It builds and runs commands using input as arguments. -> Option A
  4. Quick Check:

    xargs builds commands from input = A [OK]
Hint: Remember: xargs turns input into command arguments [OK]
Common Mistakes:
  • Confusing xargs with listing or permission commands
  • Thinking xargs modifies files directly
  • Assuming xargs shows directory info
2. Which of the following is the correct syntax to use xargs to delete files listed by find . -name '*.tmp'?
easy
A. rm | xargs find . -name '*.tmp'
B. xargs find . -name '*.tmp' rm
C. find . -name '*.tmp' | xargs rm
D. find . -name '*.tmp' rm | xargs

Solution

  1. Step 1: Understand the command flow

    find . -name '*.tmp' lists files; piping to xargs rm runs rm on those files.
  2. Step 2: Check syntax correctness

    find . -name '*.tmp' | xargs rm correctly pipes the list to xargs rm. Other options misuse command order or pipe incorrectly.
  3. Final Answer:

    find . -name '*.tmp' | xargs rm -> Option C
  4. Quick Check:

    Pipe find output to xargs rm = A [OK]
Hint: Pipe list to xargs then command: list | xargs command [OK]
Common Mistakes:
  • Placing xargs before find
  • Piping commands in wrong order
  • Using rm inside find without xargs
3. What is the output of this command sequence?
echo 'one two three four' | xargs -n 2 echo
medium
A. one two three four
B. one two three four
C. one two three four
D. one two three

Solution

  1. Step 1: Understand -n 2 option

    -n 2 tells xargs to use 2 arguments per command execution.
  2. Step 2: Apply to input

    Input words are split: 'one two' and 'three four'. So echo runs twice: once with 'one two', once with 'three four', printing two lines.
  3. Final Answer:

    one two three four -> Option B
  4. Quick Check:

    -n 2 splits input into pairs = B [OK]
Hint: Use -n to limit args per command run [OK]
Common Mistakes:
  • Thinking all input prints on one line
  • Assuming one argument per command by default
  • Ignoring how -n splits input
4. Identify the error in this command and fix it:
cat files.txt | xargs -n echo rm
medium
A. The command should be: cat files.txt | xargs -n 1 rm
B. The command should be: cat files.txt | xargs rm -n
C. The command should be: xargs cat files.txt rm -n
D. The command is correct as is

Solution

  1. Step 1: Analyze the incorrect option usage

    -n requires a number argument, but here it is followed by echo, which is wrong.
  2. Step 2: Correct the syntax

    The correct syntax is to specify the number after -n and then the command, e.g., xargs -n 1 rm.
  3. Final Answer:

    cat files.txt | xargs -n 1 rm -> Option A
  4. Quick Check:

    Use -n with number before command = C [OK]
Hint: Place number after -n before command in xargs [OK]
Common Mistakes:
  • Putting command immediately after -n without number
  • Misordering arguments
  • Confusing xargs options with command options
5. You have a file urls.txt with 100 URLs, one per line. You want to download them using wget, but only 5 downloads should run at the same time to avoid overload. Which command correctly uses xargs to do this?
hard
A. wget < urls.txt | xargs -P 5 -n 1
B. cat urls.txt | xargs -n 5 wget
C. xargs -P 1 -n 5 wget < urls.txt
D. cat urls.txt | xargs -P 5 -n 1 wget

Solution

  1. Step 1: Understand parallel option -P 5

    -P 5 runs up to 5 commands in parallel, which limits simultaneous downloads.
  2. Step 2: Use -n 1 to run one URL per wget command

    Each wget command downloads one URL; -n 1 ensures one argument per command.
  3. Final Answer:

    cat urls.txt | xargs -P 5 -n 1 wget -> Option D
  4. Quick Check:

    Use -P for parallel and -n 1 for one arg each = D [OK]
Hint: Use -P for parallel jobs and -n 1 for single args [OK]
Common Mistakes:
  • Confusing -n with number of parallel jobs
  • Running all downloads sequentially
  • Misplacing input redirection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard