简体   繁体   English

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

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

I trying to write shell script which will move all executable files in the current directory to a folder called "executables". 我试图编写shell脚本,它将当前目录中的所有可执行文件移动到名为“ executables”的文件夹中。

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

when executed ,it says 当执行时,它说

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

so how i avoid checking the 'executable' folder in the if condition. 因此,如何避免在if条件下检查“ executable”文件夹。 or there is any other perfect solution for this. 或对此有任何其他完美的解决方案。

  1. Don't parse the output of ls . 不要解析ls的输出。
  2. Most directories have the executable bit set. 大多数目录都有可执行位设置。
  3. cp is copying, mv is moving. cp正在复制, mv正在移动。

Adapting your script: 修改脚本:

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

With GNU find : 使用GNU find

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

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

相关问题 Shell脚本-将文件移动到文件夹 - Shell script - move files into folder Shell Script,将每 2 个文件移动到每个目录 - Shell Script, move each 2 files to each directory linux shell脚本在当前文件夹中运行可执行文件 - linux shell script to run an executable file in current folder Shell 脚本当前目录? - Shell script current directory? Shell脚本:需要使用当前目录之前的文件夹 - Shell Script: Need to use previous folder than current directory linux将子文件夹中的文件移动到当前目录 - linux move files inside sub folder to current directory Shell脚本 - 查找今天修改的文件,创建目录并将其移动到那里 - Shell script - Find files modified today, create directory, and move them there 使用 shell 脚本删除目录下的所有文件,将文件目录作为参数传递但不想删除文件夹本身 - Deleting all the files under a directory using shell script, passing file directory as an argument but do not want to delete the folder itself Bash / Shell-将子目录中的所有文件移动到目标目录中? - Bash/Shell-Move all files from subdirectories into target directory? 如何使用根目录中存在的所有目录下特定文件夹中的Shell脚本列出所有文件? - How to list all the files using shell script present in a specific folder under all the directories those are present in the root directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM