如何使整个目录可执行?
我有一个专门用于python脚本的整个文件夹。
我厌倦了在我写的每个新的python脚本上做chmod。
有没有办法让我文件夹中的每个文件都可执行,如果它是python脚本?
如果有一个脚本可以检查每当创建一个新的.py脚本并且是否有一个新的.py脚本使它在现场可执行时,那将是一件好事。
- 我用Vim。
另一个不错的选择是Incron 。 它适用于具有给定位置的可指定条件的inotify。
所以我可以说看这个文件夹,当你看到创建的文件时,运行一个命令。
就像一个样本incrontab …
/path/to/scripts IN_CREATE chmod +x $@$# # <--- this arcane bit is path ($@) + file ($#)
可以类似地使用路径/文件作为bash脚本的参数,以允许它在需要时通过.py
扩展名进行过滤。
chmod +x /path/to/python/scripts/dir/*.py
将目录/ path /中的所有当前.py
文件设置为/ python / scripts / dir 。
我不知道你描述的自动工具。 可能在编辑器中有一个宏可以执行此操作,但不能使用我使用的编辑器。 😉
作为第一步,您可以在~/.vimrc
尝试:
autocmd BufWritePost *.py silent execute "! chmod +x %"
- 当您写入时,这会在所有
.py
文件的文件名上运行chmod +x
。 查看事件列表(:h events
),我找不到创建新文件的事件,因此每次写入时我都必须满足于运行。 -
第一次应用
chmod
时,文件会被更改,vim
会提醒您:"test.py" [New] 0L, 0C written W16: Warning: Mode of file "test.py" has changed since editing started See ":help W16" for more info. [O]K, (L)oad File:
我尝试了几个技巧,让它只是为了这个改变自动
autoread
,但没有运气。 所以你必须按Enter键两次。
启动时,下面的脚本会自动更改目录中给定类型(扩展名)的所有文件的权限(一次)。 之后,脚本每5秒检查一次目录以查找新添加的文件 , 如果文件属于给定类型(在本例中为.py
文件),则更改权限
它有几个选项:在这种情况下,它使新添加的文件可执行,但也可以执行其他操作,如行中所定义: command = "chmod +x"
。 此外,您可以定义(更改)应执行操作的文件类型(语言扩展名)。
如何使用
将下面的脚本复制到一个空文件中。 将其另存为change_permission.py
并通过以下命令在后台运行:
python3
剧本
#!/usr/bin/env python3 import subprocess import time import sys directory = sys.argv[1] command = "chmod +x"; check_interval = 5; extensions = (".py") def current_files(): read = subprocess.check_output(["ls", directory]).decode("utf-8").strip() return [item for item in read.split("\n") if item[item.rfind("."):] in extensions] initial_files = current_files() for file in initial_files: subprocess.call(["/bin/bash", "-c", command+" "+directory+"/"+file]) while True: update = current_files() for file in update: if not file in initial_files: subprocess.call(["/bin/bash", "-c", command+" "+directory+"/"+file]) initial_files = update time.sleep(check_interval)
*注意:如果您需要sudo权限,只需使用sudo
运行脚本即可
以下是一些可能有用的命令信息,请查看http://ss64.com/bash/syntax-permissions.html
find . -type f -print0 | xargs -0 chmod 775 # change all file permissions in current directory find . -type d -print0 | xargs -0 chmod 755 # change directory permissions
您可以使用以下标头脚本。 将mkscript.sh
放在$PATH
。 从存储python脚本的工作目录执行mkscript.sh
。 该脚本创建一些有用的标题信息,标题脚本并使其可执行,然后打开所选的编辑器; 在你的情况下VIM。
我修改了mkscript.sh
,它将生成带有python扩展名*.py
脚本
调用变量${PYTHON_VERSION}
,因此PYTHON_VERSION="/usr/bin/python --version"
已添加到/etc/environment
文件中。 请查看https://help.ubuntu.com/community/EnvironmentVariables
#!/bin/bash - #title :mkscript.sh #description :This script will make a header for a PYTHON script. #author :bgw #date :20111101 #version :0.4 #usage :bash mkscript.sh #notes :Install Vim and Emacs to use this script. #bash_version :4.1.5(1)-release #============================================================================== today=$(date +%Y%m%d) div======================================= /usr/bin/clear _select_title(){ # Get the user input. printf "Enter a title: " ; read -r title # Remove the spaces from the title if necessary. title=${title// /_} # Convert uppercase to lowercase. title=${title,,} # Add .sh to the end of the title if it is not there already. [ "${title: -3}" != '.py' ] && title=${title}.py # Check to see if the file exists already. if [ -e $title ] ; then printf "\n%s\n%s\n\n" "The script \"$title\" already exists." \ "Please select another title." _select_title fi } _select_title printf "Enter a description: " ; read -r dscrpt printf "Enter your name: " ; read -r name printf "Enter the version number: " ; read -r vnum # Format the output and write it to a file. printf "%-16s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %-16s%-8s\n\ %s\n\n\n" '#!/usr/bin/python -' '#title' ":$title" '#description' \ ":${dscrpt}" '#author' ":$name" '#date' ":$today" '#version' \ ":$vnum" '#usage' ":./$title" '#notes' ':' '#python_version' \ ":${PYTHON_VERSION}" \#$div${div} > $title # Make the file executable. chmod +x $title /usr/bin/clear _select_editor(){ # Select between Vim or Emacs. printf "%s\n%s\n%s\n\n" "Select an editor." "1 for Vim." "2 for Emacs." read -r editor # Open the file with the cursor on the twelth line. case $editor in 1) vim +12 $title ;; 2) emacs +12 $title & ;; *) /usr/bin/clear printf "%s\n%s\n\n" "I did not understand your selection." \ "Press to quit." _select_editor ;; esac } _select_editor