Page 3 of 7

Re: Calibrating permissions

Posted: Thu Sep 21, 2023 4:45 pm
by fehlix
Jakob77 wrote: Thu Sep 21, 2023 4:03 pm thomasl

Thank you. I need to start my test computer soon.



fehlix

Thank you.
Yes, prevention is a much better strategy than cure.
I hope to be able to do the prevention just by choosing the best way to format:
viewtopic.php?t=77258
If more is needed I suppose you send it out in an automatic update.?

The script in quest is meant for repair when prevention has failed.
No prevention or script needed if you follow my advice given for data files.
For executable files to backup, do backup with tools to keep permissions, e.g place them into tar,
or use a permission-keeping filesystem as backup file system.

Re: Calibrating permissions

Posted: Thu Sep 21, 2023 6:07 pm
by DukeComposed
Jakob77 wrote: Thu Sep 21, 2023 10:38 am As a peasant in scripting I would like a little feedback on this simple idea for a script to calibrate a folder for instance ~/Music and its sub folders (all files are just music files) :

1) Somehow establish a copy of the whole tree with folders but without files.
2) Then use chmod with 0755 on all the folders in the tree with no files
3) And use chmod with 0644 on the other tree with all the files.
4) Afterwards copy/combine in a way so the folders in the empty tree overwrites the folders in the tree with all the files.

Could that be a simple and okay way to go.?

I don't have the code for 1 or 4 yet.
This is entirely too much work for what you need to do. The find command knows the difference between files and directories, so there is no need to mess with two separate trees and try to merge them together. You've been given several examples of how to use find, with examples that can fix all files or all directories in one operation, or both with thomasl's script. You have all the instructions you need.

If you mess up and set all files to have the wrong permissions, which you admit they already have, you can run find again and fix it.

Re: Calibrating permissions

Posted: Fri Sep 22, 2023 4:07 am
by Jakob77
How about .thunderbird.?

In all this mess I do feel a little lucky that my strategy so far always has been not to restore more than I need.
So for instance .config and .mozilla are not infected.
But I think the .thunderbird permissions for files and folders has to be checked thoroughly.

That is if Firefox and Mozilla don't do it automatically on regular basis.
If they don't maybe it could be worth asking them to do it.?

I don't know how to do it, and I have used backup with random format for years.
It might only be me who has been under informed about backup format for years but if there are many more out there I think this is a frustration we ought to spare them from, if we can.






DukeComposed

Thank you very much for your feedback.
But I understand your way is to edit one file at a time and that is a huge work when it is a lot of files in different folders.

Re: Calibrating permissions

Posted: Fri Sep 22, 2023 5:01 am
by DukeComposed
Jakob77 wrote: Fri Sep 22, 2023 4:07 am But I understand your way is to edit one file at a time and that is a huge work when it is a lot of files in different folders.
thomasl has explicitly told you what his script does and how it does it. Emphasis added:
thomasl wrote: Thu Sep 21, 2023 8:29 am This is a script (called chmodr) I've used for a long time to change permissions for whole trees (as you've seen copying big directory trees from NTFS or FAT volumes can produce "wrong" permissions):

Code: Select all

#!/bin/bash
sudo find ./ -type d | while read DIR ; do sudo chmod $1 "${DIR}" ; done
sudo find ./ -type f | while read DIR ; do sudo chmod $2 "${DIR}" ; done
Start in the directory which you want to change, give the permissions for dirs and files and it'll do its job recursively:

Code: Select all

chmodr 755 644

Re: Calibrating permissions

Posted: Fri Sep 22, 2023 1:43 pm
by Jakob77
DukeComposed wrote: Fri Sep 22, 2023 5:01 am
Jakob77 wrote: Fri Sep 22, 2023 4:07 am But I understand your way is to edit one file at a time and that is a huge work when it is a lot of files in different folders.
thomasl has explicitly told you what his script does and how it does it. Emphasis added:
thomasl wrote: Thu Sep 21, 2023 8:29 am This is a script (called chmodr) I've used for a long time to change permissions for whole trees (as you've seen copying big directory trees from NTFS or FAT volumes can produce "wrong" permissions):

Code: Select all

#!/bin/bash
sudo find ./ -type d | while read DIR ; do sudo chmod $1 "${DIR}" ; done
sudo find ./ -type f | while read DIR ; do sudo chmod $2 "${DIR}" ; done
Start in the directory which you want to change, give the permissions for dirs and files and it'll do its job recursively:

