2017-03-07 16 views
5

में खोज कमांड का उपयोग करते समय "निर्देशिका है" परिणाम छोड़ दें मैं निर्देशिका संरचना के भीतर एक स्ट्रिंग को खोजने के लिए निम्न आदेश का उपयोग करता हूं।यूनिक्स

find . -exec grep -l samplestring {} \; 

लेकिन जब मैं एक बड़ी निर्देशिका संरचना के भीतर आदेश चला, वहाँ

grep: ./xxxx/xxxxx_yy/eee: Is a directory 
grep: ./xxxx/xxxxx_yy/eee/local: Is a directory 
grep: ./xxxx/xxxxx_yy/eee/lib: Is a directory 

की एक लंबी सूची हो जाएगा मैं उन ऊपर दिए गए परिणामों को छोड़ करना चाहते हैं। और बस प्रदर्शित स्ट्रिंग के साथ फ़ाइल का नाम प्राप्त करें। क्या कोई मदद कर सकता है?

उत्तर

3

जब भी आप find . कह रहे हैं, उपयोगिता अपने वर्तमान निर्देशिका संरचना के भीतर सभी तत्वों को वापस जाने के लिए जा रहा है: फ़ाइलें, निर्देशिका, लिंक ...

तुम सिर्फ फाइलों को खोजने के लिए चाहते हैं, तो सिर्फ इतना कहना!

grep -lR "samplestring" 
+2

'find -type f' और' grep -r' या 'grep -R' अच्छे विकल्प हैं ... केवल उत्तर को पूरा करने के लिए, शायद यह जोड़ें कि '-d skip' विशेष रूप से' निर्देशिका है 'का ख्याल रखता है मुद्दा ... कम से कम मामलों में उपयोगी है जैसे पुनरावर्ती खोज के लिए खोल विकल्प का उपयोग करना, पूर्व के लिए: 'grep -d skip -l' samplestring '**/@ (*। txt | * .log) ' – Sundeep

+1

@ सुंदिप अच्छा ! मुझे '-'' झंडा के बारे में पता नहीं था। – fedorqui

3

grep -s या grep --no-messages

यह पढ़ने पोर्टेबिलिटी the GNU grep documentation में नोट आप उपयोग करना उम्मीद कर रहे हैं, तो हो सकता है:

find . -type f -exec grep -l samplestring {} \; 
#  ^^^^^^^ 

हालांकि, अगर आप स्ट्रिंग वाली सभी फाइलों को कह रही है खोजने के लिए चाहते हो सकता है यह कोड एकाधिक स्थान, हालांकि:

-s --no-messages Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep’s -q option.1 USG-style grep also lacked -q but its -s option behaved like GNU grep’s. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)

+0

मुझे नहीं लगता कि यह त्रुटियों को छिपाने के लिए उपयोगी है, जब आप उनसे बच सकते हैं। मेरा जवाब देखें – fedorqui