LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Trying to delete all directories within a folder under a set size (https://www.linuxquestions.org/questions/linux-software-2/trying-to-delete-all-directories-within-a-folder-under-a-set-size-4175578592/)

steve51184 04-28-2016 09:29 PM

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

rknichols 04-28-2016 09:55 PM

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.

steve51184 04-28-2016 09:57 PM

That's a little over my head.. Can I get a little help please?

rknichols 04-28-2016 10:11 PM

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.


All times are GMT -5. The time now is 06:51 PM.