chmod tips and tricks

I have utilised the commands chmod and chgrp for many years.  Time and time again I’ve needed to make fileA’s permissions match fileB’s permissions, until recently I have always performed something similar to:

# ls -l file*
-rw-r–r–   1 fred      users  70    Jan   1  09:01  fileA
-rw—-rw-  1 wilma  users  132  Jan   1  09:02  fileB

chmod 0644 fileB

Unless you are a sandal wearing, bearded IT technician you’re probably not lucky enough to understand the four digit octal number passed into the chmod command e.g. 0644.  Octal is not rocket science, but, working out what octal number represents any given permission string can be tedious. All is not lost, there are things you can do to simplify the process.

Take for example the stat command:

-rw-r–r–   1 fred      users  70    Jan   1  09:01  fileA
# stat fileA
File: `fileA’
Size: 321             Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 983075      Links: 1
Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-01-05 13:15:15.000000000 +0000
Modify: 2010-01-05 13:15:15.000000000 +0000
Change: 2010-01-05 13:21:26.000000000 +0000

# ls -l file*

The stat command returns several attributes of the file in question – including the octal equivalent!

If the information returned by the stat command is a little intimidating, a more concise version can be ascertained e.g.

# stat –format=”%a/%A” fileA
644/-rw-r–r–

The other trick I use for copying the permission from fileA to fileB, is to utilise the –reference parameter of the chmod command e.g.

# ls -l file*
-rw-r–r– 1 root root   321 Jan  5 13:15 fileA
-r-x-w–wx 1 root root 41652 Jan  5 13:15 fileB
# chmod –reference fileA fileB
# ls -l file*
-rw-r–r– 1 root root   321 Jan  5 13:15 fileA
-rw-r–r– 1 root root 41652 Jan  5 13:15 fileB

As you can see the –reference parameter means you do not need the octal string – simple.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s