1. create a function to generate file with the filename start with 10 random characters
#!/bin/bash WORK_DIR=/tmp/00 #define the function create(){ i=1 while [ $i -le 5 ] do if [ ! -d $WORK_DIR ] then mkdir -p $WORK_DIR cd $WORK_DIR && touch `</dev/urandom tr -dc "a-z" | head -c 10`_test.txt else cd $WORK_DIR && touch `</dev/urandom tr -dc "a-z" | head -c 10`_test.txt fi i=$(( $i+1 )) done } # invoke the function create
2. Using for loop to write a function to rename file in batch.
#!/bin/bash changeName(){ DIR=/tmp/test01 FILE=`ls $DIR` postfix=a.txt for i in $FILE do c=`echo $i |cut -c 1-10` mv $DIR/$i $DIR/$c$postfix echo "The file $i was renamed to $c$postfix" done } changeName
3. using for loop print the odd number between 1 - 100
for((i=1;i<=100;i++)) do if ((i%2==1)) then echo $i continue fi done
4. using while loop print the odd number between 1 - 100
i=1 while(($i<=100)) do if(($i%2==1)) then echo $i fi i=$(($i+1)) done
5. Another way to print the odd number
for i in {1..100..2} do echo $i done
6. print the file name for a folder
for f in `ls ~` do name=`echo "$f" | awk -F. '{print $1}'` echo $name done