bash 如何为目录中的每个文件创建软链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4864482/
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
How to create soft-links for every file in a directory?
提问by User1
I have a directory, /original, that has hundreds of files. I have a script that will process files one at a time and delete the file so it's not executed again if the script gets interrupted. So, I need a bunch of soft links to the files on /originalto /processing. Here's what I tried:
我有一个/original包含数百个文件的目录。我有一个脚本,它将一次处理一个文件并删除该文件,因此如果脚本被中断,它就不会再次执行。所以,我需要一堆软链接/original到/processing. 这是我尝试过的:
find /original -name "*.processme" -exec echo ln -s {} $(basename {}) \;
and got something like:
并得到类似的东西:
ln -s /original/1.processme /original/1.processme ln -s /original/2.processme /original/2.processme ln -s /original/3.processme /original/3.processme ...
I wanted something like:
我想要这样的东西:
ln -s /original/1.processme 1.processme ln -s /original/2.processme 2.processme ln -s /original/3.processme 3.processme ...
It appears that $(basename)is running before {}is converted. Is there a way to fix that? If not, how else could I reach my goal?
看来$(basename)是运行之前{}被转换了。有没有办法解决这个问题?如果没有,我还能如何达到我的目标?
回答by Hasturkun
You can also use cp(specifically the -soption, which creates symlinks), eg.
您还可以使用cp(特别是-s创建符号链接的选项),例如。
find /original -name "*.processme" -print0 | xargs -0 cp -s --target-directory=.
回答by User1
find /original -name '*.processme' -exec echo ln -s {} . \;
find /original -name '*.processme' -exec echo ln -s {} . \;
Special thanks to Ryan Oberoi for helping me realize that I can use a .instead of $(basename ...).
特别感谢瑞安奥贝罗伊帮助我意识到,我可以用一个.代替$(basename ...)。
回答by Ryan Oberoi
How about -
怎么样 -
ln -s $(echo /original/*.processme) .
回答by Paused until further notice.
Give this a try:
试试这个:
find /original -name "*.processme" -exec sh -c 'echo ln -s "$@" $(basename "$@")' _ {} \;
回答by Brett Sanford
you simply need to remove echo and strip the repeat of the filepath and basename entirely
您只需要删除 echo 并完全去除文件路径和基本名称的重复
If your Source folder is this
如果您的源文件夹是这个
ls -l /original
total 3
-rw-r--r-- 1 user user 345 Dec 17 21:17 1.processme
-rw-r--r-- 1 user user 345 Dec 17 21:17 2.processme
-rw-r--r-- 1 user user 345 Dec 17 21:17 3.processme
Then
然后
cd /processing
find /original -name "*.processme" -exec ln -s '{}' \;
Should Produce
应该产生
ls -l /processing
total 3
lrwxrwxrwx 1 user user 33 Dec 17 21:38 1.processme -> /original/1.processme
lrwxrwxrwx 1 user user 33 Dec 17 21:38 2.processme -> /original/2.processme
lrwxrwxrwx 1 user user 33 Dec 17 21:38 3.processme -> /original/3.processme
Aware the OP is from 5 years ago I post this for those seeking the same solution like myself before I worked it out.
意识到 OP 来自 5 年前,我将其发布给那些在我解决之前寻求与我相同解决方案的人。

