Home > Back-end >  How do I change the permissions of all files of one extension using bash?
How do I change the permissions of all files of one extension using bash?

Time:08-26

I'm trying to write a script that changes permissions based on file extensions and command line arguments given.

The command line arguments that will be given are:

  1. directory
  2. file extension
  3. u/g/o (only one)
  4. r/w/x (only one)

for example, if you give .txt and u r, it will give all the users read permissions to files that end in .txt.

I know how to change the permissions based on the arguments given, but I'm not sure how to make it only apply to files of a certain extension in a folder

CodePudding user response:

You could use the following command:

find /folder/path/ -name "*.txt" -exec chmod  x {} \;

where /folder/path, *.txt and x must be customized by you.

CodePudding user response:

Use this command:

shopt -s globstar
chmod u r /path/to/directory/**/*.txt

Bash manual of globstar:

If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.

Of course, customize the arguments of the chmod command to suit other needs.

  • Related