LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-23-2008, 09:11 AM   #1
jonabyte
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Mint, Debian, Ubuntu Server
Posts: 31

Rep: Reputation: 15
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
 
Old 10-23-2008, 09:26 AM   #2
jonabyte
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Mint, Debian, Ubuntu Server
Posts: 31

Original Poster
Rep: Reputation: 15
I think i found it in another post, using the find command:

find -type f -size -50k -exec rm {} \;
 
Old 10-23-2008, 09:53 AM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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. )
 
Old 10-23-2008, 10:04 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by PTrenholme View Post
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.
 
Old 10-23-2008, 04:37 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
That's the second mistake I've made today. Sorry. I read that as /;, not \;, and confused myself.

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).
 
Old 10-23-2008, 05:36 PM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by PTrenholme View Post
That's the second mistake I've made today. Sorry. I read that as /;, not \;, and confused myself.
No harm, no foul
Quote:
Originally Posted by PTrenholme View Post
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.
 
Old 10-23-2008, 06:18 PM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 10-23-2008 at 06:19 PM.
 
Old 10-24-2008, 09:22 AM   #8
jonabyte
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Mint, Debian, Ubuntu Server
Posts: 31

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
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.
 
Old 10-24-2008, 10:21 AM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by jonabyte View Post
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?

Last edited by PTrenholme; 10-24-2008 at 10:22 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help - delete files older than 45 days but exclude the system files jojothedogboy Linux - Software 3 06-13-2008 03:43 PM
Delete old files script simpi Linux - Newbie 11 04-25-2008 02:37 AM
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 04:56 AM
Delete Files by size LoafOfBread34 Linux - General 3 11-28-2005 11:16 AM
delete files by size Serena Linux - Newbie 2 04-25-2002 07:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:50 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
Open Source Consulting | Domain Registration