for i in `ls -l`; do for j in `ls -l /abc`; do cat $j | grep $i;done;done;
I want to search for each filename in pwd with files in abc directory. Please help me in this.
for i in `ls -l`; do for j in `ls -l /abc`; do cat $j | grep $i;done;done;
I want to search for each filename in pwd with files in abc directory. Please help me in this.
I guess you want:
for filename in * ; do
grep -F -- "$filename" /abc/*
done
Note that, due to the nature of grep
, this will not work properly if any of your filenames contain newlines. (Fortunately, that's quite rare; but it's something to keep in mind.)
Something like this would be the right approach IF the files in your pwd cannot contain newlines in their names:
ls |
awk '
NR==FNR{ files[$0]; next }
{
for (file in files) {
if ( index($0,file) ) {
print FILENAME, file
}
}
}
' - /abc/*
but, like your grep approach, it's error prone as a file named "the" would match a line containing "then", and it'd report the same match multiple times if it appeared multiple times in a target file. If any of that's an issue update your question to show some sample input and output so we can help you.
Not really a programming question.. but what about just using find?
find abc -type f -name $filename
..
Even if the above does not do exactly what you want (which is a bit unclear to me), using a nested ls
is pretty crazy if you already have a method which is ment to do just that.
man find
if you want to learn more about the find command.
To do more with the files you located, you can use the -exec flag to execute something for each found file.