find, and Names With Spaces
What this is. find walks a directory tree and reports everything matching a description.
`
find . -name "*.js"
`
Read it as: starting here, find everything whose name matches *.js. The first argument is where to start, and everything after describes what you want.
-name pattern matches the filename.
-type f restricts to files, -type d to directories.
✦ Quote the pattern, always
Write find . -name "*.js" with quotes rather than find . -name *.js without them. Without quotes the shell expands the wildcard first, against the current directory, and hands find a single already-resolved filename. It then searches for exactly that one name and you get a confusing partial answer. The quotes stop the shell touching it, so find receives the pattern itself and applies it at every level of the tree.
Names with spaces. A space separates arguments, so a file called my notes.txt looks like two arguments. Two ways round it:
cat "my notes.txt" quotes the whole name.
cat my\ notes.txt escapes the space with a backslash.
For anything you create yourself, prefer my-notes.txt or my_notes.txt. Spaces in filenames cause a small tax on every command you will ever run against them.
Mark this lesson complete
Signed in, your progress follows you to every device.