Operating System - Linux
1753708 Members
4761 Online
108799 Solutions
New Discussion юеВ

Scripting Question CIFS file moves

 
SOLVED
Go to solution
rmueller58
Valued Contributor

Scripting Question CIFS file moves

I have two CIFS mounts to microsoft boxes, on a linux box,

Currently the dba's are creating PDF files with following naming conventions:

2006 - 2007_Forms_yayyaayayay.pdf

When I try to define a filename variable with in a script:
SCRIPT
#!/bin/bash

export source=/wst-tas/userdir
export dest=/wst-docs/employee

cd $source
for dirname in `ls $source`

do
for fn in `ls $dirname`
do
echo "$fn"
done
read

done

the scripts see the "2006" "-" as file names when passed as variables?

Is there a way to define the filename variable when the filename contains "spaces" in the filename from the BASH shell?

I need to copy files from $source/$EMPID to $destination/$EMPID and it the file exists in destination to ignor and go on to next file.

Any assistance appreciated

Rex M - ESU#3 LaVista NE
6 REPLIES 6
Heironimus
Honored Contributor
Solution

Re: Scripting Question CIFS file moves

Any time you're using `ls foo` in a script you're almost certainly doing something wrong. You probably want something like this:

for fn in ${source}/*/* ; do
d=$(echo "${fn}" | sed -e "s#${source}#${dest}#")
if [ ! -f "${d}" ] ; then
rm -f "${d}"
cp "${fn}" "${d}"
fi
done
Stuart Browne
Honored Contributor

Re: Scripting Question CIFS file moves

You can do quoted-strings for file names, but if you are wanting to do loops, you'll have to start messing with a few other things.

If you want to stick with 'for' and 'ls', you'll need to mess with the 'IFS' so it doesn't treat a space as a seperator.

So, first solution (probably the easiest):

-=-=-=-=-=-=-=-=-=-=-=-
#!/bin/bash
export SRCE=/wst-tas/userdir
export DEST=/wst-docs/employee

cd $SRCE
for FN in *
do
[ ! -f "${DEST}/${FN}" ] && /bin/cp "${FN}" $DEST/
done
-=-=-=-=-=-=-=-=-=-=-=-

Why is this the easiest? Using '*' instead of 'ls $source' keeps the file names intact even with a space in the IFS.

Using "${FN}" will ensure that the spaces within the file name are kept intact during the copy also.

The "[ -f ... ]" will check for the existance of the destination file.

If you insist in using 'ls', you should use a 'while' loop, which makes this a bit messier:

-=-=-=-=-=-=-=-=-=-=-=-
#!/bin/bash
export SRCE=/wst-tas/userdir
export DEST=/wst-docs/employee

cd $SRCE
while read FN
do
[ ! -f "${DEST}/${FN}" ] && /bin/cp "${FN}" $DEST/
done < <(/bin/ls -1 ${SRCE})
-=-=-=-=-=-=-=-=-=-=-=-

As you can see, not much difference, but you're sub-shelling out to do the 'ls'.

If there are a fantastic amount of files in the directory (many many thousands), you may need to use this method, as shell environment space isn't limitless, and the '*' method expands into the current environment, where as this method uses file-descriptors to pass the list.

Anyway, some things for you to think about.
One long-haired git at your service...
James R. Ferguson
Acclaimed Contributor

Re: Scripting Question CIFS file moves

Hi Rex:

Here's another way to attach this:

#/usr/bin/sh
ls ${source} | while read FILE
do
cp "${FILE}" "${DESTDIR}/${FILE}"
done

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Scripting Question CIFS file moves

Hi Rex:

Here's another way to attach this:

#/usr/bin/sh
ls ${source} | while read FILE
do
cp "${FILE}" "${DESTDIR}/${FILE}"
done

Regards!

...JRF...
rmueller58
Valued Contributor

Re: Scripting Question CIFS file moves

Thanks all,

that did the trick
rmueller58
Valued Contributor

Re: Scripting Question CIFS file moves

all items help, my script was looking at the output of a ls, i've got several scripts that work in that what, but the file names are without spaces present. The spaces screwed me up.