Clean up command for folders under 10mb?


Recommended Posts

  • 4 weeks later...

Here is a script that will remove directories within a source directory less than a certain size limit.  I used 100M, but you can change it to any size in MB.  The script can be easily changed to KB, GB, TB etc...  Don't forget to set your source directory.

#!/bin/bash

limit=100M
source="/mnt/user/Movies"

limit=$(echo $limit | cut -d 'M' -f 1)

for dir in "$source"/*/
do
	size=$(du -sBM "$dir" | cut -d 'M' -f 1)

	if (( $limit > $size ))
	then
		echo remove: $dir
		rm -rf "$dir"
	fi
done

removeDirUnderSize.sh

Edited by JoeUnraidUser
Renamed script.
  • Like 2
  • Thanks 1
Link to comment

Hi thanks for providing this, just having a slight issue running it.


I changed the source to a test folder with 4 folders 2 with movies, nfo and jpg and 2 folders with just nfo and jpg and no movies but

limit=100M
source="/mnt/user/Downloads/data/test"

limit=$(echo $limit | cut -d 'M' -f 1)

for dir in "$source"/*/
do
	size=$(du -sBM "$dir" | cut -d 'M' -f 1)

	if (( $limit > $size ))
	then
		echo remove: $dir
		rm -rf "$dir"
	if
done

I get the following error

root@Server:~# /mnt/user/Downloads/data/test/cleanupscript.sh
/mnt/user/Downloads/data/test/cleanupscript.sh: line 3: $'\r': command not found
/mnt/user/Downloads/data/test/cleanupscript.sh: line 5: $'\r': command not found
/mnt/user/Downloads/data/test/cleanupscript.sh: line 7: syntax error near unexpected token `$'do\r''
'mnt/user/Downloads/data/test/cleanupscript.sh: line 7: `do
root@Server:~# 
Link to comment

Ignore me didnt copying it correctly. Missed the #!/bin/bash from the top but was using notepad++ and it still didnt like it.

 

Copied your attached file and modified using notepad.exe and worked perfectly. The 2 folders with nothing but .nfo and .jpg in there were removed and the other to folders with movies remained.

 

Will run on my entire movie folder now to clean it up :)

 

Many thanks

Link to comment

36 folders removed very pleased was expecting a lot more they were mainly duplicates that were removed from plex but the folder remained on share.

 

Is there a way to change it so I can delete folders larger than say 20gb? When I moved to Radarr I didnt set my profiles correctly and got loads of 4k version I dont actually need.

Edited by bally12345
typo
Link to comment

Ended up filtering movies in Plex by Resolution then manually deleted the 4K versions if there is just a single file in the folder plex removes the folder but if there's any extra files like subs or nfo it deletes the movie file and leaves the folder.

This cleanup script has done a brilliant job

Hopefully Radarr will detect the missing 4K movies and download using 1080p profile.

Sent from my SM-G973F using Tapatalk

Link to comment
I have to go out for dinner now, but I will write it up later on this evening.  Are you looking to just delete .mpg and .mkv files or do you  want to delete any file over 20GB?  Do you just want the files removed or the whole directory?
Remove the whole folder, or just delete the mkv/mp4 etc then I can run the cleanup script.

Either way no rush. Just helps to recover some valuable space.

Many thanks

Sent from my SM-G973F using Tapatalk

Link to comment

I know this is overkill, but it only took about 15 minutes.  Here are 3 different scripts for different situations:

 

Remove directories within a source directory over a certain size:

#!/bin/bash

limit=20G
source="/mnt/user/Movies"

limit=$(echo $limit | cut -d 'G' -f 1)

