Working with directories on Linux

Working with directories on Linux

$ grep umask /etc/bashrc
    # Set default umask for non-login shell only if it is set to 0
    [ `umask` -eq 0 ] && umask 022

One of the more unusual things about umasks is that they are “masks”. That is, they determine what privileges are denied. A zero in the first digit of a umask means more rights will be assigned to the owner than are assigned to the group (2) and everyone else (2). With a umask of 0027, for example, the owner will have read and write access (second 0) to a file while other group members will have only read access (2) and everyone else will have no access at all (7 means no access). Think of a mask as specifying the rights that are blocked rather than the rights which are bestowed.

$ umask
$ touch newfile
$ ls -l newfile
-rw-r-----. 1 shs shs 0 Apr  2 12:04 junkfile

Creating a directory with the same umask setting will work the same except that write permission will also be provided to the owner and group.

$ mkdir newdir
$ ls -ld newdir
drwxr-x---. 1 shs shs 0 Apr  2 12:05 junkdir

Here’s a 2-minute video on how to use the umask command that might be helpful.

Using the mkdir command

The mkdir (make directory) command allows you to create a directory or even a directory “tree” (directories within directories) with a single command. As mentioned earlier, one of the most common directories that Linux users create is called “bin” (as in “binary”) since it’s the location where scripts and programs are normally stored. To create a bin directory, you would use the command “mkdir bin” when logged in and sitting in your home directory.

If you want to set up a directory structure with a single command, you can use a command like one of those below. The first creates a three-level directory structure. The second creates a directory that contains five other directories (e.g., team/3).

$ mkdir -p level1,level2,level3
$ mkdir -p teams/{1,2,3,4,5}

Using the chmod command

The mkdir command, by itself, won’t always do everything you need. You may also need to use the chmod command to alter file and directory permissions as needed. If you have a directory that no one else should have access to, for example, you can remove anyone else’s access with a command like this:


Related Articles

Leave a Reply

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