Linux find all files with pattern

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.

12 Answers 12

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.

Why when I do ls -l B* does it show the contents of all directories starting with B instead of just the names of the matching directories?

@ErikE Because when you ask ls to list a directory, it opens it, it’s the same as doing ls -l BirthdayPhotos . You can suppress that behavior with ls -d B* .

@ErikE Because using the file glob B* expands to match all directories that start with B so you’re really passing ls a list of directories, so it opens them all for you.

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

You can use find command to search files with pattern

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

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:

. and replacing ABC with your text.

Читайте также:  Suse linux enterprise server i586

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

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

    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

    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\)»

    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, 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 
    • 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.

    Источник

    Linux, find all files matching pattern and delete

    Looking to find all files (recursively) which have an underscore in their file name and then delete them via command line.

    4 Answers 4

    This is the safest and fastest variant:

    find /path -type f -name '*_*' -delete 

    It does not require piping and doesn’t break if files contain spaces or globbing characters or anything else that other constructs would choke on. The easiest rule to remember here is to never parse find output. And never grep on filenames if you want to do something with them later. You can do almost anything with find directly.

    This includes directories which are considered files. Some of the other examples using xargs will fail if the filename contains spaces.

    If you only want regular files:

    find . -type f -name '*_*' -exec rm -f <> \; 

    Just a hint, if you use \+ instead of \; your command will be faster since it will substitute the name of all found files instead of calling rm one at a time.

    Alright, let’s do this progressively.

    As a first pass, this is just a simple exercise in passing a wildcard to the find command, remembering to quote it of course, and executing the rm command for every file found:

    But of course that’s dreadfully inefficient. It starts up a whole rm process for each individual file. So while we could take a short detour through \+ that’s not where we are going to end up, so let’s take the shorter route and bring in xargs to batch up the filenames into groups:

    find $/ -name '*_*' -print | xargs rm 

    But that has two security holes. First, if any filename found happens to begin with a minus sign rm will treat it as a command-line option rather than a filename, and generate an error. (The -exec rm <> version also has this problem.) Second, filenames containing whitespace will not be handled properly by xargs . So a further iteration is to make this a little more bulletproof:

    find $/ -name '*_*' -print0 | xargs -0 rm -- 

    And, of course, there are the interactive features of rm that you probably don’t want:

    find $/ -name '*_*' -print0 | xargs -0 rm -f -- 

    The -print0 and -0 options are not standard, but the GNU find and xargs , as well as the FreeBSD find and xargs , understand them. However, even this is improvable. We don’t need to spawn any extra processes at all. The GNU and FreeBSD find s can both invoke the unlink(2) system call directly:

    As a last preventative measure to stop you doing more than you intended in certain circumstances, remember that the filesystem can contain more than just regular files:

    find $/ -name '*_*' -type f -delete 

    Источник

    linux search file based on file name pattern [closed]

    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

    This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

    enter image description here

    I want to search a series of files based on the files name. Here is my directory : For example I had the file on above. I only want to search out the file which is without _bak.

    Your question is far too vague to give a good answer. Are you trying to search the contents of the files? The filenames themselves? Are you just trying to get filenames that don’t end in _bak ?

    Also, you should really post your directory contents in plain text, not an image. See the formatting help.

    I’m not certain what the confusion is about. The OP has tagged this as linux unix and he’s asking to pick out the files that don’t have _bak at the end. Am I missing something? (Although the formatting thing is true. You should really use plain text.)

    question seems very clear to me. pattern is starts with «ei469390ONL00», looks like windows, not linux, so dir

    3 Answers 3

    If you’re wanting to limit your search to the current directory, use:

    find . -maxdepth 1 -mindepth 1 ! -name '*_bak' 

    If you want to recursively find in all directories remove the -maxdepth 1 .

    Edit in response to OP’s comment

    To get files that begin with ei and do not end with _bak use:

    find . -type f -name 'ei*' -a ! -name '*_bak' 

    Note, you must use quotes or double quotes. This won’t work: find . -name *_bak because the shell will replace the *_bak with matching filenames in the current directory, hence the unknown primary or operator error you typically get. Use quotes or double quotes as in the answer here.

    Источник

Оцените статью
Adblock
detector