Sorting out directories by searching certain file extension

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Let say, I am in a directory A, under A there are many folders (B, C, D, etc.), and in each folder there is a file "*.out" and sub-folders. I want to run a script from A which will look for the text "index123" in *.out file and print out all corresponding folder names.



Here is my sript:



#!/bin/sh 
FILES=home/A
grep --include=*.out -rnw $FILES -e "index123" | while read file; do
str1="FILES/$(basename $file)"
echo $str1
done


This shows error.



N.B. This can be done by "find" in one line code, but why while loop shown shows error?







share|improve this question






















  • What is the error? And why do you try to access /../A?
    – Kusalananda
    Apr 11 at 19:02











  • Why not just grep index123 */*.out
    – Jeff Schaller
    Apr 11 at 19:07






  • 1




    Or grep -l to get the filenames
    – Jeff Schaller
    Apr 11 at 19:08










  • @Kusalananda, it can be anywhere, say, /home/A
    – Akand
    Apr 11 at 20:00










  • @JeffSchaller is grep index123 */*.out before while | ? If so, it shows no such file or directory
    – Akand
    Apr 11 at 20:04














up vote
1
down vote

favorite












Let say, I am in a directory A, under A there are many folders (B, C, D, etc.), and in each folder there is a file "*.out" and sub-folders. I want to run a script from A which will look for the text "index123" in *.out file and print out all corresponding folder names.



Here is my sript:



#!/bin/sh 
FILES=home/A
grep --include=*.out -rnw $FILES -e "index123" | while read file; do
str1="FILES/$(basename $file)"
echo $str1
done


This shows error.



N.B. This can be done by "find" in one line code, but why while loop shown shows error?







share|improve this question






















  • What is the error? And why do you try to access /../A?
    – Kusalananda
    Apr 11 at 19:02











  • Why not just grep index123 */*.out
    – Jeff Schaller
    Apr 11 at 19:07






  • 1




    Or grep -l to get the filenames
    – Jeff Schaller
    Apr 11 at 19:08










  • @Kusalananda, it can be anywhere, say, /home/A
    – Akand
    Apr 11 at 20:00










  • @JeffSchaller is grep index123 */*.out before while | ? If so, it shows no such file or directory
    – Akand
    Apr 11 at 20:04












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Let say, I am in a directory A, under A there are many folders (B, C, D, etc.), and in each folder there is a file "*.out" and sub-folders. I want to run a script from A which will look for the text "index123" in *.out file and print out all corresponding folder names.



Here is my sript:



#!/bin/sh 
FILES=home/A
grep --include=*.out -rnw $FILES -e "index123" | while read file; do
str1="FILES/$(basename $file)"
echo $str1
done


This shows error.



N.B. This can be done by "find" in one line code, but why while loop shown shows error?







share|improve this question














Let say, I am in a directory A, under A there are many folders (B, C, D, etc.), and in each folder there is a file "*.out" and sub-folders. I want to run a script from A which will look for the text "index123" in *.out file and print out all corresponding folder names.



Here is my sript:



#!/bin/sh 
FILES=home/A
grep --include=*.out -rnw $FILES -e "index123" | while read file; do
str1="FILES/$(basename $file)"
echo $str1
done


This shows error.



N.B. This can be done by "find" in one line code, but why while loop shown shows error?









share|improve this question













share|improve this question




share|improve this question








edited Jul 20 at 16:11









peterh

3,92082755




3,92082755










asked Apr 11 at 18:59









Akand

426




426











  • What is the error? And why do you try to access /../A?
    – Kusalananda
    Apr 11 at 19:02











  • Why not just grep index123 */*.out
    – Jeff Schaller
    Apr 11 at 19:07






  • 1




    Or grep -l to get the filenames
    – Jeff Schaller
    Apr 11 at 19:08










  • @Kusalananda, it can be anywhere, say, /home/A
    – Akand
    Apr 11 at 20:00










  • @JeffSchaller is grep index123 */*.out before while | ? If so, it shows no such file or directory
    – Akand
    Apr 11 at 20:04
















  • What is the error? And why do you try to access /../A?
    – Kusalananda
    Apr 11 at 19:02











  • Why not just grep index123 */*.out
    – Jeff Schaller
    Apr 11 at 19:07






  • 1




    Or grep -l to get the filenames
    – Jeff Schaller
    Apr 11 at 19:08










  • @Kusalananda, it can be anywhere, say, /home/A
    – Akand
    Apr 11 at 20:00










  • @JeffSchaller is grep index123 */*.out before while | ? If so, it shows no such file or directory
    – Akand
    Apr 11 at 20:04















