Linux Shelling Programming Warm-up Example – For Loop

This little program:
1. loop though files in "linuxPractice" directory
2. put file names to a file "new.txt"
3. copy every file to the "new" directory

for i in /usa/programcreek/linuxPractice/*
 do
        if [ -f $i ]
        then
        #check if the file is there by using [-f 4i]                                                                                                                                                                                                               
        filename=${i#/usa/programcreek/linuxPractice/}
	  #check file name, that is, exclude path
            echo   $i $filename | cat >> new.txt
 	  #use pipe redirect two file names to new.txt
        cp -p $i /usa/programcreek/linuxPractice/new/$filename
	  #copy file to new directory
        fi
done
Category >> Uncategorized  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. ryanlr on 2014-8-12

    No space between asignment variable and expression!

Leave a comment

*