• We had to restore from a backup today after a failed software update. Backup was from 0000 EDT and restored it at 0800 EDT so we lost about 8hrs. Today is 07/20/2024. More info here.

Linux+: Linux Shell 10 – Shell Commands Directories

J

Jarret W. Buse

Guest
Linux+: Linux Shell 10 – Shell Commands Directories

By now you can handle files from the shell, but now we will cover directory management. We will cover the commands used to make and remove directories.

The two directory commands are:
  1. mkdir (make directory)
  2. rmdir (remove directory)
First, let's look at making a directory. The syntax is:

mkdir [options] [directories]

There are six options for making a directory and are as follows:

  1. -m (--mode=mode) – sets the permissions on the directory
  2. -p (--parents) – makes parent folders as required
  3. -v (--verbose) – displays a message for each created directory
  4. -Z (--context=CTX) – sets SELinux security of each created folder to CTX
  5. --help – displays help information for mkdir
  6. --version – displays version number of mkdir

To use the command without any options would create a folder with the same permissions as the parent folder. Multiple directories can be made in the current folder by separating each new folder name with a space. For example, to create folders named temp1, temp2 and temp3 in the current directory, use the following command:

mkdir temp1 temp2 temp3

To create a directory in another folder, you can switch to the directory or specify the full path. For example, to create a folder in the directory at '/usr/bin/' named 'other' the following command would be used:

mkdir /usr/bin/other

Multiple directories can be created with different paths for each folder.

The mode option lets you specify the permissions of the folder when it is created. For the system, each permission is in groups. The groups are OWNER, GROUP and OTHERS. The numbering system is based on three permission types: Read, Write and Execute. The numbering is as follows:

Read – 4 (r)
Write – 2 (w)
Execute – 1 (x)

To make this work, you add up the values for what you want as follows:

4+2+1 = 7 – Read, Write, Execute
4+2 = 6 – Read, Write
4+1 = 5 – Read, Execute
4+0 = 4 – Read
2+1=3 – Write, Execute
2+0=2 – Write
1+0=1 – Execute

For example, to create a folder in the current directory called 'help' and allow the User Read, Write and Execute permissions (7), and the Group permissions of Read and Write (6), and Others only read permissions (4), the total Permission Mode is 764. If files were to be searched for in the Home folder with these permissions, we would use the following command:

mkdir --mode=764 help

The -p option allows for any nonexistant folders to be created. Be careful if you attempt to specify the full path and mistype one of the folder names. If the name is mistyped and does not exist, with the -p option, the folders will then be created. For example, we place a folder in the home directory with the structure of '/home/temp/help/others'. If the temp folder does not exist, then the temp folder is created then the help folder and finally the others directory.

The -v or verbose generates a comment for each time a folder is created or failed. When a directory is made with the verbose option, the following comment is generated when a folder named help is created:

mkdir: created directory ‘help’

When the -Z option is used, it creates a directory with the SELinux security set.

For help information for the mkdir command, use the --help option.

To find the version of mkdir, use the --version option.

To remove directories, use the rmdir command. The syntax is as follows:

rmdir [options] [directories]

The five options are as follows:

  1. --ignore-fail-on-non-empty – ignores all errors that are due to the folder not being empty
  2. -p (--parents) – remove explicit parent directories
  3. -v (--verbose) – generate a message for each folder removed
  4. --help – shows help information for the rmdir command
  5. --version – displays version number for the rmdir command

Similar to the mkdir command, the rmdir command can allow for multiple folder names to be used. When multiple directory names are used, each folder is removed. For example, to remove the folders temp1, temp2 and temp2 from the current directory, the following command is used:

rmdir temp1 temp2 temp3

The option --ignore-fail-on-non-empty is used when a folder has files within it. If the rmdir command is used on a directory with files in it, you get the following error with the folder named help:

rmdir: failed to remove ‘help’: Directory not empty

To bypass this error, you use the option as follows:

rmdir --ignore-fail-on-non-empty help

The folder is not removed, but an error is not generated.

When you need to remove a chain of subdirectories, you can use the -p option. For example, if a directory structure exists such as /help/temp1/temp2 and you wish to remove all three folders. The command would be as follows:

rmdir -p /help/temp1/temp2

This would remove the folder temp2 then temp1 and finally help. If any folder is not empty, then the specific folder would not be removed as well as any parent folder.

The -v option provides a message for each folder removed. For example, removing the temp1 folder would produce the following message:

rmdir: removing directory, ‘temp1’

The --help option shows the help information for the rmdir command.

The --version option shows the version information for the rmdir command.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 205,537


I don't see the neccessary to use rmdir -p while rm -r could do it better.
 


Top