and thank you in advance for any advice.

I'm working on a script that will automatically sort my downloads. The script is very simple, and running it manually has the intended effect. The problem I'm having is with the fswatch command that's intended to trigger the script - I can't get the output from echo correctly piped into xargs. Here's the command:

fswatch --event Created Downloads/ | (while read x; do echo $x | xargs -0 bash ./dlsort.sh; done)

and the script:

if (echo "$1" | grep -q '\.jpg$'); then
  mv "$1" Desktop/images/ &
  mv "$1" Downloads/other/ &

I've determined what the issue is, I just don't know how to correct it. a newline character is getting put at the end of the filename, which causes the mv command to fail with a "no such file or directory" error in the script. I've been experimenting with quite a few variations in both the echo and fswatch commands, but nothing I've tried has worked.

Thanks again to anyone who can help with this

  • did you try echo -n $x? – Lety Dec 9 '15 at 18:12
  • I did, when I do that I only get mv: (name of the file): no such file or directory (it doesn't print any destination folder). Not sure why that is either, but that could definitely be a part of the solution – Curtis Everingham Dec 9 '15 at 18:29
  • Well that's odd... I went to clear out the directories that this script is sorting into, and some files actually have made it (when using echo -n). I still get an error message, but it looks like the file is getting moved where it's supposed to. That works, but I would like to figure out why I'm still getting a mv: no such file or directory message – Curtis Everingham Dec 9 '15 at 18:38
  • The script is getting called twice... that's why. Working on figuring out why that is. Thanks for getting me back on the right track, @Lety :) – Curtis Everingham Dec 9 '15 at 18:54
  • reading and echoing within a while loop seems superfluous here, to me. Why don't you either pipe the (null-delimited) fswatch command straight to xargs -0 or read each null-delimited argument in a while loop and call your script on it? – steeldriver Dec 9 '15 at 20:03

Figured out a good answer to this specific problem:

Use the fswatch option --format="%p%0". This makes sure that the null character is at the end of the path name that gets handed off to xargs, which is what it's looking for with the -0 option enabled.

| improve this answer | |
  • You seem to subvert this by reading and echoing the result (which will split on whitespace I think?) – steeldriver Dec 9 '15 at 20:04

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.