Challenge - 5 Problems
xargs Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
What is the output of this xargs command?
Given a file
What is the output of:
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, {}Attempts:
2 left
💡 Hint
Remember that xargs runs the command once per input line when using -I.
✗ Incorrect
The -I option replaces {} with each line from input and runs the command separately once per line. Each echo outputs 'Hello, ' followed by a newline, producing three lines.
💻 Command Output
intermediate1:30remaining
How many files are deleted by this command?
Assuming the current directory has files:
How many files are deleted?
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
Attempts:
2 left
💡 Hint
xargs passes all arguments to rm in one go unless limited.
✗ Incorrect
The ls command lists all .txt files, and xargs passes all filenames to rm at once, deleting all 3 files.
🔧 Debug
advanced2:00remaining
Why does this xargs command fail?
Consider this command:
It outputs:
instead of:
Why?
echo "one two three" | xargs echo
It outputs:
one two three
instead of:
one two three
Why?
Linux CLI
echo "one two three" | xargs echoAttempts:
2 left
💡 Hint
Check how echo behaves with multiple arguments.
✗ Incorrect
xargs runs echo once with all words as arguments. echo prints all arguments on one line separated by spaces.
📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
The placeholder {} must be specified with -I option.
✗ Incorrect
Option C uses -I {} correctly to replace {} with each input line. Option C uses -i which is deprecated and requires lowercase i with no argument, but may not work on all systems. Options B and D have syntax errors.
🚀 Application
expert2: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?Attempts:
2 left
💡 Hint
Use null character as separator to handle spaces in filenames.
✗ Incorrect
Option A uses null character as delimiter with -0 option, which safely handles filenames with spaces. Option A uses -d '\n' which may not be supported everywhere. Option A breaks on spaces. Option A runs rm once per file but may break on spaces without proper quoting.