简体   繁体   English

如何在LINUX的另一个目录中有选择地创建指向特定文件的符号链接?

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

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. 例如,我想在当前目录bar1中用名称中的单词“ foo”链接所有文件,该目录不具有扩展名“ .cc”,并将符号链接放置在目录bar2中。

I was wondering if there was single line command that could accomplish this in LINUX bash. 我想知道在LINUX bash中是否有单行命令可以完成此任务。

假设您位于包含目录bar1bar2目录中:

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. 对空运行感到满意后,请从命令中删除echo并使其真正运行。

This is easily handled with extended globbing: 使用扩展的glob很容易处理:

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. 这两个例子是相同的,但第一个例子更容易阅读。

从技术上讲,这不算单行答案...但是可以将其粘贴到单个实例中,并且应该执行您想要的操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从另一个目录访问目录中的文件(linux) - how do i access files in directory from another directory (linux) 如何将具有符号链接的文件夹复制到另一个目录 - How do i copy a folder having symbolic link to another directory 如何在Linux中使用命令Shell在目录中创建特定文件的数组? - How do I make an array of specific files in a directory using the command shell in linux? 如何使用链接在Linux上创建目录的副本 - How to create a copy of a directory on Linux with links 如何在 linux 平台上使用 C# 和 .Net 核心以编程方式创建符号链接 - How do I create symbolic link programatically using C# and .Net core on a linux platform 如何比较unix(Linux)中的2个符号链接? - How to compare 2 symbolic links in unix (Linux)? 当路径包含符号链接时,如何在unix / linux中移回一个目录? - How to move one directory back in unix / linux when path contains symbolic links? 批量移动文件并在Linux目录中的位置处创建链接 - Move files in bulk and create links in their place in the directory in linux 复制目录,并带有指向复制文件的符号链接(在目录树内部) - Copy directory with symbolic links pointing to the copied files (inside directory tree) 删除目录中的所有文件而不删除符号链接及其指向的文件 - Delete All Files In a Directory Without Deleting Symbolic Links and the Files They Point To
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM