Page Contents
How to Remove (Delete) Files and Directories in Linux?
If you’re looking for information on ‘How to Remove (Delete) Files and Directories in Linux,’ you’ve come to the right place.
You’ve arrived at the correct location, and I will share an amazing method with you. In Linux, how can you delete (delete) a directory?
This lesson will show you how to delete files and directories in Linux using the rm, unlink, and rmdir commands.
If you try to delete a directory using a command like rmdir and it fails, you may receive a message like “rmdir: ‘dir’: the directory is not empty.”
It would help if you compared the directory name in the picture above with “mydir”: When you run the command, it will delete all files and subdirectories in the directory at the same time.
You may be searching for WinDataReflector Crack
How to Remove Files and Directories In Linux?
If you want to remove (or delete) any file in Linux from the command line, use either the rm
(remove) or unlink
command.
The unlink
the command will make you able to remove only a single file, while rm
you will easily remove multiple files at once.
Note: Be extremely careful while removing files or directories because once you have deleted the file, it cannot be recovered easily.
- To delete a single file, use the
rm
orunlink
a command followed by the file name:unlink filename
rm filename
If the file is already write-protected, you will see a prompted confirmation, as shown below. To remove or delete the file type
y
and hitEnter
. And in case the file is not write-protected, it will be deleted without any prompting notification or warning.
rm: remove write-protected regular empty file 'filename'?- To delete multiple files simultaneously, use the command followed by the file names separated by space.
-
rm filename1 filename2 filename3
There is also a use of wildcards (
*
) and regular expansions to match multiple files. For example, removing all.pdf
For files in the current directory, use the following command:rm *.pdf
When using regular extensions, first list the files with the ls command to see which files will be deleted before running the rm command.
- Use rm with the -i option to validate each file before deleting it:
rm -i filename(s)
- If you want to remove the files without prompting even if the files are write-protected then pass the
-f
(force) option to therm
command:rm -f filename(s)
- You can also combine
rm
options. For example, removing all.txt
files in the current directory without a prompt in verbose mode, use the following command:rm -fv *.txt
How to Remove Directories (Folders)?
In Linux, you will easily remove/delete directories with the rmdir
and rm
.
rmdir is a command-line utility for deleting empty directories, while with rm you can delete directories and their contents recursively.
- To remove an empty directory, use either
rmdir
orrm -d
followed by the directory name:rm -d dirname
rmdir dirname
- If you want to remove non-empty directories and all the files within them, use the
rm
command with the-r
(recursive) option:rm -r dirname
If a directory or file in the directory is write-protected, you will be asked 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
a command followed by the directory names separated by space.rm -r dirname1 dirname2 dirname3
As with files, you can also use a wildcard (*) and regular extensions to match multiple directories.
How to Remove (Delete) Directory in Linux?
On Linux systems, there are different ways to delete directories. If you use Windows file managers like Gnome Files or KDE Dolphin, it will delete files and folders using the system’s graphical user interface.
But you can uninstall directories (folders) from the command line if you are running on a headless server or want to disable multiple directories simultaneously.
An option that will ask you once before deleting.
rm -rI dir1
To remove the directory type y
and hit Enter
.
rm: remove 1 argument recursively? y
You can also use regular extensions to join and remove multiple directories. For example, to remove all top-level directories from the current directory ending with _back, you would use the following command:
rm -r *_bak
Using regular extensions when deleting directories can be risky. It is recommended that you first list the directories with the ls command so that you can see which directories will be removed before running the rm command.
Removing Directories with “find
“
find is a command-line utility that allows you to search for files and directories according to a given expression and perform an action on each corresponding file or directory.
The most common scenario is to use the “search” command to remove model-based directories. For example, to remove all directories ending with _cache in the current working directory, you should run:
find . -type d -name '*_cache' -exec rm -r {} +
Let’s analyze the command above:
- / dir: recherche récursivement dans le répertoire de travail courant (.).
- -type d: restreindre la recherche aux répertoires.
- -name ‘* _cache’ – recherche uniquement les répertoires se terminant par _cache
- -exec: exécute une commande externe avec des arguments facultatifs, dans ce cas, qui est rm -r.
- {} +: Ajoutez les fichiers trouvés à la fin de la commande rm.
Removing All Empty Directories:
To remove all empty directories in a directory tree you would run:
find /dir -type d -empty -delete
Here is an explanation of the options used:
- / dir: searches recursively in the / dir directory.
- -type d: restrict the search to directories.
- -empty: restrict the search to empty directories only.
- -delete: delete all empty directories found in the sub-tree. -delete can only delete empty directories.
Use the -delete option with extreme caution. The search command line is evaluated as an expression, and if you add the -delete option first, the command will remove everything below the start points you specified.
Always try the command first without the -delete option and use -delete as the last option.
/bin/rm: Argument list too long:
This error message appears when you use the rm command to delete a directory containing many files. This happens because the number of files is greater than the system limit for the size of the command line argument.
There are several different solutions to this problem. For example, you can cd to the directory manually or through a loop to remove subdirectories one by one.
The simplest solution is to first delete all the files in the directory using the find command, then delete the directory:
find /dir -type f -delete && rm -r /dir
Conclusion:
At this point, you should have a good understanding of the use of the Linux rm, rmdir, and unlink commands, and you should be able to remove files and directories from the command line safely.
With rm and find you can delete directories based on different criteria quickly and efficiently. Deleting directories is simple, and easy, but you must avoid deleting important data.