我想用find命令查找txt檔案并移動找到的檔案的目錄,然后使用bash shell單行將命令應用于該檔案
例如,此命令有效,但acmd在當前目錄中執行。
$ find . -name "*.txt" | xargs acmd
我想acmd在txt檔案的目錄中運行。
有沒有人有好主意?
uj5u.com熱心網友回復:
從查找手冊頁:-
-execdir command ;
-execdir command {}
Like -exec, but the specified command is run from the subdirec‐
tory containing the matched file, which is not normally the
directory in which you started find. This a much more secure
method for invoking commands, as it avoids race conditions dur‐
ing resolution of the paths to the matched files. As with the
-exec action, the ` ' form of -execdir will build a command line
to process more than one matched file, but any given invocation
of command will only list files that exist in the same subdirec‐
tory. If you use this option, you must ensure that your $PATH
environment variable does not reference `.'; otherwise, an
attacker can run any commands they like by leaving an appropri‐
ately-named file in a directory in which you will run -execdir.
The same applies to having entries in $PATH which are empty or
which are not absolute directory names. If find encounters an
error, this can sometimes cause an immediate exit, so some pend‐
ing commands may not be run at all. The result of the action
depends on whether the or the ; variant is being used;
-execdir command {} always returns true, while -execdir com‐
mand {} ; returns true only if command returns 0.
uj5u.com熱心網友回復:
只是為了完整性,另一種選擇是:
$ find . -name \*.txt | xargs -i sh -c 'echo "for file $(basename {}), the directory is $(dirname '{}')"'
for file schedutil.txt, the directory is ./Documentation/scheduler
for file devices.txt, the directory is ./Documentation/admin-guide
for file kernel-parameters.txt, the directory is ./Documentation/admin-guide
for file gdbmacros.txt, the directory is ./Documentation/admin-guide/kdump
...
即有 xargs “遵從 shell”。在-execdir足夠的用例中,去吧。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/349464.html
