PDA

View Full Version : Using "chmod" in a simple shell script



Matodo
June 4th, 2011, 01:45 AM
Hello

I'm trying to write a shell script that takes a folder name as an argument, and deletes "x" permissions for all files inside thas folder and in each subfolder.

Here is what I tried:

#!/bin/bash
fname=$1
chmod -x -R $fname

The problem is, I can't access those folders after executing that script. I only hoped to apply those changes on ordinary files inside those folders, but not on folders themselves.

How can I do it?
Thanks a lot :)

cgroza
June 4th, 2011, 02:08 AM
I suggest you recursively cd to each folder to apply your chomd command.

matt_symes
June 4th, 2011, 02:28 AM
Hi


#!/bin/bash

# Use find.
find "$1" -maxdepth 0 -type f -exec chmod -x '{}' \;Quote your paths or paths with spaces may give you problems.

-maxdepth 0 option will only change in that directory. Adjust -maxdepth for other depth traversals in the file system or remove -maxdepth to recurse to bottom of tree from the path passed.

Add error checking for the path as well. Make sure it exists ;)

Kind regards

Petrolea
June 4th, 2011, 10:41 AM
Hello

I'm trying to write a shell script that takes a folder name as an argument, and deletes "x" permissions for all files inside thas folder and in each subfolder.

Here is what I tried:


The problem is, I can't access those folders after executing that script. I only hoped to apply those changes on ordinary files inside those folders, but not on folders themselves.

How can I do it?
Thanks a lot :)

How about adding 'read' at the same time?