Can the find command's “exec” feature start a program in the background?

ⅰ亾dé卋堺 提交于 2020-01-12 10:26:06

问题


I would like to do something like:

find . -iname "*Advanced*Linux*Program*" -exec kpdf {} & \;

Possible? Some other comparable method available?


回答1:


Firstly, it won't work as you've typed, because the shell will interpret it as

find . -iname "*Advanced*Linux*Program*" -exec kpdf {} &
\;

which is an invalid find run in the background, followed by a command that doesn't exist.

Even escaping it doesn't work, since find -exec actually execs the argument list given, instead of giving it to a shell (which is what actually handles & for backgrounding).

Once you know that that's the problem, all you have to do is start a shell to give these commands to:

find . -iname "*Advanced*Linux*Program*" -exec sh -c '"$0" "$@" &' kpdf {} \;

On the other hand, given what you're trying to do, I would suggest one of

find ... -exec kfmclient exec {} \;  # KDE
find ... -exec gnome-open {} \;      # Gnome
find ... -exec xdg-open {} \;        # any modern desktop

which will open the file in the default program as associated by your desktop environment.




回答2:


If your goal is just not having to close one pdf in order to see the next one as opposed to display each pdf in its own separate instance, you might try

find . -iname "*Advanced*Linux*Program*" -exec kpdf {} \+ &

With the plussed variant, -exec builds the command line like xargs would so all the files found would be handed to the same instance of kpdf. The & in the end then affects the whole find. With very large numbers of files found it might still open them in batches because command lines grow too long, but with respect to ressource consumption on your system this may even be a good thing. ;)

kpdf has to be able to take a list of files on the command line for this to work, as I don't use it myself I don't know this.



来源:https://stackoverflow.com/questions/853451/can-the-find-commands-exec-feature-start-a-program-in-the-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!