Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux shell脚本,将当前目录中的所有可执行文件移动到单独的文件夹中_Linux_Bash_Shell - Fatal编程技术网

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

Linux shell脚本,将当前目录中的所有可执行文件移动到单独的文件夹中,linux,bash,shell,Linux,Bash,Shell,我正在尝试编写shell脚本,该脚本将当前目录中的所有可执行文件移动到名为“executables”的文件夹中 当执行时,它说 cp: cannot copy a directory, 'executable', into itself, './executable/executable'. 因此,我如何避免在if条件下检查“executable”文件夹。 或者有任何其他完美的解决方案 不要解析ls的输出 大多数目录都设置了可执行位 cp正在复制,mv正在移动 调整脚本: for f in *

我正在尝试编写shell脚本,该脚本将当前目录中的所有可执行文件移动到名为“executables”的文件夹中

当执行时,它说

cp: cannot copy a directory, 'executable', into itself, './executable/executable'.
因此,我如何避免在if条件下检查“executable”文件夹。 或者有任何其他完美的解决方案

  • 不要解析
    ls
    的输出
  • 大多数目录都设置了可执行位
  • cp
    正在复制,
    mv
    正在移动
  • 调整脚本:

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

    $ find . -maxdepth 1 -type f -perm +a=x -print0 | xargs -0 -I {} mv {} executables/
    
  • 不要解析
    ls
    的输出
  • 大多数目录都设置了可执行位
  • cp
    正在复制,
    mv
    正在移动
  • 调整脚本:

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

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

    尝试使用
    find
    实用程序进行此类操作。它提供了一些特定于文件处理的功能,而不是基于字符串的操作。例如,您可以将搜索限制为普通文件。尝试使用
    find
    实用程序查找此类文件。它提供了一些特定于文件处理的功能,而不是基于字符串的操作。例如,您可以将搜索限制为普通文件。