-->

Managing Directories On Linux

Linux has a few commands that work for both files and directories (such as the cp command), and some that work only for directories. To create a new directory, you need to use a specific command, which is covered in this section. Removing directories can get interesting, so that is covered in this section as well.

Managing Directories On Linux
Managing Directories On Linux

Creating directories

Creating a new directory in Linux is easy — just use the mkdir command:

  1. $ mkdir New_Dir
  2. $ ls -ld New_Dir
  3. drwxrwxr-x 2 christine christine 4096 May 22 09:48 New_Dir
  4. $

The system creates a new directory named New_Dir. Notice in the new directory’s long listing that the directory’s record begins with a d. This indicates that New_Dir is not a file, but a directory.
You can create directories and subdirectories in “bulk” if needed. However, if you attempt this with just the mkdir command, you get the following error message:

  1. $ mkdir New_Dir/Sub_Dir/Under_Dir
  2. mkdir: cannot create directory ‘New_Dir/Sub_Dir/Under_Dir’:
  3. No such file or directory
  4. $

To create several directories and subdirectories at the same time, you need to add the -pparameter:

  1. $ mkdir -p New_Dir/Sub_Dir/Under_Dir
  2. $$
  3.     ls -R New_Dir
  4.     New_Dir:
  5.     Sub_Dir
  6.     New_Dir/Sub_Dir:
  7.     Under_Dir
  8.     New_Dir/Sub_Dir/Under_Dir:
  9. $

The -p option on the mkdir command makes any missing parent directories as needed. A parent directory is a directory that contains other directories at the next level down the directory tree.

Of course, after you make something, you need to know how to delete it. This is especially useful if you created a directory in the wrong location.

Deleting directories

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

  1. $ touch New_Dir/my_file
  2. $ ls -li New_Dir/
  3. total 0
  4. 294561 -rw-rw-r— 1 christine christine 0 May 22 09:52 my_file
  5. $$
  6. rmdir New_Dir
  7. rmdir: failed to remove ‘New_Dir’: Directory not empty
  8. $

By default, the rmdir command works only for removing empty directories. Because we created a file, my_file, in the New_Dir directory, the rmdir command refuses to remove it. To fix this, we must remove the file first. Then we can use the rmdir command on the now empty directory:

  1. $ rm -i New_Dir/my_file
  2. rm: remove regular empty file ‘New_Dir/my_file’? y
  3. $$
  4. rmdir New_Dir
  5. $$

ls -ld New_Dir
ls: cannot access New_Dir: No such file or directory

The rmdir has no -i option to ask if you want to remove the directory. This is one reason it is helpful that rmdir removes only empty directories.

You can also use the rm command on entire non-empty directories. Using the -r option allows the command to descend into the directory, remove the files, and then remove the directory itself:

  1. $ ls -l My_Dir
  2. total 0
  3. -rw-rw-r— 1 christine christine 0 May 22 10:02 another_file
  4. $$
  5. rm -ri My_Dir
  6. rm: descend into directory ‘My_Dir’? y
  7. rm: remove regular empty file ‘My_Dir/another_file’? y
  8. rm: remove directory ‘My_Dir’? y
  9. $$
  10. ls -l My_Dir
    ls: cannot access My_Dir: No such file or directory
  11. $


This also works for descending into multiple subdirectories and is especially useful when you have lots of directories and files to delete:

  1. $ ls -FR Small_Dir
  2. Small_Dir:
  3. a_file b_file c_file Teeny_Dir/ Tiny_Dir/
  4. Small_Dir/Teeny_Dir:
  5. e_file
  6. Small_Dir/Tiny_Dir:
  7. d_file
  8. $$
  9. rm -ir Small_Dir
  10. rm: descend into directory ‘Small_Dir’? y
  11. rm: remove regular empty file ‘Small_Dir/a_file’? y
  12. rm: descend into directory ‘Small_Dir/Tiny_Dir’? y
  13. rm: remove regular empty file ‘Small_Dir/Tiny_Dir/d_file’? y
  14. rm: remove directory ‘Small_Dir/Tiny_Dir’? y
  15. rm: descend into directory ‘Small_Dir/Teeny_Dir’? y
  16. rm: remove regular empty file ‘Small_Dir/Teeny_Dir/e_file’? y
  17. rm: remove directory ‘Small_Dir/Teeny_Dir’? y
  18. rm: remove regular empty file ‘Small_Dir/c_file’? y
  19. rm: remove regular empty file ‘Small_Dir/b_file’? y
  20. rm: remove directory ‘Small_Dir’? y
  21. $$
  22. ls -FR Small_Dir
  23. ls: cannot access Small_Dir: No such file or directory
  24. $

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

Note
For the rm command, the -r parameter and the -R parameter work exactly the same.
When used with the rm command, the -R parameter also recursively traverses through
the directory removing files. It is unusual for a shell command to have different cased
parameters with the same function.

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:

  1. $ tree Small_Dir
  2. Small_Dir
  3. ├ a_file
  4. ├ b_file
  5. ├ c_file
  6. ├ Teeny_Dir
  7. | └e_file
  8. └Tiny_Dir
  9. └d_file
  10. 2 directories, 5 files
  11. $$
  12. rm -rf Small_Dir
  13. $$
  14. tree Small_Dir
  15. Small_Dir [error opening dir]
  16. 0 directories, 0 files
  17. $

The rm -rf command gives no warnings and no fanfare. This, of course, is an extremely dangerous tool to have, especially if have superuser privileges. Use it sparingly, and only after triple checking to make sure that you’re doing exactly what you want to do!

Note
Notice in the preceding example that we used the tree utility. It nicely displays directories, subdirectories, and their files. It’s a useful utility when you need to understand a directory structure, especially before removing it. This utility may not be installed by default in your Linux distribution. See last posted article for learning about installing software In the last few sections, you looked at managing both files and directories. So far we covered everything you need to know about files, except for how to peek inside of them.

0 Response to "Managing Directories On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel