How do I selectively create symbolic links to spec

2019-05-22 06:56发布

问题:

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