我有一个包含超过 500 个视频的文件夹,该文件夹的大小约为 80 GB。
我想将 30GB 的视频移动到另一个文件夹,但如何操作呢?
我正在使用终端,无法安装 GUI。
我不知道有哪个命令行工具可以做到这一点...但是,您可以使用脚本来移动文件,直到移动了一定的总大小,例如像这样(仅运行脚本一次...如果再次运行它,它将再次从剩余文件中移动指定的大小):
#!/bin/bash
sourc_dir="/path/to/directory" # Set the path to the source directory "no trailing /"
dest_dir="/path/to/directory" # Set the path to the destination directory "no trailing /"
move_size="30" # Set the total size of files to move in Gigabytes
# Don't edit below this line
move_size=$(( move_size * 1000000 ))
current_size=$(du -sk "$sourc_dir" | awk '{printf "%d", $1}')
remaining_size=$(( current_size - move_size ))
for f in "$sourc_dir"/*; do
[ -f "$f" ] || continue
new_size=$(du -sk "$sourc_dir" | awk '{printf "%d", $1}')
(( new_size <= remaining_size )) && exit
mv -n -- "$f" "$dest_dir"/
done
Run Code Online (Sandbox Code Playgroud)
或者甚至是一个单行代码(不需要脚本文件),您可以在终端中运行,例如:
for f in /path/to/source/*; do [ -f "$f" ] || continue; (( size >= (30*(1000**3)) )) && break || (( size += $(stat --printf "%s" "$f") )) && mv -n -- "$f" /path/to/destination/; done
Run Code Online (Sandbox Code Playgroud)
您可能希望unset size能够在同一个 shell 实例中重新运行它。
您始终可以根据文件名、大小、类型等从搜索工具指定 shell 模式或管道。
或者,更方便的是,您可以使用基于控制台的文件管理器,它支持多个文件选择,例如众所周知且功能丰富的GNU Midnight Commander,它可以从 Ubuntu 官方存储库获得,并且可以像这样安装:
sudo apt install mc
Run Code Online (Sandbox Code Playgroud)
要运行它,请键入mc并点击Enter...然后您将看到一个可导航的用户界面,您可以在其中使用Insert键盘键选择多个文件...您将能够看到所选文件的总大小及其数量实时如下:
选择完要移动的所有文件后,按F6键盘键打开移动/重命名对话框并输入目标目录,然后选择[< OK >]立即移动所选文件,如下所示:
或者甚至是鲜为人知、功能较少的fff ( Fucking Fast File-Manager ) :-) ...这是有趣的写法bash...虽然它支持多个文件选择,但您可能需要对其进行一些自定义才能看到所选文件的总大小,例如作为快速补丁,您可以更改159源fff文件中的行:
sudo apt install mc
Run Code Online (Sandbox Code Playgroud)
对此:
local mark_ui="[${#marked_files[@]}] selected (${file_program[*]}) [p] ->"
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样简单地运行该文件(无需安装):
local mark_ui="[${#marked_files[@]}] $(for f in "${marked_files[@]}"; do (( tsize += $(stat --printf "%s" "$f") )); done; tsize=$((tsize/(1000**2))); printf '[%d M]' "$tsize";) selected (${file_program[*]}) [p] ->"
Run Code Online (Sandbox Code Playgroud)
或者查看使用帮助,如下所示:
bash fff
Run Code Online (Sandbox Code Playgroud)
然后您所要做的就是简单地导航到源目录并开始使用m(小写)键盘键选择要移动的文件,如下所示:
选择文件后,导航到目标目录并通过按p(小写)键盘键将先前选择的文件移动到该目录。
这是Raffa想法的替代方案,不需要du每个文件的整个目录。相反,它对stat每个文件执行一次,使用find来-printf %s获取大小并%p获取要移动的文件的路径。
#!/bin/bash
src='foo'
dest='bar'
# 30GB in bytes to move:
(( bytestomove=30*(1000**3) ))
movedbytes=0
while IFS=$'\t' read -r -d '' bytes file
do
# if we already moved enough, break out
(( movedbytes >= bytestomove )) && break;
# try to move it, break out if it fails
mv -n -- "$file" "$dest" || break
# successfully moved, add bytes to movedbytes
(( movedbytes += bytes ))
done < <(find "$src" -type f -printf "%s\t%p\0")
(( movedmegs=movedbytes/(1000**2) ))
printf "Moved %d MB\n" $movedmegs
Run Code Online (Sandbox Code Playgroud)