55

I want to search a series of files based on the files name.

Here is my directory :

enter image description here

For example I had the file on above.

I only want to search out the file which is without _bak.

tshepang
  • 12,111
  • 21
  • 91
  • 136
crchin
  • 9,473
  • 6
  • 22
  • 28
  • 1
    What programming language are you using? Which APIs? – sarnold Jun 15 '12 at 02:22
  • 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`? – David Cain Jun 15 '12 at 02:31
  • Also, you should really post your directory contents in plain text, not an image. See the [formatting help](http://stackoverflow.com/editing-help#code). – David Cain Jun 15 '12 at 02:34
  • 1
    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.) – Tim Pote Jun 15 '12 at 02:39
  • question seems very clear to me. pattern is starts with "ei469390ONL00", looks like windows, not linux, so dir – HelpVampire666 Oct 05 '17 at 07:57
  • This question is a duplicate of [List files not matching a pattern?](https://stackoverflow.com/q/8525437/11725753) – EvgenKo423 Jul 06 '21 at 11:53

3 Answers3

77

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'
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
  • 1
    Hi Tim Pote , your answer is fine if without -maxdepth 1 -mindepth 1. Thanks for you help. – crchin Jun 15 '12 at 03:00
  • @user1154655 Changed per your suggestion. – Tim Pote Jun 15 '12 at 03:03
  • 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. – Rian Rizvi Oct 26 '21 at 20:07
6

Please try this:

 find . -type f ! -iname "*_bak"

The above command will find all files that have not end with _bak.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
AzizSM
  • 6,199
  • 4
  • 42
  • 53
  • The command is helpful. thanks . if the directory contain other files as well , but i just want to search out the file name starts with ei but without bak ? – crchin Jun 15 '12 at 02:53
  • 2
    This will do `find . -name 'ei*' -type f ! -iname "*_bak"` – AzizSM Jun 15 '12 at 03:01
-2

Use the Grep Invert Match option.

For example, if you want all the file without the word _bak, use :

grep -v *_bak /path/to/file
Erwald
  • 2,118
  • 2
  • 14
  • 20