更新时间:2023-02-22 10:19:01
除了描述的如何的苦心经营冗长下面,这种方法也可能是独一无二的,它结合了内置调试。它基本上没有做任何事情书面除了编译并保存到一个变量的所有命令,它认为它应该以执行要求的工作要做。
Besides describing the how with painstaking verbosity below, this method may also be unique in that it incorporates built-in debugging. It basically doesn't do anything at all as written except compile and save to a variable all commands it believes it should do in order to perform the work requested.
这也明确的避免循环尽可能。除了为的模式的多个匹配 SED
递归搜索的有没有其他的递归据我所知。
It also explicitly avoids loops as much as possible. Besides the sed
recursive search for more than one match of the pattern there is no other recursion as far as I know.
而在去年,这完全是空
分隔 - 它不会对任何字符,除了空$ C $任何文件跳闸C>。我不认为你应该有一个。
And last, this is entirely null
delimited - it doesn't trip on any character in any filename except the null
. I don't think you should have that.
顺便说一句,这是的真正的快。你看:
By the way, this is REALLY fast. Look:
% _mvnfind() { mv -n "${1}" "${2}" && cd "${2}"
> read -r SED <<SED
> :;s|${3}\(.*/[^/]*${5}\)|${4}\1|;t;:;s|\(${5}.*\)${3}|\1${4}|;t;s|^[0-9]*\(.*\)${5}|\1|p
> SED
> find . -name "*${3}*" -printf "%d\tmv %P ${5} %P\000" |
> sort -zg | sed -nz ${SED} | read -r ${6}
> echo <<EOF
> Prepared commands saved in variable: ${6}
> To view do: printf ${6} | tr "\000" "\n"
> To run do: sh <<EORUN
> $(printf ${6} | tr "\000" "\n")
> EORUN
> EOF
> }
% rm -rf "${UNNECESSARY:=/any/dirs/you/dont/want/moved}"
% time ( _mvnfind ${SRC=./test_tree} ${TGT=./mv_tree} \
> ${OLD=google} ${NEW=replacement_word} ${sed_sep=SsEeDd} \
> ${sh_io:=sh_io} ; printf %b\\000 "${sh_io}" | tr "\000" "\n" \
> | wc - ; echo ${sh_io} | tr "\000" "\n" | tail -n 2 )
<actual process time used:>
0.06s user 0.03s system 106% cpu 0.090 total
<output from wc:>
Lines Words Bytes
115 362 20691 -
<output from tail:>
mv .config/replacement_word-chrome-beta/Default/.../googlestars \
.config/replacement_word-chrome-beta/Default/.../replacement_wordstars
注意:的上述函数
可能会要求 GNU
版本 SED
和找到
来妥善处理找到的printf
和 SED -z -e
和 :;递归正则表达式测试;吨
来电。如果这些都没有提供给你的功能可以有可能与一些小的调整被复制。
NOTE: The above function
will likely require GNU
versions of sed
and find
to properly handle the find printf
and sed -z -e
and :;recursive regex test;t
calls. If these are not available to you the functionality can likely be duplicated with a few minor adjustments.
您从开始想这应该尽一切努力用很少大惊小怪完成。我做了叉
与 SED
,但我也练了一些 SED
递归分支技术,所以这就是为什么我在这里。这有点像在学校理发得到一个打折理发,我猜。这里的工作流程:
This should do everything you wanted from start to finish with very little fuss. I did fork
with sed
, but I was also practicing some sed
recursive branching techniques so that's why I'm here. It's kind of like getting a discount haircut at a barber school, I guess. Here's the workflow:
室射频$ {UNNECESSARY}
./应用
可能是不必要的。删除或其他事前移动它,或者,你可以建立一个 \\(-path PATTERN -exec室射频\\ {\\} \\)
例程找到
来做到这一点编程,但一个人的都是你的。 rm -rf ${UNNECESSARY}
./app
might be unwanted. Delete it or move it elsewhere beforehand, or, alternatively, you could build in a \( -path PATTERN -exec rm -rf \{\} \)
routine to find
to do it programmatically, but that one's all yours. $ {sh_io}
是因为它节省了从函数返回尤其重要。 $ {sed_sep}
进来紧随其后;这是用来引用的sed
的递归函数中的任意字符串。如果 $ {sed_sep}
设置为可能在你的任何路径依赖或文件名中找到采取行动......嗯,就是不让一个值它可以。 ${sh_io}
is especially important in that it saves the return from the function. ${sed_sep}
comes in a close second; this is an arbitrary string used to reference sed
's recursion in the function. If ${sed_sep}
is set to a value that could potentially be found in any of your path- or file-names acted upon... well, just don't let it be. MV
的 -noclobber
选项设置;书面,此功能将不会把 $ {SRC_DIR}
凡 $ {TGT_DIR}
已存在。-noclobber
option set for mv
; as written, this function will not put ${SRC_DIR}
where a ${TGT_DIR}
already exists.找到
过程。随着找到
我们只搜索任何需要重命名,因为我们已经做了所有的地方到地方的MV
操作与函数的第一个命令。而不是采取与的任何直接的行动找到
,像 EXEC
调用,比如,我们用它代替打造出来的命令行动态与 -printf
。find
process. With find
we search only for anything that needs renaming because we already did all of the place-to-place mv
operations with the function's first command. Rather than take any direct action with find
, like an exec
call, for instance, we instead use it to build out the command-line dynamically with -printf
.找到
查找文件,我们需要它直接生成并打印出(的最的),我们需要处理的重命名命令。在 DIR%深度
上涨到每行的开头将有助于确保我们不会试图在树中的一个文件或目录重命名,目前尚未父对象要重命名。 找到
使用各种优化技术,走你的文件系统树,它不是一个肯定的事,它会返回我们需要一个安全的换操作顺序数据。这就是为什么我们下一个... find
locates the files we need it directly builds and prints out (most) of the command we'll need to process your renaming. The %dir-depth
tacked onto the beginning of each line will help to ensure we're not trying to rename a file or directory in the tree with a parent object that has yet to be renamed. find
uses all sorts of optimization techniques to walk your filesystem tree and it is not a sure thing that it will return the data we need in a safe-for-operations order. This is why we next...发现的基础上%目录深入输出
所以,在关系最近的路径为$ {} SRC首先的工作。这样就避免了可能涉及的错误 MV
ING文件到不存在的位置,并且它最大限度地减少需要递归循环。 (事实上,你可能会pssed找到所有的循环硬$ P $ 的)find
's output based on %directory-depth
so that the paths nearest in relationship to ${SRC} are worked first. This avoids possible errors involving mv
ing files into non-existent locations, and it minimizes need to for recursive looping. (in fact, you might be hard-pressed to find a loop at all)%路径在
打印的情况下,它包含多个$ {每串OLD}值,可能需要更换。所有其他解决方案我想象中涉及到第二个 SED
过程,虽然很短的循环可能不是理想的,当然它打败产卵和分叉的整个过程。 SED
在这里所做的就是搜索$ {sed_sep},然后,在找到它,直到它找到$保存它,当它遇到的所有字符{OLD} ,它再与$ {NEW}取代。然后,头回到$ {} sed_sep,再次寻找$ {OLD},如果它发生比字符串中出现多次。如果没有找到它,它打印修改后的字符串标准输出
(它捉住,然后再下一个),并结束循环。 MV
命令字符串,这需要包括$上半年{OLD}当然也包括它,下半场被改变多次,有必要从 MV
的目标路径擦拭$ {OLD}名称。%Path
printed for each string in case it contains more than one ${OLD} value that might need replacing. All other solutions I imagined involved a second sed
process, and while a short loop may not be desirable, certainly it beats spawning and forking an entire process.sed
does here is search for ${sed_sep}, then, having found it, saves it and all characters it encounters until it finds ${OLD}, which it then replaces with ${NEW}. It then heads back to ${sed_sep} and looks again for ${OLD}, in case it occurs more than once in the string. If it is not found, it prints the modified string to stdout
(which it then catches again next) and ends the loop. mv
command string, which needs to include ${OLD} of course, does include it, and the second half is altered as many times as is necessary to wipe the ${OLD} name from mv
's destination path. -exec
通话发生在这里没有第二个叉
。首先,正如我们所看到的,我们修改 MV
按提供的命令找到的
-printf
函数命令正确改变的$ {OLD}至$ {NEW}所有引用,但为了做到这一点,我们不得不使用这不应该是一些任意的参考点包含在最终的输出。所以一旦 SED
完成所有需要做的,我们指示它从保持缓冲一起传递之前,消除其参考点。 -exec
calls here happen without a second fork
. In the first, as we've seen, we modify the mv
command as supplied by find
's -printf
function command as necessary to properly alter all references of ${OLD} to ${NEW}, but in order to do so we had to use some arbitrary reference points which should not be included in the final output. So once sed
finishes all it needs to do, we instruct it to wipe out its reference points from the hold-buffer before passing it along. ,现在我们回到我的身边的
AND NOW WE'RE BACK AROUND
读
将收到类似如下的命令:
read
will receive a command that looks like this:
% mv /path2/$SRC/$OLD_DIR/$OLD_FILE /same/path_w/$NEW_DIR/$NEW_FILE \000
它将读
入 $ {味精}
为 $ {sh_io}
可随意功能之外进行检查。
It will read
it into ${msg}
as ${sh_io}
which can be examined at will outside of the function.
酷。
-Mike