Changing file permissions recursively

Every now and then you’ve got a massive directory tree whose reading permissions need to be changed. For example, all files readable to all users, all directories accessible to all users, all executables executable by all users.

This is easy enough to do with find and chmod commands. Assuming that the root of the directory tree is somedir:

Set all directories to rwxr-xr-x (a.k.a. 755)
find somedir -type d -print -exec chmod 755 {} \;

Make all files readable
find somedir -type f -print -exec chmod go+r {} \;

Change all 744 files, executable by the owner, to 755, executable by all:
find somedir -type f -perm 744 -print -exec chmod 755 {} \;


Print directory tree disk usage on the command line

Suppose you have a large tree of directories containing lots of data (such as source code of a big project or numerical output of your simulations) and you need to estimate the total size of the whole tree. In graphical user interface this can be done by examining the directory properties. But as usual things can be done faster on the command line, where the suitable command is du (for Disk Usage).

An example (assuming that the root of your directory tree is called data)

du -h --max-depth=1 data
1.1G data/soln
3.0M data/binary
1.1G data

The -h option tells du to use human readable format, i.e. MB, GB etc instead of bytes. Option --max-depth=1 means that only the first subdirectories are listed. For more info on the options run man du.

    It is convenient to create an alias for shortening the long command such as
    alias disku='du -h --max-depth=1'
    For the alias to be present in all future sessions, add the line to your shell initialization file (for bash shell ~/.bashrc for example).

%d bloggers like this: