Deleting Files and Directories

But how do you get rid of files? Again, this is relatively easy, but first a word of caution: the shell doesn't operate any kind of Recycle Bin. Once a file is deleted, it's gone forever. (There are utilities you can use to recover files, but these are specialized tools and aren't to be relied on for day-to-day use.)

Removing a file is achieved by typing something like this:

rm myfile

It's as simple as that.

In some instances,you'll be asked to confirm the deletion after you issue the command. If you want to delete a file without being asked to confirm it, type the following:

rm -f myfile

The f stands for force (that is, force the deletion).

If you try to use the rm command to remove a directory, you'll see an error message. This is because the command needs an additional option:

rm -rf mydirectory

As noted earlier, the r stands for recursive and indicates that any folder specified afterward should be deleted, in addition to any files it contains.

■Tip You might have used wildcards within Windows and DOS. They can be used within Ubuntu, too. For example, the asterisk (*) can be used to mean any file. So, you can type rm -f * to delete all files within a directory, or type rm -f myfile* to delete all files that start with the word myfile. But remember to be careful with the rm command. Keep in mind that you cannot salvage files easily if you accidentally delete them!


If, at the command prompt, you try to copy, move or otherwise manipulate files that have spaces in their names, you'll run into problems. For example, suppose you want to move the file picture from germany.jpg to the directory mydirectory. In theory the following command should do the trick:

mv picture from germany.jpg mydirectory/

But when we tried it on our test Ubuntu setup, we got the following errors:

mv: cannot stat 'picture': No such file or directory mv: cannot stat 'from': No such file or directory mv: cannot stat 'germany.jpg': No such file or directory

In other words, BASH had interpreted each word as a separate file and tried to move each of them! The error messages tell us that BASH cannot find the file picture, from, or germany.jpg.

There are two solutions. The easiest is to enclose the filename in quotation marks ("), so the previous command would read as follows:

mv "picture from germany.jpg" mydirectory/

The other solution is to precede each space with a backslash. This tells BASH you're including a literal character in the filename. In other words, you're telling BASH not to interpret the space in the way it normally does, which is as a separator between filenames or commands. Here's how the command looks if you use backslashes:

mv picture\ from\ germany.jpg mydirectory/

The backslash can also be used to stop BASH from interpreting other symbols in the way it normally does. For example, the less than and greater than symbols (<>) have a specific meaning in BASH, which we discuss in Chapter 17, but they're allowed in filenames. So to copy the file <bach>.mp3 to the directory mydirectory, you could type:

cp \<bach\>.mp3 mydirectory/

Generally speaking, however, simply enclosing filenames in quotation marks is the easiest approach.

Was this article helpful?

0 0
Computer Hard Drive Data Recovery

Computer Hard Drive Data Recovery

Learn How To Recover Your Hard Drive Data After A Computer Failure.

Get My Free Ebook

Post a comment