LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-29-2017, 07:58 PM   #1
kenw232
Member
 
Registered: May 2006
Posts: 125

Rep: Reputation: 12
How do I find all folder sand remove files under it?


How do I do this? I want to remove all files under /cur in each .Trash folder. It doesn't work.
Code:
find /path -type d -name ".Trash" -exec rm -fr '{}/cur/*' \;

But this does work to at least list the files only in .Trash/cur:
Code:
find /path -type d -name ".Trash" -exec ls -l -a '{}/cur/' \;
 
Old 08-29-2017, 08:52 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
I would probably go along this route using -path instead of -iname.
The find command deals with finding where each /cur is in the Trash folder and then you glob all files in path/cur/* to delete

Code:
# while read i; do rm -r "$i"/*; done < <(find /path -path '*Trash/cur')

while read i; 
do 
  echo rm -r "$i"/*
  # rm -r "$i"/* 
done < <(find /path -path '*Trash/cur')
 
Old 08-29-2017, 09:05 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,787

Rep: Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217
Your first command fails because there is no shell to expand the "*" wildcard. "find ... -exec ..." invokes the given command directly, without involving a shell.

Something like this should work:
Code:
find /path -type d -name ".Trash" -exec sh -c 'rm -fr "{}/cur/"*' \;
I think I got the quoting right in that. You should certainly try it with something less harmful than "rm" though.
 
Old 08-29-2017, 11:27 PM   #4
kenw232
Member
 
Registered: May 2006
Posts: 125

Original Poster
Rep: Reputation: 12
Thanks for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and remove empty directories till Parent folder SOABEE Linux - General 8 08-25-2015 09:17 AM
[SOLVED] Using terminal command -Find files in a folder and copy them to a different folder j-jock Linux - General 4 11-28-2011 02:20 AM
[SOLVED] Bash command to remove all files within a specific folder shayno90 Linux - Newbie 21 10-21-2010 11:55 AM
remove root files from my home folder Levi Gruber Linux - Newbie 3 04-01-2010 09:39 AM
Help with `find -exec` : Remove selected files from a backup folder bobkatz Linux - Server 12 01-29-2010 10:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration