forked from swcarpentry/shell-novice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-find.html
1214 lines (1164 loc) · 71.1 KB
/
07-find.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!-- START: inst/pkgdown/templates/layout.html --><!-- Generated by pkgdown: do not edit by hand --><html lang="en" data-bs-theme="auto"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><title>The Unix Shell: Finding Things</title><meta name="viewport" content="width=device-width, initial-scale=1"><script src="assets/themetoggle.js"></script><link rel="stylesheet" type="text/css" href="assets/styles.css"><script src="assets/scripts.js" type="text/javascript"></script><!-- mathjax --><script type="text/x-mathjax-config">
MathJax.Hub.Config({
config: ["MMLorHTML.js"],
jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML", "output/PreviewHTML"],
extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "a11y/accessibility-menu.js"],
TeX: {
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
},
tex2jax: {
inlineMath: [['\\(', '\\)']],
displayMath: [ ['$$','$$'], ['\\[', '\\]'] ],
processEscapes: true
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><!-- Responsive Favicon for The Carpentries --><link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png"><link rel="manifest" href="site.webmanifest"><link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5"><meta name="msapplication-TileColor" content="#da532c"><meta name="theme-color" media="(prefers-color-scheme: light)" content="white"><meta name="theme-color" media="(prefers-color-scheme: dark)" content="black"></head><body>
<header id="top" class="navbar navbar-expand-md top-nav software"><svg xmlns="http://www.w3.org/2000/svg" class="d-none"><symbol id="check2" viewbox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z"></path></symbol><symbol id="circle-half" viewbox="0 0 16 16"><path d="M8 15A7 7 0 1 0 8 1v14zm0 1A8 8 0 1 1 8 0a8 8 0 0 1 0 16z"></path></symbol><symbol id="moon-stars-fill" viewbox="0 0 16 16"><path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"></path><path d="M10.794 3.148a.217.217 0 0 1 .412 0l.387 1.162c.173.518.579.924 1.097 1.097l1.162.387a.217.217 0 0 1 0 .412l-1.162.387a1.734 1.734 0 0 0-1.097 1.097l-.387 1.162a.217.217 0 0 1-.412 0l-.387-1.162A1.734 1.734 0 0 0 9.31 6.593l-1.162-.387a.217.217 0 0 1 0-.412l1.162-.387a1.734 1.734 0 0 0 1.097-1.097l.387-1.162zM13.863.099a.145.145 0 0 1 .274 0l.258.774c.115.346.386.617.732.732l.774.258a.145.145 0 0 1 0 .274l-.774.258a1.156 1.156 0 0 0-.732.732l-.258.774a.145.145 0 0 1-.274 0l-.258-.774a1.156 1.156 0 0 0-.732-.732l-.774-.258a.145.145 0 0 1 0-.274l.774-.258c.346-.115.617-.386.732-.732L13.863.1z"></path></symbol><symbol id="sun-fill" viewbox="0 0 16 16"><path d="M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"></path></symbol></svg><a class="visually-hidden-focusable skip-link" href="#main-content">Skip to main content</a>
<div class="container-fluid top-nav-container">
<div class="col-md-8">
<div class="large-logo">
<img id="swc-logo" alt="Software Carpentry" src="assets/images/software-logo.svg"></div>
</div>
<div class="selector-container">
<div id="theme-selector">
<li class="nav-item dropdown" id="theme-button-list">
<button class="btn btn-link nav-link px-0 px-lg-2 dropdown-toggle d-flex align-items-center" id="bd-theme" type="button" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static" aria-label="Toggle theme (auto)">
<svg class="bi my-1 theme-icon-active"><use href="#circle-half"></use></svg><i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="bd-theme-text"><li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#sun-fill"></use></svg>
Light
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#moon-stars-fill"></use></svg>
Dark
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center active" data-bs-theme-value="auto" aria-pressed="true">
<svg class="bi me-2 theme-icon"><use href="#circle-half"></use></svg>
Auto
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
</ul></li>
</div>
<div class="dropdown" id="instructor-dropdown">
<button class="btn btn-secondary dropdown-toggle bordered-button" type="button" id="dropdownMenu1" data-bs-toggle="dropdown" aria-expanded="false">
<i aria-hidden="true" class="icon" data-feather="eye"></i> Learner View <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1"><li><button class="dropdown-item" type="button" onclick="window.location.href='instructor/07-find.html';">Instructor View</button></li>
</ul></div>
</div>
</div>
<hr></header><nav class="navbar navbar-expand-xl bottom-nav software" aria-label="Main Navigation"><div class="container-fluid nav-container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle Navigation">
<span class="navbar-toggler-icon"></span>
<span class="menu-title">Menu</span>
</button>
<div class="nav-logo">
<img class="small-logo" alt="Software Carpentry" src="assets/images/software-logo-sm.svg"></div>
<div class="lesson-title-md">
The Unix Shell
</div>
<div class="search-icon-sm">
<!-- TODO: do not show until we have search
<i role="img" aria-label="Search the All In One page" data-feather="search"></i>
-->
</div>
<div class="desktop-nav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item">
<span class="lesson-title">
The Unix Shell
</span>
</li>
<li class="nav-item">
<a class="nav-link" href="key-points.html">Key Points</a>
</li>
<li class="nav-item">
<a class="nav-link" href="reference.html#glossary">Glossary</a>
</li>
<li class="nav-item">
<a class="nav-link" href="profiles.html">Learner Profiles</a>
</li>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" id="navbarDropdown" data-bs-toggle="dropdown" aria-expanded="false">
More <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown"><li><a class="dropdown-item" href="discuss.html">Discussion</a></li><li><a class="dropdown-item" href="reference.html">Summary of Basic Commands</a></li>
</ul></li>
</ul></div>
<!--
<form class="d-flex col-md-2 search-form">
<fieldset disabled>
<input class="form-control me-2 searchbox" type="search" placeholder="" aria-label="">
<button class="btn btn-outline-success tablet-search-button" type="submit">
<i class="search-icon" data-feather="search" role="img" aria-label="Search the All In One page"></i>
</button>
</fieldset>
</form>
-->
<a id="search-button" class="btn btn-primary" href="aio.html" role="button" aria-label="Search the All In One page">Search the All In One page</a>
</div><!--/div.container-fluid -->
</nav><div class="col-md-12 mobile-title">
The Unix Shell
</div>
<aside class="col-md-12 lesson-progress"><div style="width: 83%" class="percentage">
83%
</div>
<div class="progress software">
<div class="progress-bar software" role="progressbar" style="width: 83%" aria-valuenow="83" aria-label="Lesson Progress" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</aside><div class="container">
<div class="row">
<!-- START: inst/pkgdown/templates/navbar.html -->
<div id="sidebar-col" class="col-lg-4">
<div id="sidebar" class="sidebar">
<nav aria-labelledby="flush-headingEleven"><button role="button" aria-label="close menu" alt="close menu" aria-expanded="true" aria-controls="sidebar" class="collapse-toggle" data-collapse="Collapse " data-episodes="Episodes ">
<i class="search-icon" data-feather="x" role="img"></i>
</button>
<div class="sidebar-inner">
<div class="row mobile-row" id="theme-row-mobile">
<div class="col" id="theme-selector">
<li class="nav-item dropdown" id="theme-button-list">
<button class="btn btn-link nav-link px-0 px-lg-2 dropdown-toggle d-flex align-items-center" id="bd-theme" type="button" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static" aria-label="Toggle theme (auto)">
<svg class="bi my-1 theme-icon-active"><use href="#circle-half"></use></svg><span class="d-lg-none ms-1" id="bd-theme-text">Toggle Theme</span>
</button>
<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="bd-theme-text"><li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#sun-fill"></use></svg>
Light
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<svg class="bi me-2 theme-icon"><use href="#moon-stars-fill"></use></svg>
Dark
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
<li>
<button type="button" class="btn dropdown-item d-flex align-items-center active" data-bs-theme-value="auto" aria-pressed="true">
<svg class="bi me-2 theme-icon"><use href="#circle-half"></use></svg>
Auto
<svg class="bi ms-auto d-none"><use href="#check2"></use></svg></button>
</li>
</ul></li>
</div>
</div>
<div class="row mobile-row">
<div class="col">
<div class="sidenav-view-selector">
<div class="accordion accordion-flush" id="accordionFlush9">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingNine">
<button class="accordion-button collapsed" id="instructor" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseNine" aria-expanded="false" aria-controls="flush-collapseNine">
<i id="eye" aria-hidden="true" class="icon" data-feather="eye"></i> Learner View
</button>
</h2>
<div id="flush-collapseNine" class="accordion-collapse collapse" aria-labelledby="flush-headingNine" data-bs-parent="#accordionFlush2">
<div class="accordion-body">
<a href="instructor/07-find.html">Instructor View</a>
</div>
</div>
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
</div><!--div.sidenav-view-selector -->
</div><!--/div.col -->
<hr></div><!--/div.mobile-row -->
<div class="accordion accordion-flush" id="accordionFlush11">
<div class="accordion-item">
<button id="chapters" class="accordion-button show" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseEleven" aria-expanded="false" aria-controls="flush-collapseEleven">
<h2 class="accordion-header chapters" id="flush-headingEleven">
EPISODES
</h2>
</button>
<div id="flush-collapseEleven" class="accordion-collapse show collapse" aria-labelledby="flush-headingEleven" data-bs-parent="#accordionFlush11">
<div class="accordion-body">
<div class="accordion accordion-flush" id="accordionFlush1">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading1">
<a href="index.html">Summary and Setup</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush2">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading2">
<a href="01-intro.html">1. Introducing the Shell</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush3">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading3">
<a href="02-filedir.html">2. Navigating Files and Directories</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush4">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading4">
<a href="03-create.html">3. Working With Files and Directories</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush5">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading5">
<a href="04-pipefilter.html">4. Pipes and Filters</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush6">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading6">
<a href="05-loop.html">5. Loops</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush7">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading7">
<a href="06-script.html">6. Shell Scripts</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlushcurrent">
<div class="accordion-item">
<div class="accordion-header" id="flush-headingcurrent">
<span class="visually-hidden">Current Chapter</span>
<span class="current-chapter">
7. Finding Things
</span>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
</div>
</div>
</div>
<hr class="half-width"><div class="accordion accordion-flush lesson-resources" id="accordionFlush12">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingTwelve">
<button class="accordion-button collapsed" id="lesson-resources" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwelve" aria-expanded="false" aria-controls="flush-collapseTwelve">
RESOURCES
</button>
</h2>
<div id="flush-collapseTwelve" class="accordion-collapse collapse" aria-labelledby="flush-headingTwelve" data-bs-parent="#accordionFlush12">
<div class="accordion-body">
<ul><li>
<a href="key-points.html">Key Points</a>
</li>
<li>
<a href="reference.html#glossary">Glossary</a>
</li>
<li>
<a href="profiles.html">Learner Profiles</a>
</li>
<li><a href="discuss.html">Discussion</a></li><li><a href="reference.html">Summary of Basic Commands</a></li>
</ul></div>
</div>
</div>
</div>
<hr class="half-width lesson-resources"><a href="aio.html">See all in one page</a>
<hr class="d-none d-sm-block d-md-none"><div class="d-grid gap-1">
</div>
</div><!-- /div.accordion -->
</div><!-- /div.sidebar-inner -->
</nav></div><!-- /div.sidebar -->
</div><!-- /div.sidebar-col -->
<!-- END: inst/pkgdown/templates/navbar.html-->
<!-- START: inst/pkgdown/templates/content-instructor.html -->
<div class="col-xl-8 col-lg-12 primary-content">
<nav class="lesson-content mx-md-4" aria-label="Previous and Next Chapter"><!-- content for small screens --><div class="d-block d-sm-block d-md-none">
<a class="chapter-link" href="06-script.html"><i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>Previous</a>
<a class="chapter-link float-end" href="index.html">Next<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i></a>
</div>
<!-- content for large screens -->
<div class="d-none d-sm-none d-md-block">
<a class="chapter-link" href="06-script.html" rel="prev">
<i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>
Previous: Shell Scripts
</a>
<a class="chapter-link float-end" href="index.html" rel="next">
Home
<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i>
</a>
</div>
<hr></nav><main id="main-content" class="main-content"><div class="container lesson-content">
<h1>Finding Things</h1>
<p>Last updated on 2023-11-09 |
<a href="https://github.com/swcarpentry/shell-novice/edit/main/episodes/07-find.md" class="external-link">Edit this page <i aria-hidden="true" data-feather="edit"></i></a></p>
<div class="text-end">
<button role="button" aria-pressed="false" tabindex="0" id="expand-code" class="pull-right" data-expand="Expand All Solutions " data-collapse="Collapse All Solutions "> Expand All Solutions <i aria-hidden="true" data-feather="plus"></i></button>
</div>
<div class="overview card">
<h2 class="card-header">Overview</h2>
<div class="row g-0">
<div class="col-md-4">
<div class="card-body">
<div class="inner">
<h3 class="card-title">Questions</h3>
<ul><li>How can I find files?</li>
<li>How can I find things in files?</li>
</ul></div>
</div>
</div>
<div class="col-md-8">
<div class="card-body">
<div class="inner bordered">
<h3 class="card-title">Objectives</h3>
<ul><li>Use <code>grep</code> to select lines from text files that match
simple patterns.</li>
<li>Use <code>find</code> to find files and directories whose names
match simple patterns.</li>
<li>Use the output of one command as the command-line argument(s) to
another command.</li>
<li>Explain what is meant by ‘text’ and ‘binary’ files, and why many
common tools don’t handle the latter well.</li>
</ul></div>
</div>
</div>
</div>
</div>
<p>In the same way that many of us now use ‘Google’ as a verb meaning
‘to find’, Unix programmers often use the word ‘grep’. ‘grep’ is a
contraction of ‘global/regular expression/print’, a common sequence of
operations in early Unix text editors. It is also the name of a very
useful command-line program.</p>
<p><code>grep</code> finds and prints lines in files that match a
pattern. For our examples, we will use a file that contains three haiku
taken from a <a href="https://web.archive.org/web/19991201042211/http://salon.com/21st/chal/1998/01/26chal.html" class="external-link">1998
competition</a> in <em>Salon</em> magazine (Credit to authors Bill
Torcaso, Howard Korder, and Margaret Segall, respectively. See Haiku
Error Messsages archived <a href="https://web.archive.org/web/20000310061355/http://www.salon.com/21st/chal/1998/02/10chal2.html" class="external-link">Page
1</a> and <a href="https://web.archive.org/web/20000229135138/http://www.salon.com/21st/chal/1998/02/10chal3.html" class="external-link">Page
2</a> .). For this set of examples, we’re going to be working in the
writing subdirectory:</p>
<div class="codewrapper sourceCode" id="cb1">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="ex">$</span> cd</span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="ex">$</span> cd Desktop/shell-lesson-data/exercise-data/writing</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="ex">$</span> cat haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>The Tao that is seen
Is not the true Tao, until
You bring fresh toner.
With searching comes loss
and the presence of absence:
"My Thesis" not found.
Yesterday it worked
Today it is not working
Software is like that.</code></pre>
</div>
<p>Let’s find lines that contain the word ‘not’:</p>
<div class="codewrapper sourceCode" id="cb3">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="ex">$</span> grep not haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>Is not the true Tao, until
"My Thesis" not found
Today it is not working</code></pre>
</div>
<p>Here, <code>not</code> is the pattern we’re searching for. The grep
command searches through the file, looking for matches to the pattern
specified. To use it type <code>grep</code>, then the pattern we’re
searching for and finally the name of the file (or files) we’re
searching in.</p>
<p>The output is the three lines in the file that contain the letters
‘not’.</p>
<p>By default, grep searches for a pattern in a case-sensitive way. In
addition, the search pattern we have selected does not have to form a
complete word, as we will see in the next example.</p>
<p>Let’s search for the pattern: ‘The’.</p>
<div class="codewrapper sourceCode" id="cb5">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="ex">$</span> grep The haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>The Tao that is seen
"My Thesis" not found.</code></pre>
</div>
<p>This time, two lines that include the letters ‘The’ are outputted,
one of which contained our search pattern within a larger word,
‘Thesis’.</p>
<p>To restrict matches to lines containing the word ‘The’ on its own, we
can give <code>grep</code> the <code>-w</code> option. This will limit
matches to word boundaries.</p>
<p>Later in this lesson, we will also see how we can change the search
behavior of grep with respect to its case sensitivity.</p>
<div class="codewrapper sourceCode" id="cb7">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-w</span> The haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>The Tao that is seen</code></pre>
</div>
<p>Note that a ‘word boundary’ includes the start and end of a line, so
not just letters surrounded by spaces. Sometimes we don’t want to search
for a single word, but a phrase. We can also do this with
<code>grep</code> by putting the phrase in quotes.</p>
<div class="codewrapper sourceCode" id="cb9">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-w</span> <span class="st">"is not"</span> haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>Today it is not working</code></pre>
</div>
<p>We’ve now seen that you don’t have to have quotes around single
words, but it is useful to use quotes when searching for multiple words.
It also helps to make it easier to distinguish between the search term
or phrase and the file being searched. We will use quotes in the
remaining examples.</p>
<p>Another useful option is <code>-n</code>, which numbers the lines
that match:</p>
<div class="codewrapper sourceCode" id="cb11">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-n</span> <span class="st">"it"</span> haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>5:With searching comes loss
9:Yesterday it worked
10:Today it is not working</code></pre>
</div>
<p>Here, we can see that lines 5, 9, and 10 contain the letters
‘it’.</p>
<p>We can combine options (i.e. flags) as we do with other Unix
commands. For example, let’s find the lines that contain the word ‘the’.
We can combine the option <code>-w</code> to find the lines that contain
the word ‘the’ and <code>-n</code> to number the lines that match:</p>
<div class="codewrapper sourceCode" id="cb13">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-n</span> <span class="at">-w</span> <span class="st">"the"</span> haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>2:Is not the true Tao, until
6:and the presence of absence:</code></pre>
</div>
<p>Now we want to use the option <code>-i</code> to make our search
case-insensitive:</p>
<div class="codewrapper sourceCode" id="cb15">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-n</span> <span class="at">-w</span> <span class="at">-i</span> <span class="st">"the"</span> haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>1:The Tao that is seen
2:Is not the true Tao, until
6:and the presence of absence:</code></pre>
</div>
<p>Now, we want to use the option <code>-v</code> to invert our search,
i.e., we want to output the lines that do not contain the word
‘the’.</p>
<div class="codewrapper sourceCode" id="cb17">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-n</span> <span class="at">-w</span> <span class="at">-v</span> <span class="st">"the"</span> haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>1:The Tao that is seen
3:You bring fresh toner.
4:
5:With searching comes loss
7:"My Thesis" not found.
8:
9:Yesterday it worked
10:Today it is not working
11:Software is like that.</code></pre>
</div>
<p>If we use the <code>-r</code> (recursive) option, <code>grep</code>
can search for a pattern recursively through a set of files in
subdirectories.</p>
<p>Let’s search recursively for <code>Yesterday</code> in the
<code>shell-lesson-data/exercise-data/writing</code> directory:</p>
<div class="codewrapper sourceCode" id="cb19">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-r</span> Yesterday .</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>./LittleWomen.txt:"Yesterday, when Aunt was asleep and I was trying to be as still as a
./LittleWomen.txt:Yesterday at dinner, when an Austrian officer stared at us and then
./LittleWomen.txt:Yesterday was a quiet day spent in teaching, sewing, and writing in my
./haiku.txt:Yesterday it worked</code></pre>
</div>
<p><code>grep</code> has lots of other options. To find out what they
are, we can type:</p>
<div class="codewrapper sourceCode" id="cb21">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">--help</span></span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c
Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:
... ... ...</code></pre>
</div>
<div id="using-grep" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="using-grep" class="callout-inner">
<h3 class="callout-title">Using <code>grep</code>
</h3>
<div class="callout-content">
<p>Which command would result in the following output:</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>and the presence of absence:</code></pre>
</div>
<ol style="list-style-type: decimal"><li><code>grep "of" haiku.txt</code></li>
<li><code>grep -E "of" haiku.txt</code></li>
<li><code>grep -w "of" haiku.txt</code></li>
<li><code>grep -i "of" haiku.txt</code></li>
</ol></div>
</div>
</div>
<div id="accordionSolution1" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution1" aria-expanded="false" aria-controls="collapseSolution1">
<h4 class="accordion-header" id="headingSolution1"> Show me the solution </h4>
</button>
<div id="collapseSolution1" class="accordion-collapse collapse" data-bs-parent="#accordionSolution1" aria-labelledby="headingSolution1">
<div class="accordion-body">
<p>The correct answer is 3, because the <code>-w</code> option looks
only for whole-word matches. The other options will also match ‘of’ when
part of another word.</p>
</div>
</div>
</div>
</div>
<div id="wildcards" class="callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="wildcards" class="callout-inner">
<h3 class="callout-title">Wildcards</h3>
<div class="callout-content">
<p><code>grep</code>‘s real power doesn’t come from its options, though;
it comes from the fact that patterns can include wildcards. (The
technical name for these is <strong>regular expressions</strong>, which
is what the ’re’ in ‘grep’ stands for.) Regular expressions are both
complex and powerful; if you want to do complex searches, please look at
the lesson on <a href="https://librarycarpentry.org/lc-data-intro/01-regular-expressions.html" class="external-link">our
website</a>. As a taster, we can find lines that have an ‘o’ in the
second position like this:</p>
<div class="codewrapper sourceCode" id="cb24">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="at">-E</span> <span class="st">"^.o"</span> haiku.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>You bring fresh toner.
Today it is not working
Software is like that.</code></pre>
</div>
<p>We use the <code>-E</code> option and put the pattern in quotes to
prevent the shell from trying to interpret it. (If the pattern contained
a <code>*</code>, for example, the shell would try to expand it before
running <code>grep</code>.) The <code>^</code> in the pattern anchors
the match to the start of the line. The <code>.</code> matches a single
character (just like <code>?</code> in the shell), while the
<code>o</code> matches an actual ‘o’.</p>
</div>
</div>
</div>
<div id="tracking-a-species" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="tracking-a-species" class="callout-inner">
<h3 class="callout-title">Tracking a Species</h3>
<div class="callout-content">
<p>Leah has several hundred data files saved in one directory, each of
which is formatted like this:</p>
<pre class="source"><code>2012-11-05,deer,5
2012-11-05,rabbit,22
2012-11-05,raccoon,7
2012-11-06,rabbit,19
2012-11-06,deer,2
2012-11-06,fox,4
2012-11-07,rabbit,16
2012-11-07,bear,1</code></pre>
<p>She wants to write a shell script that takes a species as the first
command-line argument and a directory as the second argument. The script
should return one file called <code><species>.txt</code>
containing a list of dates and the number of that species seen on each
date. For example using the data shown above, <code>rabbit.txt</code>
would contain:</p>
<pre class="source"><code>2012-11-05,22
2012-11-06,19
2012-11-07,16</code></pre>
<p>Below, each line contains an individual command, or pipe. Arrange
their sequence in one command in order to achieve Leah’s goal:</p>
<div class="codewrapper sourceCode" id="cb28">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a><span class="fu">cut</span> <span class="at">-d</span> : <span class="at">-f</span> 2</span>
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a><span class="op">></span></span>
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a><span class="kw">|</span></span>
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a><span class="fu">grep</span> <span class="at">-w</span> <span class="va">$1</span> <span class="at">-r</span> <span class="va">$2</span></span>
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a><span class="kw">|</span></span>
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a><span class="va">$1</span><span class="ex">.txt</span></span>
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a><span class="fu">cut</span> <span class="at">-d</span> , <span class="at">-f</span> 1,3</span></code></pre>
</div>
<p>Hint: use <code>man grep</code> to look for how to grep text
recursively in a directory and <code>man cut</code> to select more than
one field in a line.</p>
<p>An example of such a file is provided in
<code>shell-lesson-data/exercise-data/animal-counts/animals.csv</code></p>
</div>
</div>
</div>
<div id="accordionSolution2" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution2" aria-expanded="false" aria-controls="collapseSolution2">
<h4 class="accordion-header" id="headingSolution2"> Show me the solution </h4>
</button>
<div id="collapseSolution2" class="accordion-collapse collapse" data-bs-parent="#accordionSolution2" aria-labelledby="headingSolution2">
<div class="accordion-body">
<pre class="source"><code>grep -w $1 -r $2 | cut -d : -f 2 | cut -d , -f 1,3 > $1.txt</code></pre>
<p>Actually, you can swap the order of the two cut commands and it still
works. At the command line, try changing the order of the cut commands,
and have a look at the output from each step to see why this is the
case.</p>
<p>You would call the script above like this:</p>
<div class="codewrapper sourceCode" id="cb30">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a><span class="ex">$</span> bash count-species.sh bear .</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div id="little-women" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="little-women" class="callout-inner">
<h3 class="callout-title">Little Women</h3>
<div class="callout-content">
<p>You and your friend, having just finished reading <em>Little
Women</em> by Louisa May Alcott, are in an argument. Of the four sisters
in the book, Jo, Meg, Beth, and Amy, your friend thinks that Jo was the
most mentioned. You, however, are certain it was Amy. Luckily, you have
a file <code>LittleWomen.txt</code> containing the full text of the
novel
(<code>shell-lesson-data/exercise-data/writing/LittleWomen.txt</code>).
Using a <code>for</code> loop, how would you tabulate the number of
times each of the four sisters is mentioned?</p>
<p>Hint: one solution might employ the commands <code>grep</code> and
<code>wc</code> and a <code>|</code>, while another might utilize
<code>grep</code> options. There is often more than one way to solve a
programming task, so a particular solution is usually chosen based on a
combination of yielding the correct result, elegance, readability, and
speed.</p>
</div>
</div>
</div>
<div id="accordionSolution3" class="accordion challenge-accordion accordion-flush">
<div class="accordion-item">
<button class="accordion-button solution-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseSolution3" aria-expanded="false" aria-controls="collapseSolution3">
<h4 class="accordion-header" id="headingSolution3"> Solutions </h4>
</button>
<div id="collapseSolution3" class="accordion-collapse collapse" data-bs-parent="#accordionSolution3" aria-labelledby="headingSolution3">
<div class="accordion-body">
<pre class="source"><code>for sis in Jo Meg Beth Amy
do
echo $sis:
grep -ow $sis LittleWomen.txt | wc -l
done</code></pre>
<p>Alternative, slightly inferior solution:</p>
<pre class="source"><code>for sis in Jo Meg Beth Amy
do
echo $sis:
grep -ocw $sis LittleWomen.txt
done</code></pre>
<p>This solution is inferior because <code>grep -c</code> only reports
the number of lines matched. The total number of matches reported by
this method will be lower if there is more than one match per line.</p>
<p>Perceptive observers may have noticed that character names sometimes
appear in all-uppercase in chapter titles (e.g. ‘MEG GOES TO VANITY
FAIR’). If you wanted to count these as well, you could add the
<code>-i</code> option for case-insensitivity (though in this case, it
doesn’t affect the answer to which sister is mentioned most
frequently).</p>
</div>
</div>
</div>
</div>
<p>While <code>grep</code> finds lines in files, the <code>find</code>
command finds files themselves. Again, it has a lot of options; to show
how the simplest ones work, we’ll use the
<code>shell-lesson-data/exercise-data</code> directory tree shown
below.</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>.
├── animal-counts/
│ └── animals.csv
├── creatures/
│ ├── basilisk.dat
│ ├── minotaur.dat
│ └── unicorn.dat
├── numbers.txt
├── alkanes/
│ ├── cubane.pdb
│ ├── ethane.pdb
│ ├── methane.pdb
│ ├── octane.pdb
│ ├── pentane.pdb
│ └── propane.pdb
└── writing/
├── haiku.txt
└── LittleWomen.txt</code></pre>
</div>
<p>The <code>exercise-data</code> directory contains one file,
<code>numbers.txt</code> and four directories:
<code>animal-counts</code>, <code>creatures</code>, <code>alkanes</code>
and <code>writing</code> containing various files.</p>
<p>For our first command, let’s run <code>find .</code> (remember to run
this command from the <code>shell-lesson-data/exercise-data</code>
folder).</p>
<div class="codewrapper sourceCode" id="cb34">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb34-1"><a href="#cb34-1" tabindex="-1"></a><span class="ex">$</span> find .</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">.</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">writing</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">writing</span><span class="op">/</span><span class="va">LittleWomen.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">writing</span><span class="op">/</span><span class="va">haiku.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span><span class="op">/</span><span class="va">basilisk.dat</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span><span class="op">/</span><span class="va">unicorn.dat</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span><span class="op">/</span><span class="va">minotaur.dat</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">animal</span><span class="op">-</span><span class="va">counts</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">animal</span><span class="op">-</span><span class="va">counts</span><span class="op">/</span><span class="va">animals.csv</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">numbers.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">ethane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">propane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">octane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">pentane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">methane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">cubane.pdb</span></span></code></pre>
</div>
<p>As always, the <code>.</code> on its own means the current working
directory, which is where we want our search to start.
<code>find</code>’s output is the names of every file
<strong>and</strong> directory under the current working directory. This
can seem useless at first but <code>find</code> has many options to
filter the output and in this lesson we will discover some of them.</p>
<p>The first option in our list is <code>-type d</code> that means
‘things that are directories’. Sure enough, <code>find</code>’s output
is the names of the five directories (including <code>.</code>):</p>
<div class="codewrapper sourceCode" id="cb36">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb36-1"><a href="#cb36-1" tabindex="-1"></a><span class="ex">$</span> find . <span class="at">-type</span> d</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">.</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">writing</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">animal</span><span class="op">-</span><span class="va">counts</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span></span></code></pre>
</div>
<p>Notice that the objects <code>find</code> finds are not listed in any
particular order. If we change <code>-type d</code> to
<code>-type f</code>, we get a listing of all the files instead:</p>
<div class="codewrapper sourceCode" id="cb38">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb38-1"><a href="#cb38-1" tabindex="-1"></a><span class="ex">$</span> find . <span class="at">-type</span> f</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">.</span><span class="op">/</span><span class="va">writing</span><span class="op">/</span><span class="va">LittleWomen.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">writing</span><span class="op">/</span><span class="va">haiku.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span><span class="op">/</span><span class="va">basilisk.dat</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span><span class="op">/</span><span class="va">unicorn.dat</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">creatures</span><span class="op">/</span><span class="va">minotaur.dat</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">animal</span><span class="op">-</span><span class="va">counts</span><span class="op">/</span><span class="va">animals.csv</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">numbers.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">ethane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">propane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">octane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">pentane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">methane.pdb</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">alkanes</span><span class="op">/</span><span class="va">cubane.pdb</span></span></code></pre>
</div>
<p>Now let’s try matching by name:</p>
<div class="codewrapper sourceCode" id="cb40">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb40-1"><a href="#cb40-1" tabindex="-1"></a><span class="ex">$</span> find . <span class="at">-name</span> <span class="pp">*</span>.txt</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">.</span><span class="op">/</span><span class="va">numbers.txt</span></span></code></pre>
</div>
<p>We expected it to find all the text files, but it only prints out
<code>./numbers.txt</code>. The problem is that the shell expands
wildcard characters like <code>*</code> <em>before</em> commands run.
Since <code>*.txt</code> in the current directory expands to
<code>./numbers.txt</code>, the command we actually ran was:</p>
<div class="codewrapper sourceCode" id="cb42">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb42-1"><a href="#cb42-1" tabindex="-1"></a><span class="ex">$</span> find . <span class="at">-name</span> numbers.txt</span></code></pre>
</div>
<p><code>find</code> did what we asked; we just asked for the wrong
thing.</p>
<p>To get what we want, let’s do what we did with <code>grep</code>: put
<code>*.txt</code> in quotes to prevent the shell from expanding the
<code>*</code> wildcard. This way, <code>find</code> actually gets the
pattern <code>*.txt</code>, not the expanded filename
<code>numbers.txt</code>:</p>
<div class="codewrapper sourceCode" id="cb43">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb43-1"><a href="#cb43-1" tabindex="-1"></a><span class="ex">$</span> find . <span class="at">-name</span> <span class="st">"*.txt"</span></span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">.</span><span class="op">/</span><span class="va">writing</span><span class="op">/</span><span class="va">LittleWomen.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">writing</span><span class="op">/</span><span class="va">haiku.txt</span></span>
<span><span class="va">.</span><span class="op">/</span><span class="va">numbers.txt</span></span></code></pre>
</div>
<div id="listing-vs.-finding" class="callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="listing-vs.-finding" class="callout-inner">
<h3 class="callout-title">Listing vs. Finding</h3>
<div class="callout-content">
<p><code>ls</code> and <code>find</code> can be made to do similar
things given the right options, but under normal circumstances,
<code>ls</code> lists everything it can, while <code>find</code>
searches for things with certain properties and shows them.</p>
</div>
</div>
</div>
<p>As we said earlier, the command line’s power lies in combining tools.
We’ve seen how to do that with pipes; let’s look at another technique.
As we just saw, <code>find . -name "*.txt"</code> gives us a list of all
text files in or below the current directory. How can we combine that
with <code>wc -l</code> to count the lines in all those files?</p>
<p>The simplest way is to put the <code>find</code> command inside
<code>$()</code>:</p>
<div class="codewrapper sourceCode" id="cb45">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb45-1"><a href="#cb45-1" tabindex="-1"></a><span class="ex">$</span> wc <span class="at">-l</span> <span class="va">$(</span><span class="fu">find</span> . <span class="at">-name</span> <span class="st">"*.txt"</span><span class="va">)</span></span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code> 21022 ./writing/LittleWomen.txt
11 ./writing/haiku.txt
5 ./numbers.txt
21038 total</code></pre>
</div>
<p>When the shell executes this command, the first thing it does is run
whatever is inside the <code>$()</code>. It then replaces the
<code>$()</code> expression with that command’s output. Since the output
of <code>find</code> is the three filenames
<code>./writing/LittleWomen.txt</code>,
<code>./writing/haiku.txt</code>, and <code>./numbers.txt</code>, the
shell constructs the command:</p>
<div class="codewrapper sourceCode" id="cb47">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb47-1"><a href="#cb47-1" tabindex="-1"></a><span class="ex">$</span> wc <span class="at">-l</span> ./writing/LittleWomen.txt ./writing/haiku.txt ./numbers.txt</span></code></pre>
</div>
<p>which is what we wanted. This expansion is exactly what the shell
does when it expands wildcards like <code>*</code> and <code>?</code>,
but lets us use any command we want as our own ‘wildcard’.</p>
<p>It’s very common to use <code>find</code> and <code>grep</code>
together. The first finds files that match a pattern; the second looks
for lines inside those files that match another pattern. Here, for
example, we can find txt files that contain the word “searching” by
looking for the string ‘searching’ in all the <code>.txt</code> files in
the current directory:</p>
<div class="codewrapper sourceCode" id="cb48">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb48-1"><a href="#cb48-1" tabindex="-1"></a><span class="ex">$</span> grep <span class="st">"searching"</span> <span class="va">$(</span><span class="fu">find</span> . <span class="at">-name</span> <span class="st">"*.txt"</span><span class="va">)</span></span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>./writing/LittleWomen.txt:sitting on the top step, affected to be searching for her book, but was
./writing/haiku.txt:With searching comes loss</code></pre>
</div>
<div id="matching-and-subtracting" class="callout challenge">
<div class="callout-square">
<i class="callout-icon" data-feather="zap"></i>
</div>
<div id="matching-and-subtracting" class="callout-inner">