tags:

views:

184

answers:

5

I've got a folder that's got a whole lot of folders inside, which each contain a movie file. Now I want to be able to see all these movies at one time (but I have to preserve the folder structure) so I wanted to symlink them all to the top level folder (flat view in Directory Opus would be the go here, but I'm on a mac). Here's my attempt

for file in */*/*.mov; 
do 
newname='echo $file | sed "s|[^/]*/[^/]*/||g"';
ln -s $file echo $newname;
done

So what I'm trying to do on line 3 is turn the path foo/bar/spud.mov into spud.mov and then on line 4 do ln -s foo/bar/spud.mov spud.mov

But it doesn't work, all I get is

ln: "s|[^/]/[^/]/||g": No such file or directory

I've tried various combinations of quotes and escapes, but with no luck. The sed expression works, but I've obviously mucked up something with my variables.

Thanks

+1  A: 

Basename is what you want, and the problem with your solution is poor quoting:

bash-3.2$ echo $file
foo/bar/spud.mov
bash-3.2$ newname=$(echo $file | sed 's|[^/]*/[^/]*/||g'); echo $newname
spud.mov
William Pursell
+2  A: 

This will do the same for all folder levels:


for i in `find . -name "*.mov"`
do
  file=`echo $i | /bin/awk -F'/' '{print $NF}'`
  /bin/ln -s $i $file
done
Freddy
The conventional way to get the last component of a pathname is the basename command: `ln -s $i $(basename $i)`
Jonathan Leffler
its ok to use basename for one of two files, however, for many files, using the shell internals string capabilities to get "basename" is much more efficient.
ghostdog74
you don't need either of those; just "." works, because when passed a directory as the last argument, ln -s does the same thing...
freiheit
+2  A: 

What's with all the echo $file | sed or awk stuff, anyways? basename's made for this purpose.

for file in */*/*.mov; do 
  newname=`basename $file1
  ln -s $file $newname;
done

But, really, ln does the same thing when given a directory, so:

for file in */*/*.mov; do
  ln -s "$file" .
done

Or a really general purpose version that's smart enough to only call ln as many times as needed:

find . -name \*.mov -depth +1 -print0 | xargs -0 -J % ln -s % .

That is, in the local directory, find everything with a name ending in .mov that's not in the current directory (depth is more than 1), separate the output with NULLs, and pipe into xargs, which is looking for NULL-separate input, and will pass . as the argument to ln after all the arguments it read on stdin.

freiheit
A: 

Well thanks everyone. Seems to be a few worms in this can. It seems that what I was doing, while not the best way, would have worked if I'd used the right syntax.

But I've learned about basename and improved my knowledge of ln, so I'm happy.

stib
A: 

I know a bit more about how to use find, so now I can do it in one line:

find . -name "*.mov" -exec ln -s {} \;

the -exec flag in the find command lets you run another process every time it finds a file. Put in a pair of curly braces as above to give that process the file as a parameter. Took me a while to work out that there needs to be a space before the escaped semicolon.

stib