Link to home
Start Free TrialLog in
Avatar of sunhux
sunhux

asked on

Script or method to get "du . -s " with outputs for sizes above certain size & later than certain date


When I'm doing housekeeping, issuing
 " du . -s "   or " du . " often give volumes
of outputs which I have to manually sieve
through.

Any Shell script/method for me to sieve out only
 those outputs of a certain size (which I can
 specify, say 200 kblocks) & later than certain
 date (say last 1 week) ?  Don't want any installed
 software/rpm.

I'm running on HP-Ux  B11.11 & RHES 4.x/5.x

Avatar of Papertrip
Papertrip
Flag of United States of America image

Find all files over 200k blocks:
find . -size +200000b

Open in new window

Find all files modified more than 7 days ago
find . -mtime +7

Open in new window

If you want to delete files that match either of those finds, do this:
find . -size +200000b -print0 |xargs -0 rm -f

Open in new window

or
find . -size +200000b -exec rm -f {} +

Open in new window


The difference between the 2 boils down to, for the most part, how to handle "weird" characters in filenames (spaces fall into that category too.)  This also depends on the versions of find and xargs, I suggest testing both safely.

Try each of those commands but with 'ls -l' instead of 'rm -f' to do your testing to see which, if not both, commands work for the files you are dealing with.
Actually after looking at my syntax again, I see something that might hold you up --

add '-type f' into the find syntax to find files only, so that you don't accidentally delete directories that match your criteria which might have files underneath it that do not match it.  Aside from that, you would get an error from 'rm -f' for those matches anyways so the rm shouldn't rm those directories, but better to be safe than sorry.

Ah yet again, some info that could prove useful to you.

You probably aren't going to be searching for files by blocks used (are you?).  Here are the other suffixes that can be used with '-size'... keep in mind the note at the end regarding if you are really going to search by block size.

 -size n[cwbkMG]
              File uses n units of space.  The following suffixes can be used:

              `b'    for 512-byte blocks (this is the default if no suffix is used)

              `c'    for bytes

              `w'    for two-byte words

              `k'    for Kilobytes (units of 1024 bytes)

              `M'    for Megabytes (units of 1048576 bytes)

              `G'    for Gigabytes (units of 1073741824 bytes)

              The  size  does  not count indirect blocks, but it does count blocks in sparse files that are not actually allocated.
              Bear in mind that the `%k' and `%b' format specifiers of -printf handle sparse files  differently.   The  `b'  suffix
              always denotes 512-byte blocks and never 1 Kilobyte blocks, which is different to the behaviour of -ls.

Open in new window

Now in regards to the time-based need you asked about, there are other options for that as well... atime, ctime, mtime.  Check out the 'find' man page for other modifiers, but mtime which stands for 'modified time' is probably the most common out of the bunch.

Long story short, now that you have an idea of how to do what you want, I recommend using the 'find' man page to fine tune it to your needs :)



Avatar of sunhux
sunhux

ASKER


Can you combine both the criteria (& one more criteria) together in one single command :

list out files ( newer NOT older  than 7 days) of more than 100kBytes & text files only
(don't want binaries as they should be quite static)
Avatar of sunhux

ASKER



Would something like the following work?  Correct my syntax if it's wrong :

find . -mtime -7  -size +100000b -name *.log -exec rm -f {} +

ASKER CERTIFIED SOLUTION
Avatar of Papertrip
Papertrip
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Tintin
find . -type f -mtime -7  -size +100000b -name "*.log" | xargs rm

Open in new window

That syntax is fine (except for the mtime typo) if your files do not contain any characters that should be escaped.

The syntax I posted however will:
find . -size +200000b -print0 |xargs -0 rm -f

Open in new window


That syntax can of course be used in conjunction with the other arguments posted in this thread such as -type  and -name.
>mtime typo

What typo?


Note that HP/UX does not have GNU find.
Avatar of sunhux

ASKER

ok