bash loop through files

Loop Through Files

for FILE in *; do
# do something with $FILE
echo “File: $FILE”
done
For more control, use /usr/bin/file

for FILE in $(find ./ -name *.html -type f); do
# do something with $FILE
echo “HTML File: $FILE”
done
Loop Through Lines in a File

while read LINE; do
# do something with $LINE
echo “Line: $LINE”
done < /etc/hosts
Loop Through Words in a File

for WORD in $(cat /etc/hosts); do
# do something with $WORD
echo "Word: $WORD"
done
Loop Through Characters in a File

while read -n1 CHAR; do
# do something with $CHAR
echo "Character: $CHAR"
done < /etc/hosts

Advertisements