read file by file in a directory

Hi

I have directory which has 5 files names test1, test2, test3, test4, test5
Now i want to read file by file and generate md5checksum.
Command used is
md5sum test1.
How can i read file names one after the other and generate checksum and send the output to a file.
Please reply

for i in `ls`
do
md5sum $i
done

Is always a bad idea. It's inefficient, since it involves forking another shell and exec'ing ls. Worse, if any of the ls output contains file or directory names which contain IFS characters (space/tab/newline by default), they'll be mangled. Just use a simple glob in this case; it's faster and not subject to field-splitting breakage.

for i in *

Regards,
Alister

2 Likes

So altogether it becomes

for i in *
do
   md5sum "$i"
done

or more specific

for i in test[1-5]
do
   md5sum "$i"
done