Special care should be taken when using the delete action of the find command, using it incorrectly could lead to deleting all the files recursively. Terms and Policy. Buy me a coffee. Follow us! You don't even need to use rm in this case if you are afraid. Use find :. Also, make sure that -delete is the last argument in your command. 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. Deleting files is for me not something you should use rm for. Here is an alternative:. 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. To delete. If you want to delete all files of a certain type, but only 1 folder "deep" from the current folder:.
This will list out the search results that are piped to rm command via xargs. Once you are sure about the list you can drop the echo in above command. Ubuntu Community Ask! Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Learn more. How can I recursively delete all files of a specific extension in the current directory? Ask Question. Asked 8 years, 2 months ago. Active 7 months ago. 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. It is possible that you have to enable globstar option shopt -s globstar.
So for the question the code should look like this:. If you want to do something recursively, I suggest you use recursion yes, you can do it using stacks and so on, but hey.
That's all. The trailing extension ". Option dotglob includes files that start wit a dot hidden files. Beware that before bash 4. There is no reason to pipe the output of find into another utility. The other answers provided will not include files or directories that start with a. I think the most straightforward solution is to use recursion, in the following example, I have printed all the file names in the directory and its subdirectories.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to loop through a directory recursively to delete files with certain extensions Ask Question. Asked 11 years ago. Active 23 days ago. Viewed k times. Improve this question. Elitmiar Elitmiar I know it's bad form to execute code without understanding it, but a lot of people come to this site to learn bash scripting. I got here by googling "bash scripting files recursively", and almost ran one of these answers just to test the recursion without realizing it would delete files.
I know rm is a part of OP's code, but it's not actually relevant to the question asked. I think it'd be safer if answers were phrased using a harmless command like echo. Similar question here: stackoverflow.
Add a comment. Active Oldest Votes. Improve this answer. Gilles 'SO- stop being evil' James Scriven James Scriven 7, 1 1 gold badge 29 29 silver badges 35 35 bronze badges.
0コメント