Bash 脚本自动创建到树中子目录的符号链接
好吧,这是我第三次尝试发帖,也许我问错了问题!
我已经有几年没有做过任何 shell 编程了,所以我有点生疏了...
我正在尝试创建一个简单的 shell 脚本,它查找树中某个命名子目录下的所有子目录,并创建到的符号链接这些目录(听起来比实际更令人困惑)。我在 Windows XP 上使用 cygwin。
这个 find/grep 命令会像我想要的那样查找文件系统中的目录:
find -mindepth 3 -maxdepth 3 -type d | grep "New Parts"
现在是困难的部分......我只想获取该列表,将其通过管道输入 ln 并创建一些符号链接。目录列表有一些空格,所以我尝试使用 xargs 来清理一下:
find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | xargs -0 ln -s -t /cygdrive/c/Views
不幸的是,ln 吐出一长串所有连接在一起的目录(用 \n 分隔)并吐出“文件名太长”错误。
想法??
Ok, this is my third try posting this, maybe I'm asking the wrong question!!
It's been a few years since I've done any shell programming so I'm a bit rusty...
I'm trying to create a simple shell script that finds all subdirectories under a certain named subdirectory in a tree and creates symbolic links to those directories (sounds more confusing than it is). I'm using cygwin on Windows XP.
This find/grep command finds the directories in the filesystem like I want it to:
find -mindepth 3 -maxdepth 3 -type d | grep "New Parts"
Now for the hard part... I just want to take that list, pipe it into ln and create some symlinks. The list of directories has some whitespace, so I was trying to use xargs to clean things up a bit:
find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | xargs -0 ln -s -t /cygdrive/c/Views
Unfortunately, ln spits out a long list of all the directories concatenated together (seperated by \n) and spits out a "File name too long" error.
Ideas??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你可以在 find 命令中完成这一切。 OTTOMH:
希望我没记错语法。
I think you can do this all within your find command. OTTOMH:
Hope I remembered that syntax right.
您的命令
对 xargs 有参数“-0”,但您没有告诉 find 为“-print0”(如果您这样做了 grep 无法在中间的管道中工作)。我猜你想要的是:
tr
命令会将换行符转换为 ascii null。your command
have argument "-0" to xargs but you did not tell find to "-print0" (if you did grep could not work in the pipe inbetween). What you want is the following I guess:
The
tr
command will convert newlines to ascii null.使用 for 循环。
Xargs 存在以下问题:来自管道的输入位于命令末尾。您想要的是多个命令,而不仅仅是 1 个命令。
我在 find 命令中执行操作的经验有时可能会很慢,尽管它确实可以完成工作。
Use a for loop.
Xargs has issues where the input from the pipe goes at the end of the command. What you want is multiple commands, not just 1 command.
My experience with doing things within the find command can sometimes be slow, although it does get the job done.