Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

Click here to view code image
matthew@seymour:~$ rm ~/randomdirectory/filename


If you try to use rm to remove an empty directory, you receive an error
message: rm: cannot remove `random/’: Is a directory. In
this case, you must use rmdir. However, you can remove a directory and its
contents by using rm.


CAUTION
Be sure that all the contents of a directory are known and unwanted if you
choose to delete them. There is no way to recover them later. Also, be
careful to ensure that you don’t have extra spaces, mistype the name of the
directory, or use sudo to delete something that you shouldn’t be deleting.
Linux gives you great power, and it lets you use that power without
questioning you about it; that’s the human’s job.

To delete a directory and all its contents, use the -r recursive switch (which
works with many commands, not only rm):


Click here to view code image
matthew@seymour:~$ rm -r /home/matthew/randomdirectory/


Everything in randomdirectory as well as the directory itself will be
deleted, including other subdirectories, without considering whether they are
empty or have contents.


Moving or Renaming a File with mv


In Linux land, moving and renaming a file are the same thing. It doesn’t
matter whether you are moving the directory to another or from one filename
to another filename in the same directory; there is only one command to
remember. To move a file named filename from ~/documents to
~/archive, use this command:


Click here to view code image
matthew@seymour:~$ mv documents/filename archive


Notice that the filename is not included in the destination. The destination
here must be an existing directory. If it is not, the file is renamed to the term
used. Some examples will make this clear.


Assuming that you are the user matthew, to rename a file that is in your
current directory, you could use the following:


Click here to view code image

Free download pdf