How To Remove Directories and Files in Linux

Last Updated: 

LINUX

How To Remove Directories and Files in Linux

Learn how to remove directories and files using terminal in Ubuntu, Debian and other Linux distros with this step by step guide.

Linux is a family of open-source Unix-like operating systems based on the Linux Kernel. Everything is a file in Linux even including the directories. There are multiple ways of removing a directory in Linux. You can use file managers like Nautilus, Dolphin, Xfe file manager, etc. You can then delete files and directories using the manager’s graphical user interface.

Another way of removing directories and files is via the command-line interface. Removing directories using the graphical interface is easy and needs no mention, but if you want to know how to remove directories or files using a command line, then you have come to the right place.

Here we will explain how to delete directories in Linux using the rmdir, rm commands.

Removing directories or files from the command line permanently deletes them i.e directories and files are not moved to the trash bin and cannot be fully recovered.

Commands to remove directories and files in Linux

There are two commands that you can use to delete a directory in Linux:

1. rmdir

rmdir command remove directory command removes directories only if they are empty.

Syntax:

rmdir [OPTION] *DIRECTORY*

Some common options are:

- -help prints information about the command along with the syntax and various options that can be used with the command.

- -ignore-fail-on-non-empty ignore each failure that is solely because a directory is non-empty.

-p, - -parents remove DIRECTORY and its ancestors i.e removes the specified DIRECTORY and also its parent DIRECTORY if any.

-v, - -verbose if you want to see a detailed output log for every processed directory then you can pass this option as an argument to the command.

rmdir removes the directory only if the directory is empty. If you try to remove an empty directory you will get rmdir: failed to remove directory: No such file or directory error. To remove the empty directory use rm command.

Usage:

  • Delete a directory and its parent directory

rmdir_1

rmdir -p dir/dir1

This command will first delete the dir1 which is inside of dir and then afterward delete the parent directory i.e dir

  • To remove a directory named dir1

rmdir_2

rmdir dir1
  • Remove multiple directories

rmdir_3

rmdir dir1 dir2 dir3

First dir1 will be removed then dir2 and after that dir3.

2. rm command

rm short for remove is used to remove directories, files, and symbolic links. By default, it does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.

Syntax:

rm [OPTION] FILENAME

Some common options are:

-f,- -force ignore nonexistent files and arguments, never prompt.

-i prompt before every removal.

-I prompt once before removing more than three files, or when removing recursively.

-r, -R, - -recursive remove directories and their contents recursively.

-d, - -dir remove empty directories

-v, - -verbose shows logs about the current operation being performed by the command

- -help prints information about the command along with the syntax and various options that can be used with the command.

Usage:

  • To delete a directory named dir1.

rm_1

rm dir1
  • To forcefully remove a write-protected directory or file without getting prompt for the confirmation of removal

rm_2

rm -rf [filename|directory]
  • To get a confirmation prompt before removing a file or directory

rm_3

rm -i [filename|directory]
  • To delete all the files and sub-directories recursively of the parent directory

rm_4

rm -r dir

All the files and sub-directories of dir will be deleted

  • Removing more than one file at a time

rm_5

rm x.txt y.txt z.txt

Files will be removed in the same order as in the command.

Conclusion: Remove Directories and Files in Linux Easily

That's all! These were the two commands to remove directories and files in Linux. If you want to get more info about these commands type man rmdir or man rm in the terminal.

Read : 7 Best Download Managers For Linux

If you have any questions please let me know in the comment section. If you found this article helpful, make sure to share it on social media.

Share

Tags:  linuxhow-toterminal
Dinker Thakur

Dinker Thakur is a tech enthusiast who loves to write and read about technology. His area of interest include Artificial intelligence, IT security and consumer electronics.