R
R
ruboss2015-07-29 21:46:10
linux
ruboss, 2015-07-29 21:46:10

Chown and chmod for a lot of files?

Hello everyone, there is Apache on the server and a bunch of folders with root rights ~ 1,200,000 folders and 1 file in each. Everything is on Amazon aws, linux ubuntu. Since the disk itself is 22GB, the number of stored files per unit of memory has been increased. The file system is ext4, inode_ratio = 4096 , which makes it possible to store ~ ​​6,000,000 inodes per 22GB.
Task: all 2,400,000 files need to be given chown www-data.www-data and chmod 0755
I do: sudo chown www-data.www-data -R dir/
as a result, 15 minutes pass and I can only set the owner for 600,000 files , but Kopal does not go further
in the internet, I found some xargs -p and find . I can not link them, tell me who knows how to installchown only for files that are currently root and also chmod 0755 for files that are not
. Something like this ( don't use this example, it's not valid )
find -user root dir/ | xargs -p chown www-data.www-data -R dir/
find -permissions -rw-r--r-- dir/ | xargs -p chmod 0755 -R dir/

UPDATE:
important detail - all folders are numbered from 1 to 1,200,000 and have the following structure 1/file.txt
ANSWER
Thanks for the help Aves and fomistoklus !!!
For this task, I could not find a solution from the already implemented tools. They may work, but I couldn't verify it. A script was written to solve the problem:

#!/bin/bash
echo "run it"

filename=/var/www/html/counter.txt
current=0
max=0
lim=0
cnt=1
totalcount=1
while read -r string
do
        for word in $string; do

                 if [ "$cnt" -eq "1" ]
                         then  current=$word
                 elif [ "$cnt" -eq "2" ]
                        then  max=$word
                 else
                          lim=$word
                 fi
                cnt=$((cnt+1))

        done

done < "$filename"


echo "$current"
echo "$max"
echo "$lim"


 while [ "$current" -le "$max" ]
 do
                if [ -d /var/www/html/questions/"$current"/ ]; then
                         cd /var/www/html/questions/"$current"/
                         find . ! -perm 0755 -exec chmod 0755 {} \;
                         find . ! -user www-data -exec chown www-data.www-data {} \;
                         totalcount=$((totalcount+1))
                fi

                echo "loop"
                current=$((current+1))


                if [ "$totalcount" -gt "$lim" ]
                        then break
                fi

 done

 echo  "$current $max $lim"
 echo  "$current $max $lim" > /var/www/html/counter.txt

 cd /var/www/html/

where counter.txt is a file with the current folder, the maximum number of files and the limit in this form
1 1150022 60
Do not forget to set the script chown +x script.sh
Next, set cron for every minute:
*/1 * * * *  /var/www/html/script.sh >/dev/null 2>/dev/null

And voila: 364 1151860 60- after 5 minutes
The speed and limit depends on your server, disk, in my case 1GB of RAM and 6,000,000 innodes almost, which significantly reduces the speed.
In about 2000 minutes (~14 days) privileges and owners will be set for all ~2,400,000 files.
How to speed up?
run the script with different counters.txt:
1 180000 60
180000 360000 60
360000 540000 60
540000 720000 60
720000 100000 60
100000 120000 60
6 times faster, which in theory gives us the result in about 2 days

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aves, 2015-07-29
@ruboss

man find says

find dir ! -perm 0755 -exec chmod 0755 {} \;
find dir ! -user root -exec chown root {} \;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question