繁体   English   中英

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

[英]shell script which will move all executable files in the current directory to a seperate folder

我试图编写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条件下检查“ executable”文件夹。 或对此有任何其他完美的解决方案。

  1. 不要解析ls的输出。
  2. 大多数目录都有可执行位设置。
  3. cp正在复制, mv正在移动。

修改脚本:

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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM