1

Is there a bash command that can be used as Spotlight or an option with ls that only prints files containing certain text? I want to be able to open Terminal and type something ls -SOMEOPTION filename and find all the files that have filename in their name, and this option could be used with -R and it would work as a replacement for Spotlight.

1

To find all files which contain a certain string, you can use just grep on its own:

grep -R /path/to/directory "myword"

In the example above, "myword" is what you're looking for, and /path/to/directory is the directory you want to search through. -R tells grep to run a recursive search (i.e. to start at the path you specified, and go into each subdirectory in there).

EDIT

I've re-read your question and I think you're looking for how to find files that have a certain filename. For this use:

find /path/to/directory -name "*glob*" -print

where /path/to/directory is where you want to start your search (and find will also look in the subdirectories) and "*glob*" is a full or a partial file name that you want to look for. Please note, you can use wildcards (. and * for any single and any multiple characters match, respectively) in the glob. As a variation to this, you can use, as proposed by others:

find /path/to/directory -name "*glob*" -exec ls -la {} \;

in the above, what you put after the option -exec is any UNIX command with paramteres (in this case ls -la). The {} will be substituted by the file name find finds, and the \; is a termination string.

EDIT 2

Please note, the commands above give you similar results to Spotlight, but they don't use Spotlight at all.

If you'd like to use Spotlight from command line, you can use mdfind command, which is a command-line interface for Spotlight. Typing mdfind on its own will give you a quick help for using it with a few examples. man mdfind will give you a more detailed manual for it.

Essentially mdfind takes a search string (and optional modifiers) as it's argument and it's the same as you would type in Spotlight's text field when using it in a GUI.

5
 find . -type f -name "*filename*" -exec grep -il 'mywordinthefile' {} \;
  • find search for file recursively in a directory.

  • First parameters is the directory you want to look in. Here it's . so the current directory. You could use ~/Documents for example or any other directories.

  • -type f- option is to only return files, not directories.

  • -name option takes a pattern to find specifics filenames.

  • grep is used here to find the word/pattern you want in the files found by find.

  • I copied the command into Terminal, I replaced myword with "test" because I have a file called test.txt in the directory, but I get no output. – Arc676 Nov 7 '13 at 14:40
  • -exec grep is only needed if you ant to find something in the file. For the file name, replace the -name – Matthieu Riegler Nov 7 '13 at 14:42
0

To find files with a specific string in the filename, use

    find . -type f -name '*<filename>*' 2> /dev/null

Substitute <filename> with the string that you would like to look for in the filename. The redirection at the end is to avoid printing errors when certain files/folders are not accessible.


You can put this command in a shell script, called say, lsf that would contain the following:

find . -type f -name "*$1*" 2> /dev/null

Then make it executable using

chmod +x lsf

You can also put this in your ~/.bashrc or ~/.bash_profile file instead of a shell script as described above (the command is still named lsf here):

myls()
{
    find . -type f -name "*$1*" 2> /dev/null
}
alias lsf=myls

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .