Find the answer to your Linux question:
Results 1 to 5 of 5
Hello , I am trying to search particular directory which has files with extensions like .html,.mp3,.xml etc I have a list of such files What I am doing in my ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82

    Exclamation [SOLVED] Searching File in directories


    Hello ,

    I am trying to search particular directory which has files with extensions like .html,.mp3,.xml etc I have a list of such files

    What I am doing in my script is

    for file_name in `find /home/ -name index.html -o -name song.mp3 -o -name help.xml`;
    do
    if [ $file!='' ]
    then
    dir=`echo "$file_name"| sed s'/\/index.*$\|song.*$\|help.*$//'`;
    echo $dir;
    fi
    done

    I have around 100+ files name with some particular extension , this code works fine if the directory name does not have any special character in it like " " (white character) .

    It is failing to give the output.

    IF I run the find command on the console the I am getting the correct file name with location
    =========================
    /home/user1/public_html/mediawiki/config/movie.mp3
    /home/user1/public_html/mediawiki/movie.mp3
    /home/user1/public_html/mediawiki/pop.mp3
    /home/user2/public_html/index test/pop.mp3
    /home/user2/public_html/index test/web.xml

    /home/user2/public_html/song.mp3
    =========================

    If I echo the file name from the script Then output is little missed match when a directory name has special char like " "

    =========================
    /home/user1/public_html/mediawiki/config/movie.mp3
    /home/user1/public_html/mediawiki/movie.mp3
    /home/user1/public_html/mediawiki/pop.mp3
    /home/user2/public_html/index
    test/pop.mp3
    /home/user2/public_html/index
    test/web.xml

    /home/user2/public_html/song.mp3
    =========================

    I am not sure what is wrong with script or my code .. Does shell have un-controlled behavior of manipulating the output of any command.

    I am stuck with this any help will be really appreciated.

    Thank you
    Pratap

  2. #2
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82
    Any clue .. I am not able to find out any solution for this problem .

    Thank you
    pratap

  3. #3
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82
    OK I have now find the correct way to do this but now I am facing another issue with regex.

    My code is like
    #!/bin/bash
    find /home/$user/ -name index.html -o -name song.mp3 -o -name help.xml | while read file
    do
    if [ "${file}"!='' ]
    then
    echo "Files found $file";
    dir_name="`echo "$file" | sed s'/index.*$\|song.*$\|help.*//'`";
    echo -e "Dir To check$dir_name\n";
    fi
    done

    It is printing the file name correctly as desired now what is happening is while replacing the file name from the absolute path of file name it is replacing the folder name if the folder name is same as the file name :


    File Found /home/user1/public_html/mediawiki/config/movie.mp3
    Dir to check /home/user1/public_html/mediawiki/config/
    File Found /home/user1/public_html/mediawiki/movie.mp3
    Dir to check /home/user1/public_html/mediawiki/
    File Found /home/user1/public_html/mediawiki/pop.mp3
    Dir to check /home/user1/public_html/mediawiki/
    File Found /home/user2/public_html/index test/pop.mp3
    Dir to check /home/user2/public_html/
    File Found /home/user2/public_html/index test/web.xml
    Dir to check /home/user2/public_html/

    File Found /home/user2/public_html/song.mp3
    Dir to check /home/user2/public_html/

  4. #4
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82
    I have found the solution for it now, And everything is working like as it was expected :

    #!/bin/bash
    find /home/ -name index.html -o -name song.mp3 -o -name help.xml | while read file
    do
    if [ "${file}"!='' ]
    then
    echo "Files found $file";
    file_to_rep=`echo "$file" | rev | cut -d"/" -f1 | rev`;
    dir_tocheck="`echo "$file" | sed s"/$file_to_rep$//"`";
    echo -e "Dir To check $dir_name\n";
    fi
    done

  5. #5
    Just Joined! pratapsingh's Avatar
    Join Date
    Feb 2009
    Posts
    82

    Thumbs up

    loop can be also replaced via

    #!/bin/bash

    while read -r file
    do
    echo "File found $file"
    echo "Dir to check ${file%/*}"
    done< <(find home -name '*.html' -o -name '*.mp3' -o -name '*.xml')

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •