Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Script for creating symlinks
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
water
Guru
Guru


Joined: 19 Jun 2002
Posts: 387
Location: Zierikzee, The Netherlands

PostPosted: Thu Feb 19, 2004 1:15 pm    Post subject: Script for creating symlinks Reply with quote

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
View user's profile Send private message
boroshan
l33t
l33t


Joined: 16 Apr 2003
Posts: 730
Location: upside down

PostPosted: Thu Feb 19, 2004 3:37 pm    Post subject: Reply with quote

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
View user's profile Send private message
TheEternalVortex
Apprentice
Apprentice


Joined: 15 Oct 2002
Posts: 207
Location: San Jose, CA

PostPosted: Thu Feb 19, 2004 8:46 pm    Post subject: Reply with quote

Wouldn't it be easier to use cp?
$ cp -s mydir/* mydir2
_________________
-- Andy
Back to top
View user's profile Send private message
water
Guru
Guru


Joined: 19 Jun 2002
Posts: 387
Location: Zierikzee, The Netherlands

PostPosted: Fri Feb 20, 2004 10:56 am    Post subject: Reply with quote

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. :D

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
View user's profile Send private message
Dolio
l33t
l33t


Joined: 17 Jun 2002
Posts: 650

PostPosted: Sat Feb 21, 2004 2:01 am    Post subject: Reply with quote

"cp -s" creates sym-links.
_________________
They don't have a good bathroom to do coke in.
Back to top
View user's profile Send private message
boroshan
l33t
l33t


Joined: 16 Apr 2003
Posts: 730
Location: upside down

PostPosted: Sat Feb 21, 2004 10:29 am    Post subject: Reply with quote

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
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Sat Feb 21, 2004 1:17 pm    Post subject: Reply with quote

You can also use lndir
_________________
Search | Read | Answer | Report | Strip
Back to top
View user's profile Send private message
water
Guru
Guru


Joined: 19 Jun 2002
Posts: 387
Location: Zierikzee, The Netherlands

PostPosted: Sat Feb 21, 2004 1:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Sat Feb 21, 2004 2:01 pm    Post subject: Reply with quote

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
View user's profile Send private message
water
Guru
Guru


Joined: 19 Jun 2002
Posts: 387
Location: Zierikzee, The Netherlands

PostPosted: Sat Feb 21, 2004 2:38 pm    Post subject: Reply with quote

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. :D
_________________
Groeten uit Holland
Back to top
View user's profile Send private message
TheEternalVortex
Apprentice
Apprentice


Joined: 15 Oct 2002
Posts: 207
Location: San Jose, CA

PostPosted: Sat Feb 21, 2004 7:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
tomk
Bodhisattva
Bodhisattva


Joined: 23 Sep 2003
Posts: 7221
Location: Sat in front of my computer

PostPosted: Sat Feb 21, 2004 9:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum