How to Remove Directories and Files Using Linux Command Line

How to Remove Directories (Folders)

You can remove or delete directories or folder  with the rmdir and rm in in linux,.

Here are examples to help you delete either file or folder. Enjoy.

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

rm -d dirname
rmdir dirname

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

rm -r dirname

  • f a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion.
  • To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:
$ rm -rf dirname

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

$ rm -r dirname1 dirname2 dirname3

Same as with files, you can also use a wildcard (*) and regular expansions to match multiple directories

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

$ unlink filename
$ rm filename

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

$s rm filename1 filename2 filename3

You can also use a wildcard (*) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:

&s rm *.pdf

Leave a Comment

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