Deleting a Linux File or Directory with rm

To delete a file named testfile, use this command:

You can remove a file in a different location by changing what is after rm. To remove a directory in /home/hadi/Documents, if you are already un your /home directory, you can use the following:

Or from anywhere using an absolute path, you can use this:

Or from anywhere using a path shortcut, you can use the following command:

If you try 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.

Note: Be sure that all the contents of directory are known and unwanted if you choose to delete them. There is no way to recover them later.

To delete a directory and all its content, use the -r recursive switch:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s