2

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

2 Answers 2

8

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.)

1
  • Thanks! I remember that notation, now that you mention it!!! I saw it somewhere then I forgot, thanks!
    – thedoctar
    Mar 9, 2014 at 8:00
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .