Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In bash, say I want to link some but not all files from a directory, e.g.,

ln -s /dir/file1 /dir/file2 ... /other/dir

but the format of the names of the files do not follow some simple rule, so we can't use something like

ln -s /dir/file* /target/dir.

Is there anyway to not repeat /dir, something like:

/dir/{file1 file2 ...} = /dir/file1 /dir/file2 ...

so we can do something like:

ln -s /dir/{file1 file2 ...} /target/dir
share|improve this question

2 Answers 2

up vote 8 down vote accepted

Yes, the syntax is pretty similar to what you wanted to do. Saying:

ln -s /dir/{file1,file2,foo,bar} /target/dir

would link the files file1, file2, foo, bar from /dir/ to /target/dir/.

The above syntax is referred to as brace expansion.

(Brace expansion causes /dir/{file1,file2,foo,bar} to expand to /dir/file1 /dir/file2 /dir/foo /dir/bar.)

share|improve this answer
    
Thanks! I remember that notation, now that you mention it!!! I saw it somewhere then I forgot, thanks! –  thedoctar Mar 9 at 8:00

For complex rules try so:

cd dir
find . -name 'file1*' -or -name 'file2*' -or ... -exec ln -s '{}' '/other/dir/{}' ';'

Take notice that all '*', '{}' and ';' must be quoted for preventing shell expansion and raw passing to find!

For simple enumerations is shell expansion {aa,bb,cc} good enough.

share|improve this answer

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.