Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to search for files starting with some particular name. There can be multiple files starting with a particular pattern and I want to list all such files present in the directory.

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

To complete exisitng answers:

ls

The default directory list utility ls supports wildcard search. To search for all files with pattern abc:

ls -a abc*   # list all files starting with abc---
ls -a *abc*  # list all files containing --abc--
ls -a *abc   # list all files ending with --abc

Note that the file extension is relevant for the search results too.

tree Install banshee

In case we need to list files in a directory tree we can also issue tree to search for a given pattern like

tree -P abc*  # list directory tree of file starting with abc---
tree -l def*  # exclude files starting with def---
share|improve this answer
add comment

You can search for a particular pattern using the Nautilus file manager and regular expressions.

To do so, click on Select Items Matching in the Gear menu like below (you can also press Ctrl+s).

enter image description here

Then, just type the regular expression ABC* and validate.

enter image description here

Every file whose name matches your pattern will be automatically selected.

enter image description here

I'm using Nautilus 3.6.* from GNOME3 PPA on Ubuntu 12.10 (Quantal).

share|improve this answer
add comment

There are many ways to do it, depending on exactly what you want to do with them. Generally, if you want to just list them, you can do it in a terminal using:

find | grep '^\./ABC'

... and replacing ABC with your text.

To understand the command, let's break it down a bit:

  • find lists all files under the current directory and its sub-directories; using it alone will just list everything there. Note that find outputs each file or directory starting with ./, indicating that their path is relative to the current directory. Being aware of this is important because it means we will search for results starting with ./ABC and not just ABC.

  • The pipe character | redirects the output of one command to another, in this case the output of find is redirected to grep. This is called piping.

  • grep takes the output and filters it using the given pattern, ^\./ABC.

    • Notice that the pattern is quoted with single quotes ' ' to prevent the shell from interpreting the special characters inside it.
  • Now the pattern itself is written in a particular syntax called regular expression, or regex for short. Regex is an extremely powerful searching tool if you master it, and there are sites such as this which teach you about it in more depth, but note that grep is not a full-fledged regex engine and you can't do everything with it.

  • For our purpose:

    • ^ in regex matches the beginning of the string; this prevents it from matching the pattern if it doesn't occur in the beginning of the file name.

    • . in regex has a special meaning too: it means "match any single character here". In case you want to use it as a literal dot, you'll have to "escape" it using a backslash \ before it. (Yeah, matching any character would be harmless in our case, but I did it for completeness' sake.)

share|improve this answer
add comment

You can use find command to search files with pattern

find . -type f -name "abc*" 

The above command will search the file that starts with abc under the current working directory.

-name 'abc' will list the files that are exact match. Eg: abc

You can also use

 -iname
 -regex 

option with find command to search filename using a pattern

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.