one line command to change mode only if necessary

hi,
sorry for posting this for a quick answer.
Is there a one line command to change permissions on files in a directory to a given mode (say 554) and only for those files that do not already have that mode?

Running chmod updates the last access/modified timestamp on the files, and i want to only run chmod on those that do not already have permissions 554 set

psudeo code:
for files where mode <> 554, do chmod 554

Thanks,
-srinivas yelamanchilli

Yes!!

chmod 554 * 

:smiley:

More seriously what would be the point to look if the files have or not tests are CPU/time consuming...
My concern is more what about if directories are found there, they would be unreadable for others, if thst is the case (subdirs...) you are anyway going to have to look, in which case the one liner you are looking for is most certainly going to use find ...

chmod 554 *
works and on all the files and that's not what i want to happen

I am hoping someone could post a one line command to only apply to files in the current directory where the file is not already set to 554

Thanks,
-srinivas

#!/bin/sh

for i in *; do
        [ $(stat -c%a "$i") -ne 554 ] && echo "$i"
done

exit 0

Try this as this (dry run). Once you're OK with the output, change echo "$i" with chmod 554 "$i" to do the real job.

As vbe mentioned, find can do the job. Consult the man page for how to use -perm and -exec.

Regards,
Alister

1 Like

Thanks tukuyomi,
i tried and get this error on hp-ux 11.11 box
cm.sh[3]: stat: not found.
cm.sh[3]: test: Specify a parameter with this command.

We don't have stat,fstat or lstat available !!!

Thanks,
-srinivas

I would like some explanations here...

I'll bite:

find . -type f  ! -perm 554 -exec chmod 554 {} \;
1 Like

...we tried hard to make the person search a bit and you gave the answer away...:rolleyes:
well, still waiting an answer about timestamp...:smiley:

Sorry. :o

:b: :wink:

Gary, that was exactly what i needed, thank you.

Regarding why i am doing this, when chmod is done the 'c' attribute (ls -ltc) gets updated and i don't want this to be changed for those files that are already have the correct permissions.

Thanks