Linux 101: How to delete files and folders from the CLI

In another entry of his Linux 101 series, Jack Wallen shows you how to delete files and folders from Linux using the Command Line Interface (CLI).

How to delete files and folders in Linux from the CLI

When you're using a GUI, deleting a file and/or a folder is as simple as right-clicking and selecting delete. But when you're logged into a GUI-less server, you won't have that option. What do you do? 

As you probably expected, there's a command for that. In fact, there's a single command you can use for both instances. How you use that command varies, depending on what you're deleting. 

SEE: 5 Linux server distributions you should be using (TechRepublic Premium)

Let me explain. 

The command in question is rm, which stands for remove. The command is really simple to use. For example, you have a file named TEST and you want to delete it. For that, you'd issue the command rm TEST. Easy right? 

You can even use the command to delete multiple files, such as rm TEST TEST1 TEST2 TEST3

Or you can remove all files that begin with TEST with rm TEST*

You might have files with spaces in their names, at which point you'd need to wrap the name in single quotes, such as rm 'MY TEST FILE'

But what if that file is actually a directory? If you have a directory, named PROJECT, that contains files you don't need anymore, you can't just delete it with the rm PROJECT command, as you'll be informed that PROJECT is directory. 

SEE: Rust: What developers need to know about this programming language (free PDF) (TechRepublic)

To delete that directory, and its contents, you'd need to do a recursive/force delete, by adding the -rf options to the command, as in rm -rf PROJECT

One thing to keep in mind is that the -rf option doesn't care what it's deleting, so use it with caution. A better option might be to go the interactive route with rm -ri PROJECT, which will have you verify every file deletion before the command takes action. And that's all there is to deleting files and folders from the Linux command line.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see


Image: Jack Wallen