Bird
Raised Fist0
Linux CLIscripting~10 mins

xargs for building commands from input in Linux CLI - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use xargs to list details of files passed from echo.

Linux CLI
echo file1.txt file2.txt | xargs [1]
Drag options to blanks, or click blank then click option'
Als -l
Bcat
Crm
Dmkdir
Attempts:
3 left
💡 Hint
Common Mistakes
Using cat instead of ls -l will show file contents, not details.
Using rm will delete files, which is not intended here.
2fill in blank
medium

Complete the code to delete files listed in files.txt using xargs.

Linux CLI
cat files.txt | xargs [1]
Drag options to blanks, or click blank then click option'
Arm
Bls
Cmkdir
Dtouch
Attempts:
3 left
💡 Hint
Common Mistakes
Using ls will only list files, not delete them.
Using mkdir or touch will create directories or files, not delete.
3fill in blank
hard

Fix the error in the code to correctly count lines in files listed by find using xargs.

Linux CLI
find . -name '*.txt' | xargs [1]
Drag options to blanks, or click blank then click option'
Als -l
Bwc -l
Ccat
Drm
Attempts:
3 left
💡 Hint
Common Mistakes
Using ls -l lists file details but does not count lines.
Using cat outputs file contents but does not count lines.
4fill in blank
hard

Fill both blanks to create a command that finds all .log files and compresses them using gzip.

Linux CLI
find /var/log -name '*.log' | xargs [1] [2]
Drag options to blanks, or click blank then click option'
Agzip
B-v
C-9
Dls
Attempts:
3 left
💡 Hint
Common Mistakes
Using ls does not compress files.
Using -9 is a compression level but not required here.
5fill in blank
hard

Fill all three blanks to create a command that finds all .txt files, counts words in each, and shows only those with more than 100 words.

Linux CLI
find . -name '*.txt' | xargs [1] | awk '[2] [3] 100'
Drag options to blanks, or click blank then click option'
Awc -w
B$1
C>
Dls
Attempts:
3 left
💡 Hint
Common Mistakes
Using ls does not count words.
Using wrong comparison operators like < will filter incorrectly.

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