How do I find all files that do not begin with a given prefix in bash?

12,676

Solution 1

This should do the trick in any shell

ls | grep -v '^prefix'

The -v option inverts grep's search logic, making it filter out all matches. Using grep instead of find you can use powerful regular expressions instead of the limited glob patterns.

Solution 2

If you're doing subdirectories as well:

find . ! -name "bar_*"

Or, equivalently,

find . -not -name "*bar_*"

Solution 3

You want to find filenames not starting with bar_*?

recursive:

find ! -name 'bar_*' > Negatives.txt

top directory:

find -maxdepth 1 ! -name 'bar_*' > Negatives.txt
Share:
12,676
Robert
Author by

Robert

iOS Developer

Updated on June 05, 2022

Comments

  • Robert
    Robert almost 2 years

    I have a bunch of files in a folder:

    foo_1 
    foo_2
    foo_3
    bar_1
    bar_2
    buzz_1
    ...
    

    I want to find all the files that do not start with a given prefix and save the list to a text file. Here is an example for the files that do have a given prefix:

    find bar_* > Positives.txt