View previous topic :: View next topic |
Author |
Message |
water Guru
Joined: 19 Jun 2002 Posts: 387 Location: Zierikzee, The Netherlands
|
Posted: Thu Feb 19, 2004 1:15 pm Post subject: Script for creating symlinks |
|
|
I've made a simple script to create a large amount of symlinks at once:
Code: |
#!/bin/bash
#$1 = input directory
#$2 = output directory
# read original files and write them to a tempory file
ls $1 > ~/.links_tmp
# read tempory file
exec < ~/.links_tmp
# create symlinks for each file that is listed in the temproy file
while read ORG_FILE;
do
ln -s $1/$ORG_FILE $2/$ORG_FILE
done
#remove the tempory file
rm ~/.links_tmp
|
Use it with: scriptname inputdir outputdir
I use it to create symlinks for cursors, backgrounds, fonts, etc.
Enjoy. _________________ Groeten uit Holland |
|
Back to top |
|
|
boroshan l33t
Joined: 16 Apr 2003 Posts: 730 Location: upside down
|
Posted: Thu Feb 19, 2004 3:37 pm Post subject: |
|
|
I always find it interesting to see how someone else solves a probelm. This is how I would do that:
Code: | #! /bin/bash
#
# named parameters
#
src_dir=$1 ; shift
dest_dir=$1 ; shift
#
# check arguments
#
if [ x$dest_dir = x ]
then
echo "usage: $(basename $0) source_dir destination_dir"
exit 1
fi
#
# loop through files in source directory
#
for file in $src_dir/*
do
ln -s $file $dest_dir
done
exit 0
|
mainly a question of style, but if you're doing anything non-trivial, the temp file and the exec will both cost you time.
There's also the issue of temp files getting left lying around if the script fails or is interupted. A good approach is to use trap to catch program exit:
Code: | #! /bin/bash
#
# generate a unique temp file name
#
script=`basename $0`
tempfile=`mktemp -t $script.XXXXXXXXXX`
#
# make sure it gets deleted - -f supresses error messages if it don't exist yet
#
trap "rm -f $tempfile" 0
#
# create it
#
touch $tempfile
#
# make sure it's there
#
ls $tempfile
#
# exit prematurely
#
exit
#
# and this never gets done
#
echo never happens!
rm $tempfile
|
You can check after it runs to see that the file no longer exists. _________________ Don't let THEM immanentize the Eschaton! |
|
Back to top |
|
|
TheEternalVortex Apprentice
Joined: 15 Oct 2002 Posts: 207 Location: San Jose, CA
|
Posted: Thu Feb 19, 2004 8:46 pm Post subject: |
|
|
Wouldn't it be easier to use cp?
$ cp -s mydir/* mydir2 _________________ -- Andy |
|
Back to top |
|
|
water Guru
Joined: 19 Jun 2002 Posts: 387 Location: Zierikzee, The Netherlands
|
Posted: Fri Feb 20, 2004 10:56 am Post subject: |
|
|
boroshan wrote: | I always find it interesting to see how someone else solves a probelm. |
I still don't know very much about bash-scripting, so i use the things i know.
But i will study your solutions, so i can learn from it.
TheEternalVortex wrote: | Wouldn't it be easier to use cp?
$ cp -s mydir/* mydir2 |
Linking saves diskspace. _________________ Groeten uit Holland |
|
Back to top |
|
|
Dolio l33t
Joined: 17 Jun 2002 Posts: 650
|
Posted: Sat Feb 21, 2004 2:01 am Post subject: |
|
|
"cp -s" creates sym-links. _________________ They don't have a good bathroom to do coke in. |
|
Back to top |
|
|
boroshan l33t
Joined: 16 Apr 2003 Posts: 730 Location: upside down
|
Posted: Sat Feb 21, 2004 10:29 am Post subject: |
|
|
It's been a while since I last read the man page for cp. How incredibly sensible of someone.
Thank you for that. Much appreciated. _________________ Don't let THEM immanentize the Eschaton! |
|
Back to top |
|
|
tomk Bodhisattva
Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
|
Back to top |
|
|
water Guru
Joined: 19 Jun 2002 Posts: 387 Location: Zierikzee, The Netherlands
|
Posted: Sat Feb 21, 2004 1:53 pm Post subject: |
|
|
tomk wrote: | You can also use lndir |
That one is not on my system? Which package should i emerge.
BTW: thanks for the tip of cp -s. It makes my script not usefull anymore, but it's the solution of my problem. (i didn't read carefully enough the first time) _________________ Groeten uit Holland |
|
Back to top |
|
|
tomk Bodhisattva
Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
Posted: Sat Feb 21, 2004 2:01 pm Post subject: |
|
|
Code: | qpkg -f `which lndir`
x11-base/xfree *
|
Probably a bit OTT to install X just to get lndir. I think X uses it during it's installtion. _________________ Search | Read | Answer | Report | Strip |
|
Back to top |
|
|
water Guru
Joined: 19 Jun 2002 Posts: 387 Location: Zierikzee, The Netherlands
|
Posted: Sat Feb 21, 2004 2:38 pm Post subject: |
|
|
Code: |
Pentium3 / # locate lndir
/usr/X11R6/man/man1/lndir.1x.gz
/usr/X11R6/bin/lndir
/usr/portage/dev-lang/ghc/files/lndir.c
|
Found it. _________________ Groeten uit Holland |
|
Back to top |
|
|
TheEternalVortex Apprentice
Joined: 15 Oct 2002 Posts: 207 Location: San Jose, CA
|
Posted: Sat Feb 21, 2004 7:53 pm Post subject: |
|
|
lndir is much less flexible than cp, since it always recurses and can't do some of the other nifty things that cp can. _________________ -- Andy |
|
Back to top |
|
|
tomk Bodhisattva
Joined: 23 Sep 2003 Posts: 7221 Location: Sat in front of my computer
|
Posted: Sat Feb 21, 2004 9:59 pm Post subject: |
|
|
You're right, I've not used lndir but remember my friend using it for some scripts he was writting. Looking at the man page it only has 3 options, best to stick with cp. _________________ Search | Read | Answer | Report | Strip |
|
Back to top |
|
|
|