Tuesday, 22 January 2013

Linux Commands For Directory Handling

In Linux there are a few commands that work for both files and directories (such as the cp command),and some that only work for directories. To create a new directory, you’ll need to use a specific command, which I’ll discuss here. Removing directories can get interesting, so we’ll look at that as well as.
Creating/Making directories
There’s not much to creating a new directory in Linux, just use the mkdir command:
$ mkdir dir3
$ ls -il
total 16
1954886                  drwxr-xr-x             2              rich rich         4096        Sep 1 09:42           dir1/
1954889                 drwxr-xr-x              2             rich rich          4096        Sep 1 10:55            dir2/
1954893                 drwxr-xr-x              2              rich rich         4096        Sep 1 11:01            dir3/
1954888                  -rw-r--r--                1             rich rich        0              Dec 25 2008           test3
1954793                  -rw-r--r--                1              rich rich        6             Sep 1 09:51            test4
The system creates a new directory and assigns it a new inode number.
Deleting/Removing directories
Removing directories can be tricky, but there’s a reason for that. There are lots of opportunity        for bad things to happen when you start deleting directories. The bash shell tries to protect us from accidental catastrophes as much as possible. The basic command for removing a directory
is rmdir:
$ rmdir dir3
$ rmdir dir1
rmdir: dir1: Directory not empty
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.
Our friend the rm command can also help us out some when handling directories. If you try using it with not parameters, as with files, you’ll be somewhat disappointed:
$ rm dir1
rm: dir1: is a directory
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:
$ rm -r dir2
rm: descend into directory `dir2’? y
rm: remove `dir2/test1’? y
rm: remove `dir2/test3’? y
rm: remove `dir2/test4’? y
rm: remove directory `dir2’? y
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:
$ rm -rf dir2
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.

No comments:

Post a Comment