< Previous | Contents | Next >
Removing Files And Directories
As we covered earlier, the rm command is used to delete files and directories. We are go- ing to use it to clean up our playground a little bit. First, let's delete one of our hard links:
[me@linuxbox playground]$ rm fun-hard
[me@linuxbox playground]$ ls -l
total 12
drwxrwxr-x 2 me me 4096 2016-01-15 15:17 dir1
lrwxrwxrwx 1 me me 4 2016-01-16 14:45 dir1-sym -> dir1
[me@linuxbox playground]$ rm fun-hard
[me@linuxbox playground]$ ls -l
total 12
drwxrwxr-x 2 me me 4096 2016-01-15 15:17 dir1
lrwxrwxrwx 1 me me 4 2016-01-16 14:45 dir1-sym -> dir1
drwxrwxr-x | 2 | me | me | 4096 | 2016-01-15 | 15:17 | dir2 |
-rw-r--r-- | 3 | me | me | 1650 | 2016-01-10 | 16:33 | fun |
lrwxrwxrwx | 1 | me | me | 3 | 2016-01-15 | 15:15 | fun-sym -> fun |
That worked as expected. The file fun-hard is gone and the link count shown for fun is reduced from four to three, as indicated in the second field of the directory listing. Next, we'll delete the file fun, and just for enjoyment, we'll include the “-i” option to show what that does:
[me@linuxbox playground]$ rm -i fun
rm: remove regular file `fun'?
[me@linuxbox playground]$ rm -i fun
rm: remove regular file `fun'?
Enter “y” at the prompt and the file is deleted. But let's look at the output of ls now. No- ticed what happened to fun-sym? Since it's a symbolic link pointing to a now-nonexis- tent file, the link is broken:
[me@linuxbox playground]$ ls -l
total 8
drwxrwxr-x | 2 | me | me | 4096 | 2016-01-15 | 15:17 | dir1 |
lrwxrwxrwx | 1 | me | me | 4 | 2016-01-16 | 14:45 | dir1-sym -> dir1 |
drwxrwxr-x | 2 | me | me | 4096 | 2016-01-15 | 15:17 | dir2 |
lrwxrwxrwx | 1 | me | me | 3 | 2016-01-15 | 15:15 | fun-sym -> fun |
Most Linux distributions configure ls to display broken links. On a Fedora box, broken links are displayed in blinking red text! The presence of a broken link is not in and of it- self dangerous, but it is rather messy. If we try to use a broken link we will see this:
[me@linuxbox playground]$ less fun-sym
fun-sym: No such file or directory
[me@linuxbox playground]$ less fun-sym
fun-sym: No such file or directory
Let's clean up a little. We'll delete the symbolic links:
[me@linuxbox playground]$ rm fun-sym dir1-sym
[me@linuxbox playground]$ ls -l
total 8
drwxrwxr-x 2 me me 4096 2016-01-15 15:17 dir1
drwxrwxr-x 2 me me 4096 2016-01-15 15:17 dir2
[me@linuxbox playground]$ rm fun-sym dir1-sym
[me@linuxbox playground]$ ls -l
total 8
drwxrwxr-x 2 me me 4096 2016-01-15 15:17 dir1
drwxrwxr-x 2 me me 4096 2016-01-15 15:17 dir2
One thing to remember about symbolic links is that most file operations are carried out on the link's target, not the link itself. rm is an exception. When you delete a link, it is the link that is deleted, not the target.
Finally, we will remove our playground. To do this, we will return to our home directory and use rm with the recursive option (-r) to delete playground and all of its contents, in- cluding its subdirectories:
[me@linuxbox playground]$ cd
[me@linuxbox ~]$ rm -r playground
[me@linuxbox playground]$ cd
[me@linuxbox ~]$ rm -r playground
Creating Symlinks With The GUI
The file managers in both GNOME and KDE provide an easy and automatic method of creating symbolic links. With GNOME, holding the Ctrl+Shift keys while dragging a file will create a link rather than copying (or moving) the file. In KDE, a small menu appears whenever a file is dropped, offering a choice of copy- ing, moving, or linking the file.