Recursively hard linking files -- bonehead question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursively hard linking files -- bonehead question
# 1  
Old 11-09-2008
Question Recursively hard linking files -- bonehead question

I used to program ksh a while back, but I've been off in Oracle/Windows land (for clients -- not by choice) for so long that I can't remember what should be an easy thing. Here's the scenario:

1)Find all files and directories beneath some directory point (A).
2)If directory, make the subdirectory at some different directory point (B)
3)If file, link (not symbolic) the file in the correct subdirectory under point B.

For a more concrete example, here's a scenario.

Point A: /usr/test
Point B: /var/test

File structures:
/usr/test
/usr/test/oracle
/usr/test/oracle/script 1
/usr/test/oracle/script 2
/usr/test/db2
/usr/test/db2/script 1
/usr/test/db2/script 2

I'm trying to write a script that will result in the following output (and/or actually do the work instead of writing an output script, but I can live with creating a secondary script):

mkdir /var/test/oracle
ln /usr/test/oracle/"script 1" /var/test/oracle/"script 1"
ln /usr/test/oracle/"script 2" /var/test/oracle/"script 2"
mkdir /var/test/db2
ln /usr/test/db2/"script 1" /var/test/db2/"script 1"
ln /usr/test/db2/"script 2" /var/test/db2/"script 2"

Here's the pseudo code for what I've been working on so far:

#!/bin/bash

#declare variables
cnt=0 #array counter
# &1 == Source Directory
# &2 == Target Directory
source_len=awk '{ print length }'

# Find out what the subdirectories are
full_list=find $1 -print 2>/dev/null

## Populate the subdirectories into an array
## Trim the root from the subdirectories into a second array
subdir = full_list| cut -c source_len-

# Loop through, creating them one at a time.
if [-d full_list]
then
# make the new directory
echo "mkdir $target/$subdir"
elif [-f source]
then
# link the file
echo "ln $full_list $target/$subdir"
fi

Someone suggested to me mounting /usr/test to /var/test, but that doesn't completely satisfy the necessary requirements (and would result in a few thousand mount points being scattered through the environment -- not a pretty picture)...

if this was PL/SQL, it would have taken me about 15 seconds to write, but I'm rusty on bash scripting. Any thoughts?

--CS
# 2  
Old 11-09-2008
no error checking but first thoughts:
Code:
oldifs=$IFS
IFS='
'
find /home/chris/tmp/usr/test |
while read f
do
    dest=$(echo $f | sed 's/usr/var/')
    [ -d "$f" ] && mkdir -p "$dest" || ln "$f" "$dest"
done
IFS=$oldifs

this produced:
Code:
find /home/chris/tmp/usr
/home/chris/tmp/usr
/home/chris/tmp/usr/test
/home/chris/tmp/usr/test/oracle
/home/chris/tmp/usr/test/oracle/script 1
/home/chris/tmp/usr/test/oracle/script 2
/home/chris/tmp/usr/test/db2
/home/chris/tmp/usr/test/db2/script 1
/home/chris/tmp/usr/test/db2/script 2

find /home/chris/tmp/var
/home/chris/tmp/var

bash linker.sh
find /home/chris/tmp/var
/home/chris/tmp/var
/home/chris/tmp/var/test
/home/chris/tmp/var/test/oracle
/home/chris/tmp/var/test/oracle/script 1
/home/chris/tmp/var/test/oracle/script 2
/home/chris/tmp/var/test/db2
/home/chris/tmp/var/test/db2/script 1
/home/chris/tmp/var/test/db2/script 2

and if you run ls -lRi on both dirs you should see that the files are linked, not copied.
# 3  
Old 11-09-2008
Quote:
Originally Posted by wempy
Code:
  {{snip}}
    dest=$(echo $f | sed 's/usr/var/')
  {{snip}}

Unfortunately, the /usr and /var are just examples. I was hoping to parameterize the two directories to support changing it on the fly at runtime.

--CS
# 4  
Old 11-10-2008
Code:
srcdir=/some/dir
targetdir=/some/other/dir
  {{snip}}
find $srcdir |
  {{snip}}
    dest=$(echo $f | sed "s#$srcdir#$targetdir#")
  {{snip}}

# 5  
Old 11-10-2008
Quote:
Originally Posted by Annihilannic
Code:
srcdir=/some/dir
targetdir=/some/other/dir
  {{snip}}
find $srcdir |
  {{snip}}
    dest=$(echo $f | sed "s#$srcdir#$targetdir#")
  {{snip}}

Okay, so to make source directory and target directory command line parameters, it would be something like:

Code:
srcdir=$1
targetdir=$2

Right?
# 6  
Old 11-10-2008
certainly would, yes.
# 7  
Old 11-13-2008
Okay, I finally got a break in work that pays to try this out. Here's the script:

