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.
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.
To complete existing answers:
ls
The default directory list utility
ls
can be used in combination with the shell's wildcards . To search for all files with patternabc
:Note that the file extension is relevant for the search results too.
tree
In case we need to list files in a directory tree we can also issue
tree
to search for a given pattern like:In this case,
tree
itself supports wildcards.You can use
find
command to search files with patternThe 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: abcYou can also use
option with
find
command to search filename using a patternThere 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 thatfind
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 justABC
.The pipe character
|
redirects the output of one command to another, in this case the output offind
is redirected togrep
. This is called piping.grep
takes the output and filters it using the given pattern,^\./ABC
.' '
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.)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).
Then, just type the regular expression
ABC*
and validate.Every file whose name matches your pattern will be automatically selected.
I'm using Nautilus 3.6.* from GNOME3 PPA on Ubuntu 12.10 (Quantal).
The easiest solution to me
Here you can give any regular expression in the PATTERN.
For example, to find files with "ab" anywhere within its name, type
To find the files starting with "ab", type
you can use GREP, I think this is the most simple solution, probably also add some other grep parameters to make the match more accurate
If you don't know the directory the
ABC*
files are located in, and you have millions of files, thelocate
command is the fastest method.Notes:
find
command starting at/
root directory will a very long time and generate many permission errors.sudo updatedb
first.Command-t in one of my favorite vim plugins, it's ruby based plugin above integration with FZF.
By using Comamnd-T and FZF you can do the search with an extremely fast "fuzzy" mechanism for:
As you can see
I always search in command history by opening a new terminal and hit:
In addition to searching in all folders recursively by writing in any terminal tab:
Then start your file name
Also, you can write inside
vim
Really helpful especially in large folders.
I use
or
It show all files with abc, not just starting with abc. But, It's a really easy way for me for to do this.
Python:
Perl: