How to Remove Directory in Linux by command and GUI

How to Remove Directory in Linux by command and GUI

Are you new user of Linux OR You are trying to delete a directory in Linux, and getting error “Directory is not empty”

Don’t worry anymore. This article will help learn “How to remove directory in Linux step by step by using a command line interface and graphical interface both.

Content

How to remove Directory in Linux

Reading this article is showing your interest to learn new thing, After completing this section you will learn how to remove empty directory in Linux by using commands.

rmdir command is used to remove empty directory. Synatx is very simple as follow

$rmdir /dirctory_path

If the directory is not empty, rmdir command will not remove it. you will get the error “Directory is not empty”. In the next section, you will learn how to remove the directory with its content.

vijay@Ubuntu-19:~/Documents$rmdir no_content/
vijay@Ubuntu-19:~/Documents$rmdir old_data/
rmdir: failed to remove 'old_data/': Directory not empty
vijay@Ubuntu-19:~/Documents$

rm is another command to use remove file and directory, but you must tell to rm you are removing a directory. Syntax is:

$rm -d /Directory_path

You don’t need to give the path, if you are removing directory from current directory.

See the Examples:

vijay@Ubuntu-19:~/Documents$rm new_content/
rm: cannot remove 'new_content/': Is a directory
vijay@Ubuntu-19:~/Documents$rm -d new_content/
vijay@Ubuntu-19:~/Documents$rm -d old_data/
rm: cannot remove 'old_data/': Directory not empty
vijay@Ubuntu-19:~/Documents$

How to remove Directory with its content

By now you can easily remove an empty directory. I have directory name old_data is not empty. You have seen in the last section.

Use -r option with rm command.

$rm or /Directory_Path

vijay@Ubuntu-19:~/Documents$rm -r old_data/
vijay@Ubuntu-19:~/Documents$ls
vijay@Ubuntu-19:~/Documents$

You should not go through mistakes

Using -r option is not enough to remove directory with its content. I will remove everything for given directory without your confirmation. If you run this command by mistake you will repent only.

Use -ir option with rm command, it will better for you. If you are working as system administrator and have much responsibility.

vijay@Ubuntu-19:~/Documents$rm -ir old_data/
rm: descend into directory 'old_data/'? y
rm: remove regular empty file 'old_data/file5.doc'? y
rm: remove regular empty file 'old_data/file1.doc'? y
rm: remove regular empty file 'old_data/file4.doc'? 

How to remove multiple directories at the same time

If you want to remove multiple directories at the same time then you can use rm command respectively followed by -r/-ir option as per you requirements.

Syntax is simple:

$rm -ir First_Directory Second_directory Third_Directory

vijay@Ubuntu-19:~/Documents$rm -ir dir1 dir2 
rm: remove directory 'dir1'? y
rm: remove directory 'dir2'? y
vijay@Ubuntu-19:~/Documents$

If you want to remove multiple directories have different location. You should not worry anymore.

Go through following example.

vijay@Ubuntu-19:~/Documents$rm -ir /home/vijay/Downloads/dir_2/ /home/vijay/Videos/dir_1/
rm: remove directory '/home/vijay/Downloads/dir_2/'? y
rm: remove directory '/home/vijay/Videos/dir_1/'? y
vijay@Ubuntu-19:~/Documents$

How to remove directory by using GUI

Most computer users love graphical interface. It is easy to learn and easy to use.

Delete option is not available in Linux, Here is another option to delete directory called move to trash.

17-how-to-delete-directory-in-linux

Conclusion

By now you are expert to remove the directory and have good knowledge of using rm and rmdir commands.

The important thing, you should remove the directory safely by using the command line.

If you have any question, please write in comment box. For any specific discussion please Join our Group HERE

If Appreciate My Work, You should consider:

Leave a Comment