An Annoyance
Search on a Mac seems to be a real headache. It's slow. If you
search for that file BigFraction.py
, and you have a bunch of
different versions, finding the newest one is a kafkaesque journey into
endless point-and-click. ugh. Often you get fusillades of irrelevant
cruft that is laborious to sift through.
Turboharge Search with find
The command-line offers a cure: find
. The
find
command finds stuff with huge efficiency and specificity.
We will do an example on my system. I have opened a termianl and am now in my home directory.
unix> pwd /Users/morrison unix> ls 0323F.jsh comp node_modules 110 convert.sed ocw 20212022 convert.set p3 2022_2023 crypt.js package-lock.json 211 crypto package.json 96-1738.pdf data pandas Applications data.dat pension.txt CLASSES dataviz-with-python-and-js personal Classes20161017 dict_games.py personal2 Classes20172018 eclipse phone.txt Classes20182019 eclipse-workspace pictures2 Classes20192020 empty.html pix Classes20202021 endian powshift.py D3Book endian.c prime_meat.py Desktop env programming Documents env.c psvm Downloads file public_html HelloFX.java first.cbl public_html.tar LSPCobol first.js py4e Library foot pymit LifeCycle.java gone.txt recursion LinuxGuide.pdf google-cloud-sdk reeeet Math-for-Programmers gym.txt regex Movies hello restore_ports.tcl Music hello.cbl rhino.ico Person.java hello.cob rt.py Pictures hello.js rust Pictures.zip htmlCssCourse sample.aux Point image.php sample.pdf Point.java install-tl-20221121 sample.tex Point.py intsqrt.py scherzo.py Point.zip is_perfect.py schoolImages PracticeLP1.java jarred shell.html Public javaprep.sh show_all Sites junk show_all.c Stand.java leaker sigma.py Symmetry Test files.zip leaker.c stackHeap.py adventure leaker.dSYM startPage.html adventure.c learning stateConvert.sed aiPython letter.tex table_of_values.py amigocode logins.csv teaching api_key.txt lst test.awk apple.txt map.dump test.css assorted map.svg test.html bash mathbooks test.js bigFractionAssignment mess test_harder.py bin miles1.dat try_pygal.py book miniterm typescript-book books moreProgramming unix_dos_rosetta.html books.txt morrison update-tlmgr-latest.sh box morrison.c uses cc4e movies.txt words.txt challenge myports.txt youTube chararrays newEdition codingBooks nodeLists unix>
Frightening, isn't it?
For a cheap parlor trick, I am going to find all directories named students
in my home directory.
unix> find /Users/morrison -name students -type d /Users/morrison/public_html/1920T3/428/students /Users/morrison/public_html/miniterm/Miniterm2014/students /Users/morrison/CLASSES/20152016Classes/T2/LP2/students /Users/morrison/CLASSES/20152016Classes/T3/BF/students /Users/morrison/Classes20202021/S2/4240/students /Users/morrison/Classes20202021/S2/4280/students /Users/morrison/Classes20202021/S1/4240/students /Users/morrison/Classes20202021/S1/4260/students /Users/morrison/Classes20161017/T3/BC/students /Users/morrison/Classes20161017/T1/students /Users/morrison/Classes20182019/T2/352/students /Users/morrison/Classes20182019/T2/424/students /Users/morrison/Classes20182019/T2/426/students /Users/morrison/Classes20182019/T3/426/students /Users/morrison/Classes20182019/T3/428/students /Users/morrison/Classes20182019/T1/students /Users/morrison/Classes20192020/T2/424/students /Users/morrison/Classes20192020/T2/426/students /Users/morrison/Classes20192020/T2/402/students /Users/morrison/Classes20192020/T3/424/students /Users/morrison/Classes20192020/T3/426/students /Users/morrison/Classes20192020/T3/428/students /Users/morrison/Classes20192020/T1/424/students /Users/morrison/Classes20192020/T1/402/students /Users/morrison/pix/students /Users/morrison/Classes20172018/T2/352/students /Users/morrison/20212022/S2/4240/students /Users/morrison/20212022/S2/4280/students /Users/morrison/20212022/S1/4240/students /Users/morrison/20212022/S1/4020/students /Users/morrison/20212022/S1/students /Users/morrison/2022_2023/accelerator/students
No stupidity here. Here I have every dirctory named students
that lives in my home directory. I have the (useful) absolute path, too. We could
also do this.
unix> find . -name studnets -type d ./public_html/1920T3/428/students ./public_html/miniterm/Miniterm2014/students ./CLASSES/20152016Classes/T2/LP2/students ./CLASSES/20152016Classes/T3/BF/students ./Classes20202021/S2/4240/students ./Classes20202021/S2/4280/students ./Classes20202021/S1/4240/students ./Classes20202021/S1/4260/students ./Classes20161017/T3/BC/students ./Classes20161017/T1/students find: ./Library/Saved Application State/com.LaCie.LaCieSetup.savedState: Permission denied ./Classes20182019/T2/352/students ./Classes20182019/T2/424/students ./Classes20182019/T2/426/students ./Classes20182019/T3/426/students ./Classes20182019/T3/428/students ./Classes20182019/T1/students ./Classes20192020/T2/424/students ./Classes20192020/T2/426/students ./Classes20192020/T2/402/students ./Classes20192020/T3/424/students ./Classes20192020/T3/426/students ./Classes20192020/T3/428/students ./Classes20192020/T1/424/students ./Classes20192020/T1/402/students ./pix/students ./Classes20172018/T2/352/students ./20212022/S2/4240/students ./20212022/S2/4280/students ./20212022/S1/4240/students ./20212022/S1/4020/students ./20212022/S1/students ./2022_2023/accelerator/students
What happened here? The first item specified is a directory; in one case we used an
absolute path, the other we used the relative path .
(the current working
directory). The -name students
tells the OS to find files or
directories named students
. The -type d
tells the OS only to find
directories.
The command ls -lt
lists files in reverse chronologcal format in long format.
The -d
flag for ls
lists directories but not their contents.
Hey, let's see who's the last modified here.
drwxr-xr-x 5 morrison 1671898719 160 Jun 16 11:16 ./2022_2023/accelerator/students drwxr-xr-x 59 morrison 1671898719 1888 May 24 2022 ./20212022/S2/4240/students drwxr-xr-x 103 morrison 1671898719 3296 Mar 3 2022 ./20212022/S1/4240/students drwxr-xr-x 26 morrison 1671898719 832 Mar 2 2022 ./20212022/S2/4280/students drwxr-xr-x 34 morrison 1671898719 1088 Dec 14 2021 ./20212022/S1/4020/students drwxr-xr-x 20 morrison 1671898719 640 Oct 18 2021 ./Classes20172018/T2/352/students drwxr-xr-x 4 morrison 1671898719 128 Oct 13 2021 ./pix/students drwxr-xr-x 51 morrison 1671898719 1632 Oct 6 2021 ./Classes20161017/T3/BC/students drwxr-xr-x 26 morrison 1671898719 832 Oct 6 2021 ./Classes20202021/S2/4280/students drwxr-xr-x 6 morrison 1671898719 192 Oct 6 2021 ./Classes20182019/T2/352/students drwxr-xr-x 45 morrison 1671898719 1440 Sep 20 2021 ./Classes20202021/S1/4240/students drwxr-xr-x 3 morrison 1671898719 96 Sep 3 2021 ./20212022/S1/students drwxr-xr-x 23 morrison 1671898719 736 May 27 2021 ./Classes20182019/T2/426/students drwxr-xr-x 51 morrison 1671898719 1632 May 26 2021 ./Classes20202021/S2/4240/students drwxr-xr-x 20 morrison 1671898719 640 Dec 14 2020 ./Classes20202021/S1/4260/students drwxr-xr-x 34 morrison 1671898719 1088 May 26 2020 ./Classes20192020/T3/424/students drwxr-xr-x 30 morrison 1671898719 960 May 25 2020 ./Classes20192020/T3/426/students drwxr-xr-x 23 morrison 1671898719 736 May 25 2020 ./Classes20192020/T3/428/students drwxr-xr-x 25 morrison 1671898719 800 May 7 2020 ./Classes20182019/T2/424/students drwxr-xr-x 2 morrison staff 64 Mar 19 2020 ./public_html/1920T3/428/students drwxr-xr-x 30 morrison 1671898719 960 Feb 12 2020 ./Classes20192020/T2/402/students drwxr-xr-x 28 morrison 1671898719 896 Jan 14 2020 ./Classes20192020/T2/426/students drwxr-xr-x 23 morrison 1671898719 736 Dec 3 2019 ./Classes20192020/T2/424/students drwxr-xr-x 25 morrison 1671898719 800 Oct 30 2019 ./Classes20192020/T1/402/students drwxr-xr-x 29 morrison 1671898719 928 Oct 28 2019 ./Classes20192020/T1/424/students drwxr-xr-x 17 morrison 1671898719 544 Mar 22 2019 ./Classes20182019/T3/428/students drwxr-xr-x 21 morrison 1671898719 672 Mar 15 2019 ./Classes20182019/T3/426/students drwxr-xr-x 76 morrison 1671898719 2432 Feb 28 2019 ./Classes20182019/T1/students drwxr-xr-x 67 morrison 1671898719 2144 Sep 23 2016 ./Classes20161017/T1/students drwxr-xr-x 32 morrison 1671898719 1024 Mar 22 2016 ./CLASSES/20152016Classes/T3/BF/students drwxr-xr-x 47 morrison 1671898719 1504 Feb 15 2016 ./CLASSES/20152016Classes/T2/LP2/students drwxr-xr-x 26 morrison staff 832 Feb 24 2014 ./public_html/miniterm/Miniterm2014/students
What happened here? First of all, the + at the end of the line is necessary to end
the -exec
loop. The {}
walks thrugh eaach item that find
ferrets out and it executes the indicated command on it. We now know the last accessed
directory was accessed on 16 June 2023.
Find Command Specifiers | ||
---|---|---|
-name | Specify ad file/directory name. | |
-iname | Specify ad file/directory name, case insensitive.. | |
-type | Specify ad file/directory name. | |
d | find directories only | |
f | find regular files only | |
-exec | Do the command for each item foound |
Wildcarding with * and ?
You can also use * as a wildcard for any number of characters and ? for a single character.
Here I will find all .js
in my directory programming
. Note
use of single-quotes.
unix> find programming -name '*.js' programming/dataviz/sandpit/index.js programming/python/python/c2/js/libs/l10n.js programming/python/python/c2/js/app.js programming/python/python/c2/c2/js/libs/l10n.js programming/python/python/c2/c2/js/app.js programming/python/python/c2/c2/c2/js/libs/l10n.js programming/python/python/c2/c2/c2/js/app.js programming/css/checkers.js programming/css/jeopardy.js programming/css/grid1.js programming/js/stochastic.js programming/js/rest.js programming/js/months.js programming/js/rowz.js programming/js/0921FA.js programming/js/no.js programming/js/condon.js programming/js/fmt.js programming/js/final.js programming/js/TrigTable.js programming/js/0921F.js programming/js/name.js programming/js/0925F.js programming/js/first.js programming/js/sleep.js programming/js/product.js programming/web/script.js programming/typescript/test.js programming/typescript/hello.js programming/c/SisterMary/testMe_files/0aaba1df8137a44ae17ad2987b98e96aembedcompiled__en.js
You can limit the depth of the search.
unix> find programming -name '*.js' -depth 2 programming/css/checkers.js programming/css/jeopardy.js programming/css/grid1.js programming/js/stochastic.js programming/js/rest.js programming/js/months.js programming/js/rowz.js programming/js/0921FA.js programming/js/no.js programming/js/condon.js programming/js/fmt.js programming/js/final.js programming/js/TrigTable.js programming/js/0921F.js programming/js/name.js programming/js/0925F.js programming/js/first.js programming/js/sleep.js programming/js/product.js programming/web/script.js programming/typescript/test.js programming/typescript/hello.js
Hey let's find how how big these files are.
unix> find programming -name '*.js' -depth 2 -exec wc -c {} + 21 programming/css/checkers.js 21 programming/css/jeopardy.js 21 programming/css/grid1.js 248 programming/js/stochastic.js 354 programming/js/rest.js 1649 programming/js/months.js 21 programming/js/rowz.js 491 programming/js/0921FA.js 41 programming/js/no.js 604 programming/js/condon.js 286 programming/js/fmt.js 4686 programming/js/final.js 0 programming/js/TrigTable.js 302 programming/js/0921F.js 331 programming/js/name.js 150 programming/js/0925F.js 48 programming/js/first.js 208 programming/js/sleep.js 187 programming/js/product.js 1400 programming/web/script.js 57 programming/typescript/test.js 112 programming/typescript/hello.js 11238 total
Here are a couple of other fun examples. Any JS files out there with second letter 'a' and four letters? How about JS files containing 'ab'?
unix> find programming -name '?a??.js' programming/js/name.js unix> find programming -name '*ab*.js' programming/js/TrigTable.js