Save file/folder structure including permissions

It has always been a great concern to keep track of folder permissions since most of the lame errors in Linux are due to a permission denial by the system, and sysadmins should change some permissions from time to time to fix things. At this point having a good record of what has been changed is vital since in terminal there is no undo or Ctrl+z and you should keep track of what you are doing to reverse the change and correct the mistakes.

In this post I briefly want to present some lines of code that can help you keep track.

Remember that you can always write a while or for loop to get things done but in terminal most command have recursive option which lets you recursively go in each folder and do what you want. In this post I’m using only those recursive options. but let me know if you wanted to also have the while loop version.

The following command has three parts, first part only go through each and every subfolder and get the file names. The second part is it self part of the find command and it’s job is to extract information from the files found by find command. The third part which comes at the end starts with a > (greater than sign) followed by a file name and basically it means that store everything the previous part is passing into a text file (remember to change theĀ /target/folder/path/ with your folder path) :

find /target/folder/path/ -printf '%m\t%u\t%g\t%p\n' > filelist.txt

You can View the output file by:

less filelist.txt

And it will look like the following:

755 mehrad NixSOS 3.1/bumphunter
644 mehrad NixSOS 3.1/bumphunter/NEWS.Rd
644 mehrad NixSOS 3.1/bumphunter/DESCRIPTION
644 mehrad NixSOS 3.1/bumphunter/NAMESPACE
755 mehrad NixSOS 3.1/bumphunter/help
644 mehrad NixSOS 3.1/bumphunter/help/AnIndex
644 mehrad NixSOS 3.1/bumphunter/help/aliases.rds
644 mehrad NixSOS 3.1/bumphunter/help/paths.rds
644 mehrad NixSOS 3.1/bumphunter/help/bumphunter.rdx
644 mehrad NixSOS 3.1/bumphunter/help/bumphunter.rdb
755 mehrad NixSOS 3.1/bumphunter/Meta
644 mehrad NixSOS 3.1/bumphunter/Meta/data.rds
644 mehrad NixSOS 3.1/bumphunter/Meta/links.rds

(you can quit the less environment by pressing q)

As you may guess, the %m returns the permission of the file/folder, %u returns user, %g returns group and %p returns the path for each item.
There is more you can do to this command which makes it more useful. You can modify the quoted text in front of -printf and create a shell script:

find /target/folder/path/ -printf "chown %u:%g %p\nchmod %m %p\n" >

This will give you the following shell file

chown mehrad:NixSOS 3.1/bumphunter
chmod 755 3.1/bumphunter
chown mehrad:NixSOS 3.1/bumphunter/NEWS.Rd
chmod 644 3.1/bumphunter/NEWS.Rd
chown mehrad:NixSOS 3.1/bumphunter/DESCRIPTION
chmod 644 3.1/bumphunter/DESCRIPTION
chown mehrad:NixSOS 3.1/bumphunter/NAMESPACE
chmod 644 3.1/bumphunter/NAMESPACE
chown mehrad:NixSOS 3.1/bumphunter/help
chmod 755 3.1/bumphunter/help
chown mehrad:NixSOS 3.1/bumphunter/help/AnIndex
chmod 644 3.1/bumphunter/help/AnIndex
chown mehrad:NixSOS 3.1/bumphunter/help/aliases.rds
chmod 644 3.1/bumphunter/help/aliases.rds
chown mehrad:NixSOS 3.1/bumphunter/help/paths.rds
chmod 644 3.1/bumphunter/help/paths.rds
chown mehrad:NixSOS 3.1/bumphunter/help/bumphunter.rdx
chmod 644 3.1/bumphunter/help/bumphunter.rdx
chown mehrad:NixSOS 3.1/bumphunter/help/bumphunter.rdb
chmod 644 3.1/bumphunter/help/bumphunter.rdb
chown mehrad:NixSOS 3.1/bumphunter/Meta
chmod 755 3.1/bumphunter/Meta
chown mehrad:NixSOS 3.1/bumphunter/Meta/data.rds
chmod 644 3.1/bumphunter/Meta/data.rds
chown mehrad:NixSOS 3.1/bumphunter/Meta/links.rds

And here the only part left for your undo script is to make it executable and run it:

chmod +x

Leave a Reply

Your email address will not be published. Required fields are marked *

This is new type of CAPTCHA, make the puzzle please !WordPress CAPTCHA