22

I want to search for all files with the .sql extension in folders and sub-folders (recursive).

How can I do this?

4 Answers 4

38

Or...

find $directory_name -name \*.sql

For example

find / -name \*.sql

Or

find ~ \*.sql

(where ~ equates to /home/your_username/), or...

find /usr/local/share/ \*.sql

and so forth.

3

Run this in the command line:

cd / && find | grep '\.sql$'

Change '/' to the directory you want to search.

The find command is able to accomplish the task without grep (using extra options), but I find the above usage more convenient.

In order, the above command:

  1. Changes the current directory to the root directory (cd /)
  2. Lists all files and directories at and below the current directory (find)
  3. Filters the files and directories for anything that ends with '.sql' (| grep '\.sql$')
6
  • thanks, can you explain it? your using grep and passing its output to find?
    – Blankman
    Dec 11, 2011 at 1:50
  • Vice versa, actually. I'm using find and passing its output to grep. I added a more detailed explanation above.
    – Barbarrosa
    Dec 11, 2011 at 2:00
  • would this work on a Mac also? I tried 'find | grep '\.sql$' and it outputted the usage help info.
    – Blankman
    Dec 11, 2011 at 3:14
  • find is a powerful tool and you really do not need to pipe the output to grep. This is a nice link - content.hccfl.edu/pollock/unix/findcmd.htm
    – Panther
    Dec 11, 2011 at 3:52
  • 1
    find has already filter capabilities
    – enzotib
    Dec 11, 2011 at 12:04
1

I know that this is an old post, but i am pretty new at this and i've found an easy way to find all files of a certain extension in a directory and its children subdirectories. Well you first navigate to the parent Directory then find . -name '*.sql' and that will find you all files with .sql extension in the directories and its subdirectories.

In my case i wanted to delete all .xml files in the directory and its subdirectories, so what i did more is that i added remove as in here find . -name '*.xml' | xargs rm

Hope this help someone :)

1
  • 2
    find understands the -delete command. No need to involve rm. Or you can use the -exec command to invoke it directly without the detour through a pipe and xargs. Feb 23, 2018 at 14:41
0
locate -br \\.sql$ | egrep '^/folder/path/'

If it is not installed, then previously install it with:

sudo apt install mlocate
6
  • This requires an up-to-date mlocate.db instance which is not set up out-of-the box in Ubuntu (I blieve). Also, why not simply locate '/folder/path/*.sql'? Feb 23, 2018 at 11:45
  • @DavidFoersterI I tried as you specified but it doesn't work, I guess regex in locate has some different setting from regex used in egrep
    – Vzzarr
    Feb 23, 2018 at 13:42
  • locate matches glob expressions like /folder/path/*.sql, not regular expressions. Feb 23, 2018 at 14:37
  • sorry, but from man locate I get -r, --regexp REGEXP - Search for a basic regexp REGEXP so I assumed it was a regex. Anyway I tried your "global expression" but doesn't work, I'm sorry
    – Vzzarr
    Feb 23, 2018 at 15:19
  • 1
    Please read at least the first few sentences of the description before you start to cherry-pick from the option synposis: “If --regex is not specified, PATTERNs can contain globbing characters.” (source) Feb 24, 2018 at 1:21

You must log in to answer this question.

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