如何在 ubuntu 终端上获取 fstrim 的进度条

Alf*_*.37 4 command-line trim

有时,如果您在终端上运行以下命令,可能需要一些时间,具体取决于要修剪的空间。

sudo fstrim -v /
Run Code Online (Sandbox Code Playgroud)

如何为此获取进度条。

我的研究结果:

进度条在其他一些情况下也是可能的:

Art*_*ild 5

您可以创建自己的函数,在等待上一个作业完成时绘制进度条:

# Function to show progbar while running command in the background
show_progbar() {
  # Uses PID of previous command, unless PID is given as 1st parameter
  if [[ -z $1 ]]
  then
    PID=$!
  else
    PID=$1
  fi
  ii=0
  jj=0
  bar="="       # Character to draw
  echo -n " [ "

  # While the process with PID is running, progbar is drawing
  while [[ -d "/proc/$PID" ]]
  do
    printf "%b" "\b${bar}]"
    (( ii++ ))
    sleep .1    # Speed of the progress bar
  done
  (( ii=ii+3 ))

  # Erases the progress bar after it is done
  while [[ $jj -lt $ii ]]
  do
    printf "\b"
    (( jj++ ))
  done
}
Run Code Online (Sandbox Code Playgroud)

这必须在您的 shell(或脚本)中定义或获取。要使用它,请运行以下命令:

sudo fstrim -v / & \
show_progbar
Run Code Online (Sandbox Code Playgroud)

(从 shell 运行时需要反斜杠来换行 - 在脚本中可以省略。)