linux - 移动未使用的文件

标签 linux bash file

知道如何移动未被任何进程使用的文件,从而使用 bash 脚本保留对通配符的支持吗?

基本概念是:

for file in $1..$n-1
  if ! fuser file
    mv file $n

其中 $1..$n-1 是源文件/目录,$n 是目标路径。

编辑:工作脚本

#!/bin/bash

# Move files which are not open by any process
dest=${@:$#}  # get last arg
for file in "${@:1:$#-1}"; do  # get all but last args
    fuser "$file" >/dev/null 2>&1 && continue
    mv "$file" "$dest"
done

谢谢大家的帮助!

编辑2 这个脚本有一个错误,某些目录下使用的文件没有被 fuser 检查。稍后会检查这个。

最佳答案

我会将目标目录作为第一个 参数传递。那么你的伪代码就差不多了。

dest=$1
shift
for file; do    # shorthand for for file in "$@"; do
    fuser "$file" >/dev/null && continue
    mv "$file" "$dest"
done

关于linux - 移动未使用的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175119/

相关文章:

javascript - 如何在 Javascript 组件文件之间导出/导入模块

php - 这个foreach循环负责神杀猫吗?

c - 读取 .txt 文件并获取单词/整数作为字符串

c - 将 pid 附加到文件名

python - 使用python查找USB大容量存储设备的卷标

c - 现在实现平板分配器值得吗?

python - linux anaconda环境不安装模块

bash - 如何在 cron 中的每个月的第一个工作日运行脚本?

bash - 为什么有人会在 shell 脚本中使用两个 shebang?

json - 在 bash 中使用 jq 在 json 文档上链接 select 和 max_by