qwex
qwex

Reputation: 23

How do I automatically make symlinks based on a file?

I have a text file that contains multiple lines of sources and targets.

Example:

"/home/desktop/aaa t" "/home/desktop/bbb"
"/home/desktop/ee e" "/home/desktop/aa/rr r"

I have tried this: cat ../symlinks.txt | xargs -i{} -d\n ln -s {} but that seems to be broken, and ln does not seem to like that.

I need to create a lot of symlinks from a file to the target path, that symlinks to the source. How would I be able to do this, using bash? The paths are currently all surrounded in quotation marks and are a absolute path, since ln doesn't seem to be able to take relative paths.

Upvotes: 2

Views: 57

Answers (1)

dash-o
dash-o

Reputation: 14491

Instead of using the '-I' will will pass the whole input line as a single argument, consider using the '-L1', which will use the default xargs word splitting, which will support quotes, and backslash.

xargs -L1 ln -s < ../symlinks.txt

From xargs man:

... xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines ...

Note regarding relative path: There is no reason that ln will not work with relative symlinks. Try the following

echo '"a b" c' | xargs -L1 ln -s

Which will create a symlink 'c' to (non-existent) file 'a b' in the current folder.

Upvotes: 4

Related Questions