What is the error? And why do you try to access /../A?
– Kusalananda
Apr 11 at 19:02





What is the error? And why do you try to access /../A?
– Kusalananda
Apr 11 at 19:02













Why not just grep index123 */*.out
– Jeff Schaller
Apr 11 at 19:07




Why not just grep index123 */*.out
– Jeff Schaller
Apr 11 at 19:07




1




1




Or grep -l to get the filenames
– Jeff Schaller
Apr 11 at 19:08




Or grep -l to get the filenames
– Jeff Schaller
Apr 11 at 19:08












@Kusalananda, it can be anywhere, say, /home/A
– Akand
Apr 11 at 20:00




@Kusalananda, it can be anywhere, say, /home/A
– Akand
Apr 11 at 20:00












@JeffSchaller is grep index123 */*.out before while | ? If so, it shows no such file or directory
– Akand
Apr 11 at 20:04




@JeffSchaller is grep index123 */*.out before while | ? If so, it shows no such file or directory
– Akand
Apr 11 at 20:04










2 Answers
2






active

oldest

votes

















up vote
2
down vote













Assuming a directory structure like the following:



A
|-- B
| |-- file1.out
| |-- file2.out
| `-- file3.out
|-- C
| |-- file1.out
| |-- file2.out
| `-- file3.out
|-- D
| |-- file1.out
| |-- file2.out
| `-- file3.out
`-- E
|-- file1.out
|-- file2.out
`-- file3.out


The issue with your code is that your grep will produce output that looks like



./B/file1.out:2:some data which includes the word index123
./B/file2.out:2:some data which includes the word index123
./B/file3.out:2:some data which includes the word index123
./C/file1.out:2:some data which includes the word index123
./C/file2.out:2:some data which includes the word index123
./C/file3.out:2:some data which includes the word index123
./D/file1.out:2:some data which includes the word index123
./D/file2.out:2:some data which includes the word index123
./D/file3.out:2:some data which includes the word index123
./E/file1.out:2:some data which includes the word index123
./E/file2.out:2:some data which includes the word index123
./E/file3.out:2:some data which includes the word index123


That is the output of



grep --include=*.out -rnw . -e "index123"


with A as the current directory.



You will then try to run basename on these individual lines, which fails since basename takes at most two arguments (a pathame and a suffix to strip from that pathname). GNU basename will complain about an "extra operand" while BSD basename will complain about incorrect usage.




grep will show you the names of the files (only, i.e. not the complete line that matched) when you use it with the -l flag.



This means that your script may be replaced by the single command



grep -w -l "index123" */*.out


This will give output on the form



B/file1.out
B/file2.out
B/file3.out
C/file1.out
C/file2.out
C/file3.out
D/file1.out
D/file2.out
D/file3.out
E/file1.out
E/file2.out
E/file3.out


I added -w as you used in your grep command line. -n (for numbering lines, which you are also using) may not be used together with -l.



Judging from your code, this is what you want.



If you need just the folder names, then do



$ grep -w -l "index123" */*.out | sed 's#/[^/]*##' | sort -u
B
C
D
E


All of this assumes that A is the current working directory, but you said that this was the case in the question so it shouldn't be a problem.






share|improve this answer






















  • Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
    – Akand
    Apr 11 at 20:37










  • @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
    – Kusalananda
    Apr 11 at 20:42











  • For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
    – Akand
    Apr 11 at 21:37











  • @Akand See updated answer.
    – Kusalananda
    Apr 12 at 6:22










  • I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
    – Akand
    Apr 12 at 18:55

















up vote
1
down vote



accepted










As per from the post find a file through particular search in while loop one of solutions can be as follow by using the while loop:



#!/bin/bash

while IFS= read -r d;

grep -q "index123" "$d" && dirname "$d"|awk -F'/' 'print $2'

done < <(find . -maxdepth 2 -type f -name "*.out")






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437118%2fsorting-out-directories-by-searching-certain-file-extension%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Assuming a directory structure like the following:



    A
    |-- B
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- C
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- D
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    `-- E
    |-- file1.out
    |-- file2.out
    `-- file3.out


    The issue with your code is that your grep will produce output that looks like



    ./B/file1.out:2:some data which includes the word index123
    ./B/file2.out:2:some data which includes the word index123
    ./B/file3.out:2:some data which includes the word index123
    ./C/file1.out:2:some data which includes the word index123
    ./C/file2.out:2:some data which includes the word index123
    ./C/file3.out:2:some data which includes the word index123
    ./D/file1.out:2:some data which includes the word index123
    ./D/file2.out:2:some data which includes the word index123
    ./D/file3.out:2:some data which includes the word index123
    ./E/file1.out:2:some data which includes the word index123
    ./E/file2.out:2:some data which includes the word index123
    ./E/file3.out:2:some data which includes the word index123


    That is the output of



    grep --include=*.out -rnw . -e "index123"


    with A as the current directory.



    You will then try to run basename on these individual lines, which fails since basename takes at most two arguments (a pathame and a suffix to strip from that pathname). GNU basename will complain about an "extra operand" while BSD basename will complain about incorrect usage.




    grep will show you the names of the files (only, i.e. not the complete line that matched) when you use it with the -l flag.



    This means that your script may be replaced by the single command



    grep -w -l "index123" */*.out


    This will give output on the form



    B/file1.out
    B/file2.out
    B/file3.out
    C/file1.out
    C/file2.out
    C/file3.out
    D/file1.out
    D/file2.out
    D/file3.out
    E/file1.out
    E/file2.out
    E/file3.out


    I added -w as you used in your grep command line. -n (for numbering lines, which you are also using) may not be used together with -l.



    Judging from your code, this is what you want.



    If you need just the folder names, then do



    $ grep -w -l "index123" */*.out | sed 's#/[^/]*##' | sort -u
    B
    C
    D
    E


    All of this assumes that A is the current working directory, but you said that this was the case in the question so it shouldn't be a problem.






    share|improve this answer






















    • Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
      – Akand
      Apr 11 at 20:37










    • @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
      – Kusalananda
      Apr 11 at 20:42











    • For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
      – Akand
      Apr 11 at 21:37











    • @Akand See updated answer.
      – Kusalananda
      Apr 12 at 6:22










    • I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
      – Akand
      Apr 12 at 18:55














    up vote
    2
    down vote













    Assuming a directory structure like the following:



    A
    |-- B
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- C
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- D
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    `-- E
    |-- file1.out
    |-- file2.out
    `-- file3.out


    The issue with your code is that your grep will produce output that looks like



    ./B/file1.out:2:some data which includes the word index123
    ./B/file2.out:2:some data which includes the word index123
    ./B/file3.out:2:some data which includes the word index123
    ./C/file1.out:2:some data which includes the word index123
    ./C/file2.out:2:some data which includes the word index123
    ./C/file3.out:2:some data which includes the word index123
    ./D/file1.out:2:some data which includes the word index123
    ./D/file2.out:2:some data which includes the word index123
    ./D/file3.out:2:some data which includes the word index123
    ./E/file1.out:2:some data which includes the word index123
    ./E/file2.out:2:some data which includes the word index123
    ./E/file3.out:2:some data which includes the word index123


    That is the output of



    grep --include=*.out -rnw . -e "index123"


    with A as the current directory.



    You will then try to run basename on these individual lines, which fails since basename takes at most two arguments (a pathame and a suffix to strip from that pathname). GNU basename will complain about an "extra operand" while BSD basename will complain about incorrect usage.




    grep will show you the names of the files (only, i.e. not the complete line that matched) when you use it with the -l flag.



    This means that your script may be replaced by the single command



    grep -w -l "index123" */*.out


    This will give output on the form



    B/file1.out
    B/file2.out
    B/file3.out
    C/file1.out
    C/file2.out
    C/file3.out
    D/file1.out
    D/file2.out
    D/file3.out
    E/file1.out
    E/file2.out
    E/file3.out


    I added -w as you used in your grep command line. -n (for numbering lines, which you are also using) may not be used together with -l.



    Judging from your code, this is what you want.



    If you need just the folder names, then do



    $ grep -w -l "index123" */*.out | sed 's#/[^/]*##' | sort -u
    B
    C
    D
    E


    All of this assumes that A is the current working directory, but you said that this was the case in the question so it shouldn't be a problem.






    share|improve this answer






















    • Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
      – Akand
      Apr 11 at 20:37










    • @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
      – Kusalananda
      Apr 11 at 20:42











    • For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
      – Akand
      Apr 11 at 21:37











    • @Akand See updated answer.
      – Kusalananda
      Apr 12 at 6:22










    • I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
      – Akand
      Apr 12 at 18:55












    up vote
    2
    down vote










    up vote
    2
    down vote









    Assuming a directory structure like the following:



    A
    |-- B
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- C
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- D
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    `-- E
    |-- file1.out
    |-- file2.out
    `-- file3.out


    The issue with your code is that your grep will produce output that looks like



    ./B/file1.out:2:some data which includes the word index123
    ./B/file2.out:2:some data which includes the word index123
    ./B/file3.out:2:some data which includes the word index123
    ./C/file1.out:2:some data which includes the word index123
    ./C/file2.out:2:some data which includes the word index123
    ./C/file3.out:2:some data which includes the word index123
    ./D/file1.out:2:some data which includes the word index123
    ./D/file2.out:2:some data which includes the word index123
    ./D/file3.out:2:some data which includes the word index123
    ./E/file1.out:2:some data which includes the word index123
    ./E/file2.out:2:some data which includes the word index123
    ./E/file3.out:2:some data which includes the word index123


    That is the output of



    grep --include=*.out -rnw . -e "index123"


    with A as the current directory.



    You will then try to run basename on these individual lines, which fails since basename takes at most two arguments (a pathame and a suffix to strip from that pathname). GNU basename will complain about an "extra operand" while BSD basename will complain about incorrect usage.




    grep will show you the names of the files (only, i.e. not the complete line that matched) when you use it with the -l flag.



    This means that your script may be replaced by the single command



    grep -w -l "index123" */*.out


    This will give output on the form



    B/file1.out
    B/file2.out
    B/file3.out
    C/file1.out
    C/file2.out
    C/file3.out
    D/file1.out
    D/file2.out
    D/file3.out
    E/file1.out
    E/file2.out
    E/file3.out


    I added -w as you used in your grep command line. -n (for numbering lines, which you are also using) may not be used together with -l.



    Judging from your code, this is what you want.



    If you need just the folder names, then do



    $ grep -w -l "index123" */*.out | sed 's#/[^/]*##' | sort -u
    B
    C
    D
    E


    All of this assumes that A is the current working directory, but you said that this was the case in the question so it shouldn't be a problem.






    share|improve this answer














    Assuming a directory structure like the following:



    A
    |-- B
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- C
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    |-- D
    | |-- file1.out
    | |-- file2.out
    | `-- file3.out
    `-- E
    |-- file1.out
    |-- file2.out
    `-- file3.out


    The issue with your code is that your grep will produce output that looks like



    ./B/file1.out:2:some data which includes the word index123
    ./B/file2.out:2:some data which includes the word index123
    ./B/file3.out:2:some data which includes the word index123
    ./C/file1.out:2:some data which includes the word index123
    ./C/file2.out:2:some data which includes the word index123
    ./C/file3.out:2:some data which includes the word index123
    ./D/file1.out:2:some data which includes the word index123
    ./D/file2.out:2:some data which includes the word index123
    ./D/file3.out:2:some data which includes the word index123
    ./E/file1.out:2:some data which includes the word index123
    ./E/file2.out:2:some data which includes the word index123
    ./E/file3.out:2:some data which includes the word index123


    That is the output of



    grep --include=*.out -rnw . -e "index123"


    with A as the current directory.



    You will then try to run basename on these individual lines, which fails since basename takes at most two arguments (a pathame and a suffix to strip from that pathname). GNU basename will complain about an "extra operand" while BSD basename will complain about incorrect usage.




    grep will show you the names of the files (only, i.e. not the complete line that matched) when you use it with the -l flag.



    This means that your script may be replaced by the single command



    grep -w -l "index123" */*.out


    This will give output on the form



    B/file1.out
    B/file2.out
    B/file3.out
    C/file1.out
    C/file2.out
    C/file3.out
    D/file1.out
    D/file2.out
    D/file3.out
    E/file1.out
    E/file2.out
    E/file3.out


    I added -w as you used in your grep command line. -n (for numbering lines, which you are also using) may not be used together with -l.



    Judging from your code, this is what you want.



    If you need just the folder names, then do



    $ grep -w -l "index123" */*.out | sed 's#/[^/]*##' | sort -u
    B
    C
    D
    E


    All of this assumes that A is the current working directory, but you said that this was the case in the question so it shouldn't be a problem.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 12 at 6:21

























    answered Apr 11 at 20:20









    Kusalananda

    102k13199316




    102k13199316











    • Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
      – Akand
      Apr 11 at 20:37










    • @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
      – Kusalananda
      Apr 11 at 20:42











    • For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
      – Akand
      Apr 11 at 21:37











    • @Akand See updated answer.
      – Kusalananda
      Apr 12 at 6:22










    • I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
      – Akand
      Apr 12 at 18:55
















    • Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
      – Akand
      Apr 11 at 20:37










    • @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
      – Kusalananda
      Apr 11 at 20:42











    • For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
      – Akand
      Apr 11 at 21:37











    • @Akand See updated answer.
      – Kusalananda
      Apr 12 at 6:22










    • I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
      – Akand
      Apr 12 at 18:55















    Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
    – Akand
    Apr 11 at 20:37




    Thanks! As I mentioned in my original post, this can be done by single line command. However, I am curious why the while loop is not working. The error message I've received is basename: extra operand ':'. Moreover, the command grep -w -l "index123" */*.out shows permission deny issue as because I am working in a server system with limited access. I need to look only within the working directory.
    – Akand
    Apr 11 at 20:37












    @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
    – Kusalananda
    Apr 11 at 20:42





    @Akand Well, if you look at what you actually get from your grep command, you will notice that there is a lot more than just the filename in the result. You are, for example requesting the line numbers... Also, I don't know how you're going to grep files in subdirectories without actually accessing the files in the subdirectories (they are there, not in the current directory).
    – Kusalananda
    Apr 11 at 20:42













    For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
    – Akand
    Apr 11 at 21:37





    For clarification: I am in the directory A from where the code will scan *.out file of every folders of A by searching the text "index123" and then will print out the corresponding folder names. I like to do it by while loop as shown in the original post.
    – Akand
    Apr 11 at 21:37













    @Akand See updated answer.
    – Kusalananda
    Apr 12 at 6:22




    @Akand See updated answer.
    – Kusalananda
    Apr 12 at 6:22












    I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
    – Akand
    Apr 12 at 18:55




    I appreciate your answer which does solve the problem. However, as mentioned in my post although there is solution of one line code, I like to do by while loop as I need to perform other operations.
    – Akand
    Apr 12 at 18:55












    up vote
    1
    down vote



    accepted










    As per from the post find a file through particular search in while loop one of solutions can be as follow by using the while loop:



    #!/bin/bash

    while IFS= read -r d;

    grep -q "index123" "$d" && dirname "$d"|awk -F'/' 'print $2'

    done < <(find . -maxdepth 2 -type f -name "*.out")






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      As per from the post find a file through particular search in while loop one of solutions can be as follow by using the while loop:



      #!/bin/bash

      while IFS= read -r d;

      grep -q "index123" "$d" && dirname "$d"|awk -F'/' 'print $2'

      done < <(find . -maxdepth 2 -type f -name "*.out")






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        As per from the post find a file through particular search in while loop one of solutions can be as follow by using the while loop:



        #!/bin/bash

        while IFS= read -r d;

        grep -q "index123" "$d" && dirname "$d"|awk -F'/' 'print $2'

        done < <(find . -maxdepth 2 -type f -name "*.out")






        share|improve this answer












        As per from the post find a file through particular search in while loop one of solutions can be as follow by using the while loop:



        #!/bin/bash

        while IFS= read -r d;

        grep -q "index123" "$d" && dirname "$d"|awk -F'/' 'print $2'

        done < <(find . -maxdepth 2 -type f -name "*.out")







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 20 at 15:45









        Akand

        426




        426






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437118%2fsorting-out-directories-by-searching-certain-file-extension%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)