shell脚本,它将当前目录中的所有可执行文件移动到一个单独的文件夹中

时间:2016-07-28 17:23:32

标签: linux bash shell

我尝试编写shell脚本,它会将当前目录中的所有可执行文件移动到名为" executables"的文件夹中。

  1   for f in `ls`
  2    do
  3     if [ -x $f ]
  4      then
  5       cp -R $f ./executable/
  6     fi
  7    done

执行时,它说

cp: cannot copy a directory, 'executable', into itself, './executable/executable'.

所以我如何避免检查可执行文件' if条件下的文件夹。  或者还有其他任何完美的解决方案。

1 个答案:

答案 0 :(得分:0)

  1. 不解析ls
  2. 的输出
  3. 大多数目录都设置了可执行位。
  4. cp正在复制,mv正在移动。
  5. 调整脚本:

    for f in *; do
      if [ -f "$f" ] && [ -x "$f" ]; then
        mv "$f" executables/
      fi
    done
    

    使用GNU find

    $ find . -maxdepth 1 -type f -perm +a=x -print0 | xargs -0 -I {} mv {} executables/
    
相关问题