Score:0

Efficiently removing x-bit from files in large directory

vn flag

I have a directory /some/dir containing millions of files and directories. I need to remove the X-bit for all file, but not from the directories.

The following works:

find /some/dir -type f -chmod -x {} \;

But I believe it spawns a "chmod" process for each file. That may explain why it runs for a long time.

Is there a way to do this in a more efficient manner?

Score:3
jp flag

Use xargs to limit the spawning:

find /path/to/base/dir -type f -print0 | xargs -0 chmod -x
Score:1
cn flag

Besides xargs, as Ljm Dullaart was mentioning, you can use:

find /some/dir -type f -exec chmod -x {} +

As a debug trick, you can see the generated command with:

find /some/dir -type f -exec echo chmod -x {} +

From the man page of find(1) command:

-exec command {} +
This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of {} is allowed within the command, and it must appear at the end, immediately before the +; it needs to be escaped (with a \) or quoted to protect it from interpretation by the shell. The command is executed in the starting directory. If any invocation with the + form returns a non-zero value as exit status, then find returns a non-zero exit status. If find encounters an error, this can sometimes cause an immediate exit, so some pending commands may not be run at all. For this reason -exec my-command ... {} + -quit may not result in my-command actually being run. This variant of -exec always returns true.

Troels Arvin avatar
vn flag
Thanks to both you and Ljm Dullaart. I had seen the print0 trick before, but I thought it couldn't work when the number of files was larger than whatever limit there is for arguments to chmod. It seems find is able to generate the file list in chunks which do not overflow chmod's argument limit.
cn flag
`xargs` is doing the same. They are just alternatives. And only GNU find has that `-exec command {} +` option.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.