-1

I want to identify a string provided as an argument on command-line.I need to search only in just current directory and archives. my command is:

if grep -s "$1" * .* 
then echo "found" else echo "not found"

But for example if i have one folder with the word "father" and another with "fathersa" and the string input is "father" my code output both folders.But if my input is "father "(father and space) the output should be only the first folder but my code display both folder.What should i do instead? Also is this command good enough to search through all folders and archives from my current directory? Edit: i`m using Ubuntu desktop (Ubuntu 12.04.4 LTS)

I also tried grep -w but my string is not exactly a fixed string, its actually a pattern so it isnt really what i wanted.If my input is "fa" my code should output all folders that contains the "fa" structures like "father" "fat" and so on but if my input would be "fa " with space my output should be only the folders with "fa" and just that

MIfi22
  • 1
  • Good point!Thanks – MIfi22 May 06 '20 at 17:43
  • This question is specific to an end of life https://wiki.ubuntu.com/Releases Ubuntu release. These are no longer supported and are therefore off-topic here. To upgrade, see: How to install software or upgrade from old unsupported release? https://askubuntu.com/questions/91815/how-to-install-software-or-upgrade-from-an-old-unsupported-release – K7AAY May 06 '20 at 17:46
  • @bac0n and where should i put this command?After if grep? – MIfi22 May 06 '20 at 18:18
  • @K7AAY to which version should i update ? – MIfi22 May 06 '20 at 18:28
  • The versions which have not yet reached end-of-public-support are 16.04, 18.04, 19.10, and 20.04. 19.10 reaches end-of-public-support soon,. though. – K7AAY May 06 '20 at 18:33
  • @bac0n as i tried this command the output displays me all the folders with the pattern name not the folders that actually contains this pattern.I need to search in the content of the files,not in the name of the files. – MIfi22 May 06 '20 at 18:54
  • do you quote your argument ./script.sh 'fa ' or fa\ else your space will get trimmed –  May 06 '20 at 19:46
  • im using ./script.sh fa and it output me the same thing with/without space.Tried also ./script.sh 'fa ' with and without space..still the same result – MIfi22 May 06 '20 at 20:22
  • if command; then command; else command; fi –  May 06 '20 at 20:45
  • @bac0n if grep -s "$1" * .* ; then echo "found"; else echo "not found"; fi You mean like that?Still the same result:( – MIfi22 May 07 '20 at 08:53
  • you have to update your question, what you say cannot happen because first, the code in your Q is broken, and second the script does not traverse into directories. For that you need to use if grep -q -R "$1"; then echo ok; else echo fail; fi as the answer says. –  May 07 '20 at 09:30
  • I figured it out that the problem is my code didnt see space as a string(pattern) because when i tried to input "father mother" it didnt really work.My code is working with only one word,not more and i need to work for more and maybe thats why i dont have the results i need – MIfi22 May 07 '20 at 09:51
  • '(father|mother)' And you need to add -E option to grep –  May 07 '20 at 10:06
  • So this command works and i want to ty very much.But this command also search through all types of archives? – MIfi22 May 07 '20 at 13:18

1 Answers1

1

use grep -w

-w, --word-regexp
Select only those lines containing matches that form whole words.

Also you might want to use -q. And if you have a fixed string and not a pattern, add -F to make grep perform much faster!

Your command will not search recursively.

pLumo
  • 26,947
  • Ok so i just tried your command but my string is not exactly a fixed string, its actually a pattern so it isnt really what i wanted.If my input is "fa" my code should output all folders that contains the "fa" structures like "father" "fat" and so on but if my input would be "fa " with space my output should be only the folders with "fa" and just that. – MIfi22 May 06 '20 at 17:17
  • would the -R option not also come in handy since OP is doing a directory search, with potential folders? – George Udosen May 06 '20 at 17:17
  • Copy the command you issued and the exact output you get and add it to the question, not as a comment. – jpezz May 06 '20 at 18:15