How to Remove Files and Directories in Linux

How to Remove Files and Directories Using Linux Command Line

In this tutorial, I’m going to show you how to browse around the terminal and remove files and directories.

Notice: If you want to delete a file you can use the command RM. Be aware of this and please notice that there is no recycle bin in terminal so that file is gone for good. But also you can see How to Recover Permanently Deleted Files In Linux

Now I’ll show you how to use the rm and rmdir commands to remove files and directories in Linux.

How to Remove Files

To remove (or delete) a file in Linux from the command line, use the rm (remove) command.

The unlink command allows you to remove only a single file, while with rm you can remove multiple files at once.

  • To delete a single file, use the rm or unlink command followed by the file name:

  • To delete multiple files at once, use the rm command followed by the file names separated by space.

  • Use the rm with the -i option to confirm each file before deleting it:

  • To remove files without prompting even if the files are write-protected pass the -f (force) option to the rm command:

  • You can also combine rm options. For example, to remove all .php files in the current directory without a prompt in verbose mode, use the following command:

How to Remove Directories

In Linux, you can remove (delete) directories with the rmdir and rm.

rmdir is a command-line utility for deleting empty directories while with rm you can remove directories and their contents recursively.

  • To remove an empty directory, use either rmdir or rm -d followed by the directory name:

  • To remove non-empty directories and all the files within them, use the rm command with the-r (recursive) option:

  • To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

  • To remove multiple directories at once, use the rm -r command followed by the directory names separated by space.

Thatas it! You should already have a good insight of using rm, rmdir and unlink commands in Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *