In Linux, you can easily remove the directory or file if you’re using GUI. If you don’t have access to the GUI and have command line access still it’s pretty simple to remove directories and files using simple commands.
In Linux everything is a file including the directories, A directory is nothing just a group of files in Linux. There are two commands in Linux to delete files and directories
rmdir and these commands are also available on Unix-like operating systems. The
rmdir commands delete files and folders permanently and they cannot be recovered unless you have a backup. You can also delete files and directories in Windows using,
Remove Directories or Files in Linux
There are four commands to remove directories (directory) or files in Linux:
- rm – This command removes files including sub-directories.
- rmdir – This command deletes the specified directories and folders in Linux
- unlink – This command can only remove files excluding directories.
- find –
rmdir commands remove directories and files permanently without sending them to Trash first. Be careful while executing these commands.
1. How to Remove Files Using rm
The simplest way of removing a file in directories using the
Remove Directory in Linux
To remove a directory in Linux we use the -r or –recursive option in order to remove the directory, and this will delete all the subdirectories as well.
rm -r directory
Remove Empty Directories
If you just want to remove empty directories you can use -d parameter and this will delete all the specified empty directories.
rm -d directory/*
Removing Files and Directories Interactively
To remove files and directories interactively we use -i or –interactive option, So it will ask for confirmation before deleting any files or folders.
rm -r -i directories
What Files and Folders are Being Deleted
The rm command deletes files and folders without giving output in the terminal with -v or –verbose option you can see the output log of what files and folders are being deleted.
rm -r -v directories
2. How to Remove Directories Using rmdir
Another command you can use is rmdir which can remove or delete directories, rmdir only deletes directories, unlike rm which can delete both files and directories, and rmdir only delete empty directories.
To remove a single directory you can use the following command:
To remove multiple directories you can use the following command:
rmdir directory_1 directory_2
Remove Parent Directories
If you have multiple parent directories, you can use -p parameter to delete them as well.
To remove parent directories you can use the following command:
rmdir -p directory/*
3. How to Remove Files Using unlink
We can also use
unlink command to remove or delete files, but it only removes a single file but with the
rm you can remove multiple files at once.
4. How to Remove Files and Directories Using find
Sometimes it’s convenient to use find a tool that works great and if you’re a DevOps expert or Linux expert then you might come across this multiple times.
To remove a file through find:
If you want to remove all the “sample.txt” files you can use the -delete option with find.
find directory_here -name 'sample.txt' -delete
You can replace directory_here with your directory name or path to remove a specific file through that directory. Or you can use ‘.’ dot which means that the find command will delete sample.txt from everywhere.