script to delete files base don size
Hi,
I am trying to write a script that will delete files in a folder under a certain size say 50kb. I haven't had much luck in finding any solutions. Thanks |
I think i found it in another post, using the find command:
find -type f -size -50k -exec rm {} \; |
If you replace the -exec... with a -print, you'll get a list of the files you'll remove before you actually do so. This sometimes avoids later anguish, eh? ;)
By the way, the slash (/;) is redundant, since it's just telling bash to continue the command to the next line, which is then specified as empty. If it had been intended to run find with a search of the whole file system, the command should be find / -type f -size -50k -exec rm {}. You could use a period (.) (or nothing as in the example you posted) for your current directory, and ~/ for your home directory. Doing it for the whole file system could, potentially, remove necessary system files. (Especially if you're running the command with "root" privileges.:D ) |
Quote:
Code:
14 pwc101@jessie:~>find -type f -size -50k -exec echo rm {} Quote:
|
That's the second mistake I've made today. Sorry. :o I read that as /;, not \;, and confused myself. :cry:
I do, however, think my point about using -print first may be helpful, as may also be my comment that the command, as written, will run in the current working directory (and all its children). |
Quote:
Quote:
I always run the command with an echo statement before it: Code:
find -type f -size -50k -exec echo rm {} \; |
It may be advisable to not use the root directory for doing things like this. You can have multiple directories in the find command:
find /srv/samba/documents /home/username/ /tmp -type f -size -50k | less -print is the default action. I'll pipe the output into less, examine the results and if they look OK. Hit the up arrow and add "-execdir rm '{}' \;" to the previous find command. Removing 50k files using the root dir. (/) will wipe out practically all of the /etc/ directory and make the system unusable. Even performing backups, you don't want to include /proc, /sys, /dev, /mnt and /tmp. |
Quote:
|
Quote:
Of course, the obverse of the basic rule that "If it didn't go wrong, it couldn't have gone wrong," never convinced my mother to stay off my case. Ah well, mothers, what can you say? :D |
All times are GMT -5. The time now is 11:09 AM. |