for dir in "$source"/*/
do
	size=$(du -sBG "$dir" | cut -d 'G' -f 1)

	if (( $size > $limit ))
	then
		echo remove: $dir
		rm -rf "$dir"
	fi
done

removeDirOverSize.sh

 

Remove files within a source directory over a certain size:

#!/bin/bash

limit=20G
source="/mnt/user/Movies"

find "$source" -type f -size +$limit -delete -print

removeFilesOverSize.sh

 

Remove files within a source directory over a certain size with specific extensions:

#!/bin/bash

limit=20G
source="/mnt/user/Movies"

find "$source" -regextype posix-extended -iregex '.*\.(mpg|mkv)' -size +$limit -delete -print

To change extensions you could replace mpg|mkv with avi or mpg|mkv|avi etc...

removeFilesOverSizeByExtension.sh

 

Edited by JoeUnraidUser
Changed wording.
  • Like 1
Link to comment
  • 4 months later...
  • 2 years later...

Hi

 

Just found this it's very cool :) and works great.

could this be modified to work recursively, right now i have to set up 8 different script with each their path.

my folder structure is like this.

data/media/movies/

abcd

efgh

..... 

 

so i would like to point it to my top movie folder, could this be done.

 

thanks :)

Link to comment
  • 7 months later...

Is there any way to put a path in and this go thru folders one or two folders deep?  For instance, I use path /mnt/user/Music, but have Artist then Album and I want it to delete Albums that have no file.  Lidarr isn't cleaning them up because they have a 100KB cover art and you script works but will only delete the Artist folder if it's empty.  I have a rather large collection and am hoping I don't have to go folder by folder.

Link to comment
  • 8 months later...
On 5/17/2019 at 4:23 PM, JoeUnraidUser said:

Here is a script that will remove directories within a source directory less than a certain size limit.  I used 100M, but you can change it to any size in MB.  The script can be easily changed to KB, GB, TB etc...  Don't forget to set your source directory.

#!/bin/bash

limit=100M
source="/mnt/user/Movies"

limit=$(echo $limit | cut -d 'M' -f 1)

for dir in "$source"/*/
do
	size=$(du -sBM "$dir" | cut -d 'M' -f 1)

	if (( $limit > $size ))
	then
		echo remove: $dir
		rm -rf "$dir"
	fi
done

removeDirUnderSize.sh 237 B · 30 downloads

Hey,

 

I copy and pasted that, the copy and pasted the directory in that I want to finish with this: 

 

#!/bin/bash

 

limit=10M
source="/mnt/user/Media/Ingress/Compressed"

 

limit=$(echo $limit | cut -d 'M' -f 1)

 

for dir in "$source"/*/
do
    size=$(du -sBM "$dir" | cut -d 'M' -f 1)

 

    if (( $limit > $size ))
    then
        echo remove: $dir
        rm -rf "$dir"
    fi


done

 

 

But I get error 

du: cannot access '/mnt/user/Media/Ingress/Compressed/*/': No such file or directory
/tmp/user.scripts/tmpScripts/Delete empty compressed folders/script: line 12: ((: 10000000 > : syntax error: operand expected (error token is "> ")

 

This is what the folder looks like, I am trying to deleted the folder ".ISdr1c"

 

For more info, this is a temporary folder created by handbrake when it is compressing my files. it deletes it on it's own, but I want to automatically turn off handbrake in the morning and when I do that, it DOES leave those folders there. 

 

Do you know if I need to mofify something in your script to make it work?

 

Thanks in advance! 

Edited by PartyingChair
Link to comment
7 hours ago, PartyingChair said:

Hey,

 

I copy and pasted that, the copy and pasted the directory in that I want to finish with this: 

 

#!/bin/bash

 

limit=10M
source="/mnt/user/Media/Ingress/Compressed"

 

limit=$(echo $limit | cut -d 'M' -f 1)

 

for dir in "$source"/*/
do
    size=$(du -sBM "$dir" | cut -d 'M' -f 1)

 

    if (( $limit > $size ))
    then
        echo remove: $dir
        rm -rf "$dir"
    fi


done

 

 

But I get error 

du: cannot access '/mnt/user/Media/Ingress/Compressed/*/': No such file or directory
/tmp/user.scripts/tmpScripts/Delete empty compressed folders/script: line 12: ((: 10000000 > : syntax error: operand expected (error token is "> ")

 

This is what the folder looks like, I am trying to deleted the folder ".ISdr1c"

 

For more info, this is a temporary folder created by handbrake when it is compressing my files. it deletes it on it's own, but I want to automatically turn off handbrake in the morning and when I do that, it DOES leave those folders there. 

 

Do you know if I need to mofify something in your script to make it work?

 

Thanks in advance! 

".ISdr1c" is a dot file so the script would not see it.  Try substituting the following line with the "." Infront of the "*":

for dir in "$source"/.*/

Link to comment
  • 1 month later...
On 5/17/2019 at 6:23 PM, JoeUnraidUser said:

Here is a script that will remove directories within a source directory less than a certain size limit.  I used 100M, but you can change it to any size in MB.  The script can be easily changed to KB, GB, TB etc...  Don't forget to set your source directory.

#!/bin/bash

limit=100M
source="/mnt/user/Movies"

limit=$(echo $limit | cut -d 'M' -f 1)

for dir in "$source"/*/
do
	size=$(du -sBM "$dir" | cut -d 'M' -f 1)

	if (( $limit > $size ))
	then
		echo remove: $dir
		rm -rf "$dir"
	fi
done

removeDirUnderSize.sh 237 B · 30 downloads

This really helped me recently. Thank you so much!

  • Thanks 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.