🎓 A1Learner
🔍
0 of 50 lessons completed

Shell Finding Files

Finding a file means searching folders for a file by its name. The find command searches a folder and all the folders inside it.

Find one file by name

find searches the current folder and all inside it.

find . -name "notes.txt" | sort

Find all text files in a folder

*.txt matches every file whose name ends in .txt.

find docs -name "*.txt" | sort

Find all photos

*.jpg matches every file whose name ends in .jpg.

find photos -name "*.jpg" | sort

Find a file type

*.csv matches every file whose name ends in .csv.

find . -name "*.csv" | sort

Example

find . -name "*.txt" | sort

Key points

  • find: a command that searches for files by name.
  • Use find with a folder name to search that folder and all inside it.
  • Use -name with a pattern in quotes to search for a name.
  • A pattern is a name with * in it, and * matches any letters.
  • sort: a command that puts lines in order.
  • Send the find output to sort with |, so the order stays the same every time.

In this example, find searches the current folder and all inside it. The pattern "*.txt" matches every file whose name ends in .txt. Then sort puts the five lines in the same order every time.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command finds every .txt file under the current folder?