Code: Select all

chmodr 755 644
Yes, thank you again. That is one and not a several, but it looks really good, and even better since you confirm the code.
But I do hope you agree I have to know what the right file settings are supposed to be, before I for instance just edit the whole .thunderbird tree.

Re: Calibrating permissions

Posted: Fri Sep 22, 2023 9:56 pm
by Jakob77
thomasl wrote: Thu Sep 21, 2023 8:29 am This is a script (called chmodr) I've used for a long time to change permissions for whole trees (as you've seen copying big directory trees from NTFS or FAT volumes can produce "wrong" permissions):

Code: Select all

#!/bin/bash
sudo find ./ -type d | while read DIR ; do sudo chmod $1 "${DIR}" ; done
sudo find ./ -type f | while read DIR ; do sudo chmod $2 "${DIR}" ; done
Start in the directory which you want to change, give the permissions for dirs and files and it'll do its job recursively:

Code: Select all

chmodr 755 644
Nowadays I'm doing two things WRT this. First I mount all NTFS or FAT volumes with the "right" permissions to start with. Secondly, I replaced the find command with a utility called fd (https://github.com/sharkdp/fd for details) which is faster, easier to use and at least for certain tasks more powerful. YMMV.

Thank you very much. :thumbup:
I have been testing it a little now, and it looks to me it is some real magnificent code I can be proud to copy.

I want to make a script that solves it in specific folders if they exist.
For instance with a sequence like this for '~/Music' :

Code: Select all

  
            if [ -d "$HOME/Music" ] ; then
find ~/Music -type d | while read DIR ; do chmod 755 "${DIR}" ; done

fi


            if [ -d "$HOME/Music" ] ; then

find ~/Music -type f | while read DIR ; do chmod 644 "${DIR}" ; done

fi

I wonder if there perhaps is something I can write instead of "~/Music" so it will work in all languages.?

Re: Calibrating permissions

Posted: Fri Sep 22, 2023 10:12 pm
by DukeComposed
Jakob77 wrote: Fri Sep 22, 2023 9:56 pm I want to make a script that solves it in specific folders if they exist.

I wonder if there perhaps is something I can write instead of "~/Music" so it will work in all languages.?
The only thing I'd point out about thomasl's script is that find does not need to pipe its output to a while loop. This works just the same:

Code: Select all

find . -type d -exec chmod 0755 {} \;
That said, where his script says "sudo chmod $1" and "sudo chmod $2", the $1 and $2 refer to command-line arguments, the first and the second, respectively.

So if you edit it to something like this:

Code: Select all

#/bin/sh
find $1 -type d -exec chmod $2 {} \;
find $1 -type f -exec chmod $3 {} \;
The script can be run like "my_chmod.sh ~/Music 0755 0644" and will search the first argument, ~/Music, and replace the permissions on the directories with the second argument, 0755, and then replace the permissions on the files with the third argument, 0644. This prevents you from needing to hardcode any paths or permissions into the script.

Re: Calibrating permissions

Posted: Sat Sep 23, 2023 6:40 am
by Jakob77
Thank you, I am glad it does the same so I for now can stay with the version I already have begun to test.







About this:
I wonder if there perhaps is something I can write instead of "~/Music" so it will work in all languages.?

Maybe not but if anyone knows a simple code for it please let me see it.

I think it can make a big difference.

In English it is "~/Music" in Danish it is "~/Musik" and so on..
That means if I don't find an international code, then the script will only be compatible with one language, or it will be huge.

Re: Calibrating permissions

Posted: Sat Sep 23, 2023 8:46 am
by thomasl
DukeComposed wrote: Fri Sep 22, 2023 10:12 pmThe only thing I'd point out about thomasl's script is that find does not need to pipe its output to a while loop. This works just the same:

Code: Select all

find . -type d -exec chmod 0755 {} \;
Indeed. After switching from Windows to MX (and stumbling head first into the wonderful world of Linux file permissions :eek: ) this was among the very first bash scripts I created... after yet another tree copied from an NTFS volume, with all the perms scrambled :mad: . At some point I did what @fehlix has suggested further up and am now mounting those volumes as 755/644.

These days I have mostly abandoned such short scripts; instead I rely much more on zsh functions and a nice selection of powerful utilities.

