One of those handy things about Finder (or other file browsers if you're not on a Mac), is that you can easily search for the contents of files.
How can you search within files without a handy little magnifying glass? 🤔
There's a goofy-sounding command called grep
that will do just that.
Grep
grep
lets you look for a certain text within a file (or multiple files). The usage for this is less obvious when you're not working on a big code project.
Nonetheless, it will come in handy one day for you. grep takes two arguments:
the text for which you're looking
the file(s) in which you want to look
To demonstrate this concept, I'll create a text file that contains an excerpt from the Wikipedia article on Edward Hopper using a bonus command here, echo
.
Running echo
plus text in quotation marks, followed by a >
and a filename, will add the text to the file you specify (and create the file if it doesn't already exist)! ✨
Here's my enormous article shoved into quotation marks and fed to echo
:
echo "Edward Hopper (July 22, 1882 – May 15, 1967) was a prominent American realist painter and printmaker.
While he was most popularly known for his oil paintings, he was equally proficient as a watercolorist and printmaker in etching.
Both in his urban and rural scenes, his spare and finely calculated renderings reflected his personal vision of modern American life.
Always reluctant to discuss himself and his art, Hopper simply said, 'The whole answer is there on the canvas.'
Hopper was stoic and fatalistic—a quiet introverted man with a gentle sense of humor and a frank manner.
Hopper was someone drawn to an emblematic, anti-narrative symbolism, who 'painted short isolated moments of configuration, saturated with suggestion'.
His silent spaces and uneasy encounters 'touch us where we are most vulnerable', and have 'a suggestion of melancholy, that melancholy being enacted'.
His sense of color revealed him as a pure painter as he 'turned the Puritan into the purist, in his quiet canvasses where blemishes and blessings balance'.
According to critic Lloyd Goodrich, he was 'an eminently native painter, who more than any other was getting more of the quality of America into his canvases'.
Conservative in politics and social matters (Hopper asserted for example that 'artists' lives should be written by people very close to them'), he accepted things as they were and displayed a lack of idealism.
Cultured and sophisticated, he was well-read, and many of his paintings show figures reading.
He was generally good company and unperturbed by silences, though sometimes taciturn, grumpy, or detached. He was always serious about his art and the art of others, and when asked would return frank opinions.
Hopper's most systematic declaration of his philosophy as an artist was given in a handwritten note, entitled 'Statement', submitted in 1953 to the journal, Reality:
Great art is the outward expression of an inner life in the artist, and this inner life will result in his personal vision of the world.
No amount of skillful invention can replace the essential element of imagination.
One of the weaknesses of much abstract painting is the attempt to substitute the inventions of the human intellect for a private imaginative conception.
The inner life of a human being is a vast and varied realm and does not concern itself alone with stimulating arrangements of color, form and design.
The term life used in art is something not to be held in contempt, for it implies all of existence and the province of art is to react to it and not to shun it.
Painting will have to deal more fully and less obliquely with life and nature's phenomena before it can again become great." > hopper.txt
This Hopper article is a long text but will allow us to have some good results from grep
.
Remember, you have to specify:
the text for which you're looking
the file(s) in which you want to look
In this case, you'd want the word art, and the file hopper.txt.
Let grep where the word "art" appears in the article:
grep art hopper.txt
grep
will return the lines that contain the content you seek, which is why our output is a fraction of the original article:
Always reluctant to discuss himself and his art, Hopper simply said, 'The whole answer is there on the canvas.'
Conservative in politics and social matters (Hopper asserted for example that 'artists' lives should be written by people very close to them'), he accepted things as they were and displayed a lack of idealism.
He was generally good company and unperturbed by silences, though sometimes taciturn, grumpy, or detached. He was always serious about his art and the art of others, and when asked would return frank opinions.
Hopper's most systematic declaration of his philosophy as an artist was given in a handwritten note, entitled 'Statement', submitted in 1953 to the journal, Reality:
Great art is the outward expression of an inner life in the artist, and this inner life will result in his personal vision of the world.
The term life used in art is something not to be held in contempt, for it implies all of existence and the province of art is to react to it and not to shun it.
grep
can be customized depending on the type of project on which you're working. If you want to go further with grep, here is a larger resource on how to do so.
Conclusion
That was the last command you ran in this course! Here's a recap of everything you've learned:
pwd
stands for "print working directory." Terminal will tell you which folder you're currently in.cd
stands for "change directory." Pass it the name of another folder, and the terminal will move you to that folder.ls
stands for "list." Running this command will show the contents of your current folder.mkdir
stands for "make directory." Running this command, plus the name of your desired new folder, will create a new folder.touch
plus the name of your desired new file, will create a new file.mv
stands for move. Running this command, plus the name of the content you want to move, plus the location where you want to move it, will move the content.cp
stands forcopy
. Running this command, plus the name of the content you want to copy, plus the location where you want to move it, will copy the content to another location.Several shortcuts allow you to be more nimble on the command line, like ctrl + e, ctrl + a, option + click, etc.
Congratulations on taking your first steps on the command line. Whether you keep these tips in your back pocket, or go further with them as you evolve in a potential tech career, they'll serve you well. 💻