Command Line – Search

In this item I will go through a couple of the commands you can use to locate files on your system.

find

Surprisingly enough, to find some files you can run find, examples of usage are below

find -x / -name filename

this will search your boot volume for files entitled filename

find / -name filename

this will search all mounted volumes (including network shares) for the file named filename, this can take rather a long time so probably best to specify the volume you wish to search

find . -name ‘*filename*’

this searches the current directory and subdirectories for any files with filename anywhere in their name

grep

Search the contents of text files and print lines containing the word searched for

grep searchterm ~/Documents/filename

searches the file called filename stored in my Documents folder, for the text ‘searchterm’

grep -R searchterm ~/Documents

searches through all the files and folders in my Documents folder for ‘searchterm’

grep -Rl searchterm ~/Documents

searches through all the files and folders in my Documents folder for ‘searchterm’, but this time it will only list the file path, not the contents of the file matching the ‘searchterm’

grep daemon /var/log/system.log

searches the system log for entries that mention ‘daemon’

For more info on additional options, check out their manuals

man find

man grep

 

Command Line Navigation

A lot of people who have moved from Windows to Mac rarely dive into the Terminal to accomplish tasks, but if you are wondering how exactly you’d go about navigating the file system then I will run through some of the basic commands used to do this

pwd

type this to find out what directory you are currently in, and stands for print working directory

cd

used to change directory, below are some examples of how to use this

cd / will take you to the top folder of your system drive, also known as the root

cd .. will take you back one folder, so if you were in /Applications/Utilities, it would take you to /Applications/, if you want to go back 2 folders then you could type cd ../.. and 3 folders would be cd ../../.. and so on, but chances are you would not need to do this

cd  ~/ will take you to your home folder

Also, instead of typing the full folder name you can just type a few letters of the name and then press the tab key and it will autocomplete the folder/filename, providing the info is unique, so for example, typing cd /Users/$USER/Do and then pressing tab will not do anything as you have a Documents and a Downloads folder in that location, but typing cd /Users/$USER/Doc and then pressing tab would autocomplete the path to /Users/$USER/Documents

ls

used to list the files in a directory, see below for usage examples

ls -l will list the files in your current directory

ls -la will list all the files in a current directory, same as above but hidden files will now show up

ls -l /Volumes/Folder/Folder will list the files in that folder without changing you to that directory (/Volumes/Folder/Folder is just an example so probably won’t return anything if you type this in exactly as shown)

ls -l *.suffix will list all files in the folder with the suffix mentioned, so to find all the JPEGs in a folder you would type ls -l *.jpg

ls -l *partialname* is useful if you know part of the filename, but not the suffix or the full name, so to find a file with exam in the name you would type ls -l *exam*