Create symbolic link from find

大兔子大兔子 提交于 2020-12-28 07:51:35

问题


I'm trying to create a symbolic link (soft link) from the results of a find command. I'm using sed to remove the ./ that precedes the file name. I'm doing this so I can paste the file name to the end of the path where the link will be saved. I'm working on this with Ubuntu Server 8.04.

I learned from this post, which is kind of the solution to my problem but not quite-

How do I selectively create symbolic links to specific files in another directory in LINUX?

The resulting file name didn't work, though, so I started trying to learn awk and then decided on sed.

I'm using a one-line loop to accomplish this. The problem is that the structure of the loop is separating the filename, creating a link for each word in the filename. There are quite a few files and I would like to automate the process with each link taking the filename of the file it's linked to.

I'm comfortable with basic bash commands but I'm far from being a command line expert. I started this with ls and awk and moved to find and sed. My sed syntax could probably be better but I've learned this in two days and I'm kind of stuck now.

for t in find -type f -name "*txt*" | sed -e 's/.//' -e 's$/$$'; do echo ln -s $t ../folder2/$t; done

Any help or tips would be greatly appreciated. Thanks.


回答1:


Easier:

Go to the folder where you want to have the files in and do:

find /path/with/files -type f -name "*txt*" -exec ln -s {} . ';'



回答2:


Execute your for loop like this:

(IFS=$'\n'; for t in `find -type f -name "*txt*" | sed 's|.*/||'`; do ln -s $t ../folder2/$t; done)

By setting the IFS to only a newline, you should be able to read the entire filename without getting splitted at space.

The brackets are to make sure the loop is executed in a sub-shell and the IFS of the current shell does not get changed.



来源:https://stackoverflow.com/questions/13577769/create-symbolic-link-from-find

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!