Directory Handling

In Linux there are a few commands that work for both files and directories and some that only work for directories. This section discusses the commands that can only work with directories.

Creating Directories

There’s not much to creating a new directory in Linux; just use the mkdir command:

An output shows the usage of the m k d i r and l s hyphen i l commands.
Description

An output shows the usage of the m k d i r and l s hyphen i l commands.
Description

The system creates a new directory and assigns it a new inode number.

Deleting Directories

Removing directories can be tricky, but there’s a reason for that. There are lots of opportunities for bad things to happen when you start deleting directories. Bash tries to protect us from accidental catastrophes as much as possible. The basic command for removing a directory is rmdir:

Line 1: dollar space r m d i r space d i r 3. Line 2: dollar space r m d i r space d i r 1. Line 3: r m d i r colon d i r 1 colon Directory not empty. Line 4: dollar.

By default, the rmdir command only works for removing empty directories. Since there is a file in the dir1 directory, the rmdir command refuses to remove it. You can remove nonempty directories using the --ignore-fail-on-non-empty parameter.

You can also use the rm command when handling directories.

If you try using it without parameters, as with files, you’ll be somewhat disappointed:

Line 1: dollar space r m space d i r 1. Line 2: r m colon space d i r 1 colon space is a directory. Line 3: dollar.

However, if you really want to remove a directory, you can use the -r parameter to recursively remove the files in the directory, then the directory itself:

An output shows the removal of files in the directory, then the directory itself, using the r m command with hyphen r parameter.
Description

While this works, it’s somewhat awkward. Notice that you still must verify every file that gets removed. For a directory with lots of files and subdirectories, this can become tedious.

The ultimate solution for throwing caution to the wind and removing an entire directory, contents and all, is the rm command with both the -r and -f parameters:

Line 1: dollar space r m space hyphen r f space d i r 2. Line 2: dollar.

That’s it. No warnings, no fanfare, just another shell prompt. This, of course, is an extremely dangerous tool to have, especially if you’re logged in as the root user account. Use it sparingly and only after triple checking to make sure that you’re doing exactly what you want to do.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.187