Skip to content
Blog Inline Shell Script with a For Loop

Inline Shell Script with a For Loop

Often I’m asked how to traverse a list of items in a file. You can easily go through a list of items using a for loop. Here’s an example of copying selected contents of originalDir to destinationDir via the command line.

#for i in `cat /home/esofthub/mylist.dat`


#cp -pr /originalDir/$i /destinationDir/.

#echo $i done

sh – shell

cat – lists each item in the list one iteration at a time

cp – the copy utility for a local workstation (plain files and directories, no symbolic links)

-pr – these options preserve the permission, “p” and copies recursively, “r”

echo – lists the item copied

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.