Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

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

like image 332
ASdsdgf Avatar asked Jun 28 '17 09:06

ASdsdgf


People also ask

How do I make a .sh file executable from anywhere?

Make the scripts executable: chmod +x $HOME/scrips/* This needs to be done only once. Add the directory containing the scripts to the PATH variable: export PATH=$HOME/scrips/:$PATH (Verify the result with echo $PATH .) The export command needs to be run in every shell session.


2 Answers

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).

like image 148
kabanus Avatar answered Nov 04 '22 04:11

kabanus


another way :

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

you can first check your command by using

find . -name "*.sh" -print
like image 33
mbieren Avatar answered Nov 04 '22 02:11

mbieren