How to make all files non-executable recursively using find without affecting directories?

4

I need to strip the executable flag from all files within a certain directory and sub directories. Right now I'm doing it with a 2 step process

find /dir/ -type f -exec chmod ugo-x {} \;
find /dir/ -type d -exec chmod ugo+rx {} \;

Is it possible to modify the first line so that I can strip exec flag from all non-directory files? Since this needs to be done on a fairly regular basis across a lot of directories and files, I'd prefer not to use a bash script which would slow it down.

wting

Posted 2010-09-26T08:15:08.957

Reputation: 1 022

Out of curiosity, what exactly doesn't your current command do properly? The -type f predicate already selects all files (or equivalently, all non-directory files, since anything that is a file cannot also be a directory). – David Z – 2010-09-26T08:56:09.710

@David: Sockets, FIFOs, symlinks, devices, etc. – Ignacio Vazquez-Abrams – 2010-09-26T09:09:39.003

@Ignacio: Yes, but the question was about files, not all those other non-file things. – David Z – 2010-09-26T09:26:29.450

@David: This is *nix. Everything is a file. – Ignacio Vazquez-Abrams – 2010-09-26T09:29:35.013

1@Ignacio: Everything has a filesystem path, sure, but you can't always say that everything is actually a file. Some people do, but others use "file" in the sense of a regular file, i.e. something that would be matched by the -type f predicate to find. To me, the wording of this question strongly suggested the latter meaning. – David Z – 2010-09-27T04:15:37.590

Answers

4

find ... '!' -type d ...

Ignacio Vazquez-Abrams

Posted 2010-09-26T08:15:08.957

Reputation: 100 516

Thanks, I didn't realize find had operators. Are the single quotes necessary as I managed to get it to work without them? – wting – 2010-09-26T08:31:30.280

1It depends on your shell and its options. bash uses ! in history expansion, so quoting it in the CLI is a good idea. You probably won't need it in a script though. – Ignacio Vazquez-Abrams – 2010-09-26T09:08:44.917

History expansion is not active in scripts, correct. – Daenyth – 2010-09-26T13:28:54.807