要将一个文件夹内的所有文件都软连接到另一个文件夹,并忽略重复的文件,可以使用ln命令结合循环和条件判断来实现。以下是一个使用Bash脚本的示例:
假设:
• 源文件夹是/path/to/source
• 目标文件夹是/path/to/destination步骤: 1. 打开终端。
2. 使用以下脚本:
【bash】
#!/bin/bash
source_dir="/path/to/source"
destination_dir="/path/to/destination"# 确保目标文件夹存在
sudo mkdir -p “KaTeX parse error: Expected 'EOF', got '#' at position 17: …estination_dir"#̲ 遍历源文件夹中的所有文件
f…source_dir”/*; do
# 获取文件名
filename=(basename"(basename "(basename"file")
# 目标文件的完整路径
destination_file=“destinationdir/destination_dir/destinationdir/filename”
# 检查目标文件夹中是否已经存在同名文件
if [ ! -e “KaTeX parse error: Expected 'EOF', got '#' at position 58: … #̲ 如果不存在,则创建软链接
…file” “$destination_file”
echo “Created symlink: $destination_file -> $file”
else
echo “Skipped (exists): destinationfile"fidone说明:•mkdir−p"destination_file"
fi
done
说明:
• mkdir -p "destinationfile"fidone说明:•mkdir−p"destination_dir”:确保目标文件夹存在,如果不存在则创建。
• for file in “sourcedir"/∗;do:遍历源文件夹中的所有文件。•basename"source_dir"/*; do:遍历源文件夹中的所有文件。
• basename "sourcedir"/∗;do:遍历源文件夹中的所有文件。•basename"file”:提取文件名(去掉路径)。
• if [ ! -e “$destination_file” ]; then:检查目标文件夹中是否已经存在同名文件。
• ln -s "file""file" "file""destination_file"
:创建软链接。
• echo:输出操作信息,方便查看哪些文件被链接或跳过。
使用方法:
- 将上述脚本保存为一个文件,例如create_symlinks.sh。
- 给脚本添加执行权限:【bash】 chmod +x create_symlinks.sh
- 运行脚本:
【bash】
./create_symlinks.sh
或者指定编译器运行(用上面的命令大概率会报错,要么找不到,要么权限不够)
sudo /usr/bin/bash create_symlinks.sh
这样,脚本会将源文件夹中的所有文件软链接到目标文件夹,并忽略已经存在的文件。
适合于安装完一堆库后,直接全部软连接
文件编辑完后,可能会出现在 Linux 系统中,\r 会被解释为普通字符,导致出现类似 KaTeX parse error: Can't use function '\r' in math mode at position 2: '\̲r̲': 未找到命令 的错误。 执…//’ your_script.sh
这个文本太难搞,给张图片吧,我优化了一下