Linux ext4 restore file and directory access rights after bad backup/restore

ext4filesystemslinux

I kind of screwed up the backup of my personal directory with rsync (maybe because I'm backuping on an NTFS filesystem): all files are here but all files and directories access rights are 777. I was wondering if there was a magic utility that would recursively change:

  • the directories from 777 to 755.
  • the regular files from 777 to 644. I don't have many executables in my home, so I can manage that by hand later.
  • leave other files (links, any thing else ?) unchanged.

Doing it in shell is easy, but it will take hours…

Subsidiary question: any advice to backup properly a linux directory hierarchy on NTFS (with rsyncor other).

Best Answer

The standard recommended solution is straight-forward:

find . -type d -exec chmod 0755 "{}" \+
find . -type f -exec chmod 0644 "{}" \+

This will append as many filenames as possible as arguments to a single command, up to the system's maximum command line length. If the line exceeds this length, the command will be called multiple times.

If you want to call the command once per file, you can instead do:

find . -type d -exec chmod 0755 "{}" \;
find . -type f -exec chmod 0644 "{}" \;
Related Question