I try this:

for file in `ls -1 ../files/file_??_??.dat` ; do echo "$file" ; ln "$file"  ; done

And for every file it gives this error:

ln: failed to create hard link ‘./file_01_am.dat’ => ‘../files/file_01_am.dat’: No such file or directory

But on the command line this works fine:

ln ../files/file_01_am.dat

Why does ln fail in the loop (also just written directly on the command line) and not fail on the command line when executed directly?

Just for info, the following command also fails in the same way as the loop above (this one also tries absolute pathnames for good measure):

find /media/files -name "file_??_??.dat" -print0 | xargs -0 -L 1 ln

I really do not want to have to manually hard link every file!

The files directory is a mounted smb file system. What is strange is the ln command works fine if I issue it directly, just not when done in a loop of any kind.

Please help.

  • For further bewilderment, this works fine (abbreviated example): for file in a_am.dat a_pm.dat ; do ln $file ; done. But, this fails: for file in a_??.dat ;do ln $file ; done. Where a_??.dat evaluates to the two files. – Aaron Harwood May 28 '14 at 4:59

use this :

find /media/file_??_??.dat -exec ln {} \;
  • I receive the same error. I solved the problem by creating a large file with individual ln commands and then running that. – Aaron Harwood May 29 '14 at 0:11

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.