0

I copied some legacy files/folders from /srv/my-old-disk/_oldstuff/ to /srv/my-normal-disk/_oldstuff.

How do I get this analysis for /srv/my-old-disk/_oldstuff/ ?

How do I then change certain file types (file, dir, link) with a certain unwanted permission (e.g. 400) set to a desired permission set (e.g. 644) in a batch?

1 Answer 1

0

find & sort + uniq -c & perl RegEx

File permissions batch change

0) Create the necessary analysis file(s)

  • A) Get listing with find and certain -printf flags

  • B) Get stats with sort + uniq -c

  • C) Get listing with files/dirs NOT having "normal" permissions with

    $ cd /srv/dev-disk-by-uuid-f0233da3-473e-450d-9a18-b2ae254fc439/_get/_move
    
    $ find . -printf "%m %y %p\n" > ~/filelist-1a-perms-type-path-findraw.txt
    
    $ cat ~/list-perms-type-path-findraw.txt | sort > ~/filelist-1b-perms-type-path-sorted.txt
    
    $ find . -printf "%m %y\n" > ~/filelist-1c-perms-type-findraw.txt
    
    $ find . ~/filelist-1c-perms-type-findraw.txt | sort | uniq -c | sort > ~/filelist-1d-perms-type-extraordinary.txt
    
    $ perl -ne 'print if /^(?!755 |644 )/' ~/filelist-1b-perms-type-path-sorted.txt ~/filelist-1f-perms-type-path-extraordinary.txt
    
  • Then you get these following files with the following exemplary content.


~/filelist-1a-perms-type-path-findraw.txt

  • The raw output by find.

  • Note that find does not traverse in lexicographical order, but by logic of the filesystem (comment appreciated, if anyone can explain exactly how).

    755 d .
    644 f ./Installers-Notes.txt
    644 f ./Installers-Bootdisk.txt
    755 d ./Software
    755 d ./Software/Multiplatform
    644 f ./Software/Multiplatform/.DS_Store
    644 f ./Software/Multiplatform/somesfiles.tgz
    755 d ./Software/Multiplatform/Some Extensions
    644 f ./Software/Multiplatform/Some Extensions/.DS_Store
    644 f ./Software/Multiplatform/Some Extensions/some_plugin.xpi
    ...
    

~/filelist-1b-perms-type-path-sorted.txt

  • Sorted from leftmost to rightmost column

  • First by permissions, then by type (file, dir, link, etc), then by filepath.

  • You could also create a different sorting order if needed.

    444 f ./Software/Mac/Software1.zip
    444 f ./Software/Mac/Software5.cdr
    444 f ./Software/Mac/Software7.zip
    ...
    
    640 f ./Software/Mac/Software2.dmg
    640 f ./Software/Mac/Software3/Software3.dmg
    640 f ./Software/Mac/Software4.zip
    640 f ./Software/Mac/Software6.dmg
    640 f ./Software/Mac/Software7.zip
    ...
    

~/filelist-1c-perms-type-findraw.txt

  • 1c is just an auxillary file for the generation of 1d.

  • You could also skip it by piping right away.

  • But for long listings which take long, I prefer to have the intermediary files, should I do a mistake somewhere, I do not need to generate from scratch again.

  • You can later through this away ofc.

    755 d
    644 f
    644 f
    755 d
    755 d
    644 f
    644 f
    755 d
    644 f
    644 f
    644 f
    

~/filelist-1d-perms-type-extraordinary.txt

  • This file gives you some statistics.

  • Based on this you decided which uncommon files/folders you want listed in your 1f "extraordinary" analysis file, and shape the RegEx accordingly.

          2 555 f
          6 700 d
          7 645 f
         13 600 f
         20 444 f
         22 700 f
         57 640 f
        124 744 f
        423 777 l
      12315 755 d
      35606 755 f
      60759 644 f
    

~/filelist-1e-perms-type-extraordinary-annotated.txt

  • This is just a dupe of 1d plus remarks of your research/thoughts.

    # Files:
          2 555 f
          7 645 f
         13 600 f
         20 444 f # Your remark here why this perms good/bad
         22 700 f
         57 640 f
        124 744 f
      35606 755 f
      60759 644 f
    
    # Directories:
          6 700 d
      12315 755 d
    
    # Links:
        423 777 l
    

~/filelist-1f-perms-type-path-extraordinary.txt

  • This file helps you to analyze and later maybe take action, like batch change the permissions.
  • You got this file with the line starting with perl -ne 'print if /^(?!755 |644 )/'
  • Explanation of the regular expression:
    • Line start ^

    • Is not followed by (?!expression) this is the so called negative lookahead

    • And the expression is 755 | (logical or) 644

    • Note the space after the numbers is not necessary as their are no 4-digit permission expressions such as "7550" which it could mismatch. Nevertheless I feel extra certain with this.

      444 f ./Software/Mac/SoftwareA/File1.bom
      444 f ./Software/Mac/SoftwareA/Fil2.pax.gz
      444 f ./Software/Mac/SoftwareA/File3/Contents/PkgInfo
      444 f ./Software/Mac/SoftwareA/File3/Contents/File1.sizes
      444 f ./Software/Mac/SoftwareA/File3/Contents/File2.plist
      444 f ./Software/Mac/SoftwareA/File3/Contents/File3.info
      ...
      
      
      555 f ./Software/Mac/CategoryA/SoftwareB/README.txt
      555 f ./Software/Mac/_Audio Production Software/Propellerheads Reason 5/Source.nfo
      555 f ./Software/Mac/CategoryA/SoftwareC/Source.nfo
      ...
      
  • This filelist should help you which directories/files maybe need adaptation.
  • You could also then use find to selectively find file entries of a certain type (file/dir/link) and with certain permission(s), and then change those to a desired other permission.

Find files/folders with a certain permission set and change them to another permission set

Let's say you want to change all files with permissions 444 to 644.

1) Let's print all files of a certain undesired permission set

$ find . -type f -perm 444 -printf "%m %y %p\n"
  • -type f to only find files
  • -perm 444 which have permission 444
  • -printf to get proper output for controlling what we are doing.
    • Note that printf should be after all filter criteria
    • Otherwise if will print what it encountered so far, but not what will remain after applying the next chain of filter(s).
444 f ./Software/Mac/SoftwareA/File1.bom
444 f ./Software/Mac/SoftwareA/Fil2.pax.gz
444 f ./Software/Mac/SoftwareA/File3/Contents/PkgInfo
444 f ./Software/Mac/SoftwareA/File3/Contents/File1.sizes
444 f ./Software/Mac/SoftwareA/File3/Contents/File2.plist
444 f ./Software/Mac/SoftwareA/File3/Contents/File3.info
...

2) Now let's change them in batch

$ find . -type f -perm 444 -printf "%m %y %p\n" -exec chmod 644 {} \;
  • We added -exec chmod 644 {} \; at the end
  • On each file that makes it through the filter criteria execute chmod 644
  • You should get exactly the same listing as in the previous step (if the file contants have not changed since then).
  • If you encounter errors you would get them additionally.

3) Finally check that it was performed well

$ find . -type f -perm 444 -printf "%m %y %p\n"
  • Same command line as in step 3
  • This time it should return NULL (no file listing) because there should be no more files with 444 below the path where we applied find (which was . our then current working directory)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.