LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   script to delete files base don size (http://www.linuxquestions.org/questions/linux-general-1/script-to-delete-files-base-don-size-678554/)

jonabyte 10-23-2008 10:11 AM

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

jonabyte 10-23-2008 10:26 AM

I think i found it in another post, using the find command:

find -type f -size -50k -exec rm {} \;

PTrenholme 10-23-2008 10:53 AM

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 )

pwc101 10-23-2008 11:04 AM

Quote:

Originally Posted by PTrenholme (Post 3319733)
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.

That's not the case on my system:
Code:

14 pwc101@jessie:~>find -type f -size -50k -exec echo rm {}
find: missing argument to `-exec'
15 pwc101@jessie:~> find --version
GNU find version 4.1.20

Quote:

Originally Posted by man find
ACTIONS
-exec command ;
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command
until an argument consisting of ; is encountered. The string
{} is replaced by the current file name being processed
everywhere it occurs in the arguments to the command, not just
in arguments where it is alone, as in some versions of find.
Both of these constructions might need to be escaped (with a
\) or quoted to protect them from expansion by the shell.
The command is executed in the starting directory.

Also, instead of using a \;, you can use a \+ which makes the exec command work on batches of files, instead of single files. See http://www.sunmanagers.org/pipermail...ch/006255.html. You need to escape the ; and + so they don't get interpreted by bash, and are instead passed to find.

PTrenholme 10-23-2008 05:37 PM

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).

pwc101 10-23-2008 06:36 PM

Quote:

Originally Posted by PTrenholme (Post 3320145)
That's the second mistake I've made today. Sorry. :o I read that as /;, not \;, and confused myself. :cry:

No harm, no foul :)
Quote:

Originally Posted by PTrenholme (Post 3320145)
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).

No arguments here!

I always run the command with an echo statement before it:
Code:

find -type f -size -50k -exec echo rm {} \;
That way you get to see exactly what's going to be "typed" into the terminal.

jschiwal 10-23-2008 07:18 PM

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.

jonabyte 10-24-2008 10:22 AM

Quote:

Originally Posted by jschiwal (Post 3320227)
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.

Actually this is going to be run in a folder that is setup for recordings in mp3's so there won't be any of the above issue, thanks.

PTrenholme 10-24-2008 11:21 AM

Quote:

Originally Posted by jonabyte (Post 3320807)
Actually this is going to be run in a folder that is setup for recordings in mp3's so there won't be any of the above issue, thanks.

Remember the basic rule of life is "If it can go wrong, it will go wrong." And these threads are often read by someone other than the original poster, so I, for one, like to point out where a slip of the finger can lead to serious consequences.

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 06:17 AM.