I'm trying to create a script in order to keep the files with the same extension in a folder separated from the rest. In particular, I want a script that, executed in a folder, create soft links of all .tex files and put them in a subfolder "sources".

I know that ln copes well with regular expressions, and $PWD or $(pwd) returns the path to the current folder. So I tried to execute

ln -s $PWD/*.tex ./sources/

in my folder, but soon I realized that in my path there was a folder with spaces in it, that ln reads as multiple files. So I tried with

ln -s '$PWD'/*.tex ./sources/


ln -s '$PWD/*.tex' ./sources/

but in both cases, the script produces a single file named *.tex in my sources folder. Following some post, I tried to modify the spaces in the path like these

ln -s $(printf "%q\n" "$PWD")/*.tex sources/

ln -s '$(printf "%q\n" "$PWD")'/*.tex sources/

ln -s '$(printf "%q\n" "$PWD")/*.tex' sources/

but the output was the same (with the difference of an additional character \).



Your first attempt should work. Does it not? What does it do instead?

* substitutes with all matching filenames, but does not perform splitting at internal spaces afterwards.

"I know that ln copes well with regular expressions" – this is wrong. First, it's shell patterns that you see here, not regular expressions. Second, it's not ln coping with them but your shell (typically bash) prior to launching ln or any other command.

| improve this answer | |
  • the first attempt produces multiple files according to the spaces in the path: if, for example, the folder is called "my files", then the command ln -s $PWD/*.tex sources/ produces two files that are named my and *.tex disregarding of what is inside the folder – Exodd Aug 2 '17 at 14:56
  • 1
    Oh, sorry, I misunderstood the situation. Thought it was the .tex files that had spaces in their names. You should surround $PWD by double quotes: "$PWD"/* – egmont Aug 2 '17 at 16:11
  • U should write that in your answer – Exodd Aug 2 '17 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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