Nested Symlinks?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested Symlinks?
# 1  
Old 09-07-2007
Nested Symlinks?

Please don't laugh or call me a fool...

I'm trying to set up a script that will go through my Music File directory and generate a set of symbolic links in a directory called "What's New". Within that directory there will be a "30 Days", "3 Months", "6 Months" and "A Year" directories. Within each of those my script should create symlinks based on the mtime of the directories that the 'find' command returns. The general reason for me doing this is that I've been downloading stuff from emusic.com and sometimes I find myself forgetting what I downloaded. Having a directory with "fuzzy" time references would go a long way to helping me remember. So I wrote the script below which almost does what I need. But it has one HUGE glaring error. For some reason, the 'ln' command creates a self referencing symlink within the trailing directory for each directory that matches my mtime arg. So once it's done, I have what I want in the "What's New" directory. But for every directory that 'find' returns I have a symlink in the last directory in the path that points to itself. I'm sure there's something stupid I'm missing in my logic with the 'ln' command. Just to make sure I'm clear, let's say my script finds a directory less than 31 days old:

/mnt/data/mp3/The Rolling Stones/Sticky Fingers

I get the symlink I want in:

/mnt/data/mp3/What's New/30 Days/The Rolling Stones/Sticky Fingers

pointing to:

/mnt/data/mp3/The Rolling Stones/Sticky Fingers

And I also get a new, unwanted symlink in:

/mnt/data/mp3/The Rolling Stones/Sticky Fingers/Sticky Fingers

pointing to:

/mnt/data/mp3/The Rolling Stones/Sticky Fingers


Obviously this is not what I want. So either I'm looping in the wrong way, or I'm making an incorrect assumption about the 'ln' command. I have to admit this is a pretty sloppy script, but I haven't had enough free time to really sit down and really set it up with internal functions. Smilie Here's my script:


Code:
#!/bin/bash

# Define any needed variables first: 'find' variables: 31, 91, 181, 366, search path, "What's New" path

MUSICPATH=/mnt/data/mp3

MONTH[1]=31
QUARTER[1]=91
HALFYEAR[1]=181
YEAR[1]=366

WHATSNEW="$MUSICPATH/What's New"
MONTH[2]="1 Month"
QUARTER[2]="3 Months"
HALFYEAR[2]="6 Months"
YEAR[2]="A Year"
OWNER="root"
GROUP="fileusers"
PERMS="755"

# Create the "What's New" directory if it doesn't exist and set proper owner:group and permissions

if test ! -e "$WHATSNEW"
then
  mkdir "$WHATSNEW"
fi

# First we scan the directory /mnt/data/mp3 for files that are newer than 30 days and dump that to a temp file.  (Maybe do 30, 90, 180, 365 days?)

for a in ${MONTH[1]} ${QUARTER[1]} ${HALFYEAR[1]} ${YEAR[1]}
do
  find $MUSICPATH -type d -mtime -$a > /tmp/$a.out
done

# Next we prep the "What's New" folder in /mnt/data/mp3.  It should be set up to contain "30 Days" "3 Months" "6 Months" "1 Year"

for a in "${MONTH[2]}" "${QUARTER[2]}" "${HALFYEAR[2]}" "${YEAR[2]}"
do
  rm -rf "$WHATSNEW/$a"
  mkdir "$WHATSNEW/$a"
done

# Then we loop through the temp file and create symlinks for the newer folders and files

for a in MONTH QUARTER HALFYEAR YEAR
do

  if test "$a" = "MONTH"
  then
    REPORT="${MONTH[1]}"
    ENDPATH="${MONTH[2]}"
  fi

  if test "$a" = "QUARTER"
  then
    REPORT="${QUARTER[1]}"
    ENDPATH="${QUARTER[2]}"
  fi

  if test "$a" = "HALFYEAR"
  then
    REPORT="${HALFYEAR[1]}"
    ENDPATH="${HALFYEAR[2]}"
  fi

  if test "$a" = "YEAR"
  then
    REPORT="${YEAR[1]}"
    ENDPATH="${YEAR[2]}"
  fi

  while read report
  do
    # The ${report:14} is to strip off the unwanted /mnt/data/mp3/ prefix
    echo ln -s "$report" "$WHATSNEW/$ENDPATH/${report:14}"
  done < /tmp/"$REPORT".out

