1

i have to search the files that don't have pattern:-

*abc*.txt and *xyz*.txt

Please suggest a way to list all the files which don't have the above patterns.

4 Answers 4

2

You can use an extended glob, such as the following:

!(*@(abc|xyz)*.txt) 

In ksh, this works by default, whereas in bash you need to first enable a shell option:

shopt -s extglob 

! negates the match and @ matches any of the pipe-separated patterns.

This pattern expands to the list of files that don't match *abc*.txt or *xyz*.txt, so you can pass it to another command to see the result, e.g. printf:

printf '%s\n' !(*@(abc|xyz)*.txt) 
Sign up to request clarification or add additional context in comments.

8 Comments

can you please share the command i am not familiar with extended glob
It's not a command, it's just a fancy glob that expands to the list of files. I added an example anyway.
i am getting error like:- bash: !: event not found
!(*@(abc|xyz)*.txt) -- This is called a subpattern in ksh.. Use ksh to execute it..
@Tanmay you're clearly using bash if you get that error
|
2

With find command:

find -type f ! \( -name '*abc*.txt' -o -name '*xyz*.txt' \) 

3 Comments

i got error find: illegal option -- t find: [-H | -L] path-list predicate-list
@TanmayDutta Some implementations of find allow you to omit the path(s) in which to search, defaulting to .. Yours apparently does not; try find . -type f ....
what does -o mean!? what is this ( ) notation? on what situation can I use it? for what other commands?
0

You can use the --hide=PATTERN option with ls. In your case it would be

ls --hide="*abc*.txt" --hide="*xyz*.txt" 

6 Comments

which shell are you using?
I am using bash shell
It seems that something is broken with your shell, you get illegal option -- every time you enter a command with an option. Which OS are you using? Any special bash config?
@piarston --hide is a non-standard option to ls so it's perfectly reasonable to expect that it doesn't exist on some systems.
The standard ones can be found by searching "opengroup ls", also works for other commands.
|
0

I got the solution simple grep is working for me

ls | grep -v "abc" | grep -v "xyz" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.