用于自动创建指向树中子目录的符号链接的 Bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1331138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:09:43  来源:igfitidea点击:

Bash script to automatically create symlinks to subdirectories in a tree

bashwhitespacesymlink

提问by NWCoder

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...

我已经有几年没有做过任何 shell 编程了,所以我有点生疏了......

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.

我正在尝试创建一个简单的 shell 脚本,该脚本在树中的某个命名子目录下查找所有子目录,并创建到这些目录的符号链接(听起来比实际更混乱)。我在 Windows XP 上使用 cygwin。

This find/grep command finds the directories in the filesystem like I want it to:

这个 find/grep 命令像我想要的那样在文件系统中查找目录:

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:

现在是困难的部分...我只想获取该列表,将其通过管道传输到 ln 并创建一些符号链接。目录列表有一些空格,所以我试图使用 xargs 来清理一下:

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.

不幸的是, ln 吐出一长串所有连接在一起的目录(由 \n 分隔)并吐出“文件名太长”错误。

Ideas??

想法??

回答by NWCoder

I think you can do this all within your find command. OTTOMH:

我认为你可以在你的 find 命令中完成这一切。奥托姆:

find -mindepth 3 -maxdepth 3 -type d -name "*New Parts*" -exec ln -s -t /cygdrive/c/Views {} \;

Hope I remembered that syntax right.

希望我记得那个语法是对的。

回答by hlovdal

your command

你的命令

find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | xargs -0 ln -s -t /cygdrive/c/Views

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:

有参数“-0”到 xargs 但你没有告诉 find 到“-print0”(如果你做了 grep 无法在中间的管道中工作)。我想你想要的是以下内容:

find -mindepth 3 -maxdepth 3 -type d | grep "New Parts" | tr '2' '
for name in $(find $from_dir -mindepth 3 -maxdepth 3 -type d); do
  ln -s $name $to_dir
done
0' | xargs -0 ln -s -t /cygdrive/c/Views

The trcommand will convert newlines to ascii null.

tr命令会将换行符转换为 ascii null。

回答by KFro

Use a for loop.

使用 for 循环。

##代码##

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.

Xargs 存在来自管道的输入在命令末尾的问题。您想要的是多个命令,而不仅仅是 1 个命令。

My experience with doing things within the find command can sometimes be slow, although it does get the job done.

我在 find 命令中做事的经验有时会很慢,尽管它确实完成了工作。