Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I need to execute a loop as a string. This:

bash -c "for l in $(/bin/ls /dir1); do /bin/ln -sf $l /dir2/; done"

Gives an error:

/bin/bash: -c: line 1: syntax error near unexpected token `file1'
/bin/bash: -c: line 1: `file1'

file1 is one of those that I want to turn into symlinks in the first directory.

share|improve this question
    
Using single quote instead bash -c 'for l in /dir1/*; do /bin/ln -sf "$l" /dir2/; done' –  cuonglm Aug 11 at 2:11
1  
Don't parse ls output: mywiki.wooledge.org/ParsingLs –  Peter Cordes Aug 11 at 19:56
    
@cuonglm: your command doesn't do exactly the same thing he's trying to do. He's creating relative symlinks to files that don't necessarily exist, based on names found in a different directory. –  Peter Cordes Aug 11 at 20:04
    
@PeterCordes: Not sure what you mean, what is the difference between ls /dir with using glob? –  cuonglm Aug 12 at 1:32
    
ls /dir lists just the filenames, not the full paths. –  Peter Cordes Aug 12 at 1:51

1 Answer 1

up vote 1 down vote accepted

Don't parse ls output: http://mywiki.wooledge.org/ParsingLs.

Also, in this example you don't need a loop, just let ln loop over args. cd to the directory you're creating the symlinks in, if you want relative symlink targets to work when you use relative paths from the current directory to generate them.

(cd /dir1 && ln -sf -- * /dir2)      # make broken symlinks in dir2
(cd /dir2 && ln -sf -- ../dir1/* .)  # make relative symlinks
ln -sf /dir1/* /dir2/                # make absolute symlinks

(subshell) to avoid changing directory in the current shell.

You can also use ln -sf -t /dir2 -- *, which is useful with find -exec ln -t /dir {} +, because find -exec {} + can only put the list at the end of the command line.

GNU cp also has -l and -s options, to make hard / symbolic links.

share|improve this answer
    
It should be noted that it won't create links to the files in /dir1 inside /dir2, for which it should be ln -s /dir1/* /dir2/ (hard to tell what the OP wants) –  Stéphane Chazelas Aug 11 at 22:10
    
@StéphaneChazelas: Yeah, probably a good idea to include all 3 things the OP might want. Thanks for the -- edit. I always remember to quote my variables, but I often forget about -- to protect things other than the shell from weird stuff in filenames. >.<. –  Peter Cordes Aug 11 at 22:18
1  
Note that (cd /dir2 && ln -sf -- ../dir1/* .) may not work if /dir2 itself is a symlink. –  Stéphane Chazelas Aug 11 at 22:23
    
@StéphaneChazelas: yeah, that can get confusing. cd -P before using ../ relative paths is often a good idea. -P = physical, as opposed to the usual logical mode. –  Peter Cordes Aug 11 at 22:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.