Americas

  • United States
sandra_henrystocker
Unix Dweeb

Working with directories on Linux

How-To
Apr 04, 20245 mins
Linux

Directories provide a hierarchical way to organize your files, and you can categorize them to suit your needs.

file folder tabs
Credit: Shutterstock

Directories on Linux systems provide an easy and very convenient way to organize your files. In fact, depending on how you like to work, you can set up as many layers of directories within directories (usually referred to as “subdirectories”) to organize and categorize your files as finely as needed.

Directories are, of course, digital “folders” that provide a hierarchical way to organize your files and are extremely useful. Your home directory is the most obvious, but you can use the cd command to move around your system’s file system to your heart’s content. If you have the proper permissions, you can move into any directory you want to explore. Unless you’re using the root account, however, this won’t be the case. Some directories can only be used by root.

Examining directories

To see the details (permissions and such) for a directory itself, you need to use a command like “ls -ld bin”. Otherwise, you will see the contents of the directory rather than the directory details.

$ ls -ld bin
drwxr-x---. 1 shs shs 18 Jan  3 13:44 bin

The first character in the second line above identifies the file as a directory and is followed by the permissions granted to the owner (rwx), the group (r-x) and everyone else (—). Using a command like “ls bin” shows the files it contains. The read, write, execute and no rights option should be very clear.

Using directory contents

There are a number of ways to view the contents of a directory – even without having to cd into it. To list the files in a directory by name, use a command like “ls bin”. To see file details, add a -l (ls -l bin). To include files that begin with a dot, add the -a option (e.g, ls -al bin). To list the contents of one of your folders while in some place in the file system outside of your home directory, use a command like “ls ~/bin” (The ~ represents your home directory). To list the contents of the directory above the one you’re in, use the “ls ..” command.

Checking your umask settings

The umask setting determines the default permissions that are assigned to files when you create them. By default, these settings will be set up in the /etc/bashrc or the /etc/profile file. In the example below, umask will be set up by the /etc/bashrc file if its current value is zero.

$ 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
0027
$ 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:

$ chmod 700 personaldir

Keep in mind that, by default, group access to home directories is only granted to the individual who owns the account. User groups generally have only that one user. Unless other users have been added to that user’s group, you can pretty much ignore those settings.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.