done

chown -R $OWNER:$GROUP "$WHATSNEW"
chmod -R $PERMS "$WHATSNEW"

# This job is run daily

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rsync move with symlinks

Hi, I use rsync to move from source to target, but there are cases that I need to exclude: Suppose in my file system, I have a soft link ~/data -> /media/volgrp/data. Under data folder, there is a file hello.txt. After moving command "rsync --remove-source-files -aH --force ~/data/... (3 Replies)
Discussion started by: huangyingw
3 Replies

2. Shell Programming and Scripting

Find + Symlinks = me confused

So i have read the man pages a few time. Searched google but I am not quite sure i understand all the lingo. What i want to do is list all files on / except i dont want any symlinks (because if I am searching / I will find the "true" file...correct?) So there is the -P, -H, and '-type l'... (2 Replies)
Discussion started by: nitrobass24
2 Replies

3. Shell Programming and Scripting

Look for, backup and delete symlinks

Hi, My first post here: Was looking if someone can help enhancing my code. I am limited to sh or ash shell (android / busybox) I made a script to look for busybox symlinks, backup them and delete them I have these questions about the below code: - busybox tar do not has the options... (2 Replies)
Discussion started by: Phil3759
2 Replies

4. Slackware

Context dependent symlinks

Ive got multiple PCs, sharing an NFS mounted home dir. For certain apps I would like to keep the config files host specific. Easy solution is to create symlinks to local folders for configs. Ideally I would still want the .config files to reside in the user home folder. Is it possible to... (2 Replies)
Discussion started by: agentrnge
2 Replies

5. UNIX for Dummies Questions & Answers

Problems using RCP with symlinks. Trying to use CPIO instead

Hello, I have inherited an old Solaris box and I have to copy all of its files onto another machine, a Centos Box. The Solaris box it so ancient is does not have rsync, scp or any other useful copy functions. I tried using RCP but it handles symlinks terribly: Extraneous data is written to my... (1 Reply)
Discussion started by: mojoman
1 Replies

6. Shell Programming and Scripting

Following Symlinks to Actual Script

I need to have my script know what directory it's in, even if it's run from a symlink located elsewhere. Here's what I've come up with, for the benefit of anyone with a similar need, but I'm also interested to know if there's a more elegant solution. I'd rather not get into awk-land, but I couldn't... (2 Replies)
Discussion started by: jeffclough
2 Replies

7. UNIX for Dummies Questions & Answers

tar symlinks: relative vs absolute

I create the tar file from / like so: tar cEhf name.tar usr/us And this creates the tar with the links intact. The problem is that this tar is going to be used for testing, so we want the links to point to the files in the tar. But when I extract the tar into /tmp, I get /tmp/usr/us/... as I... (2 Replies)
Discussion started by: TreeMan
2 Replies

8. Shell Programming and Scripting

preserve timestamp of symlinks using rsync

Hi, In my shell programming, When i copy sym links from one server to another server using rsync it does not preserve time stamp . it showing current time after copying in destination server.the command i used is rsync -vaR -- How can i preserve sym links time stamp using rsync. Thanx (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

9. UNIX for Dummies Questions & Answers

Symlinks

Given a filename, is it possible to know haow many symbolic links are pointing to it? How can I tell? (1 Reply)
Discussion started by: ct1977
1 Replies

10. UNIX for Advanced & Expert Users

search and replace symlinks

Hello All, Assuming i have a thousand symlinks under directory /mydir (and its sub-dir) such as: mysymlink1 -> ../../../myfoo/mysymlink1 mysymlink2 -> ../../../myfoo/mysymlink2 How can I search the string "myfoo" and replaced with "yourfoo" such that after the operation is complete the... (2 Replies)
Discussion started by: nixrock
2 Replies
Login or Register to Ask a Question

Featured Tech Videos