LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-28-2016, 10:29 PM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Rep: Reputation: 30
Trying to delete all directories within a folder under a set size


So I have a folder with files and folders in it

I want to delete just the folders and just the ones less then 100mb

And also don't want to delete the main /home/steve/New folder

Here's what I have so far:

Code:
find /home/steve/New/ -size -100M -type d -exec rm -rf {} \;
I doesn't work with the size parameter and without it then deleted all folders including working /home/steve/New folder

Last edited by steve51184; 04-28-2016 at 10:35 PM.
 
Old 04-28-2016, 10:55 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,360

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
You can't do it that way. The reported size of a directory is just the size of the directory file itself (it holds the map of filenames to inode numbers) and does not include the sizes of the files under that directory. The only way to do what you want is to use the du command to see the total space used under each directory and parse the output according to your criteria. Note that parsing the output from du can be challenging if there are any directory names with "odd" characters, like newlines, backspaces, anything special to the shell, etc.
 
Old 04-28-2016, 10:57 PM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 381

Original Poster
Rep: Reputation: 30
That's a little over my head.. Can I get a little help please?
 
Old 04-28-2016, 11:11 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,360

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Quick, untested loop that includes confirmation:
Code:
du -k /home/steve/New | while read Size Dname; do
    if [[ Size < 102400 ]]; then
        read -p "OK to remove '$Dname\` ? "
        [[ $REPLY = y ]] && rm -r "$Dname"
    fi
done
The possibility of odd characters in Dname is left as an exercise. If these are just your own, sanely named directories, you probably don't have to worry about it.

Last edited by rknichols; 04-28-2016 at 11:13 PM.
 
  


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
[SOLVED] Old Timeshift Folder won't Delete from Trash Folder. gael33 Linux - Newbie 2 09-04-2015 11:39 AM
Find the directories which is older than x days delete and zip those directories ramesh pagadala Linux - Newbie 2 08-29-2013 09:17 AM
[SOLVED] how to set different permission for sub directories of a samba shared folder golden_boy615 Linux - Server 10 12-16-2011 07:54 PM
Is it possible to set a maximum folder size? KDE bglnelissen General 1 03-20-2005 08:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration