Sign Up

Sign In

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Please make a payment to be able to ask a question.

Pay to ask a question

Please make a payment to be able to add post.

Pay to add a post

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

How to Use the Command Line to Delete Files and Directories in Linux

Delete files or directories is a fundamental operation in Linux. Although it may seem straightforward, there are different ways to delete files or directories, each with their own benefits.

In this tutorial, we will show you how to delete files or directories in Linux along with the commands you can use.

How to Delete a File in Linux

The file is not immediately removed from the storage device, but its space is marked as available for reuse after it has been deleted from the file system.

Here are some of the most common methods for deleting a file in Linux:

Using the GUI file manager

With most Linux distributions, you can delete files using a graphical interface. Simply navigate to the file you wish to delete, right-click it, and select “Delete” or “Move to Trash.”

Using the rm command

The rm (remove) command can also be used to delete files and directories in Linux. To delete a file, type the following command in the terminal:

rm filename

If the file is write-protected or you do not have sufficient permissions to delete it, you will be prompted to confirm the deletion.

Using the shred command

By overwriting the file’s contents multiple times before deleting it, the shred command makes it difficult to recover deleted files.

To use the shred command, type the following command in the terminal:

shred -u filename

The -u option tells shred to delete the file after overwriting it. Replace filename with the name of the file you wish to delete.

Using the trash-cli command

By moving files to the trash instead of immediately deleting them, the trash-cli command provides a safer way to delete files. After installing the trash-cli command, you can use it as follows:

sudo apt-get install trash-cli

The following command can be used to delete a file after installation:

trash filename

In Linux, how to delete a directory

Linux supports the rmdir and rm commands. The rmdir command removes an empty directory, while the rm command removes the entire directory.

Using the rm command

Using the rm command, you can delete a directory in Linux:

Open the terminal: A directory can be deleted using the command line in Linux. Open the terminal by pressing “Ctrl+Alt+T” on your keyboard or by searching for “terminal” in your system’s application launcher.

Navigate to the directory you want to delete: The cd command can be used to navigate to a directory you wish to delete, such as my_directory, located in your home folder.

Check the contents of the directory: Check the directory’s contents before deleting it. For example, type ls and press “Enter” to see the files and folders inside the my_directory folder before deleting it.

Delete the directory and its contents: You can delete the directory and all its contents by using the rm command with the -r option. Type rm -r my_directory and press “Enter”. You will be prompted to confirm the deletion.

Verify that the directory has been deleted: If you are in your home folder and the my_directory folder was located there, type ls ~/ and press “Enter”. The my_directory folder should no longer be listed.

Using the rm -r command can irreversibly delete files and directories.

The rmdir command can be used to remove directories

Using the rmdir command, you can delete a directory in Linux:

Open the terminal: The terminal can be accessed by pressing “Ctrl+Alt+T” on your keyboard or by searching for “terminal” in the application launcher.

Navigate to the directory you want to delete: If the directory is not empty, you will receive an error message and the directory will not be deleted. Use the rmdir command followed by the directory name. Type rmdir my_directory and press “Enter”.

Delete the directory: For example, if the my_directory folder was located in your home folder, use the ls command to list the contents of the parent directory. If the my_directory folder is no longer listed, the removal of the directory was successful.

Conclusion

By following this step-by-step guide, you will be able to effectively delete files or directories in Linux by using the rm command, rmdir command, or rm command with the -r or -R options.

Related Posts

Leave a comment