0
0
Linux CLIscripting~20 mins

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

Choose your learning style9 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.