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:
- directory
- file extension
- u/g/o (only one)
- 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.