在shell脚本中设置目标文件夹

时间:2013-12-28 06:03:34

标签: linux bash shell terminal

我正在尝试将一些文件从/path/to/folder1符号链接到/path/to/folder2,并将一些自定义文本附加到生成的符号链接名称中,但是,在最终确定正确处理basename之后,语法要么不起作用或我cd /path/to/folder2只使用${file%%.*}-something.txt文件符号链接到/path/to/folder1

这是我的代码:

for file in $(find /path/to/folder1 -iname '*.txt' ! -name 'file.txt' -type f)
do ln -s $file /path/to/folder2/${file%%.*}-something.txt
done

如何正确地做到这一点?

1 个答案:

答案 0 :(得分:2)

对于文件/path/to/folder1/abc.txt${file%%.*}会为您提供/path/to/folder1/abc

所以你ln命令转换为:

ln -s /path/to/folder1/abc.txt /path/to/folder2//path/to/folder1/abc-something.txt

如果您想将/path/to/folder1/abc.txt/path/to/folder2/abc-something.txt相关联,可以这样做:

for file in $(find /path/to/folder1 -iname '*.txt' ! -name 'file.txt' -type f)
    do ln -s $file /path/to/folder2/$(basename $file .txt)-something.txt
done