La soluzione standard raccomandata è semplice:
find . -type d -exec chmod 0755 "{}" \+
find . -type f -exec chmod 0644 "{}" \+
Questo aggiungerà il maggior numero possibile di nomi di file come argomenti a un singolo comando, fino alla lunghezza massima della riga di comando del sistema. Se la riga supera questa lunghezza, il comando verrà chiamato più volte.
Se vuoi chiamare il comando una volta per file, puoi invece fare:
find . -type d -exec chmod 0755 "{}" \;
find . -type f -exec chmod 0644 "{}" \;
chmod -R a=,u+rwX,go+rX $DIR sembra funzionare bene ed è molto probabile che sia il più veloce, comunque lo guardi.
(Ho controllato con strace , e ne fa solo uno fchmodat() syscall per file/directory -- per file con 644 e per directory con 755).
Il trucco è il X permesso, documentato in man chmod , che si comporta come x solo per le directory -- proprio la distinzione che volevi.
Cosa non documentato è la mia ipotesi che verrebbero applicati nella stessa sequenza in cui sono specificati, e non solo in un ordine casuale, ma test ripetuti con diverse varianti mi hanno convinto che funzionano davvero nell'ordine dato, quindi sono abbastanza sicuro funzionerà sempre così.
Dovrei menzionare che questo è su Linux, anche se una lettura superficiale della manpage BSD per chmod suggerisce che dovrebbe lavorare anche lì.
Ho confrontato la risposta di sitaram, il commento di Peter Cordes, la risposta di Fanatique e la risposta di harrymc, ma questa risposta ha il modo più veloce .
Medie:
- Risposta di Deltik* – 7.480 secondi
- risposta di sitaram:12,962 secondi (73,275% più lento del migliore)
- Commento di Peter Cordes – 14,414 secondi (92,685% più lento del migliore)
- Risposta di Fanatique:14,570 secondi (94,772% più lento del migliore)
- Risposta aggiornata di harrymc - 14,791 secondi (97,730% più lenta della migliore)
- Risposta originale di harrymc:1061,926 secondi (14096,113% più lento del migliore)
Riepilogo statistico completo:
Author N min q1 median q3 max mean stddev
------------------ -- ------- ------- ------- ------- ------- ------- --------
Deltik 10 7.121 7.3585 7.4615 7.558 8.005 7.4804 0.248965
sitaram 10 12.651 12.803 12.943 13.0685 13.586 12.9617 0.276589
Peter Cordes 10 14.096 14.2875 14.375 14.4495 15.101 14.4136 0.269732
Fanatique 10 14.219 14.512 14.5615 14.6525 14.892 14.5697 0.211788
harrymc (updated) 10 14.38 14.677 14.8595 14.9025 15.119 14.791 0.21817
harrymc (original) 1 1061.93 1061.93 1061.93 1061.93 1061.93 1061.93 N/A
Comando di Deltik, in formato benchmark:
find "$(pwd)" -type d | xargs -P4 chmod 755 & \ find "$(pwd)" -type f | xargs -P4 chmod 644 & wait
comando di sitaram, in formato benchmark:
chmod -R a=,u+rwX,go+rX "$(pwd)"
Comando di Peter Cordes, in formato benchmark:
find "$(pwd)" \( -type d -exec chmod 755 {} + \) \
-o \( -type f -exec chmod 644 {} + \)
Comando di Fanatique, in formato benchmark:
find "$(pwd)" -type d -print0 | xargs -0 chmod 755 ; \ find "$(pwd)" -type f -print0 | xargs -0 chmod 644
comando aggiornato di harrymc, in formato benchmark:
find "$(pwd)" -type d -exec chmod 755 {} + ; \
find "$(pwd)" -type f -exec chmod 644 {} +
comando originale di harrymc, in formato benchmark:
find "$(pwd)" -type d -exec chmod 755 {} \; ; \
find "$(pwd)" -type f -exec chmod 644 {} \;
Il mio comando è stato il più veloce grazie ai quattro chmod paralleli processi per tipo di file. Ciò ha consentito a più core della CPU di eseguire chmod , che sposta il collo di bottiglia verso i thread di I/O del kernel o il disco.
il comando di sitaram è stato il secondo classificato perché tutto viene eseguito all'interno dell'chmod comando. Ciò riduce sostanzialmente il sovraccarico rispetto alle altre risposte perché:
- I file devono essere scansionati solo una volta (simile a fare un
findinvece di due), e - Non è necessario creare alcun processo figlio.
Questo comando è il meno flessibile, tuttavia, perché si basa su un trucco che coinvolge il diverso significato del bit eseguibile tra file normali e directory.
Il commento di Peter Cordes, che utilizza un find comando, impedisce doppie ricerche di voci di directory. Più file ci sono, più sostanziale è questo miglioramento. Ha ancora il sovraccarico della creazione del figlio chmod processi, motivo per cui è un po' più lento del chmod -unica soluzione.
Tra il comando di Fanatique e il comando aggiornato di harrymc, find convogliato in xargs (find | xargs ) era più veloce perché il flusso di risultati viene elaborato in modo asincrono. Invece di find mettendo in pausa il suo comportamento di ricerca per -exec , i risultati trovati vengono inviati a xargs per l'elaborazione simultanea.
(Il delimitatore di byte nullo (find -print0 | xargs -0 ) non sembrava influenzare il tempo di esecuzione.)
Il comando originale di harrymc era troppo lento a causa dell'overhead di un nuovo chmod comando per ogni singolo file e cartella, ciascuno eseguito in sequenza.
Nella configurazione di test, c'erano 1000002 file regolari contenuti in 1001 directory:
example@unixlinux.online:~# echo {0..999} | xargs mkdir -p
example@unixlinux.online:~# find -type d -exec bash -c "cd {}; echo {0..999} | xargs touch" \;
example@unixlinux.online:~# find | wc -l
1001003
example@unixlinux.online:~# find -type d | wc -l
1001
example@unixlinux.online:~# find -type f | wc -l
1000002
Ho impostato tutti i file e le cartelle in modo che abbiano 777 autorizzazioni, come le condizioni iniziali della domanda.
Quindi, ho confrontato i comandi dieci volte, ripristinando ogni volta i permessi a 777 con chmod -R 0777 "$(pwd)" prima di eseguire il test.
Con OUTPUT rappresentando un file che contiene l'output di ciascun comando di benchmark, ho calcolato il tempo medio utilizzando:
bc <<< "scale=3; ($(grep real OUTPUT | grep -Po '(?<=m).*(?=s)' | xargs | sed 's/ /+/g'))/10"
Risultati del benchmark della risposta di Deltik
example@unixlinux.online:~# for i in {0..9} ; do chmod -R 0777 "$(pwd)" ; time { find "$(pwd)" -type d | xargs -P4 chmod 755 & find "$(pwd)" -type f | xargs -P4 chmod 644 & wait ; } ; done
[1] 9791
[2] 9793
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.634s
user 0m2.536s
sys 0m23.384s
[1] 9906
[2] 9908
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.443s
user 0m2.636s
sys 0m23.106s
[1] 10021
[2] 10023
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m8.005s
user 0m2.672s
sys 0m24.557s
[1] 10136
[2] 10138
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.480s
user 0m2.541s
sys 0m23.699s
[1] 10251
[2] 10253
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.397s
user 0m2.558s
sys 0m23.583s
[1] 10366
[2] 10368
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.482s
user 0m2.601s
sys 0m23.728s
[1] 10481
[2] 10483
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.679s
user 0m2.749s
sys 0m23.395s
[1] 10596
[2] 10598
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.243s
user 0m2.583s
sys 0m23.400s
[1] 10729
[2] 10731
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.320s
user 0m2.640s
sys 0m23.403s
[1] 10844
[2] 10847
[1]- Done find "$(pwd)" -type d | xargs -P4 chmod 755
[2]+ Done find "$(pwd)" -type f | xargs -P4 chmod 644
real 0m7.121s
user 0m2.490s
sys 0m22.943s
Tempo medio:7.480 secondi
Risultati del benchmark della risposta di sitaram
example@unixlinux.online:~# for i in {0..9} ; do chmod -R 0777 "$(pwd)" ; time chmod -R a=,u+rwX,go+rX "$(pwd)" ; done
real 0m12.860s
user 0m0.940s
sys 0m11.725s
real 0m13.059s
user 0m0.896s
sys 0m11.937s
real 0m12.819s
user 0m0.945s
sys 0m11.706s
real 0m13.078s
user 0m0.855s
sys 0m12.000s
real 0m12.653s
user 0m0.856s
sys 0m11.667s
real 0m12.787s
user 0m0.820s
sys 0m11.834s
real 0m12.651s
user 0m0.916s
sys 0m11.578s
real 0m13.098s
user 0m0.939s
sys 0m12.004s
real 0m13.586s
user 0m1.024s
sys 0m12.372s
real 0m13.026s
user 0m0.976s
sys 0m11.910s
Tempo medio:12,962 secondi
Risultati del benchmark del commento di Peter Cordes
example@unixlinux.online:~# for i in {0..9} ; do chmod -R 0777 "$(pwd)" ; time find "$(pwd)" \( -type d -exec chmod 755 {} + \) -o \( -type f -exec chmod 644 {} + \) ; done
real 0m14.096s
user 0m1.455s
sys 0m12.456s
real 0m14.492s
user 0m1.398s
sys 0m12.897s
real 0m14.309s
user 0m1.518s
sys 0m12.576s
real 0m14.451s
user 0m1.477s
sys 0m12.776s
real 0m15.101s
user 0m1.554s
sys 0m13.378s
real 0m14.223s
user 0m1.470s
sys 0m12.560s
real 0m14.266s
user 0m1.459s
sys 0m12.609s
real 0m14.357s
user 0m1.415s
sys 0m12.733s
real 0m14.393s
user 0m1.404s
sys 0m12.830s
real 0m14.448s
user 0m1.492s
sys 0m12.717s
Tempo medio:14,414 secondi
Risultati del benchmark della risposta di Fanatique
example@unixlinux.online:~# for i in {0..9} ; do chmod -R 0777 "$(pwd)" ; time { find "$(pwd)" -type d -print0 | xargs -0 chmod 755 ; find "$(pwd)" -type f -print0 | xargs -0 chmod 644 ; } ; done
real 0m14.561s
user 0m1.991s
sys 0m13.343s
real 0m14.521s
user 0m1.958s
sys 0m13.352s
real 0m14.696s
user 0m1.967s
sys 0m13.463s
real 0m14.562s
user 0m1.875s
sys 0m13.400s
real 0m14.609s
user 0m1.841s
sys 0m13.533s
real 0m14.892s
user 0m2.050s
sys 0m13.630s
real 0m14.291s
user 0m1.885s
sys 0m13.182s
real 0m14.843s
user 0m2.066s
sys 0m13.578s
real 0m14.219s
user 0m1.837s
sys 0m13.145s
real 0m14.503s
user 0m1.803s
sys 0m13.419s
Tempo medio:14.570 secondi
Risultati del benchmark della risposta aggiornata di harrymc
example@unixlinux.online:~# for i in {0..9} ; do chmod -R 0777 "$(pwd)" ; time { find "$(pwd)" -type d -exec chmod 755 {} + ; find "$(pwd)" -type f -exec chmod 644 {} + ; } ; done
real 0m14.975s
user 0m1.728s
sys 0m13.050s
real 0m14.710s
user 0m1.586s
sys 0m12.979s
real 0m14.644s
user 0m1.641s
sys 0m12.872s
real 0m14.927s
user 0m1.706s
sys 0m13.036s
real 0m14.867s
user 0m1.597s
sys 0m13.086s
real 0m15.119s
user 0m1.666s
sys 0m13.259s
real 0m14.878s
user 0m1.590s
sys 0m13.098s
real 0m14.852s
user 0m1.681s
sys 0m13.045s
real 0m14.380s
user 0m1.603s
sys 0m12.663s
real 0m14.558s
user 0m1.514s
sys 0m12.899s
Tempo medio:14,791 secondi
Risultati del benchmark della risposta originale di harrymc
A causa della lentezza di questo comando, ho eseguito il benchmark solo una volta.
example@unixlinux.online:~# for i in {0..0} ; do chmod -R 0777 "$(pwd)" ; time { find "$(pwd)" -type d -exec chmod 755 {} \; ; find "$(pwd)" -type f -exec chmod 644 {} \; ; } ; done
real 17m41.926s
user 12m26.896s
sys 4m58.332s
Tempo impiegato:1061,926 secondi