📖 Scenario: You have a list of filenames and want to delete them safely using a command that reads the filenames from input. Instead of typing each filename manually, you will use xargs to build the delete commands automatically.
🎯 Goal: Learn how to use xargs to take input from a list and build commands that operate on each item. You will create a file list, prepare a command to delete those files using xargs, and then display the command output.
📋 What You'll Learn
Create a file named files.txt with exact filenames
Use a variable to hold the delete command prefix
Use xargs to build the delete commands from the file list
Display the output of the command
💡 Why This Matters
🌍 Real World
Using xargs helps automate command building when you have lists of items, like filenames, user names, or URLs, saving time and avoiding manual typing.
💼 Career
System administrators and developers often use xargs to efficiently run commands on many files or inputs, making scripting and automation faster and less error-prone.
Progress0 / 4 steps
1
Create a file list
Create a file named files.txt with these exact filenames, one per line: file1.txt, file2.txt, file3.txt
Linux CLI
Hint
Use echo with -e and newline characters to create the file.
2
Set the delete command prefix
Create a variable called delete_cmd and set it to the string rm -v which will be used to delete files verbosely
Linux CLI
Hint
Use simple variable assignment without spaces around the equals sign.
3
Use xargs to build delete commands
Use xargs with the variable delete_cmd to delete the files listed in files.txt. Use the -I {} option to replace {} with each filename.
Linux CLI
Hint
Use input redirection < files.txt to feed filenames to xargs.
4
Display the output of the delete commands
Run the full command that uses xargs with delete_cmd to delete the files from files.txt and print the output to the terminal.
Linux CLI
Hint
Make sure the files exist before running the command to see the verbose delete messages.
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
Step 1: Understand the purpose of xargs
xargs reads input and uses it to build commands by passing the input as arguments.
Step 2: Compare with other options
Options B, C, and D describe other commands like ls, chmod, and pwd, which are unrelated to xargs.
Final Answer:
It builds and runs commands using input as arguments. -> Option A
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
Step 1: Understand the command flow
find . -name '*.tmp' lists files; piping to xargs rm runs rm on those files.
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.
Final Answer:
find . -name '*.tmp' | xargs rm -> Option C
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
Step 1: Understand -n 2 option
-n 2 tells xargs to use 2 arguments per command execution.
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.
Final Answer:
one two
three four -> Option B
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
Step 1: Analyze the incorrect option usage
-n requires a number argument, but here it is followed by echo, which is wrong.
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.
Final Answer:
cat files.txt | xargs -n 1 rm -> Option A
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
Step 1: Understand parallel option -P 5
-P 5 runs up to 5 commands in parallel, which limits simultaneous downloads.
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.
Final Answer:
cat urls.txt | xargs -P 5 -n 1 wget -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.