小智 43
for file in $(ls -p | grep -v / | tail -100)
do
mv $file /other/location
done
Run Code Online (Sandbox Code Playgroud)
假设文件名不包含空格、换行符(假设默认值为$IFS
)、通配符(?
, *
, [
)或以-
.
Gil*_*il' 40
在 zsh 中最简单:
mv -- *([1,100]) /other/location/
Run Code Online (Sandbox Code Playgroud)
这使第一100非隐藏文件(任何类型的,变化([1,100])
到(.[1,100])
对普通文件只,或(^/[1,100])
任何类型的,但目录)的名称字典顺序。您可以使用o
glob qualifier选择不同的排序顺序,例如移动 100 个最旧的文件:
mv -- *(Om[1,100]) /other/location/
Run Code Online (Sandbox Code Playgroud)
使用其他 shell,您可以在循环中提前退出。
i=0
for x in *; do
if [ "$i" = 100 ]; then break; fi
mv -- "$x" /other/location/
i=$((i+1))
done
Run Code Online (Sandbox Code Playgroud)
另一种可移植的方法是构建文件列表并删除除最后 100 之外的所有文件。
如果您不使用 zsh:
set -- *
[ "$#" -le 100 ] || shift "$(($# - 100))"
mv -- "$@" /target/dir
Run Code Online (Sandbox Code Playgroud)
将移动最后一个(按字母顺序)100 个。
小智 7
以下对我有用。对不起,如果它以前发布过,但我没有在快速扫描中看到它。
ls path/to/dir/containing/files/* | head -100 | xargs -I{} cp {} /Path/to/new/dir
Run Code Online (Sandbox Code Playgroud)
随机移动一百个:
shuf -n 100 -e * | xargs -i mv {} path-to-new-folder
Run Code Online (Sandbox Code Playgroud)
假设所有文件名都不包含引号或反斜杠或以-
空格开头。由于mv
的 stdin 现在是一个管道,因此它可能发出的用于用户确认的提示将不起作用。更正确、可靠和高效的方法(使用支持 ksh 样式进程替换的 shell)将是:
xargs -r0a <(shuf -zn100 -e -- *) mv -t path-to-new-folder --
Run Code Online (Sandbox Code Playgroud)