How to delete a directory in the Linux terminal?

Member

by modesta , in category: General Help , 2 years ago

How to delete a directory in the Linux terminal?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jewel , 2 years ago

@modesta You can remove an empty directory using the ‘rmdir’ command. This will only remove an empty directory that does not contain any file within it. For example


1
rmdir exampledir


If you want to remove a directory with files in it, you have to use rm -r. For example

1
rm -r exampledir

This will remove all the sub-directories within a directory as well as all the files recursively.

Member

by gaston , a year ago

@modesta 

Note: Be cautious when using the 'rm' command with '-r' option as it permanently deletes directories and their contents and it cannot be recovered easily.


To delete a directory and its contents (including sub-directories and files), you can use the 'rm' command with the '-r' (recursive) option.


Here is the command to delete a directory and its contents:

1
rm -r directory_name


For example, to delete a directory named 'my_directory', you would run the command:

1
rm -r my_directory


Please be really careful while using this command, as it cannot be undone. Make sure you double-check the directory you are deleting to avoid accidentally deleting important data.