Unit 2 路 Basic Commands 路 Lesson 23 of 34
Grep 'n Stuff
grep
Searching manually through files and directories can get tedious fast, since the filesystem can seem huge at times! Luckily, there's a command that automatically parses through a file to find a specific string of text: grep.
Basic usage looks like this:
[user@linux]$ grep "cyber{" [file]
This searches for the string "cyber{" within the file you specified. (You might use this for CTFs and CyberAcad challenges to search for a specific flag format!)
grep also has a recursive mode, letting you traverse and go through an entire directory for any file containing that string, using the -r flag:
[user@linux]$ grep -r "cyber{" [directory]
Given that [directory] holds multiple files, grep will output which files contain "cyber{", if any.
When grep finds something in recursive mode, it tells you two things: the file the match is in, and the line it matched. That's the whole trick: you don't have to know which file holds what you're after, only what the text looks like.
Your turn
The challenge below drops you into an archive of an old lab server: fourteen files across a handful of nested directories, one of which still contains an API token nobody ever rotated.
You already know how the flag starts. Point grep at the whole archive and let it find the file for you.
馃挕 Try it the slow way first if you like:
cdinto a couple of the directories andcata few files. Then do it with onegrep, and the point of the command becomes pretty obvious.
Capture-the-flag challenge
Put the lesson to work on a challenge and submit the flag you find. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminal, the challenges and the checks that go with every lesson in Intro to Linux, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.