gpt4 book ai didi

linux - 如何创建一个 bash 文件,为从文件夹 A move 到文件夹 B 的每个文件创建符号链接(symbolic link) (linux)

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:10 26 4
gpt4 key购买 nike

如何创建一个 bash 文件,为从文件夹 A move 到文件夹 B 的每个文件创建符号链接(symbolic link) (linux)。但是,这需要从文件夹 A 中选择 150 个最大的当前文件来完成。

最佳答案

可能可以用一行代码编写,但用简单的 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.

此外,仅对 $(command) 中的文件执行 似乎是个好主意,但要处理替换和 while/read works better in general to avoid word-splitting issues like splitting up files with spaces, etc...

关于linux - 如何创建一个 bash 文件,为从文件夹 A move 到文件夹 B 的每个文件创建符号链接(symbolic link) (linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21788630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com