LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Recursive Copy / Create (https://www.linuxquestions.org/questions/programming-9/recursive-copy-create-502536/)

Luvz2Fly 11-17-2006 10:34 AM

Recursive Copy / Create
 
Greetings all...
I have over 300 directories in one place and I must create 4 symlinks in each dir. Is there a quick way to copy 4 symlinks recursively or do I need to create a script that will traverse the directories, make the symlinks and then move to the next dir? Any ideas on how I should do that?

Thanks so much!

ygloo 11-17-2006 11:12 AM

post more info...
u want to create the same symlinks in every dir??

Luvz2Fly 11-17-2006 12:12 PM

Yep - I need the same exact symlink in every dir:
/dir/1
/dir/2
/dir/3
...
/dir/x

for /dir/1->x I must place 4 exact symlinks - for example:
dir/1/index.html --> links to /dir/master/index.html
dir/2/index.html --> links to /dir/master/index.html

95se 11-17-2006 12:15 PM

Code:

#!/bin/bash

create_symlinks()
{
  [ $# -eq 1 ] || return false

  orig_dir="`pwd`"

  cd $1 || return false

  for i in *; do
    [ -d $i ] && create_symlinks $i
  done

  # Run your symlink commands here
  # eg.
  # ln -s /blah/blah/blah some_link1

  cd $orig_dir
}

( [ $# -eq 0 ] && start_dir="." ) || ( ( [ -d $1 ] && start_dir="$1" ) || ( echo "$1 is not a directory" && exit ) )

create_symlinks $start_dir

Edit: Forgot to add. Save it as whatever you want (ex. create_links), then run it w/ sh create_links some_dir where some_dir is your starting dir.

Luvz2Fly 11-17-2006 01:51 PM

Thanks for the fast reply ...

I ran the script as instructed and it did make the links in the current directory but did not traverse the other directories:

FROM:
/var/www/vhosts/my-domain.com/httpdocs/Info-A

I ran

./make-sym /var/www/vhosts/my-domain.com/httpdocs/Info-A

It DID make the 4 sym links in Info-A dir but NONE in

Info-A/1
Info-A/2
Info-A/3
...
Info-A/x

Thanks again!

firstfire 11-18-2006 04:52 PM

Hello.

Try this one-liner:
Code:

export to=`pwd`/index.html;
find dir/ -type d -exec bash -c "cd {}; ln -s $to `basename $to`" \;

Instead of basename it's better to define a variable, which holds base name of your file (i.e. without path).


All times are GMT -5. The time now is 07:03 AM.