===== BASH For Loop =====
{{tag>Linux}}
Here is an example of a double for loop that will go into each directory of rsyslog and remove the archive files.
[root@MATX01LIMO01 rsyslog]# pwd
/var/log/rsyslog
\\
Here is the long written out format for ease of reading;
for i in *
do
for j in {1..7}
do
rm -rf $i/messages.$j.txt
done
done
\\
Here is the single line CLI version for understanding where the ";" breaks are;
[root@MATX01LIMO01 rsyslog]# for i in *; do for j in {1..7}; do rm -rf $i/messages.$j.txt; done; done
Example of for loop through a bash array for seeing of rsync destination directory is available;
#!/bin/bash
DIRECTORY=(
/mnt/drive1/folder\ one/
/mnt/drive2/folder2/
)
for ((i = 0; i < ${#DIRECTORY[@]}; i++))
do
if [ -d "${DIRECTORY[$i]}" ]; then
echo -e "\n${DIRECTORY[$i]}"
ls "${DIRECTORY[$i]}"
# -n = --dry-run and is used for testing
#rsync -n -v -h -r -P --append --exclude .example1 --exclude example2 /home/USERNAME/SOURCEDIRECTORY "${DIRECTORY[$i]}"
fi
if [ ! -d "${DIRECTORY[$i]}" ]; then
echo -e "\n${DIRECTORY[$i]} is not available"
fi
done
echo -e ""