Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm getting frustrated attempting to write this shell script.
What it needs to do is parse all the symbolic links in the /home/sites directory and change the links so instead of being linked to /home/sites/some/path/ it's linked to ./some/path.
What books, etc. are you using to learn shell scripting?
The way to develop a script is to first test the various commands to make sure they do what you want. The first thing is to find all the lines in "ls -l" that begin with "l"
ls -l | grep "^l"
AFAIK, you cannot modify the target of a link--I think you have to delete them and make new ones. One straightforward way would be to store the link name & target in an array. Then read the array and process the target to create the new name. Finally, issue ln -s for each element of the array.
To grab the link names, etc. you can use awk:
Here is a sample output from the command above:
lrwxrwxrwx 1 mherring users 3 2007-06-04 12:03 das -> DAS
The link name and the target are simply the 8th and 10th fields (make sure awk is using "space" as the delimiter)
I hope this at least gets you started in the right direction.
You will need to remove the link and recreate it. Moving a link only moves the source, not the target of the link. Hope this helps.
#!/bin/sh
# Use at your own risk. Feel free to modify and share.
# used /etc to test the script since I don't have a "/home/sites" directory
SRCDIR=/etc
#SRCDIR=/home/sites
# save the current directory so we can come back.
CURRENT_DIR=`pwd`
# change to the directory containing the links
cd $SRCDIR
# List any links in the directory, get their target and source, and then see if
# the target is is in the source directory (so we can make it relative)
for FILE in `ls -la $SRCDIR | grep "^l" | awk '{print $10 ":" $8}'| grep "^$SRCDIR"`; do
TARGET=`echo $FILE | awk -F: '{print $1}'`
SRC=`echo $FILE | awk -F: '{print $2}'`
echo $SRC '->' $TARGET
# I'm not exactly sure how you want to change TARGET to NEW_TARGET
# so I'm guessing you simply want to remove the source directory from the
# beginning of the path.
sep=$'\001' # Use Control-a as separator, it will never be in a dir name
replace=.
NEW_TARGET=`echo $TARGET | sed "s${sep}^${SRCDIR}${sep}$replace${sep}"`
echo $SRC '->' $NEW_TARGET
# commenting out the dangerous stuff until you test it.
# rm $SRC
# ln -s $NEW_TARGET $SRC
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.