Computing.Net > Forums > Unix > A simple script for creating a link

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

A simple script for creating a link

Reply to Message Icon

Name: amoati
Date: June 17, 2005 at 02:10:39 Pacific
OS: HP-UX
CPU/Ram: HP-UX B11.11
Comment:

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 :)



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: June 17, 2005 at 04:02:56 Pacific
Reply:

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


0

Response Number 2
Name: amoati
Date: June 17, 2005 at 04:50:59 Pacific
Reply:

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 :)


0

Response Number 3
Name: Jim Boothe
Date: June 17, 2005 at 06:51:50 Pacific
Reply:

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.


0

Response Number 4
Name: amoati
Date: June 17, 2005 at 09:04:23 Pacific
Reply:

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 :)


0

Response Number 5
Name: Jim Boothe
Date: June 17, 2005 at 10:15:49 Pacific
Reply:

OK, got it. I will give it some thought.


0

Related Posts

See More



Response Number 6
Name: David Perry
Date: June 17, 2005 at 10:36:38 Pacific
Reply:

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.


0

Response Number 7
Name: amoati
Date: June 18, 2005 at 02:08:03 Pacific
Reply:

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 :)


0

Response Number 8
Name: cislord
Date: June 19, 2005 at 19:32:43 Pacific
Reply:

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


0

Response Number 9
Name: David Perry
Date: June 20, 2005 at 06:20:11 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: A simple script for creating a link

Unix shell script for opening a file and www.computing.net/answers/unix/unix-shell-script-for-opening-a-file-and/3452.html

Simple script to copy files www.computing.net/answers/unix/simple-script-to-copy-files/4521.html

creating a UNIX phonebook www.computing.net/answers/unix/creating-a-unix-phonebook/2652.html