Bash 脚本自动创建到树中子目录的符号链接

发布于 2024-08-02 20:20:06 字数 572 浏览 5 评论 0原文

好吧,这是我第三次尝试发帖,也许我问错了问题!

我已经有几年没有做过任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

审判长 2024-08-09 20:20:06

我认为你可以在 find 命令中完成这一切。 OTTOMH:

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

希望我没记错语法。

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

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

Hope I remembered that syntax right.

浴红衣 2024-08-09 20:20:06

您的命令

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

对 xargs 有参数“-0”,但您没有告诉 find 为“-print0”(如果您这样做了 grep 无法在中间的管道中工作)。我猜你想要的是:

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

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

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:

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

The tr command will convert newlines to ascii null.

无声无音无过去 2024-08-09 20:20:06

使用 for 循环。

for name in $(find $from_dir -mindepth 3 -maxdepth 3 -type d); do
  ln -s $name $to_dir
done

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

我在 find 命令中执行操作的经验有时可能会很慢,尽管它确实可以完成工作。

Use a for loop.

for name in $(find $from_dir -mindepth 3 -maxdepth 3 -type d); do
  ln -s $name $to_dir
done

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文