Some examples of iterating through files and looping through file content in Bash. For details, see the Bash man page or the Bash reference manual.
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
Comments
tiffany jewelry Choose, buy
tiffany jewelry
Choose, buy and shop for on sale tiffany jewelry including Tiffany & Co Silver Necklace, Pendants, Bangles, Bracelets, Earrings, Rings and Accessories.
tiffany co
Tiffany Jewellery offering bangle Jewellery, bracelet jewelry, eardrop jewelry, necklace jewelry, ring jewelry, finger ring jewelry and earring jewelry
tiffany
tiffany and co
links of london
links london
Tiffany Style Silver Jewelry: Rings, Earrings, Necklaces, Bracelets and more Tiffany Jewellery at low prices.
replica Audemars Piguet
replica Audemars Piguet Watches
fake Audemars Piguet Watches
B.R.M Watches
replica B.R.M Watches
Chopard Watches
replica Chopard Watches
fake Chopard Watches
Christian Dior watches
Rolex Yachtmaster
replica Rolex Yachtmaster
fake Rolex Yachtmaster
A.Lange & Sohne watches
replica A.Lange & Sohne watches
fake A.Lange & Sohne watches
Alain Silberstein watches
replica Alain Silberstein watches
fake Alain Silberstein watches
Audemars Piguet Watches
replica Audemars Piguet Watches
fake Audemars Piguet Watches
B.R.M Watches
replica B.R.M Watches
Chopard Watches
replica Chopard Watches
fake Chopard Watches
Christian Dior watches
replica Corum Watches
fake Corum Watches
Looping through the content
Looping through the content definitely does work well. IT definitely is a good process to use.
whistleblower
Thanks for useful
Thanks for useful guidelines!
Web design company
custom wordpress skins
blog website design
Gucci Gucci 'Abbey' Medium
Gucci
Gucci 'Abbey' Medium Tote
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Bag
Gucci 'Babouska' Evening Clutch
Gucci 'Babouska' Evening Clutch
Gucci 'Babouska' Evening Clutch
Gucci 'Babouska' Evening Clutch
Gucci 'Babouska' Evening Clutch
关键词
Gucci 'Babouska' Evening Clutch
Gucci 'Babouska' Large Boston Bag
Gucci 'Babouska' Large Boston Bag
Gucci 'Babouska' Large Boston Bag
Gucci 'Babouska' Large Boston Bag
Gucci 'Babouska' Large Shoulder Bag
tiffany jewelry Choose, buy
tiffany jewelry
Choose, buy and shop for on sale tiffany jewelry including Tiffany & Co Silver Necklace, Pendants, Bangles, Bracelets, Earrings, Rings and Accessories.
tiffany co
Tiffany Jewellery offering bangle Jewellery, bracelet jewelry, eardrop jewelry, necklace jewelry, ring jewelry, finger ring jewelry and earring jewelry
tiffany
tiffany and co
links of london
links london
Tiffany Style Silver Jewelry: Rings, Earrings, Necklaces, Bracelets and more Tiffany Jewellery at low prices.
always provide GUCCI Replica
always provide GUCCI Replica Handbags, and all kinds of replica GUCCI Handbags lower price for sale, the more fake GUCCI Handbags you choose, the cheaper price you get.
As everyone knows that the Chanel Handbags are popular in the whole world, sell replica Chanel Handbags can get more profit, many people like to sell chanel bags because fashion women love them.
Taking the Prada Handbags to join the big party can show women's fashion, so even the poor people like to buy replica Prada Handbags, so when you walk on the road, you never surprised by so much fake Prada Handbags, do you feel have a Prada replica Handbags is a cool action?
Fashion women love Miu Miu Handbgs, but the price is to high, so many ladies choose Miu Miu replica Handbgs, in the replica Miu Miu Handbgs shops, you will get the popular fake Miu Miu Handbgs that you love.
Gucci 'D-Gold' Medium
Gucci 'D-Gold' Medium Tote
Gucci 'D-Gold' Messenger Bag
Gucci 'D-Gold' Small Tote
Gucci 'Guccioli' Medium Tote
Gucci 'Hysteria' Clutch
Gucci 'Hysteria' Large Top Handle Bag
Gucci 'Hysteria' Large Top Handle Bag
Gucci 'Hysteria' Large Top Handle Bag
Gucci 'Hysteria' Large Tote
Gucci 'Hysteria' Large Tote
Gucci 'Hysteria' Medium Shoulder Bag
v
Thanks for the tutorial in
Thanks for the tutorial in iterating through files and looping through file content in Bash
I think there is a typo: >
I think there is a typo:
> For more control, use /usr/bin/file
should be:
> For more control, use /usr/bin/find
Consider using find and
Consider using find and xargs for doing something across a group of files without risking breaking your script because you're operating on too many files (shell wildcard expansion has limits). Using GNU find and xargs:
find . -name '*.foo' -type f -print0 | xargs -0I {} ~/bin/my-script '{}'will run "/bin/my-script" on all files (-type f) whose name ends in ".foo" in the current working directory (the dot after the find command) and every directory therein (find is recursive unless you tell it not to be). When you make /bin/my-script all you need to do is describe how to process one file.
find will normally print the list of matching files with a newline. Here we tell find to use NULLs to terminate each list entry (-print0) you can handle pathnames with spaces and other characters that break most scripts. The -0 tells xargs what to use to know where each filename ends. Just don't forget to quote properly in your script (called /bin/my-script here) so the complete pathname is preserved.
I'm also telling xargs where each filename is in the command (-I {} says to use the open/close curly brace as a placeholder for the pathname). This is optional if the last argument to the command is the pathname.
If you have files or
If you have files or directories that have spaces in them, one way to loop over them is with read:
ls * | while read X; do echo cp -p $X ${X}.bak; done
Cheers,
Jeff McCune
That is a very useful hint,
That is a very useful hint, Jeff. Thank you for it!
Peter