0

I am trying to move symbolic links from one folder to another folder:

When I try to move a symbolic link on its own, it works:

mv ./f28 /me/TSR/HR/Dev/TSR_Main_links/

But when I try and loop thru the contents of the folder with a foreach, I get an error:

foreach i ( 'find . -maxdepth 1 -type l' )
mv $i /me/TSR/HR/Dev/TSR_Main_links/
end

This is a follow up question to here.

ZakS
  • 1,073
  • 3
  • 15
  • 27
  • 1
    Don't. `find . -maxdepth 1 -type l -exec mv '{}' /me/TSR/HR/Dev/TSR_Main_links/ ';'` instead. It's unsafe to use string-splitting (on any other character other than NUL) on `find`'s output, because all those non-NUL characters are allowed to exist inside filenames. – Charles Duffy Dec 30 '18 at 15:12
  • 1
    (BTW, is there a reason you're using a csh-family shell? It's frowned on for good reasons; brief intro @ http://www.grymoire.com/unix/CshTop10.txt -- csh made good sense as an interactive shell when the only other option that didn't cost money was 1970s-era Bourne, but there've been better free-of-cost options for 30 years now). – Charles Duffy Dec 30 '18 at 15:15
  • Thank you! Sorry for the 101 question but where was I string splitting in the code? – ZakS Dec 30 '18 at 15:16
  • unfortunately, I have no choice but to use tcsh – ZakS Dec 30 '18 at 15:16
  • 1
    ...so, you're expecting the `foreach` code to split the output of `find` into individual elements so those elements can be assigned in turn to `i`, right? Thing is, those elements are separated by newlines. Newlines can exist in filenames on UNIX, so the shell has no way to differentiate between a newline find printed because it was part of the name and a newline find printed to separate two names. (That's not a csh-specific bug; you get it when using `find` in a similar way on POSIX-family shells too). – Charles Duffy Dec 30 '18 at 15:16
  • @CharlesDuffy it worked great! I don't want to trouble you further but could you point me to further reading on what the ';' does at the end? – ZakS Dec 30 '18 at 15:33
  • 1
    Sure, see the manual for `find`. If the last argument is `+`, it substitutes as many names as it can in place of `{}` (which needs, in that case, to be in second-to-last position); if the last argument is `;`, it substitutes only one, and calls the command once per file. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html – Charles Duffy Dec 30 '18 at 15:36
  • 1
    It's very POSIX-family-shell-centric documentation, and so not all of it's very applicable to csh, but [Using Find](http://mywiki.wooledge.org/UsingFind) is a source that goes more into best practices around using `find` safely, beyond the basic spec. – Charles Duffy Dec 30 '18 at 15:38

0 Answers0