Mutiple For loops - moving files to another directory

I need to clean out some application subdirectories from backup scripts we used to rename to various backup extensions just in case the script failed in production and we need to rollback. I will be moving these old scripts to a staging directory and then removing them after 30 days (I have the find command for removing scripts after so many days).

The subdirectories would be application directories in a specified place with another subdirectory that would contain the scripts, both of which I have to loop through, they will look like this: /dir/dir//scripts (the �' is the application subdirectory that I will have to loop through, like /dir/dir/finance/scripts or /dir/dir/admin/scripts for example - there are around 15 of them or so). So I have to loop through the application directories, go into their scripts directory, and loop through the list of files to find any backup files and move them to a new staging directory like (/dir/dir/staging).

These scripts could have one of a variety of possible extensions: kshO, .bkp, .bkup, .OLD, or _OLD
The environment is KSH.

My knowledge of Unix at this point is limited to simple loops, so I'm not sure how to do 2 loops in 2 different places yet - could it be as simple as nesting a for loop in another for loop?

Does this work for you:

mDirs="/dir/dir/*/"
for mFile in ${mDirs}.[kshO|bkp|bkup|OLD|_OLD]
do
  echo "mFile = "$mFile
done

I tried it as you stated and it came back with a syntax error of '|' unexpected. I then tried it with ||, and [[ ]]. Using your logic, I will research some more. If you have any other ideas, I'm appreciative!

You have to be in a POSIX shell to have that work - like bash or ksh.

Sorry Tekster -- here is another version:

mDirs="/dir/dir/*/*"
for mFile in ${mDirs}
do
  mExt=`echo $mFile | sed 's/.*\.//'`
  case ${mExt} in
    kshO|bkp|bkup|OLD|_OLD) echo "mFile = "${mFile};;
  esac
done

With find:

find /dir/dir \( -name "*.kshO" -or -name "*.bkp" -or -name "*.bkup" -or -name "*.OLD" -or -name "*._OLD" \) -exec mv {} /dir/dir/staging \;

Regards

Hey ShellLife,

That last one seemed to work better, I got some results back, but not quite as I expected, but getting closer ! :slight_smile: At some point, I will have to exchange the last '' for the directory "scripts" because I will be searching only the scripts folder in each application directory (/dir/dir//scripts), but for now, I'm just trying to work with the code you're suggesting. Funny, when I tried it several more times, I didn't get anything back (I checked my syntax over and over again). At any rate, this is what it gave me:

ksh[3]: mExt/dir/dir/admin/data: not found
ksh[3]: mExt/dir/dir/admin/scripts: not found
ksh[3]: mExt/dir/dir/finance/data: not found
ksh[3]: mExt/dir/dir/finance/scripts: not found

So it seems to be finding the folders, but not the stuff listed in the case statement, and it seems to be tacking on mExt to the end. I like the idea of the case statement though, it seems to be helping. I'll keep playing with it...

Hello again,

I've changed the script to use an AWK statement instead and I kind of got it to work, but I'm still getting some errors along with correct returns. This is what I have so far (I put an asterisk on the end of 'scripts' because I have copies of the same 4 backup scripts (only '.bkp' extension for now) in 2 folders: 'scripts' and 'scripts2'):

mDirs=�/dir/dir/dir/scripts*�
mFiles=`ls ${mDirs}`
for file in ${mFiles}
do
mExt=`echo $file | awk �BEGIN { FS �.� } $NF ~ /O$|bkp$/ {print $NF}'`
if [ ${mExt} = ${file} ]
then
echo ${mExt}
fi
done

I set it up to return from 2 directories, 4 files each. This is what it is returning:

ksh[4]: test: argument expected
ksh[4]: test: argument expected
file1.bkp
ksh[4]: test: argument expected
file2.bkp
ksh[4]: test: argument expected
file3.bkp
ksh[4]: test: argument expected
file4.bkp
ksh[4]: test: argument expected
ksh[4]: test: argument expected
file1.bkp
ksh[4]: test: argument expected
file2.bkp
ksh[4]: test: argument expected
file3.bkp
ksh[4]: test: argument expected
file4.bkp

Any ideas on what I'm doing wrong?

mv /dir/dir/*/scripts/*.+(kshO|bk?(u)p|?(_)OLD) /dir/dir/staging

Thank you everyone!

I figured out why I was getting my error, I needed double quotes around the variables in my if statement:

if [ "${mExt}" = "${file}" ]

Still learning I guess - I forgot about the arithmetic thing (if it's not arithmetic, it should be in quotes).

Thanks ShellLife for all your time and great ideas, and thanks Radoulov for that final piece - I was going to do it a different way but now I see that wouldn't have worked, your way is better!