简体   繁体   中英

How to Exclude Directories from grep command in linux?

I Am trying to exclude directories from grep matches, i have tried with --exclude-dir=PATTERN method. But its not working.

ls | grep -E "^Acct" --exclude-dir=Acct

File Structure

Acct/ ---> directory needs to be excluded from grep
AcctReq/ ---> directory needs to be excluded from grep
AcctAdd.txt
AcctInq.txt     
AcctMod.txt       
AcctTrnInq.txt
CardInq.txt
CardHold.txt
Cardacq.txt

In Above files I am using ls | grep -E "^Acct" ls | grep -E "^Acct" command to get only the files starting with Acct , But it is considering the directories as well.

Output:

Acct/
AcctReq/
AcctAdd.txt
AcctInq.txt     
AcctMod.txt       
AcctTrnInq.txt

Expected Output:

AcctAdd.txt
AcctInq.txt     
AcctMod.txt       
AcctTrnInq.txt

--exclude-dir is used to skip directories when running a recursive grep. It's meaningless when you're piping input to grep , since there are no directories or filenames to skip.

If you want to exclude matches from the data being piped to grep use -v .

ls | grep -v 'Acct.*/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM