3

i hate retyping chmod +x filename and insect to run from terminal. how do i set this for all .sh files ? I've tried Inspect then manually setting the extension

1
  • 1
    Permissions are attached to each file, and not a file type. are you looking for a way to iterate the filesystem and automate dozens or thousands of changes or is this more a software recommendation to make a helper app that does things on demand?
    – bmike
    Commented Jul 26, 2019 at 12:10

1 Answer 1

5

Using a find command would be simple enough:

find . -iname "*.sh" -exec bash -c 'chmod +x "$0"' {} \;

The command breaks down as
find [obvious!]
. = from this folder. You can put a path instead
-iname = case insensitive name
"*.sh" = wildcard filename
-exec = utility to execute commands
bash = what tool you want to use (you can use sh instead)
-c flag means execute the following command as interpreted by this program.
chmod +x = command to change the file to executable
"$0" = The value that was passed to the utility
{} = If the string {} appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file.
; = Terminates the command

4
  • 1
    Nice explanation; however, you did not explain the backslash before the semicolon, but did explain all the other arguments of the command. I'm sure there are many users that don't understand why or even that the semicolon is being escaped by the backslash as it's a special character to the shell, and if not escaped find would complain with find: -exec: no terminating ";" or "+". The ; could also be placed in quotes, e.g. ';', but why type an extra character when a single backslash will do. :) Commented Jul 26, 2019 at 11:57
  • 1
    Actually, iname "*.sh" interprets as files that end with .sh, .SH, .sH, or Sh.
    – fd0
    Commented Jul 26, 2019 at 13:31
  • @user3439894 Well spotted. Forgot to explain the escape char! Thanks for adding.
    – chriszanf
    Commented Jul 29, 2019 at 10:01
  • @fd0 Covers those who have fat fingers when saving files (I have on occasion saved files with varied case extensions!)
    – chriszanf
    Commented Jul 29, 2019 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .