Course outline
Course / The terminal and the language

Hand many files to one command

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/03-05-xargs-batch-commands
cd 03-05-xargs-batch-commands
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

The folder testdata holds log files at different depths, mixed with other files. Use find and xargs to print the line count of every .log file, one line per file, in sorted path order.

Example

If testdata held these files:

testdata/a.log        (2 lines)
testdata/sub/b.log    (5 lines)
testdata/notes.txt    (9 lines)

your script prints:

2 testdata/a.log
5 testdata/sub/b.log
7 total

notes.txt is not a .log file, so it is left out. The last line, total, is added by wc itself whenever it counts two or more files. Keep it.

Rules

  • Edit answer.sh. Search the whole testdata folder, including every sub-folder, and print the result.
  • Only files whose names end in .log count. Ignore everything else.
  • Use sort between find and xargs so the files are counted in sorted path order. find alone returns files in an order that depends on the filesystem.
  • Every folder in this lab holds at least two .log files, so wc always prints the total line last.
  • Keep the paths exactly as find testdata prints them, starting with testdata/.
  • Spaces at the start of a line are ignored when your output is checked, so do not worry about how wc pads its numbers.
  • Do not edit or delete any file. Your script only prints.

What you'll practice

find -name, sort, xargs, and wc -l: one command run over many files.

Run it

gonomicon fetch week-1/03-05-xargs-batch-commands
cd 03-05-xargs-batch-commands
gonomicon test
gonomicon submit

Hints

  1. find testdata -name "*.log" lists the log files. xargs can hand that whole list to another command as arguments.
  2. The command you want is wc -l. Put sort between find and xargs so the file order is fixed.
  3. The shape is find testdata -name "*.log" | sort | xargs wc -l.

Something wrong with this lesson? Report a problem