Re: Calibrating permissions

Posted: Sat Sep 23, 2023 3:31 pm
by Jakob77
Okay, I intend to calibrate the permissions in my home folder by building more to this script that I have named 'permicali':

Code: Select all

#!/bin/bash
# This script is being developed to calibrate permissions for some user files and folders in Linux MX 21.3
# Link to general script template: https://forum.mxlinux.org/viewtopic.php?p=725990#p725990
# Link to code and discussion: https://forum.mxlinux.org/viewtopic.php?p=745301#p745301

# Hovedsekvens begynd

case $1 in



# Sekvens begynd
    'Music'|'music'|'Musik'|'musik')

    
            if [ -d "$HOME/Musik" ] ; then
find ~/Musik -type d | while read DIR ; do chmod 755 "${DIR}" ; done

fi

            if [ -d "$HOME/Musik" ] ; then

find ~/Musik -type f | while read DIR ; do chmod 644 "${DIR}" ; done

fi

        ;;
# Sekvens slut
# Sekvens begynd
    'Billeder'|'billeder'|'Pictures'|'pictures')

    
            if [ -d "$HOME/Billeder" ] ; then
find ~/Billeder -type d | while read DIR ; do chmod 755 "${DIR}" ; done

fi

            if [ -d "$HOME/Billeder" ] ; then

find ~/Billeder -type f | while read DIR ; do chmod 644 "${DIR}" ; done

fi

        ;;
# Sekvens slut
# Sekvens begynd
    'Videoklip'|'videoklip'|'Videoclip'|'videoclip')

    
            if [ -d "$HOME/Videoklip" ] ; then
find ~/Billeder -type d | while read DIR ; do chmod 755 "${DIR}" ; done

fi

            if [ -d "$HOME/Videoklip" ] ; then

find ~/Billeder -type f | while read DIR ; do chmod 644 "${DIR}" ; done

fi

        ;;
# Sekvens slut
# Sekvens begynd
    'all')


        echo ''
        echo 'Option "all" is not ready yet'
        echo ''

        ;;
# Sekvens slut
# Sekvens begynd
    '-h'|'--h'|'help'|'-help'|'--help'|'/h')
        echo 'WARNING: Repair script under development, not tested'
        echo "Use: '${0##*/}' [Argument] - Calibrate permissions for files and folders."
        echo ''
        echo ''
        echo ''
        echo 'Arguments:'
        echo '  none                Shows way to help'
        echo '  help                Shows this help'
        echo '  musik               Calibrate sub folders and fils in ~/Musik'
        echo '  billeder            Calibrate sub folders and fils in ~/Billeder'
        echo '  videoklip           Calibrate sub folders and fils in ~/Videoklip'
        echo ''
        echo ''
        echo '  all                 Calibrate all user folders mentioned above'
        echo ''
        echo 'Examples of use:'
        echo '"'${0##*/}' musik"    Calibrate sub folders and fils in ~/Music'
        echo '"'${0##*/}' all"      Calibrate permissions in ~/Musik, ~/Videoklip and ~/Billeder'
        ;;
# Sekvens slut
# Herunder kommandoen, som benyttes, når intet argument forinden er blevet genkendt.
    *)
            echo ''
            echo 'Help syntax: "'${0##*/}' -h"'
            echo ''
        ;;
# Herover kommandoen, som benyttes, når intet argument forinden er blevet genkendt.

esac
# Hovedsekvens slut.




As you can see the folder names in the script are hard coded in Danish so it will need some edit before it can be used for another language. I might make an English version also if there is a demand for it but I still don't know if it ends up being worth sharing.



My plan is now, guided by the task, to go from folder to folder and make a specific script sequence for each one.

I don't know how far I will get, that also depends on the demand and the skills others wants to put in it.

However, I think I at least have to fix these user folders if I can:


~/Music

~/Pictures

~/Videoclip

~/bin

~/.thunderbird

~/.restore

~/.icons

~/Documents

~/Desktop



Do you know any files in them folders that are not supposed to be chmod 0644 ?
Or folders that are not supposed to be chmod 0755 ?


I know some files in ~/bin but I believe they are easy to calibrate.
Maybe launchers in Desktop are also not that difficult to handle.
I don't know about ~/.icons
~/.thunderbird and its files are important but I am almost blank.


Where can I find information about the correct state for permissions.?