In this case, the mv operation only needs to rename the directory. However, because of the way that Linux filesystems are created, moving directories across filesystems that live on different hard disk partitions requires that each file and subdirectory first be copied and then deleted by the mv command, as in the following example:
$ mv -v include_test /tmp/backup 'include_test' -> '/tmp/backup' 'include_test/libxml2' -> '/tmp/backup/libxml2'
'include_test/libxml2/xmlops.h' -> '/tmp/backup/libxml2/xmlops.h' 'include_test/netdev' -> '/tmp/backup/netdev'
'include_test/netdev/devname.h' -> '/tmp/backup/netdev/devname.h' 'include_test/system' -> '/tmp/backup/system' 'include_test/system/libxml2' -> '/tmp/backup/system/libxml2' 'include_test/system/libxml2/xmlops.h' -> \
'/tmp/backup/system/libxml2/xmlops.h' 'include_test/system/netdev' -> '/tmp/backup/system/netdev' 'include_test/system/netdev/devname.h' -> \
'/tmp/backup/system/netdev/devname.h' 'include_test/new_file' -> '/tmp/backup/new_file' removed 'include_test/libxml2/xmlops.h' removed directory: 'include_test/libxml2' removed 'include_test/netdev/devname.h' removed directory: 'include_test/netdev' removed 'include_test/system/libxml2/xmlops.h' removed directory: 'include_test/system/libxml2' removed 'include_test/system/netdev/devname.h' removed directory: 'include_test/system/netdev' removed directory: 'include_test/system' removed 'include_test/new_file' removed directory: 'include_test'
As you can see, much of this output is very similar to that of the cp command. In this example, I first used the df command to demonstrate that my current directory (".") and the target directory (/tmp) are actually located on different filesystems. You can see the difference in the verbose output of the mv command.
Along with copying and moving files and directories, deleting files and directories is a similarly common operation. Whether you're deleting things to free up disk space for future projects, deleting backup copies of projects that you've finished, or deleting things that you don't want anyone else to see, it's quite easy to delete files from the command line. The key to doing so is the rm command, which continues the "the less typing, the better" philosophy and stands for remove.
To remove a single file on a Linux system, you simply type the rm command followed by the name of the file. For example, to delete the file file.txt, you would do the following:
The file is gone. As with the cp command, the rm command provides a -r (recursive) option to enable you to remove entire directories. For example, to permanently remove a directory named test, you would issue the following command:
; - p You can remove empty directories using the rmdi r command, but I don't find that to be pi^^'^bJUSOTljSiaS a common scenario. I rarely have empty directories just sitting around because I usually create directories to store something in them. i generally use the rm -r command to delete any directory because that way i don't have to manually delete its contents first. Your mileage may vary.
If you've moved to Ubuntu or Kubuntu from a Microsoft Windows system, you may be painfully aware that it is easy to recover files that you've deleted on a Windows system because it initially just erases the directory entry that identifies the file or folder that you're deleting. This is not the case on a Linux system. When you delete a file or directory on a Linux system, all of the disk storage associated with the file, directory, and the contents of that directory are returned to a general list of free space that is available on your system. Although deleted files and directories can still be recovered on a Linux system, it is much harder to do so and requires the assistance of someone who really knows the details of the filesystem.
The downside of this is that you can't easily recover any files that you've deleted by mistake unless you do so using a graphical utility such as a file manager. To help protect yourself from accidentally deleting files, see the section later in this chapter on "Defining and Using Aliases" for information about how to permanently set up a safe version of the rm command that will ask you for confirmation before it actually removes anything.
: .-. P .-'rj '-•" -,'Sg All of the cp, mv, and rm commands have many more options than I've covered here. To see |MKBSsfcBiSiH8SMH» all of the options available for any of these commands, use the man or info commands to see the online reference information for the command that you're interested in.
Was this article helpful?