Linux Delete Folder Recursively
rm command syntax to delete directories recursively
The syntax is as follows:[donotprint]
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | None |
Time | 2m |
[/donotprint] rm -r dirName
Examples
In this example, recursively delete data folder:
rm -r /home/vivek/data/
The specified /home/vivek/data/ will first be emptied of any subdirectories including their subdirectories and files and then data directory removed. The user is prompted for removal of any write-protected files in the directories unless the -f (force) option is given on command line:
rm -r -f /path/to/folder/
To remove a folder whose name starts with a — , for example ‘—dsaatia’, use one of these commands:
See Linux rm(1) command man page or rm command example page for more information.
How to remove files and directories quickly via terminal (bash shell) [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 5 years ago .
From terminal window:
When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders.
If I have a directory nested with files and folders within folders with files and so on, is there any way to delete all the files and folders without all the strenuous command typing?
If it makes a difference, I am using the mac bash shell from terminal, not Microsoft DOS or linux.
4 Answers 4
-r «recursive» -f «force» (suppress confirmation messages)
Would remove everything (folders & files) in the current directory.
But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.
Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .
The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.
And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .
Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.
How can I recursively delete all files of a specific extension in the current directory?
How do I safely delete all files with a specific extension (e.g. .bak ) from current directory and all subfolders using one command-line? Simply, I’m afraid to use rm since I used it wrong once and now I need advice.
7 Answers 7
You don’t even need to use rm in this case if you are afraid. Use find :
But use it with precaution. Run first:
to see exactly which files you will remove.
Also, make sure that -delete is the last argument in your command. If you put it before the -name *.bak argument , it will delete everything.
See man find and man rm for more info and see also this related question on SE:
First run the command shopt -s globstar . You can run that on the command line, and it’ll have effect only in that shell window. You can put it in your .bashrc , and then all newly started shells will pick it up. The effect of that command is to make **/ match files in the current directory and its subdirectories recursively (by default, **/ means the same thing as */ : only in the immediate subdirectories). Then:
(or gvfs-trash **/*.bak or what have you).
Deleting files is for me not something you should use rm for. Here is an alternative:
As Flimm states in the comments:
The package trash-cli does the same thing as gvfs-trash without the dependency on gvfs.
You don’t need to make an alias for this, because the trash-cli package provides a command trash , which does what we want.
As Eliah Kagan makes clear in extensive comments, you can also make this recursive using find . In that case you can’t use an alias, so the commands below assume you have installed trash-cli . I summarise Eliah’s comments:
This command finds and displays all .bak files and symlinks anywhere in the current directory or its subdirectories or below.
To delete them, append an -exec with the trash command:
-xtype f selects files and symlinks to files, but not folders. To delete .bak folders too, remove that part, and use -execdir , which avoids cannot trash non-existent errors for .bak files inside .bak directories:
Recursively delete all files with a given extension [duplicate]
I want to delete all *.o files in a directory and its sub-directories. However, I get an error:
On the other hand, rm *.o works, but it’s not recursive.
2 Answers 2
That is evil: rm -r is not for deleting files but for deleting directories. Luckily there are probably no directories matching *.o .
What you want is possible with zsh but not with sh or bash (new versions of bash can do this, but only if you enable the shell option globstar with shopt -s globstar ). The globbing pattern is **/*.o but that would not be limited to files, too (maybe zsh has tricks for the exclusion of non-files, too).
But this is rather for find :
or (as I am not sure whether -delete is POSIX)
That’s not quite how the -r switch of rm works:
rm has no file searching functionality, its -r switch does not make it descend into local directories and identify files matching the pattern you give it. Instead, the pattern ( *.o ) is expanded by the shell and rm will descend into and remove any directories whose name matches that pattern. If you had a directory whose name ended in .o , then the command you tried would have deleted it, but it won’t find .o files in subdirectories.
What you need to do is either use find :
or, for non-GNU find :
Alternatively, if you are using bash you can enable globstar :
NOTE: all three options will delete directories whose name ends in .o as well, if that’s not what you want, use one of these:
How to loop through a directory recursively to delete files with certain extensions
I need to loop through a directory recursively and remove all files with extension .pdf and .doc . I’m managing to loop through a directory recursively but not managing to filter the files with the above mentioned file extensions.
I need help to complete the code, since I’m not getting anywhere.
15 Answers 15
find is just made for that.
As a followup to mouviciel’s answer, you could also do this as a for loop, instead of using xargs. I often find xargs cumbersome, especially if I need to do something more complicated in each iteration.
As a number of people have commented, this will fail if there are spaces in filenames. You can work around this by temporarily setting the IFS (internal field seperator) to the newline character. This also fails if there are wildcard characters \[?* in the file names. You can work around that by temporarily disabling wildcard expansion (globbing).
If you have newlines in your filenames, then that won’t work either. You’re better off with an xargs based solution:
(The escaped brackets are required here to have the -print0 apply to both or clauses.)
GNU and *BSD find also has a -delete action, which would look like this:
How to Remove Files and Directories Using Linux Command Line
Updated Sep 1, 2019
In this tutorial, we will show you how to use the rm , unlink , and rmdir commands to remove files and directories in Linux.
How to Remove Files #
To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.
The unlink command allows you to remove only a single file, while with rm you can remove multiple files at once.
Be extra careful when removing files or directories, because once the file is deleted, it cannot be easily recovered.
To delete a single file, use the rm or unlink command followed by the file name:
If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y and hit Enter . Otherwise, if the file is not write-protected, it will be deleted without prompting.
To delete multiple files at once, use the rm command followed by the file names separated by space.
You can also use a wildcard ( * ) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:
When using regular expansions, first list the files with the ls command so that you can see what files will be deleted before running the rm command.
Use the rm with the -i option to confirm each file before deleting it:
To remove files without prompting even if the files are write-protected pass the -f (force) option to the rm command:
You can also combine rm options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:
How to Remove Directories (Folders) #
In Linux, you can remove/delete directories with the rmdir and rm .
rmdir is a command-line utility for deleting empty directories while with rm you can remove directories and their contents recursively.
To remove an empty directory, use either rmdir or rm -d followed by the directory name:
To remove non-empty directories and all the files within them, use the rm command with the -r (recursive) option:
If a directory or a file within the directory is write-protected, you will be prompted 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:
To remove multiple directories at once, use the rm -r command followed by the directory names separated by space.
Same as with files you can also use a wildcard ( * ) and regular expansions to match multiple directories.
Conclusion #
By now you should have a good understanding of how to use the Linux rm , rmdir and unlink commands and you should be able to safely remove files and directories from the command line.