LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2012, 05:35 AM   #1
atmos1234
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
ln -s sub directories bash script..


Hi all,

i use this function in my script. The problem i have it doesn't pick up sub directories. Can someone tell me what im doing wrong by example? It works exactly how it should with files.. I tried some stuff but it cant get it to work.

Code:
function _recurselink {

target="/tempcomplete"
source="/home/complete"  

    if [[ ! -d "$target"/"$1" ]]
        then
       
    mkdir -p "$target"/"${1// /.}"
    fi
        for item in "$source"/"$1"/*
        do

                if [[ -f "$item" ]]
                   then
                      ln -s "$item" "$target"/"${1// /.}"
                fi
                     if [[ -d "$item" ]]
                        then
                          _recurselink "$item"
                     fi
        done
}
 
Old 05-28-2012, 06:18 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Try placing set -xv at the start of your script and look at the output to see where your code is coming unstuck.
 
Old 05-28-2012, 08:30 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
One problem can be that since the first iteration, the argument of _recurselink is an absolute path since item is
Code:
"$source"/"$1"/something
as assigned by the for loop. At this point the full path of the source is added again and any test of existence of files or directories fails. For example, if you call the function with testdir the item at the first recursion becomes:
Code:
/home/complete//home/complete/testdir
this directory obviously doesn't exist and the function terminates gracefully.
 
Old 05-28-2012, 08:41 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Unless this is a programming exercise or compatibility with other *nix systems is required, an alternative is a single cp command with the proper options:
Code:
cp -Rs /home/complete /tempcomplete
where the -s option tells to link files symbolically instead of copying. The directory structure is preserved and recursion is performed by the option -R. Notice that the absolute path of the source is mandatory, otherwise symbolic links cannot be created, since relative paths are referred to your current working directory and not to the destination.
 
1 members found this post helpful.
Old 05-28-2012, 08:42 AM   #5
atmos1234
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
One problem can be that since the first iteration, the argument of _recurselink is an absolute path since item is
Code:
"$source"/"$1"/something
as assigned by the for loop. At this point the full path of the source is added again and any test of existence of files or directories fails. For example, if you call the function with testdir the item at the first recursion becomes:
Code:
/home/complete//home/complete/testdir
this directory obviously doesn't exist and the function terminates gracefully.
Yes, this is what happens.. But if i remove $source then nothing happens..
Do you know How can i solve this? I need it for files but also for files in a subdirectory.

Thank you for taking the time to explain.
 
Old 05-28-2012, 09:33 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I noticed you posted almost at the same time of my add-on and maybe you missed my last post about the cp alternative. Is it suitable for you?
 
Old 05-28-2012, 09:53 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Anyway, here is a working recursive function:
Code:
function _recurselink {
  mkdir -p "${2// /.}"
  for item in "$1"/*
  do
    if [[ -f "$item" ]]
    then
      ln -s "$item" "${2// /.}"
    fi
    if [[ -d "$item" ]]
    then
      _recurselink "$item" "$2/${item##*/}"
    fi
  done
}
It is independent from the actual path (notice it does not define source or target) and you have to call it with two arguments, the source directory (absolute path) and the target directory, exactly the same as the suggested cp command:
Code:
_recurselink /home/complete /tempcomplete
Also notice that using the -p option of mkdir makes the test for the existence of the directory not necessary.
 
Old 05-28-2012, 01:09 PM   #8
atmos1234
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Works great!! I never would thought of this.. Thank alot!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need Assistance with a Bash script for Ceaning Home Directories Joseph4200 Programming 2 07-25-2011 12:29 PM
Synchronize two directories (bash script) SydPersia Programming 2 02-11-2011 05:20 PM
BASH script that removes sub-directories (not just files) but keeps the two newest. kennc Linux - Newbie 2 04-23-2009 01:09 PM
Bash script to search through directories. mcdrr Programming 5 05-11-2007 05:41 PM
Bash script about directories tarja Programming 3 11-23-2005 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration