如何创建一个bash文件,为从文件夹A移动到文件夹B的每个文件创建符号链接(linux)。 但是,这将从文件夹A中选择150个最大的当前文件。
答案 0 :(得分:0)
可以将它写在一个内容中,但在像
这样的简单bash脚本中更容易#!/bin/bash
FolderA="/some/folder/to/copy/from/"
FolderB="/some/folder/to/copy/to/"
while read -r size file; do
mv -iv "$file" "$FolderB"
ln -s "${FolderB}${file##*/}" "$file"
done < <(find "$FolderA" -maxdepth 1 -type f -printf '%s %p\n'| sort -rn | head -n150)
注意${file##*/}
会移除上一个/
之前的所有内容,每个
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the
pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of
parameter with the shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case)
deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and
the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal
operation is applied to each member of the array in turn, and the expansion is the resultant list.
此外,仅执行for file in $(command)
似乎是一个好主意,但过程替换和while/read
works better in general to avoid word-splitting issues like splitting up files with spaces, etc...
答案 1 :(得分:-1)
与任何任务一样,将其分解成更小的部分,事情就会落到实处。
从
中选择最大的150个文件FolderA
可以使用du
,sort
和awk
来完成此操作,您将其输出放入数组中:
du -h /path/to/FolderA/* | sort -h | head -n150 | awk '{print $2}'
将文件从
FolderA
移至FolderB
从最后一个命令中取出列表,然后迭代它:
for file in ${myarray[@]}; do mv "$file" /path/to/FolderB/; done
为新位置创建符号链接
再次,只需遍历列表:
for file in ${myarray[@]; do ln -s "$file" "/path/to/FolderB/${file/*FolderA\/}"; done