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

守給你的承諾、 提交于 2019-12-06 09:35:41

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/ \;

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.

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.

Al Greene

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