Linux 移动未使用的文件

Linux 移动未使用的文件,linux,bash,file,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

知道如何移动任何进程都不使用的文件吗?使用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
谢谢你们的帮助

EDIT2
此脚本中有一个错误,fuser未检查某些目录下使用的文件。稍后将对此进行检查。

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

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

lsof
将为您提供以下信息:如果任何进程正在使用目录中的文件,如果未使用,则将其移动到其他位置

for file in $1..$n-1;do
   var=`lsof +D $file`
   if [[ -z "$var" ]]; then
        mv $file $n
   fi
done

“未使用”文件的具体标准是什么?不能由任何其他进程打开,可以使用
fuser
命令进行检查。感谢您的代码!我对其进行了编辑,并添加了支持,以传递更自然的参数,同时将所有输出从fuser重定向到/dev/null。
for x in "$@"
do
  target="$x"
done
test -d "$target" ||exit       # last arg isn't a dir


for source in "$@"
do
  test "$source" = "$target" && continue
  if test -d "$source"
    then
      # source is a dir, check all files in it
      for f in "$source"/*
      do
        fuser "$f" || mv "$f" "$target"
      done
    else
      # source isn't a dir
      fuser "$source" ||mv "$source" "$target"
  fi
done