I am writing a shell script (runs on HP Unix) which copies files from a source directory to another destination daily. The destination directory always have the files with same name as in the source directory. And daily a new file will be created in the source.
cp command works fine if the file in the destination directory is not in use (not opened by a user).
But fails to copy, if the file in the destination directory is opened. Any ways to copy a file even if it is opened or in use by a user?
Example code I am using:
cp ${sourcepath}/filename ${destination}/filename
Have tried cp with " -f " option too.
Thanks,
Arun
You can mv (rename, or relocate) an open file as long as the mv destination is on the same filesystem the file currently lives on.
So, try this, an assumption is you have a text file with a list of files to cp, also that there are no permissions errors, we assume the only error is 'open file':
#!/bin/ksh
# create dumping ground directory
[ -d /path/to/destination/tmp ] || mkdir /path/to/destination/tmp
# process the list of files
while read fname
do
cp $fname /path/to/destination
if [ $? -ne 0 ] ; then
base=$( basename $fname)
mv /path/to/destination/${base} /path/to/destination/tmp/${base}
cp $fname /path/to/destination
fi
done < textfile_with_names_to_cp