Files which are being used by any process should not get copied.
Issue
The following information has been provided by Red Hat, but is outside the scope of the posted Service Level Agreements and support procedures. The information is provided as-is and any configuration settings or installed applications made from the information in this article could make the Operating System unsupported by Red Hat Global Support Services. The intent of this article is to provide information to accomplish the system's needs. Use of the information in this article at the user's own risk.
Issue
- Move all txt files from /home/$USER to /commonfiles. However files which are being used by any process or user should not get copied.
Environment
- Red Hat Enterprise Linux
Resolution
Following script can be used to copy files from one directory to another excluding files which are in open mode.
#!/bin/bash
find /home/user -iname "*.txt" | awk -F'/' '{print $NF}' > testfile
test=`cat testfile | cut -d '/' -f 4`
for id in $test
do
/usr/sbin/lsof | grep $id &>/dev/null
OUT=$?
if [ $OUT -ne 0 ];then
filen=`grep $id testfile`
find /home/user -iname $filen -exec cp {} /commonfiles/ \;
fi
done
Set executable (x) permission on the script, then execute periodically using crontab.
Subscriber exclusive content
A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
