Linux find file names with given string recursively

LinuxStringFind

Linux Problem Overview


I'm on Ubuntu, and I'd like to find all files in the current directory and subdirectories whose name contains the string "John". I know that grep can match the content of the files, but I have no idea how to use it with file names.

Linux Solutions


Solution 1 - Linux

Use the find command,

find . -type f -name "*John*"

Solution 2 - Linux

The find command will take long time because it scans real files in file system.

The quickest way is using locate command, which will give result immediately:

locate "John"

If the command is not found, you need to install mlocate package and run updatedb command first to prepare the search database for the first time.

More detail here: https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab

Solution 3 - Linux

This is a very simple solution using the tree command in the directory you want to search for. -f shows the full file path and | is used to pipe the output of tree to grep to find the file containing the string filename in the name.

tree -f | grep filename

Solution 4 - Linux

use ack its simple. just type ack <string to be searched>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJJ BeckView Question on Stackoverflow
Solution 1 - LinuxRich AdamsView Answer on Stackoverflow
Solution 2 - LinuxthucnguyenView Answer on Stackoverflow
Solution 3 - LinuxcaylusView Answer on Stackoverflow
Solution 4 - LinuxAnnuView Answer on Stackoverflow