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.
12 Answers
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 pattern abc
:
ls -d abc* # list all files starting with abc---
ls -d *abc* # list all files containing --abc--
ls -d *abc # list all files ending with --abc
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:
tree -P 'abc*' # list directory tree of file starting with abc---
tree -l 'def*' # exclude files starting with def---
In this case, tree
itself supports wildcards.
-
2Why when I do
ls -l B*
does it show the contents of all directories starting withB
instead of just the names of the matching directories?– ErikEJan 24, 2018 at 23:24 -
1@ErikE Because when you ask
ls
to list a directory, it opens it, it's the same as doingls -l BirthdayPhotos
. You can suppress that behavior withls -d B*
. Apr 17, 2018 at 17:23 -
-
@ErikE Because using the file glob
B*
expands to match all directories that start withB
so you're really passingls
a list of directories, so it opens them all for you. Apr 17, 2018 at 17:26 -
3Does not answer the question.
ls -d ABC*
was what the author was asking about. Also comments in the code snippet are wrong,ls abc*
lists content of directories starting with abc. Apr 11, 2019 at 18:38
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
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 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
.- Notice that the pattern is quoted with single quotes
' '
to prevent the shell from interpreting the special characters inside it.
- Notice that the pattern is quoted with single quotes
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.)
-
3Why would you
find | grep
whenfind
has its own perfectly good pattern-matching capabilities?– DaraelJul 11, 2017 at 11:26
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
ls | grep PATTERN
Here you can give any regular expression in the PATTERN.
For example, to find files with "ab" anywhere within its name, type
ls | grep ".*ab.*"
To find the files starting with "ab", type
ls | grep "^ab"
-
it takes forever for me and i am trying to get video file names with for example (1990)
find . -maxdepth 1 | grep -R "\(\d\d\d\d\)"
Feb 11, 2021 at 8:43
If you don't know the directory the ABC*
files are located in, and you have millions of files, the locate
command is the fastest method.
$ locate /ABC
/mnt/clone/home/rick/.cache/mozilla/firefox/9fu0cuql.default/cache2/entries/ABC6AD2FEC16465049B48D39FD2FE538258F2A34
/mnt/clone/home/rick/.cache/mozilla/firefox/9fu0cuql.default/cache2/entries/ABCBFDA54262F47253F95ED6ED4131A465EE0E39
/mnt/clone/usr/src/linux-headers-5.0.1-050001/tools/lib/lockdep/tests/ABCABC.sh
/mnt/clone/usr/src/linux-headers-5.0.1-050001/tools/lib/lockdep/tests/ABCDBCDA.sh
/mnt/clone/usr/src/linux-headers-5.0.1-050001/tools/lib/lockdep/tests/ABCDBDDA.sh
/mnt/old/home/rick/.cache/mozilla/firefox/3vkvi6ov.default/cache2/entries/ABC0C99FCEABAD0C6AA2078CD025A1CDE48D7BA1
/usr/src/linux-headers-5.0.1-050001/tools/lib/lockdep/tests/ABCABC.sh
/usr/src/linux-headers-5.0.1-050001/tools/lib/lockdep/tests/ABCDBCDA.sh
/usr/src/linux-headers-5.0.1-050001/tools/lib/lockdep/tests/ABCDBDDA.sh
Notes:
- The above command takes 1 second to run on 1 million files.
- In comparison the
find
command starting at/
root directory will a very long time and generate many permission errors. - If files were created today you must run
sudo updatedb
first.
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
tree | grep ABC
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:
- Opening files and buffers
- Jumping to tags and help
- Running commands, or previous searches and commands
- with a minimal number of keystrokes.
As you can see
I always search in command history by opening a new terminal and hit:
CTRL+R
In addition to searching in all folders recursively by writing in any terminal tab:
fzf
Then start your file name
ABC
Also, you can write inside vim
:CommandT
Really helpful especially in large folders.
I use
ls | grep abc
or
ls -la | grep abc
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:
$ python -c 'import sys,os;found=[os.path.join(r,i) for r,s,f in os.walk(".") for i in f if i.startswith("ABC")];map(lambda x: sys.stdout.write(x+"\n") ,found)'
Perl:
$ perl -le 'use File::Find;find(sub{ -f && $_ =~/^ABC/ && print $File::Find::name },".")'
printf "%s" /path/to/files/ABC*
This is glob pattern matching which is anchored at both ends. This will match all occurrences of files starting with "ABC" such as "ABC", "ABC.txt", "ABC123", but not "xABC". From the command line using 'ls' in place of 'printf' here is a safe alternative however, depending on who's opinion you agree with, 'ls' is not safe for use in a script. In that case using 'printf' with glob pattern matching is considered safe. If you going to use this in a script the output of 'printf' will not contain a new line character until the end of the output stream as such:
printf "%s" /path/to/files/ABC*
Returns:
/path/to/files/ABC /path/to/files/ABC123
If you need line breaks after each instance:
printf "%s\n" /path/to/files/ABC*
Returns:
/path/to/files/ABC
/path/to/files/ABC123
"/path/to/files/" remains in the output if you entered it that way when you ran the 'printf' command. Without it just the file name appears:
printf "%s" ABC*
Returns
ABC ABC123
Assuming you run the command within the directory in which the files exist.
-
The
echo /path/to/files/ABC* | tr " " "\n"
isn't a good idea. Consider if there's a file/path/to/files/ABC EFG
, there is a space in the filename. Yourtr
will break the filename into two separate lines, which is not good. Even worse if there's multiple spaces in filename. I'd recommend you useprintf "%s\n" ABC*
instead, which should help with spaces in the filenames, but still is problematic with other special characters like\n
Jul 13, 2017 at 7:14
Assume that I am at the root directory and I want the list of the etc directory only: we write,
find -type d -name "etc"
result we get,
[root@unix /]# find -type d -name "etc"
./etc
./usr/lib/firefox/bundled/etc
./usr/lib/unix-yarn/etc
./usr/lib/unix/etc
./usr/lib/festival/etc
./usr/bin/lib/vmware-tools/lib64/libconf/etc
./usr/bin/lib/vmware-tools/lib32/libconf/etc
./usr/etc
./usr/share/doc/oddjob-0.30/sample/etc
./usr/share/festival/lib/etc
./usr/local/etc
./usr/java/jdk1.7.0_25/lib/visualvm/etc
./home/user1/Desktop/VMware Tools/vmware-tools-distrib/lib/lib64/libconf/etc
./home/user1/Desktop/VMware Tools/vmware-tools-distrib/lib/lib32/libconf/etc
./home/user1/Desktop/VMware Tools/vmware-tools-distrib/etc
./home/user1/Desktop/VMware Tools/vmware-tools-distrib/caf/etc
Another Example,
Also we can write:
ls | grep "etc"
we get,
etc