I want to write a script to loop through files in two different directories, and echo the files of different filename patterns
I made one like this but it is inappropriate
#Files in one dir ABC-123-1.txt
#Files in subdir ABC-123.doc
for f in *.doc
do
f1=`echo $f|sed 's/.doc//g`'
for f2 in ../*.txt
do
f3=`echo $f2|sed 's/..\///g'|sed 's/-1.txt//g'`
if [ "$f1" != "$f3" ]
then
echo $f3
fi
done
done
If I understand correctly what you intend to do with your script, you want to iterate over the files ending in “.doc” in dir1 and then print the files in dir2 with the same file name and ending “-1.txt”.
This can be achieved in bash with this one liner:
(replace dir1 and dir2 with your directories)
The “2>/dev/null” will ignore files that do not match in the second directory and will only print the ones that do match.
Hope that helps.