在 Debian(或其他 Linux 系统)中,“批量移动文件”通常有几种常见做法,取决于你按什么条件筛选文件。下面按使用场景给你总结最实用的方法。
mv *.jpg /target/dir/
把当前目录下所有 .jpg 文件移动到 /target/dir/
mv *.jpg *.png /target/dir/
使用 find + mv:
find . -maxdepth 1 -name "*.log" -exec mv {} /target/dir/ \;
说明:
. 当前目录-maxdepth 1 不进入子目录*.log 文件名匹配-exec 对每个文件执行 mvfind . -maxdepth 1 -type f -exec mv {} /target/dir/ \;
find . -maxdepth 1 -type d -exec mv {} /target/dir/ \;
find . -maxdepth 1 -type f -size +10M -exec mv {} /target/dir/ \;
find . -maxdepth 1 -type f -size -1M -exec mv {} /target/dir/ \;
find . -maxdepth 1 -type f -mtime -7 -exec mv {} /target/dir/ \;
find . -maxdepth 1 -type f -mtime +30 -exec mv {} /target/dir/ \;
xargs(更快,适合大量文件)find . -maxdepth 1 -name "*.txt" | xargs -I {} mv {} /target/dir/
或:
find . -maxdepth 1 -name "*.txt" -print0 | xargs -0 mv -t /target/dir/
先看看会移动哪些文件:
find . -maxdepth 1 -name "*.log"
确认无误后再执行 mv
例如:加前缀
for f in *.jpg; do
mv "$f" "backup_$f"
done
如果你能告诉我:
我可以给你精确的一条命令。