Members Log In
Finding stuff on the Unix terminal
Posted by Hamman W Samuel on 2018-11-17 15:44:58 Go Back
On Linux or other Unix-based systems, the terminal can be a powerful interface to locating and searching for stuff on your hard drive. The visual interface and search box may have limited options. Ever tried looking for files with some text in them? Here’s two commands that I’ve found very useful on the terminal, and I actually don’t use the search box interface anymore. To find files by their names or wild cards, use:
find -iwholename “*name*.ext”
The quotes are where you can put in what you want to look for. This command will look in the current directory for files that end with “.ext” and contain “name” somewhere in their name. The –iwholename is just one of the many options in this command, and the -i part ensures the search is not case sensitive. For more info on the find command, here’s a link to the online man pages: http://bit.ly/qNtN93 To find files by searching what’s inside them, use:
grep -ilr “sometext” ./
I am assuming these are text files, but there’s more complex options for searching binary files too. Have a look at the online man pages of the grep command for more options: http://bit.ly/p53tsq The -lir options together give us a nice and tidy output. The -l part prints just the file names in which “sometext” was found, while the -i part makes searching case insensitive, and the -r part allows looking at sub-directories. The final “./” implies the current directory as the starting point for searching.
Notes to Self
This is tested on my Debian virtual OS, does it work on other distros? How do I make find search in sub-directories? Can I make find and grep work together (i.e. piping, etc) so I can search for only file types that have a certain keyword?