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

北战南征 提交于 2020-01-13 19:09:37

问题


I'm not exactly sure how to go about doing this, but I need to create symbolic links for certain files in one directory and place the symbolic links in another directory.

For instance, I want to link all files with the word "foo" in its name in the current directory bar1 that does not have the extension ".cc" and place the symbolic links in a directory bar2.

I was wondering if there was single line command that could accomplish this in LINUX bash.


回答1:


Assuming you are in a directory that contains directories bar1 and bar2:

find bar1 -name '*foo*' -not -type d -not -name '*.cc' -exec ln -s $PWD/'{}' bar2/ \;



回答2:


Try this:

cd bar1
find . -maxdepth 1 -name '*foo*' -not -name '*.cc'  -exec echo ln -s $PWD/{} ../bar2 \;

Once you are satisfied with the dry run, remove echo from the command and run it for real.




回答3:


This is easily handled with extended globbing:

shopt -s extglob
cd bar2
ln -s ../bar1/foo!(*.cc) .

If you really want it all on one line, just use the command separator:

shopt -s extglob; cd bar2; ln -s ../bar1/foo!(*.cc) .

The two examples are identical, but the first is much easier to read.




回答4:


This technically doesn't count as a one line answer...but it can be pasted in a single instance and should do what you are looking for.

list=`ls | grep foo | grep -v .cc`;for file in $list;do ln $file /bar2/;done


来源:https://stackoverflow.com/questions/10888105/how-do-i-selectively-create-symbolic-links-to-specific-files-in-another-director

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