Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
What I need to do is very simple :) I just need to run a script every hour to check on a certain directory on the HP-UX server we have e.g. /tmp/tools to know what are the new files created in that directory and then it creates a link of those files in another directory e.g. /home/admin It will be a link just a link of the newly created files from the first directory into the second directory.
Thanks a lot :)

cd /home/admin
for file in /tmp/tools/* ; do
$file=`basename $file`
if [ ! -a $file ] ; then
ln -s /tmp/tools/${file} $file
fi
done

Thanks David for your reply :) but your solution depends on that the old links are still in the /home/admin so what if these old links are being deleted regularly? I mean that the files are written in the first directory and then the link is created in the second directory. then we go fetch the files through the links and then delete the links for the files we fetched! And so on there will be no more than around 4 links in the second directory. I hope I cleared it a little bit more. And thanks once again :)

I think David's script does what you want, but the statement:
$file=`basename $file`
needs to have the leading $ removed.
His script does not care if the files in /tmp/tools are newly created or not, but for each file in /tmp/tools, his script confirms that there is "something" of that name in the target directory, and if not, creates a link for that file.
If you want to confirm that the "something" in the target directory is in fact a link and not something else such as a regular file or a directory, then use -h $file instead of -a $file.

It seems that I didn't explain myself well in the first response because what I meant agrees with what you said Jim but I ll explain more by giving an example:
- let's assume that file1 and file 2 were created in the first directory so when this script proposed by David runs for the first time it will create a link of both files in the second directory.
- Further if file3 was created in the first directory and for instance file1 link was removed from the second directory
- then when this script runs at the next time it will find that there are no links for file 1 and file3 and create the links again! while I want is for this script to create a link for file3 only and ignore file1 which was already created earlier by the script.
If I'm still not making myself clear please let me know and thanks very much for both of you :)

LOGFILE=/tmp/link.log
cd /home/admin
for file in /tmp/tools/* ; do
file=`basename $file`
if [ ! -a $file -a `grep -w -c $file $LOGFILE` -eq 0 ] ; then
ln -s /tmp/tools/${file} $file
echo $file >> $LOGFILE
fi
done=====
Sorry about the leading $. You are right.

Ok I belive that ll do it as long you will keep the history of the move in the log file :)
Thanks a lot but can you please explain me the if statement and its arguments a little bit more?Thanks again :)

Here's a method I use to find new files and I've modified it to create a link. No need to keep a log with this method BUT it does assume that this process runs every hour.
============================================
# Get the time one hour previous
# Note - I'm in Melbourne Australia, so you'll have to fiddle with the
# TZ=EST-8 equation to suit your location
export ONE_HRS_PREV=$(TZ=EST-9;date '+%Y%m%d%H%M')# Create a temp file
TEMP_TIME_FILE=/tmp/${ONE_HRS_PREV}.$$# Create and change the temp file's timestamp
touch -amt "$ONE_HRS_PREV" $TEMP_TIME_FILE# Now find all the files newer that the temp file in /tmp/tools
# and create a link in /home/admin
find /tmp/tools -type f -newer $TEMP_TIME_FILE | while read F
do
NEW_LINK_NAME=`basename $F`
ln -s $F /home/admin/$NEW_LINK_NAME
done# Cleanup
rm -f $TEMP_TIME_FILE

for file in /tmp/tools/* ; do
# get a list of files under /tmp/tools
file=`basename $file`
# strip the directory from the file name
if [ ! -a $file -a `grep -w -c $file $LOGFILE` -eq 0 ] ; then
# if the file does not exist in the current directory and has zero occurance in the log file
ln -s /tmp/tools/${file} $file
# create the link
echo $file >> $LOGFILE
# add the file to the log file
fi
done

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |