简体   繁体   中英

How to find all .sh files and make them executable using bash in linux?

also when I launch I want to pass path to folder when located these .sh files

I started with this

#!/bin/bash
find /home/user_name -name "*.sh" 

And after script has to write in logo list with executable files

The safest and way both in terms of security and in terms of weird file names (spaces, weird characters, and so forth) is to use find directly:

find /home/user -name "*.sh" -execdir chmod u+x {} +

You can check the comments and the manual of find why this is safe, but in short, it makes sure your file is properly quoted in the chmod command. execdir (rather then -exec ) is an extra security feature making sure the command is executed in the directory the file was found in avoiding race conditions (elaborated in the manual).

如果你想让当前用户的所有文件都可以执行,你可以使用如下命令(假设你对目标主文件夹中的所有文件都有权限):

find /home/user_name -name "*.sh" -print0 | xargs -0 chmod u+x

another way :

find . -name "*.sh" -exec chmod ux+y {} \;

you can first check your command by using

find . -name "*.sh" -print

By @kabanus command

#!/bin/bash

# chmod u+x $(find $1 -name "*.sh")
# ls -1 $1/*.sh
find $1 -name "*.sh" -print -exec chmod u+x {} +

And use as

$ ./script.sh /your_directory

/your_directory - first argument ( $1 ) in the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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