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 ...
- 02-23-2011 #1
[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
- 02-24-2011 #2
Any clue .. I am not able to find out any solution for this problem .
Thank you
pratap
- 02-25-2011 #3
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/
- 02-25-2011 #4
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
- 02-25-2011 #5
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')


