How to search for all the files starting with the name "ABC" in a 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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

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

Solution 3

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.)

Solution 4

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).

Solution 5

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"

Solution 6

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

Solution 7

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.

Solution 8

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

enter image description here

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.

Solution 9

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.

Solution 10

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  },".")' 

Solution 11

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.

Solution 12

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,

[[email protected] /]# 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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply