Course / The terminal and the language
Hand many files to one command
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitGoal
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 wholetestdatafolder, including every sub-folder, and print the result. - Only files whose names end in
.logcount. Ignore everything else. - Use
sortbetweenfindandxargsso the files are counted in sorted path order.findalone returns files in an order that depends on the filesystem. - Every folder in this lab holds at least two
.logfiles, sowcalways prints thetotalline last. - Keep the paths exactly as
find testdataprints them, starting withtestdata/. - Spaces at the start of a line are ignored when your output is checked, so do not worry about how
wcpads 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
find testdata -name "*.log"lists the log files.xargscan hand that whole list to another command as arguments.- The command you want is
wc -l. Putsortbetweenfindandxargsso the file order is fixed. - The shape is
find testdata -name "*.log" | sort | xargs wc -l.