Code:
srcdir=$1
targetdir=$2

oldifs=$IFS
IFS='
'
find $srcdir |
while read f
do
    dest=$(echo $f | sed 's#$srcdir#$targetdir#g')
    echo $srcdir
    echo $targetdir
    echo $dest
    echo $f
    echo $f |sed 's/\/library\/\data\/Total List/$targetdir/g'
#    [ -d "$f" ] && mkdir -p "$dest" || ln "$f" "$dest"
done
IFS=$oldifs

And the results (just to provide a sample) are:

Quote:
/library/data/Total List/data point 1
/library/data/Ordered/1
/library/data/Total List/data point 1/subset 1/image1.iso
/library/data/Total List/data point 1/subset 1/image1.iso
$targetdir/data point 1/subset 1/image1.iso
Thoughts/Suggestions?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Linker errors linking to .a files on OS X

Basically my problem is that when I try to compile anything using ./configure && make, it fails because of linker errors. I can reproduce the behavior I'm getting as follows: I have the two following files main.c: #include <stdio.h> extern void func(void); int main(int argc, char... (5 Replies)
Discussion started by: MarshallBanana
5 Replies

2. Shell Programming and Scripting

Script for linking files with paths in 2 text files

I have 2 txt files, 1.txt and 2.txt which contain the paths to files that need to be linked. Example 1.txt: /root/001/folder2/image4.nii.gz /root/002/folder2/image4.nii.gz Example 2.txt: /root/001/folder2/image5.nii.gz /root/002/folder2/image5.nii.gz Each line represents images from... (7 Replies)
Discussion started by: LeftoverStew
7 Replies

3. Shell Programming and Scripting

Recursively move directories along with files/specific files

I would like to transfer all files ending with .log from /tmp and to /tmp/archive (using find ) The directory structure looks like :- /tmp a.log b.log c.log /abcd d.log e.log When I tried the following command , it movies all the log files... (8 Replies)
Discussion started by: frintocf
8 Replies

4. UNIX for Dummies Questions & Answers

Need help in moving files recursively

Hi, I have d1,d2,d3 directories / /home/abc/d1 /home/abc/d2 /home/abc/d3 d1,d2 and d3 also have subdirctories. d1-->d11-->d12 d2-->d22-->d23 d3-->d33-->d34 All these directories have files like date_filename.txt so I want to find the files recusively for a particular date from... (1 Reply)
Discussion started by: jagadish_gaddam
1 Replies

5. UNIX for Advanced & Expert Users

hard question

I have a directory containing a series of files of the format: A2008001231000.L2 I only care about the 6-8 digits, so the files are effectively: ?????---*.L2 I have files that range from ?????001*.L2 to ?????366*.L2 It should be noted these three digits represent the julian day of the... (2 Replies)
Discussion started by: msb65
2 Replies

6. Programming

C files searching and Linking

Dear friends, First off all , let me apologize for my inexperience. I am just starting use of Linux and gcc . Actually I ve some .c files in the present directory , and now I am giving +vc <my_file1.c> <my.file2.c> <myfile3.c>. All the c files are in present directory.... (2 Replies)
Discussion started by: user_prady
2 Replies

7. UNIX for Dummies Questions & Answers

Linking Files in UNIX (shortcuts)

Hello, Can anyone tell me how I can make a file link or shortcut in UNIX 4.0, several file links where damaged during an outage examples below. Examples: file -> file libX11.so -> /usr/shlib/libX11.so.pre.O3D All of the file links that were damaged were in /shlib and point to... (3 Replies)
Discussion started by: jays337
3 Replies

8. UNIX for Dummies Questions & Answers

modifying C file and linking back to project files

hi, This is the first time I work in a big C project. All source code files are located in say directory /source/pp and all header files are in /include/pp. I've created a link to both of these directories from my home dir, say /home/ss. So in the /home/ss dir I have the /source/pp and /include/pp... (1 Reply)
Discussion started by: bruins2005
1 Replies

9. Programming

Linking problem while linking to shared library

Hi I'm getting ld: fatal: option -h and building a dynamic executable are incompatible ld: fatal: Flags processing errors When I run ld -shared -L/usr/dt/lib -lDtSvc -o builtin.so Workspace.o after running gcc -fPIC -I/usr/X11R6/include -I/usr/dt/include -c Workspace.c I'm... (6 Replies)
Discussion started by: laho
6 Replies

10. Programming

compiling and linking 2 C files ...??

Hi mates, I am trying to copile and link to C programs with command: cc file1,file2 but i raises the error "file not found" ... am i doing the right way? any suggestion will be appreciated. thanks abdul (4 Replies)
Discussion started by: abdul
4 Replies
Login or Register to Ask a Question

Featured Tech Videos