0
ln -s $(ls ../*.txt)

When I do the command above it replay with an error message saying : "ln: target '../foo.txt' is not a directory".

foo.txt is the first file that ls command has found in the parent dir.

I am trying to make symbolic links for all the files in the parent directory which ends with ".txt".

Can you please explain why my command did not work ?

olabie
  • 24
  • 6
  • Your attempt to use `ls` in a command substitution is dangerously close to [Bash Pitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29) (there is a reason that is #1...) – David C. Rankin Aug 17 '22 at 00:05
  • [Why *not* parse `ls` (and what to do instead)?](https://unix.stackexchange.com/q/128985/44425) – phuclv Aug 17 '22 at 03:08
  • In the future, you can find this type of errors by yourself, when running the code with `set -x` being activated. – user1934428 Aug 17 '22 at 06:20

1 Answers1

2

You forgot the directory name to put all the links into. If you want them put into the current directory, use ..

There's also no need to use ls, and you'll get the wrong results if any of the filenames contain whitespace or wildcard characters, because those will be processed in the $(...) output. Just use the wildcard directly.

ln -s ../*.txt .
Barmar
  • 741,623
  • 53
  • 500
  • 612